Skip to content

Commit 72a31b8

Browse files
author
caijieming
committed
issue=1258, Tcache support block-level cache evict
1 parent f15ac00 commit 72a31b8

File tree

10 files changed

+1372
-38
lines changed

10 files changed

+1372
-38
lines changed

src/io/tablet_io.cc

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,18 +1668,25 @@ void TabletIO::SetupOptionsForLG() {
16681668
lg_info->env = LeveldbMockEnv();
16691669
} else if (store == MemoryStore) {
16701670
if (FLAGS_tera_use_flash_for_memenv) {
1671-
lg_info->env = LeveldbFlashEnv();
1671+
if (FLAGS_tera_tabletnode_block_cache_enabled) {
1672+
LOG(INFO) << "MemLG[" << lg_i << "] activate TCache";
1673+
lg_info->env = io::DefaultBlockCacheEnv();
1674+
} else {
1675+
lg_info->env = LeveldbFlashEnv();
1676+
}
16721677
} else {
16731678
lg_info->env = LeveldbMemEnv();
16741679
}
16751680
lg_info->seek_latency = 0;
16761681
lg_info->block_cache = m_memory_cache;
16771682
} else if (store == FlashStore) {
1678-
if (!FLAGS_tera_tabletnode_cache_enabled) {
1679-
lg_info->env = LeveldbFlashEnv();
1683+
if (FLAGS_tera_tabletnode_block_cache_enabled) {
1684+
//LOG(INFO) << "activate block-level Cache store";
1685+
//lg_info->env = leveldb::EnvThreeLevelCache();
1686+
LOG(INFO) << "FlashLG[" << lg_i << "] activate TCache";
1687+
lg_info->env = io::DefaultBlockCacheEnv();
16801688
} else {
1681-
LOG(INFO) << "activate block-level Cache store";
1682-
lg_info->env = leveldb::EnvThreeLevelCache();
1689+
lg_info->env = LeveldbFlashEnv();
16831690
}
16841691
lg_info->seek_latency = FLAGS_tera_leveldb_env_local_seek_latency;
16851692
} else {

src/io/utils_leveldb.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "leveldb/comparator.h"
1919
#include "leveldb/env_dfs.h"
2020
#include "leveldb/env_flash.h"
21+
#include "leveldb/block_cache.h"
2122
#include "leveldb/env_inmem.h"
2223
#include "leveldb/env_mock.h"
2324
#include "leveldb/table_utils.h"
@@ -31,6 +32,7 @@ DECLARE_string(tera_leveldb_env_hdfs2_nameservice_list);
3132
DECLARE_string(tera_tabletnode_path_prefix);
3233
DECLARE_string(tera_dfs_so_path);
3334
DECLARE_string(tera_dfs_conf);
35+
DECLARE_int32(tera_leveldb_block_cache_env_num_thread);
3436

3537
namespace tera {
3638
namespace io {
@@ -66,6 +68,20 @@ leveldb::Env* LeveldbBaseEnv() {
6668
}
6769
}
6870

71+
// Tcache: default env
72+
static pthread_once_t block_cache_once = PTHREAD_ONCE_INIT;
73+
static Env* default_block_cache_env;
74+
static void InitDefaultBlockCacheEnv() {
75+
default_block_cache_env = new BlockCacheEnv(leveldbBaseEnv);
76+
default_block_cache_env->SetBackgroundThreads(FLAGS_tera_leveldb_block_cache_env_num_thread);
77+
}
78+
79+
leveldb::Env* DefaultBlockCacheEnv() {
80+
pthread_once(&block_cache_once, InitDefaultBlockCacheEnv);
81+
return default_block_cache_env;
82+
}
83+
84+
// mem env
6985
leveldb::Env* LeveldbMemEnv() {
7086
static Mutex mutex;
7187
static leveldb::Env* mem_env = NULL;
@@ -78,6 +94,7 @@ leveldb::Env* LeveldbMemEnv() {
7894
return mem_env;
7995
}
8096

97+
// flash env
8198
leveldb::Env* LeveldbFlashEnv() {
8299
static Mutex mutex;
83100
static leveldb::Env* flash_env = NULL;

src/io/utils_leveldb.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ void InitDfsEnv();
1818
// return the base env leveldb used (dfs/local), singleton
1919
leveldb::Env* LeveldbBaseEnv();
2020

21+
leveldb::Env* DefaultBlockCacheEnv(); // ssd + base
22+
2123
// return the mem env leveldb used, singleton
2224
leveldb::Env* LeveldbMemEnv();
2325

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Copyright (c) 2015, Baidu.com, Inc. All Rights Reserved
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef STOREAGE_LEVELDB_UTIL_BLOCK_CACHE_H
6+
#define STOREAGE_LEVELDB_UTIL_BLOCK_CACHE_H
7+
#include "leveldb/status.h"
8+
#include "leveldb/options.h"
9+
10+
namespace leveldb {
11+
/////////////////////////////////////////////
12+
// Tcache
13+
/////////////////////////////////////////////
14+
uint64_t kBlockSize = 8192UL;
15+
uint64_t kDataSetSize = 134217728UL;
16+
uint64_t kFidBatchNum = 200000UL;
17+
uint64_t kCacheSize = 350000000000UL;
18+
uint64_t kMetaBlockSize = 2000UL;
19+
uint64_t kMetaTableSize = 500UL;
20+
uint64_t kWriteBufferSize = 1048576UL;
21+
22+
struct BlockCacheOptions {
23+
Options opts;
24+
std::string cache_dir;
25+
uint64_t block_size;
26+
uint64_t dataset_size;
27+
uint64_t fid_batch_num;
28+
uint64_t cache_size;
29+
uint64_t dataset_num;
30+
uint64_t meta_block_cache_size;
31+
uint64_t meta_table_cache_size;
32+
uint64_t write_buffer_size;
33+
Env* env;
34+
35+
BlockCacheOptions()
36+
: block_size(kBlockSize),
37+
dataset_size(kDataSetSize),
38+
fid_batch_num(kFidBatchNum),
39+
cache_size(kCacheSize),
40+
meta_block_cache_size(kMetaBlockSize),
41+
meta_table_cache_size(kMetaTableSize),
42+
write_buffer_size(kWriteBufferSize),
43+
env(NULL) {
44+
dataset_num = cache_size / dataset_size + 1;
45+
}
46+
};
47+
48+
class BlockCacheWritableFile;
49+
class BlockCacheRandomAccessFile;
50+
class BlockCacheImpl;
51+
52+
class BlockCacheEnv : public EnvWrapper {
53+
public:
54+
BlockCacheEnv(Env* base);
55+
56+
~BlockCacheEnv();
57+
58+
virtual Status FileExists(const std::string& fname);
59+
60+
virtual Status GetChildren(const std::string& path,
61+
std::vector<std::string>* result);
62+
63+
virtual Status DeleteFile(const std::string& fname);
64+
65+
virtual Status CreateDir(const std::string& name);
66+
67+
virtual Status DeleteDir(const std::string& name);
68+
69+
virtual Status CopyFile(const std::string& from,
70+
const std::string& to);
71+
72+
virtual Status GetFileSize(const std::string& fname, uint64_t* size);
73+
74+
virtual Status RenameFile(const std::string& src, const std::string& target);
75+
76+
virtual Status LockFile(const std::string& fname, FileLock** lock);
77+
78+
virtual Status UnlockFile(FileLock* lock);
79+
80+
virtual Status NewSequentialFile(const std::string& fname,
81+
SequentialFile** result); // never cache log
82+
83+
// cache relatively
84+
virtual Status NewRandomAccessFile(const std::string& fname,
85+
RandomAccessFile** result); // cache Pread
86+
87+
virtual Status NewWritableFile(const std::string& fname,
88+
WritableFile** result); // cache Append
89+
Status LoadCache(const BlockCacheOptions& opts, const std::string& cache_dir);
90+
91+
private:
92+
std::vector<BlockCacheImpl*> cache_vec_;
93+
Env* dfs_env_;
94+
};
95+
96+
Env* NewBlockCacheEnv(Env* base);
97+
98+
} // leveldb
99+

src/leveldb/include/leveldb/cache.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ class Cache;
3131

3232
// Create a new cache with a fixed size capacity. This implementation
3333
// of Cache uses a least-recently-used eviction policy.
34-
extern Cache* NewLRUCache(size_t capacity);
34+
enum LRUCacheType {
35+
kLRUCacheDefault = 0,
36+
kLRUCache2Q = 1,
37+
};
38+
extern Cache* NewLRUCache(size_t capacityint, LRUCacheType type = kLRUCacheDefault);
3539

3640
class Cache {
3741
public:

0 commit comments

Comments
 (0)