|
| 1 | +// Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
| 2 | +// This source code is licensed under both the GPLv2 (found in the |
| 3 | +// COPYING file in the root directory) and Apache 2.0 License |
| 4 | +// (found in the LICENSE.Apache file in the root directory). |
| 5 | + |
| 6 | +#include "db/db_test_util.h" |
| 7 | + |
| 8 | +namespace ROCKSDB_NAMESPACE { |
| 9 | + |
| 10 | +class DBEtc3Test : public DBTestBase { |
| 11 | + public: |
| 12 | + DBEtc3Test() : DBTestBase("db_etc3_test", /*env_do_fsync=*/true) {} |
| 13 | +}; |
| 14 | + |
| 15 | +TEST_F(DBEtc3Test, ManifestRollOver) { |
| 16 | + do { |
| 17 | + Options options; |
| 18 | + // Force new manifest on each manifest write |
| 19 | + options.max_manifest_file_size = 0; |
| 20 | + options.max_manifest_space_amp_pct = 0; |
| 21 | + options = CurrentOptions(options); |
| 22 | + CreateAndReopenWithCF({"pikachu"}, options); |
| 23 | + { |
| 24 | + ASSERT_OK(Put(1, "key1", std::string(1000, '1'))); |
| 25 | + ASSERT_OK(Put(1, "key2", std::string(1000, '2'))); |
| 26 | + ASSERT_OK(Put(1, "key3", std::string(1000, '3'))); |
| 27 | + uint64_t manifest_before_flush = dbfull()->TEST_Current_Manifest_FileNo(); |
| 28 | + ASSERT_OK(Flush(1)); // This should trigger LogAndApply. |
| 29 | + uint64_t manifest_after_flush = dbfull()->TEST_Current_Manifest_FileNo(); |
| 30 | + ASSERT_GT(manifest_after_flush, manifest_before_flush); |
| 31 | + // Re-open should always re-create manifest file |
| 32 | + ReopenWithColumnFamilies({"default", "pikachu"}, options); |
| 33 | + ASSERT_GT(dbfull()->TEST_Current_Manifest_FileNo(), manifest_after_flush); |
| 34 | + ASSERT_EQ(std::string(1000, '1'), Get(1, "key1")); |
| 35 | + ASSERT_EQ(std::string(1000, '2'), Get(1, "key2")); |
| 36 | + ASSERT_EQ(std::string(1000, '3'), Get(1, "key3")); |
| 37 | + } |
| 38 | + } while (ChangeCompactOptions()); |
| 39 | +} |
| 40 | + |
| 41 | +TEST_F(DBEtc3Test, AutoTuneManifestSize) { |
| 42 | + // Ensure we have auto-tuning beyond max_manifest_file_size by default |
| 43 | + ASSERT_EQ(DBOptions{}.max_manifest_space_amp_pct, 500); |
| 44 | + |
| 45 | + Options options = CurrentOptions(); |
| 46 | + ASSERT_OK(db_->SetOptions({{"level0_file_num_compaction_trigger", "20"}})); |
| 47 | + |
| 48 | + // Use large column family names to essentially control the amount of payload |
| 49 | + // data needed for the manifest file. Drop manifest entries don't include the |
| 50 | + // CF name so are small. |
| 51 | + uint64_t prev_manifest_num = 0, cur_manifest_num = 0; |
| 52 | + std::deque<ColumnFamilyHandle*> handles; |
| 53 | + int counter = 5; |
| 54 | + auto AddCfFn = [&]() { |
| 55 | + std::string name = "cf" + std::to_string(counter++); |
| 56 | + name.resize(1000, 'a'); |
| 57 | + ASSERT_OK(db_->CreateColumnFamily(options, name, &handles.emplace_back())); |
| 58 | + prev_manifest_num = cur_manifest_num; |
| 59 | + cur_manifest_num = dbfull()->TEST_Current_Manifest_FileNo(); |
| 60 | + }; |
| 61 | + auto DropCfFn = [&]() { |
| 62 | + ASSERT_OK(db_->DropColumnFamily(handles.front())); |
| 63 | + ASSERT_OK(db_->DestroyColumnFamilyHandle(handles.front())); |
| 64 | + handles.pop_front(); |
| 65 | + prev_manifest_num = cur_manifest_num; |
| 66 | + cur_manifest_num = dbfull()->TEST_Current_Manifest_FileNo(); |
| 67 | + }; |
| 68 | + auto TrivialManifestWriteFn = [&]() { |
| 69 | + ASSERT_OK(Put("x", std::to_string(counter++))); |
| 70 | + ASSERT_OK(Flush()); |
| 71 | + prev_manifest_num = cur_manifest_num; |
| 72 | + cur_manifest_num = dbfull()->TEST_Current_Manifest_FileNo(); |
| 73 | + }; |
| 74 | + |
| 75 | + options.max_manifest_file_size = 1000000; |
| 76 | + options.max_manifest_space_amp_pct = 0; // no auto-tuning yet |
| 77 | + DestroyAndReopen(options); |
| 78 | + |
| 79 | + // With the generous (minimum) maximum manifest size, should not be rotated |
| 80 | + AddCfFn(); |
| 81 | + AddCfFn(); |
| 82 | + AddCfFn(); |
| 83 | + ASSERT_EQ(prev_manifest_num, cur_manifest_num); |
| 84 | + |
| 85 | + // Change options for small max and (still) no auto-tuning |
| 86 | + ASSERT_OK(db_->SetDBOptions({{"max_manifest_file_size", "3000"}})); |
| 87 | + |
| 88 | + // Takes effect on the next manifest write |
| 89 | + TrivialManifestWriteFn(); |
| 90 | + ASSERT_LT(prev_manifest_num, cur_manifest_num); |
| 91 | + |
| 92 | + // Now we have to rewrite the whole manifest on each write because the |
| 93 | + // compacted size exceeds the "max" size. |
| 94 | + AddCfFn(); |
| 95 | + ASSERT_LT(prev_manifest_num, cur_manifest_num); |
| 96 | + DropCfFn(); |
| 97 | + ASSERT_LT(prev_manifest_num, cur_manifest_num); |
| 98 | + AddCfFn(); |
| 99 | + ASSERT_LT(prev_manifest_num, cur_manifest_num); |
| 100 | + TrivialManifestWriteFn(); |
| 101 | + ASSERT_LT(prev_manifest_num, cur_manifest_num); |
| 102 | + |
| 103 | + // Enabling auto-tuning should fix this, immediately for next manifest writes. |
| 104 | + // This will allow up to double-ish the size of the compacted manifest, |
| 105 | + // which last should have been 4000 + some bytes. |
| 106 | + ASSERT_EQ(handles.size(), 4U); |
| 107 | + ASSERT_OK(db_->SetDBOptions({{"max_manifest_space_amp_pct", "105"}})); |
| 108 | + |
| 109 | + // After 9 CF names should be enough to rotate the manifest |
| 110 | + for (int i = 1; i <= 5; ++i) { |
| 111 | + if ((i % 2) == 1) { |
| 112 | + DropCfFn(); |
| 113 | + } |
| 114 | + AddCfFn(); |
| 115 | + ASSERT_EQ(prev_manifest_num, cur_manifest_num); |
| 116 | + } |
| 117 | + TrivialManifestWriteFn(); |
| 118 | + ASSERT_LT(prev_manifest_num, cur_manifest_num); |
| 119 | + |
| 120 | + // We now have a different last compacted manifest size, should be |
| 121 | + // able to go beyond 9 CFs named in manifest this time. |
| 122 | + ASSERT_EQ(handles.size(), 6U); |
| 123 | + |
| 124 | + DropCfFn(); |
| 125 | + DropCfFn(); |
| 126 | + for (int i = 1; i <= 4; ++i) { |
| 127 | + DropCfFn(); |
| 128 | + AddCfFn(); |
| 129 | + ASSERT_EQ(prev_manifest_num, cur_manifest_num); |
| 130 | + } |
| 131 | + // We've written 10 named CFs to the manifest. We should be able to |
| 132 | + // dynamically change the auto-tuning still based on the last "compacted" |
| 133 | + // manifest size of 7000 + some bytes. |
| 134 | + ASSERT_OK(db_->SetDBOptions({{"max_manifest_space_amp_pct", "51"}})); |
| 135 | + TrivialManifestWriteFn(); |
| 136 | + ASSERT_LT(prev_manifest_num, cur_manifest_num); |
| 137 | + // And the "compacted" manifest size has reset again, so should be changed |
| 138 | + // again sooner. |
| 139 | + ASSERT_EQ(handles.size(), 4U); |
| 140 | + for (int i = 1; i <= 2; ++i) { |
| 141 | + AddCfFn(); |
| 142 | + ASSERT_EQ(prev_manifest_num, cur_manifest_num); |
| 143 | + } |
| 144 | + // Enough for manifest change |
| 145 | + AddCfFn(); |
| 146 | + ASSERT_LT(prev_manifest_num, cur_manifest_num); |
| 147 | + |
| 148 | + // Wrap up |
| 149 | + while (!handles.empty()) { |
| 150 | + DropCfFn(); |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +} // namespace ROCKSDB_NAMESPACE |
| 155 | + |
| 156 | +int main(int argc, char** argv) { |
| 157 | + ROCKSDB_NAMESPACE::port::InstallStackTraceHandler(); |
| 158 | + ::testing::InitGoogleTest(&argc, argv); |
| 159 | + RegisterCustomObjects(argc, argv); |
| 160 | + return RUN_ALL_TESTS(); |
| 161 | +} |
0 commit comments