From 31026288735c70e94a2680328b493b5c342780e7 Mon Sep 17 00:00:00 2001 From: Mayank Agarwal Date: Fri, 10 May 2013 14:19:47 -0700 Subject: [PATCH] Bring read_only into ttl Summary: added an argument to openttldb for read only and open the db in normal readonly mode if the arguments is set to true Test Plan: make ttl_test; ./ttl_test Reviewers: dhruba, haobo, vamsi, sheki Reviewed By: dhruba CC: leveldb Differential Revision: https://reviews.facebook.net/D10749 --- include/utilities/utility_db.h | 5 ++++- utilities/ttl/db_ttl.cc | 14 ++++++++++---- utilities/ttl/db_ttl.h | 3 ++- utilities/ttl/ttl_test.cc | 19 +++++++++++++++++++ 4 files changed, 35 insertions(+), 6 deletions(-) diff --git a/include/utilities/utility_db.h b/include/utilities/utility_db.h index be2f2c4072..0bbfd7dc0e 100644 --- a/include/utilities/utility_db.h +++ b/include/utilities/utility_db.h @@ -30,6 +30,8 @@ class UtilityDB { // Different TTL may be used during different Opens // Example: Open1 at t=0 with ttl=4 and insert k1,k2, close at t=2 // Open2 at t=3 with ttl=5. Now k1,k2 should be deleted at t>=5 + // read_only=true opens in the usual read-only mode. Compactions will not be + // triggered(neither manual nor automatic), so no expired entries removed // // CONSTRAINTS: // The caller must not specify any compaction-filter in options @@ -44,7 +46,8 @@ class UtilityDB { static Status OpenTtlDB(const Options& options, const std::string& name, DB** dbptr, - int32_t ttl = 0); + int32_t ttl = 0, + bool read_only = false); }; } // namespace leveldb diff --git a/utilities/ttl/db_ttl.cc b/utilities/ttl/db_ttl.cc index d89e089417..949140c582 100644 --- a/utilities/ttl/db_ttl.cc +++ b/utilities/ttl/db_ttl.cc @@ -72,13 +72,18 @@ class TtlIterator : public Iterator { DBWithTTL::DBWithTTL(const int32_t ttl, const Options& options, const std::string& dbname, - Status& st) + Status& st, + bool read_only) : ttl_(ttl) { assert(options.CompactionFilter == nullptr); Options options_to_open = options; options_to_open.compaction_filter_args = &ttl_; options_to_open.CompactionFilter = DeleteByTS; - st = DB::Open(options_to_open, dbname, &db_); + if (read_only) { + st = DB::OpenForReadOnly(options_to_open, dbname, &db_); + } else { + st = DB::Open(options_to_open, dbname, &db_); + } } DBWithTTL::~DBWithTTL() { @@ -89,9 +94,10 @@ Status UtilityDB::OpenTtlDB( const Options& options, const std::string& dbname, DB** dbptr, - int32_t ttl) { + int32_t ttl, + bool read_only) { Status st; - *dbptr = new DBWithTTL(ttl, options, dbname, st); + *dbptr = new DBWithTTL(ttl, options, dbname, st, read_only); if (!st.ok()) { delete dbptr; } diff --git a/utilities/ttl/db_ttl.h b/utilities/ttl/db_ttl.h index 0a38070c35..b36573b874 100644 --- a/utilities/ttl/db_ttl.h +++ b/utilities/ttl/db_ttl.h @@ -15,7 +15,8 @@ class DBWithTTL : public DB { DBWithTTL(const int32_t ttl, const Options& options, const std::string& dbname, - Status& st); + Status& st, + bool read_only); virtual ~DBWithTTL(); diff --git a/utilities/ttl/ttl_test.cc b/utilities/ttl/ttl_test.cc index fb4ad1acd8..0c3207499c 100644 --- a/utilities/ttl/ttl_test.cc +++ b/utilities/ttl/ttl_test.cc @@ -44,6 +44,12 @@ class TtlTest { ASSERT_OK(UtilityDB::OpenTtlDB(options_, dbname_, &db_ttl_, ttl)); } + // Open database with TTL support in read_only mode + void OpenReadOnlyTtl(int32_t ttl) { + assert(db_ttl_ == nullptr); + ASSERT_OK(UtilityDB::OpenTtlDB(options_, dbname_, &db_ttl_, ttl, true)); + } + void CloseTtl() { delete db_ttl_; db_ttl_ = nullptr; @@ -273,6 +279,19 @@ TEST(TtlTest, MultiOpenDifferent) { CloseTtl(); } +// Checks presence during ttl in read_only mode +TEST(TtlTest, ReadOnlyPresentForever) { + MakeKVMap(kSampleSize); + + OpenTtl(1); // T=0:Open the db normally + PutValues(0, kSampleSize); // T=0:Insert Set1. Delete at t=1 + CloseTtl(); + + OpenReadOnlyTtl(1); + SleepCompactCheck(2, 0, kSampleSize, true); // T=2:Set1 should still be there + CloseTtl(); +} + } // namespace leveldb // A black-box test for the ttl wrapper around rocksdb