fork of https://github.com/oxigraph/rocksdb and https://github.com/facebook/rocksdb for nextgraph and oxigraph
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
109 lines
3.7 KiB
109 lines
3.7 KiB
4 years ago
|
// 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).
|
||
|
|
||
4 years ago
|
#include "db/periodic_work_scheduler.h"
|
||
4 years ago
|
|
||
|
#include "db/db_impl/db_impl.h"
|
||
|
|
||
|
#ifndef ROCKSDB_LITE
|
||
|
namespace ROCKSDB_NAMESPACE {
|
||
|
|
||
4 years ago
|
PeriodicWorkScheduler::PeriodicWorkScheduler(Env* env) {
|
||
4 years ago
|
timer = std::unique_ptr<Timer>(new Timer(env));
|
||
|
}
|
||
|
|
||
4 years ago
|
void PeriodicWorkScheduler::Register(DBImpl* dbi,
|
||
|
unsigned int stats_dump_period_sec,
|
||
|
unsigned int stats_persist_period_sec) {
|
||
4 years ago
|
static std::atomic<uint64_t> initial_delay(0);
|
||
4 years ago
|
timer->Start();
|
||
4 years ago
|
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);
|
||
|
}
|
||
4 years ago
|
timer->Add([dbi]() { dbi->FlushInfoLog(); },
|
||
|
GetTaskName(dbi, "flush_info_log"),
|
||
|
initial_delay.fetch_add(1) % kDefaultFlushInfoLogPeriodSec *
|
||
|
kMicrosInSecond,
|
||
|
kDefaultFlushInfoLogPeriodSec * kMicrosInSecond);
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
void PeriodicWorkScheduler::Unregister(DBImpl* dbi) {
|
||
4 years ago
|
timer->Cancel(GetTaskName(dbi, "dump_st"));
|
||
|
timer->Cancel(GetTaskName(dbi, "pst_st"));
|
||
4 years ago
|
timer->Cancel(GetTaskName(dbi, "flush_info_log"));
|
||
4 years ago
|
if (!timer->HasPendingTask()) {
|
||
|
timer->Shutdown();
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
PeriodicWorkScheduler* PeriodicWorkScheduler::Default() {
|
||
4 years ago
|
// Always use the default Env for the scheduler, as we only use the NowMicros
|
||
|
// which is the same for all env.
|
||
|
// The Env could only be overridden in test.
|
||
4 years ago
|
static PeriodicWorkScheduler scheduler(Env::Default());
|
||
4 years ago
|
return &scheduler;
|
||
|
}
|
||
|
|
||
4 years ago
|
std::string PeriodicWorkScheduler::GetTaskName(DBImpl* dbi,
|
||
|
const std::string& func_name) {
|
||
4 years ago
|
std::string db_session_id;
|
||
4 years ago
|
// TODO: Should this error be ignored?
|
||
|
dbi->GetDbSessionId(db_session_id).PermitUncheckedError();
|
||
4 years ago
|
return db_session_id + ":" + func_name;
|
||
|
}
|
||
|
|
||
|
#ifndef NDEBUG
|
||
|
|
||
|
// Get the static scheduler. For a new env, 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 MockEnv,
|
||
|
// Close all db instances and then re-open them.
|
||
4 years ago
|
PeriodicWorkTestScheduler* PeriodicWorkTestScheduler::Default(Env* env) {
|
||
|
static PeriodicWorkTestScheduler scheduler(env);
|
||
4 years ago
|
static port::Mutex mutex;
|
||
|
{
|
||
|
MutexLock l(&mutex);
|
||
|
if (scheduler.timer.get() != nullptr &&
|
||
|
scheduler.timer->TEST_GetPendingTaskNum() == 0) {
|
||
|
scheduler.timer->Shutdown();
|
||
|
scheduler.timer.reset(new Timer(env));
|
||
|
}
|
||
|
}
|
||
|
return &scheduler;
|
||
|
}
|
||
|
|
||
4 years ago
|
void PeriodicWorkTestScheduler::TEST_WaitForRun(
|
||
4 years ago
|
std::function<void()> callback) const {
|
||
|
if (timer != nullptr) {
|
||
|
timer->TEST_WaitForRun(callback);
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
size_t PeriodicWorkTestScheduler::TEST_GetValidTaskNum() const {
|
||
4 years ago
|
if (timer != nullptr) {
|
||
|
return timer->TEST_GetPendingTaskNum();
|
||
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
4 years ago
|
PeriodicWorkTestScheduler::PeriodicWorkTestScheduler(Env* env)
|
||
|
: PeriodicWorkScheduler(env) {}
|
||
4 years ago
|
|
||
|
#endif // !NDEBUG
|
||
|
} // namespace ROCKSDB_NAMESPACE
|
||
|
|
||
|
#endif // ROCKSDB_LITE
|