summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenety Goh <benety@mongodb.com>2022-02-24 19:53:44 -0500
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2022-02-25 01:26:21 +0000
commitd41cbd82130527fb078c9d4b1f47c6d2611fcbb5 (patch)
tree9e6735f9eb82d1776487e6d7579b74104b99f26f
parent74280e7af5405e41d2f128d9a261e4d139cc8e71 (diff)
downloadmongo-d41cbd82130527fb078c9d4b1f47c6d2611fcbb5.tar.gz
SERVER-62006 add listCatalog aggregation stage
-rw-r--r--src/mongo/db/pipeline/SConscript1
-rw-r--r--src/mongo/db/pipeline/document_source_list_catalog.cpp123
-rw-r--r--src/mongo/db/pipeline/document_source_list_catalog.h107
-rw-r--r--src/mongo/db/storage/storage_parameters.idl4
4 files changed, 235 insertions, 0 deletions
diff --git a/src/mongo/db/pipeline/SConscript b/src/mongo/db/pipeline/SConscript
index 84ffd9cca7c..6a67e39b95d 100644
--- a/src/mongo/db/pipeline/SConscript
+++ b/src/mongo/db/pipeline/SConscript
@@ -291,6 +291,7 @@ pipelineEnv.Library(
'document_source_internal_split_pipeline.cpp',
'document_source_limit.cpp',
'document_source_list_cached_and_active_users.cpp',
+ 'document_source_list_catalog.cpp',
'document_source_list_local_sessions.cpp',
'document_source_list_sessions.cpp',
'document_source_lookup.cpp',
diff --git a/src/mongo/db/pipeline/document_source_list_catalog.cpp b/src/mongo/db/pipeline/document_source_list_catalog.cpp
new file mode 100644
index 00000000000..fc29a777360
--- /dev/null
+++ b/src/mongo/db/pipeline/document_source_list_catalog.cpp
@@ -0,0 +1,123 @@
+/**
+ * Copyright (C) 2022-present MongoDB, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the Server Side Public License, version 1,
+ * as published by MongoDB, Inc.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * Server Side Public License for more details.
+ *
+ * You should have received a copy of the Server Side Public License
+ * along with this program. If not, see
+ * <http://www.mongodb.com/licensing/server-side-public-license>.
+ *
+ * As a special exception, the copyright holders give permission to link the
+ * code of portions of this program with the OpenSSL library under certain
+ * conditions as described in each individual source file and distribute
+ * linked combinations including the program with the OpenSSL library. You
+ * must comply with the Server Side Public License in all respects for
+ * all of the code used other than as permitted herein. If you modify file(s)
+ * with this exception, you may extend this exception to your version of the
+ * file(s), but you are not obligated to do so. If you do not wish to do so,
+ * delete this exception statement from your version. If you delete this
+ * exception statement from all source files in the program, then also delete
+ * it in the license file.
+ */
+
+#include "mongo/db/pipeline/document_source_list_catalog.h"
+
+#include <fmt/format.h>
+
+#include "mongo/db/commands/feature_compatibility_version_documentation.h"
+#include "mongo/db/namespace_string.h"
+#include "mongo/db/pipeline/expression_context.h"
+#include "mongo/db/pipeline/process_interface/mongo_process_interface.h"
+#include "mongo/db/server_options.h"
+#include "mongo/db/storage/storage_parameters_gen.h"
+#include "mongo/util/version/releases.h"
+
+namespace mongo {
+
+using boost::intrusive_ptr;
+
+REGISTER_DOCUMENT_SOURCE_WITH_MIN_VERSION(listCatalog,
+ DocumentSourceListCatalog::LiteParsed::parse,
+ DocumentSourceListCatalog::createFromBson,
+ AllowedWithApiStrict::kNeverInVersion1,
+ multiversion::FeatureCompatibilityVersion::kVersion_5_3);
+
+const char* DocumentSourceListCatalog::getSourceName() const {
+ return kStageName.rawData();
+}
+
+PrivilegeVector DocumentSourceListCatalog::LiteParsed::requiredPrivileges(
+ bool isMongos, bool bypassDocumentValidation) const {
+
+ // Refer to privileges for the readAnyDatabase role in addReadOnlyAnyDbPrivileges().
+ // See builtin_roles.cpp.
+ // TODO(SERVER-63748): Change privileges to a combination of listDatabases, listCollections,
+ // and listIndexes.
+ return {Privilege(ResourcePattern::forDatabaseName("admin"), ActionType::find)};
+}
+
+DocumentSource::GetNextResult DocumentSourceListCatalog::doGetNext() {
+ if (!_catalogDocsInitialized) {
+ _catalogDocs = pExpCtx->mongoProcessInterface->listCatalog(pExpCtx->opCtx);
+ _catalogDocsInitialized = true;
+ }
+
+ if (!_catalogDocs.empty()) {
+ Document doc{_catalogDocs.front()};
+ _catalogDocs.pop_front();
+ return doc;
+ }
+
+ return GetNextResult::makeEOF();
+}
+
+DocumentSourceListCatalog::DocumentSourceListCatalog(
+ const intrusive_ptr<ExpressionContext>& pExpCtx)
+ : DocumentSource(kStageName, pExpCtx) {}
+
+intrusive_ptr<DocumentSource> DocumentSourceListCatalog::createFromBson(
+ BSONElement elem, const intrusive_ptr<ExpressionContext>& pExpCtx) {
+ uassert(6200600,
+ "The $listCatalog stage specification must be an empty object",
+ elem.type() == Object && elem.Obj().isEmpty());
+
+ const NamespaceString& nss = pExpCtx->ns;
+
+ uassert(ErrorCodes::InvalidNamespace,
+ "$listCatalog must be run against the 'admin' database with {aggregate: 1}",
+ nss.db() == NamespaceString::kAdminDb && nss.isCollectionlessAggregateNS());
+
+ uassert(ErrorCodes::QueryFeatureNotAllowed,
+ fmt::format("The {} aggregation stage is not enabled", kStageName),
+ feature_flags::gDocumentSourceListCatalog.isEnabled(
+ serverGlobalParams.featureCompatibility));
+
+ // We declare this stage with a min version but the base class DocumentSource checks the
+ // minimum version only if pExpCtx->maxFeatureCompatibilityVersion is provided.
+ if (!pExpCtx->maxFeatureCompatibilityVersion) {
+ const auto& globalFcv = serverGlobalParams.featureCompatibility;
+ using FCV = multiversion::FeatureCompatibilityVersion;
+ uassert(ErrorCodes::QueryFeatureNotAllowed,
+ fmt::format("The {} aggregation stage is not allowed in the current feature "
+ "compatibility version. See {} for more information.",
+ kStageName,
+ feature_compatibility_version_documentation::kCompatibilityLink),
+ !globalFcv.isVersionInitialized() ||
+ globalFcv.isGreaterThanOrEqualTo(FCV::kVersion_5_3));
+ }
+
+ return new DocumentSourceListCatalog(pExpCtx);
+}
+
+Value DocumentSourceListCatalog::serialize(
+ boost::optional<ExplainOptions::Verbosity> explain) const {
+ return Value(DOC(getSourceName() << Document()));
+}
+} // namespace mongo
diff --git a/src/mongo/db/pipeline/document_source_list_catalog.h b/src/mongo/db/pipeline/document_source_list_catalog.h
new file mode 100644
index 00000000000..c0cb3829a00
--- /dev/null
+++ b/src/mongo/db/pipeline/document_source_list_catalog.h
@@ -0,0 +1,107 @@
+/**
+ * Copyright (C) 2022-present MongoDB, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the Server Side Public License, version 1,
+ * as published by MongoDB, Inc.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * Server Side Public License for more details.
+ *
+ * You should have received a copy of the Server Side Public License
+ * along with this program. If not, see
+ * <http://www.mongodb.com/licensing/server-side-public-license>.
+ *
+ * As a special exception, the copyright holders give permission to link the
+ * code of portions of this program with the OpenSSL library under certain
+ * conditions as described in each individual source file and distribute
+ * linked combinations including the program with the OpenSSL library. You
+ * must comply with the Server Side Public License in all respects for
+ * all of the code used other than as permitted herein. If you modify file(s)
+ * with this exception, you may extend this exception to your version of the
+ * file(s), but you are not obligated to do so. If you do not wish to do so,
+ * delete this exception statement from your version. If you delete this
+ * exception statement from all source files in the program, then also delete
+ * it in the license file.
+ */
+
+#pragma once
+
+#include <deque>
+
+#include "mongo/bson/bsonobj.h"
+#include "mongo/db/pipeline/document_source.h"
+#include "mongo/db/pipeline/lite_parsed_document_source.h"
+
+namespace mongo {
+
+/**
+ * Provides a document source interface to retrieve catalog metadata for collections and views.
+ * Each document returned represents either:
+ * - a single collection with its indexes; or
+ * - a view instance.
+ */
+class DocumentSourceListCatalog final : public DocumentSource {
+public:
+ static constexpr StringData kStageName = "$listCatalog"_sd;
+
+ class LiteParsed final : public LiteParsedDocumentSource {
+ public:
+ static std::unique_ptr<LiteParsed> parse(const NamespaceString& nss,
+ const BSONElement& spec) {
+ return std::make_unique<LiteParsed>(spec.fieldName());
+ }
+
+ explicit LiteParsed(std::string parseTimeName)
+ : LiteParsedDocumentSource(std::move(parseTimeName)) {}
+
+ stdx::unordered_set<NamespaceString> getInvolvedNamespaces() const final {
+ return stdx::unordered_set<NamespaceString>();
+ }
+
+
+ PrivilegeVector requiredPrivileges(bool isMongos,
+ bool bypassDocumentValidation) const final;
+
+ bool isInitialSource() const final {
+ return true;
+ }
+ };
+
+ // virtuals from DocumentSource
+ const char* getSourceName() const final;
+ Value serialize(boost::optional<ExplainOptions::Verbosity> explain = boost::none) const final;
+
+ StageConstraints constraints(Pipeline::SplitState pipeState) const final {
+ StageConstraints constraints(StreamType::kStreaming,
+ PositionRequirement::kFirst,
+ HostTypeRequirement::kAnyShard,
+ DiskUseRequirement::kNoDiskUse,
+ FacetRequirement::kNotAllowed,
+ TransactionRequirement::kNotAllowed,
+ LookupRequirement::kAllowed,
+ UnionRequirement::kAllowed);
+
+ constraints.isIndependentOfAnyCollection = true;
+ constraints.requiresInputDocSource = false;
+ return constraints;
+ }
+
+ boost::optional<DistributedPlanLogic> distributedPlanLogic() final {
+ return boost::none;
+ }
+
+ static boost::intrusive_ptr<DocumentSource> createFromBson(
+ BSONElement elem, const boost::intrusive_ptr<ExpressionContext>& pExpCtx);
+
+private:
+ DocumentSourceListCatalog(const boost::intrusive_ptr<ExpressionContext>& pExpCtx);
+ GetNextResult doGetNext() final;
+
+ bool _catalogDocsInitialized = false;
+ std::deque<BSONObj> _catalogDocs;
+};
+
+} // namespace mongo
diff --git a/src/mongo/db/storage/storage_parameters.idl b/src/mongo/db/storage/storage_parameters.idl
index f306417a6da..871ce5e09b4 100644
--- a/src/mongo/db/storage/storage_parameters.idl
+++ b/src/mongo/db/storage/storage_parameters.idl
@@ -152,3 +152,7 @@ feature_flags:
description: "When enabled, support batching multi-document deletions"
cpp_varname: feature_flags::gBatchMultiDeletes
default: false
+ featureFlagDocumentSourceListCatalog:
+ description: "When enabled, allow the use of the $listCatalog aggregation stage"
+ cpp_varname: feature_flags::gDocumentSourceListCatalog
+ default: false