1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
10 #include "db/db_impl/db_impl.h"
11 #include "util/timer.h"
13 namespace ROCKSDB_NAMESPACE
{
15 // PeriodicWorkScheduler is a singleton object, which is scheduling/running
16 // DumpStats(), PersistStats(), and FlushInfoLog() for all DB instances. All DB
17 // instances use the same object from `Default()`.
19 // Internally, it uses a single threaded timer object to run the periodic work
20 // functions. Timer thread will always be started since the info log flushing
21 // cannot be disabled.
22 class PeriodicWorkScheduler
{
24 static PeriodicWorkScheduler
* Default();
26 PeriodicWorkScheduler() = delete;
27 PeriodicWorkScheduler(const PeriodicWorkScheduler
&) = delete;
28 PeriodicWorkScheduler(PeriodicWorkScheduler
&&) = delete;
29 PeriodicWorkScheduler
& operator=(const PeriodicWorkScheduler
&) = delete;
30 PeriodicWorkScheduler
& operator=(PeriodicWorkScheduler
&&) = delete;
32 void Register(DBImpl
* dbi
, unsigned int stats_dump_period_sec
,
33 unsigned int stats_persist_period_sec
);
35 void Unregister(DBImpl
* dbi
);
37 // Periodically flush info log out of application buffer at a low frequency.
38 // This improves debuggability in case of RocksDB hanging since it ensures the
39 // log messages leading up to the hang will eventually become visible in the
41 static const uint64_t kDefaultFlushInfoLogPeriodSec
= 10;
44 std::unique_ptr
<Timer
> timer
;
45 // `timer_mu_` serves two purposes currently:
46 // (1) to ensure calls to `Start()` and `Shutdown()` are serialized, as
47 // they are currently not implemented in a thread-safe way; and
48 // (2) to ensure the `Timer::Add()`s and `Timer::Start()` run atomically, and
49 // the `Timer::Cancel()`s and `Timer::Shutdown()` run atomically.
50 port::Mutex timer_mu_
;
52 explicit PeriodicWorkScheduler(Env
* env
);
55 std::string
GetTaskName(DBImpl
* dbi
, const std::string
& func_name
);
59 // PeriodicWorkTestScheduler is for unittest, which can specify the Env like
60 // SafeMockTimeEnv. It also contains functions for unittest.
61 class PeriodicWorkTestScheduler
: public PeriodicWorkScheduler
{
63 static PeriodicWorkTestScheduler
* Default(Env
* env
);
65 void TEST_WaitForRun(std::function
<void()> callback
) const;
67 size_t TEST_GetValidTaskNum() const;
70 explicit PeriodicWorkTestScheduler(Env
* env
);
74 } // namespace ROCKSDB_NAMESPACE
76 #endif // ROCKSDB_LITE