2013-10-16 21:59:46 +00:00
|
|
|
// Copyright (c) 2013, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under the BSD-style license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
// of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
//
|
2013-08-23 15:38:13 +00:00
|
|
|
#include "rocksdb/db.h"
|
2012-10-31 18:47:18 +00:00
|
|
|
#include "db/db_impl.h"
|
|
|
|
#include "db/version_set.h"
|
|
|
|
#include "util/logging.h"
|
|
|
|
#include "util/testutil.h"
|
|
|
|
#include "util/testharness.h"
|
2014-04-15 18:28:52 +00:00
|
|
|
#include "util/ldb_cmd.h"
|
2012-10-31 18:47:18 +00:00
|
|
|
|
2013-10-04 04:49:15 +00:00
|
|
|
namespace rocksdb {
|
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() {
|
|
|
|
dbname_ = test::TmpDir() + "/db_reduce_levels_test";
|
|
|
|
DestroyDB(dbname_, Options());
|
2013-03-21 17:45:57 +00:00
|
|
|
db_ = nullptr;
|
2012-10-31 18:47:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Status OpenDB(bool create_if_missing, int levels,
|
|
|
|
int mem_table_compact_level);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status CompactMemTable() {
|
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.");
|
|
|
|
}
|
|
|
|
DBImpl* db_impl = reinterpret_cast<DBImpl*>(db_);
|
2013-10-14 22:12:15 +00:00
|
|
|
return db_impl->TEST_FlushMemTable();
|
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;
|
rocksdb: Replace ASSERT* with EXPECT* in functions that does not return void value
Summary:
gtest does not use exceptions to fail a unit test by design, and `ASSERT*`s are implemented using `return`. As a consequence we cannot use `ASSERT*` in a function that does not return `void` value ([[ https://code.google.com/p/googletest/wiki/AdvancedGuide#Assertion_Placement | 1]]), and have to fix our existing code. This diff does this in a generic way, with no manual changes.
In order to detect all existing `ASSERT*` that are used in functions that doesn't return void value, I change the code to generate compile errors for such cases.
In `util/testharness.h` I defined `EXPECT*` assertions, the same way as `ASSERT*`, and redefined `ASSERT*` to return `void`. Then executed:
```lang=bash
% USE_CLANG=1 make all -j55 -k 2> build.log
% perl -naF: -e 'print "-- -number=".$F[1]." ".$F[0]."\n" if /: error:/' \
build.log | xargs -L 1 perl -spi -e 's/ASSERT/EXPECT/g if $. == $number'
% make format
```
After that I reverted back change to `ASSERT*` in `util/testharness.h`. But preserved introduced `EXPECT*`, which is the same as `ASSERT*`. This will be deleted once switched to gtest.
This diff is independent and contains manual changes only in `util/testharness.h`.
Test Plan:
Make sure all tests are passing.
```lang=bash
% USE_CLANG=1 make check
```
Reviewers: igor, lgalanis, sdong, yufei.zhu, rven, meyering
Reviewed By: meyering
Subscribers: dhruba, leveldb
Differential Revision: https://reviews.facebook.net/D33333
2015-03-17 03:52:32 +00:00
|
|
|
EXPECT_TRUE(db_->GetProperty(
|
|
|
|
"rocksdb.num-files-at-level" + NumberToString(level), &property));
|
2012-10-31 18:47:18 +00:00
|
|
|
return atoi(property.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string dbname_;
|
|
|
|
DB* db_;
|
|
|
|
};
|
|
|
|
|
|
|
|
Status ReduceLevelTest::OpenDB(bool create_if_missing, int num_levels,
|
|
|
|
int mem_table_compact_level) {
|
2013-10-04 04:49:15 +00:00
|
|
|
rocksdb::Options opt;
|
2012-10-31 18:47:18 +00:00
|
|
|
opt.num_levels = num_levels;
|
|
|
|
opt.create_if_missing = create_if_missing;
|
|
|
|
opt.max_mem_compaction_level = mem_table_compact_level;
|
2014-09-18 20:32:44 +00:00
|
|
|
opt.max_background_flushes = 0;
|
2013-10-04 04:49:15 +00:00
|
|
|
rocksdb::Status st = rocksdb::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) {
|
2013-10-04 04:49:15 +00:00
|
|
|
std::vector<std::string> args = rocksdb::ReduceDBLevelsCommand::PrepareArgs(
|
2013-01-11 19:09:23 +00:00
|
|
|
dbname_, target_level, false);
|
2014-06-23 23:48:20 +00:00
|
|
|
LDBCommand* level_reducer = LDBCommand::InitFromCmdLineArgs(
|
|
|
|
args, Options(), LDBOptions());
|
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) {
|
2012-10-31 18:47:18 +00:00
|
|
|
// create files on all levels;
|
|
|
|
ASSERT_OK(OpenDB(true, 4, 3));
|
|
|
|
ASSERT_OK(Put("aaaa", "11111"));
|
|
|
|
ASSERT_OK(CompactMemTable());
|
|
|
|
ASSERT_EQ(FilesOnLevel(3), 1);
|
|
|
|
CloseDB();
|
|
|
|
|
|
|
|
ASSERT_TRUE(ReduceLevels(3));
|
|
|
|
ASSERT_OK(OpenDB(true, 3, 1));
|
|
|
|
ASSERT_EQ(FilesOnLevel(2), 1);
|
|
|
|
CloseDB();
|
|
|
|
|
|
|
|
ASSERT_TRUE(ReduceLevels(2));
|
|
|
|
ASSERT_OK(OpenDB(true, 2, 1));
|
|
|
|
ASSERT_EQ(FilesOnLevel(1), 1);
|
|
|
|
CloseDB();
|
|
|
|
}
|
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(ReduceLevelTest, Top_Level) {
|
2012-10-31 18:47:18 +00:00
|
|
|
// create files on all levels;
|
|
|
|
ASSERT_OK(OpenDB(true, 5, 0));
|
|
|
|
ASSERT_OK(Put("aaaa", "11111"));
|
|
|
|
ASSERT_OK(CompactMemTable());
|
|
|
|
ASSERT_EQ(FilesOnLevel(0), 1);
|
|
|
|
CloseDB();
|
|
|
|
|
|
|
|
ASSERT_TRUE(ReduceLevels(4));
|
|
|
|
ASSERT_OK(OpenDB(true, 4, 0));
|
|
|
|
CloseDB();
|
|
|
|
|
|
|
|
ASSERT_TRUE(ReduceLevels(3));
|
|
|
|
ASSERT_OK(OpenDB(true, 3, 0));
|
|
|
|
CloseDB();
|
|
|
|
|
|
|
|
ASSERT_TRUE(ReduceLevels(2));
|
|
|
|
ASSERT_OK(OpenDB(true, 2, 0));
|
|
|
|
CloseDB();
|
|
|
|
}
|
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(ReduceLevelTest, All_Levels) {
|
2012-10-31 18:47:18 +00:00
|
|
|
// create files on all levels;
|
|
|
|
ASSERT_OK(OpenDB(true, 5, 1));
|
|
|
|
ASSERT_OK(Put("a", "a11111"));
|
|
|
|
ASSERT_OK(CompactMemTable());
|
|
|
|
ASSERT_EQ(FilesOnLevel(1), 1);
|
|
|
|
CloseDB();
|
|
|
|
|
|
|
|
ASSERT_OK(OpenDB(true, 5, 2));
|
|
|
|
ASSERT_OK(Put("b", "b11111"));
|
|
|
|
ASSERT_OK(CompactMemTable());
|
|
|
|
ASSERT_EQ(FilesOnLevel(1), 1);
|
|
|
|
ASSERT_EQ(FilesOnLevel(2), 1);
|
|
|
|
CloseDB();
|
|
|
|
|
|
|
|
ASSERT_OK(OpenDB(true, 5, 3));
|
|
|
|
ASSERT_OK(Put("c", "c11111"));
|
|
|
|
ASSERT_OK(CompactMemTable());
|
|
|
|
ASSERT_EQ(FilesOnLevel(1), 1);
|
|
|
|
ASSERT_EQ(FilesOnLevel(2), 1);
|
|
|
|
ASSERT_EQ(FilesOnLevel(3), 1);
|
|
|
|
CloseDB();
|
|
|
|
|
|
|
|
ASSERT_OK(OpenDB(true, 5, 4));
|
|
|
|
ASSERT_OK(Put("d", "d11111"));
|
|
|
|
ASSERT_OK(CompactMemTable());
|
|
|
|
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));
|
|
|
|
ASSERT_OK(OpenDB(true, 4, 0));
|
|
|
|
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));
|
|
|
|
ASSERT_OK(OpenDB(true, 3, 0));
|
|
|
|
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));
|
|
|
|
ASSERT_OK(OpenDB(true, 2, 0));
|
|
|
|
ASSERT_EQ("a11111", Get("a"));
|
|
|
|
ASSERT_EQ("b11111", Get("b"));
|
|
|
|
ASSERT_EQ("c11111", Get("c"));
|
|
|
|
ASSERT_EQ("d11111", Get("d"));
|
|
|
|
CloseDB();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|