From 700d597bd85106eae796d8662be1a81404c618cd Mon Sep 17 00:00:00 2001 From: Akanksha Mahajan <43301668+akankshamahajan15@users.noreply.github.com> Date: Mon, 23 May 2022 22:31:08 -0700 Subject: [PATCH] Expose unix time in rocksdb::Snapshot (#9923) Summary: RocksDB snapshot already has a member unix_time_ set after snapshot is taken. It is now exposed through GetSnapshotTime() API. Pull Request resolved: https://github.com/facebook/rocksdb/pull/9923 Test Plan: Update unit tests Reviewed By: riversand963 Differential Revision: D36048275 Pulled By: akankshamahajan15 fbshipit-source-id: 825210ec287deb0bc3aaa9b8e1f079f07ad686fa --- HISTORY.md | 2 ++ db/db_test.cc | 16 ++++++++++++++++ db/snapshot_impl.h | 4 +++- include/rocksdb/snapshot.h | 5 ++++- 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 2b98bcaaf..1da9ed1cf 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,5 +1,7 @@ # Rocksdb Change Log ## Unreleased +### Public API changes +* Add new API GetUnixTime in Snapshot class which returns the unix time at which Snapshot is taken. ## 7.3.0 (05/20/2022) ### Bug Fixes diff --git a/db/db_test.cc b/db/db_test.cc index f3039f187..14abe8056 100644 --- a/db/db_test.cc +++ b/db/db_test.cc @@ -1844,6 +1844,8 @@ TEST_F(DBTest, Snapshot) { uint64_t time_snap1 = GetTimeOldestSnapshots(); ASSERT_GT(time_snap1, 0U); ASSERT_EQ(GetSequenceOldestSnapshots(), s1->GetSequenceNumber()); + ASSERT_EQ(GetTimeOldestSnapshots(), + static_cast(s1->GetUnixTime())); ASSERT_OK(Put(0, "foo", "0v2")); ASSERT_OK(Put(1, "foo", "1v2")); @@ -1853,6 +1855,8 @@ TEST_F(DBTest, Snapshot) { ASSERT_EQ(2U, GetNumSnapshots()); ASSERT_EQ(time_snap1, GetTimeOldestSnapshots()); ASSERT_EQ(GetSequenceOldestSnapshots(), s1->GetSequenceNumber()); + ASSERT_EQ(GetTimeOldestSnapshots(), + static_cast(s1->GetUnixTime())); ASSERT_OK(Put(0, "foo", "0v3")); ASSERT_OK(Put(1, "foo", "1v3")); @@ -1861,6 +1865,8 @@ TEST_F(DBTest, Snapshot) { ASSERT_EQ(3U, GetNumSnapshots()); ASSERT_EQ(time_snap1, GetTimeOldestSnapshots()); ASSERT_EQ(GetSequenceOldestSnapshots(), s1->GetSequenceNumber()); + ASSERT_EQ(GetTimeOldestSnapshots(), + static_cast(s1->GetUnixTime())); ASSERT_OK(Put(0, "foo", "0v4")); ASSERT_OK(Put(1, "foo", "1v4")); @@ -1877,6 +1883,8 @@ TEST_F(DBTest, Snapshot) { ASSERT_EQ(2U, GetNumSnapshots()); ASSERT_EQ(time_snap1, GetTimeOldestSnapshots()); ASSERT_EQ(GetSequenceOldestSnapshots(), s1->GetSequenceNumber()); + ASSERT_EQ(GetTimeOldestSnapshots(), + static_cast(s1->GetUnixTime())); ASSERT_EQ("0v1", Get(0, "foo", s1)); ASSERT_EQ("1v1", Get(1, "foo", s1)); ASSERT_EQ("0v2", Get(0, "foo", s2)); @@ -1892,6 +1900,8 @@ TEST_F(DBTest, Snapshot) { ASSERT_EQ(1U, GetNumSnapshots()); ASSERT_LT(time_snap1, GetTimeOldestSnapshots()); ASSERT_EQ(GetSequenceOldestSnapshots(), s2->GetSequenceNumber()); + ASSERT_EQ(GetTimeOldestSnapshots(), + static_cast(s2->GetUnixTime())); db_->ReleaseSnapshot(s2); ASSERT_EQ(0U, GetNumSnapshots()); @@ -2859,6 +2869,12 @@ class ModelDB : public DB { assert(false); return 0; } + + int64_t GetUnixTime() const override { + // no need to call this + assert(false); + return 0; + } }; explicit ModelDB(const Options& options) : options_(options) {} diff --git a/db/snapshot_impl.h b/db/snapshot_impl.h index 9961bdd58..dc903ea42 100644 --- a/db/snapshot_impl.h +++ b/db/snapshot_impl.h @@ -27,7 +27,9 @@ class SnapshotImpl : public Snapshot { // scope of queries to IsInSnapshot. SequenceNumber min_uncommitted_ = kMinUnCommittedSeq; - virtual SequenceNumber GetSequenceNumber() const override { return number_; } + SequenceNumber GetSequenceNumber() const override { return number_; } + + int64_t GetUnixTime() const override { return unix_time_; } private: friend class SnapshotList; diff --git a/include/rocksdb/snapshot.h b/include/rocksdb/snapshot.h index 6a7212d60..61281d7c4 100644 --- a/include/rocksdb/snapshot.h +++ b/include/rocksdb/snapshot.h @@ -19,9 +19,12 @@ class DB; // To Destroy a Snapshot, call DB::ReleaseSnapshot(snapshot). class Snapshot { public: - // returns Snapshot's sequence number virtual SequenceNumber GetSequenceNumber() const = 0; + // Returns unix time i.e. the number of seconds since the Epoch, 1970-01-01 + // 00:00:00 (UTC). + virtual int64_t GetUnixTime() const = 0; + protected: virtual ~Snapshot(); };