/Users/deen/code/yugabyte-db/src/yb/rocksdb/db/compact_files_test.cc
Line | Count | Source |
1 | | // Copyright (c) 2011-present, 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 | | // The following only applies to changes made to this file as part of YugaByte development. |
7 | | // |
8 | | // Portions Copyright (c) YugaByte, Inc. |
9 | | // |
10 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
11 | | // in compliance with the License. You may obtain a copy of the License at |
12 | | // |
13 | | // http://www.apache.org/licenses/LICENSE-2.0 |
14 | | // |
15 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
16 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
17 | | // or implied. See the License for the specific language governing permissions and limitations |
18 | | // under the License. |
19 | | // |
20 | | |
21 | | #ifndef ROCKSDB_LITE |
22 | | |
23 | | #include <mutex> |
24 | | #include <string> |
25 | | #include <thread> |
26 | | #include <vector> |
27 | | |
28 | | #include "yb/rocksdb/db.h" |
29 | | #include "yb/rocksdb/env.h" |
30 | | #include "yb/rocksdb/util/sync_point.h" |
31 | | #include "yb/rocksdb/util/testharness.h" |
32 | | #include "yb/rocksdb/util/testutil.h" |
33 | | |
34 | | #include "yb/util/string_util.h" |
35 | | #include "yb/util/test_util.h" |
36 | | |
37 | | namespace rocksdb { |
38 | | |
39 | | class CompactFilesTest : public RocksDBTest { |
40 | | public: |
41 | 2 | CompactFilesTest() { |
42 | 2 | env_ = Env::Default(); |
43 | 2 | db_name_ = test::TmpDir(env_) + "/compact_files_test"; |
44 | 2 | } |
45 | | |
46 | | std::string db_name_; |
47 | | Env* env_; |
48 | | }; |
49 | | |
50 | 1 | TEST_F(CompactFilesTest, ObsoleteFiles) { |
51 | 1 | Options options; |
52 | | // to trigger compaction more easily |
53 | 1 | const int kWriteBufferSize = 10000; |
54 | 1 | options.create_if_missing = true; |
55 | | // Disable RocksDB background compaction. |
56 | 1 | options.compaction_style = kCompactionStyleNone; |
57 | | // Small slowdown and stop trigger for experimental purpose. |
58 | 1 | options.level0_slowdown_writes_trigger = 20; |
59 | 1 | options.level0_stop_writes_trigger = 20; |
60 | 1 | options.write_buffer_size = kWriteBufferSize; |
61 | 1 | options.max_write_buffer_number = 2; |
62 | 1 | options.compression = kNoCompression; |
63 | | |
64 | | // Add listener |
65 | 1 | auto* collector = new test::FlushedFileCollector(); |
66 | 1 | options.listeners.emplace_back(collector); |
67 | | |
68 | 1 | DB* db = nullptr; |
69 | 1 | ASSERT_OK(DestroyDB(db_name_, options)); |
70 | 1 | Status s = DB::Open(options, db_name_, &db); |
71 | 1 | assert(s.ok()); |
72 | 1 | assert(db); |
73 | | |
74 | | // create couple files |
75 | 1.00k | for (int i = 1000; i < 2000; ++i) { |
76 | 1.00k | ASSERT_OK(db->Put(WriteOptions(), ToString(i), |
77 | 1.00k | std::string(kWriteBufferSize / 10, 'a' + (i % 26)))); |
78 | 1.00k | } |
79 | | |
80 | 1 | auto l0_files = collector->GetFlushedFiles(); |
81 | 1 | ASSERT_OK(db->CompactFiles(CompactionOptions(), l0_files, 1)); |
82 | | |
83 | | // verify all compaction input files are deleted |
84 | 16 | for (auto fname : l0_files) { |
85 | 16 | ASSERT_TRUE(env_->FileExists(fname).IsNotFound()); |
86 | 16 | } |
87 | 1 | delete db; |
88 | 1 | } |
89 | | |
90 | 1 | TEST_F(CompactFilesTest, CapturingPendingFiles) { |
91 | 1 | Options options; |
92 | 1 | options.create_if_missing = true; |
93 | | // Disable RocksDB background compaction. |
94 | 1 | options.compaction_style = kCompactionStyleNone; |
95 | | // Always do full scans for obsolete files (needed to reproduce the issue). |
96 | 1 | options.delete_obsolete_files_period_micros = 0; |
97 | | |
98 | | // Add listener. |
99 | 1 | auto* collector = new test::FlushedFileCollector(); |
100 | 1 | options.listeners.emplace_back(collector); |
101 | | |
102 | 1 | DB* db = nullptr; |
103 | 1 | ASSERT_OK(DestroyDB(db_name_, options)); |
104 | 1 | Status s = DB::Open(options, db_name_, &db); |
105 | 1 | assert(s.ok()); |
106 | 1 | assert(db); |
107 | | |
108 | | // Create 5 files. |
109 | 6 | for (int i = 0; i < 5; ++i) { |
110 | 5 | ASSERT_OK(db->Put(WriteOptions(), "key" + ToString(i), "value")); |
111 | 5 | ASSERT_OK(db->Flush(FlushOptions())); |
112 | 5 | } |
113 | | |
114 | 1 | auto l0_files = collector->GetFlushedFiles(); |
115 | 1 | EXPECT_EQ(5, l0_files.size()); |
116 | | |
117 | 1 | rocksdb::SyncPoint::GetInstance()->LoadDependency({ |
118 | 1 | {"CompactFilesImpl:2", "CompactFilesTest.CapturingPendingFiles:0"}, |
119 | 1 | {"CompactFilesTest.CapturingPendingFiles:1", "CompactFilesImpl:3"}, |
120 | 1 | }); |
121 | 1 | rocksdb::SyncPoint::GetInstance()->EnableProcessing(); |
122 | | |
123 | | // Start compacting files. |
124 | 1 | std::thread compaction_thread( |
125 | 1 | [&] { EXPECT_OK(db->CompactFiles(CompactionOptions(), l0_files, 1)); }); |
126 | | |
127 | | // In the meantime flush another file. |
128 | 1 | TEST_SYNC_POINT("CompactFilesTest.CapturingPendingFiles:0"); |
129 | 1 | ASSERT_OK(db->Put(WriteOptions(), "key5", "value")); |
130 | 1 | ASSERT_OK(db->Flush(FlushOptions())); |
131 | 1 | TEST_SYNC_POINT("CompactFilesTest.CapturingPendingFiles:1"); |
132 | | |
133 | 1 | compaction_thread.join(); |
134 | | |
135 | 1 | rocksdb::SyncPoint::GetInstance()->DisableProcessing(); |
136 | | |
137 | 1 | delete db; |
138 | | |
139 | | // Make sure we can reopen the DB. |
140 | 1 | s = DB::Open(options, db_name_, &db); |
141 | 1 | ASSERT_TRUE(s.ok()); |
142 | 1 | assert(db); |
143 | 1 | delete db; |
144 | 1 | } |
145 | | |
146 | | } // namespace rocksdb |
147 | | |
148 | 13.2k | int main(int argc, char** argv) { |
149 | 13.2k | ::testing::InitGoogleTest(&argc, argv); |
150 | 13.2k | return RUN_ALL_TESTS(); |
151 | 13.2k | } |
152 | | |
153 | | #else |
154 | | #include <stdio.h> |
155 | | |
156 | | int main(int argc, char** argv) { |
157 | | fprintf(stderr, |
158 | | "SKIPPED as DBImpl::CompactFiles is not supported in ROCKSDB_LITE\n"); |
159 | | return 0; |
160 | | } |
161 | | |
162 | | #endif // !ROCKSDB_LITE |