]> git.proxmox.com Git - ceph.git/blame - ceph/src/rocksdb/tools/reduce_levels_test.cc
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / rocksdb / tools / reduce_levels_test.cc
CommitLineData
7c673cae 1// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
11fdf7f2
TL
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).
7c673cae
FG
5//
6
7#ifndef ROCKSDB_LITE
8
9#include "db/db_impl.h"
10#include "db/version_set.h"
11#include "rocksdb/db.h"
12#include "rocksdb/utilities/ldb_cmd.h"
13#include "tools/ldb_cmd_impl.h"
14#include "util/string_util.h"
15#include "util/testharness.h"
16#include "util/testutil.h"
17
18namespace rocksdb {
19
20class ReduceLevelTest : public testing::Test {
21public:
22 ReduceLevelTest() {
11fdf7f2 23 dbname_ = test::PerThreadDBPath("db_reduce_levels_test");
7c673cae
FG
24 DestroyDB(dbname_, Options());
25 db_ = nullptr;
26 }
27
28 Status OpenDB(bool create_if_missing, int levels);
29
30 Status Put(const std::string& k, const std::string& v) {
31 return db_->Put(WriteOptions(), k, v);
32 }
33
34 std::string Get(const std::string& k) {
35 ReadOptions options;
36 std::string result;
37 Status s = db_->Get(options, k, &result);
38 if (s.IsNotFound()) {
39 result = "NOT_FOUND";
40 } else if (!s.ok()) {
41 result = s.ToString();
42 }
43 return result;
44 }
45
46 Status Flush() {
47 if (db_ == nullptr) {
48 return Status::InvalidArgument("DB not opened.");
49 }
50 DBImpl* db_impl = reinterpret_cast<DBImpl*>(db_);
51 return db_impl->TEST_FlushMemTable();
52 }
53
54 void MoveL0FileToLevel(int level) {
55 DBImpl* db_impl = reinterpret_cast<DBImpl*>(db_);
56 for (int i = 0; i < level; ++i) {
57 ASSERT_OK(db_impl->TEST_CompactRange(i, nullptr, nullptr));
58 }
59 }
60
61 void CloseDB() {
62 if (db_ != nullptr) {
63 delete db_;
64 db_ = nullptr;
65 }
66 }
67
68 bool ReduceLevels(int target_level);
69
70 int FilesOnLevel(int level) {
71 std::string property;
72 EXPECT_TRUE(db_->GetProperty(
73 "rocksdb.num-files-at-level" + NumberToString(level), &property));
74 return atoi(property.c_str());
75 }
76
77private:
78 std::string dbname_;
79 DB* db_;
80};
81
82Status ReduceLevelTest::OpenDB(bool create_if_missing, int num_levels) {
83 rocksdb::Options opt;
84 opt.num_levels = num_levels;
85 opt.create_if_missing = create_if_missing;
86 rocksdb::Status st = rocksdb::DB::Open(opt, dbname_, &db_);
87 if (!st.ok()) {
88 fprintf(stderr, "Can't open the db:%s\n", st.ToString().c_str());
89 }
90 return st;
91}
92
93bool ReduceLevelTest::ReduceLevels(int target_level) {
94 std::vector<std::string> args = rocksdb::ReduceDBLevelsCommand::PrepareArgs(
95 dbname_, target_level, false);
96 LDBCommand* level_reducer = LDBCommand::InitFromCmdLineArgs(
97 args, Options(), LDBOptions(), nullptr, LDBCommand::SelectCommand);
98 level_reducer->Run();
99 bool is_succeed = level_reducer->GetExecuteState().IsSucceed();
100 delete level_reducer;
101 return is_succeed;
102}
103
104TEST_F(ReduceLevelTest, Last_Level) {
105 ASSERT_OK(OpenDB(true, 4));
106 ASSERT_OK(Put("aaaa", "11111"));
107 Flush();
108 MoveL0FileToLevel(3);
109 ASSERT_EQ(FilesOnLevel(3), 1);
110 CloseDB();
111
112 ASSERT_TRUE(ReduceLevels(3));
113 ASSERT_OK(OpenDB(true, 3));
114 ASSERT_EQ(FilesOnLevel(2), 1);
115 CloseDB();
116
117 ASSERT_TRUE(ReduceLevels(2));
118 ASSERT_OK(OpenDB(true, 2));
119 ASSERT_EQ(FilesOnLevel(1), 1);
120 CloseDB();
121}
122
123TEST_F(ReduceLevelTest, Top_Level) {
124 ASSERT_OK(OpenDB(true, 5));
125 ASSERT_OK(Put("aaaa", "11111"));
126 Flush();
127 ASSERT_EQ(FilesOnLevel(0), 1);
128 CloseDB();
129
130 ASSERT_TRUE(ReduceLevels(4));
131 ASSERT_OK(OpenDB(true, 4));
132 CloseDB();
133
134 ASSERT_TRUE(ReduceLevels(3));
135 ASSERT_OK(OpenDB(true, 3));
136 CloseDB();
137
138 ASSERT_TRUE(ReduceLevels(2));
139 ASSERT_OK(OpenDB(true, 2));
140 CloseDB();
141}
142
143TEST_F(ReduceLevelTest, All_Levels) {
144 ASSERT_OK(OpenDB(true, 5));
145 ASSERT_OK(Put("a", "a11111"));
146 ASSERT_OK(Flush());
147 MoveL0FileToLevel(4);
148 ASSERT_EQ(FilesOnLevel(4), 1);
149 CloseDB();
150
151 ASSERT_OK(OpenDB(true, 5));
152 ASSERT_OK(Put("b", "b11111"));
153 ASSERT_OK(Flush());
154 MoveL0FileToLevel(3);
155 ASSERT_EQ(FilesOnLevel(3), 1);
156 ASSERT_EQ(FilesOnLevel(4), 1);
157 CloseDB();
158
159 ASSERT_OK(OpenDB(true, 5));
160 ASSERT_OK(Put("c", "c11111"));
161 ASSERT_OK(Flush());
162 MoveL0FileToLevel(2);
163 ASSERT_EQ(FilesOnLevel(2), 1);
164 ASSERT_EQ(FilesOnLevel(3), 1);
165 ASSERT_EQ(FilesOnLevel(4), 1);
166 CloseDB();
167
168 ASSERT_OK(OpenDB(true, 5));
169 ASSERT_OK(Put("d", "d11111"));
170 ASSERT_OK(Flush());
171 MoveL0FileToLevel(1);
172 ASSERT_EQ(FilesOnLevel(1), 1);
173 ASSERT_EQ(FilesOnLevel(2), 1);
174 ASSERT_EQ(FilesOnLevel(3), 1);
175 ASSERT_EQ(FilesOnLevel(4), 1);
176 CloseDB();
177
178 ASSERT_TRUE(ReduceLevels(4));
179 ASSERT_OK(OpenDB(true, 4));
180 ASSERT_EQ("a11111", Get("a"));
181 ASSERT_EQ("b11111", Get("b"));
182 ASSERT_EQ("c11111", Get("c"));
183 ASSERT_EQ("d11111", Get("d"));
184 CloseDB();
185
186 ASSERT_TRUE(ReduceLevels(3));
187 ASSERT_OK(OpenDB(true, 3));
188 ASSERT_EQ("a11111", Get("a"));
189 ASSERT_EQ("b11111", Get("b"));
190 ASSERT_EQ("c11111", Get("c"));
191 ASSERT_EQ("d11111", Get("d"));
192 CloseDB();
193
194 ASSERT_TRUE(ReduceLevels(2));
195 ASSERT_OK(OpenDB(true, 2));
196 ASSERT_EQ("a11111", Get("a"));
197 ASSERT_EQ("b11111", Get("b"));
198 ASSERT_EQ("c11111", Get("c"));
199 ASSERT_EQ("d11111", Get("d"));
200 CloseDB();
201}
202
203}
204
205int main(int argc, char** argv) {
206 ::testing::InitGoogleTest(&argc, argv);
207 return RUN_ALL_TESTS();
208}
209
210#else
211#include <stdio.h>
212
11fdf7f2 213int main(int /*argc*/, char** /*argv*/) {
7c673cae
FG
214 fprintf(stderr, "SKIPPED as LDBCommand is not supported in ROCKSDB_LITE\n");
215 return 0;
216}
217
218#endif // !ROCKSDB_LITE