From 0211d6ff3fdbe75f8d088f7a143a3e40df6d8c73 Mon Sep 17 00:00:00 2001 From: jianguo Date: Tue, 22 Oct 2024 17:47:31 +0800 Subject: [PATCH 1/2] update --- validator/db/celldb.cpp | 37 ++++++++++++++++++++++++++++++++----- validator/db/celldb.hpp | 14 +++++++++++--- validator/db/rootdb.cpp | 31 +++++++++++++++++++++++++++++-- validator/db/rootdb.hpp | 1 + 4 files changed, 73 insertions(+), 10 deletions(-) diff --git a/validator/db/celldb.cpp b/validator/db/celldb.cpp index 1701ae588..b1293bf0f 100644 --- a/validator/db/celldb.cpp +++ b/validator/db/celldb.cpp @@ -66,7 +66,11 @@ void CellDbBase::execute_sync(std::function f) { CellDbIn::CellDbIn(td::actor::ActorId root_db, td::actor::ActorId parent, std::string path, td::Ref opts) - : root_db_(root_db), parent_(parent), path_(std::move(path)), opts_(opts) { + : root_db_(root_db), parent_(parent), path_(std::move(path)), opts_(opts), std::shared_ptr cell_db{ + if (cell_db != NULL) { + LOG(INFO) << "zjg: using provided KeyValue"; + cell_db_ = cell_db; + } } void CellDbIn::start_up() { @@ -97,8 +101,10 @@ void CellDbIn::start_up() { LOG(WARNING) << "Set CellDb block cache size to " << td::format::as_size(opts_->get_celldb_cache_size().value()); } db_options.use_direct_reads = opts_->get_celldb_direct_io(); - cell_db_ = std::make_shared(td::RocksDb::open(path_, std::move(db_options)).move_as_ok()); - + if (cell_db_ == NULL){ + LOG(INFO) << "zjg: using new KeyValue"; + cell_db_ = std::make_shared(td::RocksDb::open(path_, std::move(db_options)).move_as_ok()); + } boc_ = vm::DynamicBagOfCellsDb::create(); boc_->set_celldb_compress_depth(opts_->get_celldb_compress_depth()); @@ -438,12 +444,28 @@ void CellDbIn::migrate_cells() { } } +int GetRandom(){ + std::random_device rd; + std::mt19937 gen(rd()); + std::uniform_int_distribution<> distr(0, THREAD_COUNTS -1 ); + int random_number = distr(gen); + return random_number; +} + void CellDb::load_cell(RootHash hash, td::Promise> promise) { + int ranNum = GetRandom(); + static int64_t ranCount = 0; + ranCount++; + if (ranCount % 1000 == 0) { + LOG(ERROR) << "yus " << this->get_name() << " " << this->get_actor_info_ptr()->mailbox().reader().calc_size() << ", ranNum: " << ranNum; + // LOG(ERROR) << "yus " << this->get_name() << " " << this->get_actor_info_ptr()->mailbox().reader().calc_size(); + ranCount = 0; + } if (!started_) { - td::actor::send_closure(cell_db_, &CellDbIn::load_cell, hash, std::move(promise)); + td::actor::send_closure(cell_db_read_[ranNum], &CellDbIn::load_cell, hash, std::move(promise)); } else { auto P = td::PromiseCreator::lambda( - [cell_db_in = cell_db_.get(), hash, promise = std::move(promise)](td::Result> R) mutable { + [cell_db_in = cell_db_read_[ranNum].get(), hash, promise = std::move(promise)](td::Result> R) mutable { if (R.is_error()) { td::actor::send_closure(cell_db_in, &CellDbIn::load_cell, hash, std::move(promise)); } else { @@ -471,6 +493,11 @@ void CellDb::start_up() { boc_ = vm::DynamicBagOfCellsDb::create(); boc_->set_celldb_compress_depth(opts_->get_celldb_compress_depth()); cell_db_ = td::actor::create_actor("celldbin", root_db_, actor_id(this), path_, opts_); + + // cell_db_ = td::actor::create_actor("celldbin", root_db_, actor_id(this), path_, opts_, rocks_db_); + for (int i = 0; i < THREAD_COUNTS; i++) { + cell_db_read_[i] = td::actor::create_actor("celldbin", root_db_, actor_id(this), path_, opts_, rocks_db_); + } on_load_callback_ = [actor = std::make_shared>( td::actor::create_actor("celldbmigration", cell_db_.get())), compress_depth = opts_->get_celldb_compress_depth()](const vm::CellLoader::LoadResult& res) { diff --git a/validator/db/celldb.hpp b/validator/db/celldb.hpp index 335d8a08e..69810b80b 100644 --- a/validator/db/celldb.hpp +++ b/validator/db/celldb.hpp @@ -28,6 +28,8 @@ #include "validator.h" #include "db-utils.h" #include "td/db/RocksDb.h" +#include +#include namespace rocksdb { class Statistics; @@ -35,6 +37,10 @@ class Statistics; namespace ton { +const int THREAD_COUNTS = 20; + +int GetRandom(); + namespace validator { class RootDb; @@ -68,7 +74,7 @@ class CellDbIn : public CellDbBase { void flush_db_stats(); CellDbIn(td::actor::ActorId root_db, td::actor::ActorId parent, std::string path, - td::Ref opts); + td::Ref opts, std::shared_ptr cell_db, std::shared_ptr); void start_up() override; void alarm() override; @@ -171,8 +177,8 @@ class CellDb : public CellDbBase { void get_cell_db_reader(td::Promise> promise); void get_last_deleted_mc_state(td::Promise promise); - CellDb(td::actor::ActorId root_db, std::string path, td::Ref opts) - : root_db_(root_db), path_(path), opts_(opts) { + CellDb(td::actor::ActorId root_db, std::string path, td::Ref opts, std::shared_ptr rocks_db) + : root_db_(root_db), path_(path), opts_(opts), rocks_db_(rocks_db) { } void start_up() override; @@ -183,6 +189,8 @@ class CellDb : public CellDbBase { td::Ref opts_; td::actor::ActorOwn cell_db_; + td::actor::ActorOwn cell_db_read_[THREAD_COUNTS]; + std::shared_ptr rocks_db_; std::unique_ptr boc_; bool started_ = false; diff --git a/validator/db/rootdb.cpp b/validator/db/rootdb.cpp index 3071f565d..f7bc2b423 100644 --- a/validator/db/rootdb.cpp +++ b/validator/db/rootdb.cpp @@ -264,7 +264,16 @@ void RootDb::get_block_state(ConstBlockHandle handle, td::Promisestate(), std::move(P)); + + int ranNum = GetRandom(); + static int64_t ranCount = 0; + ranCount++; + if (ranCount % 1000 == 0) { + LOG(ERROR) << "yus " << this->get_name() << " " << this->get_actor_info_ptr()->mailbox().reader().calc_size() << ", ranNum: " << ranNum; + ranCount = 0; + } + + td::actor::send_closure(cell_db_read_[ranNum], &CellDb::load_cell, handle->state(), std::move(P)); } else { promise.set_error(td::Status::Error(ErrorCode::notready, "state not in db")); } @@ -407,7 +416,25 @@ void RootDb::get_hardforks(td::Promise> promise) { } void RootDb::start_up() { - cell_db_ = td::actor::create_actor("celldb", actor_id(this), root_path_ + "/celldb/", opts_); + td::RocksDbOptions db_options; + auto statistics_ = td::RocksDb::create_statistics(); + if (!opts_->get_disable_rocksdb_stats()) { + db_options.snapshot_statistics = std::make_shared(); + } + db_options.statistics = statistics_; + 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()); + } + db_options.use_direct_reads = opts_->get_celldb_direct_io(); + auto path = root_path_ + "/celldb/"; + auto rock_db = std::make_shared(td::RocksDb::open(path, std::move(db_options)).move_as_ok()); + + cell_db_ = td::actor::create_actor("celldb", actor_id(this), path, opts_, rock_db); + for (int i = 0; i < THREAD_COUNTS; i++){ + cell_db_read_[i] = td::actor::create_actor("celldb", actor_id(this), path, opts_, rock_db); + } + // cell_db_ = td::actor::create_actor("celldb", actor_id(this), root_path_ + "/celldb/", opts_); state_db_ = td::actor::create_actor("statedb", actor_id(this), root_path_ + "/state/"); static_files_db_ = td::actor::create_actor("staticfilesdb", actor_id(this), root_path_ + "/static/"); archive_db_ = td::actor::create_actor("archive", actor_id(this), root_path_, opts_); diff --git a/validator/db/rootdb.hpp b/validator/db/rootdb.hpp index 061e9add8..5189b7e1d 100644 --- a/validator/db/rootdb.hpp +++ b/validator/db/rootdb.hpp @@ -145,6 +145,7 @@ class RootDb : public Db { td::Ref opts_; td::actor::ActorOwn cell_db_; + td::actor::ActorOwn cell_db_read_[THREAD_COUNTS]; td::actor::ActorOwn state_db_; td::actor::ActorOwn static_files_db_; td::actor::ActorOwn archive_db_; From f313f65e3d0a1849baaa72f56c8aab4f51162297 Mon Sep 17 00:00:00 2001 From: jianguo Date: Wed, 23 Oct 2024 18:36:24 +0800 Subject: [PATCH 2/2] update --- validator/db/celldb.cpp | 22 +++++++--------------- validator/db/celldb.hpp | 9 +-------- validator/db/db-utils.cpp | 10 ++++++++++ validator/db/db-utils.h | 4 ++++ validator/db/rootdb.cpp | 4 ++-- 5 files changed, 24 insertions(+), 25 deletions(-) diff --git a/validator/db/celldb.cpp b/validator/db/celldb.cpp index b1293bf0f..f7da83c68 100644 --- a/validator/db/celldb.cpp +++ b/validator/db/celldb.cpp @@ -65,10 +65,10 @@ void CellDbBase::execute_sync(std::function f) { } CellDbIn::CellDbIn(td::actor::ActorId root_db, td::actor::ActorId parent, std::string path, - td::Ref opts) - : root_db_(root_db), parent_(parent), path_(std::move(path)), opts_(opts), std::shared_ptr cell_db{ + td::Ref opts, std::shared_ptr cell_db) + : root_db_(root_db), parent_(parent), path_(std::move(path)), opts_(opts){ if (cell_db != NULL) { - LOG(INFO) << "zjg: using provided KeyValue"; + LOG(INFO) << "Using provided cell_db"; cell_db_ = cell_db; } } @@ -102,7 +102,7 @@ void CellDbIn::start_up() { } db_options.use_direct_reads = opts_->get_celldb_direct_io(); if (cell_db_ == NULL){ - LOG(INFO) << "zjg: using new KeyValue"; + LOG(INFO) << "CellDbIn using a new rocksdb instance"; cell_db_ = std::make_shared(td::RocksDb::open(path_, std::move(db_options)).move_as_ok()); } @@ -444,20 +444,12 @@ void CellDbIn::migrate_cells() { } } -int GetRandom(){ - std::random_device rd; - std::mt19937 gen(rd()); - std::uniform_int_distribution<> distr(0, THREAD_COUNTS -1 ); - int random_number = distr(gen); - return random_number; -} - void CellDb::load_cell(RootHash hash, td::Promise> promise) { - int ranNum = GetRandom(); + int ranNum = GetDBRandomNum(); static int64_t ranCount = 0; ranCount++; if (ranCount % 1000 == 0) { - LOG(ERROR) << "yus " << this->get_name() << " " << this->get_actor_info_ptr()->mailbox().reader().calc_size() << ", ranNum: " << ranNum; + LOG(ERROR) << "CellDb mailbox: " << this->get_name() << " " << this->get_actor_info_ptr()->mailbox().reader().calc_size() << ", ranNum: " << ranNum; // LOG(ERROR) << "yus " << this->get_name() << " " << this->get_actor_info_ptr()->mailbox().reader().calc_size(); ranCount = 0; } @@ -492,7 +484,7 @@ void CellDb::start_up() { CellDbBase::start_up(); boc_ = vm::DynamicBagOfCellsDb::create(); boc_->set_celldb_compress_depth(opts_->get_celldb_compress_depth()); - cell_db_ = td::actor::create_actor("celldbin", root_db_, actor_id(this), path_, opts_); + cell_db_ = td::actor::create_actor("celldbin", root_db_, actor_id(this), path_, opts_, rocks_db_); // cell_db_ = td::actor::create_actor("celldbin", root_db_, actor_id(this), path_, opts_, rocks_db_); for (int i = 0; i < THREAD_COUNTS; i++) { diff --git a/validator/db/celldb.hpp b/validator/db/celldb.hpp index 69810b80b..86901e226 100644 --- a/validator/db/celldb.hpp +++ b/validator/db/celldb.hpp @@ -28,19 +28,12 @@ #include "validator.h" #include "db-utils.h" #include "td/db/RocksDb.h" -#include -#include namespace rocksdb { class Statistics; } namespace ton { - -const int THREAD_COUNTS = 20; - -int GetRandom(); - namespace validator { class RootDb; @@ -74,7 +67,7 @@ class CellDbIn : public CellDbBase { void flush_db_stats(); CellDbIn(td::actor::ActorId root_db, td::actor::ActorId parent, std::string path, - td::Ref opts, std::shared_ptr cell_db, std::shared_ptr); + td::Ref opts, std::shared_ptr cell_db); void start_up() override; void alarm() override; diff --git a/validator/db/db-utils.cpp b/validator/db/db-utils.cpp index 9375f97e6..2e9d1f852 100644 --- a/validator/db/db-utils.cpp +++ b/validator/db/db-utils.cpp @@ -19,6 +19,8 @@ #include "td/utils/logging.h" #include +#include +#include namespace ton::validator { @@ -51,4 +53,12 @@ void PercentileStats::clear() { values_.clear(); } +int GetDBRandomNum(){ + std::random_device rd; + std::mt19937 gen(rd()); + std::uniform_int_distribution<> distr(0, THREAD_COUNTS -1); + int random_number = distr(gen); + return random_number; +} + } // namespace ton::validator \ No newline at end of file diff --git a/validator/db/db-utils.h b/validator/db/db-utils.h index 8ffbf5c97..359fce80c 100644 --- a/validator/db/db-utils.h +++ b/validator/db/db-utils.h @@ -30,4 +30,8 @@ class PercentileStats { std::multiset values_; }; +const int THREAD_COUNTS = 10; +int GetDBRandomNum(); + + } // namespace ton::validator \ No newline at end of file diff --git a/validator/db/rootdb.cpp b/validator/db/rootdb.cpp index f7bc2b423..1f3ae0ab5 100644 --- a/validator/db/rootdb.cpp +++ b/validator/db/rootdb.cpp @@ -265,11 +265,11 @@ void RootDb::get_block_state(ConstBlockHandle handle, td::Promiseget_name() << " " << this->get_actor_info_ptr()->mailbox().reader().calc_size() << ", ranNum: " << ranNum; + LOG(ERROR) << "RootDb mailbox: " << this->get_name() << " " << this->get_actor_info_ptr()->mailbox().reader().calc_size() << ", ranNum: " << ranNum; ranCount = 0; }