12f1137355
Summary: Introduces and uses a SystemClock class to RocksDB. This class contains the time-related functions of an Env and these functions can be redirected from the Env to the SystemClock. Many of the places that used an Env (Timer, PerfStepTimer, RepeatableThread, RateLimiter, WriteController) for time-related functions have been changed to use SystemClock instead. There are likely more places that can be changed, but this is a start to show what can/should be done. Over time it would be nice to migrate most (if not all) of the uses of the time functions from the Env to the SystemClock. There are several Env classes that implement these functions. Most of these have not been converted yet to SystemClock implementations; that will come in a subsequent PR. It would be good to unify many of the Mock Timer implementations, so that they behave similarly and be tested similarly (some override Sleep, some use a MockSleep, etc). Additionally, this change will allow new methods to be introduced to the SystemClock (like https://github.com/facebook/rocksdb/issues/7101 WaitFor) in a consistent manner across a smaller number of classes. Pull Request resolved: https://github.com/facebook/rocksdb/pull/7858 Reviewed By: pdillinger Differential Revision: D26006406 Pulled By: mrambacher fbshipit-source-id: ed10a8abbdab7ff2e23d69d85bd25b3e7e899e90
79 lines
2.7 KiB
C++
79 lines
2.7 KiB
C++
// 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).
|
|
|
|
#pragma once
|
|
|
|
#ifndef ROCKSDB_LITE
|
|
|
|
#include "db/db_impl/db_impl.h"
|
|
#include "util/timer.h"
|
|
|
|
namespace ROCKSDB_NAMESPACE {
|
|
class SystemClock;
|
|
|
|
// PeriodicWorkScheduler is a singleton object, which is scheduling/running
|
|
// DumpStats(), PersistStats(), and FlushInfoLog() for all DB instances. All DB
|
|
// instances use the same object from `Default()`.
|
|
//
|
|
// Internally, it uses a single threaded timer object to run the periodic work
|
|
// functions. Timer thread will always be started since the info log flushing
|
|
// cannot be disabled.
|
|
class PeriodicWorkScheduler {
|
|
public:
|
|
static PeriodicWorkScheduler* Default();
|
|
|
|
PeriodicWorkScheduler() = delete;
|
|
PeriodicWorkScheduler(const PeriodicWorkScheduler&) = delete;
|
|
PeriodicWorkScheduler(PeriodicWorkScheduler&&) = delete;
|
|
PeriodicWorkScheduler& operator=(const PeriodicWorkScheduler&) = delete;
|
|
PeriodicWorkScheduler& operator=(PeriodicWorkScheduler&&) = delete;
|
|
|
|
void Register(DBImpl* dbi, unsigned int stats_dump_period_sec,
|
|
unsigned int stats_persist_period_sec);
|
|
|
|
void Unregister(DBImpl* dbi);
|
|
|
|
// Periodically flush info log out of application buffer at a low frequency.
|
|
// This improves debuggability in case of RocksDB hanging since it ensures the
|
|
// log messages leading up to the hang will eventually become visible in the
|
|
// log.
|
|
static const uint64_t kDefaultFlushInfoLogPeriodSec = 10;
|
|
|
|
protected:
|
|
std::unique_ptr<Timer> timer;
|
|
// `timer_mu_` serves two purposes currently:
|
|
// (1) to ensure calls to `Start()` and `Shutdown()` are serialized, as
|
|
// they are currently not implemented in a thread-safe way; and
|
|
// (2) to ensure the `Timer::Add()`s and `Timer::Start()` run atomically, and
|
|
// the `Timer::Cancel()`s and `Timer::Shutdown()` run atomically.
|
|
port::Mutex timer_mu_;
|
|
|
|
explicit PeriodicWorkScheduler(const std::shared_ptr<SystemClock>& clock);
|
|
|
|
private:
|
|
std::string GetTaskName(DBImpl* dbi, const std::string& func_name);
|
|
};
|
|
|
|
#ifndef NDEBUG
|
|
// PeriodicWorkTestScheduler is for unittest, which can specify the SystemClock
|
|
// It also contains functions for unittest.
|
|
class PeriodicWorkTestScheduler : public PeriodicWorkScheduler {
|
|
public:
|
|
static PeriodicWorkTestScheduler* Default(
|
|
const std::shared_ptr<SystemClock>& clock);
|
|
|
|
void TEST_WaitForRun(std::function<void()> callback) const;
|
|
|
|
size_t TEST_GetValidTaskNum() const;
|
|
|
|
private:
|
|
explicit PeriodicWorkTestScheduler(const std::shared_ptr<SystemClock>& clock);
|
|
};
|
|
#endif // !NDEBUG
|
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
|
|
|
#endif // ROCKSDB_LITE
|