diff options
author | Kaloian Manassiev <kaloian.manassiev@mongodb.com> | 2023-03-25 10:56:45 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2023-03-30 12:06:58 +0000 |
commit | c528a0ea57d731bad38fd70cebff9c7c06ea2518 (patch) | |
tree | 466083410a2e21a6e324fdcc8a48b50ae5de3ae8 | |
parent | fcd8862523226aba53062faed279c329d7f07ad5 (diff) | |
download | mongo-c528a0ea57d731bad38fd70cebff9c7c06ea2518.tar.gz |
SERVER-73756 Use ScopedAcquisition in the drop indexes command
-rw-r--r-- | src/mongo/db/commands/drop_indexes_cmd.cpp | 41 | ||||
-rw-r--r-- | src/mongo/db/s/shardsvr_drop_indexes_command.cpp | 1 | ||||
-rw-r--r-- | src/mongo/s/commands/cluster_drop_indexes_cmd.cpp | 4 |
3 files changed, 22 insertions, 24 deletions
diff --git a/src/mongo/db/commands/drop_indexes_cmd.cpp b/src/mongo/db/commands/drop_indexes_cmd.cpp index ac10fbfd954..216ab956580 100644 --- a/src/mongo/db/commands/drop_indexes_cmd.cpp +++ b/src/mongo/db/commands/drop_indexes_cmd.cpp @@ -27,9 +27,6 @@ * it in the license file. */ - -#include "mongo/platform/basic.h" - #include <string> #include <vector> @@ -45,12 +42,12 @@ #include "mongo/db/commands.h" #include "mongo/db/concurrency/exception_util.h" #include "mongo/db/curop.h" -#include "mongo/db/db_raii.h" #include "mongo/db/drop_indexes_gen.h" #include "mongo/db/index/index_descriptor.h" #include "mongo/db/index_builds_coordinator.h" #include "mongo/db/op_observer/op_observer.h" #include "mongo/db/service_context.h" +#include "mongo/db/shard_role.h" #include "mongo/db/timeseries/catalog_helper.h" #include "mongo/db/timeseries/timeseries_commands_conversion_helper.h" #include "mongo/db/vector_clock.h" @@ -61,9 +58,7 @@ #define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kCommand - namespace mongo { - namespace { MONGO_FAIL_POINT_DEFINE(reIndexCrashAfterDrop); @@ -173,17 +168,21 @@ public: << toReIndexNss << "' while replication is active"); } - AutoGetCollection autoColl(opCtx, toReIndexNss, MODE_X); - if (!autoColl) { - if (CollectionCatalog::get(opCtx)->lookupView(opCtx, toReIndexNss)) - uasserted(ErrorCodes::CommandNotSupportedOnView, "can't re-index a view"); - else - uasserted(ErrorCodes::NamespaceNotFound, "collection does not exist"); - } + auto acquisition = [&] { + auto collOrViewAcquisition = acquireCollectionOrView( + opCtx, + CollectionOrViewAcquisitionRequest::fromOpCtx( + opCtx, toReIndexNss, AcquisitionPrerequisites::OperationType::kWrite), + MODE_X); + uassert(ErrorCodes::CommandNotSupportedOnView, + "can't re-index a view", + !std::holds_alternative<ScopedViewAcquisition>(collOrViewAcquisition)); + return std::move(std::get<ScopedCollectionAcquisition>(collOrViewAcquisition)); + }(); + uassert(ErrorCodes::NamespaceNotFound, "collection does not exist", acquisition.exists()); - CollectionWriter collection(opCtx, autoColl); IndexBuildsCoordinator::get(opCtx)->assertNoIndexBuildInProgForCollection( - collection->uuid()); + acquisition.uuid()); // This is necessary to set up CurOp and update the Top stats. OldClientContext ctx(opCtx, toReIndexNss); @@ -195,7 +194,7 @@ public: std::vector<std::string> indexNames; writeConflictRetry(opCtx, "listIndexes", toReIndexNss.ns(), [&] { indexNames.clear(); - collection->getAllIndexes(&indexNames); + acquisition.getCollectionPtr()->getAllIndexes(&indexNames); }); all.reserve(indexNames.size()); @@ -203,7 +202,7 @@ public: for (size_t i = 0; i < indexNames.size(); i++) { const std::string& name = indexNames[i]; BSONObj spec = writeConflictRetry(opCtx, "getIndexSpec", toReIndexNss.ns(), [&] { - return collection->getIndexSpec(name); + return acquisition.getCollectionPtr()->getIndexSpec(name); }); { @@ -244,6 +243,7 @@ public: "Uninitialized"); writeConflictRetry(opCtx, "dropAllIndexes", toReIndexNss.ns(), [&] { WriteUnitOfWork wunit(opCtx); + CollectionWriter collection(opCtx, &acquisition); collection.getWritableCollection(opCtx)->getIndexCatalog()->dropAllIndexes( opCtx, collection.getWritableCollection(opCtx), true, {}); @@ -258,6 +258,7 @@ public: // The 'indexer' can throw, so ensure build cleanup occurs. ScopeGuard abortOnExit([&] { + CollectionWriter collection(opCtx, &acquisition); indexer->abortIndexBuild(opCtx, collection, MultiIndexBlock::kNoopOnCleanUpFn); }); @@ -268,12 +269,14 @@ public: // The following function performs its own WriteConflict handling, so don't wrap it in a // writeConflictRetry loop. - uassertStatusOK(indexer->insertAllDocumentsInCollection(opCtx, collection.get())); + uassertStatusOK( + indexer->insertAllDocumentsInCollection(opCtx, acquisition.getCollectionPtr())); - uassertStatusOK(indexer->checkConstraints(opCtx, collection.get())); + uassertStatusOK(indexer->checkConstraints(opCtx, acquisition.getCollectionPtr())); writeConflictRetry(opCtx, "commitReIndex", toReIndexNss.ns(), [&] { WriteUnitOfWork wunit(opCtx); + CollectionWriter collection(opCtx, &acquisition); uassertStatusOK(indexer->commit(opCtx, collection.getWritableCollection(opCtx), MultiIndexBlock::kNoopOnCreateEachFn, diff --git a/src/mongo/db/s/shardsvr_drop_indexes_command.cpp b/src/mongo/db/s/shardsvr_drop_indexes_command.cpp index 1cdcd57d561..a50ace9a5ae 100644 --- a/src/mongo/db/s/shardsvr_drop_indexes_command.cpp +++ b/src/mongo/db/s/shardsvr_drop_indexes_command.cpp @@ -45,7 +45,6 @@ #define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kSharding - namespace mongo { namespace { diff --git a/src/mongo/s/commands/cluster_drop_indexes_cmd.cpp b/src/mongo/s/commands/cluster_drop_indexes_cmd.cpp index db92433afab..c92236ec973 100644 --- a/src/mongo/s/commands/cluster_drop_indexes_cmd.cpp +++ b/src/mongo/s/commands/cluster_drop_indexes_cmd.cpp @@ -27,9 +27,6 @@ * it in the license file. */ - -#include "mongo/platform/basic.h" - #include "mongo/db/auth/authorization_session.h" #include "mongo/db/commands.h" #include "mongo/logv2/log.h" @@ -40,7 +37,6 @@ #define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kCommand - namespace mongo { namespace { |