diff options
author | David Storch <david.storch@mongodb.com> | 2020-02-21 09:49:30 -0500 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2020-02-25 02:48:51 +0000 |
commit | bc4caba144ed5a422609a8400e1ba8cb5a88810a (patch) | |
tree | 5e29cf6eb20d3b8a2b85206080413a1df087ecf6 /src | |
parent | 0aac1805c04aa5b1481ba99dcab2273d423df10c (diff) | |
download | mongo-bc4caba144ed5a422609a8400e1ba8cb5a88810a.tar.gz |
SERVER-46196 Fix creation of orphan Top entries on failed collection creation
Diffstat (limited to 'src')
-rw-r--r-- | src/mongo/db/catalog/create_collection.cpp | 38 |
1 files changed, 26 insertions, 12 deletions
diff --git a/src/mongo/db/catalog/create_collection.cpp b/src/mongo/db/catalog/create_collection.cpp index 68d73151f69..ebb298b22e4 100644 --- a/src/mongo/db/catalog/create_collection.cpp +++ b/src/mongo/db/catalog/create_collection.cpp @@ -68,12 +68,6 @@ Status _createView(OperationContext* opCtx, Database* db = autoDb.getDb(); - AutoStatsTracker statsTracker(opCtx, - nss, - Top::LockType::NotLocked, - AutoStatsTracker::LogMode::kUpdateTopAndCurOp, - db->getProfilingLevel()); - if (opCtx->writesAreReplicated() && !repl::ReplicationCoordinator::get(opCtx)->canAcceptWritesFor(opCtx, nss)) { return Status(ErrorCodes::NotMaster, @@ -91,6 +85,19 @@ Status _createView(OperationContext* opCtx, wuow.commit(); WriteUnitOfWork wunit(opCtx); + + AutoStatsTracker statsTracker(opCtx, + nss, + Top::LockType::NotLocked, + AutoStatsTracker::LogMode::kUpdateTopAndCurOp, + db->getProfilingLevel()); + + // If the view creation rolls back, ensure that the Top entry created for the view is + // deleted. + opCtx->recoveryUnit()->onRollback([nss, serviceContext = opCtx->getServiceContext()]() { + Top::get(serviceContext).collectionDropped(nss); + }); + Status status = db->userCreateNS(opCtx, nss, collectionOptions, true, idIndex); if (!status.isOK()) { return status; @@ -120,12 +127,6 @@ Status _createCollection(OperationContext* opCtx, str::stream() << "A view already exists. NS: " << nss); } - AutoStatsTracker statsTracker(opCtx, - nss, - Top::LockType::NotLocked, - AutoStatsTracker::LogMode::kUpdateTopAndCurOp, - autoDb.getDb()->getProfilingLevel()); - if (opCtx->writesAreReplicated() && !repl::ReplicationCoordinator::get(opCtx)->canAcceptWritesFor(opCtx, nss)) { return Status(ErrorCodes::NotMaster, @@ -133,6 +134,19 @@ Status _createCollection(OperationContext* opCtx, } WriteUnitOfWork wunit(opCtx); + + AutoStatsTracker statsTracker(opCtx, + nss, + Top::LockType::NotLocked, + AutoStatsTracker::LogMode::kUpdateTopAndCurOp, + autoDb.getDb()->getProfilingLevel()); + + // If the collection creation rolls back, ensure that the Top entry created for the + // collection is deleted. + opCtx->recoveryUnit()->onRollback([nss, serviceContext = opCtx->getServiceContext()]() { + Top::get(serviceContext).collectionDropped(nss); + }); + Status status = autoDb.getDb()->userCreateNS(opCtx, nss, collectionOptions, true, idIndex); if (!status.isOK()) { return status; |