diff options
author | Dianna Hohensee <dianna.hohensee@mongodb.com> | 2020-10-05 13:11:19 -0400 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2020-10-08 08:45:03 +0000 |
commit | ca0150eaf6cad95debae550c28fd50bb016db396 (patch) | |
tree | bc37490d499c2ada761a9ac2485ce8027fec5d10 /src/mongo/db/catalog_raii.cpp | |
parent | 1f11a9c73e11ebb6a89b1600a0a03741111c48bc (diff) | |
download | mongo-ca0150eaf6cad95debae550c28fd50bb016db396.tar.gz |
SERVER-50678 Implement lock-free version of AutoGetCollection
Diffstat (limited to 'src/mongo/db/catalog_raii.cpp')
-rw-r--r-- | src/mongo/db/catalog_raii.cpp | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/mongo/db/catalog_raii.cpp b/src/mongo/db/catalog_raii.cpp index f19b9738453..2e1c62ad736 100644 --- a/src/mongo/db/catalog_raii.cpp +++ b/src/mongo/db/catalog_raii.cpp @@ -194,6 +194,43 @@ Collection* AutoGetCollection::getWritableCollection(CollectionCatalog::Lifetime return _writableColl; } +AutoGetCollectionLockFree::AutoGetCollectionLockFree(OperationContext* opCtx, + const NamespaceStringOrUUID& nsOrUUID, + AutoGetCollectionViewMode viewMode, + Date_t deadline) + : _globalLock( + opCtx, MODE_IS, deadline, Lock::InterruptBehavior::kThrow, true /* skipRSTLLock */) { + + // Wait for a configured amount of time after acquiring locks if the failpoint is enabled + setAutoGetCollectionWait.execute( + [&](const BSONObj& data) { sleepFor(Milliseconds(data["waitForMillis"].numberInt())); }); + + _resolvedNss = CollectionCatalog::get(opCtx).resolveNamespaceStringOrUUID(opCtx, nsOrUUID); + _collection = + CollectionCatalog::get(opCtx).lookupCollectionByNamespaceForRead(opCtx, _resolvedNss); + _collectionPtr = CollectionPtr(_collection); + + // TODO (SERVER-51319): add DatabaseShardingState::checkDbVersion somewhere. + + if (_collection) { + // If the collection exists, there is no need to check for views. + return; + } + + // Returns nullptr for 'viewCatalog' if db does not exist. + auto viewCatalog = DatabaseHolder::get(opCtx)->getSharedViewCatalog(opCtx, _resolvedNss.db()); + if (!viewCatalog) { + return; + } + + // TODO (SERVER-51320): this code will invariant in ViewCatalog::lookup() because it takes a + // CollectionLock that expects a DBLock to be held on the database already. + _view = viewCatalog->lookup(opCtx, _resolvedNss.ns()); + uassert(ErrorCodes::CommandNotSupportedOnView, + str::stream() << "Namespace " << _resolvedNss.ns() << " is a view, not a collection", + !_view || viewMode == AutoGetCollectionViewMode::kViewsPermitted); +} + struct CollectionWriter::SharedImpl { SharedImpl(CollectionWriter* parent) : _parent(parent) {} |