2016-02-09 23:12:00 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-15 23:03:42 +00:00
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2013-10-16 21:59:46 +00:00
|
|
|
//
|
2015-07-14 01:45:55 +00:00
|
|
|
|
|
|
|
#ifndef ROCKSDB_LITE
|
|
|
|
|
2019-05-31 18:52:59 +00:00
|
|
|
#include "db/db_impl/db_impl.h"
|
2012-10-31 18:47:18 +00:00
|
|
|
#include "db/version_set.h"
|
2016-05-06 23:09:09 +00:00
|
|
|
#include "rocksdb/db.h"
|
|
|
|
#include "rocksdb/utilities/ldb_cmd.h"
|
2019-05-30 18:21:38 +00:00
|
|
|
#include "test_util/testharness.h"
|
|
|
|
#include "test_util/testutil.h"
|
2019-05-31 00:39:43 +00:00
|
|
|
#include "tools/ldb_cmd_impl.h"
|
2020-07-03 02:24:25 +00:00
|
|
|
#include "util/cast_util.h"
|
2019-05-31 00:39:43 +00:00
|
|
|
#include "util/string_util.h"
|
2012-10-31 18:47:18 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2012-10-31 18:47:18 +00:00
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
class ReduceLevelTest : public testing::Test {
|
2012-10-31 18:47:18 +00:00
|
|
|
public:
|
|
|
|
ReduceLevelTest() {
|
2018-07-14 00:18:39 +00:00
|
|
|
dbname_ = test::PerThreadDBPath("db_reduce_levels_test");
|
2012-10-31 18:47:18 +00:00
|
|
|
DestroyDB(dbname_, Options());
|
2013-03-21 17:45:57 +00:00
|
|
|
db_ = nullptr;
|
2012-10-31 18:47:18 +00:00
|
|
|
}
|
|
|
|
|
2015-07-17 19:02:52 +00:00
|
|
|
Status OpenDB(bool create_if_missing, int levels);
|
2012-10-31 18:47:18 +00:00
|
|
|
|
|
|
|
Status Put(const std::string& k, const std::string& v) {
|
|
|
|
return db_->Put(WriteOptions(), k, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Get(const std::string& k) {
|
|
|
|
ReadOptions options;
|
|
|
|
std::string result;
|
|
|
|
Status s = db_->Get(options, k, &result);
|
|
|
|
if (s.IsNotFound()) {
|
|
|
|
result = "NOT_FOUND";
|
|
|
|
} else if (!s.ok()) {
|
|
|
|
result = s.ToString();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-07-17 19:02:52 +00:00
|
|
|
Status Flush() {
|
2013-03-21 17:45:57 +00:00
|
|
|
if (db_ == nullptr) {
|
2012-10-31 18:47:18 +00:00
|
|
|
return Status::InvalidArgument("DB not opened.");
|
|
|
|
}
|
2020-07-03 02:24:25 +00:00
|
|
|
DBImpl* db_impl = static_cast_with_check<DBImpl>(db_);
|
2013-10-14 22:12:15 +00:00
|
|
|
return db_impl->TEST_FlushMemTable();
|
2012-10-31 18:47:18 +00:00
|
|
|
}
|
|
|
|
|
2015-07-17 19:02:52 +00:00
|
|
|
void MoveL0FileToLevel(int level) {
|
2020-07-03 02:24:25 +00:00
|
|
|
DBImpl* db_impl = static_cast_with_check<DBImpl>(db_);
|
2015-07-17 19:02:52 +00:00
|
|
|
for (int i = 0; i < level; ++i) {
|
|
|
|
ASSERT_OK(db_impl->TEST_CompactRange(i, nullptr, nullptr));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-31 18:47:18 +00:00
|
|
|
void CloseDB() {
|
2013-03-21 17:45:57 +00:00
|
|
|
if (db_ != nullptr) {
|
2012-10-31 18:47:18 +00:00
|
|
|
delete db_;
|
2013-03-21 17:45:57 +00:00
|
|
|
db_ = nullptr;
|
2012-10-31 18:47:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ReduceLevels(int target_level);
|
|
|
|
|
|
|
|
int FilesOnLevel(int level) {
|
|
|
|
std::string property;
|
2022-05-06 20:03:58 +00:00
|
|
|
EXPECT_TRUE(db_->GetProperty(
|
|
|
|
"rocksdb.num-files-at-level" + std::to_string(level), &property));
|
2012-10-31 18:47:18 +00:00
|
|
|
return atoi(property.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string dbname_;
|
|
|
|
DB* db_;
|
|
|
|
};
|
|
|
|
|
2015-07-17 19:02:52 +00:00
|
|
|
Status ReduceLevelTest::OpenDB(bool create_if_missing, int num_levels) {
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::Options opt;
|
2012-10-31 18:47:18 +00:00
|
|
|
opt.num_levels = num_levels;
|
|
|
|
opt.create_if_missing = create_if_missing;
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::Status st =
|
|
|
|
ROCKSDB_NAMESPACE::DB::Open(opt, dbname_, &db_);
|
2012-10-31 18:47:18 +00:00
|
|
|
if (!st.ok()) {
|
|
|
|
fprintf(stderr, "Can't open the db:%s\n", st.ToString().c_str());
|
|
|
|
}
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ReduceLevelTest::ReduceLevels(int target_level) {
|
2020-02-20 20:07:53 +00:00
|
|
|
std::vector<std::string> args =
|
|
|
|
ROCKSDB_NAMESPACE::ReduceDBLevelsCommand::PrepareArgs(
|
|
|
|
dbname_, target_level, false);
|
2016-05-13 19:12:39 +00:00
|
|
|
LDBCommand* level_reducer = LDBCommand::InitFromCmdLineArgs(
|
|
|
|
args, Options(), LDBOptions(), nullptr, LDBCommand::SelectCommand);
|
2013-01-11 19:09:23 +00:00
|
|
|
level_reducer->Run();
|
|
|
|
bool is_succeed = level_reducer->GetExecuteState().IsSucceed();
|
|
|
|
delete level_reducer;
|
|
|
|
return is_succeed;
|
2012-10-31 18:47:18 +00:00
|
|
|
}
|
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(ReduceLevelTest, Last_Level) {
|
2015-07-17 19:02:52 +00:00
|
|
|
ASSERT_OK(OpenDB(true, 4));
|
2012-10-31 18:47:18 +00:00
|
|
|
ASSERT_OK(Put("aaaa", "11111"));
|
2020-12-23 07:44:44 +00:00
|
|
|
ASSERT_OK(Flush());
|
2015-07-17 19:02:52 +00:00
|
|
|
MoveL0FileToLevel(3);
|
2012-10-31 18:47:18 +00:00
|
|
|
ASSERT_EQ(FilesOnLevel(3), 1);
|
|
|
|
CloseDB();
|
|
|
|
|
|
|
|
ASSERT_TRUE(ReduceLevels(3));
|
2015-07-17 19:02:52 +00:00
|
|
|
ASSERT_OK(OpenDB(true, 3));
|
2012-10-31 18:47:18 +00:00
|
|
|
ASSERT_EQ(FilesOnLevel(2), 1);
|
|
|
|
CloseDB();
|
|
|
|
|
|
|
|
ASSERT_TRUE(ReduceLevels(2));
|
2015-07-17 19:02:52 +00:00
|
|
|
ASSERT_OK(OpenDB(true, 2));
|
2012-10-31 18:47:18 +00:00
|
|
|
ASSERT_EQ(FilesOnLevel(1), 1);
|
|
|
|
CloseDB();
|
|
|
|
}
|
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(ReduceLevelTest, Top_Level) {
|
2015-07-17 19:02:52 +00:00
|
|
|
ASSERT_OK(OpenDB(true, 5));
|
2012-10-31 18:47:18 +00:00
|
|
|
ASSERT_OK(Put("aaaa", "11111"));
|
2020-12-23 07:44:44 +00:00
|
|
|
ASSERT_OK(Flush());
|
2012-10-31 18:47:18 +00:00
|
|
|
ASSERT_EQ(FilesOnLevel(0), 1);
|
|
|
|
CloseDB();
|
|
|
|
|
|
|
|
ASSERT_TRUE(ReduceLevels(4));
|
2015-07-17 19:02:52 +00:00
|
|
|
ASSERT_OK(OpenDB(true, 4));
|
2012-10-31 18:47:18 +00:00
|
|
|
CloseDB();
|
|
|
|
|
|
|
|
ASSERT_TRUE(ReduceLevels(3));
|
2015-07-17 19:02:52 +00:00
|
|
|
ASSERT_OK(OpenDB(true, 3));
|
2012-10-31 18:47:18 +00:00
|
|
|
CloseDB();
|
|
|
|
|
|
|
|
ASSERT_TRUE(ReduceLevels(2));
|
2015-07-17 19:02:52 +00:00
|
|
|
ASSERT_OK(OpenDB(true, 2));
|
2012-10-31 18:47:18 +00:00
|
|
|
CloseDB();
|
|
|
|
}
|
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(ReduceLevelTest, All_Levels) {
|
2015-07-17 19:02:52 +00:00
|
|
|
ASSERT_OK(OpenDB(true, 5));
|
2012-10-31 18:47:18 +00:00
|
|
|
ASSERT_OK(Put("a", "a11111"));
|
2015-07-17 19:02:52 +00:00
|
|
|
ASSERT_OK(Flush());
|
|
|
|
MoveL0FileToLevel(4);
|
|
|
|
ASSERT_EQ(FilesOnLevel(4), 1);
|
2012-10-31 18:47:18 +00:00
|
|
|
CloseDB();
|
|
|
|
|
2015-07-17 19:02:52 +00:00
|
|
|
ASSERT_OK(OpenDB(true, 5));
|
2012-10-31 18:47:18 +00:00
|
|
|
ASSERT_OK(Put("b", "b11111"));
|
2015-07-17 19:02:52 +00:00
|
|
|
ASSERT_OK(Flush());
|
|
|
|
MoveL0FileToLevel(3);
|
|
|
|
ASSERT_EQ(FilesOnLevel(3), 1);
|
|
|
|
ASSERT_EQ(FilesOnLevel(4), 1);
|
2012-10-31 18:47:18 +00:00
|
|
|
CloseDB();
|
|
|
|
|
2015-07-17 19:02:52 +00:00
|
|
|
ASSERT_OK(OpenDB(true, 5));
|
2012-10-31 18:47:18 +00:00
|
|
|
ASSERT_OK(Put("c", "c11111"));
|
2015-07-17 19:02:52 +00:00
|
|
|
ASSERT_OK(Flush());
|
|
|
|
MoveL0FileToLevel(2);
|
2012-10-31 18:47:18 +00:00
|
|
|
ASSERT_EQ(FilesOnLevel(2), 1);
|
|
|
|
ASSERT_EQ(FilesOnLevel(3), 1);
|
2015-07-17 19:02:52 +00:00
|
|
|
ASSERT_EQ(FilesOnLevel(4), 1);
|
2012-10-31 18:47:18 +00:00
|
|
|
CloseDB();
|
|
|
|
|
2015-07-17 19:02:52 +00:00
|
|
|
ASSERT_OK(OpenDB(true, 5));
|
2012-10-31 18:47:18 +00:00
|
|
|
ASSERT_OK(Put("d", "d11111"));
|
2015-07-17 19:02:52 +00:00
|
|
|
ASSERT_OK(Flush());
|
|
|
|
MoveL0FileToLevel(1);
|
2012-10-31 18:47:18 +00:00
|
|
|
ASSERT_EQ(FilesOnLevel(1), 1);
|
|
|
|
ASSERT_EQ(FilesOnLevel(2), 1);
|
|
|
|
ASSERT_EQ(FilesOnLevel(3), 1);
|
|
|
|
ASSERT_EQ(FilesOnLevel(4), 1);
|
|
|
|
CloseDB();
|
|
|
|
|
|
|
|
ASSERT_TRUE(ReduceLevels(4));
|
2015-07-17 19:02:52 +00:00
|
|
|
ASSERT_OK(OpenDB(true, 4));
|
2012-10-31 18:47:18 +00:00
|
|
|
ASSERT_EQ("a11111", Get("a"));
|
|
|
|
ASSERT_EQ("b11111", Get("b"));
|
|
|
|
ASSERT_EQ("c11111", Get("c"));
|
|
|
|
ASSERT_EQ("d11111", Get("d"));
|
|
|
|
CloseDB();
|
|
|
|
|
|
|
|
ASSERT_TRUE(ReduceLevels(3));
|
2015-07-17 19:02:52 +00:00
|
|
|
ASSERT_OK(OpenDB(true, 3));
|
2012-10-31 18:47:18 +00:00
|
|
|
ASSERT_EQ("a11111", Get("a"));
|
|
|
|
ASSERT_EQ("b11111", Get("b"));
|
|
|
|
ASSERT_EQ("c11111", Get("c"));
|
|
|
|
ASSERT_EQ("d11111", Get("d"));
|
|
|
|
CloseDB();
|
|
|
|
|
|
|
|
ASSERT_TRUE(ReduceLevels(2));
|
2015-07-17 19:02:52 +00:00
|
|
|
ASSERT_OK(OpenDB(true, 2));
|
2012-10-31 18:47:18 +00:00
|
|
|
ASSERT_EQ("a11111", Get("a"));
|
|
|
|
ASSERT_EQ("b11111", Get("b"));
|
|
|
|
ASSERT_EQ("c11111", Get("c"));
|
|
|
|
ASSERT_EQ("d11111", Get("d"));
|
|
|
|
CloseDB();
|
|
|
|
}
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
2012-10-31 18:47:18 +00:00
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2015-03-17 21:08:00 +00:00
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
|
|
return RUN_ALL_TESTS();
|
2012-10-31 18:47:18 +00:00
|
|
|
}
|
2015-07-14 01:45:55 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2018-04-16 00:19:57 +00:00
|
|
|
int main(int /*argc*/, char** /*argv*/) {
|
2015-07-14 01:45:55 +00:00
|
|
|
fprintf(stderr, "SKIPPED as LDBCommand is not supported in ROCKSDB_LITE\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // !ROCKSDB_LITE
|