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).
|
2015-03-11 21:06:59 +01:00
|
|
|
|
2015-07-20 20:24:54 +02:00
|
|
|
#ifndef ROCKSDB_LITE
|
|
|
|
|
2015-03-11 21:06:59 +01:00
|
|
|
#include <mutex>
|
|
|
|
#include <string>
|
2016-02-22 22:54:58 +01:00
|
|
|
#include <thread>
|
2015-03-11 21:06:59 +01:00
|
|
|
#include <vector>
|
|
|
|
|
2019-05-31 20:52:59 +02:00
|
|
|
#include "db/db_impl/db_impl.h"
|
2017-02-06 23:43:55 +01:00
|
|
|
#include "port/port.h"
|
2015-03-11 21:06:59 +01:00
|
|
|
#include "rocksdb/db.h"
|
|
|
|
#include "rocksdb/env.h"
|
2019-05-30 20:21:38 +02:00
|
|
|
#include "test_util/sync_point.h"
|
|
|
|
#include "test_util/testharness.h"
|
2020-07-03 04:24:25 +02:00
|
|
|
#include "util/cast_util.h"
|
2019-05-31 02:39:43 +02:00
|
|
|
#include "util/string_util.h"
|
2015-03-11 21:06:59 +01:00
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2015-03-11 21:06:59 +01:00
|
|
|
|
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();
|
2018-07-14 02:18:39 +02:00
|
|
|
db_name_ = test::PerThreadDBPath("compact_files_test");
|
2015-03-11 21:06:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string db_name_;
|
|
|
|
Env* env_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// A class which remembers the name of each flushed file.
|
|
|
|
class FlushedFileCollector : public EventListener {
|
|
|
|
public:
|
|
|
|
FlushedFileCollector() {}
|
2019-02-14 22:52:47 +01:00
|
|
|
~FlushedFileCollector() override {}
|
2015-03-11 21:06:59 +01:00
|
|
|
|
2019-02-14 22:52:47 +01:00
|
|
|
void OnFlushCompleted(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;
|
|
|
|
}
|
2016-10-19 22:37:51 +02:00
|
|
|
void ClearFlushedFiles() {
|
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
flushed_files_.clear();
|
|
|
|
}
|
2015-03-11 21:06:59 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->LoadDependency({
|
2015-11-13 21:01:00 +01:00
|
|
|
{"CompactFilesImpl:0", "BackgroundCallCompaction:0"},
|
|
|
|
{"BackgroundCallCompaction:1", "CompactFilesImpl:1"},
|
|
|
|
});
|
2020-02-20 21:07:53 +01:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
2015-11-13 21:01:00 +01:00
|
|
|
|
|
|
|
// create couple files
|
|
|
|
// Background compaction starts and waits in BackgroundCallCompaction:0
|
|
|
|
for (int i = 0; i < kLevel0Trigger * 4; ++i) {
|
2022-05-06 22:03:58 +02:00
|
|
|
ASSERT_OK(db->Put(WriteOptions(), std::to_string(i), ""));
|
|
|
|
ASSERT_OK(db->Put(WriteOptions(), std::to_string(100 - i), ""));
|
2020-12-23 08:44:44 +01:00
|
|
|
ASSERT_OK(db->Flush(FlushOptions()));
|
2015-11-13 21:01:00 +01:00
|
|
|
}
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
ROCKSDB_NAMESPACE::ColumnFamilyMetaData meta;
|
2015-11-13 21:01:00 +01:00
|
|
|
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
|
2020-02-20 21:07:53 +01:00
|
|
|
ASSERT_OK(db->CompactFiles(ROCKSDB_NAMESPACE::CompactionOptions(),
|
|
|
|
{file1, file2}, 0));
|
2015-11-13 21:01:00 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-02-20 21:07:53 +01:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
|
2015-11-13 21:01:00 +01:00
|
|
|
delete db;
|
|
|
|
}
|
|
|
|
|
2021-03-25 05:17:06 +01:00
|
|
|
TEST_F(CompactFilesTest, MultipleLevel) {
|
|
|
|
Options options;
|
|
|
|
options.create_if_missing = true;
|
|
|
|
options.level_compaction_dynamic_level_bytes = true;
|
|
|
|
options.num_levels = 6;
|
|
|
|
// 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_OK(s);
|
|
|
|
ASSERT_NE(db, nullptr);
|
|
|
|
|
|
|
|
// create couple files in L0, L3, L4 and L5
|
|
|
|
for (int i = 5; i > 2; --i) {
|
|
|
|
collector->ClearFlushedFiles();
|
2022-05-06 22:03:58 +02:00
|
|
|
ASSERT_OK(db->Put(WriteOptions(), std::to_string(i), ""));
|
2021-03-25 05:17:06 +01:00
|
|
|
ASSERT_OK(db->Flush(FlushOptions()));
|
2022-02-22 21:13:39 +01:00
|
|
|
// Ensure background work is fully finished including listener callbacks
|
|
|
|
// before accessing listener state.
|
|
|
|
ASSERT_OK(static_cast_with_check<DBImpl>(db)->TEST_WaitForBackgroundWork());
|
2021-03-25 05:17:06 +01:00
|
|
|
auto l0_files = collector->GetFlushedFiles();
|
|
|
|
ASSERT_OK(db->CompactFiles(CompactionOptions(), l0_files, i));
|
|
|
|
|
|
|
|
std::string prop;
|
2022-05-06 22:03:58 +02:00
|
|
|
ASSERT_TRUE(db->GetProperty(
|
|
|
|
"rocksdb.num-files-at-level" + std::to_string(i), &prop));
|
2021-03-25 05:17:06 +01:00
|
|
|
ASSERT_EQ("1", prop);
|
|
|
|
}
|
2022-05-06 22:03:58 +02:00
|
|
|
ASSERT_OK(db->Put(WriteOptions(), std::to_string(0), ""));
|
2021-03-25 05:17:06 +01:00
|
|
|
ASSERT_OK(db->Flush(FlushOptions()));
|
|
|
|
|
2021-03-30 02:09:31 +02:00
|
|
|
ColumnFamilyMetaData meta;
|
2021-03-25 05:17:06 +01:00
|
|
|
db->GetColumnFamilyMetaData(&meta);
|
|
|
|
// Compact files except the file in L3
|
|
|
|
std::vector<std::string> files;
|
|
|
|
for (int i = 0; i < 6; ++i) {
|
|
|
|
if (i == 3) continue;
|
|
|
|
for (auto& file : meta.levels[i].files) {
|
|
|
|
files.push_back(file.db_path + "/" + file.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-30 02:09:31 +02:00
|
|
|
SyncPoint::GetInstance()->LoadDependency({
|
2021-03-25 05:17:06 +01:00
|
|
|
{"CompactionJob::Run():Start", "CompactFilesTest.MultipleLevel:0"},
|
|
|
|
{"CompactFilesTest.MultipleLevel:1", "CompactFilesImpl:3"},
|
|
|
|
});
|
2021-03-30 02:09:31 +02:00
|
|
|
SyncPoint::GetInstance()->EnableProcessing();
|
2021-03-25 05:17:06 +01:00
|
|
|
|
|
|
|
std::thread thread([&] {
|
|
|
|
TEST_SYNC_POINT("CompactFilesTest.MultipleLevel:0");
|
|
|
|
ASSERT_OK(db->Put(WriteOptions(), "bar", "v2"));
|
|
|
|
ASSERT_OK(db->Put(WriteOptions(), "foo", "v2"));
|
|
|
|
ASSERT_OK(db->Flush(FlushOptions()));
|
|
|
|
TEST_SYNC_POINT("CompactFilesTest.MultipleLevel:1");
|
|
|
|
});
|
|
|
|
|
2021-03-30 02:09:31 +02:00
|
|
|
// Compaction cannot move up the data to higher level
|
|
|
|
// here we have input file from level 5, so the output level has to be >= 5
|
|
|
|
for (int invalid_output_level = 0; invalid_output_level < 5;
|
|
|
|
invalid_output_level++) {
|
|
|
|
s = db->CompactFiles(CompactionOptions(), files, invalid_output_level);
|
|
|
|
std::cout << s.ToString() << std::endl;
|
|
|
|
ASSERT_TRUE(s.IsInvalidArgument());
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT_OK(db->CompactFiles(CompactionOptions(), files, 5));
|
|
|
|
SyncPoint::GetInstance()->DisableProcessing();
|
2021-03-25 05:17:06 +01:00
|
|
|
thread.join();
|
|
|
|
|
|
|
|
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
|
2016-09-24 02:16:38 +02:00
|
|
|
const int kWriteBufferSize = 65536;
|
2015-03-11 21:06:59 +01:00
|
|
|
options.create_if_missing = true;
|
|
|
|
// Disable RocksDB background compaction.
|
|
|
|
options.compaction_style = kCompactionStyleNone;
|
2016-09-26 19:39:07 +02:00
|
|
|
options.level0_slowdown_writes_trigger = (1 << 30);
|
|
|
|
options.level0_stop_writes_trigger = (1 << 30);
|
2015-03-11 21:06:59 +01:00
|
|
|
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);
|
2020-12-23 08:44:44 +01:00
|
|
|
ASSERT_OK(s);
|
|
|
|
ASSERT_NE(db, nullptr);
|
2015-03-11 21:06:59 +01:00
|
|
|
|
|
|
|
// create couple files
|
|
|
|
for (int i = 1000; i < 2000; ++i) {
|
2022-05-06 22:03:58 +02:00
|
|
|
ASSERT_OK(db->Put(WriteOptions(), std::to_string(i),
|
2020-12-23 08:44:44 +01:00
|
|
|
std::string(kWriteBufferSize / 10, 'a' + (i % 26))));
|
2015-03-11 21:06:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto l0_files = collector->GetFlushedFiles();
|
|
|
|
ASSERT_OK(db->CompactFiles(CompactionOptions(), l0_files, 1));
|
2020-12-23 08:44:44 +01:00
|
|
|
ASSERT_OK(static_cast_with_check<DBImpl>(db)->TEST_WaitForCompact());
|
2015-03-11 21:06:59 +01:00
|
|
|
|
|
|
|
// 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
|
|
|
}
|
2016-09-24 02:16:38 +02:00
|
|
|
delete db;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CompactFilesTest, NotCutOutputOnLevel0) {
|
|
|
|
Options options;
|
|
|
|
options.create_if_missing = true;
|
|
|
|
// Disable RocksDB background compaction.
|
|
|
|
options.compaction_style = kCompactionStyleNone;
|
|
|
|
options.level0_slowdown_writes_trigger = 1000;
|
|
|
|
options.level0_stop_writes_trigger = 1000;
|
|
|
|
options.write_buffer_size = 65536;
|
|
|
|
options.max_write_buffer_number = 2;
|
|
|
|
options.compression = kNoCompression;
|
|
|
|
options.max_compaction_bytes = 5000;
|
|
|
|
|
|
|
|
// 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 = 0; i < 500; ++i) {
|
2022-05-06 22:03:58 +02:00
|
|
|
ASSERT_OK(db->Put(WriteOptions(), std::to_string(i),
|
2020-12-23 08:44:44 +01:00
|
|
|
std::string(1000, 'a' + (i % 26))));
|
2016-09-24 02:16:38 +02:00
|
|
|
}
|
2020-12-23 08:44:44 +01:00
|
|
|
ASSERT_OK(static_cast_with_check<DBImpl>(db)->TEST_WaitForFlushMemTable());
|
2016-09-24 02:16:38 +02:00
|
|
|
auto l0_files_1 = collector->GetFlushedFiles();
|
|
|
|
collector->ClearFlushedFiles();
|
|
|
|
for (int i = 0; i < 500; ++i) {
|
2022-05-06 22:03:58 +02:00
|
|
|
ASSERT_OK(db->Put(WriteOptions(), std::to_string(i),
|
2020-12-23 08:44:44 +01:00
|
|
|
std::string(1000, 'a' + (i % 26))));
|
2016-09-24 02:16:38 +02:00
|
|
|
}
|
2020-12-23 08:44:44 +01:00
|
|
|
ASSERT_OK(static_cast_with_check<DBImpl>(db)->TEST_WaitForFlushMemTable());
|
2016-09-24 02:16:38 +02:00
|
|
|
auto l0_files_2 = collector->GetFlushedFiles();
|
|
|
|
ASSERT_OK(db->CompactFiles(CompactionOptions(), l0_files_1, 0));
|
|
|
|
ASSERT_OK(db->CompactFiles(CompactionOptions(), l0_files_2, 0));
|
|
|
|
// no assertion failure
|
2015-03-11 21:06:59 +01:00
|
|
|
delete db;
|
|
|
|
}
|
|
|
|
|
2016-02-22 22:54:58 +01:00
|
|
|
TEST_F(CompactFilesTest, CapturingPendingFiles) {
|
|
|
|
Options options;
|
|
|
|
options.create_if_missing = true;
|
|
|
|
// Disable RocksDB background compaction.
|
|
|
|
options.compaction_style = kCompactionStyleNone;
|
|
|
|
// Always do full scans for obsolete files (needed to reproduce the issue).
|
|
|
|
options.delete_obsolete_files_period_micros = 0;
|
|
|
|
|
|
|
|
// 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);
|
2020-12-23 08:44:44 +01:00
|
|
|
ASSERT_OK(s);
|
2016-02-22 22:54:58 +01:00
|
|
|
assert(db);
|
|
|
|
|
|
|
|
// Create 5 files.
|
|
|
|
for (int i = 0; i < 5; ++i) {
|
2022-05-06 22:03:58 +02:00
|
|
|
ASSERT_OK(db->Put(WriteOptions(), "key" + std::to_string(i), "value"));
|
2020-12-23 08:44:44 +01:00
|
|
|
ASSERT_OK(db->Flush(FlushOptions()));
|
2016-02-22 22:54:58 +01:00
|
|
|
}
|
|
|
|
|
2022-02-22 21:13:39 +01:00
|
|
|
// Ensure background work is fully finished including listener callbacks
|
|
|
|
// before accessing listener state.
|
|
|
|
ASSERT_OK(static_cast_with_check<DBImpl>(db)->TEST_WaitForBackgroundWork());
|
2016-02-22 22:54:58 +01:00
|
|
|
auto l0_files = collector->GetFlushedFiles();
|
|
|
|
EXPECT_EQ(5, l0_files.size());
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->LoadDependency({
|
2016-02-22 22:54:58 +01:00
|
|
|
{"CompactFilesImpl:2", "CompactFilesTest.CapturingPendingFiles:0"},
|
|
|
|
{"CompactFilesTest.CapturingPendingFiles:1", "CompactFilesImpl:3"},
|
|
|
|
});
|
2020-02-20 21:07:53 +01:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
2016-02-22 22:54:58 +01:00
|
|
|
|
|
|
|
// Start compacting files.
|
2020-02-20 21:07:53 +01:00
|
|
|
ROCKSDB_NAMESPACE::port::Thread compaction_thread(
|
2016-02-22 22:54:58 +01:00
|
|
|
[&] { EXPECT_OK(db->CompactFiles(CompactionOptions(), l0_files, 1)); });
|
|
|
|
|
|
|
|
// In the meantime flush another file.
|
|
|
|
TEST_SYNC_POINT("CompactFilesTest.CapturingPendingFiles:0");
|
2020-12-23 08:44:44 +01:00
|
|
|
ASSERT_OK(db->Put(WriteOptions(), "key5", "value"));
|
|
|
|
ASSERT_OK(db->Flush(FlushOptions()));
|
2016-02-22 22:54:58 +01:00
|
|
|
TEST_SYNC_POINT("CompactFilesTest.CapturingPendingFiles:1");
|
|
|
|
|
|
|
|
compaction_thread.join();
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
|
2016-02-22 22:54:58 +01:00
|
|
|
|
|
|
|
delete db;
|
|
|
|
|
|
|
|
// Make sure we can reopen the DB.
|
|
|
|
s = DB::Open(options, db_name_, &db);
|
2020-12-23 08:44:44 +01:00
|
|
|
ASSERT_OK(s);
|
2016-02-22 22:54:58 +01:00
|
|
|
assert(db);
|
|
|
|
delete db;
|
|
|
|
}
|
|
|
|
|
2017-01-25 23:03:49 +01:00
|
|
|
TEST_F(CompactFilesTest, CompactionFilterWithGetSv) {
|
|
|
|
class FilterWithGet : public CompactionFilter {
|
|
|
|
public:
|
2019-02-14 22:52:47 +01:00
|
|
|
bool Filter(int /*level*/, const Slice& /*key*/, const Slice& /*value*/,
|
|
|
|
std::string* /*new_value*/,
|
|
|
|
bool* /*value_changed*/) const override {
|
2017-01-25 23:03:49 +01:00
|
|
|
if (db_ == nullptr) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
std::string res;
|
|
|
|
db_->Get(ReadOptions(), "", &res);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetDB(DB* db) {
|
|
|
|
db_ = db;
|
|
|
|
}
|
|
|
|
|
2019-02-14 22:52:47 +01:00
|
|
|
const char* Name() const override { return "FilterWithGet"; }
|
2017-01-25 23:03:49 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
DB* db_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<FilterWithGet> cf(new FilterWithGet());
|
|
|
|
|
|
|
|
Options options;
|
|
|
|
options.create_if_missing = true;
|
|
|
|
options.compaction_filter = cf.get();
|
|
|
|
|
|
|
|
DB* db = nullptr;
|
|
|
|
DestroyDB(db_name_, options);
|
|
|
|
Status s = DB::Open(options, db_name_, &db);
|
|
|
|
ASSERT_OK(s);
|
|
|
|
|
|
|
|
cf->SetDB(db);
|
|
|
|
|
|
|
|
// Write one L0 file
|
2020-12-23 08:44:44 +01:00
|
|
|
ASSERT_OK(db->Put(WriteOptions(), "K1", "V1"));
|
|
|
|
ASSERT_OK(db->Flush(FlushOptions()));
|
2017-01-25 23:03:49 +01:00
|
|
|
|
|
|
|
// Compact all L0 files using CompactFiles
|
2020-02-20 21:07:53 +01:00
|
|
|
ROCKSDB_NAMESPACE::ColumnFamilyMetaData meta;
|
2017-01-25 23:03:49 +01:00
|
|
|
db->GetColumnFamilyMetaData(&meta);
|
|
|
|
for (auto& file : meta.levels[0].files) {
|
|
|
|
std::string fname = file.db_path + "/" + file.name;
|
|
|
|
ASSERT_OK(
|
2020-02-20 21:07:53 +01:00
|
|
|
db->CompactFiles(ROCKSDB_NAMESPACE::CompactionOptions(), {fname}, 0));
|
2017-01-25 23:03:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
delete db;
|
|
|
|
}
|
|
|
|
|
2018-10-01 10:16:16 +02:00
|
|
|
TEST_F(CompactFilesTest, SentinelCompressionType) {
|
Check for compression lib support before test exec (#4443)
Summary:
Before running CompactFilesTest.SentinelCompressionType, we should check
whether zlib and snappy are supported.
CompactFilesTest.SentinelCompressionType is a newly added test. Compilation and
linking with different options, e.g. COMPILE_WITH_TSAN, COMPILE_WITH_ASAN, etc.
lead to generation of different binaries. On the one hand, it's not clear why
zlib or snappy is present under ASAN, but not under TSAN. On the other hand,
changing the compilation flags for TSAN or ASAN seems a bigger change worth much
more attention. To unblock the cont-runs, I suggest that we simply add these
two checks at the beginning of the test, as we did for
GeneralTableTest.ApproximateOffsetOfCompressed in table/table_test.cc.
Future actions include invesigating the absence of zlib and snappy when
compiling with TSAN, i.e. COMPILE_WITH_TSAN=1, if necessary.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/4443
Differential Revision: D10140935
Pulled By: riversand963
fbshipit-source-id: 62f96d1e685386accd2ef0b98f6f754d3fd67b3e
2018-10-02 19:40:15 +02:00
|
|
|
if (!Zlib_Supported()) {
|
|
|
|
fprintf(stderr, "zlib compression not supported, skip this test\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!Snappy_Supported()) {
|
|
|
|
fprintf(stderr, "snappy compression not supported, skip this test\n");
|
|
|
|
return;
|
|
|
|
}
|
2018-10-01 10:16:16 +02:00
|
|
|
// Check that passing `CompressionType::kDisableCompressionOption` to
|
|
|
|
// `CompactFiles` causes it to use the column family compression options.
|
|
|
|
for (auto compaction_style :
|
|
|
|
{CompactionStyle::kCompactionStyleLevel,
|
|
|
|
CompactionStyle::kCompactionStyleUniversal,
|
|
|
|
CompactionStyle::kCompactionStyleNone}) {
|
|
|
|
DestroyDB(db_name_, Options());
|
|
|
|
Options options;
|
|
|
|
options.compaction_style = compaction_style;
|
|
|
|
// L0: Snappy, L1: ZSTD, L2: Snappy
|
|
|
|
options.compression_per_level = {CompressionType::kSnappyCompression,
|
|
|
|
CompressionType::kZlibCompression,
|
|
|
|
CompressionType::kSnappyCompression};
|
|
|
|
options.create_if_missing = true;
|
|
|
|
FlushedFileCollector* collector = new FlushedFileCollector();
|
|
|
|
options.listeners.emplace_back(collector);
|
|
|
|
DB* db = nullptr;
|
|
|
|
ASSERT_OK(DB::Open(options, db_name_, &db));
|
|
|
|
|
2020-12-23 08:44:44 +01:00
|
|
|
ASSERT_OK(db->Put(WriteOptions(), "key", "val"));
|
|
|
|
ASSERT_OK(db->Flush(FlushOptions()));
|
2018-10-01 10:16:16 +02:00
|
|
|
|
2022-02-22 21:13:39 +01:00
|
|
|
// Ensure background work is fully finished including listener callbacks
|
|
|
|
// before accessing listener state.
|
|
|
|
ASSERT_OK(static_cast_with_check<DBImpl>(db)->TEST_WaitForBackgroundWork());
|
2018-10-01 10:16:16 +02:00
|
|
|
auto l0_files = collector->GetFlushedFiles();
|
|
|
|
ASSERT_EQ(1, l0_files.size());
|
|
|
|
|
|
|
|
// L0->L1 compaction, so output should be ZSTD-compressed
|
|
|
|
CompactionOptions compaction_opts;
|
|
|
|
compaction_opts.compression = CompressionType::kDisableCompressionOption;
|
|
|
|
ASSERT_OK(db->CompactFiles(compaction_opts, l0_files, 1));
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
ROCKSDB_NAMESPACE::TablePropertiesCollection all_tables_props;
|
2018-10-01 10:16:16 +02:00
|
|
|
ASSERT_OK(db->GetPropertiesOfAllTables(&all_tables_props));
|
|
|
|
for (const auto& name_and_table_props : all_tables_props) {
|
|
|
|
ASSERT_EQ(CompressionTypeToString(CompressionType::kZlibCompression),
|
|
|
|
name_and_table_props.second->compression_name);
|
|
|
|
}
|
|
|
|
delete db;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-13 23:12:02 +01:00
|
|
|
TEST_F(CompactFilesTest, GetCompactionJobInfo) {
|
|
|
|
Options options;
|
|
|
|
options.create_if_missing = true;
|
|
|
|
// Disable RocksDB background compaction.
|
|
|
|
options.compaction_style = kCompactionStyleNone;
|
|
|
|
options.level0_slowdown_writes_trigger = 1000;
|
|
|
|
options.level0_stop_writes_trigger = 1000;
|
|
|
|
options.write_buffer_size = 65536;
|
|
|
|
options.max_write_buffer_number = 2;
|
|
|
|
options.compression = kNoCompression;
|
|
|
|
options.max_compaction_bytes = 5000;
|
|
|
|
|
|
|
|
// 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);
|
2020-12-23 08:44:44 +01:00
|
|
|
ASSERT_OK(s);
|
2018-12-13 23:12:02 +01:00
|
|
|
assert(db);
|
|
|
|
|
|
|
|
// create couple files
|
|
|
|
for (int i = 0; i < 500; ++i) {
|
2022-05-06 22:03:58 +02:00
|
|
|
ASSERT_OK(db->Put(WriteOptions(), std::to_string(i),
|
2020-12-23 08:44:44 +01:00
|
|
|
std::string(1000, 'a' + (i % 26))));
|
2018-12-13 23:12:02 +01:00
|
|
|
}
|
2020-12-23 08:44:44 +01:00
|
|
|
ASSERT_OK(static_cast_with_check<DBImpl>(db)->TEST_WaitForFlushMemTable());
|
2018-12-13 23:12:02 +01:00
|
|
|
auto l0_files_1 = collector->GetFlushedFiles();
|
|
|
|
CompactionOptions co;
|
|
|
|
co.compression = CompressionType::kLZ4Compression;
|
2019-11-01 19:44:59 +01:00
|
|
|
CompactionJobInfo compaction_job_info{};
|
2018-12-13 23:12:02 +01:00
|
|
|
ASSERT_OK(
|
|
|
|
db->CompactFiles(co, l0_files_1, 0, -1, nullptr, &compaction_job_info));
|
|
|
|
ASSERT_EQ(compaction_job_info.base_input_level, 0);
|
|
|
|
ASSERT_EQ(compaction_job_info.cf_id, db->DefaultColumnFamily()->GetID());
|
|
|
|
ASSERT_EQ(compaction_job_info.cf_name, db->DefaultColumnFamily()->GetName());
|
|
|
|
ASSERT_EQ(compaction_job_info.compaction_reason,
|
|
|
|
CompactionReason::kManualCompaction);
|
|
|
|
ASSERT_EQ(compaction_job_info.compression, CompressionType::kLZ4Compression);
|
|
|
|
ASSERT_EQ(compaction_job_info.output_level, 0);
|
|
|
|
ASSERT_OK(compaction_job_info.status);
|
|
|
|
// no assertion failure
|
|
|
|
delete db;
|
|
|
|
}
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
2015-03-11 21:06:59 +01:00
|
|
|
|
|
|
|
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>
|
|
|
|
|
2018-04-16 02:19:57 +02:00
|
|
|
int main(int /*argc*/, char** /*argv*/) {
|
2015-07-20 20:24:54 +02:00
|
|
|
fprintf(stderr,
|
|
|
|
"SKIPPED as DBImpl::CompactFiles is not supported in ROCKSDB_LITE\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // !ROCKSDB_LITE
|