2020-08-15 03:11:35 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
|
|
|
// 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).
|
|
|
|
|
2020-10-02 02:12:26 +00:00
|
|
|
#include "db/periodic_work_scheduler.h"
|
2020-08-15 03:11:35 +00:00
|
|
|
|
|
|
|
#include "db/db_impl/db_impl.h"
|
2021-01-26 06:07:26 +00:00
|
|
|
#include "rocksdb/system_clock.h"
|
2020-08-15 03:11:35 +00:00
|
|
|
|
|
|
|
#ifndef ROCKSDB_LITE
|
|
|
|
namespace ROCKSDB_NAMESPACE {
|
|
|
|
|
2021-01-26 06:07:26 +00:00
|
|
|
PeriodicWorkScheduler::PeriodicWorkScheduler(
|
|
|
|
const std::shared_ptr<SystemClock>& clock) {
|
2021-03-15 11:32:24 +00:00
|
|
|
timer = std::unique_ptr<Timer>(new Timer(clock.get()));
|
2020-08-15 03:11:35 +00:00
|
|
|
}
|
|
|
|
|
2020-10-02 02:12:26 +00:00
|
|
|
void PeriodicWorkScheduler::Register(DBImpl* dbi,
|
|
|
|
unsigned int stats_dump_period_sec,
|
|
|
|
unsigned int stats_persist_period_sec) {
|
2021-01-21 16:47:06 +00:00
|
|
|
MutexLock l(&timer_mu_);
|
2020-08-15 03:11:35 +00:00
|
|
|
static std::atomic<uint64_t> initial_delay(0);
|
2020-10-02 02:12:26 +00:00
|
|
|
timer->Start();
|
2020-08-15 03:11:35 +00:00
|
|
|
if (stats_dump_period_sec > 0) {
|
|
|
|
timer->Add([dbi]() { dbi->DumpStats(); }, GetTaskName(dbi, "dump_st"),
|
|
|
|
initial_delay.fetch_add(1) %
|
|
|
|
static_cast<uint64_t>(stats_dump_period_sec) *
|
|
|
|
kMicrosInSecond,
|
|
|
|
static_cast<uint64_t>(stats_dump_period_sec) * kMicrosInSecond);
|
|
|
|
}
|
|
|
|
if (stats_persist_period_sec > 0) {
|
|
|
|
timer->Add(
|
|
|
|
[dbi]() { dbi->PersistStats(); }, GetTaskName(dbi, "pst_st"),
|
|
|
|
initial_delay.fetch_add(1) %
|
|
|
|
static_cast<uint64_t>(stats_persist_period_sec) * kMicrosInSecond,
|
|
|
|
static_cast<uint64_t>(stats_persist_period_sec) * kMicrosInSecond);
|
|
|
|
}
|
2020-10-02 02:12:26 +00:00
|
|
|
timer->Add([dbi]() { dbi->FlushInfoLog(); },
|
|
|
|
GetTaskName(dbi, "flush_info_log"),
|
|
|
|
initial_delay.fetch_add(1) % kDefaultFlushInfoLogPeriodSec *
|
|
|
|
kMicrosInSecond,
|
|
|
|
kDefaultFlushInfoLogPeriodSec * kMicrosInSecond);
|
2020-08-15 03:11:35 +00:00
|
|
|
}
|
|
|
|
|
2020-10-02 02:12:26 +00:00
|
|
|
void PeriodicWorkScheduler::Unregister(DBImpl* dbi) {
|
2021-01-21 16:47:06 +00:00
|
|
|
MutexLock l(&timer_mu_);
|
2020-08-15 03:11:35 +00:00
|
|
|
timer->Cancel(GetTaskName(dbi, "dump_st"));
|
|
|
|
timer->Cancel(GetTaskName(dbi, "pst_st"));
|
2020-10-02 02:12:26 +00:00
|
|
|
timer->Cancel(GetTaskName(dbi, "flush_info_log"));
|
2020-08-15 03:11:35 +00:00
|
|
|
if (!timer->HasPendingTask()) {
|
|
|
|
timer->Shutdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-02 02:12:26 +00:00
|
|
|
PeriodicWorkScheduler* PeriodicWorkScheduler::Default() {
|
2021-01-26 06:07:26 +00:00
|
|
|
// Always use the default SystemClock for the scheduler, as we only use the
|
|
|
|
// NowMicros which is the same for all clocks. The Env could only be
|
|
|
|
// overridden in test.
|
|
|
|
static PeriodicWorkScheduler scheduler(SystemClock::Default());
|
2020-08-15 03:11:35 +00:00
|
|
|
return &scheduler;
|
|
|
|
}
|
|
|
|
|
2020-10-02 02:12:26 +00:00
|
|
|
std::string PeriodicWorkScheduler::GetTaskName(DBImpl* dbi,
|
|
|
|
const std::string& func_name) {
|
2020-08-15 03:11:35 +00:00
|
|
|
std::string db_session_id;
|
2020-08-21 02:16:56 +00:00
|
|
|
// TODO: Should this error be ignored?
|
|
|
|
dbi->GetDbSessionId(db_session_id).PermitUncheckedError();
|
2020-08-15 03:11:35 +00:00
|
|
|
return db_session_id + ":" + func_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
|
2021-01-26 06:07:26 +00:00
|
|
|
// Get the static scheduler. For a new SystemClock, it needs to re-create the
|
|
|
|
// internal timer, so only re-create it when there's no running task. Otherwise,
|
|
|
|
// return the existing scheduler. Which means if the unittest needs to update
|
|
|
|
// MockClock, Close all db instances and then re-open them.
|
|
|
|
PeriodicWorkTestScheduler* PeriodicWorkTestScheduler::Default(
|
|
|
|
const std::shared_ptr<SystemClock>& clock) {
|
|
|
|
static PeriodicWorkTestScheduler scheduler(clock);
|
2020-08-15 03:11:35 +00:00
|
|
|
static port::Mutex mutex;
|
|
|
|
{
|
|
|
|
MutexLock l(&mutex);
|
|
|
|
if (scheduler.timer.get() != nullptr &&
|
|
|
|
scheduler.timer->TEST_GetPendingTaskNum() == 0) {
|
2021-01-21 16:47:06 +00:00
|
|
|
{
|
|
|
|
MutexLock timer_mu_guard(&scheduler.timer_mu_);
|
|
|
|
scheduler.timer->Shutdown();
|
|
|
|
}
|
2021-03-15 11:32:24 +00:00
|
|
|
scheduler.timer.reset(new Timer(clock.get()));
|
2020-08-15 03:11:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return &scheduler;
|
|
|
|
}
|
|
|
|
|
2020-10-02 02:12:26 +00:00
|
|
|
void PeriodicWorkTestScheduler::TEST_WaitForRun(
|
2020-08-15 03:11:35 +00:00
|
|
|
std::function<void()> callback) const {
|
|
|
|
if (timer != nullptr) {
|
|
|
|
timer->TEST_WaitForRun(callback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-02 02:12:26 +00:00
|
|
|
size_t PeriodicWorkTestScheduler::TEST_GetValidTaskNum() const {
|
2020-08-15 03:11:35 +00:00
|
|
|
if (timer != nullptr) {
|
|
|
|
return timer->TEST_GetPendingTaskNum();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-01-26 06:07:26 +00:00
|
|
|
PeriodicWorkTestScheduler::PeriodicWorkTestScheduler(
|
|
|
|
const std::shared_ptr<SystemClock>& clock)
|
|
|
|
: PeriodicWorkScheduler(clock) {}
|
2020-08-15 03:11:35 +00:00
|
|
|
|
|
|
|
#endif // !NDEBUG
|
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
|
|
|
|
|
|
|
#endif // ROCKSDB_LITE
|