forked from continuwuation/rocksdb
Summary:
Adds auto-tuning of manifest file size to avoid the need to scale `max_manifest_file_size` in proportion to things like number of SST files to properly balance (a) manifest file write amp and new file creation, vs. (b) manifest file space amp and replay time, including non-incremental space usage in backups. (Manifest file write amp comes from re-writing a "live" record when the manifest file is re-created, or "compacted"; space amp is usage beyond what would be used by a compacted manifest file.) In more detail,
* Add new option `max_manifest_space_amp_pct` with default value of 500, which defaults to 0.2 write amp and up to roughly 5.0 space amp, except `max_manifest_file_size` is treated as the "minimum" size before re-creating ("compacting") the manifest file.
* `max_manifest_file_size` in a way means the same thing, with the same default of 1GB, but in a way has taken on a new role. What is the same is that we do not re-create the manifest file before reaching this size (except for DB re-open), and so users are very unlikely to see a change in default behavior (auto-tuning only kicking in if auto-tuning would exceed 1GB for effective max size for the current manifest file). The new role is as a file size lower bound before auto-tuning kicks in, to minimize churn in files considered "negligibly small." We recommend a new setting of around 1MB or even smaller like 64KB, and expect something like this to become the default soon.
* These two options along with `manifest_preallocation_size` are now mutable with SetDBOptions. The effect is nearly immediate, affecting the next write to the current manifest file.
Also in this PR:
* Refactoring of VersionSet to allow it to get (more) settings from MutableDBOptions. This touches a number of files in not very interesting ways, but notably we have to be careful about thread-safe access to MutableDBOptions fields, and even fields within VersionSet. I have decided to save copies of relevant fields from MutableDBOptions to simplify testing, etc. by not saving a reference to MutableDBOptions but getting notified of updates.
* Updated some logging in VersionSet to provide some basic data about final and compacted manifest sizes (effects of auto-tuning), making sure to avoid I/O while holding DB mutex.
* Added db_etc3_test.cc which is intended as a successor to db_test and db_test2, but having "test.cc" in its name for easier exclusion of test files when using `git grep`. Intended follow-up: rename db_test2 to db_etc2_test
* Moved+updated `ManifestRollOver` test to the new file to be closer to other manifest file rollover testing.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/14076
Test Plan:
As for correctness, new unit test AutoTuneManifestSize is pretty thorough. Some other unit tests updated appropriately. Manual tests in the performance section were also audited for expected behavior based on the new logging in the DB LOG. Example LOG data with -max_manifest_file_size=2048 -max_manifest_space_amp_pct=500:
```
2025/10/24-11:12:48.979472 2150678 [/version_set.cc:5927] Created manifest 5, compacted+appended from 52 to 116
2025/10/24-11:12:49.626441 2150682 [/version_set.cc:5927] Created manifest 24, compacted+appended from 2169 to 1801
2025/10/24-11:12:52.194592 2150682 [/version_set.cc:5927] Created manifest 91, compacted+appended from 10913 to 8707
2025/10/24-11:13:02.969944 2150682 [/version_set.cc:5927] Created manifest 362, compacted+appended from 52259 to 13321
2025/10/24-11:13:18.815120 2150681 [/version_set.cc:5927] Created manifest 765, compacted+appended from 80064 to 13304
2025/10/24-11:13:35.590905 2150681 [/version_set.cc:5927] Created manifest 1167, compacted+appended from 79863 to 13304
```
As you can see, it only took a few iterations of ramp-up to settle on the auto-tuned max manifest size for tracking ~122 live SST files, around 80KB and compacting down to about 13KB. (13KB * (500 + 100) / 100 = 78KB). With the default large setting for max_manifest_file_size, we end up with a 232KB manifest, which is more than 90% wasted space. (A long-running DB would be much worse.)
As for performance, we don't expect a difference, even with TransactionDB because actual writing of the manifest is done without holding the DB mutex. I was not able to see a performance regression using db_bench with FIFO compaction and >1000 ~10MB SST files, including settings of -max_manifest_file_size=2048 -max_manifest_space_amp_pct={500,10,0}. No "hiccups" visible with -histogram either.
I also tried seeding a 1 second delay in writing new manifest files (other than the first). This had no significant effect at -max_manifest_space_amp_pct=500 but at 100 started causing write stalls in my test. In many ways this is kind of a worst case scenario and out-of-proportion test, but gives me more confidence that a higher number like 500 is probably the best balance in general.
Reviewed By: xingbowang
Differential Revision: D85445178
Pulled By: pdillinger
fbshipit-source-id: 1e6e07e89c586762dd65c65bb7cb2b8b719513f9
161 lines
5.7 KiB
C++
161 lines
5.7 KiB
C++
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
|
// 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).
|
|
|
|
#include "db/db_test_util.h"
|
|
|
|
namespace ROCKSDB_NAMESPACE {
|
|
|
|
class DBEtc3Test : public DBTestBase {
|
|
public:
|
|
DBEtc3Test() : DBTestBase("db_etc3_test", /*env_do_fsync=*/true) {}
|
|
};
|
|
|
|
TEST_F(DBEtc3Test, ManifestRollOver) {
|
|
do {
|
|
Options options;
|
|
// Force new manifest on each manifest write
|
|
options.max_manifest_file_size = 0;
|
|
options.max_manifest_space_amp_pct = 0;
|
|
options = CurrentOptions(options);
|
|
CreateAndReopenWithCF({"pikachu"}, options);
|
|
{
|
|
ASSERT_OK(Put(1, "key1", std::string(1000, '1')));
|
|
ASSERT_OK(Put(1, "key2", std::string(1000, '2')));
|
|
ASSERT_OK(Put(1, "key3", std::string(1000, '3')));
|
|
uint64_t manifest_before_flush = dbfull()->TEST_Current_Manifest_FileNo();
|
|
ASSERT_OK(Flush(1)); // This should trigger LogAndApply.
|
|
uint64_t manifest_after_flush = dbfull()->TEST_Current_Manifest_FileNo();
|
|
ASSERT_GT(manifest_after_flush, manifest_before_flush);
|
|
// Re-open should always re-create manifest file
|
|
ReopenWithColumnFamilies({"default", "pikachu"}, options);
|
|
ASSERT_GT(dbfull()->TEST_Current_Manifest_FileNo(), manifest_after_flush);
|
|
ASSERT_EQ(std::string(1000, '1'), Get(1, "key1"));
|
|
ASSERT_EQ(std::string(1000, '2'), Get(1, "key2"));
|
|
ASSERT_EQ(std::string(1000, '3'), Get(1, "key3"));
|
|
}
|
|
} while (ChangeCompactOptions());
|
|
}
|
|
|
|
TEST_F(DBEtc3Test, AutoTuneManifestSize) {
|
|
// Ensure we have auto-tuning beyond max_manifest_file_size by default
|
|
ASSERT_EQ(DBOptions{}.max_manifest_space_amp_pct, 500);
|
|
|
|
Options options = CurrentOptions();
|
|
ASSERT_OK(db_->SetOptions({{"level0_file_num_compaction_trigger", "20"}}));
|
|
|
|
// Use large column family names to essentially control the amount of payload
|
|
// data needed for the manifest file. Drop manifest entries don't include the
|
|
// CF name so are small.
|
|
uint64_t prev_manifest_num = 0, cur_manifest_num = 0;
|
|
std::deque<ColumnFamilyHandle*> handles;
|
|
int counter = 5;
|
|
auto AddCfFn = [&]() {
|
|
std::string name = "cf" + std::to_string(counter++);
|
|
name.resize(1000, 'a');
|
|
ASSERT_OK(db_->CreateColumnFamily(options, name, &handles.emplace_back()));
|
|
prev_manifest_num = cur_manifest_num;
|
|
cur_manifest_num = dbfull()->TEST_Current_Manifest_FileNo();
|
|
};
|
|
auto DropCfFn = [&]() {
|
|
ASSERT_OK(db_->DropColumnFamily(handles.front()));
|
|
ASSERT_OK(db_->DestroyColumnFamilyHandle(handles.front()));
|
|
handles.pop_front();
|
|
prev_manifest_num = cur_manifest_num;
|
|
cur_manifest_num = dbfull()->TEST_Current_Manifest_FileNo();
|
|
};
|
|
auto TrivialManifestWriteFn = [&]() {
|
|
ASSERT_OK(Put("x", std::to_string(counter++)));
|
|
ASSERT_OK(Flush());
|
|
prev_manifest_num = cur_manifest_num;
|
|
cur_manifest_num = dbfull()->TEST_Current_Manifest_FileNo();
|
|
};
|
|
|
|
options.max_manifest_file_size = 1000000;
|
|
options.max_manifest_space_amp_pct = 0; // no auto-tuning yet
|
|
DestroyAndReopen(options);
|
|
|
|
// With the generous (minimum) maximum manifest size, should not be rotated
|
|
AddCfFn();
|
|
AddCfFn();
|
|
AddCfFn();
|
|
ASSERT_EQ(prev_manifest_num, cur_manifest_num);
|
|
|
|
// Change options for small max and (still) no auto-tuning
|
|
ASSERT_OK(db_->SetDBOptions({{"max_manifest_file_size", "3000"}}));
|
|
|
|
// Takes effect on the next manifest write
|
|
TrivialManifestWriteFn();
|
|
ASSERT_LT(prev_manifest_num, cur_manifest_num);
|
|
|
|
// Now we have to rewrite the whole manifest on each write because the
|
|
// compacted size exceeds the "max" size.
|
|
AddCfFn();
|
|
ASSERT_LT(prev_manifest_num, cur_manifest_num);
|
|
DropCfFn();
|
|
ASSERT_LT(prev_manifest_num, cur_manifest_num);
|
|
AddCfFn();
|
|
ASSERT_LT(prev_manifest_num, cur_manifest_num);
|
|
TrivialManifestWriteFn();
|
|
ASSERT_LT(prev_manifest_num, cur_manifest_num);
|
|
|
|
// Enabling auto-tuning should fix this, immediately for next manifest writes.
|
|
// This will allow up to double-ish the size of the compacted manifest,
|
|
// which last should have been 4000 + some bytes.
|
|
ASSERT_EQ(handles.size(), 4U);
|
|
ASSERT_OK(db_->SetDBOptions({{"max_manifest_space_amp_pct", "105"}}));
|
|
|
|
// After 9 CF names should be enough to rotate the manifest
|
|
for (int i = 1; i <= 5; ++i) {
|
|
if ((i % 2) == 1) {
|
|
DropCfFn();
|
|
}
|
|
AddCfFn();
|
|
ASSERT_EQ(prev_manifest_num, cur_manifest_num);
|
|
}
|
|
TrivialManifestWriteFn();
|
|
ASSERT_LT(prev_manifest_num, cur_manifest_num);
|
|
|
|
// We now have a different last compacted manifest size, should be
|
|
// able to go beyond 9 CFs named in manifest this time.
|
|
ASSERT_EQ(handles.size(), 6U);
|
|
|
|
DropCfFn();
|
|
DropCfFn();
|
|
for (int i = 1; i <= 4; ++i) {
|
|
DropCfFn();
|
|
AddCfFn();
|
|
ASSERT_EQ(prev_manifest_num, cur_manifest_num);
|
|
}
|
|
// We've written 10 named CFs to the manifest. We should be able to
|
|
// dynamically change the auto-tuning still based on the last "compacted"
|
|
// manifest size of 7000 + some bytes.
|
|
ASSERT_OK(db_->SetDBOptions({{"max_manifest_space_amp_pct", "51"}}));
|
|
TrivialManifestWriteFn();
|
|
ASSERT_LT(prev_manifest_num, cur_manifest_num);
|
|
// And the "compacted" manifest size has reset again, so should be changed
|
|
// again sooner.
|
|
ASSERT_EQ(handles.size(), 4U);
|
|
for (int i = 1; i <= 2; ++i) {
|
|
AddCfFn();
|
|
ASSERT_EQ(prev_manifest_num, cur_manifest_num);
|
|
}
|
|
// Enough for manifest change
|
|
AddCfFn();
|
|
ASSERT_LT(prev_manifest_num, cur_manifest_num);
|
|
|
|
// Wrap up
|
|
while (!handles.empty()) {
|
|
DropCfFn();
|
|
}
|
|
}
|
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
|
|
|
int main(int argc, char** argv) {
|
|
ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
RegisterCustomObjects(argc, argv);
|
|
return RUN_ALL_TESTS();
|
|
}
|