2022-08-26 01:52:37 +00:00
|
|
|
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
|
|
//
|
2020-08-15 03:11:35 +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).
|
|
|
|
|
2022-08-26 01:52:37 +00:00
|
|
|
#include "db/periodic_task_scheduler.h"
|
2020-08-15 03:11:35 +00:00
|
|
|
|
|
|
|
#include "db/db_test_util.h"
|
2021-01-26 06:07:26 +00:00
|
|
|
#include "env/composite_env_wrapper.h"
|
|
|
|
#include "test_util/mock_time_env.h"
|
2020-08-15 03:11:35 +00:00
|
|
|
|
|
|
|
namespace ROCKSDB_NAMESPACE {
|
|
|
|
|
2022-08-26 01:52:37 +00:00
|
|
|
class PeriodicTaskSchedulerTest : public DBTestBase {
|
2020-08-15 03:11:35 +00:00
|
|
|
public:
|
2022-08-26 01:52:37 +00:00
|
|
|
PeriodicTaskSchedulerTest()
|
|
|
|
: DBTestBase("periodic_task_scheduler_test", /*env_do_fsync=*/true) {
|
2021-01-26 06:07:26 +00:00
|
|
|
mock_clock_ = std::make_shared<MockSystemClock>(env_->GetSystemClock());
|
|
|
|
mock_env_.reset(new CompositeEnvWrapper(env_, mock_clock_));
|
Fix many tests to run with MEM_ENV and ENCRYPTED_ENV; Introduce a MemoryFileSystem class (#7566)
Summary:
This PR does a few things:
1. The MockFileSystem class was split out from the MockEnv. This change would theoretically allow a MockFileSystem to be used by other Environments as well (if we created a means of constructing one). The MockFileSystem implements a FileSystem in its entirety and does not rely on any Wrapper implementation.
2. Make the RocksDB test suite work when MOCK_ENV=1 and ENCRYPTED_ENV=1 are set. To accomplish this, a few things were needed:
- The tests that tried to use the "wrong" environment (Env::Default() instead of env_) were updated
- The MockFileSystem was changed to support the features it was missing or mishandled (such as recursively deleting files in a directory or supporting renaming of a directory).
3. Updated the test framework to have a ROCKSDB_GTEST_SKIP macro. This can be used to flag tests that are skipped. Currently, this defaults to doing nothing (marks the test as SUCCESS) but will mark the tests as SKIPPED when RocksDB is upgraded to a version of gtest that supports this (gtest-1.10).
I have run a full "make check" with MEM_ENV, ENCRYPTED_ENV, both, and neither under both MacOS and RedHat. A few tests were disabled/skipped for the MEM/ENCRYPTED cases. The error_handler_fs_test fails/hangs for MEM_ENV (presumably a timing problem) and I will introduce another PR/issue to track that problem. (I will also push a change to disable those tests soon). There is one more test in DBTest2 that also fails which I need to investigate or skip before this PR is merged.
Theoretically, this PR should also allow the test suite to run against an Env loaded from the registry, though I do not have one to try it with currently.
Finally, once this is accepted, it would be nice if there was a CircleCI job to run these tests on a checkin so this effort does not become stale. I do not know how to do that, so if someone could write that job, it would be appreciated :)
Pull Request resolved: https://github.com/facebook/rocksdb/pull/7566
Reviewed By: zhichao-cao
Differential Revision: D24408980
Pulled By: jay-zhuang
fbshipit-source-id: 911b1554a4d0da06fd51feca0c090a4abdcb4a5f
2020-10-27 17:31:34 +00:00
|
|
|
}
|
2020-08-15 03:11:35 +00:00
|
|
|
|
|
|
|
protected:
|
2021-01-26 06:07:26 +00:00
|
|
|
std::unique_ptr<Env> mock_env_;
|
|
|
|
std::shared_ptr<MockSystemClock> mock_clock_;
|
2020-08-15 03:11:35 +00:00
|
|
|
|
|
|
|
void SetUp() override {
|
2021-01-26 06:07:26 +00:00
|
|
|
mock_clock_->InstallTimedWaitFixCallback();
|
2020-08-15 03:11:35 +00:00
|
|
|
SyncPoint::GetInstance()->SetCallBack(
|
2022-08-26 01:52:37 +00:00
|
|
|
"DBImpl::StartPeriodicTaskScheduler:Init", [&](void* arg) {
|
|
|
|
auto periodic_task_scheduler_ptr =
|
|
|
|
reinterpret_cast<PeriodicTaskScheduler*>(arg);
|
|
|
|
periodic_task_scheduler_ptr->TEST_OverrideTimer(mock_clock_.get());
|
2020-08-15 03:11:35 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-08-26 01:52:37 +00:00
|
|
|
TEST_F(PeriodicTaskSchedulerTest, Basic) {
|
|
|
|
constexpr unsigned int kPeriodSec = 10;
|
2020-08-15 03:11:35 +00:00
|
|
|
Close();
|
|
|
|
Options options;
|
|
|
|
options.stats_dump_period_sec = kPeriodSec;
|
|
|
|
options.stats_persist_period_sec = kPeriodSec;
|
|
|
|
options.create_if_missing = true;
|
|
|
|
options.env = mock_env_.get();
|
|
|
|
|
|
|
|
int dump_st_counter = 0;
|
|
|
|
SyncPoint::GetInstance()->SetCallBack("DBImpl::DumpStats:StartRunning",
|
|
|
|
[&](void*) { dump_st_counter++; });
|
|
|
|
|
|
|
|
int pst_st_counter = 0;
|
|
|
|
SyncPoint::GetInstance()->SetCallBack("DBImpl::PersistStats:StartRunning",
|
|
|
|
[&](void*) { pst_st_counter++; });
|
2020-10-02 02:12:26 +00:00
|
|
|
|
|
|
|
int flush_info_log_counter = 0;
|
|
|
|
SyncPoint::GetInstance()->SetCallBack(
|
|
|
|
"DBImpl::FlushInfoLog:StartRunning",
|
|
|
|
[&](void*) { flush_info_log_counter++; });
|
2020-08-15 03:11:35 +00:00
|
|
|
SyncPoint::GetInstance()->EnableProcessing();
|
|
|
|
|
|
|
|
Reopen(options);
|
|
|
|
|
2020-10-02 02:12:26 +00:00
|
|
|
ASSERT_EQ(kPeriodSec, dbfull()->GetDBOptions().stats_dump_period_sec);
|
|
|
|
ASSERT_EQ(kPeriodSec, dbfull()->GetDBOptions().stats_persist_period_sec);
|
2020-08-15 03:11:35 +00:00
|
|
|
|
2020-10-02 02:12:26 +00:00
|
|
|
ASSERT_GT(kPeriodSec, 1u);
|
2022-12-12 18:37:55 +00:00
|
|
|
dbfull()->TEST_WaitForPeriodicTaskRun([&] {
|
2021-01-26 06:07:26 +00:00
|
|
|
mock_clock_->MockSleepForSeconds(static_cast<int>(kPeriodSec) - 1);
|
2020-10-02 02:12:26 +00:00
|
|
|
});
|
2020-08-15 03:11:35 +00:00
|
|
|
|
2022-08-26 01:52:37 +00:00
|
|
|
const PeriodicTaskScheduler& scheduler =
|
|
|
|
dbfull()->TEST_GetPeriodicTaskScheduler();
|
|
|
|
ASSERT_EQ(3, scheduler.TEST_GetValidTaskNum());
|
2020-08-15 03:11:35 +00:00
|
|
|
|
|
|
|
ASSERT_EQ(1, dump_st_counter);
|
|
|
|
ASSERT_EQ(1, pst_st_counter);
|
2020-10-02 02:12:26 +00:00
|
|
|
ASSERT_EQ(1, flush_info_log_counter);
|
2020-08-15 03:11:35 +00:00
|
|
|
|
2022-12-12 18:37:55 +00:00
|
|
|
dbfull()->TEST_WaitForPeriodicTaskRun(
|
2021-01-26 06:07:26 +00:00
|
|
|
[&] { mock_clock_->MockSleepForSeconds(static_cast<int>(kPeriodSec)); });
|
2020-08-15 03:11:35 +00:00
|
|
|
|
|
|
|
ASSERT_EQ(2, dump_st_counter);
|
|
|
|
ASSERT_EQ(2, pst_st_counter);
|
2020-10-02 02:12:26 +00:00
|
|
|
ASSERT_EQ(2, flush_info_log_counter);
|
2020-08-15 03:11:35 +00:00
|
|
|
|
2022-12-12 18:37:55 +00:00
|
|
|
dbfull()->TEST_WaitForPeriodicTaskRun(
|
2021-01-26 06:07:26 +00:00
|
|
|
[&] { mock_clock_->MockSleepForSeconds(static_cast<int>(kPeriodSec)); });
|
2020-08-15 03:11:35 +00:00
|
|
|
|
|
|
|
ASSERT_EQ(3, dump_st_counter);
|
|
|
|
ASSERT_EQ(3, pst_st_counter);
|
2020-10-02 02:12:26 +00:00
|
|
|
ASSERT_EQ(3, flush_info_log_counter);
|
2020-08-15 03:11:35 +00:00
|
|
|
|
|
|
|
// Disable scheduler with SetOption
|
|
|
|
ASSERT_OK(dbfull()->SetDBOptions(
|
|
|
|
{{"stats_dump_period_sec", "0"}, {"stats_persist_period_sec", "0"}}));
|
|
|
|
ASSERT_EQ(0u, dbfull()->GetDBOptions().stats_dump_period_sec);
|
|
|
|
ASSERT_EQ(0u, dbfull()->GetDBOptions().stats_persist_period_sec);
|
|
|
|
|
2020-10-02 02:12:26 +00:00
|
|
|
// Info log flush should still run.
|
2022-12-12 18:37:55 +00:00
|
|
|
dbfull()->TEST_WaitForPeriodicTaskRun(
|
2021-01-26 06:07:26 +00:00
|
|
|
[&] { mock_clock_->MockSleepForSeconds(static_cast<int>(kPeriodSec)); });
|
2020-10-02 02:12:26 +00:00
|
|
|
ASSERT_EQ(3, dump_st_counter);
|
|
|
|
ASSERT_EQ(3, pst_st_counter);
|
|
|
|
ASSERT_EQ(4, flush_info_log_counter);
|
|
|
|
|
2022-08-26 01:52:37 +00:00
|
|
|
ASSERT_EQ(1u, scheduler.TEST_GetValidTaskNum());
|
2020-08-15 03:11:35 +00:00
|
|
|
|
|
|
|
// Re-enable one task
|
|
|
|
ASSERT_OK(dbfull()->SetDBOptions({{"stats_dump_period_sec", "5"}}));
|
|
|
|
ASSERT_EQ(5u, dbfull()->GetDBOptions().stats_dump_period_sec);
|
|
|
|
ASSERT_EQ(0u, dbfull()->GetDBOptions().stats_persist_period_sec);
|
|
|
|
|
2022-08-26 01:52:37 +00:00
|
|
|
ASSERT_EQ(2, scheduler.TEST_GetValidTaskNum());
|
2020-08-15 03:11:35 +00:00
|
|
|
|
2022-12-12 18:37:55 +00:00
|
|
|
dbfull()->TEST_WaitForPeriodicTaskRun(
|
2021-01-26 06:07:26 +00:00
|
|
|
[&] { mock_clock_->MockSleepForSeconds(static_cast<int>(kPeriodSec)); });
|
2020-10-02 02:12:26 +00:00
|
|
|
ASSERT_EQ(4, dump_st_counter);
|
|
|
|
ASSERT_EQ(3, pst_st_counter);
|
|
|
|
ASSERT_EQ(5, flush_info_log_counter);
|
2020-08-15 03:11:35 +00:00
|
|
|
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
2022-08-26 01:52:37 +00:00
|
|
|
TEST_F(PeriodicTaskSchedulerTest, MultiInstances) {
|
2020-08-15 03:11:35 +00:00
|
|
|
constexpr int kPeriodSec = 5;
|
|
|
|
const int kInstanceNum = 10;
|
|
|
|
|
|
|
|
Close();
|
|
|
|
Options options;
|
|
|
|
options.stats_dump_period_sec = kPeriodSec;
|
|
|
|
options.stats_persist_period_sec = kPeriodSec;
|
|
|
|
options.create_if_missing = true;
|
|
|
|
options.env = mock_env_.get();
|
|
|
|
|
|
|
|
int dump_st_counter = 0;
|
|
|
|
SyncPoint::GetInstance()->SetCallBack("DBImpl::DumpStats:2",
|
|
|
|
[&](void*) { dump_st_counter++; });
|
|
|
|
|
|
|
|
int pst_st_counter = 0;
|
|
|
|
SyncPoint::GetInstance()->SetCallBack("DBImpl::PersistStats:StartRunning",
|
|
|
|
[&](void*) { pst_st_counter++; });
|
|
|
|
SyncPoint::GetInstance()->EnableProcessing();
|
|
|
|
|
|
|
|
auto dbs = std::vector<DB*>(kInstanceNum);
|
|
|
|
for (int i = 0; i < kInstanceNum; i++) {
|
|
|
|
ASSERT_OK(
|
|
|
|
DB::Open(options, test::PerThreadDBPath(std::to_string(i)), &(dbs[i])));
|
|
|
|
}
|
|
|
|
|
|
|
|
auto dbi = static_cast_with_check<DBImpl>(dbs[kInstanceNum - 1]);
|
2022-08-26 01:52:37 +00:00
|
|
|
|
|
|
|
const PeriodicTaskScheduler& scheduler = dbi->TEST_GetPeriodicTaskScheduler();
|
|
|
|
ASSERT_EQ(kInstanceNum * 3, scheduler.TEST_GetValidTaskNum());
|
2020-08-15 03:11:35 +00:00
|
|
|
|
|
|
|
int expected_run = kInstanceNum;
|
2022-12-12 18:37:55 +00:00
|
|
|
dbi->TEST_WaitForPeriodicTaskRun(
|
2021-01-26 06:07:26 +00:00
|
|
|
[&] { mock_clock_->MockSleepForSeconds(kPeriodSec - 1); });
|
2020-08-15 03:11:35 +00:00
|
|
|
ASSERT_EQ(expected_run, dump_st_counter);
|
|
|
|
ASSERT_EQ(expected_run, pst_st_counter);
|
|
|
|
|
|
|
|
expected_run += kInstanceNum;
|
2022-12-12 18:37:55 +00:00
|
|
|
dbi->TEST_WaitForPeriodicTaskRun(
|
2021-01-26 06:07:26 +00:00
|
|
|
[&] { mock_clock_->MockSleepForSeconds(kPeriodSec); });
|
2020-08-15 03:11:35 +00:00
|
|
|
ASSERT_EQ(expected_run, dump_st_counter);
|
|
|
|
ASSERT_EQ(expected_run, pst_st_counter);
|
|
|
|
|
|
|
|
expected_run += kInstanceNum;
|
2022-12-12 18:37:55 +00:00
|
|
|
dbi->TEST_WaitForPeriodicTaskRun(
|
2021-01-26 06:07:26 +00:00
|
|
|
[&] { mock_clock_->MockSleepForSeconds(kPeriodSec); });
|
2020-08-15 03:11:35 +00:00
|
|
|
ASSERT_EQ(expected_run, dump_st_counter);
|
|
|
|
ASSERT_EQ(expected_run, pst_st_counter);
|
|
|
|
|
|
|
|
int half = kInstanceNum / 2;
|
|
|
|
for (int i = 0; i < half; i++) {
|
|
|
|
delete dbs[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
expected_run += (kInstanceNum - half) * 2;
|
|
|
|
|
2022-12-12 18:37:55 +00:00
|
|
|
dbi->TEST_WaitForPeriodicTaskRun(
|
2021-01-26 06:07:26 +00:00
|
|
|
[&] { mock_clock_->MockSleepForSeconds(kPeriodSec); });
|
2022-12-12 18:37:55 +00:00
|
|
|
dbi->TEST_WaitForPeriodicTaskRun(
|
2021-01-26 06:07:26 +00:00
|
|
|
[&] { mock_clock_->MockSleepForSeconds(kPeriodSec); });
|
2020-08-15 03:11:35 +00:00
|
|
|
ASSERT_EQ(expected_run, dump_st_counter);
|
|
|
|
ASSERT_EQ(expected_run, pst_st_counter);
|
|
|
|
|
|
|
|
for (int i = half; i < kInstanceNum; i++) {
|
2020-12-23 07:44:44 +00:00
|
|
|
ASSERT_OK(dbs[i]->Close());
|
2020-08-15 03:11:35 +00:00
|
|
|
delete dbs[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-26 01:52:37 +00:00
|
|
|
TEST_F(PeriodicTaskSchedulerTest, MultiEnv) {
|
2020-08-15 03:11:35 +00:00
|
|
|
constexpr int kDumpPeriodSec = 5;
|
|
|
|
constexpr int kPersistPeriodSec = 10;
|
|
|
|
Close();
|
|
|
|
Options options1;
|
|
|
|
options1.stats_dump_period_sec = kDumpPeriodSec;
|
|
|
|
options1.stats_persist_period_sec = kPersistPeriodSec;
|
|
|
|
options1.create_if_missing = true;
|
|
|
|
options1.env = mock_env_.get();
|
|
|
|
|
|
|
|
Reopen(options1);
|
|
|
|
|
2021-01-26 06:07:26 +00:00
|
|
|
std::unique_ptr<Env> mock_env2(
|
|
|
|
new CompositeEnvWrapper(Env::Default(), mock_clock_));
|
2020-08-15 03:11:35 +00:00
|
|
|
Options options2;
|
|
|
|
options2.stats_dump_period_sec = kDumpPeriodSec;
|
|
|
|
options2.stats_persist_period_sec = kPersistPeriodSec;
|
|
|
|
options2.create_if_missing = true;
|
|
|
|
options1.env = mock_env2.get();
|
|
|
|
|
|
|
|
std::string dbname = test::PerThreadDBPath("multi_env_test");
|
|
|
|
DB* db;
|
|
|
|
ASSERT_OK(DB::Open(options2, dbname, &db));
|
|
|
|
|
2020-12-23 07:44:44 +00:00
|
|
|
ASSERT_OK(db->Close());
|
2020-08-15 03:11:35 +00:00
|
|
|
delete db;
|
|
|
|
Close();
|
|
|
|
}
|
2022-08-26 01:52:37 +00:00
|
|
|
|
2020-08-15 03:11:35 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2022-10-18 07:35:35 +00:00
|
|
|
ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
|
2020-08-15 03:11:35 +00:00
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
|
|
|
|
|
|
return RUN_ALL_TESTS();
|
|
|
|
}
|