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 <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#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
|
|
|
|
2014-01-15 00:19:09 +00:00
|
|
|
using namespace rocksdb;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Key2(int i) {
|
|
|
|
return Key1(i) + "_xxx";
|
|
|
|
}
|
|
|
|
|
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.
|
2018-07-14 00:18:39 +00:00
|
|
|
dbname_ = rocksdb::test::PerThreadDBPath("rocksdb_cbug_test");
|
2014-01-15 00:19:09 +00:00
|
|
|
DestroyDB(dbname_, rocksdb::Options());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string dbname_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class DestroyAllCompactionFilter : public CompactionFilter {
|
|
|
|
public:
|
|
|
|
DestroyAllCompactionFilter() {}
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
options.compression = rocksdb::kNoCompression;
|
|
|
|
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 21:36:14 +00:00
|
|
|
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());
|
|
|
|
delete itr;
|
|
|
|
|
|
|
|
delete options.compaction_filter;
|
|
|
|
delete db;
|
|
|
|
DestroyDB(dbname_, options);
|
|
|
|
}
|
|
|
|
}
|
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.
|
2013-10-04 04:49:15 +00:00
|
|
|
rocksdb::DB* db;
|
|
|
|
rocksdb::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;
|
2013-10-04 04:49:15 +00:00
|
|
|
db_options.compression = rocksdb::kNoCompression;
|
2014-01-15 00:19:09 +00:00
|
|
|
ASSERT_OK(rocksdb::DB::Open(db_options, dbname_, &db));
|
2013-06-17 20:58:17 +00:00
|
|
|
|
|
|
|
// create first key range
|
2013-10-04 04:49:15 +00:00
|
|
|
rocksdb::WriteBatch batch;
|
2013-06-17 20:58:17 +00:00
|
|
|
for (int i = 0; i < kNumKeys; i++) {
|
|
|
|
batch.Put(Key1(i), "value for range 1 key");
|
|
|
|
}
|
2013-10-04 04:49:15 +00:00
|
|
|
ASSERT_OK(db->Write(rocksdb::WriteOptions(), &batch));
|
2013-06-17 20:58:17 +00:00
|
|
|
|
|
|
|
// create second key range
|
|
|
|
batch.Clear();
|
|
|
|
for (int i = 0; i < kNumKeys; i++) {
|
|
|
|
batch.Put(Key2(i), "value for range 2 key");
|
|
|
|
}
|
2013-10-04 04:49:15 +00:00
|
|
|
ASSERT_OK(db->Write(rocksdb::WriteOptions(), &batch));
|
2013-06-17 20:58:17 +00:00
|
|
|
|
|
|
|
// delete second key range
|
|
|
|
batch.Clear();
|
|
|
|
for (int i = 0; i < kNumKeys; i++) {
|
|
|
|
batch.Delete(Key2(i));
|
|
|
|
}
|
2013-10-04 04:49:15 +00:00
|
|
|
ASSERT_OK(db->Write(rocksdb::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);
|
2013-10-04 04:49:15 +00:00
|
|
|
rocksdb::Slice least(start_key.data(), start_key.size());
|
|
|
|
rocksdb::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
|
2015-06-17 21:36:14 +00:00
|
|
|
db->CompactRange(CompactRangeOptions(), &least, &greatest);
|
2013-06-17 20:58:17 +00:00
|
|
|
|
|
|
|
// count the keys
|
2013-10-04 04:49:15 +00:00
|
|
|
rocksdb::Iterator* iter = db->NewIterator(rocksdb::ReadOptions());
|
2013-06-17 20:58:17 +00: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;
|
2014-01-15 00:19:09 +00:00
|
|
|
DestroyDB(dbname_, rocksdb::Options());
|
2013-06-17 20:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2015-03-17 21:08:00 +00:00
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
|
|
return RUN_ALL_TESTS();
|
2013-06-17 20:58:17 +00:00
|
|
|
}
|