From c1808dd194575ea7a49893e782df50fe29c00bd1 Mon Sep 17 00:00:00 2001 From: Gregory Noma Date: Tue, 8 Sep 2020 13:22:22 -0400 Subject: SERVER-50711 Clear temp directory if no internal idents are parsed on startup --- jstests/noPassthrough/libs/index_build.js | 34 +++++++++++++++++++++++-------- src/mongo/db/mongod_main.cpp | 7 ------- src/mongo/db/startup_recovery.cpp | 14 +++++++++++++ 3 files changed, 40 insertions(+), 15 deletions(-) diff --git a/jstests/noPassthrough/libs/index_build.js b/jstests/noPassthrough/libs/index_build.js index 28f0b91fba0..09ca4b01b1c 100644 --- a/jstests/noPassthrough/libs/index_build.js +++ b/jstests/noPassthrough/libs/index_build.js @@ -617,18 +617,36 @@ const ResumableIndexBuildTest = class { ResumableIndexBuildTest.checkIndexes( rst, dbName, collName, indexName, postIndexBuildInserts); - if (!failWhileParsing) { - // Ensure that the persisted Sorter data was cleaned up after failing to resume. This - // cleanup does not occur if parsing failed. - const files = listFiles(primary.dbpath + "/_tmp"); + const checkLogIdAfterRestart = function(primary, id) { + rst.stop(primary); + rst.start(primary, {noCleanData: true}); + checkLog.containsJson(primary, id); + }; + + const checkTempDirectoryCleared = function(primary) { + const tempDir = primary.dbpath + "/_tmp"; + + // If the index build was interrupted for shutdown before anything was inserted into + // the Sorter, the temp directory may not exist. + if (!fileExists(tempDir)) + return; + + // Ensure that the persisted Sorter data was cleaned up after failing to resume. + const files = listFiles(tempDir); assert.eq(files.length, 0, files); + }; + + if (failWhileParsing) { + // If we fail while parsing, the persisted Sorter data will only be cleaned up after + // another restart. + checkLogIdAfterRestart(primary, 5071100); + checkTempDirectoryCleared(primary); + } else { + checkTempDirectoryCleared(primary); // If we fail after parsing, any remaining internal idents will only be cleaned up // after another restart. - clearRawMongoProgramOutput(); - rst.stop(primary); - rst.start(primary, {noCleanData: true}); - assert(RegExp("22257.*").test(rawMongoProgramOutput())); + checkLogIdAfterRestart(primary, 22257); } } }; diff --git a/src/mongo/db/mongod_main.cpp b/src/mongo/db/mongod_main.cpp index c04117cfae3..d654bb7e881 100644 --- a/src/mongo/db/mongod_main.cpp +++ b/src/mongo/db/mongod_main.cpp @@ -445,13 +445,6 @@ ExitCode _initAndListen(ServiceContext* serviceContext, int listenPort) { startWatchdog(serviceContext); - // When starting up after an unclean shutdown, we do not attempt to use any of the temporary - // files left from the previous run. Thus, we remove them in this case. - if (!storageGlobalParams.readOnly && - LastStorageEngineShutdownState::kUnclean == lastStorageEngineShutdownState) { - boost::filesystem::remove_all(storageGlobalParams.dbpath + "/_tmp/"); - } - if (mongodGlobalParams.scriptingEnabled) { ScriptEngine::setup(); } diff --git a/src/mongo/db/startup_recovery.cpp b/src/mongo/db/startup_recovery.cpp index 309ffa96180..6beb7f8fa56 100644 --- a/src/mongo/db/startup_recovery.cpp +++ b/src/mongo/db/startup_recovery.cpp @@ -347,6 +347,20 @@ void reconcileCatalogAndRebuildUnfinishedIndexes( auto reconcileResult = fassert(40593, storageEngine->reconcileCatalogAndIdents(opCtx, reconcilePolicy)); + // If we did not find any index builds to resume or we are starting up after an unclean + // shutdown, nothing in the temp directory will be used. Thus, we can clear it. + if (reconcileResult.indexBuildsToResume.empty() || + lastStorageEngineShutdownState == LastStorageEngineShutdownState::kUnclean) { + LOGV2(5071100, "Clearing temp directory"); + + boost::system::error_code ec; + boost::filesystem::remove_all(storageGlobalParams.dbpath + "/_tmp/", ec); + + if (ec) { + LOGV2(5071101, "Failed to clear temp directory", "error"_attr = ec.message()); + } + } + // Determine which indexes need to be rebuilt. rebuildIndexesOnCollection() requires that all // indexes on that collection are done at once, so we use a map to group them together. StringMap nsToIndexNameObjMap; -- cgit v1.2.1