diff --git a/tddb/td/db/RocksDb.cpp b/tddb/td/db/RocksDb.cpp index f1aa64a5..803e18c4 100644 --- a/tddb/td/db/RocksDb.cpp +++ b/tddb/td/db/RocksDb.cpp @@ -24,6 +24,7 @@ #include "rocksdb/write_batch.h" #include "rocksdb/utilities/optimistic_transaction_db.h" #include "rocksdb/utilities/transaction.h" +#include "rocksdb/filter_policy.h" namespace td { namespace { @@ -75,6 +76,15 @@ Result RocksDb::open(std::string path, RocksDbOptions options) { } else { table_options.block_cache = options.block_cache; } + if (options.enable_bloom_filter) { + table_options.filter_policy.reset(rocksdb::NewBloomFilterPolicy(10, false)); + if (options.two_level_index_and_filter) { + table_options.index_type = rocksdb::BlockBasedTableOptions::IndexType::kTwoLevelIndexSearch; + table_options.partition_filters = true; + table_options.cache_index_and_filter_blocks = true; + table_options.pin_l0_filter_and_index_blocks_in_cache = true; + } + } db_options.table_factory.reset(rocksdb::NewBlockBasedTableFactory(table_options)); db_options.use_direct_reads = options.use_direct_reads; diff --git a/tddb/td/db/RocksDb.h b/tddb/td/db/RocksDb.h index 499a3328..b0ae0659 100644 --- a/tddb/td/db/RocksDb.h +++ b/tddb/td/db/RocksDb.h @@ -63,6 +63,8 @@ struct RocksDbOptions { std::shared_ptr snapshot_statistics = nullptr; bool use_direct_reads = false; bool no_block_cache = false; + bool enable_bloom_filter = false; + bool two_level_index_and_filter = false; }; class RocksDb : public KeyValue { diff --git a/validator-engine/validator-engine.cpp b/validator-engine/validator-engine.cpp index 2ea04e18..247bf8e2 100644 --- a/validator-engine/validator-engine.cpp +++ b/validator-engine/validator-engine.cpp @@ -1466,6 +1466,7 @@ td::Status ValidatorEngine::load_global_config() { } validator_options_.write().set_celldb_compress_depth(celldb_compress_depth_); validator_options_.write().set_celldb_in_memory(celldb_in_memory_); + validator_options_.write().set_celldb_disable_bloom_filter(celldb_disable_bloom_filter_); validator_options_.write().set_max_open_archive_files(max_open_archive_files_); validator_options_.write().set_archive_preload_period(archive_preload_period_); validator_options_.write().set_disable_rocksdb_stats(disable_rocksdb_stats_); @@ -4526,6 +4527,12 @@ int main(int argc, char *argv[]) { [&]() { acts.push_back([&x]() { td::actor::send_closure(x, &ValidatorEngine::set_celldb_in_memory, true); }); }); + p.add_option( + '\0', "celldb-disable-bloom-filter", + "disable using bloom filter in CellDb. Enabled bloom filter reduces read latency, but increases memory usage", + [&]() { + acts.push_back([&x]() { td::actor::send_closure(x, &ValidatorEngine::set_celldb_disable_bloom_filter, true); }); + }); p.add_checked_option( '\0', "catchain-max-block-delay", "delay before creating a new catchain block, in seconds (default: 0.4)", [&](td::Slice s) -> td::Status { diff --git a/validator-engine/validator-engine.hpp b/validator-engine/validator-engine.hpp index e0dc91f1..fa3ba413 100644 --- a/validator-engine/validator-engine.hpp +++ b/validator-engine/validator-engine.hpp @@ -218,6 +218,7 @@ class ValidatorEngine : public td::actor::Actor { bool celldb_direct_io_ = false; bool celldb_preload_all_ = false; bool celldb_in_memory_ = false; + bool celldb_disable_bloom_filter_ = false; td::optional catchain_max_block_delay_, catchain_max_block_delay_slow_; bool read_config_ = false; bool started_keyring_ = false; @@ -311,6 +312,9 @@ class ValidatorEngine : public td::actor::Actor { void set_celldb_in_memory(bool value) { celldb_in_memory_ = value; } + void set_celldb_disable_bloom_filter(bool value) { + celldb_disable_bloom_filter_ = value; + } void set_catchain_max_block_delay(double value) { catchain_max_block_delay_ = value; } diff --git a/validator/db/celldb.cpp b/validator/db/celldb.cpp index e86a373d..4bec7198 100644 --- a/validator/db/celldb.cpp +++ b/validator/db/celldb.cpp @@ -96,11 +96,17 @@ void CellDbIn::start_up() { db_options.snapshot_statistics = snapshot_statistics_; } db_options.statistics = statistics_; + db_options.use_direct_reads = opts_->get_celldb_direct_io(); + db_options.enable_bloom_filter = !opts_->get_celldb_disable_bloom_filter(); + db_options.two_level_index_and_filter = db_options.enable_bloom_filter + && opts_->state_ttl() >= 60 * 60 * 24 * 30; // 30 days if (opts_->get_celldb_cache_size()) { db_options.block_cache = td::RocksDb::create_cache(opts_->get_celldb_cache_size().value()); LOG(WARNING) << "Set CellDb block cache size to " << td::format::as_size(opts_->get_celldb_cache_size().value()); + } else if (db_options.two_level_index_and_filter && !opts_->get_celldb_in_memory()) { + db_options.block_cache = td::RocksDb::create_cache(16ULL << 30); + LOG(WARNING) << "Set CellDb block cache size to 16GB"; } - db_options.use_direct_reads = opts_->get_celldb_direct_io(); if (opts_->get_celldb_in_memory()) { td::RocksDbOptions read_db_options; diff --git a/validator/validator-options.hpp b/validator/validator-options.hpp index ace6b106..0baa791d 100644 --- a/validator/validator-options.hpp +++ b/validator/validator-options.hpp @@ -139,6 +139,9 @@ struct ValidatorManagerOptionsImpl : public ValidatorManagerOptions { bool get_celldb_in_memory() const override { return celldb_in_memory_; } + bool get_celldb_disable_bloom_filter() const override { + return celldb_disable_bloom_filter_; + } td::optional get_catchain_max_block_delay() const override { return catchain_max_block_delay_; } @@ -237,6 +240,9 @@ struct ValidatorManagerOptionsImpl : public ValidatorManagerOptions { void set_celldb_in_memory(bool value) override { celldb_in_memory_ = value; } + void set_celldb_disable_bloom_filter(bool value) override { + celldb_disable_bloom_filter_ = value; + } void set_catchain_max_block_delay(double value) override { catchain_max_block_delay_ = value; } @@ -304,6 +310,7 @@ struct ValidatorManagerOptionsImpl : public ValidatorManagerOptions { bool celldb_direct_io_ = false; bool celldb_preload_all_ = false; bool celldb_in_memory_ = false; + bool celldb_disable_bloom_filter_ = false; td::optional catchain_max_block_delay_, catchain_max_block_delay_slow_; bool state_serializer_enabled_ = true; td::Ref collator_options_{true}; diff --git a/validator/validator.h b/validator/validator.h index 5d6c0173..108cc835 100644 --- a/validator/validator.h +++ b/validator/validator.h @@ -111,6 +111,7 @@ struct ValidatorManagerOptions : public td::CntObject { virtual td::optional get_celldb_cache_size() const = 0; virtual bool get_celldb_direct_io() const = 0; virtual bool get_celldb_preload_all() const = 0; + virtual bool get_celldb_disable_bloom_filter() const = 0; virtual td::optional get_catchain_max_block_delay() const = 0; virtual td::optional get_catchain_max_block_delay_slow() const = 0; virtual bool get_state_serializer_enabled() const = 0; @@ -144,6 +145,7 @@ struct ValidatorManagerOptions : public td::CntObject { virtual void set_celldb_direct_io(bool value) = 0; virtual void set_celldb_preload_all(bool value) = 0; virtual void set_celldb_in_memory(bool value) = 0; + virtual void set_celldb_disable_bloom_filter(bool value) = 0; virtual void set_catchain_max_block_delay(double value) = 0; virtual void set_catchain_max_block_delay_slow(double value) = 0; virtual void set_state_serializer_enabled(bool value) = 0;