summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJordi Olivares Provencio <jordi.olivares-provencio@mongodb.com>2023-04-03 08:39:38 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2023-04-03 09:47:38 +0000
commit0d9fbf5838d712c94abd9ee9810444e8d0f8dbcc (patch)
tree723dc62c89e2a49aa3ec239eac03d4724ca204d6
parentaf10b407ed0ddcc50395e046a296551ef3ba8bb2 (diff)
downloadmongo-0d9fbf5838d712c94abd9ee9810444e8d0f8dbcc.tar.gz
SERVER-74427 Refactor OplogCapMaintainerThread lifetime
-rw-r--r--src/mongo/db/SConscript1
-rw-r--r--src/mongo/db/mongod_main.cpp6
-rw-r--r--src/mongo/db/repl/storage_interface_impl.cpp4
-rw-r--r--src/mongo/db/storage/oplog_cap_maintainer_thread.cpp17
-rw-r--r--src/mongo/db/storage/oplog_cap_maintainer_thread.h9
5 files changed, 33 insertions, 4 deletions
diff --git a/src/mongo/db/SConscript b/src/mongo/db/SConscript
index 79a75d2abc5..a783e17f555 100644
--- a/src/mongo/db/SConscript
+++ b/src/mongo/db/SConscript
@@ -2407,6 +2407,7 @@ env.Library(
'storage/disk_space_monitor',
'storage/flow_control',
'storage/flow_control_parameters',
+ 'storage/oplog_cap_maintainer_thread',
'storage/storage_control',
'storage/storage_engine_common',
'system_index',
diff --git a/src/mongo/db/mongod_main.cpp b/src/mongo/db/mongod_main.cpp
index ad6d15979ca..76a1be90001 100644
--- a/src/mongo/db/mongod_main.cpp
+++ b/src/mongo/db/mongod_main.cpp
@@ -177,6 +177,7 @@
#include "mongo/db/storage/encryption_hooks.h"
#include "mongo/db/storage/flow_control.h"
#include "mongo/db/storage/flow_control_parameters_gen.h"
+#include "mongo/db/storage/oplog_cap_maintainer_thread.h"
#include "mongo/db/storage/storage_engine.h"
#include "mongo/db/storage/storage_engine_init.h"
#include "mongo/db/storage/storage_engine_lock_file.h"
@@ -1582,6 +1583,11 @@ void shutdownTask(const ShutdownTaskArgs& shutdownArgs) {
shutdownGlobalStorageEngineCleanly(serviceContext);
}
+ // We wait for the oplog cap maintainer thread to stop. This has to be done after the engine has
+ // been closed since the thread will only die once all references to the oplog have been deleted
+ // and we're performing a shutdown.
+ OplogCapMaintainerThread::get(serviceContext)->waitForFinish();
+
// We drop the scope cache because leak sanitizer can't see across the
// thread we use for proxying MozJS requests. Dropping the cache cleans up
// the memory and makes leak sanitizer happy.
diff --git a/src/mongo/db/repl/storage_interface_impl.cpp b/src/mongo/db/repl/storage_interface_impl.cpp
index cd4696b6b73..e5da512cafd 100644
--- a/src/mongo/db/repl/storage_interface_impl.cpp
+++ b/src/mongo/db/repl/storage_interface_impl.cpp
@@ -1414,8 +1414,8 @@ void StorageInterfaceImpl::initializeStorageControlsForReplication(
// periodically execute deletion via oplog truncate markers. OplogTruncateMarkers are a
// replacement for capped collection deletion of the oplog collection history.
if (serviceCtx->getStorageEngine()->supportsOplogTruncateMarkers()) {
- BackgroundJob* backgroundThread = new OplogCapMaintainerThread();
- backgroundThread->go();
+ auto maintainerThread = OplogCapMaintainerThread::get(serviceCtx);
+ maintainerThread->go();
}
}
diff --git a/src/mongo/db/storage/oplog_cap_maintainer_thread.cpp b/src/mongo/db/storage/oplog_cap_maintainer_thread.cpp
index 337d8230721..2388800265d 100644
--- a/src/mongo/db/storage/oplog_cap_maintainer_thread.cpp
+++ b/src/mongo/db/storage/oplog_cap_maintainer_thread.cpp
@@ -50,10 +50,16 @@ namespace mongo {
namespace {
+const auto getMaintainerThread = ServiceContext::declareDecoration<OplogCapMaintainerThread>();
+
MONGO_FAIL_POINT_DEFINE(hangOplogCapMaintainerThread);
} // namespace
+OplogCapMaintainerThread* OplogCapMaintainerThread::get(ServiceContext* serviceCtx) {
+ return &getMaintainerThread(serviceCtx);
+}
+
bool OplogCapMaintainerThread::_deleteExcessDocuments() {
if (!getGlobalServiceContext()->getStorageEngine()) {
LOGV2_DEBUG(22240, 2, "OplogCapMaintainerThread: no global storage engine yet");
@@ -113,9 +119,18 @@ void OplogCapMaintainerThread::run() {
hangOplogCapMaintainerThread.pauseWhileSet();
}
- if (!_deleteExcessDocuments()) {
+ if (!_deleteExcessDocuments() && !globalInShutdownDeprecated()) {
sleepmillis(1000); // Back off in case there were problems deleting.
}
}
}
+
+void OplogCapMaintainerThread::waitForFinish() {
+ if (running()) {
+ LOGV2_INFO(7474902, "Shutting down oplog cap maintainer thread");
+ wait();
+ LOGV2(7474901, "Finished shutting down oplog cap maintainer thread");
+ }
+}
+
} // namespace mongo
diff --git a/src/mongo/db/storage/oplog_cap_maintainer_thread.h b/src/mongo/db/storage/oplog_cap_maintainer_thread.h
index 1392c3ac4c6..e322a1e6a9e 100644
--- a/src/mongo/db/storage/oplog_cap_maintainer_thread.h
+++ b/src/mongo/db/storage/oplog_cap_maintainer_thread.h
@@ -41,7 +41,9 @@ namespace mongo {
*/
class OplogCapMaintainerThread : public BackgroundJob {
public:
- OplogCapMaintainerThread() : BackgroundJob(true /* deleteSelf */) {}
+ OplogCapMaintainerThread() : BackgroundJob(false /* deleteSelf */) {}
+
+ static OplogCapMaintainerThread* get(ServiceContext* serviceCtx);
virtual std::string name() const {
return _name;
@@ -49,6 +51,11 @@ public:
virtual void run();
+ /**
+ * Waits until the maintainer thread finishes.
+ */
+ void waitForFinish();
+
private:
/**
* Returns true iff there was an oplog to delete from.