summaryrefslogtreecommitdiff
path: root/src/mongo/db/views
diff options
context:
space:
mode:
authorHenrik Edin <henrik.edin@mongodb.com>2021-05-25 14:08:46 -0400
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2021-05-28 20:31:38 +0000
commitaf3e6c163fd3cc80ee1c7433943135e943432598 (patch)
tree9dba2e91cbea234b1eb44453420ecfb358184ee4 /src/mongo/db/views
parent934d6de41bb3adbecee23e3837852e29c7ff4af6 (diff)
downloadmongo-af3e6c163fd3cc80ee1c7433943135e943432598.tar.gz
SERVER-57061 Make CollectionCatalog aware of namespaces used by ViewCatalog.
The CollectionCatalog can now ensure a namespace is not concurrently registered for a Collection and View.
Diffstat (limited to 'src/mongo/db/views')
-rw-r--r--src/mongo/db/views/view_catalog.cpp57
-rw-r--r--src/mongo/db/views/view_catalog.h4
2 files changed, 45 insertions, 16 deletions
diff --git a/src/mongo/db/views/view_catalog.cpp b/src/mongo/db/views/view_catalog.cpp
index ff6d0f4d925..9d72eb06d60 100644
--- a/src/mongo/db/views/view_catalog.cpp
+++ b/src/mongo/db/views/view_catalog.cpp
@@ -175,19 +175,24 @@ Status ViewCatalog::reload(OperationContext* opCtx,
NamespaceString::kSystemDotViewsCollectionName),
MODE_IS);
auto result =
- catalog.writable()->_reload(opCtx, ViewCatalogLookupBehavior::kValidateDurableViews);
+ catalog.writable()->_reload(opCtx, ViewCatalogLookupBehavior::kValidateDurableViews, true);
catalog.commit();
return result;
}
-Status ViewCatalog::_reload(OperationContext* opCtx, ViewCatalogLookupBehavior lookupBehavior) {
- LOGV2_DEBUG(22546, 1, "Reloading view catalog for database", "db"_attr = _durable->getName());
+Status ViewCatalog::_reload(OperationContext* opCtx,
+ ViewCatalogLookupBehavior lookupBehavior,
+ bool reloadForCollectionCatalog) {
+ const auto& dbName = _durable->getName();
+ LOGV2_DEBUG(22546, 1, "Reloading view catalog for database", "db"_attr = dbName);
_viewMap.clear();
_valid = false;
_viewGraphNeedsRefresh = true;
_stats = {};
+ absl::flat_hash_set<NamespaceString> viewsForDb;
+
auto reloadCallback = [&](const BSONObj& view) -> Status {
BSONObj collationSpec = view.hasField("collation") ? view["collation"].Obj() : BSONObj();
auto collator = parseCollator(opCtx, collationSpec);
@@ -224,6 +229,9 @@ Status ViewCatalog::_reload(OperationContext* opCtx, ViewCatalogLookupBehavior l
}
_viewMap[viewName.ns()] = std::move(viewDef);
+ if (reloadForCollectionCatalog) {
+ viewsForDb.insert(viewName);
+ }
return Status::OK();
};
@@ -235,6 +243,12 @@ Status ViewCatalog::_reload(OperationContext* opCtx, ViewCatalogLookupBehavior l
} else {
MONGO_UNREACHABLE;
}
+ if (reloadForCollectionCatalog) {
+ CollectionCatalog::write(
+ opCtx, [&dbName, viewsForDb = std::move(viewsForDb)](CollectionCatalog& catalog) {
+ catalog.replaceViewsForDatabase(dbName, std::move(viewsForDb));
+ });
+ }
} catch (const DBException& ex) {
auto status = ex.toStatus();
LOGV2(22547,
@@ -265,6 +279,9 @@ void ViewCatalog::clear(OperationContext* opCtx, const Database* db) {
catalog.writable()->_valid = true;
catalog.writable()->_viewGraphNeedsRefresh = false;
catalog.writable()->_stats = {};
+ CollectionCatalog::write(opCtx, [db](CollectionCatalog& catalog) {
+ catalog.replaceViewsForDatabase(db->name(), {});
+ });
catalog.commit();
}
@@ -325,20 +342,25 @@ Status ViewCatalog::_createOrUpdateView(OperationContext* opCtx,
_durable->upsert(opCtx, viewName, viewDefBuilder.obj());
_viewMap[viewName.ns()] = view;
- // Register the view in the CollectionCatalog mapping from ResourceID->namespace
- auto viewRid = ResourceId(RESOURCE_COLLECTION, viewName.ns());
- CollectionCatalog::write(
- opCtx, [&](CollectionCatalog& catalog) { catalog.addResource(viewRid, viewName.ns()); });
+ // Reload the view catalog with the changes applied.
+ auto res = _reload(opCtx, ViewCatalogLookupBehavior::kValidateDurableViews, false);
+ if (res.isOK()) {
+ // Register the view in the CollectionCatalog mapping from ResourceID->namespace
+ auto viewRid = ResourceId(RESOURCE_COLLECTION, viewName.ns());
- opCtx->recoveryUnit()->onRollback([viewName, opCtx, viewRid]() {
CollectionCatalog::write(opCtx, [&](CollectionCatalog& catalog) {
- catalog.removeResource(viewRid, viewName.ns());
+ catalog.registerView(viewName);
+ catalog.addResource(viewRid, viewName.ns());
});
- });
-
- // Reload the view catalog with the changes applied.
- return _reload(opCtx, ViewCatalogLookupBehavior::kValidateDurableViews);
+ opCtx->recoveryUnit()->onRollback([viewName, opCtx, viewRid]() {
+ CollectionCatalog::write(opCtx, [&](CollectionCatalog& catalog) {
+ catalog.removeResource(viewRid, viewName.ns());
+ catalog.deregisterView(viewName);
+ });
+ });
+ }
+ return res;
}
Status ViewCatalog::_upsertIntoGraph(OperationContext* opCtx, const ViewDefinition& viewDef) {
@@ -637,6 +659,11 @@ Status ViewCatalog::dropView(OperationContext* opCtx,
catalog.removeResource(viewRid, viewName.ns());
});
+ opCtx->recoveryUnit()->onCommit([viewName, opCtx](auto ts) {
+ CollectionCatalog::write(
+ opCtx, [&](CollectionCatalog& catalog) { catalog.deregisterView(viewName); });
+ });
+
opCtx->recoveryUnit()->onRollback([viewName, opCtx, viewRid]() {
CollectionCatalog::write(opCtx, [&](CollectionCatalog& catalog) {
catalog.addResource(viewRid, viewName.ns());
@@ -644,8 +671,8 @@ Status ViewCatalog::dropView(OperationContext* opCtx,
});
// Reload the view catalog with the changes applied.
- result =
- catalog.writable()->_reload(opCtx, ViewCatalogLookupBehavior::kValidateDurableViews);
+ result = catalog.writable()->_reload(
+ opCtx, ViewCatalogLookupBehavior::kValidateDurableViews, false);
}
catalog.commit();
return result;
diff --git a/src/mongo/db/views/view_catalog.h b/src/mongo/db/views/view_catalog.h
index 0340d0015af..dc8a20c1baa 100644
--- a/src/mongo/db/views/view_catalog.h
+++ b/src/mongo/db/views/view_catalog.h
@@ -212,7 +212,9 @@ private:
StringData ns,
ViewCatalogLookupBehavior lookupBehavior);
- Status _reload(OperationContext* opCtx, ViewCatalogLookupBehavior lookupBehavior);
+ Status _reload(OperationContext* opCtx,
+ ViewCatalogLookupBehavior lookupBehavior,
+ bool reloadForCollectionCatalog);
/**
* uasserts with the InvalidViewDefinition error if the current in-memory state of the view