Skip to content

Commit

Permalink
Metadata and subkey column family share a singleblock cache (apache#365)
Browse files Browse the repository at this point in the history
  • Loading branch information
caipengbo authored and ShooterIT committed Nov 2, 2021
1 parent 9ccea95 commit 7a1d958
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
7 changes: 7 additions & 0 deletions kvrocks.conf
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,13 @@ rocksdb.metadata_block_cache_size 2048
# Default: 2048MB
rocksdb.subkey_block_cache_size 2048

# Metadata column family and subkey column family will share a single block cache
# if set 'yes'. The capacity of shared block cache is
# metadata_block_cache_size + subkey_block_cache_size
#
# Default: no
rocksdb.share_metadata_and_subkey_block_cache no

# Number of open files that can be used by the DB. You may need to
# increase this if your database has a large working set. Value -1 means
# files opened are always kept open. You can estimate number of files based
Expand Down
2 changes: 2 additions & 0 deletions src/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ Config::Config() {
{"rocksdb.cache_index_and_filter_blocks", true, new YesNoField(&RocksDB.cache_index_and_filter_blocks, false)},
{"rocksdb.subkey_block_cache_size", true, new IntField(&RocksDB.subkey_block_cache_size, 2048, 0, INT_MAX)},
{"rocksdb.metadata_block_cache_size", true, new IntField(&RocksDB.metadata_block_cache_size, 2048, 0, INT_MAX)},
{"rocksdb.share_metadata_and_subkey_block_cache",
true, new YesNoField(&RocksDB.share_metadata_and_subkey_block_cache, false)},
{"rocksdb.compaction_readahead_size", false, new IntField(&RocksDB.compaction_readahead_size, 2*MiB, 0, 64*MiB)},
{"rocksdb.level0_slowdown_writes_trigger",
false, new IntField(&RocksDB.level0_slowdown_writes_trigger, 20, 1, 1024)},
Expand Down
1 change: 1 addition & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ struct Config{
bool cache_index_and_filter_blocks;
int metadata_block_cache_size;
int subkey_block_cache_size;
bool share_metadata_and_subkey_block_cache;
int max_open_files;
int write_buffer_size;
int max_write_buffer_number;
Expand Down
15 changes: 13 additions & 2 deletions src/storage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,25 @@ Status Storage::Open(bool read_only) {
size_t block_size = static_cast<size_t>(config_->RocksDB.block_size);
size_t metadata_block_cache_size = config_->RocksDB.metadata_block_cache_size*MiB;
size_t subkey_block_cache_size = config_->RocksDB.subkey_block_cache_size*MiB;

rocksdb::Options options;
InitOptions(&options);
CreateColumnFamilies(options);

std::shared_ptr<rocksdb::Cache> shared_block_cache;
if (config_->RocksDB.share_metadata_and_subkey_block_cache) {
size_t shared_block_cache_size = metadata_block_cache_size + subkey_block_cache_size;
shared_block_cache = rocksdb::NewLRUCache(shared_block_cache_size, -1, false, 0.75);
}

rocksdb::BlockBasedTableOptions metadata_table_opts;
metadata_table_opts.filter_policy.reset(rocksdb::NewBloomFilterPolicy(10, true));
metadata_table_opts.block_cache = rocksdb::NewLRUCache(metadata_block_cache_size, -1, false, 0.75);
metadata_table_opts.block_cache = shared_block_cache ?
shared_block_cache : rocksdb::NewLRUCache(metadata_block_cache_size, -1, false, 0.75);
metadata_table_opts.cache_index_and_filter_blocks = cache_index_and_filter_blocks;
metadata_table_opts.cache_index_and_filter_blocks_with_high_priority = true;
metadata_table_opts.block_size = block_size;

rocksdb::ColumnFamilyOptions metadata_opts(options);
metadata_opts.table_factory.reset(rocksdb::NewBlockBasedTableFactory(metadata_table_opts));
metadata_opts.compaction_filter_factory = std::make_shared<MetadataFilterFactory>(this);
Expand All @@ -171,7 +181,8 @@ Status Storage::Open(bool read_only) {

rocksdb::BlockBasedTableOptions subkey_table_opts;
subkey_table_opts.filter_policy.reset(rocksdb::NewBloomFilterPolicy(10, true));
subkey_table_opts.block_cache = rocksdb::NewLRUCache(subkey_block_cache_size, -1, false, 0.75);
subkey_table_opts.block_cache = shared_block_cache ?
shared_block_cache : rocksdb::NewLRUCache(subkey_block_cache_size, -1, false, 0.75);
subkey_table_opts.cache_index_and_filter_blocks = cache_index_and_filter_blocks;
subkey_table_opts.cache_index_and_filter_blocks_with_high_priority = true;
subkey_table_opts.block_size = block_size;
Expand Down

0 comments on commit 7a1d958

Please sign in to comment.