diff options
author | Jordi Olivares Provencio <jordi.olivares-provencio@mongodb.com> | 2023-03-14 13:20:41 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2023-03-14 14:04:10 +0000 |
commit | 10c54d1573b37072427d056180f26b98b50af335 (patch) | |
tree | c92e792a836972f7176d672f746d00b648eaf818 /src/mongo/db | |
parent | 13671a30c1a7889293d85184966256139f0889c6 (diff) | |
download | mongo-10c54d1573b37072427d056180f26b98b50af335.tar.gz |
SERVER-70055 Make TTL monitor pick up config changes immediately
Diffstat (limited to 'src/mongo/db')
-rw-r--r-- | src/mongo/db/ttl.cpp | 43 | ||||
-rw-r--r-- | src/mongo/db/ttl.h | 16 | ||||
-rw-r--r-- | src/mongo/db/ttl.idl | 4 |
3 files changed, 53 insertions, 10 deletions
diff --git a/src/mongo/db/ttl.cpp b/src/mongo/db/ttl.cpp index c2720153e7d..34e1bc23cc9 100644 --- a/src/mongo/db/ttl.cpp +++ b/src/mongo/db/ttl.cpp @@ -308,6 +308,10 @@ CounterMetric ttlCollSubpassesIncreasedPriority("ttl.collSubpassesIncreasedPrior using MtabType = TenantMigrationAccessBlocker::BlockerType; +TTLMonitor::TTLMonitor() + : BackgroundJob(false /* selfDelete */), + _ttlMonitorSleepSecs(Seconds{ttlMonitorSleepSecs.load()}) {} + TTLMonitor* TTLMonitor::get(ServiceContext* serviceCtx) { return getTTLMonitor(serviceCtx).get(); } @@ -323,19 +327,48 @@ void TTLMonitor::set(ServiceContext* serviceCtx, std::unique_ptr<TTLMonitor> mon ttlMonitor = std::move(monitor); } +Status TTLMonitor::onUpdateTTLMonitorSleepSeconds(int newSleepSeconds) { + if (auto client = Client::getCurrent()) { + if (auto ttlMonitor = TTLMonitor::get(client->getServiceContext())) { + ttlMonitor->updateSleepSeconds(Seconds{newSleepSeconds}); + } + } + return Status::OK(); +} + +void TTLMonitor::updateSleepSeconds(Seconds newSeconds) { + { + stdx::lock_guard lk(_stateMutex); + _ttlMonitorSleepSecs = newSeconds; + } + _notificationCV.notify_all(); +} + void TTLMonitor::run() { ThreadClient tc(name(), getGlobalServiceContext()); AuthorizationSession::get(cc())->grantInternalAuthorization(&cc()); while (true) { { - // Wait until either ttlMonitorSleepSecs passes or a shutdown is requested. - auto deadline = Date_t::now() + Seconds(ttlMonitorSleepSecs.load()); + auto startTime = Date_t::now(); + // Wait until either ttlMonitorSleepSecs passes, a shutdown is requested, or the + // sleeping time has changed. stdx::unique_lock<Latch> lk(_stateMutex); + auto deadline = startTime + _ttlMonitorSleepSecs; MONGO_IDLE_THREAD_BLOCK; - _shuttingDownCV.wait_until( - lk, deadline.toSystemTimePoint(), [&] { return _shuttingDown; }); + while (Date_t::now() <= deadline && !_shuttingDown) { + _notificationCV.wait_until(lk, deadline.toSystemTimePoint()); + // Recompute the deadline in case the sleep time has changed since we started. + auto newDeadline = startTime + _ttlMonitorSleepSecs; + if (deadline != newDeadline) { + LOGV2_INFO(7005501, + "TTL sleep deadline has changed", + "oldDeadline"_attr = deadline, + "newDeadline"_attr = newDeadline); + deadline = newDeadline; + } + } if (_shuttingDown) { return; @@ -374,7 +407,7 @@ void TTLMonitor::shutdown() { { stdx::lock_guard<Latch> lk(_stateMutex); _shuttingDown = true; - _shuttingDownCV.notify_one(); + _notificationCV.notify_all(); } wait(); LOGV2(3684101, "Finished shutting down TTL collection monitor thread"); diff --git a/src/mongo/db/ttl.h b/src/mongo/db/ttl.h index be0cc3145ab..6aa0f137ea3 100644 --- a/src/mongo/db/ttl.h +++ b/src/mongo/db/ttl.h @@ -49,12 +49,14 @@ void shutdownTTLMonitor(ServiceContext* serviceContext); class TTLMonitor : public BackgroundJob { public: - explicit TTLMonitor() : BackgroundJob(false /* selfDelete */) {} + TTLMonitor(); static TTLMonitor* get(ServiceContext* serviceCtx); static void set(ServiceContext* serviceCtx, std::unique_ptr<TTLMonitor> monitor); + static Status onUpdateTTLMonitorSleepSeconds(int newSleepSeconds); + std::string name() const { return "TTLMonitor"; } @@ -71,6 +73,8 @@ public: */ void onStepUp(OperationContext* opCtx); + void updateSleepSeconds(Seconds newSeconds); + long long getTTLPasses_forTest(); long long getTTLSubPasses_forTest(); @@ -154,11 +158,15 @@ private: // Protects the state below. mutable Mutex _stateMutex = MONGO_MAKE_LATCH("TTLMonitorStateMutex"); - // Signaled to wake up the thread, if the thread is waiting. The thread will check whether - // _shuttingDown is set and stop accordingly. - mutable stdx::condition_variable _shuttingDownCV; + // Signaled to wake up the thread, if the thread is waiting. This condition variable is used to + // notify the thread of either: + // * The server is shutting down. + // * The ttlMonitorSleepSecs variable has changed. + // If the server is shutting down the monitor will stop. + mutable stdx::condition_variable _notificationCV; bool _shuttingDown = false; + Seconds _ttlMonitorSleepSecs; }; } // namespace mongo diff --git a/src/mongo/db/ttl.idl b/src/mongo/db/ttl.idl index bfc96c06909..7c399b18127 100644 --- a/src/mongo/db/ttl.idl +++ b/src/mongo/db/ttl.idl @@ -27,7 +27,8 @@ global: cpp_namespace: mongo - + cpp_includes: + - "mongo/db/ttl.h" server_parameters: ttlMonitorEnabled: description: "Enable the TTL monitor." @@ -41,6 +42,7 @@ server_parameters: set_at: [ startup, runtime ] cpp_vartype: AtomicWord<int> cpp_varname: ttlMonitorSleepSecs + on_update: "TTLMonitor::onUpdateTTLMonitorSleepSeconds" default: 60 validator: gt: 0 |