2016-02-09 23:12:00 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-15 23:03:42 +00:00
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2013-10-16 21:59:46 +00:00
|
|
|
//
|
2013-06-17 20:58:17 +00:00
|
|
|
// Test for issue 178: a manual compaction causes deleted data to reappear.
|
|
|
|
#include <cstdlib>
|
|
|
|
|
2019-05-31 00:39:43 +00:00
|
|
|
#include "port/port.h"
|
2014-01-15 00:19:09 +00:00
|
|
|
#include "rocksdb/compaction_filter.h"
|
2019-05-31 00:39:43 +00:00
|
|
|
#include "rocksdb/db.h"
|
2014-01-15 00:19:09 +00:00
|
|
|
#include "rocksdb/slice.h"
|
2013-08-23 15:38:13 +00:00
|
|
|
#include "rocksdb/write_batch.h"
|
2019-05-30 18:21:38 +00:00
|
|
|
#include "test_util/testharness.h"
|
2013-06-17 20:58:17 +00:00
|
|
|
|
2022-01-12 17:28:09 +00:00
|
|
|
using ROCKSDB_NAMESPACE::CompactionFilter;
|
|
|
|
using ROCKSDB_NAMESPACE::CompactionStyle;
|
|
|
|
using ROCKSDB_NAMESPACE::CompactRangeOptions;
|
|
|
|
using ROCKSDB_NAMESPACE::CompressionType;
|
|
|
|
using ROCKSDB_NAMESPACE::DB;
|
|
|
|
using ROCKSDB_NAMESPACE::DestroyDB;
|
|
|
|
using ROCKSDB_NAMESPACE::FlushOptions;
|
|
|
|
using ROCKSDB_NAMESPACE::Iterator;
|
|
|
|
using ROCKSDB_NAMESPACE::Options;
|
|
|
|
using ROCKSDB_NAMESPACE::ReadOptions;
|
|
|
|
using ROCKSDB_NAMESPACE::Slice;
|
|
|
|
using ROCKSDB_NAMESPACE::WriteBatch;
|
|
|
|
using ROCKSDB_NAMESPACE::WriteOptions;
|
2014-01-15 00:19:09 +00:00
|
|
|
|
2013-06-17 20:58:17 +00:00
|
|
|
namespace {
|
|
|
|
|
2018-05-03 22:58:42 +00:00
|
|
|
// Reasoning: previously the number was 1100000. Since the keys are written to
|
|
|
|
// the batch in one write each write will result into one SST file. each write
|
|
|
|
// will result into one SST file. We reduced the write_buffer_size to 1K to
|
|
|
|
// basically have the same effect with however less number of keys, which
|
|
|
|
// results into less test runtime.
|
|
|
|
const int kNumKeys = 1100;
|
2013-06-17 20:58:17 +00:00
|
|
|
|
|
|
|
std::string Key1(int i) {
|
|
|
|
char buf[100];
|
|
|
|
snprintf(buf, sizeof(buf), "my_key_%d", i);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2022-11-02 21:34:24 +00:00
|
|
|
std::string Key2(int i) { return Key1(i) + "_xxx"; }
|
2013-06-17 20:58:17 +00:00
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
class ManualCompactionTest : public testing::Test {
|
2014-01-15 00:19:09 +00:00
|
|
|
public:
|
|
|
|
ManualCompactionTest() {
|
|
|
|
// Get rid of any state from an old run.
|
2022-01-12 17:28:09 +00:00
|
|
|
dbname_ = ROCKSDB_NAMESPACE::test::PerThreadDBPath(
|
|
|
|
"rocksdb_manual_compaction_test");
|
2022-08-11 00:34:38 +00:00
|
|
|
EXPECT_OK(DestroyDB(dbname_, Options()));
|
2014-01-15 00:19:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string dbname_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class DestroyAllCompactionFilter : public CompactionFilter {
|
|
|
|
public:
|
2023-12-04 19:17:32 +00:00
|
|
|
DestroyAllCompactionFilter() = default;
|
2014-01-15 00:19:09 +00:00
|
|
|
|
2019-02-14 21:52:47 +00:00
|
|
|
bool Filter(int /*level*/, const Slice& /*key*/, const Slice& existing_value,
|
|
|
|
std::string* /*new_value*/,
|
|
|
|
bool* /*value_changed*/) const override {
|
2014-01-15 00:19:09 +00:00
|
|
|
return existing_value.ToString() == "destroy";
|
|
|
|
}
|
|
|
|
|
2019-02-14 21:52:47 +00:00
|
|
|
const char* Name() const override { return "DestroyAllCompactionFilter"; }
|
2014-01-15 00:19:09 +00:00
|
|
|
};
|
|
|
|
|
2020-03-05 04:12:23 +00:00
|
|
|
class LogCompactionFilter : public CompactionFilter {
|
|
|
|
public:
|
|
|
|
const char* Name() const override { return "LogCompactionFilter"; }
|
|
|
|
|
|
|
|
bool Filter(int level, const Slice& key, const Slice& /*existing_value*/,
|
|
|
|
std::string* /*new_value*/,
|
|
|
|
bool* /*value_changed*/) const override {
|
|
|
|
key_level_[key.ToString()] = level;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reset() { key_level_.clear(); }
|
|
|
|
|
|
|
|
size_t NumKeys() const { return key_level_.size(); }
|
|
|
|
|
|
|
|
int KeyLevel(const Slice& key) {
|
|
|
|
auto it = key_level_.find(key.ToString());
|
|
|
|
if (it == key_level_.end()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
mutable std::map<std::string, int> key_level_;
|
|
|
|
};
|
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(ManualCompactionTest, CompactTouchesAllKeys) {
|
2014-01-15 00:19:09 +00:00
|
|
|
for (int iter = 0; iter < 2; ++iter) {
|
|
|
|
DB* db;
|
|
|
|
Options options;
|
2022-11-02 21:34:24 +00:00
|
|
|
if (iter == 0) { // level compaction
|
2014-01-15 00:19:09 +00:00
|
|
|
options.num_levels = 3;
|
2022-01-12 17:28:09 +00:00
|
|
|
options.compaction_style = CompactionStyle::kCompactionStyleLevel;
|
2022-11-02 21:34:24 +00:00
|
|
|
} else { // universal compaction
|
2022-01-12 17:28:09 +00:00
|
|
|
options.compaction_style = CompactionStyle::kCompactionStyleUniversal;
|
2014-01-15 00:19:09 +00:00
|
|
|
}
|
|
|
|
options.create_if_missing = true;
|
2022-01-12 17:28:09 +00:00
|
|
|
options.compression = CompressionType::kNoCompression;
|
2014-01-15 00:19:09 +00:00
|
|
|
options.compaction_filter = new DestroyAllCompactionFilter();
|
|
|
|
ASSERT_OK(DB::Open(options, dbname_, &db));
|
|
|
|
|
2020-12-23 07:44:44 +00:00
|
|
|
ASSERT_OK(db->Put(WriteOptions(), Slice("key1"), Slice("destroy")));
|
|
|
|
ASSERT_OK(db->Put(WriteOptions(), Slice("key2"), Slice("destroy")));
|
|
|
|
ASSERT_OK(db->Put(WriteOptions(), Slice("key3"), Slice("value3")));
|
|
|
|
ASSERT_OK(db->Put(WriteOptions(), Slice("key4"), Slice("destroy")));
|
2014-01-15 00:19:09 +00:00
|
|
|
|
|
|
|
Slice key4("key4");
|
2020-12-23 07:44:44 +00:00
|
|
|
ASSERT_OK(db->CompactRange(CompactRangeOptions(), nullptr, &key4));
|
2014-01-15 00:19:09 +00:00
|
|
|
Iterator* itr = db->NewIterator(ReadOptions());
|
|
|
|
itr->SeekToFirst();
|
|
|
|
ASSERT_TRUE(itr->Valid());
|
|
|
|
ASSERT_EQ("key3", itr->key().ToString());
|
|
|
|
itr->Next();
|
|
|
|
ASSERT_TRUE(!itr->Valid());
|
2023-10-18 16:38:38 +00:00
|
|
|
ASSERT_OK(itr->status());
|
2014-01-15 00:19:09 +00:00
|
|
|
delete itr;
|
|
|
|
|
|
|
|
delete options.compaction_filter;
|
|
|
|
delete db;
|
2022-08-11 00:34:38 +00:00
|
|
|
ASSERT_OK(DestroyDB(dbname_, options));
|
2014-01-15 00:19:09 +00:00
|
|
|
}
|
|
|
|
}
|
2013-06-17 20:58:17 +00:00
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(ManualCompactionTest, Test) {
|
2013-06-17 20:58:17 +00:00
|
|
|
// Open database. Disable compression since it affects the creation
|
|
|
|
// of layers and the code below is trying to test against a very
|
|
|
|
// specific scenario.
|
2020-03-05 04:12:23 +00:00
|
|
|
DB* db;
|
|
|
|
Options db_options;
|
2018-05-03 22:58:42 +00:00
|
|
|
db_options.write_buffer_size = 1024;
|
2013-06-17 20:58:17 +00:00
|
|
|
db_options.create_if_missing = true;
|
2022-01-12 17:28:09 +00:00
|
|
|
db_options.compression = CompressionType::kNoCompression;
|
2020-03-05 04:12:23 +00:00
|
|
|
ASSERT_OK(DB::Open(db_options, dbname_, &db));
|
2013-06-17 20:58:17 +00:00
|
|
|
|
|
|
|
// create first key range
|
2020-03-05 04:12:23 +00:00
|
|
|
WriteBatch batch;
|
2013-06-17 20:58:17 +00:00
|
|
|
for (int i = 0; i < kNumKeys; i++) {
|
2020-12-23 07:44:44 +00:00
|
|
|
ASSERT_OK(batch.Put(Key1(i), "value for range 1 key"));
|
2013-06-17 20:58:17 +00:00
|
|
|
}
|
2020-03-05 04:12:23 +00:00
|
|
|
ASSERT_OK(db->Write(WriteOptions(), &batch));
|
2013-06-17 20:58:17 +00:00
|
|
|
|
|
|
|
// create second key range
|
|
|
|
batch.Clear();
|
|
|
|
for (int i = 0; i < kNumKeys; i++) {
|
2020-12-23 07:44:44 +00:00
|
|
|
ASSERT_OK(batch.Put(Key2(i), "value for range 2 key"));
|
2013-06-17 20:58:17 +00:00
|
|
|
}
|
2020-03-05 04:12:23 +00:00
|
|
|
ASSERT_OK(db->Write(WriteOptions(), &batch));
|
2013-06-17 20:58:17 +00:00
|
|
|
|
|
|
|
// delete second key range
|
|
|
|
batch.Clear();
|
|
|
|
for (int i = 0; i < kNumKeys; i++) {
|
2020-12-23 07:44:44 +00:00
|
|
|
ASSERT_OK(batch.Delete(Key2(i)));
|
2013-06-17 20:58:17 +00:00
|
|
|
}
|
2020-03-05 04:12:23 +00:00
|
|
|
ASSERT_OK(db->Write(WriteOptions(), &batch));
|
2013-06-17 20:58:17 +00:00
|
|
|
|
|
|
|
// compact database
|
|
|
|
std::string start_key = Key1(0);
|
|
|
|
std::string end_key = Key1(kNumKeys - 1);
|
2020-03-05 04:12:23 +00:00
|
|
|
Slice least(start_key.data(), start_key.size());
|
|
|
|
Slice greatest(end_key.data(), end_key.size());
|
2013-06-17 20:58:17 +00:00
|
|
|
|
|
|
|
// commenting out the line below causes the example to work correctly
|
2020-12-23 07:44:44 +00:00
|
|
|
ASSERT_OK(db->CompactRange(CompactRangeOptions(), &least, &greatest));
|
2013-06-17 20:58:17 +00:00
|
|
|
|
|
|
|
// count the keys
|
2020-03-05 04:12:23 +00:00
|
|
|
Iterator* iter = db->NewIterator(ReadOptions());
|
2013-06-17 20:58:17 +00:00
|
|
|
int num_keys = 0;
|
|
|
|
for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {
|
|
|
|
num_keys++;
|
|
|
|
}
|
2023-10-18 16:38:38 +00:00
|
|
|
ASSERT_OK(iter->status());
|
2013-06-17 20:58:17 +00:00
|
|
|
delete iter;
|
|
|
|
ASSERT_EQ(kNumKeys, num_keys) << "Bad number of keys";
|
|
|
|
|
|
|
|
// close database
|
|
|
|
delete db;
|
2022-08-11 00:34:38 +00:00
|
|
|
ASSERT_OK(DestroyDB(dbname_, Options()));
|
2020-03-05 04:12:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ManualCompactionTest, SkipLevel) {
|
|
|
|
DB* db;
|
|
|
|
Options options;
|
2023-06-16 04:12:39 +00:00
|
|
|
options.level_compaction_dynamic_level_bytes = false;
|
2020-03-05 04:12:23 +00:00
|
|
|
options.num_levels = 3;
|
|
|
|
// Initially, flushed L0 files won't exceed 100.
|
|
|
|
options.level0_file_num_compaction_trigger = 100;
|
2022-01-12 17:28:09 +00:00
|
|
|
options.compaction_style = CompactionStyle::kCompactionStyleLevel;
|
2020-03-05 04:12:23 +00:00
|
|
|
options.create_if_missing = true;
|
2022-01-12 17:28:09 +00:00
|
|
|
options.compression = CompressionType::kNoCompression;
|
2020-03-05 04:12:23 +00:00
|
|
|
LogCompactionFilter* filter = new LogCompactionFilter();
|
|
|
|
options.compaction_filter = filter;
|
|
|
|
ASSERT_OK(DB::Open(options, dbname_, &db));
|
|
|
|
|
|
|
|
WriteOptions wo;
|
|
|
|
FlushOptions fo;
|
|
|
|
ASSERT_OK(db->Put(wo, "1", ""));
|
|
|
|
ASSERT_OK(db->Flush(fo));
|
|
|
|
ASSERT_OK(db->Put(wo, "2", ""));
|
|
|
|
ASSERT_OK(db->Flush(fo));
|
|
|
|
ASSERT_OK(db->Put(wo, "4", ""));
|
|
|
|
ASSERT_OK(db->Put(wo, "8", ""));
|
|
|
|
ASSERT_OK(db->Flush(fo));
|
|
|
|
|
|
|
|
{
|
|
|
|
// L0: 1, 2, [4, 8]
|
|
|
|
// no file has keys in range [5, 7]
|
|
|
|
Slice start("5");
|
|
|
|
Slice end("7");
|
|
|
|
filter->Reset();
|
2020-12-23 07:44:44 +00:00
|
|
|
ASSERT_OK(db->CompactRange(CompactRangeOptions(), &start, &end));
|
2020-03-05 04:12:23 +00:00
|
|
|
ASSERT_EQ(0, filter->NumKeys());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// L0: 1, 2, [4, 8]
|
|
|
|
// [3, 7] overlaps with 4 in L0
|
|
|
|
Slice start("3");
|
|
|
|
Slice end("7");
|
|
|
|
filter->Reset();
|
2020-12-23 07:44:44 +00:00
|
|
|
ASSERT_OK(db->CompactRange(CompactRangeOptions(), &start, &end));
|
2020-03-05 04:12:23 +00:00
|
|
|
ASSERT_EQ(2, filter->NumKeys());
|
|
|
|
ASSERT_EQ(0, filter->KeyLevel("4"));
|
|
|
|
ASSERT_EQ(0, filter->KeyLevel("8"));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// L0: 1, 2
|
|
|
|
// L1: [4, 8]
|
|
|
|
// no file has keys in range (-inf, 0]
|
|
|
|
Slice end("0");
|
|
|
|
filter->Reset();
|
2020-12-23 07:44:44 +00:00
|
|
|
ASSERT_OK(db->CompactRange(CompactRangeOptions(), nullptr, &end));
|
2020-03-05 04:12:23 +00:00
|
|
|
ASSERT_EQ(0, filter->NumKeys());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// L0: 1, 2
|
|
|
|
// L1: [4, 8]
|
|
|
|
// no file has keys in range [9, inf)
|
|
|
|
Slice start("9");
|
|
|
|
filter->Reset();
|
2020-12-23 07:44:44 +00:00
|
|
|
ASSERT_OK(db->CompactRange(CompactRangeOptions(), &start, nullptr));
|
2020-03-05 04:12:23 +00:00
|
|
|
ASSERT_EQ(0, filter->NumKeys());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// L0: 1, 2
|
|
|
|
// L1: [4, 8]
|
|
|
|
// [2, 2] overlaps with 2 in L0
|
|
|
|
Slice start("2");
|
|
|
|
Slice end("2");
|
|
|
|
filter->Reset();
|
2020-12-23 07:44:44 +00:00
|
|
|
ASSERT_OK(db->CompactRange(CompactRangeOptions(), &start, &end));
|
2020-03-05 04:12:23 +00:00
|
|
|
ASSERT_EQ(1, filter->NumKeys());
|
|
|
|
ASSERT_EQ(0, filter->KeyLevel("2"));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// L0: 1
|
|
|
|
// L1: 2, [4, 8]
|
|
|
|
// [2, 5] overlaps with 2 and [4, 8) in L1, skip L0
|
|
|
|
Slice start("2");
|
|
|
|
Slice end("5");
|
|
|
|
filter->Reset();
|
2020-12-23 07:44:44 +00:00
|
|
|
ASSERT_OK(db->CompactRange(CompactRangeOptions(), &start, &end));
|
2020-03-05 04:12:23 +00:00
|
|
|
ASSERT_EQ(3, filter->NumKeys());
|
|
|
|
ASSERT_EQ(1, filter->KeyLevel("2"));
|
|
|
|
ASSERT_EQ(1, filter->KeyLevel("4"));
|
|
|
|
ASSERT_EQ(1, filter->KeyLevel("8"));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// L0: 1
|
|
|
|
// L1: [2, 4, 8]
|
|
|
|
// [0, inf) overlaps all files
|
|
|
|
Slice start("0");
|
|
|
|
filter->Reset();
|
2020-12-23 07:44:44 +00:00
|
|
|
ASSERT_OK(db->CompactRange(CompactRangeOptions(), &start, nullptr));
|
2020-03-05 04:12:23 +00:00
|
|
|
ASSERT_EQ(4, filter->NumKeys());
|
2023-06-01 22:27:29 +00:00
|
|
|
// 1 is first compacted from L0 to L1, and then L1 intra level compaction
|
|
|
|
// compacts [2, 4, 8] only.
|
|
|
|
ASSERT_EQ(0, filter->KeyLevel("1"));
|
2020-03-05 04:12:23 +00:00
|
|
|
ASSERT_EQ(1, filter->KeyLevel("2"));
|
|
|
|
ASSERT_EQ(1, filter->KeyLevel("4"));
|
|
|
|
ASSERT_EQ(1, filter->KeyLevel("8"));
|
|
|
|
}
|
|
|
|
|
|
|
|
delete filter;
|
|
|
|
delete db;
|
2022-08-11 00:34:38 +00:00
|
|
|
ASSERT_OK(DestroyDB(dbname_, options));
|
2013-06-17 20:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2022-10-18 07:35:35 +00:00
|
|
|
ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
|
2015-03-17 21:08:00 +00:00
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
|
|
return RUN_ALL_TESTS();
|
2013-06-17 20:58:17 +00:00
|
|
|
}
|