periodic_task_scheduler.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright (c) Meta Platforms, Inc. and affiliates.
  2. //
  3. // This source code is licensed under both the GPLv2 (found in the
  4. // COPYING file in the root directory) and Apache 2.0 License
  5. // (found in the LICENSE.Apache file in the root directory).
  6. #pragma once
  7. #include "util/timer.h"
  8. namespace ROCKSDB_NAMESPACE {
  9. class SystemClock;
  10. using PeriodicTaskFunc = std::function<void()>;
  11. constexpr uint64_t kInvalidPeriodSec = 0;
  12. // List of task types
  13. enum class PeriodicTaskType : uint8_t {
  14. kDumpStats = 0,
  15. kPersistStats,
  16. kFlushInfoLog,
  17. kRecordSeqnoTime,
  18. kTriggerCompaction,
  19. kMax,
  20. };
  21. // PeriodicTaskScheduler contains the periodic task scheduled from the DB
  22. // instance. It's used to schedule/unschedule DumpStats(), PersistStats(),
  23. // FlushInfoLog(), etc. Each type of the task can only have one instance,
  24. // re-register the same task type would only update the repeat period.
  25. //
  26. // Internally, it uses a global single threaded timer object to run the periodic
  27. // task functions. Timer thread will always be started since the info log
  28. // flushing cannot be disabled.
  29. class PeriodicTaskScheduler {
  30. public:
  31. explicit PeriodicTaskScheduler() = default;
  32. PeriodicTaskScheduler(const PeriodicTaskScheduler&) = delete;
  33. PeriodicTaskScheduler(PeriodicTaskScheduler&&) = delete;
  34. PeriodicTaskScheduler& operator=(const PeriodicTaskScheduler&) = delete;
  35. PeriodicTaskScheduler& operator=(PeriodicTaskScheduler&&) = delete;
  36. // Register a task with its default repeat period. Thread safe call.
  37. // @param run_immediately If true, the task will run soon after it's
  38. // scheduled, instead of waiting for the repeat period.
  39. Status Register(PeriodicTaskType task_type, const PeriodicTaskFunc& fn,
  40. bool run_immediately);
  41. // Register a task with specified repeat period. 0 is an invalid argument
  42. // (kInvalidPeriodSec). To stop the task, please use Unregister().
  43. // Thread safe call.
  44. Status Register(PeriodicTaskType task_type, const PeriodicTaskFunc& fn,
  45. uint64_t repeat_period_seconds, bool run_immediately);
  46. // Unregister the task. Thread safe call.
  47. Status Unregister(PeriodicTaskType task_type);
  48. #ifndef NDEBUG
  49. // Override the timer for the unittest
  50. void TEST_OverrideTimer(SystemClock* clock);
  51. // Call Timer TEST_WaitForRun() which wait until Timer starting waiting.
  52. void TEST_WaitForRun(const std::function<void()>& callback) const {
  53. if (timer_ != nullptr) {
  54. timer_->TEST_WaitForRun(callback);
  55. }
  56. }
  57. // Get global valid task number in the Timer
  58. size_t TEST_GetValidTaskNum() const {
  59. if (timer_ != nullptr) {
  60. return timer_->TEST_GetPendingTaskNum();
  61. }
  62. return 0;
  63. }
  64. // If it has the specified task type registered
  65. bool TEST_HasTask(PeriodicTaskType task_type) const {
  66. auto it = tasks_map_.find(task_type);
  67. return it != tasks_map_.end();
  68. }
  69. #endif // NDEBUG
  70. private:
  71. // default global Timer instance
  72. static Timer* Default();
  73. // Internal structure to store task information
  74. struct TaskInfo {
  75. TaskInfo(std::string _name, uint64_t _repeat_every_sec)
  76. : name(std::move(_name)), repeat_every_sec(_repeat_every_sec) {}
  77. std::string name;
  78. uint64_t repeat_every_sec;
  79. };
  80. // Internal tasks map
  81. std::map<PeriodicTaskType, TaskInfo> tasks_map_;
  82. // Global timer pointer, which doesn't support synchronous add/cancel tasks
  83. // so having a global `timer_mutex` for add/cancel task.
  84. Timer* timer_ = Default();
  85. // Global task id, protected by the global `timer_mutex`
  86. inline static uint64_t id_;
  87. static constexpr uint64_t kMicrosInSecond = 1000U * 1000U;
  88. };
  89. } // namespace ROCKSDB_NAMESPACE