summaryrefslogtreecommitdiff
path: root/src/mongo/db/index_builds_coordinator.cpp
diff options
context:
space:
mode:
authorJordi Serra Torrens <jordi.serra-torrens@mongodb.com>2022-10-26 07:48:24 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2022-10-26 08:42:45 +0000
commit1ddc20571ad31d622045d80049c4e7879a780a14 (patch)
treed59c11cbcb41d0c961311b7e317478a42936c280 /src/mongo/db/index_builds_coordinator.cpp
parentc554c500fde9838b0125d4965b758a4b0b9af158 (diff)
downloadmongo-1ddc20571ad31d622045d80049c4e7879a780a14.tar.gz
SERVER-69435 Make the CSS acquisition a RAII
Diffstat (limited to 'src/mongo/db/index_builds_coordinator.cpp')
-rw-r--r--src/mongo/db/index_builds_coordinator.cpp52
1 files changed, 29 insertions, 23 deletions
diff --git a/src/mongo/db/index_builds_coordinator.cpp b/src/mongo/db/index_builds_coordinator.cpp
index 6da862c5d00..84852c7c9fb 100644
--- a/src/mongo/db/index_builds_coordinator.cpp
+++ b/src/mongo/db/index_builds_coordinator.cpp
@@ -116,7 +116,8 @@ void checkShardKeyRestrictions(OperationContext* opCtx,
const BSONObj& newIdxKey) {
CollectionCatalog::get(opCtx)->invariantHasExclusiveAccessToCollection(opCtx, nss);
- const auto collDesc = CollectionShardingState::get(opCtx, nss)->getCollectionDescription(opCtx);
+ const auto collDesc = CollectionShardingState::assertCollectionLockedAndAcquire(opCtx, nss)
+ ->getCollectionDescription(opCtx);
if (!collDesc.isSharded())
return;
@@ -1916,26 +1917,28 @@ IndexBuildsCoordinator::_filterSpecsAndRegisterBuild(OperationContext* opCtx,
AutoGetCollection autoColl(opCtx, nssOrUuid, MODE_X);
CollectionWriter collection(opCtx, autoColl);
- const auto& ns = collection.get()->ns();
- auto css = CollectionShardingState::get(opCtx, ns);
+ const auto& nss = collection.get()->ns();
- // Disallow index builds on drop-pending namespaces (system.drop.*) if we are primary.
- auto replCoord = repl::ReplicationCoordinator::get(opCtx);
- if (replCoord->getSettings().usingReplSets() &&
- replCoord->canAcceptWritesFor(opCtx, nssOrUuid)) {
- uassert(ErrorCodes::NamespaceNotFound,
- str::stream() << "drop-pending collection: " << ns,
- !ns.isDropPendingNamespace());
- }
+ {
+ // Disallow index builds on drop-pending namespaces (system.drop.*) if we are primary.
+ auto replCoord = repl::ReplicationCoordinator::get(opCtx);
+ if (replCoord->getSettings().usingReplSets() &&
+ replCoord->canAcceptWritesFor(opCtx, nssOrUuid)) {
+ uassert(ErrorCodes::NamespaceNotFound,
+ str::stream() << "drop-pending collection: " << nss,
+ !nss.isDropPendingNamespace());
+ }
- // This check is for optimization purposes only as since this lock is released after this,
- // and is acquired again when we build the index in _setUpIndexBuild.
- css->checkShardVersionOrThrow(opCtx);
- css->getCollectionDescription(opCtx).throwIfReshardingInProgress(ns);
+ // This check is for optimization purposes only as since this lock is released after this,
+ // and is acquired again when we build the index in _setUpIndexBuild.
+ auto scopedCss = CollectionShardingState::assertCollectionLockedAndAcquire(opCtx, nss);
+ scopedCss->checkShardVersionOrThrow(opCtx);
+ scopedCss->getCollectionDescription(opCtx).throwIfReshardingInProgress(nss);
+ }
std::vector<BSONObj> filteredSpecs;
try {
- filteredSpecs = prepareSpecListForCreate(opCtx, collection.get(), ns, specs);
+ filteredSpecs = prepareSpecListForCreate(opCtx, collection.get(), nss, specs);
} catch (const DBException& ex) {
return ex.toStatus();
}
@@ -1962,7 +1965,7 @@ IndexBuildsCoordinator::_filterSpecsAndRegisterBuild(OperationContext* opCtx,
// the catalog update when it uses the timestamp from the startIndexBuild, rather than
// the commitIndexBuild, oplog entry.
writeConflictRetry(
- opCtx, "IndexBuildsCoordinator::_filterSpecsAndRegisterBuild", ns.ns(), [&] {
+ opCtx, "IndexBuildsCoordinator::_filterSpecsAndRegisterBuild", nss.ns(), [&] {
WriteUnitOfWork wuow(opCtx);
createIndexesOnEmptyCollection(opCtx, collection, filteredSpecs, false);
wuow.commit();
@@ -2001,13 +2004,17 @@ IndexBuildsCoordinator::PostSetupAction IndexBuildsCoordinator::_setUpIndexBuild
AutoGetCollection coll(opCtx, nssOrUuid, MODE_X);
CollectionWriter collection(opCtx, coll);
- CollectionShardingState::get(opCtx, collection->ns())->checkShardVersionOrThrow(opCtx);
+
+ const auto& nss = collection.get()->ns();
+
+ CollectionShardingState::assertCollectionLockedAndAcquire(opCtx, nss)
+ ->checkShardVersionOrThrow(opCtx);
// We will not have a start timestamp if we are newly a secondary (i.e. we started as
// primary but there was a stepdown). We will be unable to timestamp the initial catalog write,
// so we must fail the index build. During initial sync, there is no commit timestamp set.
auto replCoord = repl::ReplicationCoordinator::get(opCtx);
- if (!replCoord->canAcceptWritesFor(opCtx, collection->ns()) &&
+ if (!replCoord->canAcceptWritesFor(opCtx, nss) &&
indexBuildOptions.applicationMode != ApplicationMode::kInitialSync) {
uassert(ErrorCodes::NotWritablePrimary,
str::stream() << "Replication state changed while setting up the index build: "
@@ -2022,7 +2029,7 @@ IndexBuildsCoordinator::PostSetupAction IndexBuildsCoordinator::_setUpIndexBuild
// writes a no-op just to generate an optime.
onInitFn = [&](std::vector<BSONObj>& specs) {
if (!(replCoord->getSettings().usingReplSets() &&
- replCoord->canAcceptWritesFor(opCtx, collection->ns()))) {
+ replCoord->canAcceptWritesFor(opCtx, nss))) {
// Not primary.
return Status::OK();
}
@@ -2052,7 +2059,7 @@ IndexBuildsCoordinator::PostSetupAction IndexBuildsCoordinator::_setUpIndexBuild
opCtx->getServiceContext()->getOpObserver()->onStartIndexBuild(
opCtx,
- collection->ns(),
+ nss,
replState->collectionUUID,
replState->buildUUID,
replState->indexSpecs,
@@ -2066,8 +2073,7 @@ IndexBuildsCoordinator::PostSetupAction IndexBuildsCoordinator::_setUpIndexBuild
IndexBuildsManager::SetupOptions options;
options.indexConstraints =
- repl::ReplicationCoordinator::get(opCtx)->shouldRelaxIndexConstraints(opCtx,
- collection->ns())
+ repl::ReplicationCoordinator::get(opCtx)->shouldRelaxIndexConstraints(opCtx, nss)
? IndexBuildsManager::IndexConstraints::kRelax
: IndexBuildsManager::IndexConstraints::kEnforce;
options.protocol = replState->protocol;