2016-02-10 00:12:00 +01:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-16 01:03:42 +02: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 23:59:46 +02:00
|
|
|
//
|
2013-06-17 22:58:17 +02:00
|
|
|
// Test for issue 178: a manual compaction causes deleted data to reappear.
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <cstdlib>
|
|
|
|
|
2019-05-31 02:39:43 +02:00
|
|
|
#include "port/port.h"
|
2014-01-15 01:19:09 +01:00
|
|
|
#include "rocksdb/compaction_filter.h"
|
2019-05-31 02:39:43 +02:00
|
|
|
#include "rocksdb/db.h"
|
2014-01-15 01:19:09 +01:00
|
|
|
#include "rocksdb/slice.h"
|
2013-08-23 17:38:13 +02:00
|
|
|
#include "rocksdb/write_batch.h"
|
2019-05-30 20:21:38 +02:00
|
|
|
#include "test_util/testharness.h"
|
2013-06-17 22:58:17 +02:00
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
using namespace ROCKSDB_NAMESPACE;
|
2014-01-15 01:19:09 +01:00
|
|
|
|
2013-06-17 22:58:17 +02:00
|
|
|
namespace {
|
|
|
|
|
2018-05-04 00:58:42 +02: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 22:58:17 +02:00
|
|
|
|
|
|
|
std::string Key1(int i) {
|
|
|
|
char buf[100];
|
|
|
|
snprintf(buf, sizeof(buf), "my_key_%d", i);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Key2(int i) {
|
|
|
|
return Key1(i) + "_xxx";
|
|
|
|
}
|
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
class ManualCompactionTest : public testing::Test {
|
2014-01-15 01:19:09 +01:00
|
|
|
public:
|
|
|
|
ManualCompactionTest() {
|
|
|
|
// Get rid of any state from an old run.
|
2020-02-20 21:07:53 +01:00
|
|
|
dbname_ = ROCKSDB_NAMESPACE::test::PerThreadDBPath("rocksdb_cbug_test");
|
|
|
|
DestroyDB(dbname_, ROCKSDB_NAMESPACE::Options());
|
2014-01-15 01:19:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string dbname_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class DestroyAllCompactionFilter : public CompactionFilter {
|
|
|
|
public:
|
|
|
|
DestroyAllCompactionFilter() {}
|
|
|
|
|
2019-02-14 22:52:47 +01:00
|
|
|
bool Filter(int /*level*/, const Slice& /*key*/, const Slice& existing_value,
|
|
|
|
std::string* /*new_value*/,
|
|
|
|
bool* /*value_changed*/) const override {
|
2014-01-15 01:19:09 +01:00
|
|
|
return existing_value.ToString() == "destroy";
|
|
|
|
}
|
|
|
|
|
2019-02-14 22:52:47 +01:00
|
|
|
const char* Name() const override { return "DestroyAllCompactionFilter"; }
|
2014-01-15 01:19:09 +01:00
|
|
|
};
|
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(ManualCompactionTest, CompactTouchesAllKeys) {
|
2014-01-15 01:19:09 +01:00
|
|
|
for (int iter = 0; iter < 2; ++iter) {
|
|
|
|
DB* db;
|
|
|
|
Options options;
|
|
|
|
if (iter == 0) { // level compaction
|
|
|
|
options.num_levels = 3;
|
|
|
|
options.compaction_style = kCompactionStyleLevel;
|
|
|
|
} else { // universal compaction
|
|
|
|
options.compaction_style = kCompactionStyleUniversal;
|
|
|
|
}
|
|
|
|
options.create_if_missing = true;
|
2020-02-20 21:07:53 +01:00
|
|
|
options.compression = ROCKSDB_NAMESPACE::kNoCompression;
|
2014-01-15 01:19:09 +01:00
|
|
|
options.compaction_filter = new DestroyAllCompactionFilter();
|
|
|
|
ASSERT_OK(DB::Open(options, dbname_, &db));
|
|
|
|
|
|
|
|
db->Put(WriteOptions(), Slice("key1"), Slice("destroy"));
|
|
|
|
db->Put(WriteOptions(), Slice("key2"), Slice("destroy"));
|
|
|
|
db->Put(WriteOptions(), Slice("key3"), Slice("value3"));
|
|
|
|
db->Put(WriteOptions(), Slice("key4"), Slice("destroy"));
|
|
|
|
|
|
|
|
Slice key4("key4");
|
2015-06-17 23:36:14 +02:00
|
|
|
db->CompactRange(CompactRangeOptions(), nullptr, &key4);
|
2014-01-15 01:19:09 +01:00
|
|
|
Iterator* itr = db->NewIterator(ReadOptions());
|
|
|
|
itr->SeekToFirst();
|
|
|
|
ASSERT_TRUE(itr->Valid());
|
|
|
|
ASSERT_EQ("key3", itr->key().ToString());
|
|
|
|
itr->Next();
|
|
|
|
ASSERT_TRUE(!itr->Valid());
|
|
|
|
delete itr;
|
|
|
|
|
|
|
|
delete options.compaction_filter;
|
|
|
|
delete db;
|
|
|
|
DestroyDB(dbname_, options);
|
|
|
|
}
|
|
|
|
}
|
2013-06-17 22:58:17 +02:00
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(ManualCompactionTest, Test) {
|
2013-06-17 22:58:17 +02: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-02-20 21:07:53 +01:00
|
|
|
ROCKSDB_NAMESPACE::DB* db;
|
|
|
|
ROCKSDB_NAMESPACE::Options db_options;
|
2018-05-04 00:58:42 +02:00
|
|
|
db_options.write_buffer_size = 1024;
|
2013-06-17 22:58:17 +02:00
|
|
|
db_options.create_if_missing = true;
|
2020-02-20 21:07:53 +01:00
|
|
|
db_options.compression = ROCKSDB_NAMESPACE::kNoCompression;
|
|
|
|
ASSERT_OK(ROCKSDB_NAMESPACE::DB::Open(db_options, dbname_, &db));
|
2013-06-17 22:58:17 +02:00
|
|
|
|
|
|
|
// create first key range
|
2020-02-20 21:07:53 +01:00
|
|
|
ROCKSDB_NAMESPACE::WriteBatch batch;
|
2013-06-17 22:58:17 +02:00
|
|
|
for (int i = 0; i < kNumKeys; i++) {
|
|
|
|
batch.Put(Key1(i), "value for range 1 key");
|
|
|
|
}
|
2020-02-20 21:07:53 +01:00
|
|
|
ASSERT_OK(db->Write(ROCKSDB_NAMESPACE::WriteOptions(), &batch));
|
2013-06-17 22:58:17 +02:00
|
|
|
|
|
|
|
// create second key range
|
|
|
|
batch.Clear();
|
|
|
|
for (int i = 0; i < kNumKeys; i++) {
|
|
|
|
batch.Put(Key2(i), "value for range 2 key");
|
|
|
|
}
|
2020-02-20 21:07:53 +01:00
|
|
|
ASSERT_OK(db->Write(ROCKSDB_NAMESPACE::WriteOptions(), &batch));
|
2013-06-17 22:58:17 +02:00
|
|
|
|
|
|
|
// delete second key range
|
|
|
|
batch.Clear();
|
|
|
|
for (int i = 0; i < kNumKeys; i++) {
|
|
|
|
batch.Delete(Key2(i));
|
|
|
|
}
|
2020-02-20 21:07:53 +01:00
|
|
|
ASSERT_OK(db->Write(ROCKSDB_NAMESPACE::WriteOptions(), &batch));
|
2013-06-17 22:58:17 +02:00
|
|
|
|
|
|
|
// compact database
|
|
|
|
std::string start_key = Key1(0);
|
|
|
|
std::string end_key = Key1(kNumKeys - 1);
|
2020-02-20 21:07:53 +01:00
|
|
|
ROCKSDB_NAMESPACE::Slice least(start_key.data(), start_key.size());
|
|
|
|
ROCKSDB_NAMESPACE::Slice greatest(end_key.data(), end_key.size());
|
2013-06-17 22:58:17 +02:00
|
|
|
|
|
|
|
// commenting out the line below causes the example to work correctly
|
2015-06-17 23:36:14 +02:00
|
|
|
db->CompactRange(CompactRangeOptions(), &least, &greatest);
|
2013-06-17 22:58:17 +02:00
|
|
|
|
|
|
|
// count the keys
|
2020-02-20 21:07:53 +01:00
|
|
|
ROCKSDB_NAMESPACE::Iterator* iter =
|
|
|
|
db->NewIterator(ROCKSDB_NAMESPACE::ReadOptions());
|
2013-06-17 22:58:17 +02:00
|
|
|
int num_keys = 0;
|
|
|
|
for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {
|
|
|
|
num_keys++;
|
|
|
|
}
|
|
|
|
delete iter;
|
|
|
|
ASSERT_EQ(kNumKeys, num_keys) << "Bad number of keys";
|
|
|
|
|
|
|
|
// close database
|
|
|
|
delete db;
|
2020-02-20 21:07:53 +01:00
|
|
|
DestroyDB(dbname_, ROCKSDB_NAMESPACE::Options());
|
2013-06-17 22:58:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2015-03-17 22:08:00 +01:00
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
|
|
return RUN_ALL_TESTS();
|
2013-06-17 22:58:17 +02:00
|
|
|
}
|