From c465a6a95c0943cce74f91b38647afca60ffc439 Mon Sep 17 00:00:00 2001 From: ms Date: Mon, 18 Mar 2024 23:21:03 +0100 Subject: [PATCH] Save rocksdb statistics to file every minute --- tddb/td/db/RocksDb.cpp | 15 +++++++++++++-- tddb/td/db/RocksDb.h | 6 +++++- validator/db/archive-manager.cpp | 16 +++++++++++++--- validator/db/archive-manager.hpp | 3 +++ validator/db/archive-slice.cpp | 7 ++++--- validator/db/archive-slice.hpp | 7 ++++++- validator/db/celldb.cpp | 12 +++++++++++- validator/db/celldb.hpp | 6 ++++++ 8 files changed, 61 insertions(+), 11 deletions(-) diff --git a/tddb/td/db/RocksDb.cpp b/tddb/td/db/RocksDb.cpp index 500985e2d..a84a804bb 100644 --- a/tddb/td/db/RocksDb.cpp +++ b/tddb/td/db/RocksDb.cpp @@ -59,9 +59,8 @@ RocksDb RocksDb::clone() const { return RocksDb{db_, statistics_}; } -Result RocksDb::open(std::string path) { +Result RocksDb::open(std::string path, std::shared_ptr statistics) { rocksdb::OptimisticTransactionDB *db; - auto statistics = rocksdb::CreateDBStatistics(); { rocksdb::Options options; @@ -94,6 +93,18 @@ Result RocksDb::open(std::string path) { return RocksDb(std::shared_ptr(db), std::move(statistics)); } +std::shared_ptr RocksDb::create_statistics() { + return rocksdb::CreateDBStatistics(); +} + +std::string RocksDb::statistics_to_string(const std::shared_ptr statistics) { + return statistics->ToString(); +} + +void RocksDb::reset_statistics(const std::shared_ptr statistics) { + statistics->Reset(); +} + std::unique_ptr RocksDb::snapshot() { auto res = std::make_unique(clone()); res->begin_snapshot().ensure(); diff --git a/tddb/td/db/RocksDb.h b/tddb/td/db/RocksDb.h index b8bfaf9da..1afba4cc4 100644 --- a/tddb/td/db/RocksDb.h +++ b/tddb/td/db/RocksDb.h @@ -38,7 +38,7 @@ class RocksDb : public KeyValue { public: static Status destroy(Slice path); RocksDb clone() const; - static Result open(std::string path); + static Result open(std::string path, std::shared_ptr statistics = nullptr); Result get(Slice key, std::string &value) override; Status set(Slice key, Slice value) override; @@ -60,6 +60,10 @@ class RocksDb : public KeyValue { std::unique_ptr snapshot() override; std::string stats() const override; + static std::shared_ptr create_statistics(); + static std::string statistics_to_string(const std::shared_ptr statistics); + static void reset_statistics(const std::shared_ptr statistics); + RocksDb(RocksDb &&); RocksDb &operator=(RocksDb &&); ~RocksDb(); diff --git a/validator/db/archive-manager.cpp b/validator/db/archive-manager.cpp index 2c0c82e51..9603f070e 100644 --- a/validator/db/archive-manager.cpp +++ b/validator/db/archive-manager.cpp @@ -601,7 +601,7 @@ void ArchiveManager::load_package(PackageId id) { } desc.file = - td::actor::create_actor("slice", id.id, id.key, id.temp, false, db_root_, archive_lru_.get()); + td::actor::create_actor("slice", id.id, id.key, id.temp, false, db_root_, archive_lru_.get(), statistics_); m.emplace(id, std::move(desc)); update_permanent_slices(); @@ -636,7 +636,7 @@ const ArchiveManager::FileDescription *ArchiveManager::add_file_desc(ShardIdFull td::mkdir(db_root_ + id.path()).ensure(); std::string prefix = PSTRING() << db_root_ << id.path() << id.name(); new_desc.file = - td::actor::create_actor("slice", id.id, id.key, id.temp, false, db_root_, archive_lru_.get()); + td::actor::create_actor("slice", id.id, id.key, id.temp, false, db_root_, archive_lru_.get(), statistics_); const FileDescription &desc = f.emplace(id, std::move(new_desc)); if (!id.temp) { update_desc(f, desc, shard, seqno, ts, lt); @@ -829,7 +829,8 @@ void ArchiveManager::start_up() { if (opts_->get_max_open_archive_files() > 0) { archive_lru_ = td::actor::create_actor("archive_lru", opts_->get_max_open_archive_files()); } - index_ = std::make_shared(td::RocksDb::open(db_root_ + "/files/globalindex").move_as_ok()); + statistics_ = td::RocksDb::create_statistics(); + index_ = std::make_shared(td::RocksDb::open(db_root_ + "/files/globalindex", statistics_).move_as_ok()); std::string value; auto v = index_->get(create_serialize_tl_object().as_slice(), value); v.ensure(); @@ -903,6 +904,15 @@ void ArchiveManager::start_up() { break; } } + + alarm_timestamp() = td::Timestamp::in(60.0); +} + +void ArchiveManager::alarm() { + alarm_timestamp() = td::Timestamp::in(60.0); + auto stats = td::RocksDb::statistics_to_string(statistics_); + td::atomic_write_file(db_root_ + "/db_stats.txt", stats); + td::RocksDb::reset_statistics(statistics_); } void ArchiveManager::run_gc(UnixTime mc_ts, UnixTime gc_ts, UnixTime archive_ttl) { diff --git a/validator/db/archive-manager.hpp b/validator/db/archive-manager.hpp index 1c5deaf86..aff765445 100644 --- a/validator/db/archive-manager.hpp +++ b/validator/db/archive-manager.hpp @@ -70,6 +70,7 @@ class ArchiveManager : public td::actor::Actor { td::Promise promise); void start_up() override; + void alarm() override; void commit_transaction(); void set_async_mode(bool mode, td::Promise promise); @@ -173,6 +174,8 @@ class ArchiveManager : public td::actor::Actor { bool huge_transaction_started_ = false; td::uint32 huge_transaction_size_ = 0; + std::shared_ptr statistics_; + FileMap &get_file_map(const PackageId &p) { return p.key ? key_files_ : p.temp ? temp_files_ : files_; } diff --git a/validator/db/archive-slice.cpp b/validator/db/archive-slice.cpp index 52abc0088..b38fbb7fd 100644 --- a/validator/db/archive-slice.cpp +++ b/validator/db/archive-slice.cpp @@ -465,7 +465,7 @@ void ArchiveSlice::get_archive_id(BlockSeqno masterchain_seqno, td::Promise(td::RocksDb::open(db_path_).move_as_ok()); + kv_ = std::make_unique(td::RocksDb::open(db_path_, statistics_).move_as_ok()); std::string value; auto R2 = kv_->get("status", value); R2.ensure(); @@ -604,14 +604,15 @@ void ArchiveSlice::set_async_mode(bool mode, td::Promise promise) { } ArchiveSlice::ArchiveSlice(td::uint32 archive_id, bool key_blocks_only, bool temp, bool finalized, std::string db_root, - td::actor::ActorId archive_lru) + td::actor::ActorId archive_lru, std::shared_ptr statistics) : archive_id_(archive_id) , key_blocks_only_(key_blocks_only) , temp_(temp) , finalized_(finalized) , p_id_(archive_id_, key_blocks_only_, temp_) , db_root_(std::move(db_root)) - , archive_lru_(std::move(archive_lru)) { + , archive_lru_(std::move(archive_lru)) + , statistics_(statistics) { db_path_ = PSTRING() << db_root_ << p_id_.path() << p_id_.name() << ".index"; } diff --git a/validator/db/archive-slice.hpp b/validator/db/archive-slice.hpp index f178a9b80..a58df32b6 100644 --- a/validator/db/archive-slice.hpp +++ b/validator/db/archive-slice.hpp @@ -23,6 +23,10 @@ #include "fileref.hpp" #include +namespace rocksdb { +class Statistics; +} + namespace ton { namespace validator { @@ -81,7 +85,7 @@ class ArchiveLru; class ArchiveSlice : public td::actor::Actor { public: ArchiveSlice(td::uint32 archive_id, bool key_blocks_only, bool temp, bool finalized, std::string db_root, - td::actor::ActorId archive_lru); + td::actor::ActorId archive_lru, std::shared_ptr statistics = nullptr); void get_archive_id(BlockSeqno masterchain_seqno, td::Promise promise); @@ -151,6 +155,7 @@ class ArchiveSlice : public td::actor::Actor { std::string db_root_; td::actor::ActorId archive_lru_; + std::shared_ptr statistics_; std::unique_ptr kv_; struct PackageInfo { diff --git a/validator/db/celldb.cpp b/validator/db/celldb.cpp index d29126cea..97ba35763 100644 --- a/validator/db/celldb.cpp +++ b/validator/db/celldb.cpp @@ -20,6 +20,7 @@ #include "rootdb.hpp" #include "td/db/RocksDb.h" +#include "td/utils/filesystem.h" #include "ton/ton-tl.hpp" #include "ton/ton-io.hpp" @@ -83,7 +84,9 @@ void CellDbIn::start_up() { }; CellDbBase::start_up(); - cell_db_ = std::make_shared(td::RocksDb::open(path_).move_as_ok()); + statistics_ = td::RocksDb::create_statistics(); + cell_db_ = std::make_shared(td::RocksDb::open(path_, statistics_).move_as_ok()); + statistics_flush_at_ = td::Timestamp::in(60.0); boc_ = vm::DynamicBagOfCellsDb::create(); boc_->set_celldb_compress_depth(opts_->get_celldb_compress_depth()); @@ -187,6 +190,13 @@ void CellDbIn::alarm() { } }); td::actor::send_closure(root_db_, &RootDb::allow_state_gc, block_id, std::move(P)); + + if (statistics_flush_at_.is_in_past()) { + statistics_flush_at_ = td::Timestamp::in(60.0); + auto stats = td::RocksDb::statistics_to_string(statistics_); + td::atomic_write_file(path_ + "/db_stats.txt", stats); + td::RocksDb::reset_statistics(statistics_); + } } void CellDbIn::gc(BlockIdExt block_id) { diff --git a/validator/db/celldb.hpp b/validator/db/celldb.hpp index a2a84ab4a..cc10c9acf 100644 --- a/validator/db/celldb.hpp +++ b/validator/db/celldb.hpp @@ -27,6 +27,10 @@ #include "auto/tl/ton_api.h" #include "validator.h" +namespace rocksdb { +class Statistics; +} + namespace ton { namespace validator { @@ -103,6 +107,8 @@ class CellDbIn : public CellDbBase { std::unique_ptr boc_; std::shared_ptr cell_db_; + std::shared_ptr statistics_; + td::Timestamp statistics_flush_at_; std::function on_load_callback_; std::set cells_to_migrate_;