diff options
author | Xiangyu Yao <xiangyu.yao@mongodb.com> | 2019-09-16 17:26:26 +0000 |
---|---|---|
committer | evergreen <evergreen@mongodb.com> | 2019-09-16 17:26:26 +0000 |
commit | 734306ecd8345b55b2758f9c34055b2e62bff015 (patch) | |
tree | 41ec8fe0fdb49c16b7a5b964f4a7ee369a736873 /src | |
parent | 392af48b71dfb690a6cc1d119fdc40b61925098b (diff) | |
download | mongo-734306ecd8345b55b2758f9c34055b2e62bff015.tar.gz |
SERVER-42441 renameCollectionForApplyOps should always rename the target out of the way if it exists
(cherry picked from commit e5fd4679b3ff5857140db7b644375756f2b7db82)
Diffstat (limited to 'src')
-rw-r--r-- | src/mongo/db/catalog/rename_collection.cpp | 19 |
1 files changed, 11 insertions, 8 deletions
diff --git a/src/mongo/db/catalog/rename_collection.cpp b/src/mongo/db/catalog/rename_collection.cpp index 0073084cb44..4e323041687 100644 --- a/src/mongo/db/catalog/rename_collection.cpp +++ b/src/mongo/db/catalog/rename_collection.cpp @@ -116,7 +116,8 @@ Status renameCollectionCommon(OperationContext* opCtx, const NamespaceString& target, OptionalCollectionUUID targetUUID, repl::OpTime renameOpTimeFromApplyOps, - const RenameCollectionOptions& options) { + const RenameCollectionOptions& options, + bool forApplyOps) { auto uuidString = targetUUID ? targetUUID->toString() : "no UUID"; log() << "renameCollection: renaming collection " << uuidString << " from " << source << " to " << target; @@ -205,14 +206,15 @@ Status renameCollectionCommon(OperationContext* opCtx, return {ErrorCodes::IllegalOperation, "cannot rename to a sharded collection"}; } - if (!options.dropTarget) { + // RenameCollectionForCommand cannot drop target by renaming. + if (!forApplyOps && !options.dropTarget) { return Status(ErrorCodes::NamespaceExists, "target namespace exists"); } // If UUID doesn't point to the existing target, we should rename the target rather than // drop it. - if (options.dropTargetUUID && options.dropTargetUUID != targetColl->uuid()) { - auto dropTargetNssFromUUID = getNamespaceFromUUID(opCtx, options.dropTargetUUID.get()); + if (!options.dropTarget || + (options.dropTargetUUID && options.dropTargetUUID != targetColl->uuid())) { // We need to rename the targetColl to a temporary name. auto status = renameTargetCollectionToTmp( opCtx, source, targetUUID.get(), targetDB, target, targetColl->uuid().get()); @@ -506,7 +508,7 @@ Status renameCollectionCommon(OperationContext* opCtx, // in-place rename and remove the source collection. invariant(tmpName.db() == target.db()); status = renameCollectionCommon( - opCtx, tmpName, target, targetUUID, renameOpTimeFromApplyOps, options); + opCtx, tmpName, target, targetUUID, renameOpTimeFromApplyOps, options, false); if (!status.isOK()) { return status; } @@ -534,7 +536,7 @@ Status renameCollection(OperationContext* opCtx, } OptionalCollectionUUID noTargetUUID; - return renameCollectionCommon(opCtx, source, target, noTargetUUID, {}, options); + return renameCollectionCommon(opCtx, source, target, noTargetUUID, {}, options, false); } @@ -612,7 +614,8 @@ Status renameCollectionForApplyOps(OperationContext* opCtx, } options.stayTemp = cmd["stayTemp"].trueValue(); - return renameCollectionCommon(opCtx, sourceNss, targetNss, targetUUID, renameOpTime, options); + return renameCollectionCommon( + opCtx, sourceNss, targetNss, targetUUID, renameOpTime, options, true); } Status renameCollectionForRollback(OperationContext* opCtx, @@ -625,7 +628,7 @@ Status renameCollectionForRollback(OperationContext* opCtx, RenameCollectionOptions options; invariant(!options.dropTarget); - return renameCollectionCommon(opCtx, source, target, uuid, {}, options); + return renameCollectionCommon(opCtx, source, target, uuid, {}, options, false); } } // namespace mongo |