2013-10-16 23:59:46 +02:00
|
|
|
// Copyright (c) 2013, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under the BSD-style license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
// of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
//
|
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>
|
|
|
|
|
2013-08-23 17:38:13 +02:00
|
|
|
#include "rocksdb/db.h"
|
2014-01-15 01:19:09 +01:00
|
|
|
#include "rocksdb/compaction_filter.h"
|
|
|
|
#include "rocksdb/slice.h"
|
2013-08-23 17:38:13 +02:00
|
|
|
#include "rocksdb/write_batch.h"
|
2013-06-17 22:58:17 +02:00
|
|
|
#include "util/testharness.h"
|
2015-07-16 21:10:16 +02:00
|
|
|
#include "port/port.h"
|
2013-06-17 22:58:17 +02:00
|
|
|
|
2014-01-15 01:19:09 +01:00
|
|
|
using namespace rocksdb;
|
|
|
|
|
2013-06-17 22:58:17 +02:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
const int kNumKeys = 1100000;
|
|
|
|
|
|
|
|
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.
|
|
|
|
dbname_ = rocksdb::test::TmpDir() + "/rocksdb_cbug_test";
|
|
|
|
DestroyDB(dbname_, rocksdb::Options());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string dbname_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class DestroyAllCompactionFilter : public CompactionFilter {
|
|
|
|
public:
|
|
|
|
DestroyAllCompactionFilter() {}
|
|
|
|
|
2015-02-26 20:28:41 +01:00
|
|
|
virtual bool Filter(int level, const Slice& key, const Slice& existing_value,
|
2014-01-15 01:19:09 +01:00
|
|
|
std::string* new_value,
|
2015-02-26 20:28:41 +01:00
|
|
|
bool* value_changed) const override {
|
2014-01-15 01:19:09 +01:00
|
|
|
return existing_value.ToString() == "destroy";
|
|
|
|
}
|
|
|
|
|
2015-02-26 20:28:41 +01:00
|
|
|
virtual const char* Name() const override {
|
2014-01-15 01:19:09 +01:00
|
|
|
return "DestroyAllCompactionFilter";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
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 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.
|
2013-10-04 06:49:15 +02:00
|
|
|
rocksdb::DB* db;
|
|
|
|
rocksdb::Options db_options;
|
2013-06-17 22:58:17 +02:00
|
|
|
db_options.create_if_missing = true;
|
2013-10-04 06:49:15 +02:00
|
|
|
db_options.compression = rocksdb::kNoCompression;
|
2014-01-15 01:19:09 +01:00
|
|
|
ASSERT_OK(rocksdb::DB::Open(db_options, dbname_, &db));
|
2013-06-17 22:58:17 +02:00
|
|
|
|
|
|
|
// create first key range
|
2013-10-04 06:49:15 +02:00
|
|
|
rocksdb::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");
|
|
|
|
}
|
2013-10-04 06:49:15 +02:00
|
|
|
ASSERT_OK(db->Write(rocksdb::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");
|
|
|
|
}
|
2013-10-04 06:49:15 +02:00
|
|
|
ASSERT_OK(db->Write(rocksdb::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));
|
|
|
|
}
|
2013-10-04 06:49:15 +02:00
|
|
|
ASSERT_OK(db->Write(rocksdb::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);
|
2013-10-04 06:49:15 +02:00
|
|
|
rocksdb::Slice least(start_key.data(), start_key.size());
|
|
|
|
rocksdb::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
|
2013-10-04 06:49:15 +02:00
|
|
|
rocksdb::Iterator* iter = db->NewIterator(rocksdb::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;
|
2014-01-15 01:19:09 +01:00
|
|
|
DestroyDB(dbname_, rocksdb::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
|
|
|
}
|