Skip to content

Commit b3828f8

Browse files
Print oldest celldb snapshot to stats (ton-blockchain#1078)
Co-authored-by: SpyCheese <[email protected]>
1 parent 679e6be commit b3828f8

File tree

6 files changed

+83
-3
lines changed

6 files changed

+83
-3
lines changed

tddb/td/db/KeyValue.h

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
#pragma once
2020
#include "td/utils/Status.h"
21+
#include "td/utils/Time.h"
2122
#include "td/utils/logging.h"
2223
#include <functional>
2324
namespace td {

tddb/td/db/RocksDb.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -258,11 +258,17 @@ Status RocksDb::flush() {
258258

259259
Status RocksDb::begin_snapshot() {
260260
snapshot_.reset(db_->GetSnapshot());
261+
if (options_.snapshot_statistics) {
262+
options_.snapshot_statistics->begin_snapshot(snapshot_.get());
263+
}
261264
return td::Status::OK();
262265
}
263266

264267
Status RocksDb::end_snapshot() {
265268
if (snapshot_) {
269+
if (options_.snapshot_statistics) {
270+
options_.snapshot_statistics->end_snapshot(snapshot_.get());
271+
}
266272
db_->ReleaseSnapshot(snapshot_.release());
267273
}
268274
return td::Status::OK();
@@ -271,4 +277,41 @@ Status RocksDb::end_snapshot() {
271277
RocksDb::RocksDb(std::shared_ptr<rocksdb::OptimisticTransactionDB> db, RocksDbOptions options)
272278
: db_(std::move(db)), options_(options) {
273279
}
280+
281+
void RocksDbSnapshotStatistics::begin_snapshot(const rocksdb::Snapshot *snapshot) {
282+
auto lock = std::unique_lock<std::mutex>(mutex_);
283+
auto id = reinterpret_cast<std::uintptr_t>(snapshot);
284+
auto ts = td::Timestamp::now().at();
285+
CHECK(id_to_ts_.emplace(id, ts).second);
286+
CHECK(by_ts_.emplace(ts, id).second);
287+
}
288+
289+
void RocksDbSnapshotStatistics::end_snapshot(const rocksdb::Snapshot *snapshot) {
290+
auto id = reinterpret_cast<std::uintptr_t>(snapshot);
291+
auto it = id_to_ts_.find(id);
292+
CHECK(it != id_to_ts_.end());
293+
auto ts = it->second;
294+
CHECK(by_ts_.erase(std::make_pair(ts, id)) == 1u);
295+
CHECK(id_to_ts_.erase(id) == 1u);
296+
}
297+
298+
td::Timestamp RocksDbSnapshotStatistics::oldest_snapshot_timestamp() const {
299+
auto lock = std::unique_lock<std::mutex>(mutex_);
300+
if (by_ts_.empty()) {
301+
return {};
302+
}
303+
return td::Timestamp::at(by_ts_.begin()->first);
304+
}
305+
306+
std::string RocksDbSnapshotStatistics::to_string() const {
307+
td::Timestamp oldest_snapshot = oldest_snapshot_timestamp();
308+
double value;
309+
if (oldest_snapshot) {
310+
value = td::Timestamp::now().at() - oldest_snapshot.at();
311+
} else {
312+
value = -1;
313+
}
314+
return PSTRING() << "td.rocksdb.snapshot.oldest_snapshot_ago.seconds : " << value << "\n";
315+
}
316+
274317
} // namespace td

tddb/td/db/RocksDb.h

+18
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626
#include "td/utils/Status.h"
2727
#include "td/utils/optional.h"
2828

29+
#include "td/utils/Time.h"
30+
31+
#include <map>
32+
#include <mutex>
33+
#include <set>
34+
2935
namespace rocksdb {
3036
class Cache;
3137
class OptimisticTransactionDB;
@@ -36,10 +42,22 @@ class Statistics;
3642
} // namespace rocksdb
3743

3844
namespace td {
45+
struct RocksDbSnapshotStatistics {
46+
void begin_snapshot(const rocksdb::Snapshot *snapshot);
47+
void end_snapshot(const rocksdb::Snapshot *snapshot);
48+
td::Timestamp oldest_snapshot_timestamp() const;
49+
std::string to_string() const;
50+
51+
private:
52+
mutable std::mutex mutex_;
53+
std::map<std::uintptr_t, double> id_to_ts_;
54+
std::set<std::pair<double, std::uintptr_t>> by_ts_;
55+
};
3956

4057
struct RocksDbOptions {
4158
std::shared_ptr<rocksdb::Statistics> statistics = nullptr;
4259
std::shared_ptr<rocksdb::Cache> block_cache; // Default - one 1G cache for all RocksDb
60+
std::shared_ptr<RocksDbSnapshotStatistics> snapshot_statistics = nullptr;
4361
bool use_direct_reads = false;
4462
};
4563

tddb/test/key_value.cpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,19 @@ TEST(KeyValue, simple) {
6060
ensure_value(as_slice(x), as_slice(x));
6161

6262
kv.reset();
63-
kv = std::make_unique<td::RocksDb>(td::RocksDb::open(db_name.str()).move_as_ok());
63+
td::RocksDbOptions options{.snapshot_statistics = std::make_shared<td::RocksDbSnapshotStatistics>()};
64+
kv = std::make_unique<td::RocksDb>(td::RocksDb::open(db_name.str(), options).move_as_ok());
6465
ensure_value("A", "HELLO");
6566
ensure_value(as_slice(x), as_slice(x));
67+
68+
CHECK(!options.snapshot_statistics->oldest_snapshot_timestamp());
69+
auto snapshot = kv->snapshot();
70+
CHECK(options.snapshot_statistics->oldest_snapshot_timestamp());
71+
auto snapshot2 = kv->snapshot();
72+
snapshot.reset();
73+
CHECK(options.snapshot_statistics->oldest_snapshot_timestamp());
74+
snapshot2.reset();
75+
CHECK(!options.snapshot_statistics->oldest_snapshot_timestamp());
6676
};
6777

6878
TEST(KeyValue, async_simple) {

validator/db/celldb.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,13 @@ void CellDbIn::start_up() {
8484
};
8585

8686
CellDbBase::start_up();
87+
td::RocksDbOptions db_options;
8788
if (!opts_->get_disable_rocksdb_stats()) {
8889
statistics_ = td::RocksDb::create_statistics();
8990
statistics_flush_at_ = td::Timestamp::in(60.0);
91+
snapshot_statistics_ = std::make_shared<td::RocksDbSnapshotStatistics>();
92+
db_options.snapshot_statistics = snapshot_statistics_;
9093
}
91-
td::RocksDbOptions db_options;
9294
db_options.statistics = statistics_;
9395
if (opts_->get_celldb_cache_size()) {
9496
db_options.block_cache = td::RocksDb::create_cache(opts_->get_celldb_cache_size().value());
@@ -193,7 +195,11 @@ void CellDbIn::get_cell_db_reader(td::Promise<std::shared_ptr<vm::CellDbReader>>
193195
}
194196

195197
void CellDbIn::flush_db_stats() {
196-
auto stats = td::RocksDb::statistics_to_string(statistics_) + cell_db_statistics_.to_string();
198+
if (opts_->get_disable_rocksdb_stats()) {
199+
return;
200+
}
201+
auto stats = td::RocksDb::statistics_to_string(statistics_) + snapshot_statistics_->to_string() +
202+
cell_db_statistics_.to_string();
197203
auto to_file_r =
198204
td::FileFd::open(path_ + "/db_stats.txt", td::FileFd::Truncate | td::FileFd::Create | td::FileFd::Write, 0644);
199205
if (to_file_r.is_error()) {

validator/db/celldb.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "auto/tl/ton_api.h"
2828
#include "validator.h"
2929
#include "db-utils.h"
30+
#include "td/db/RocksDb.h"
3031

3132
namespace rocksdb {
3233
class Statistics;
@@ -139,6 +140,7 @@ class CellDbIn : public CellDbBase {
139140
};
140141

141142
std::shared_ptr<rocksdb::Statistics> statistics_;
143+
std::shared_ptr<td::RocksDbSnapshotStatistics> snapshot_statistics_;
142144
CellDbStatistics cell_db_statistics_;
143145
td::Timestamp statistics_flush_at_ = td::Timestamp::never();
144146

0 commit comments

Comments
 (0)