From 7818bdcb10d520cb0f6685973c707ed5f292325a Mon Sep 17 00:00:00 2001 From: Benety Goh Date: Wed, 6 Apr 2022 06:11:55 -0400 Subject: SERVER-65137 LookupCollectionForYieldRestore saves collection namespace at construction (cherry picked from commit 729774b7c096ef0a3a6158c5394245937b883667) --- src/mongo/db/catalog/collection_catalog.cpp | 11 ++++++----- src/mongo/db/catalog/collection_catalog.h | 4 ++++ src/mongo/db/catalog_raii.cpp | 7 +++++-- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/mongo/db/catalog/collection_catalog.cpp b/src/mongo/db/catalog/collection_catalog.cpp index 7901809f2ca..ff0fc056e75 100644 --- a/src/mongo/db/catalog/collection_catalog.cpp +++ b/src/mongo/db/catalog/collection_catalog.cpp @@ -346,7 +346,8 @@ CollectionCatalog::iterator::value_type CollectionCatalog::iterator::operator*() return CollectionPtr(); } - return {_opCtx, _mapIter->second.get(), LookupCollectionForYieldRestore()}; + return { + _opCtx, _mapIter->second.get(), LookupCollectionForYieldRestore(_mapIter->second->ns())}; } Collection* CollectionCatalog::iterator::getWritableCollection(OperationContext* opCtx, @@ -672,12 +673,12 @@ CollectionPtr CollectionCatalog::lookupCollectionByUUID(OperationContext* opCtx, } if (auto coll = UncommittedCollections::getForTxn(opCtx, uuid)) { - return {opCtx, coll.get(), LookupCollectionForYieldRestore()}; + return {opCtx, coll.get(), LookupCollectionForYieldRestore(coll->ns())}; } auto coll = _lookupCollectionByUUID(uuid); return (coll && coll->isCommitted()) - ? CollectionPtr(opCtx, coll.get(), LookupCollectionForYieldRestore()) + ? CollectionPtr(opCtx, coll.get(), LookupCollectionForYieldRestore(coll->ns())) : CollectionPtr(); } @@ -757,7 +758,7 @@ CollectionPtr CollectionCatalog::lookupCollectionByNamespace(OperationContext* o // If found=true above but we don't have a Collection pointer it is a drop or rename. But first // check UncommittedCollections in case we find a new collection there. if (auto coll = UncommittedCollections::getForTxn(opCtx, nss)) { - return {opCtx, coll.get(), LookupCollectionForYieldRestore()}; + return {opCtx, coll.get(), LookupCollectionForYieldRestore(coll->ns())}; } // Report the drop or rename as nothing new was created. @@ -768,7 +769,7 @@ CollectionPtr CollectionCatalog::lookupCollectionByNamespace(OperationContext* o auto it = _collections.find(nss); auto coll = (it == _collections.end() ? nullptr : it->second); return (coll && coll->isCommitted()) - ? CollectionPtr(opCtx, coll.get(), LookupCollectionForYieldRestore()) + ? CollectionPtr(opCtx, coll.get(), LookupCollectionForYieldRestore(coll->ns())) : nullptr; } diff --git a/src/mongo/db/catalog/collection_catalog.h b/src/mongo/db/catalog/collection_catalog.h index f8ec0db2533..1103520bce6 100644 --- a/src/mongo/db/catalog/collection_catalog.h +++ b/src/mongo/db/catalog/collection_catalog.h @@ -517,7 +517,11 @@ private: * restore implementation for CollectionPtr when acquired from the catalog. */ struct LookupCollectionForYieldRestore { + explicit LookupCollectionForYieldRestore(const NamespaceString& nss) : _nss(nss) {} const Collection* operator()(OperationContext* opCtx, const UUID& uuid) const; + +private: + const NamespaceString _nss; }; /** diff --git a/src/mongo/db/catalog_raii.cpp b/src/mongo/db/catalog_raii.cpp index 13635b0a16c..5e523a362f2 100644 --- a/src/mongo/db/catalog_raii.cpp +++ b/src/mongo/db/catalog_raii.cpp @@ -325,12 +325,15 @@ Collection* AutoGetCollection::getWritableCollection(OperationContext* opCtx, // new write unit of work is opened. opCtx->recoveryUnit()->registerChange( [this, opCtx](boost::optional commitTime) { - _coll = CollectionPtr(opCtx, _coll.get(), LookupCollectionForYieldRestore()); + _coll = CollectionPtr( + opCtx, _coll.get(), LookupCollectionForYieldRestore(_coll->ns())); _writableColl = nullptr; }, [this, originalCollection = _coll.get(), opCtx]() { _coll = - CollectionPtr(opCtx, originalCollection, LookupCollectionForYieldRestore()); + CollectionPtr(opCtx, + originalCollection, + LookupCollectionForYieldRestore(originalCollection->ns())); _writableColl = nullptr; }); } -- cgit v1.2.1