Add possibility to change ttl on open DB

Summary:
We have seen cases where it could be good to change TTL on already open DB.
Change ttl in TtlCompactionFilterFactory on open db.
Next time a filter is created, it will filter accroding to the set TTL.

Is this something that could be useful for others?
Any downsides?
Closes https://github.com/facebook/rocksdb/pull/3292

Differential Revision: D6731993

Pulled By: miasantreble

fbshipit-source-id: 73b94d69237b11e8730734389052429d621a6b1e
main
jonasf 7 years ago committed by Facebook Github Bot
parent 46e599fc6b
commit 4decff6fa8
  1. 4
      include/rocksdb/utilities/db_ttl.h
  2. 11
      utilities/ttl/db_ttl_impl.cc
  3. 8
      utilities/ttl/db_ttl_impl.h
  4. 17
      utilities/ttl/ttl_test.cc

@ -60,6 +60,10 @@ class DBWithTTL : public StackableDB {
DBWithTTL** dbptr, std::vector<int32_t> ttls, DBWithTTL** dbptr, std::vector<int32_t> ttls,
bool read_only = false); bool read_only = false);
virtual void SetTtl(int32_t ttl) = 0;
virtual void SetTtl(ColumnFamilyHandle *h, int32_t ttl) = 0;
protected: protected:
explicit DBWithTTL(DB* db) : StackableDB(db) {} explicit DBWithTTL(DB* db) : StackableDB(db) {}
}; };

@ -309,5 +309,16 @@ Iterator* DBWithTTLImpl::NewIterator(const ReadOptions& opts,
return new TtlIterator(db_->NewIterator(opts, column_family)); return new TtlIterator(db_->NewIterator(opts, column_family));
} }
void DBWithTTLImpl::SetTtl(ColumnFamilyHandle *h, int32_t ttl) {
std::shared_ptr<TtlCompactionFilterFactory> filter;
Options opts;
opts = GetOptions(h);
filter = std::static_pointer_cast<TtlCompactionFilterFactory>(
opts.compaction_filter_factory);
if (!filter)
return;
filter->SetTtl(ttl);
}
} // namespace rocksdb } // namespace rocksdb
#endif // ROCKSDB_LITE #endif // ROCKSDB_LITE

@ -94,6 +94,10 @@ class DBWithTTLImpl : public DBWithTTL {
static const int32_t kMinTimestamp = 1368146402; // 05/09/2013:5:40PM GMT-8 static const int32_t kMinTimestamp = 1368146402; // 05/09/2013:5:40PM GMT-8
static const int32_t kMaxTimestamp = 2147483647; // 01/18/2038:7:14PM GMT-8 static const int32_t kMaxTimestamp = 2147483647; // 01/18/2038:7:14PM GMT-8
void SetTtl(int32_t ttl) override { SetTtl(DefaultColumnFamily(), ttl); }
void SetTtl(ColumnFamilyHandle *h, int32_t ttl) override;
}; };
class TtlIterator : public Iterator { class TtlIterator : public Iterator {
@ -209,6 +213,10 @@ class TtlCompactionFilterFactory : public CompactionFilterFactory {
ttl_, env_, nullptr, std::move(user_comp_filter_from_factory))); ttl_, env_, nullptr, std::move(user_comp_filter_from_factory)));
} }
void SetTtl(int32_t ttl) {
ttl_ = ttl;
}
virtual const char* Name() const override { virtual const char* Name() const override {
return "TtlCompactionFilterFactory"; return "TtlCompactionFilterFactory";
} }

@ -283,6 +283,12 @@ class TtlTest : public testing::Test {
delete dbiter; delete dbiter;
} }
// Set ttl on open db
void SetTtl(int32_t ttl, ColumnFamilyHandle* cf = nullptr) {
ASSERT_TRUE(db_ttl_);
cf == nullptr ? db_ttl_->SetTtl(ttl) : db_ttl_->SetTtl(cf, ttl);
}
class TestFilter : public CompactionFilter { class TestFilter : public CompactionFilter {
public: public:
TestFilter(const int64_t kSampleSize, const std::string& kNewValue) TestFilter(const int64_t kSampleSize, const std::string& kNewValue)
@ -626,6 +632,17 @@ TEST_F(TtlTest, ColumnFamiliesTest) {
db_ttl_ = nullptr; db_ttl_ = nullptr;
} }
// Puts a set of values and checks its absence using Get after ttl
TEST_F(TtlTest, ChangeTtlOnOpenDb) {
MakeKVMap(kSampleSize_);
OpenTtl(1); // T=0:Open the db with ttl = 2
SetTtl(3);
PutValues(0, kSampleSize_); // T=0:Insert Set1. Delete at t=2
SleepCompactCheck(2, 0, kSampleSize_, true); // T=2:Set1 should be there
CloseTtl();
}
} // namespace rocksdb } // namespace rocksdb
// A black-box test for the ttl wrapper around rocksdb // A black-box test for the ttl wrapper around rocksdb

Loading…
Cancel
Save