2015-03-11 21:06:59 +01:00
|
|
|
// Copyright (c) 2014, 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.
|
|
|
|
|
2015-07-20 20:24:54 +02:00
|
|
|
#ifndef ROCKSDB_LITE
|
|
|
|
|
2015-03-11 21:06:59 +01:00
|
|
|
#include <mutex>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "rocksdb/db.h"
|
|
|
|
#include "rocksdb/env.h"
|
2015-06-08 20:43:55 +02:00
|
|
|
#include "util/string_util.h"
|
2015-11-13 21:01:00 +01:00
|
|
|
#include "util/sync_point.h"
|
2015-03-11 21:06:59 +01:00
|
|
|
#include "util/testharness.h"
|
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
class CompactFilesTest : public testing::Test {
|
2015-03-11 21:06:59 +01:00
|
|
|
public:
|
|
|
|
CompactFilesTest() {
|
|
|
|
env_ = Env::Default();
|
|
|
|
db_name_ = test::TmpDir(env_) + "/compact_files_test";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string db_name_;
|
|
|
|
Env* env_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// A class which remembers the name of each flushed file.
|
|
|
|
class FlushedFileCollector : public EventListener {
|
|
|
|
public:
|
|
|
|
FlushedFileCollector() {}
|
|
|
|
~FlushedFileCollector() {}
|
|
|
|
|
|
|
|
virtual void OnFlushCompleted(
|
2015-06-05 21:28:51 +02:00
|
|
|
DB* db, const FlushJobInfo& info) override {
|
2015-03-11 21:06:59 +01:00
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
2015-06-05 21:28:51 +02:00
|
|
|
flushed_files_.push_back(info.file_path);
|
2015-03-11 21:06:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> GetFlushedFiles() {
|
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
std::vector<std::string> result;
|
|
|
|
for (auto fname : flushed_files_) {
|
|
|
|
result.push_back(fname);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<std::string> flushed_files_;
|
|
|
|
std::mutex mutex_;
|
|
|
|
};
|
|
|
|
|
2015-11-13 21:01:00 +01:00
|
|
|
TEST_F(CompactFilesTest, L0ConflictsFiles) {
|
|
|
|
Options options;
|
|
|
|
// to trigger compaction more easily
|
|
|
|
const int kWriteBufferSize = 10000;
|
|
|
|
const int kLevel0Trigger = 2;
|
|
|
|
options.create_if_missing = true;
|
|
|
|
options.compaction_style = kCompactionStyleLevel;
|
|
|
|
// Small slowdown and stop trigger for experimental purpose.
|
|
|
|
options.level0_slowdown_writes_trigger = 20;
|
|
|
|
options.level0_stop_writes_trigger = 20;
|
|
|
|
options.level0_stop_writes_trigger = 20;
|
|
|
|
options.write_buffer_size = kWriteBufferSize;
|
|
|
|
options.level0_file_num_compaction_trigger = kLevel0Trigger;
|
|
|
|
options.compression = kNoCompression;
|
|
|
|
|
|
|
|
DB* db = nullptr;
|
|
|
|
DestroyDB(db_name_, options);
|
|
|
|
Status s = DB::Open(options, db_name_, &db);
|
|
|
|
assert(s.ok());
|
|
|
|
assert(db);
|
|
|
|
|
|
|
|
rocksdb::SyncPoint::GetInstance()->LoadDependency({
|
|
|
|
{"CompactFilesImpl:0", "BackgroundCallCompaction:0"},
|
|
|
|
{"BackgroundCallCompaction:1", "CompactFilesImpl:1"},
|
|
|
|
});
|
|
|
|
rocksdb::SyncPoint::GetInstance()->EnableProcessing();
|
|
|
|
|
|
|
|
// create couple files
|
|
|
|
// Background compaction starts and waits in BackgroundCallCompaction:0
|
|
|
|
for (int i = 0; i < kLevel0Trigger * 4; ++i) {
|
|
|
|
db->Put(WriteOptions(), ToString(i), "");
|
|
|
|
db->Put(WriteOptions(), ToString(100 - i), "");
|
|
|
|
db->Flush(FlushOptions());
|
|
|
|
}
|
|
|
|
|
|
|
|
rocksdb::ColumnFamilyMetaData meta;
|
|
|
|
db->GetColumnFamilyMetaData(&meta);
|
|
|
|
std::string file1;
|
|
|
|
for (auto& file : meta.levels[0].files) {
|
|
|
|
ASSERT_EQ(0, meta.levels[0].level);
|
|
|
|
if (file1 == "") {
|
|
|
|
file1 = file.db_path + "/" + file.name;
|
|
|
|
} else {
|
|
|
|
std::string file2 = file.db_path + "/" + file.name;
|
|
|
|
// Another thread starts a compact files and creates an L0 compaction
|
|
|
|
// The background compaction then notices that there is an L0 compaction
|
|
|
|
// already in progress and doesn't do an L0 compaction
|
|
|
|
// Once the background compaction finishes, the compact files finishes
|
|
|
|
ASSERT_OK(
|
|
|
|
db->CompactFiles(rocksdb::CompactionOptions(), {file1, file2}, 0));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete db;
|
|
|
|
}
|
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(CompactFilesTest, ObsoleteFiles) {
|
2015-03-11 21:06:59 +01:00
|
|
|
Options options;
|
|
|
|
// to trigger compaction more easily
|
|
|
|
const int kWriteBufferSize = 10000;
|
|
|
|
options.create_if_missing = true;
|
|
|
|
// Disable RocksDB background compaction.
|
|
|
|
options.compaction_style = kCompactionStyleNone;
|
|
|
|
// Small slowdown and stop trigger for experimental purpose.
|
|
|
|
options.level0_slowdown_writes_trigger = 20;
|
|
|
|
options.level0_stop_writes_trigger = 20;
|
|
|
|
options.write_buffer_size = kWriteBufferSize;
|
|
|
|
options.max_write_buffer_number = 2;
|
|
|
|
options.compression = kNoCompression;
|
|
|
|
|
|
|
|
// Add listener
|
|
|
|
FlushedFileCollector* collector = new FlushedFileCollector();
|
|
|
|
options.listeners.emplace_back(collector);
|
|
|
|
|
|
|
|
DB* db = nullptr;
|
|
|
|
DestroyDB(db_name_, options);
|
|
|
|
Status s = DB::Open(options, db_name_, &db);
|
|
|
|
assert(s.ok());
|
|
|
|
assert(db);
|
|
|
|
|
|
|
|
// create couple files
|
|
|
|
for (int i = 1000; i < 2000; ++i) {
|
2015-06-08 20:43:55 +02:00
|
|
|
db->Put(WriteOptions(), ToString(i),
|
|
|
|
std::string(kWriteBufferSize / 10, 'a' + (i % 26)));
|
2015-03-11 21:06:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto l0_files = collector->GetFlushedFiles();
|
|
|
|
CompactionOptions compact_opt;
|
|
|
|
compact_opt.compression = kNoCompression;
|
|
|
|
compact_opt.output_file_size_limit = kWriteBufferSize * 5;
|
|
|
|
ASSERT_OK(db->CompactFiles(CompactionOptions(), l0_files, 1));
|
|
|
|
|
|
|
|
// verify all compaction input files are deleted
|
|
|
|
for (auto fname : l0_files) {
|
2015-07-21 02:20:40 +02:00
|
|
|
ASSERT_EQ(Status::NotFound(), env_->FileExists(fname));
|
2015-03-11 21:06:59 +01:00
|
|
|
}
|
|
|
|
delete db;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace rocksdb
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2015-03-17 22:08:00 +01:00
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
|
|
return RUN_ALL_TESTS();
|
2015-03-11 21:06:59 +01:00
|
|
|
}
|
2015-07-20 20:24:54 +02:00
|
|
|
|
|
|
|
#else
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"SKIPPED as DBImpl::CompactFiles is not supported in ROCKSDB_LITE\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // !ROCKSDB_LITE
|