diff options
author | Henrik Edin <henrik.edin@mongodb.com> | 2020-11-03 12:42:38 -0500 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2020-11-13 20:15:34 +0000 |
commit | 8f9823f644d0b6beba8a301866141a578d278534 (patch) | |
tree | 1ef4bc439da93a2b39cff7c96a975668c27e7070 /src/mongo/db/views | |
parent | b670258ac185ecea0492c742f2f50da9d8ab618f (diff) | |
download | mongo-8f9823f644d0b6beba8a301866141a578d278534.tar.gz |
SERVER-52556 Versioned CollectionCatalog. Writes are performed using copy-on-write.
Internal mutexes when reading CollectionCatalog are removed, just one mutex for writes are needed.
Lock-free reads helper AutoGetCollectionForReadLockFree stashes a CollectionCatalog consistent with snapshot on OperationContext
Diffstat (limited to 'src/mongo/db/views')
-rw-r--r-- | src/mongo/db/views/durable_view_catalog.cpp | 10 | ||||
-rw-r--r-- | src/mongo/db/views/view_catalog.cpp | 26 | ||||
-rw-r--r-- | src/mongo/db/views/view_catalog_test.cpp | 33 |
3 files changed, 39 insertions, 30 deletions
diff --git a/src/mongo/db/views/durable_view_catalog.cpp b/src/mongo/db/views/durable_view_catalog.cpp index 838d883fc0d..1031f87682f 100644 --- a/src/mongo/db/views/durable_view_catalog.cpp +++ b/src/mongo/db/views/durable_view_catalog.cpp @@ -110,8 +110,8 @@ void DurableViewCatalogImpl::_iterate(OperationContext* opCtx, ViewCatalogLookupBehavior lookupBehavior) { invariant(opCtx->lockState()->isCollectionLockedForMode(_db->getSystemViewsName(), MODE_IS)); - const CollectionPtr& systemViews = - CollectionCatalog::get(opCtx).lookupCollectionByNamespace(opCtx, _db->getSystemViewsName()); + CollectionPtr systemViews = CollectionCatalog::get(opCtx)->lookupCollectionByNamespace( + opCtx, _db->getSystemViewsName()); if (!systemViews) { return; } @@ -186,7 +186,7 @@ void DurableViewCatalogImpl::upsert(OperationContext* opCtx, dassert(opCtx->lockState()->isCollectionLockedForMode(systemViewsNs, MODE_X)); const CollectionPtr& systemViews = - CollectionCatalog::get(opCtx).lookupCollectionByNamespace(opCtx, systemViewsNs); + CollectionCatalog::get(opCtx)->lookupCollectionByNamespace(opCtx, systemViewsNs); invariant(systemViews); const bool requireIndex = false; @@ -217,8 +217,8 @@ void DurableViewCatalogImpl::remove(OperationContext* opCtx, const NamespaceStri dassert(opCtx->lockState()->isDbLockedForMode(_db->name(), MODE_IX)); dassert(opCtx->lockState()->isCollectionLockedForMode(name, MODE_IX)); - const CollectionPtr& systemViews = - CollectionCatalog::get(opCtx).lookupCollectionByNamespace(opCtx, _db->getSystemViewsName()); + CollectionPtr systemViews = CollectionCatalog::get(opCtx)->lookupCollectionByNamespace( + opCtx, _db->getSystemViewsName()); dassert(opCtx->lockState()->isCollectionLockedForMode(systemViews->ns(), MODE_X)); if (!systemViews) diff --git a/src/mongo/db/views/view_catalog.cpp b/src/mongo/db/views/view_catalog.cpp index 98931116973..c606eede46a 100644 --- a/src/mongo/db/views/view_catalog.cpp +++ b/src/mongo/db/views/view_catalog.cpp @@ -221,9 +221,9 @@ Status ViewCatalog::_createOrUpdateView(WithLock lk, _viewMap[viewName.ns()] = view; // Register the view in the CollectionCatalog mapping from ResourceID->namespace - CollectionCatalog& catalog = CollectionCatalog::get(opCtx); auto viewRid = ResourceId(RESOURCE_COLLECTION, viewName.ns()); - catalog.addResource(viewRid, viewName.ns()); + CollectionCatalog::write( + opCtx, [&](CollectionCatalog& catalog) { catalog.addResource(viewRid, viewName.ns()); }); opCtx->recoveryUnit()->onRollback([this, viewName, opCtx, viewRid]() { { @@ -232,10 +232,12 @@ Status ViewCatalog::_createOrUpdateView(WithLock lk, this->_viewGraphNeedsRefresh = true; } - CollectionCatalog& catalog = CollectionCatalog::get(opCtx); - catalog.removeResource(viewRid, viewName.ns()); + CollectionCatalog::write(opCtx, [&](CollectionCatalog& catalog) { + catalog.removeResource(viewRid, viewName.ns()); + }); }); + // Reload the view catalog with the changes applied. return _reload(lk, opCtx, ViewCatalogLookupBehavior::kValidateDurableViews); } @@ -462,8 +464,10 @@ Status ViewCatalog::modifyView(OperationContext* opCtx, this->_viewMap[viewName.ns()] = std::move(definition); } auto viewRid = ResourceId(RESOURCE_COLLECTION, viewName.ns()); - CollectionCatalog& catalog = CollectionCatalog::get(opCtx); - catalog.addResource(viewRid, viewName.ns()); + + CollectionCatalog::write(opCtx, [&](CollectionCatalog& catalog) { + catalog.addResource(viewRid, viewName.ns()); + }); }); return _createOrUpdateView(lk, @@ -502,15 +506,17 @@ Status ViewCatalog::dropView(OperationContext* opCtx, const NamespaceString& vie _viewGraph.remove(savedDefinition.name()); _viewMap.erase(viewName.ns()); - CollectionCatalog& catalog = CollectionCatalog::get(opCtx); auto viewRid = ResourceId(RESOURCE_COLLECTION, viewName.ns()); - catalog.removeResource(viewRid, viewName.ns()); + CollectionCatalog::write( + opCtx, [&](CollectionCatalog& catalog) { catalog.removeResource(viewRid, viewName.ns()); }); opCtx->recoveryUnit()->onRollback([this, viewName, savedDefinition, opCtx, viewRid]() { this->_viewGraphNeedsRefresh = true; this->_viewMap[viewName.ns()] = std::make_shared<ViewDefinition>(savedDefinition); - CollectionCatalog& catalog = CollectionCatalog::get(opCtx); - catalog.addResource(viewRid, viewName.ns()); + + CollectionCatalog::write(opCtx, [&](CollectionCatalog& catalog) { + catalog.addResource(viewRid, viewName.ns()); + }); }); // Reload the view catalog with the changes applied. diff --git a/src/mongo/db/views/view_catalog_test.cpp b/src/mongo/db/views/view_catalog_test.cpp index d84ed428c4b..c2d7367db3e 100644 --- a/src/mongo/db/views/view_catalog_test.cpp +++ b/src/mongo/db/views/view_catalog_test.cpp @@ -513,8 +513,8 @@ TEST_F(ViewCatalogFixture, LookupRIDExistingView) { ASSERT_OK(createView(operationContext(), viewName, viewOn, emptyPipeline, emptyCollation)); auto resourceID = ResourceId(RESOURCE_COLLECTION, "db.view"_sd); - auto& collectionCatalog = CollectionCatalog::get(operationContext()); - ASSERT(collectionCatalog.lookupResourceName(resourceID).get() == "db.view"); + auto collectionCatalog = CollectionCatalog::get(operationContext()); + ASSERT(collectionCatalog->lookupResourceName(resourceID).get() == "db.view"); } TEST_F(ViewCatalogFixture, LookupRIDExistingViewRollback) { @@ -533,8 +533,8 @@ TEST_F(ViewCatalogFixture, LookupRIDExistingViewRollback) { operationContext(), viewName, viewOn, emptyPipeline, emptyCollation)); } auto resourceID = ResourceId(RESOURCE_COLLECTION, "db.view"_sd); - auto& collectionCatalog = CollectionCatalog::get(operationContext()); - ASSERT(!collectionCatalog.lookupResourceName(resourceID)); + auto collectionCatalog = CollectionCatalog::get(operationContext()); + ASSERT(!collectionCatalog->lookupResourceName(resourceID)); } TEST_F(ViewCatalogFixture, LookupRIDAfterDrop) { @@ -545,8 +545,8 @@ TEST_F(ViewCatalogFixture, LookupRIDAfterDrop) { ASSERT_OK(dropView(operationContext(), viewName)); auto resourceID = ResourceId(RESOURCE_COLLECTION, "db.view"_sd); - auto& collectionCatalog = CollectionCatalog::get(operationContext()); - ASSERT(!collectionCatalog.lookupResourceName(resourceID)); + auto collectionCatalog = CollectionCatalog::get(operationContext()); + ASSERT(!collectionCatalog->lookupResourceName(resourceID)); } TEST_F(ViewCatalogFixture, LookupRIDAfterDropRollback) { @@ -554,11 +554,11 @@ TEST_F(ViewCatalogFixture, LookupRIDAfterDropRollback) { const NamespaceString viewOn("db.coll"); auto resourceID = ResourceId(RESOURCE_COLLECTION, "db.view"_sd); - auto& collectionCatalog = CollectionCatalog::get(operationContext()); { WriteUnitOfWork wunit(operationContext()); ASSERT_OK(createView(operationContext(), viewName, viewOn, emptyPipeline, emptyCollation)); - ASSERT(collectionCatalog.lookupResourceName(resourceID).get() == viewName.ns()); + ASSERT(CollectionCatalog::get(operationContext())->lookupResourceName(resourceID).get() == + viewName.ns()); wunit.commit(); } @@ -574,7 +574,8 @@ TEST_F(ViewCatalogFixture, LookupRIDAfterDropRollback) { ASSERT_OK(getViewCatalog()->dropView(operationContext(), viewName)); } - ASSERT(collectionCatalog.lookupResourceName(resourceID).get() == viewName.ns()); + ASSERT(CollectionCatalog::get(operationContext())->lookupResourceName(resourceID).get() == + viewName.ns()); } TEST_F(ViewCatalogFixture, LookupRIDAfterModify) { @@ -582,10 +583,10 @@ TEST_F(ViewCatalogFixture, LookupRIDAfterModify) { const NamespaceString viewOn("db.coll"); auto resourceID = ResourceId(RESOURCE_COLLECTION, "db.view"_sd); - auto& collectionCatalog = CollectionCatalog::get(operationContext()); ASSERT_OK(createView(operationContext(), viewName, viewOn, emptyPipeline, emptyCollation)); ASSERT_OK(modifyView(operationContext(), viewName, viewOn, emptyPipeline)); - ASSERT(collectionCatalog.lookupResourceName(resourceID).get() == viewName.ns()); + ASSERT(CollectionCatalog::get(operationContext())->lookupResourceName(resourceID).get() == + viewName.ns()); } TEST_F(ViewCatalogFixture, LookupRIDAfterModifyRollback) { @@ -593,11 +594,11 @@ TEST_F(ViewCatalogFixture, LookupRIDAfterModifyRollback) { const NamespaceString viewOn("db.coll"); auto resourceID = ResourceId(RESOURCE_COLLECTION, "db.view"_sd); - auto& collectionCatalog = CollectionCatalog::get(operationContext()); { WriteUnitOfWork wunit(operationContext()); ASSERT_OK(createView(operationContext(), viewName, viewOn, emptyPipeline, emptyCollation)); - ASSERT(collectionCatalog.lookupResourceName(resourceID).get() == viewName.ns()); + ASSERT(CollectionCatalog::get(operationContext())->lookupResourceName(resourceID).get() == + viewName.ns()); wunit.commit(); } { @@ -611,9 +612,11 @@ TEST_F(ViewCatalogFixture, LookupRIDAfterModifyRollback) { WriteUnitOfWork wunit(operationContext()); ASSERT_OK( getViewCatalog()->modifyView(operationContext(), viewName, viewOn, emptyPipeline)); - ASSERT(collectionCatalog.lookupResourceName(resourceID).get() == viewName.ns()); + ASSERT(CollectionCatalog::get(operationContext())->lookupResourceName(resourceID).get() == + viewName.ns()); } - ASSERT(collectionCatalog.lookupResourceName(resourceID).get() == viewName.ns()); + ASSERT(CollectionCatalog::get(operationContext())->lookupResourceName(resourceID).get() == + viewName.ns()); } TEST_F(ViewCatalogFixture, CreateViewThenDropAndLookup) { |