2014-10-27 22:49:46 +00: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.
|
|
|
|
|
|
|
|
#include "db/compaction_picker.h"
|
|
|
|
#include <string>
|
|
|
|
#include "util/logging.h"
|
|
|
|
#include "util/testharness.h"
|
|
|
|
#include "util/testutil.h"
|
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
|
|
|
|
class CountingLogger : public Logger {
|
|
|
|
public:
|
|
|
|
virtual void Logv(const char* format, va_list ap) override { log_count++; }
|
|
|
|
size_t log_count;
|
|
|
|
};
|
|
|
|
|
|
|
|
class CompactionPickerTest {
|
|
|
|
public:
|
2014-11-13 21:41:43 +00:00
|
|
|
const Comparator* ucmp_;
|
|
|
|
InternalKeyComparator icmp_;
|
|
|
|
Options options_;
|
|
|
|
ImmutableCFOptions ioptions_;
|
|
|
|
MutableCFOptions mutable_cf_options_;
|
2014-10-27 22:49:46 +00:00
|
|
|
LevelCompactionPicker level_compaction_picker;
|
2014-11-13 21:41:43 +00:00
|
|
|
std::string cf_name_;
|
|
|
|
CountingLogger logger_;
|
|
|
|
LogBuffer log_buffer_;
|
|
|
|
uint32_t file_num_;
|
|
|
|
CompactionOptionsFIFO fifo_options_;
|
|
|
|
std::vector<uint64_t> size_being_compacted_;
|
|
|
|
std::unique_ptr<VersionStorageInfo> vstorage_;
|
|
|
|
std::vector<std::unique_ptr<FileMetaData>> files_;
|
2014-10-27 22:49:46 +00:00
|
|
|
|
|
|
|
CompactionPickerTest()
|
2014-11-13 21:41:43 +00:00
|
|
|
: ucmp_(BytewiseComparator()),
|
|
|
|
icmp_(ucmp_),
|
|
|
|
ioptions_(options_),
|
|
|
|
mutable_cf_options_(options_, ioptions_),
|
|
|
|
level_compaction_picker(ioptions_, &icmp_),
|
|
|
|
cf_name_("dummy"),
|
|
|
|
log_buffer_(InfoLogLevel::INFO_LEVEL, &logger_),
|
|
|
|
file_num_(1),
|
|
|
|
vstorage_(nullptr) {
|
|
|
|
fifo_options_.max_table_files_size = 1;
|
|
|
|
mutable_cf_options_.RefreshDerivedOptions(ioptions_);
|
|
|
|
size_being_compacted_.resize(options_.num_levels);
|
2014-10-27 22:49:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~CompactionPickerTest() {
|
2014-11-13 21:41:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NewVersionStorage(int num_levels, CompactionStyle style) {
|
|
|
|
DeleteVersionStorage();
|
|
|
|
options_.num_levels = num_levels;
|
|
|
|
vstorage_.reset(new VersionStorageInfo(
|
|
|
|
&icmp_, ucmp_, options_.num_levels, style, nullptr));
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeleteVersionStorage() {
|
|
|
|
vstorage_.reset();
|
|
|
|
files_.clear();
|
2014-10-27 22:49:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Add(int level, uint32_t file_number, const char* smallest,
|
|
|
|
const char* largest, uint64_t file_size = 0, uint32_t path_id = 0,
|
|
|
|
SequenceNumber smallest_seq = 100,
|
|
|
|
SequenceNumber largest_seq = 100) {
|
2014-11-13 21:41:43 +00:00
|
|
|
assert(level < vstorage_->num_levels());
|
2014-10-27 22:49:46 +00:00
|
|
|
FileMetaData* f = new FileMetaData;
|
|
|
|
f->fd = FileDescriptor(file_number, path_id, file_size);
|
|
|
|
f->smallest = InternalKey(smallest, smallest_seq, kTypeValue);
|
|
|
|
f->largest = InternalKey(largest, largest_seq, kTypeValue);
|
|
|
|
f->compensated_file_size = file_size;
|
2014-10-31 15:48:19 +00:00
|
|
|
f->refs = 0;
|
2014-11-13 21:41:43 +00:00
|
|
|
vstorage_->AddFile(level, f);
|
|
|
|
files_.emplace_back(f);
|
2014-10-27 22:49:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateVersionStorageInfo() {
|
2014-11-13 21:41:43 +00:00
|
|
|
vstorage_->ComputeCompactionScore(mutable_cf_options_, fifo_options_,
|
|
|
|
size_being_compacted_);
|
|
|
|
vstorage_->UpdateFilesBySize();
|
|
|
|
vstorage_->UpdateNumNonEmptyLevels();
|
|
|
|
vstorage_->GenerateFileIndexer();
|
|
|
|
vstorage_->GenerateLevelFilesBrief();
|
|
|
|
vstorage_->SetFinalized();
|
2014-10-27 22:49:46 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(CompactionPickerTest, Empty) {
|
2014-11-13 21:41:43 +00:00
|
|
|
NewVersionStorage(6, kCompactionStyleLevel);
|
2014-10-27 22:49:46 +00:00
|
|
|
UpdateVersionStorageInfo();
|
|
|
|
std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
|
2014-11-13 21:41:43 +00:00
|
|
|
cf_name_, mutable_cf_options_, vstorage_.get(), &log_buffer_));
|
2014-10-27 22:49:46 +00:00
|
|
|
ASSERT_TRUE(compaction.get() == nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CompactionPickerTest, Single) {
|
2014-11-13 21:41:43 +00:00
|
|
|
NewVersionStorage(6, kCompactionStyleLevel);
|
|
|
|
mutable_cf_options_.level0_file_num_compaction_trigger = 2;
|
2014-10-27 22:49:46 +00:00
|
|
|
Add(0, 1U, "p", "q");
|
|
|
|
UpdateVersionStorageInfo();
|
|
|
|
|
|
|
|
std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
|
2014-11-13 21:41:43 +00:00
|
|
|
cf_name_, mutable_cf_options_, vstorage_.get(), &log_buffer_));
|
2014-10-27 22:49:46 +00:00
|
|
|
ASSERT_TRUE(compaction.get() == nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CompactionPickerTest, Level0Trigger) {
|
2014-11-13 21:41:43 +00:00
|
|
|
NewVersionStorage(6, kCompactionStyleLevel);
|
|
|
|
mutable_cf_options_.level0_file_num_compaction_trigger = 2;
|
2014-10-27 22:49:46 +00:00
|
|
|
Add(0, 1U, "150", "200");
|
|
|
|
Add(0, 2U, "200", "250");
|
|
|
|
|
|
|
|
UpdateVersionStorageInfo();
|
|
|
|
|
|
|
|
std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
|
2014-11-13 21:41:43 +00:00
|
|
|
cf_name_, mutable_cf_options_, vstorage_.get(), &log_buffer_));
|
2014-10-27 22:49:46 +00:00
|
|
|
ASSERT_TRUE(compaction.get() != nullptr);
|
2014-11-11 21:47:22 +00:00
|
|
|
ASSERT_EQ(2U, compaction->num_input_files(0));
|
2014-10-27 22:49:46 +00:00
|
|
|
ASSERT_EQ(1U, compaction->input(0, 0)->fd.GetNumber());
|
|
|
|
ASSERT_EQ(2U, compaction->input(0, 1)->fd.GetNumber());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CompactionPickerTest, Level1Trigger) {
|
2014-11-13 21:41:43 +00:00
|
|
|
NewVersionStorage(6, kCompactionStyleLevel);
|
2014-10-27 22:49:46 +00:00
|
|
|
Add(1, 66U, "150", "200", 1000000000U);
|
|
|
|
UpdateVersionStorageInfo();
|
|
|
|
|
|
|
|
std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
|
2014-11-13 21:41:43 +00:00
|
|
|
cf_name_, mutable_cf_options_, vstorage_.get(), &log_buffer_));
|
2014-10-27 22:49:46 +00:00
|
|
|
ASSERT_TRUE(compaction.get() != nullptr);
|
2014-11-11 21:47:22 +00:00
|
|
|
ASSERT_EQ(1U, compaction->num_input_files(0));
|
2014-10-27 22:49:46 +00:00
|
|
|
ASSERT_EQ(66U, compaction->input(0, 0)->fd.GetNumber());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CompactionPickerTest, Level1Trigger2) {
|
2014-11-13 21:41:43 +00:00
|
|
|
NewVersionStorage(6, kCompactionStyleLevel);
|
2014-10-30 04:03:45 +00:00
|
|
|
Add(1, 66U, "150", "200", 1000000001U);
|
2014-10-27 22:49:46 +00:00
|
|
|
Add(1, 88U, "201", "300", 1000000000U);
|
2014-10-29 23:45:07 +00:00
|
|
|
Add(2, 6U, "150", "179", 1000000000U);
|
2014-10-27 22:49:46 +00:00
|
|
|
Add(2, 7U, "180", "220", 1000000000U);
|
2014-10-29 23:45:07 +00:00
|
|
|
Add(2, 8U, "221", "300", 1000000000U);
|
2014-10-27 22:49:46 +00:00
|
|
|
UpdateVersionStorageInfo();
|
|
|
|
|
|
|
|
std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
|
2014-11-13 21:41:43 +00:00
|
|
|
cf_name_, mutable_cf_options_, vstorage_.get(), &log_buffer_));
|
2014-10-27 22:49:46 +00:00
|
|
|
ASSERT_TRUE(compaction.get() != nullptr);
|
2014-11-11 21:47:22 +00:00
|
|
|
ASSERT_EQ(1U, compaction->num_input_files(0));
|
|
|
|
ASSERT_EQ(2U, compaction->num_input_files(1));
|
2014-10-27 22:49:46 +00:00
|
|
|
ASSERT_EQ(66U, compaction->input(0, 0)->fd.GetNumber());
|
|
|
|
ASSERT_EQ(6U, compaction->input(1, 0)->fd.GetNumber());
|
|
|
|
ASSERT_EQ(7U, compaction->input(1, 1)->fd.GetNumber());
|
|
|
|
}
|
|
|
|
|
2014-10-29 23:45:07 +00:00
|
|
|
TEST(CompactionPickerTest, LevelMaxScore) {
|
2014-11-13 21:41:43 +00:00
|
|
|
NewVersionStorage(6, kCompactionStyleLevel);
|
|
|
|
mutable_cf_options_.target_file_size_base = 10000000;
|
|
|
|
mutable_cf_options_.target_file_size_multiplier = 10;
|
2014-10-29 23:45:07 +00:00
|
|
|
Add(0, 1U, "150", "200", 1000000000U);
|
|
|
|
// Level 1 score 1.2
|
|
|
|
Add(1, 66U, "150", "200", 6000000U);
|
|
|
|
Add(1, 88U, "201", "300", 6000000U);
|
|
|
|
// Level 2 score 1.8. File 7 is the largest. Should be picked
|
|
|
|
Add(2, 6U, "150", "179", 60000000U);
|
|
|
|
Add(2, 7U, "180", "220", 60000001U);
|
|
|
|
Add(2, 8U, "221", "300", 60000000U);
|
|
|
|
// Level 3 score slightly larger than 1
|
|
|
|
Add(3, 26U, "150", "170", 260000000U);
|
|
|
|
Add(3, 27U, "171", "179", 260000000U);
|
|
|
|
Add(3, 28U, "191", "220", 260000000U);
|
|
|
|
Add(3, 29U, "221", "300", 260000000U);
|
|
|
|
UpdateVersionStorageInfo();
|
|
|
|
|
|
|
|
std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
|
2014-11-13 21:41:43 +00:00
|
|
|
cf_name_, mutable_cf_options_, vstorage_.get(), &log_buffer_));
|
2014-10-29 23:45:07 +00:00
|
|
|
ASSERT_TRUE(compaction.get() != nullptr);
|
2014-11-11 21:47:22 +00:00
|
|
|
ASSERT_EQ(1U, compaction->num_input_files(0));
|
2014-10-29 23:45:07 +00:00
|
|
|
ASSERT_EQ(7U, compaction->input(0, 0)->fd.GetNumber());
|
|
|
|
}
|
|
|
|
|
2014-11-13 21:41:43 +00:00
|
|
|
TEST(CompactionPickerTest, NeedsCompactionLevel) {
|
|
|
|
const int kLevels = 6;
|
|
|
|
const int kFileCount = 20;
|
|
|
|
for (int level = 0; level < kLevels - 1; ++level) {
|
|
|
|
uint64_t file_size =
|
|
|
|
mutable_cf_options_.MaxBytesForLevel(level) * 2 / kFileCount;
|
|
|
|
for (int file_count = 1; file_count <= kFileCount; ++file_count) {
|
|
|
|
// start a brand new version in each test.
|
|
|
|
NewVersionStorage(kLevels, kCompactionStyleLevel);
|
|
|
|
for (int i = 0; i < file_count; ++i) {
|
|
|
|
Add(level, i, std::to_string((i + 100) * 1000).c_str(),
|
|
|
|
std::to_string((i + 100) * 1000 + 999).c_str(),
|
|
|
|
file_size, 0, i * 100, i * 100 + 99);
|
|
|
|
}
|
|
|
|
UpdateVersionStorageInfo();
|
|
|
|
ASSERT_EQ(vstorage_->CompactionScoreLevel(0), level);
|
2014-11-13 23:21:04 +00:00
|
|
|
ASSERT_EQ(level_compaction_picker.NeedsCompaction(vstorage_.get()),
|
|
|
|
vstorage_->CompactionScore(0) >= 1);
|
2014-11-13 21:41:43 +00:00
|
|
|
// release the version storage
|
|
|
|
DeleteVersionStorage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CompactionPickerTest, NeedsCompactionUniversal) {
|
|
|
|
NewVersionStorage(1, kCompactionStyleUniversal);
|
|
|
|
UniversalCompactionPicker universal_compaction_picker(
|
|
|
|
ioptions_, &icmp_);
|
|
|
|
// must return false when there's no files.
|
2014-11-13 23:21:04 +00:00
|
|
|
ASSERT_EQ(universal_compaction_picker.NeedsCompaction(vstorage_.get()),
|
|
|
|
false);
|
2014-11-13 21:41:43 +00:00
|
|
|
|
|
|
|
// verify the trigger given different number of L0 files.
|
|
|
|
for (int i = 1;
|
2014-11-13 23:21:04 +00:00
|
|
|
i <= mutable_cf_options_.level0_file_num_compaction_trigger * 2; ++i) {
|
2014-11-13 21:41:43 +00:00
|
|
|
Add(0, i, std::to_string((i + 100) * 1000).c_str(),
|
2014-11-13 23:21:04 +00:00
|
|
|
std::to_string((i + 100) * 1000 + 999).c_str(), 1000000, 0, i * 100,
|
|
|
|
i * 100 + 99);
|
|
|
|
ASSERT_EQ(level_compaction_picker.NeedsCompaction(vstorage_.get()),
|
|
|
|
vstorage_->CompactionScore(0) >= 1);
|
2014-11-13 21:41:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CompactionPickerTest, NeedsCompactionFIFO) {
|
|
|
|
NewVersionStorage(1, kCompactionStyleFIFO);
|
|
|
|
const int kFileCount =
|
|
|
|
mutable_cf_options_.level0_file_num_compaction_trigger * 3;
|
|
|
|
const uint64_t kFileSize = 100000;
|
|
|
|
const uint64_t kMaxSize = kFileSize * kFileCount / 2;
|
|
|
|
|
|
|
|
fifo_options_.max_table_files_size = kMaxSize;
|
|
|
|
ioptions_.compaction_options_fifo = fifo_options_;
|
|
|
|
FIFOCompactionPicker fifo_compaction_picker(ioptions_, &icmp_);
|
|
|
|
|
|
|
|
// must return false when there's no files.
|
2014-11-13 23:21:04 +00:00
|
|
|
ASSERT_EQ(fifo_compaction_picker.NeedsCompaction(vstorage_.get()), false);
|
2014-11-13 21:41:43 +00:00
|
|
|
|
|
|
|
// verify whether compaction is needed based on the current
|
|
|
|
// size of L0 files.
|
|
|
|
uint64_t current_size = 0;
|
|
|
|
for (int i = 1; i <= kFileCount; ++i) {
|
|
|
|
Add(0, i, std::to_string((i + 100) * 1000).c_str(),
|
|
|
|
std::to_string((i + 100) * 1000 + 999).c_str(),
|
|
|
|
kFileSize, 0, i * 100, i * 100 + 99);
|
|
|
|
current_size += kFileSize;
|
2014-11-13 23:21:04 +00:00
|
|
|
ASSERT_EQ(level_compaction_picker.NeedsCompaction(vstorage_.get()),
|
|
|
|
vstorage_->CompactionScore(0) >= 1);
|
2014-11-13 21:41:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-27 22:49:46 +00:00
|
|
|
} // namespace rocksdb
|
|
|
|
|
|
|
|
int main(int argc, char** argv) { return rocksdb::test::RunAllTests(); }
|