|
| 1 | +// Copyright (c) 2014, Facebook, Inc. All rights reserved. |
| 2 | +// This source code is licensed under the BSD-style license found in the |
| 3 | +// LICENSE file in the root directory of this source tree. An additional grant |
| 4 | +// of patent rights can be found in the PATENTS file in the same directory. |
| 5 | + |
| 6 | +#include "db/db_impl.h" |
| 7 | +#include "rocksdb/db.h" |
| 8 | +#include "rocksdb/env.h" |
| 9 | +#include "table/meta_blocks.h" |
| 10 | +#include "table/cuckoo_table_factory.h" |
| 11 | +#include "table/cuckoo_table_reader.h" |
| 12 | +#include "util/testharness.h" |
| 13 | +#include "util/testutil.h" |
| 14 | + |
| 15 | +namespace rocksdb { |
| 16 | + |
| 17 | +class CuckooTableDBTest { |
| 18 | + private: |
| 19 | + std::string dbname_; |
| 20 | + Env* env_; |
| 21 | + DB* db_; |
| 22 | + |
| 23 | + public: |
| 24 | + CuckooTableDBTest() : env_(Env::Default()) { |
| 25 | + dbname_ = test::TmpDir() + "/cuckoo_table_db_test"; |
| 26 | + ASSERT_OK(DestroyDB(dbname_, Options())); |
| 27 | + db_ = nullptr; |
| 28 | + Reopen(); |
| 29 | + } |
| 30 | + |
| 31 | + ~CuckooTableDBTest() { |
| 32 | + delete db_; |
| 33 | + ASSERT_OK(DestroyDB(dbname_, Options())); |
| 34 | + } |
| 35 | + |
| 36 | + Options CurrentOptions() { |
| 37 | + Options options; |
| 38 | + options.table_factory.reset(NewCuckooTableFactory()); |
| 39 | + options.memtable_factory.reset(NewHashLinkListRepFactory(4, 0, 3, true)); |
| 40 | + options.allow_mmap_reads = true; |
| 41 | + options.create_if_missing = true; |
| 42 | + options.max_mem_compaction_level = 0; |
| 43 | + return options; |
| 44 | + } |
| 45 | + |
| 46 | + DBImpl* dbfull() { |
| 47 | + return reinterpret_cast<DBImpl*>(db_); |
| 48 | + } |
| 49 | + |
| 50 | + // The following util methods are copied from plain_table_db_test. |
| 51 | + void Reopen(Options* options = nullptr) { |
| 52 | + delete db_; |
| 53 | + db_ = nullptr; |
| 54 | + Options opts; |
| 55 | + if (options != nullptr) { |
| 56 | + opts = *options; |
| 57 | + } else { |
| 58 | + opts = CurrentOptions(); |
| 59 | + opts.create_if_missing = true; |
| 60 | + } |
| 61 | + ASSERT_OK(DB::Open(opts, dbname_, &db_)); |
| 62 | + } |
| 63 | + |
| 64 | + Status Put(const Slice& k, const Slice& v) { |
| 65 | + return db_->Put(WriteOptions(), k, v); |
| 66 | + } |
| 67 | + |
| 68 | + Status Delete(const std::string& k) { |
| 69 | + return db_->Delete(WriteOptions(), k); |
| 70 | + } |
| 71 | + |
| 72 | + std::string Get(const std::string& k) { |
| 73 | + ReadOptions options; |
| 74 | + std::string result; |
| 75 | + Status s = db_->Get(options, k, &result); |
| 76 | + if (s.IsNotFound()) { |
| 77 | + result = "NOT_FOUND"; |
| 78 | + } else if (!s.ok()) { |
| 79 | + result = s.ToString(); |
| 80 | + } |
| 81 | + return result; |
| 82 | + } |
| 83 | + |
| 84 | + int NumTableFilesAtLevel(int level) { |
| 85 | + std::string property; |
| 86 | + ASSERT_TRUE( |
| 87 | + db_->GetProperty("rocksdb.num-files-at-level" + NumberToString(level), |
| 88 | + &property)); |
| 89 | + return atoi(property.c_str()); |
| 90 | + } |
| 91 | + |
| 92 | + // Return spread of files per level |
| 93 | + std::string FilesPerLevel() { |
| 94 | + std::string result; |
| 95 | + int last_non_zero_offset = 0; |
| 96 | + for (int level = 0; level < db_->NumberLevels(); level++) { |
| 97 | + int f = NumTableFilesAtLevel(level); |
| 98 | + char buf[100]; |
| 99 | + snprintf(buf, sizeof(buf), "%s%d", (level ? "," : ""), f); |
| 100 | + result += buf; |
| 101 | + if (f > 0) { |
| 102 | + last_non_zero_offset = result.size(); |
| 103 | + } |
| 104 | + } |
| 105 | + result.resize(last_non_zero_offset); |
| 106 | + return result; |
| 107 | + } |
| 108 | +}; |
| 109 | + |
| 110 | +TEST(CuckooTableDBTest, Flush) { |
| 111 | + // Try with empty DB first. |
| 112 | + ASSERT_TRUE(dbfull() != nullptr); |
| 113 | + ASSERT_EQ("NOT_FOUND", Get("key2")); |
| 114 | + |
| 115 | + // Add some values to db. |
| 116 | + Options options = CurrentOptions(); |
| 117 | + Reopen(&options); |
| 118 | + |
| 119 | + ASSERT_OK(Put("key1", "v1")); |
| 120 | + ASSERT_OK(Put("key2", "v2")); |
| 121 | + ASSERT_OK(Put("key3", "v3")); |
| 122 | + dbfull()->TEST_FlushMemTable(); |
| 123 | + |
| 124 | + TablePropertiesCollection ptc; |
| 125 | + reinterpret_cast<DB*>(dbfull())->GetPropertiesOfAllTables(&ptc); |
| 126 | + ASSERT_EQ(1U, ptc.size()); |
| 127 | + ASSERT_EQ(3, ptc.begin()->second->num_entries); |
| 128 | + ASSERT_EQ("1", FilesPerLevel()); |
| 129 | + |
| 130 | + ASSERT_EQ("v1", Get("key1")); |
| 131 | + ASSERT_EQ("v2", Get("key2")); |
| 132 | + ASSERT_EQ("v3", Get("key3")); |
| 133 | + ASSERT_EQ("NOT_FOUND", Get("key4")); |
| 134 | + ASSERT_EQ("Invalid argument: Length of key is invalid.", Get("somelongkey")); |
| 135 | + ASSERT_EQ("Invalid argument: Length of key is invalid.", Get("s")); |
| 136 | + |
| 137 | + // Now add more keys and flush. |
| 138 | + ASSERT_OK(Put("key4", "v4")); |
| 139 | + ASSERT_OK(Put("key5", "v5")); |
| 140 | + ASSERT_OK(Put("key6", "v6")); |
| 141 | + dbfull()->TEST_FlushMemTable(); |
| 142 | + |
| 143 | + reinterpret_cast<DB*>(dbfull())->GetPropertiesOfAllTables(&ptc); |
| 144 | + ASSERT_EQ(2U, ptc.size()); |
| 145 | + auto row = ptc.begin(); |
| 146 | + ASSERT_EQ(3, row->second->num_entries); |
| 147 | + ASSERT_EQ(3, (++row)->second->num_entries); |
| 148 | + ASSERT_EQ("2", FilesPerLevel()); |
| 149 | + ASSERT_EQ("v1", Get("key1")); |
| 150 | + ASSERT_EQ("v2", Get("key2")); |
| 151 | + ASSERT_EQ("v3", Get("key3")); |
| 152 | + ASSERT_EQ("v4", Get("key4")); |
| 153 | + ASSERT_EQ("v5", Get("key5")); |
| 154 | + ASSERT_EQ("v6", Get("key6")); |
| 155 | + |
| 156 | + ASSERT_OK(Delete("key6")); |
| 157 | + ASSERT_OK(Delete("key5")); |
| 158 | + ASSERT_OK(Delete("key4")); |
| 159 | + dbfull()->TEST_FlushMemTable(); |
| 160 | + reinterpret_cast<DB*>(dbfull())->GetPropertiesOfAllTables(&ptc); |
| 161 | + ASSERT_EQ(3U, ptc.size()); |
| 162 | + row = ptc.begin(); |
| 163 | + ASSERT_EQ(3, row->second->num_entries); |
| 164 | + ASSERT_EQ(3, (++row)->second->num_entries); |
| 165 | + ASSERT_EQ(3, (++row)->second->num_entries); |
| 166 | + ASSERT_EQ("3", FilesPerLevel()); |
| 167 | + ASSERT_EQ("v1", Get("key1")); |
| 168 | + ASSERT_EQ("v2", Get("key2")); |
| 169 | + ASSERT_EQ("v3", Get("key3")); |
| 170 | + ASSERT_EQ("NOT_FOUND", Get("key4")); |
| 171 | + ASSERT_EQ("NOT_FOUND", Get("key5")); |
| 172 | + ASSERT_EQ("NOT_FOUND", Get("key6")); |
| 173 | +} |
| 174 | + |
| 175 | +TEST(CuckooTableDBTest, FlushWithDuplicateKeys) { |
| 176 | + Options options = CurrentOptions(); |
| 177 | + Reopen(&options); |
| 178 | + ASSERT_OK(Put("key1", "v1")); |
| 179 | + ASSERT_OK(Put("key2", "v2")); |
| 180 | + ASSERT_OK(Put("key1", "v3")); // Duplicate |
| 181 | + dbfull()->TEST_FlushMemTable(); |
| 182 | + |
| 183 | + TablePropertiesCollection ptc; |
| 184 | + reinterpret_cast<DB*>(dbfull())->GetPropertiesOfAllTables(&ptc); |
| 185 | + ASSERT_EQ(1U, ptc.size()); |
| 186 | + ASSERT_EQ(2, ptc.begin()->second->num_entries); |
| 187 | + ASSERT_EQ("1", FilesPerLevel()); |
| 188 | + ASSERT_EQ("v3", Get("key1")); |
| 189 | + ASSERT_EQ("v2", Get("key2")); |
| 190 | +} |
| 191 | + |
| 192 | +namespace { |
| 193 | +static std::string Key(int i) { |
| 194 | + char buf[100]; |
| 195 | + snprintf(buf, sizeof(buf), "key_______%06d", i); |
| 196 | + return std::string(buf); |
| 197 | +} |
| 198 | +} |
| 199 | + |
| 200 | +TEST(CuckooTableDBTest, CompactionTrigger) { |
| 201 | + Options options = CurrentOptions(); |
| 202 | + options.write_buffer_size = 100 << 10; // 100KB |
| 203 | + options.level0_file_num_compaction_trigger = 2; |
| 204 | + Reopen(&options); |
| 205 | + |
| 206 | + // Write 11 values, each 10016 B |
| 207 | + for (int idx = 0; idx < 11; ++idx) { |
| 208 | + ASSERT_OK(Put(Key(idx), std::string(10000, 'a' + idx))); |
| 209 | + } |
| 210 | + dbfull()->TEST_WaitForFlushMemTable(); |
| 211 | + ASSERT_EQ("1", FilesPerLevel()); |
| 212 | + |
| 213 | + // Generate one more file in level-0, and should trigger level-0 compaction |
| 214 | + for (int idx = 11; idx < 22; ++idx) { |
| 215 | + ASSERT_OK(Put(Key(idx), std::string(10000, 'a' + idx))); |
| 216 | + } |
| 217 | + dbfull()->TEST_WaitForFlushMemTable(); |
| 218 | + dbfull()->TEST_CompactRange(0, nullptr, nullptr); |
| 219 | + |
| 220 | + ASSERT_EQ("0,2", FilesPerLevel()); |
| 221 | + for (int idx = 0; idx < 22; ++idx) { |
| 222 | + ASSERT_EQ(std::string(10000, 'a' + idx), Get(Key(idx))); |
| 223 | + } |
| 224 | +} |
| 225 | + |
| 226 | +TEST(CuckooTableDBTest, SameKeyInsertedInTwoDifferentFilesAndCompacted) { |
| 227 | + // Insert same key twice so that they go to different SST files. Then wait for |
| 228 | + // compaction and check if the latest value is stored and old value removed. |
| 229 | + Options options = CurrentOptions(); |
| 230 | + options.write_buffer_size = 100 << 10; // 100KB |
| 231 | + options.level0_file_num_compaction_trigger = 2; |
| 232 | + Reopen(&options); |
| 233 | + |
| 234 | + // Write 11 values, each 10016 B |
| 235 | + for (int idx = 0; idx < 11; ++idx) { |
| 236 | + ASSERT_OK(Put(Key(idx), std::string(10000, 'a'))); |
| 237 | + } |
| 238 | + dbfull()->TEST_WaitForFlushMemTable(); |
| 239 | + ASSERT_EQ("1", FilesPerLevel()); |
| 240 | + |
| 241 | + // Generate one more file in level-0, and should trigger level-0 compaction |
| 242 | + for (int idx = 0; idx < 11; ++idx) { |
| 243 | + ASSERT_OK(Put(Key(idx), std::string(10000, 'a' + idx))); |
| 244 | + } |
| 245 | + dbfull()->TEST_WaitForFlushMemTable(); |
| 246 | + dbfull()->TEST_CompactRange(0, nullptr, nullptr); |
| 247 | + |
| 248 | + ASSERT_EQ("0,1", FilesPerLevel()); |
| 249 | + for (int idx = 0; idx < 11; ++idx) { |
| 250 | + ASSERT_EQ(std::string(10000, 'a' + idx), Get(Key(idx))); |
| 251 | + } |
| 252 | +} |
| 253 | + |
| 254 | +TEST(CuckooTableDBTest, AdaptiveTable) { |
| 255 | + Options options = CurrentOptions(); |
| 256 | + |
| 257 | + // Write some keys using cuckoo table. |
| 258 | + options.table_factory.reset(NewCuckooTableFactory()); |
| 259 | + Reopen(&options); |
| 260 | + |
| 261 | + ASSERT_OK(Put("key1", "v1")); |
| 262 | + ASSERT_OK(Put("key2", "v2")); |
| 263 | + ASSERT_OK(Put("key3", "v3")); |
| 264 | + dbfull()->TEST_FlushMemTable(); |
| 265 | + |
| 266 | + // Write some keys using plain table. |
| 267 | + options.create_if_missing = false; |
| 268 | + options.table_factory.reset(NewPlainTableFactory()); |
| 269 | + Reopen(&options); |
| 270 | + ASSERT_OK(Put("key4", "v4")); |
| 271 | + ASSERT_OK(Put("key1", "v5")); |
| 272 | + dbfull()->TEST_FlushMemTable(); |
| 273 | + |
| 274 | + // Write some keys using block based table. |
| 275 | + std::shared_ptr<TableFactory> block_based_factory( |
| 276 | + NewBlockBasedTableFactory()); |
| 277 | + options.table_factory.reset(NewAdaptiveTableFactory(block_based_factory)); |
| 278 | + Reopen(&options); |
| 279 | + ASSERT_OK(Put("key5", "v6")); |
| 280 | + ASSERT_OK(Put("key2", "v7")); |
| 281 | + dbfull()->TEST_FlushMemTable(); |
| 282 | + |
| 283 | + ASSERT_EQ("v5", Get("key1")); |
| 284 | + ASSERT_EQ("v7", Get("key2")); |
| 285 | + ASSERT_EQ("v3", Get("key3")); |
| 286 | + ASSERT_EQ("v4", Get("key4")); |
| 287 | + ASSERT_EQ("v6", Get("key5")); |
| 288 | +} |
| 289 | +} // namespace rocksdb |
| 290 | + |
| 291 | +int main(int argc, char** argv) { return rocksdb::test::RunAllTests(); } |
0 commit comments