manual_compaction_test.cc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. // Test for issue 178: a manual compaction causes deleted data to reappear.
  7. #include <cstdlib>
  8. #include "port/port.h"
  9. #include "rocksdb/compaction_filter.h"
  10. #include "rocksdb/db.h"
  11. #include "rocksdb/slice.h"
  12. #include "rocksdb/write_batch.h"
  13. #include "test_util/testharness.h"
  14. using ROCKSDB_NAMESPACE::CompactionFilter;
  15. using ROCKSDB_NAMESPACE::CompactionStyle;
  16. using ROCKSDB_NAMESPACE::CompactRangeOptions;
  17. using ROCKSDB_NAMESPACE::CompressionType;
  18. using ROCKSDB_NAMESPACE::DB;
  19. using ROCKSDB_NAMESPACE::DestroyDB;
  20. using ROCKSDB_NAMESPACE::FlushOptions;
  21. using ROCKSDB_NAMESPACE::Iterator;
  22. using ROCKSDB_NAMESPACE::Options;
  23. using ROCKSDB_NAMESPACE::ReadOptions;
  24. using ROCKSDB_NAMESPACE::Slice;
  25. using ROCKSDB_NAMESPACE::WriteBatch;
  26. using ROCKSDB_NAMESPACE::WriteOptions;
  27. namespace {
  28. // Reasoning: previously the number was 1100000. Since the keys are written to
  29. // the batch in one write each write will result into one SST file. each write
  30. // will result into one SST file. We reduced the write_buffer_size to 1K to
  31. // basically have the same effect with however less number of keys, which
  32. // results into less test runtime.
  33. const int kNumKeys = 1100;
  34. std::string Key1(int i) {
  35. char buf[100];
  36. snprintf(buf, sizeof(buf), "my_key_%d", i);
  37. return buf;
  38. }
  39. std::string Key2(int i) { return Key1(i) + "_xxx"; }
  40. class ManualCompactionTest : public testing::Test {
  41. public:
  42. ManualCompactionTest() {
  43. // Get rid of any state from an old run.
  44. dbname_ = ROCKSDB_NAMESPACE::test::PerThreadDBPath(
  45. "rocksdb_manual_compaction_test");
  46. EXPECT_OK(DestroyDB(dbname_, Options()));
  47. }
  48. std::string dbname_;
  49. };
  50. class DestroyAllCompactionFilter : public CompactionFilter {
  51. public:
  52. DestroyAllCompactionFilter() = default;
  53. bool Filter(int /*level*/, const Slice& /*key*/, const Slice& existing_value,
  54. std::string* /*new_value*/,
  55. bool* /*value_changed*/) const override {
  56. return existing_value.ToString() == "destroy";
  57. }
  58. const char* Name() const override { return "DestroyAllCompactionFilter"; }
  59. };
  60. class LogCompactionFilter : public CompactionFilter {
  61. public:
  62. const char* Name() const override { return "LogCompactionFilter"; }
  63. bool Filter(int level, const Slice& key, const Slice& /*existing_value*/,
  64. std::string* /*new_value*/,
  65. bool* /*value_changed*/) const override {
  66. key_level_[key.ToString()] = level;
  67. return false;
  68. }
  69. void Reset() { key_level_.clear(); }
  70. size_t NumKeys() const { return key_level_.size(); }
  71. int KeyLevel(const Slice& key) {
  72. auto it = key_level_.find(key.ToString());
  73. if (it == key_level_.end()) {
  74. return -1;
  75. }
  76. return it->second;
  77. }
  78. private:
  79. mutable std::map<std::string, int> key_level_;
  80. };
  81. TEST_F(ManualCompactionTest, CompactTouchesAllKeys) {
  82. for (int iter = 0; iter < 2; ++iter) {
  83. DB* db;
  84. Options options;
  85. if (iter == 0) { // level compaction
  86. options.num_levels = 3;
  87. options.compaction_style = CompactionStyle::kCompactionStyleLevel;
  88. } else { // universal compaction
  89. options.compaction_style = CompactionStyle::kCompactionStyleUniversal;
  90. }
  91. options.create_if_missing = true;
  92. options.compression = CompressionType::kNoCompression;
  93. options.compaction_filter = new DestroyAllCompactionFilter();
  94. ASSERT_OK(DB::Open(options, dbname_, &db));
  95. ASSERT_OK(db->Put(WriteOptions(), Slice("key1"), Slice("destroy")));
  96. ASSERT_OK(db->Put(WriteOptions(), Slice("key2"), Slice("destroy")));
  97. ASSERT_OK(db->Put(WriteOptions(), Slice("key3"), Slice("value3")));
  98. ASSERT_OK(db->Put(WriteOptions(), Slice("key4"), Slice("destroy")));
  99. Slice key4("key4");
  100. ASSERT_OK(db->CompactRange(CompactRangeOptions(), nullptr, &key4));
  101. Iterator* itr = db->NewIterator(ReadOptions());
  102. itr->SeekToFirst();
  103. ASSERT_TRUE(itr->Valid());
  104. ASSERT_EQ("key3", itr->key().ToString());
  105. itr->Next();
  106. ASSERT_TRUE(!itr->Valid());
  107. ASSERT_OK(itr->status());
  108. delete itr;
  109. delete options.compaction_filter;
  110. delete db;
  111. ASSERT_OK(DestroyDB(dbname_, options));
  112. }
  113. }
  114. TEST_F(ManualCompactionTest, Test) {
  115. // Open database. Disable compression since it affects the creation
  116. // of layers and the code below is trying to test against a very
  117. // specific scenario.
  118. DB* db;
  119. Options db_options;
  120. db_options.write_buffer_size = 1024;
  121. db_options.create_if_missing = true;
  122. db_options.compression = CompressionType::kNoCompression;
  123. ASSERT_OK(DB::Open(db_options, dbname_, &db));
  124. // create first key range
  125. WriteBatch batch;
  126. for (int i = 0; i < kNumKeys; i++) {
  127. ASSERT_OK(batch.Put(Key1(i), "value for range 1 key"));
  128. }
  129. ASSERT_OK(db->Write(WriteOptions(), &batch));
  130. // create second key range
  131. batch.Clear();
  132. for (int i = 0; i < kNumKeys; i++) {
  133. ASSERT_OK(batch.Put(Key2(i), "value for range 2 key"));
  134. }
  135. ASSERT_OK(db->Write(WriteOptions(), &batch));
  136. // delete second key range
  137. batch.Clear();
  138. for (int i = 0; i < kNumKeys; i++) {
  139. ASSERT_OK(batch.Delete(Key2(i)));
  140. }
  141. ASSERT_OK(db->Write(WriteOptions(), &batch));
  142. // compact database
  143. std::string start_key = Key1(0);
  144. std::string end_key = Key1(kNumKeys - 1);
  145. Slice least(start_key.data(), start_key.size());
  146. Slice greatest(end_key.data(), end_key.size());
  147. // commenting out the line below causes the example to work correctly
  148. ASSERT_OK(db->CompactRange(CompactRangeOptions(), &least, &greatest));
  149. // count the keys
  150. Iterator* iter = db->NewIterator(ReadOptions());
  151. int num_keys = 0;
  152. for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {
  153. num_keys++;
  154. }
  155. ASSERT_OK(iter->status());
  156. delete iter;
  157. ASSERT_EQ(kNumKeys, num_keys) << "Bad number of keys";
  158. // close database
  159. delete db;
  160. ASSERT_OK(DestroyDB(dbname_, Options()));
  161. }
  162. TEST_F(ManualCompactionTest, SkipLevel) {
  163. DB* db;
  164. Options options;
  165. options.level_compaction_dynamic_level_bytes = false;
  166. options.num_levels = 3;
  167. // Initially, flushed L0 files won't exceed 100.
  168. options.level0_file_num_compaction_trigger = 100;
  169. options.compaction_style = CompactionStyle::kCompactionStyleLevel;
  170. options.create_if_missing = true;
  171. options.compression = CompressionType::kNoCompression;
  172. LogCompactionFilter* filter = new LogCompactionFilter();
  173. options.compaction_filter = filter;
  174. ASSERT_OK(DB::Open(options, dbname_, &db));
  175. WriteOptions wo;
  176. FlushOptions fo;
  177. ASSERT_OK(db->Put(wo, "1", ""));
  178. ASSERT_OK(db->Flush(fo));
  179. ASSERT_OK(db->Put(wo, "2", ""));
  180. ASSERT_OK(db->Flush(fo));
  181. ASSERT_OK(db->Put(wo, "4", ""));
  182. ASSERT_OK(db->Put(wo, "8", ""));
  183. ASSERT_OK(db->Flush(fo));
  184. {
  185. // L0: 1, 2, [4, 8]
  186. // no file has keys in range [5, 7]
  187. Slice start("5");
  188. Slice end("7");
  189. filter->Reset();
  190. ASSERT_OK(db->CompactRange(CompactRangeOptions(), &start, &end));
  191. ASSERT_EQ(0, filter->NumKeys());
  192. }
  193. {
  194. // L0: 1, 2, [4, 8]
  195. // [3, 7] overlaps with 4 in L0
  196. Slice start("3");
  197. Slice end("7");
  198. filter->Reset();
  199. ASSERT_OK(db->CompactRange(CompactRangeOptions(), &start, &end));
  200. ASSERT_EQ(2, filter->NumKeys());
  201. ASSERT_EQ(0, filter->KeyLevel("4"));
  202. ASSERT_EQ(0, filter->KeyLevel("8"));
  203. }
  204. {
  205. // L0: 1, 2
  206. // L1: [4, 8]
  207. // no file has keys in range (-inf, 0]
  208. Slice end("0");
  209. filter->Reset();
  210. ASSERT_OK(db->CompactRange(CompactRangeOptions(), nullptr, &end));
  211. ASSERT_EQ(0, filter->NumKeys());
  212. }
  213. {
  214. // L0: 1, 2
  215. // L1: [4, 8]
  216. // no file has keys in range [9, inf)
  217. Slice start("9");
  218. filter->Reset();
  219. ASSERT_OK(db->CompactRange(CompactRangeOptions(), &start, nullptr));
  220. ASSERT_EQ(0, filter->NumKeys());
  221. }
  222. {
  223. // L0: 1, 2
  224. // L1: [4, 8]
  225. // [2, 2] overlaps with 2 in L0
  226. Slice start("2");
  227. Slice end("2");
  228. filter->Reset();
  229. ASSERT_OK(db->CompactRange(CompactRangeOptions(), &start, &end));
  230. ASSERT_EQ(1, filter->NumKeys());
  231. ASSERT_EQ(0, filter->KeyLevel("2"));
  232. }
  233. {
  234. // L0: 1
  235. // L1: 2, [4, 8]
  236. // [2, 5] overlaps with 2 and [4, 8) in L1, skip L0
  237. Slice start("2");
  238. Slice end("5");
  239. filter->Reset();
  240. ASSERT_OK(db->CompactRange(CompactRangeOptions(), &start, &end));
  241. ASSERT_EQ(3, filter->NumKeys());
  242. ASSERT_EQ(1, filter->KeyLevel("2"));
  243. ASSERT_EQ(1, filter->KeyLevel("4"));
  244. ASSERT_EQ(1, filter->KeyLevel("8"));
  245. }
  246. {
  247. // L0: 1
  248. // L1: [2, 4, 8]
  249. // [0, inf) overlaps all files
  250. Slice start("0");
  251. filter->Reset();
  252. ASSERT_OK(db->CompactRange(CompactRangeOptions(), &start, nullptr));
  253. ASSERT_EQ(4, filter->NumKeys());
  254. // 1 is first compacted from L0 to L1, and then L1 intra level compaction
  255. // compacts [2, 4, 8] only.
  256. ASSERT_EQ(0, filter->KeyLevel("1"));
  257. ASSERT_EQ(1, filter->KeyLevel("2"));
  258. ASSERT_EQ(1, filter->KeyLevel("4"));
  259. ASSERT_EQ(1, filter->KeyLevel("8"));
  260. }
  261. delete filter;
  262. delete db;
  263. ASSERT_OK(DestroyDB(dbname_, options));
  264. }
  265. } // anonymous namespace
  266. int main(int argc, char** argv) {
  267. ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
  268. ::testing::InitGoogleTest(&argc, argv);
  269. return RUN_ALL_TESTS();
  270. }