manual_compaction_test.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 <iostream>
  8. #include <sstream>
  9. #include <cstdlib>
  10. #include "port/port.h"
  11. #include "rocksdb/compaction_filter.h"
  12. #include "rocksdb/db.h"
  13. #include "rocksdb/slice.h"
  14. #include "rocksdb/write_batch.h"
  15. #include "test_util/testharness.h"
  16. using namespace ROCKSDB_NAMESPACE;
  17. namespace {
  18. // Reasoning: previously the number was 1100000. Since the keys are written to
  19. // the batch in one write each write will result into one SST file. each write
  20. // will result into one SST file. We reduced the write_buffer_size to 1K to
  21. // basically have the same effect with however less number of keys, which
  22. // results into less test runtime.
  23. const int kNumKeys = 1100;
  24. std::string Key1(int i) {
  25. char buf[100];
  26. snprintf(buf, sizeof(buf), "my_key_%d", i);
  27. return buf;
  28. }
  29. std::string Key2(int i) {
  30. return Key1(i) + "_xxx";
  31. }
  32. class ManualCompactionTest : public testing::Test {
  33. public:
  34. ManualCompactionTest() {
  35. // Get rid of any state from an old run.
  36. dbname_ = ROCKSDB_NAMESPACE::test::PerThreadDBPath("rocksdb_cbug_test");
  37. DestroyDB(dbname_, ROCKSDB_NAMESPACE::Options());
  38. }
  39. std::string dbname_;
  40. };
  41. class DestroyAllCompactionFilter : public CompactionFilter {
  42. public:
  43. DestroyAllCompactionFilter() {}
  44. bool Filter(int /*level*/, const Slice& /*key*/, const Slice& existing_value,
  45. std::string* /*new_value*/,
  46. bool* /*value_changed*/) const override {
  47. return existing_value.ToString() == "destroy";
  48. }
  49. const char* Name() const override { return "DestroyAllCompactionFilter"; }
  50. };
  51. TEST_F(ManualCompactionTest, CompactTouchesAllKeys) {
  52. for (int iter = 0; iter < 2; ++iter) {
  53. DB* db;
  54. Options options;
  55. if (iter == 0) { // level compaction
  56. options.num_levels = 3;
  57. options.compaction_style = kCompactionStyleLevel;
  58. } else { // universal compaction
  59. options.compaction_style = kCompactionStyleUniversal;
  60. }
  61. options.create_if_missing = true;
  62. options.compression = ROCKSDB_NAMESPACE::kNoCompression;
  63. options.compaction_filter = new DestroyAllCompactionFilter();
  64. ASSERT_OK(DB::Open(options, dbname_, &db));
  65. db->Put(WriteOptions(), Slice("key1"), Slice("destroy"));
  66. db->Put(WriteOptions(), Slice("key2"), Slice("destroy"));
  67. db->Put(WriteOptions(), Slice("key3"), Slice("value3"));
  68. db->Put(WriteOptions(), Slice("key4"), Slice("destroy"));
  69. Slice key4("key4");
  70. db->CompactRange(CompactRangeOptions(), nullptr, &key4);
  71. Iterator* itr = db->NewIterator(ReadOptions());
  72. itr->SeekToFirst();
  73. ASSERT_TRUE(itr->Valid());
  74. ASSERT_EQ("key3", itr->key().ToString());
  75. itr->Next();
  76. ASSERT_TRUE(!itr->Valid());
  77. delete itr;
  78. delete options.compaction_filter;
  79. delete db;
  80. DestroyDB(dbname_, options);
  81. }
  82. }
  83. TEST_F(ManualCompactionTest, Test) {
  84. // Open database. Disable compression since it affects the creation
  85. // of layers and the code below is trying to test against a very
  86. // specific scenario.
  87. ROCKSDB_NAMESPACE::DB* db;
  88. ROCKSDB_NAMESPACE::Options db_options;
  89. db_options.write_buffer_size = 1024;
  90. db_options.create_if_missing = true;
  91. db_options.compression = ROCKSDB_NAMESPACE::kNoCompression;
  92. ASSERT_OK(ROCKSDB_NAMESPACE::DB::Open(db_options, dbname_, &db));
  93. // create first key range
  94. ROCKSDB_NAMESPACE::WriteBatch batch;
  95. for (int i = 0; i < kNumKeys; i++) {
  96. batch.Put(Key1(i), "value for range 1 key");
  97. }
  98. ASSERT_OK(db->Write(ROCKSDB_NAMESPACE::WriteOptions(), &batch));
  99. // create second key range
  100. batch.Clear();
  101. for (int i = 0; i < kNumKeys; i++) {
  102. batch.Put(Key2(i), "value for range 2 key");
  103. }
  104. ASSERT_OK(db->Write(ROCKSDB_NAMESPACE::WriteOptions(), &batch));
  105. // delete second key range
  106. batch.Clear();
  107. for (int i = 0; i < kNumKeys; i++) {
  108. batch.Delete(Key2(i));
  109. }
  110. ASSERT_OK(db->Write(ROCKSDB_NAMESPACE::WriteOptions(), &batch));
  111. // compact database
  112. std::string start_key = Key1(0);
  113. std::string end_key = Key1(kNumKeys - 1);
  114. ROCKSDB_NAMESPACE::Slice least(start_key.data(), start_key.size());
  115. ROCKSDB_NAMESPACE::Slice greatest(end_key.data(), end_key.size());
  116. // commenting out the line below causes the example to work correctly
  117. db->CompactRange(CompactRangeOptions(), &least, &greatest);
  118. // count the keys
  119. ROCKSDB_NAMESPACE::Iterator* iter =
  120. db->NewIterator(ROCKSDB_NAMESPACE::ReadOptions());
  121. int num_keys = 0;
  122. for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {
  123. num_keys++;
  124. }
  125. delete iter;
  126. ASSERT_EQ(kNumKeys, num_keys) << "Bad number of keys";
  127. // close database
  128. delete db;
  129. DestroyDB(dbname_, ROCKSDB_NAMESPACE::Options());
  130. }
  131. } // anonymous namespace
  132. int main(int argc, char** argv) {
  133. ::testing::InitGoogleTest(&argc, argv);
  134. return RUN_ALL_TESTS();
  135. }