Skip to content

Commit 342d72a

Browse files
benetyevergreen
authored andcommitted
SERVER-39002 IndexBuildsCoordinator checks replication state before writing abortIndexBuild oplog entry
1 parent e3c434d commit 342d72a

File tree

1 file changed

+33
-16
lines changed

1 file changed

+33
-16
lines changed

src/mongo/db/index_builds_coordinator.cpp

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,31 @@ void checkShardKeyRestrictions(OperationContext* opCtx,
9292
shardKeyPattern.isUniqueIndexCompatible(newIdxKey));
9393
}
9494

95+
/**
96+
* Signal downstream secondary nodes to abort index build.
97+
*/
98+
void onAbortIndexBuild(OperationContext* opCtx,
99+
const NamespaceString& nss,
100+
const ReplIndexBuildState& replState,
101+
const Status& cause) {
102+
if (!serverGlobalParams.featureCompatibility.isVersionInitialized()) {
103+
return;
104+
}
105+
106+
if (serverGlobalParams.featureCompatibility.getVersion() !=
107+
ServerGlobalParams::FeatureCompatibility::Version::kFullyUpgradedTo44) {
108+
return;
109+
}
110+
111+
invariant(opCtx->lockState()->isWriteLocked(), replState.buildUUID.toString());
112+
113+
auto opObserver = opCtx->getServiceContext()->getOpObserver();
114+
auto collUUID = replState.collectionUUID;
115+
auto fromMigrate = false;
116+
opObserver->onAbortIndexBuild(
117+
opCtx, nss, collUUID, replState.buildUUID, replState.indexSpecs, cause, fromMigrate);
118+
}
119+
95120
/**
96121
* Aborts the index build identified by the provided 'replIndexBuildState'.
97122
*
@@ -934,24 +959,16 @@ void IndexBuildsCoordinator::_runIndexBuildInner(OperationContext* opCtx,
934959
<< "; Database: " << replState->dbName));
935960
}
936961

937-
// Signal downstream secondary nodes to abort index build.
938-
if (serverGlobalParams.featureCompatibility.isVersionInitialized() &&
939-
serverGlobalParams.featureCompatibility.getVersion() ==
940-
ServerGlobalParams::FeatureCompatibility::Version::kFullyUpgradedTo44) {
941-
UninterruptibleLockGuard noInterrupt(opCtx->lockState());
942-
Lock::GlobalLock lock(opCtx, MODE_IX);
943-
auto collUUID = replState->collectionUUID;
944-
auto fromMigrate = false;
962+
UninterruptibleLockGuard noInterrupt(opCtx->lockState());
963+
Lock::GlobalLock lock(opCtx, MODE_IX);
964+
965+
auto replCoord = repl::ReplicationCoordinator::get(opCtx);
966+
if (replCoord->getSettings().usingReplSets() && replCoord->canAcceptWritesFor(opCtx, nss)) {
945967
writeConflictRetry(
946968
opCtx, "onAbortIndexBuild", NamespaceString::kRsOplogNamespace.ns(), [&] {
947-
opCtx->getServiceContext()->getOpObserver()->onAbortIndexBuild(
948-
opCtx,
949-
nss,
950-
collUUID,
951-
replState->buildUUID,
952-
replState->indexSpecs,
953-
status,
954-
fromMigrate);
969+
WriteUnitOfWork wuow(opCtx);
970+
onAbortIndexBuild(opCtx, nss, *replState, status);
971+
wuow.commit();
955972
});
956973
}
957974

0 commit comments

Comments
 (0)