diff options
author | Haley Connelly <haley.connelly@mongodb.com> | 2022-05-16 13:52:15 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2022-05-16 14:32:07 +0000 |
commit | d245f52771cbb42734f9e541e3fc839cd1854496 (patch) | |
tree | 2b837e0576c164fe552c7b0f3676cccaca6f28dc /src/mongo/db/ttl.h | |
parent | 04402176f1ae8471396905b419ebb3d1b7d1c7a9 (diff) | |
download | mongo-d245f52771cbb42734f9e541e3fc839cd1854496.tar.gz |
SERVER-66387 Declare TTLMonitor in ttl.h
Diffstat (limited to 'src/mongo/db/ttl.h')
-rw-r--r-- | src/mongo/db/ttl.h | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/src/mongo/db/ttl.h b/src/mongo/db/ttl.h index d22b29b486c..59d476671d2 100644 --- a/src/mongo/db/ttl.h +++ b/src/mongo/db/ttl.h @@ -29,6 +29,9 @@ #pragma once +#include "mongo/db/ttl_collection_cache.h" +#include "mongo/util/background.h" + namespace mongo { class ServiceContext; @@ -44,4 +47,69 @@ void startTTLMonitor(ServiceContext* serviceContext); */ void shutdownTTLMonitor(ServiceContext* serviceContext); +class TTLMonitor : public BackgroundJob { +public: + explicit TTLMonitor() : BackgroundJob(false /* selfDelete */) {} + + static TTLMonitor* get(ServiceContext* serviceCtx); + + static void set(ServiceContext* serviceCtx, std::unique_ptr<TTLMonitor> monitor); + + std::string name() const { + return "TTLMonitor"; + } + + void run(); + + /** + * Signals the thread to quit and then waits until it does. + */ + void shutdown(); + +private: + /** + * Gets all TTL specifications for every collection and deletes expired documents. + */ + void _doTTLPass(); + + /** + * Uses the TTL 'info' to determine which documents are expired and removes them from the + * collection when applicble. In some cases (i.e: on temporary resharding collections, + * collections pending to be dropped, etc), the TTLMonitor is prohibitied from removing + * documents and the method silently returns. + */ + void _deleteExpired(OperationContext* opCtx, + TTLCollectionCache* ttlCollectionCache, + const UUID& uuid, + const NamespaceString& nss, + const TTLCollectionCache::Info& info); + + /** + * Removes documents from the collection using the specified TTL index after a sufficient + * amount of time has passed according to its expiry specification. + */ + void _deleteExpiredWithIndex(OperationContext* opCtx, + TTLCollectionCache* ttlCollectionCache, + const CollectionPtr& collection, + std::string indexName); + + /* + * Removes expired documents from a clustered collection using a bounded collection scan. + * On time-series buckets collections, TTL operates on type 'ObjectId'. On general purpose + * collections, TTL operates on type 'Date'. + */ + void _deleteExpiredWithCollscan(OperationContext* opCtx, + TTLCollectionCache* ttlCollectionCache, + const CollectionPtr& collection); + + // 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; + + bool _shuttingDown = false; +}; + } // namespace mongo |