@@ -258,11 +258,17 @@ Status RocksDb::flush() {
258
258
259
259
Status RocksDb::begin_snapshot () {
260
260
snapshot_.reset (db_->GetSnapshot ());
261
+ if (options_.snapshot_statistics ) {
262
+ options_.snapshot_statistics ->begin_snapshot (snapshot_.get ());
263
+ }
261
264
return td::Status::OK ();
262
265
}
263
266
264
267
Status RocksDb::end_snapshot () {
265
268
if (snapshot_) {
269
+ if (options_.snapshot_statistics ) {
270
+ options_.snapshot_statistics ->end_snapshot (snapshot_.get ());
271
+ }
266
272
db_->ReleaseSnapshot (snapshot_.release ());
267
273
}
268
274
return td::Status::OK ();
@@ -271,4 +277,41 @@ Status RocksDb::end_snapshot() {
271
277
RocksDb::RocksDb (std::shared_ptr<rocksdb::OptimisticTransactionDB> db, RocksDbOptions options)
272
278
: db_(std::move(db)), options_(options) {
273
279
}
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
+
274
317
} // namespace td
0 commit comments