Allow dynamic modification of window size and deletion trigger (#4403)

Summary:
Make the CompactOnDeletionCollectorFactory class public, and provide
methods to update the window size and deletion trigger params. These
will take effect on subsequent created SST files.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/4403

Differential Revision: D9976857

Pulled By: anand1976

fbshipit-source-id: 31dbf0511c12fa2bb9b2a7ba620079e0ee09cf48
main
Anand Ananthabhotla 6 years ago committed by Facebook Github Bot
parent 02dc074916
commit 72712f4e28
  1. 53
      db/db_table_properties_test.cc
  2. 50
      include/rocksdb/utilities/table_properties_collectors.h
  3. 12
      utilities/table_properties_collectors/compact_on_deletion_collector.cc
  4. 32
      utilities/table_properties_collectors/compact_on_deletion_collector.h

@ -252,13 +252,14 @@ TEST_F(DBTablePropertiesTest, GetColumnFamilyNameProperty) {
}
TEST_F(DBTablePropertiesTest, DeletionTriggeredCompactionMarking) {
const int kNumKeys = 1000;
const int kWindowSize = 100;
const int kNumDelsTrigger = 90;
int kNumKeys = 1000;
int kWindowSize = 100;
int kNumDelsTrigger = 90;
std::shared_ptr<TablePropertiesCollectorFactory> compact_on_del =
NewCompactOnDeletionCollectorFactory(kWindowSize, kNumDelsTrigger);
Options opts = CurrentOptions();
opts.table_properties_collector_factories.emplace_back(
NewCompactOnDeletionCollectorFactory(kWindowSize, kNumDelsTrigger));
opts.table_properties_collector_factories.emplace_back(compact_on_del);
Reopen(opts);
// add an L1 file to prevent tombstones from dropping due to obsolescence
@ -280,6 +281,48 @@ TEST_F(DBTablePropertiesTest, DeletionTriggeredCompactionMarking) {
dbfull()->TEST_WaitForCompact();
ASSERT_EQ(0, NumTableFilesAtLevel(0));
ASSERT_GT(NumTableFilesAtLevel(1), 0);
// Change the window size and deletion trigger and ensure new values take
// effect
kWindowSize = 50;
kNumDelsTrigger = 40;
static_cast<CompactOnDeletionCollectorFactory*>
(compact_on_del.get())->SetWindowSize(kWindowSize);
static_cast<CompactOnDeletionCollectorFactory*>
(compact_on_del.get())->SetDeletionTrigger(kNumDelsTrigger);
for (int i = 0; i < kNumKeys; ++i) {
if (i >= kNumKeys - kWindowSize &&
i < kNumKeys - kWindowSize + kNumDelsTrigger) {
Delete(Key(i));
} else {
Put(Key(i), "val");
}
}
Flush();
dbfull()->TEST_WaitForCompact();
ASSERT_EQ(0, NumTableFilesAtLevel(0));
ASSERT_GT(NumTableFilesAtLevel(1), 0);
// Change the window size to disable delete triggered compaction
kWindowSize = 0;
static_cast<CompactOnDeletionCollectorFactory*>
(compact_on_del.get())->SetWindowSize(kWindowSize);
static_cast<CompactOnDeletionCollectorFactory*>
(compact_on_del.get())->SetDeletionTrigger(kNumDelsTrigger);
for (int i = 0; i < kNumKeys; ++i) {
if (i >= kNumKeys - kWindowSize &&
i < kNumKeys - kWindowSize + kNumDelsTrigger) {
Delete(Key(i));
} else {
Put(Key(i), "val");
}
}
Flush();
dbfull()->TEST_WaitForCompact();
ASSERT_EQ(1, NumTableFilesAtLevel(0));
}
} // namespace rocksdb

@ -5,12 +5,60 @@
#pragma once
#ifndef ROCKSDB_LITE
#include <atomic>
#include <memory>
#include "rocksdb/table_properties.h"
namespace rocksdb {
// A factory of a table property collector that marks a SST
// file as need-compaction when it observe at least "D" deletion
// entries in any "N" consecutive entires.
class CompactOnDeletionCollectorFactory
: public TablePropertiesCollectorFactory {
public:
virtual ~CompactOnDeletionCollectorFactory() {}
virtual TablePropertiesCollector* CreateTablePropertiesCollector(
TablePropertiesCollectorFactory::Context context) override;
// Change the value of sliding_window_size "N"
// Setting it to 0 disables the delete triggered compaction
void SetWindowSize(size_t sliding_window_size) {
sliding_window_size_.store(sliding_window_size);
}
// Change the value of deletion_trigger "D"
void SetDeletionTrigger(size_t deletion_trigger) {
deletion_trigger_.store(deletion_trigger);
}
virtual const char* Name() const override {
return "CompactOnDeletionCollector";
}
private:
friend std::shared_ptr<CompactOnDeletionCollectorFactory>
NewCompactOnDeletionCollectorFactory(
size_t sliding_window_size,
size_t deletion_trigger);
// A factory of a table property collector that marks a SST
// file as need-compaction when it observe at least "D" deletion
// entries in any "N" consecutive entires.
//
// @param sliding_window_size "N"
// @param deletion_trigger "D"
CompactOnDeletionCollectorFactory(
size_t sliding_window_size,
size_t deletion_trigger) :
sliding_window_size_(sliding_window_size),
deletion_trigger_(deletion_trigger) {}
std::atomic<size_t> sliding_window_size_;
std::atomic<size_t> deletion_trigger_;
};
// Creates a factory of a table property collector that marks a SST
// file as need-compaction when it observe at least "D" deletion
// entries in any "N" consecutive entires.
@ -20,7 +68,7 @@ namespace rocksdb {
// than the specified size.
// @param deletion_trigger "D". Note that even when "N" is changed,
// the specified number for "D" will not be changed.
extern std::shared_ptr<TablePropertiesCollectorFactory>
extern std::shared_ptr<CompactOnDeletionCollectorFactory>
NewCompactOnDeletionCollectorFactory(
size_t sliding_window_size,
size_t deletion_trigger);

@ -20,7 +20,6 @@ CompactOnDeletionCollector::CompactOnDeletionCollector(
deletion_trigger_(deletion_trigger),
need_compaction_(false),
finished_(false) {
assert(bucket_size_ > 0U);
memset(num_deletions_in_buckets_, 0, sizeof(size_t) * kNumBuckets);
}
@ -35,6 +34,11 @@ Status CompactOnDeletionCollector::AddUserKey(const Slice& /*key*/,
SequenceNumber /*seq*/,
uint64_t /*file_size*/) {
assert(!finished_);
if (bucket_size_ == 0) {
// This collector is effectively disabled
return Status::OK();
}
if (need_compaction_) {
// If the output file already needs to be compacted, skip the check.
return Status::OK();
@ -71,14 +75,14 @@ TablePropertiesCollector*
CompactOnDeletionCollectorFactory::CreateTablePropertiesCollector(
TablePropertiesCollectorFactory::Context /*context*/) {
return new CompactOnDeletionCollector(
sliding_window_size_, deletion_trigger_);
sliding_window_size_.load(), deletion_trigger_.load());
}
std::shared_ptr<TablePropertiesCollectorFactory>
std::shared_ptr<CompactOnDeletionCollectorFactory>
NewCompactOnDeletionCollectorFactory(
size_t sliding_window_size,
size_t deletion_trigger) {
return std::shared_ptr<TablePropertiesCollectorFactory>(
return std::shared_ptr<CompactOnDeletionCollectorFactory>(
new CompactOnDeletionCollectorFactory(
sliding_window_size, deletion_trigger));
}

@ -9,38 +9,6 @@
#include "rocksdb/utilities/table_properties_collectors.h"
namespace rocksdb {
// A factory of a table property collector that marks a SST
// file as need-compaction when it observe at least "D" deletion
// entries in any "N" consecutive entires.
class CompactOnDeletionCollectorFactory
: public TablePropertiesCollectorFactory {
public:
// A factory of a table property collector that marks a SST
// file as need-compaction when it observe at least "D" deletion
// entries in any "N" consecutive entires.
//
// @param sliding_window_size "N"
// @param deletion_trigger "D"
CompactOnDeletionCollectorFactory(
size_t sliding_window_size,
size_t deletion_trigger) :
sliding_window_size_(sliding_window_size),
deletion_trigger_(deletion_trigger) {}
virtual ~CompactOnDeletionCollectorFactory() {}
virtual TablePropertiesCollector* CreateTablePropertiesCollector(
TablePropertiesCollectorFactory::Context context) override;
virtual const char* Name() const override {
return "CompactOnDeletionCollector";
}
private:
size_t sliding_window_size_;
size_t deletion_trigger_;
};
class CompactOnDeletionCollector : public TablePropertiesCollector {
public:
CompactOnDeletionCollector(

Loading…
Cancel
Save