]>
Commit | Line | Data |
---|---|---|
20effc67 TL |
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). | |
5 | ||
6 | #pragma once | |
7 | ||
8 | #ifndef ROCKSDB_LITE | |
9 | ||
10 | #include "db/db_impl/db_impl.h" | |
11 | #include "util/timer.h" | |
12 | ||
13 | namespace ROCKSDB_NAMESPACE { | |
14 | ||
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()`. | |
18 | // | |
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 { | |
23 | public: | |
24 | static PeriodicWorkScheduler* Default(); | |
25 | ||
26 | PeriodicWorkScheduler() = delete; | |
27 | PeriodicWorkScheduler(const PeriodicWorkScheduler&) = delete; | |
28 | PeriodicWorkScheduler(PeriodicWorkScheduler&&) = delete; | |
29 | PeriodicWorkScheduler& operator=(const PeriodicWorkScheduler&) = delete; | |
30 | PeriodicWorkScheduler& operator=(PeriodicWorkScheduler&&) = delete; | |
31 | ||
32 | void Register(DBImpl* dbi, unsigned int stats_dump_period_sec, | |
33 | unsigned int stats_persist_period_sec); | |
34 | ||
35 | void Unregister(DBImpl* dbi); | |
36 | ||
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 | |
40 | // log. | |
41 | static const uint64_t kDefaultFlushInfoLogPeriodSec = 10; | |
42 | ||
43 | protected: | |
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_; | |
51 | ||
52 | explicit PeriodicWorkScheduler(Env* env); | |
53 | ||
54 | private: | |
55 | std::string GetTaskName(DBImpl* dbi, const std::string& func_name); | |
56 | }; | |
57 | ||
58 | #ifndef NDEBUG | |
59 | // PeriodicWorkTestScheduler is for unittest, which can specify the Env like | |
60 | // SafeMockTimeEnv. It also contains functions for unittest. | |
61 | class PeriodicWorkTestScheduler : public PeriodicWorkScheduler { | |
62 | public: | |
63 | static PeriodicWorkTestScheduler* Default(Env* env); | |
64 | ||
65 | void TEST_WaitForRun(std::function<void()> callback) const; | |
66 | ||
67 | size_t TEST_GetValidTaskNum() const; | |
68 | ||
69 | private: | |
70 | explicit PeriodicWorkTestScheduler(Env* env); | |
71 | }; | |
72 | #endif // !NDEBUG | |
73 | ||
74 | } // namespace ROCKSDB_NAMESPACE | |
75 | ||
76 | #endif // ROCKSDB_LITE |