From 970c93880474dd811a42dd4e08c899db9c67a973 Mon Sep 17 00:00:00 2001 From: Gregory Noma Date: Tue, 4 Jan 2022 21:51:24 +0000 Subject: SERVER-62173 Wait for no index builds to be in progress on BackgroundOperationInProgressForNamespace during steady state replication with FCV less than 5.2 (cherry picked from commit f7ab9b6b8825cb09ce92ed4797b0ee75a49b5e22) --- src/mongo/db/repl/oplog.cpp | 53 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 8 deletions(-) diff --git a/src/mongo/db/repl/oplog.cpp b/src/mongo/db/repl/oplog.cpp index a1c0a273f1f..5454baf2abd 100644 --- a/src/mongo/db/repl/oplog.cpp +++ b/src/mongo/db/repl/oplog.cpp @@ -1927,14 +1927,51 @@ Status applyCommand_inlock(OperationContext* opCtx, auto ns = cmd->parse(opCtx, OpMsgRequest::fromDBAndBody(nss.db(), o))->ns(); - invariant(mode == OplogApplication::Mode::kInitialSync); - abortIndexBuilds( - opCtx, entry.getCommandType(), ns, "Aborting index builds during initial sync"); - LOGV2_DEBUG(4665901, - 1, - "Conflicting DDL operation encountered during initial sync; " - "aborting index build and retrying", - "namespace"_attr = ns); + // TODO (SERVER-61481): Once kLastLTS is 6.0, this error will only be possible in + // mode kInitialSync. + if (mode == OplogApplication::Mode::kInitialSync) { + abortIndexBuilds(opCtx, + entry.getCommandType(), + ns, + "Aborting index builds during initial sync"); + LOGV2_DEBUG(4665901, + 1, + "Conflicting DDL operation encountered during initial sync; " + "aborting index build and retrying", + logAttrs(ns)); + } else { + auto lockState = opCtx->lockState(); + Locker::LockSnapshot lockSnapshot; + auto locksReleased = lockState->saveLockStateAndUnlock(&lockSnapshot); + + ScopeGuard guard{[&] { + if (locksReleased) { + invariant(!lockState->isLocked()); + lockState->restoreLockState(lockSnapshot); + } + }}; + + auto swUUID = entry.getUuid(); + if (!swUUID) { + LOGV2_ERROR(21261, + "Failed command during oplog application. Expected a UUID", + "command"_attr = redact(o), + logAttrs(ns)); + } + IndexBuildsCoordinator::get(opCtx)->awaitNoIndexBuildInProgressForCollection( + opCtx, swUUID.get()); + + opCtx->recoveryUnit()->abandonSnapshot(); + opCtx->checkForInterrupt(); + + LOGV2_DEBUG( + 51775, + 1, + "Acceptable error during oplog application: background operation in " + "progress for namespace", + logAttrs(ns), + "oplogEntry"_attr = redact(entry.toBSONForLogging())); + } break; } -- cgit v1.2.1