2014-10-31 16:48:19 +01: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 <string>
|
|
|
|
#include "db/version_edit.h"
|
|
|
|
#include "db/version_set.h"
|
|
|
|
#include "util/logging.h"
|
|
|
|
#include "util/testharness.h"
|
|
|
|
#include "util/testutil.h"
|
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
|
|
|
|
class VersionBuilderTest {
|
|
|
|
public:
|
2014-11-11 23:28:18 +01:00
|
|
|
const Comparator* ucmp_;
|
|
|
|
InternalKeyComparator icmp_;
|
|
|
|
Options options_;
|
|
|
|
ImmutableCFOptions ioptions_;
|
|
|
|
MutableCFOptions mutable_cf_options_;
|
|
|
|
VersionStorageInfo vstorage_;
|
|
|
|
uint32_t file_num_;
|
|
|
|
CompactionOptionsFIFO fifo_options_;
|
|
|
|
std::vector<uint64_t> size_being_compacted_;
|
2014-10-31 16:48:19 +01:00
|
|
|
|
|
|
|
VersionBuilderTest()
|
2014-11-11 23:28:18 +01:00
|
|
|
: ucmp_(BytewiseComparator()),
|
|
|
|
icmp_(ucmp_),
|
|
|
|
ioptions_(options_),
|
|
|
|
mutable_cf_options_(options_, ioptions_),
|
|
|
|
vstorage_(&icmp_, ucmp_, options_.num_levels, kCompactionStyleLevel,
|
2014-10-31 16:48:19 +01:00
|
|
|
nullptr),
|
2014-11-11 23:28:18 +01:00
|
|
|
file_num_(1) {
|
|
|
|
mutable_cf_options_.RefreshDerivedOptions(ioptions_);
|
|
|
|
size_being_compacted_.resize(options_.num_levels);
|
2014-10-31 16:48:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
~VersionBuilderTest() {
|
2014-11-11 23:28:18 +01:00
|
|
|
for (int i = 0; i < vstorage_.num_levels(); i++) {
|
|
|
|
for (auto* f : vstorage_.LevelFiles(i)) {
|
2014-10-31 16:48:19 +01:00
|
|
|
if (--f->refs == 0) {
|
|
|
|
delete f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
InternalKey GetInternalKey(const char* ukey,
|
|
|
|
SequenceNumber smallest_seq = 100) {
|
|
|
|
return InternalKey(ukey, smallest_seq, kTypeValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2014-11-11 23:28:18 +01:00
|
|
|
SequenceNumber largest_seq = 100,
|
|
|
|
uint64_t num_entries = 0, uint64_t num_deletions = 0,
|
|
|
|
bool sampled = false) {
|
|
|
|
assert(level < vstorage_.num_levels());
|
2014-10-31 16:48:19 +01:00
|
|
|
FileMetaData* f = new FileMetaData;
|
|
|
|
f->fd = FileDescriptor(file_number, path_id, file_size);
|
|
|
|
f->smallest = GetInternalKey(smallest, smallest_seq);
|
|
|
|
f->largest = GetInternalKey(largest, largest_seq);
|
|
|
|
f->compensated_file_size = file_size;
|
|
|
|
f->refs = 0;
|
2014-11-11 23:28:18 +01:00
|
|
|
f->num_entries = num_entries;
|
|
|
|
f->num_deletions = num_deletions;
|
2014-11-13 22:41:43 +01:00
|
|
|
vstorage_.AddFile(level, f);
|
2014-11-11 23:28:18 +01:00
|
|
|
if (sampled) {
|
|
|
|
f->init_stats_from_file = true;
|
|
|
|
vstorage_.UpdateAccumulatedStats(f);
|
|
|
|
}
|
2014-10-31 16:48:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateVersionStorageInfo() {
|
2014-11-11 23:28:18 +01:00
|
|
|
vstorage_.UpdateFilesBySize();
|
|
|
|
vstorage_.UpdateNumNonEmptyLevels();
|
|
|
|
vstorage_.GenerateFileIndexer();
|
|
|
|
vstorage_.GenerateLevelFilesBrief();
|
|
|
|
vstorage_.SetFinalized();
|
2014-10-31 16:48:19 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(VersionBuilderTest, ApplyAndSaveTo) {
|
|
|
|
Add(0, 1U, "150", "200", 100U);
|
|
|
|
// Level 1 score 1.2
|
|
|
|
Add(1, 66U, "150", "200", 100U);
|
|
|
|
Add(1, 88U, "201", "300", 100U);
|
|
|
|
// Level 2 score 1.8. File 7 is the largest. Should be picked
|
|
|
|
Add(2, 6U, "150", "179", 100U);
|
|
|
|
Add(2, 7U, "180", "220", 100U);
|
|
|
|
Add(2, 8U, "221", "300", 100U);
|
|
|
|
// Level 3 score slightly larger than 1
|
|
|
|
Add(3, 26U, "150", "170", 100U);
|
|
|
|
Add(3, 27U, "171", "179", 100U);
|
|
|
|
Add(3, 28U, "191", "220", 100U);
|
|
|
|
Add(3, 29U, "221", "300", 100U);
|
|
|
|
UpdateVersionStorageInfo();
|
|
|
|
|
|
|
|
VersionEdit version_edit;
|
|
|
|
version_edit.AddFile(2, 666, 0, 100U, GetInternalKey("301"),
|
|
|
|
GetInternalKey("350"), 200, 200);
|
|
|
|
version_edit.DeleteFile(3, 27U);
|
|
|
|
|
|
|
|
EnvOptions env_options;
|
|
|
|
|
2014-11-11 23:28:18 +01:00
|
|
|
VersionBuilder version_builder(env_options, nullptr, &vstorage_);
|
2014-10-31 16:48:19 +01:00
|
|
|
|
2014-11-11 23:28:18 +01:00
|
|
|
VersionStorageInfo new_vstorage(&icmp_, ucmp_, options_.num_levels,
|
2014-10-31 16:48:19 +01:00
|
|
|
kCompactionStyleLevel, nullptr);
|
|
|
|
version_builder.Apply(&version_edit);
|
|
|
|
version_builder.SaveTo(&new_vstorage);
|
|
|
|
|
|
|
|
ASSERT_EQ(400U, new_vstorage.NumLevelBytes(2));
|
|
|
|
ASSERT_EQ(300U, new_vstorage.NumLevelBytes(3));
|
|
|
|
|
2015-01-07 19:29:21 +01:00
|
|
|
for (int i = 0; i < new_vstorage.num_levels(); i++) {
|
|
|
|
for (auto* f : new_vstorage.LevelFiles(i)) {
|
|
|
|
if (--f->refs == 0) {
|
|
|
|
delete f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(VersionBuilderTest, ApplyMultipleAndSaveTo) {
|
|
|
|
UpdateVersionStorageInfo();
|
|
|
|
|
|
|
|
VersionEdit version_edit;
|
|
|
|
version_edit.AddFile(2, 666, 0, 100U, GetInternalKey("301"),
|
|
|
|
GetInternalKey("350"), 200, 200);
|
|
|
|
version_edit.AddFile(2, 676, 0, 100U, GetInternalKey("401"),
|
|
|
|
GetInternalKey("450"), 200, 200);
|
|
|
|
version_edit.AddFile(2, 636, 0, 100U, GetInternalKey("601"),
|
|
|
|
GetInternalKey("650"), 200, 200);
|
|
|
|
version_edit.AddFile(2, 616, 0, 100U, GetInternalKey("501"),
|
|
|
|
GetInternalKey("550"), 200, 200);
|
|
|
|
version_edit.AddFile(2, 606, 0, 100U, GetInternalKey("701"),
|
|
|
|
GetInternalKey("750"), 200, 200);
|
|
|
|
|
|
|
|
EnvOptions env_options;
|
|
|
|
|
|
|
|
VersionBuilder version_builder(env_options, nullptr, &vstorage_);
|
|
|
|
|
|
|
|
VersionStorageInfo new_vstorage(&icmp_, ucmp_, options_.num_levels,
|
|
|
|
kCompactionStyleLevel, nullptr);
|
|
|
|
version_builder.Apply(&version_edit);
|
|
|
|
version_builder.SaveTo(&new_vstorage);
|
|
|
|
|
|
|
|
ASSERT_EQ(500U, new_vstorage.NumLevelBytes(2));
|
|
|
|
|
|
|
|
for (int i = 0; i < new_vstorage.num_levels(); i++) {
|
|
|
|
for (auto* f : new_vstorage.LevelFiles(i)) {
|
|
|
|
if (--f->refs == 0) {
|
|
|
|
delete f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(VersionBuilderTest, ApplyDeleteAndSaveTo) {
|
|
|
|
UpdateVersionStorageInfo();
|
|
|
|
|
|
|
|
EnvOptions env_options;
|
|
|
|
VersionBuilder version_builder(env_options, nullptr, &vstorage_);
|
|
|
|
VersionStorageInfo new_vstorage(&icmp_, ucmp_, options_.num_levels,
|
|
|
|
kCompactionStyleLevel, nullptr);
|
|
|
|
|
|
|
|
VersionEdit version_edit;
|
|
|
|
version_edit.AddFile(2, 666, 0, 100U, GetInternalKey("301"),
|
|
|
|
GetInternalKey("350"), 200, 200);
|
|
|
|
version_edit.AddFile(2, 676, 0, 100U, GetInternalKey("401"),
|
|
|
|
GetInternalKey("450"), 200, 200);
|
|
|
|
version_edit.AddFile(2, 636, 0, 100U, GetInternalKey("601"),
|
|
|
|
GetInternalKey("650"), 200, 200);
|
|
|
|
version_edit.AddFile(2, 616, 0, 100U, GetInternalKey("501"),
|
|
|
|
GetInternalKey("550"), 200, 200);
|
|
|
|
version_edit.AddFile(2, 606, 0, 100U, GetInternalKey("701"),
|
|
|
|
GetInternalKey("750"), 200, 200);
|
|
|
|
version_builder.Apply(&version_edit);
|
|
|
|
|
|
|
|
VersionEdit version_edit2;
|
|
|
|
version_edit.AddFile(2, 808, 0, 100U, GetInternalKey("901"),
|
|
|
|
GetInternalKey("950"), 200, 200);
|
|
|
|
version_edit2.DeleteFile(2, 616);
|
|
|
|
version_edit2.DeleteFile(2, 636);
|
|
|
|
version_edit.AddFile(2, 806, 0, 100U, GetInternalKey("801"),
|
|
|
|
GetInternalKey("850"), 200, 200);
|
|
|
|
version_builder.Apply(&version_edit2);
|
|
|
|
|
|
|
|
version_builder.SaveTo(&new_vstorage);
|
|
|
|
|
|
|
|
ASSERT_EQ(300U, new_vstorage.NumLevelBytes(2));
|
|
|
|
|
2014-11-04 02:45:55 +01:00
|
|
|
for (int i = 0; i < new_vstorage.num_levels(); i++) {
|
2014-10-31 16:48:19 +01:00
|
|
|
for (auto* f : new_vstorage.LevelFiles(i)) {
|
|
|
|
if (--f->refs == 0) {
|
|
|
|
delete f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-11 23:28:18 +01:00
|
|
|
TEST(VersionBuilderTest, EstimatedActiveKeys) {
|
2014-11-12 00:22:06 +01:00
|
|
|
const uint32_t kTotalSamples = 20;
|
|
|
|
const uint32_t kNumLevels = 5;
|
|
|
|
const uint32_t kFilesPerLevel = 8;
|
|
|
|
const uint32_t kNumFiles = kNumLevels * kFilesPerLevel;
|
|
|
|
const uint32_t kEntriesPerFile = 1000;
|
|
|
|
const uint32_t kDeletionsPerFile = 100;
|
|
|
|
for (uint32_t i = 0; i < kNumFiles; ++i) {
|
|
|
|
Add(static_cast<int>(i / kFilesPerLevel), i + 1,
|
2014-11-25 05:44:49 +01:00
|
|
|
ToString((i + 100) * 1000).c_str(),
|
|
|
|
ToString((i + 100) * 1000 + 999).c_str(),
|
2014-11-11 23:28:18 +01:00
|
|
|
100U, 0, 100, 100,
|
|
|
|
kEntriesPerFile, kDeletionsPerFile,
|
|
|
|
(i < kTotalSamples));
|
|
|
|
}
|
|
|
|
// minus 2X for the number of deletion entries because:
|
|
|
|
// 1x for deletion entry does not count as a data entry.
|
|
|
|
// 1x for each deletion entry will actually remove one data entry.
|
|
|
|
ASSERT_EQ(vstorage_.GetEstimatedActiveKeys(),
|
|
|
|
(kEntriesPerFile - 2 * kDeletionsPerFile) * kNumFiles);
|
|
|
|
}
|
|
|
|
|
2014-10-31 16:48:19 +01:00
|
|
|
} // namespace rocksdb
|
|
|
|
|
|
|
|
int main(int argc, char** argv) { return rocksdb::test::RunAllTests(); }
|