summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorgalon1 <gil.alon@mongodb.com>2022-10-14 16:20:17 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2022-10-14 17:13:05 +0000
commit199173d18b9af33156290967dc50d1af3e0c34b3 (patch)
tree0e0b932e0ad0d56c2df1598e6fbbf1b11e1f5f27 /src
parentcf440fb8650821337a7e3228e8ba4ee7b537e0d3 (diff)
downloadmongo-199173d18b9af33156290967dc50d1af3e0c34b3.tar.gz
SERVER-63811 Add check so documents stage runs when db does not exist
Diffstat (limited to 'src')
-rw-r--r--src/mongo/db/pipeline/dispatch_shard_pipeline_test.cpp34
-rw-r--r--src/mongo/db/pipeline/document_source_documents.cpp2
-rw-r--r--src/mongo/db/pipeline/document_source_documents.h24
-rw-r--r--src/mongo/db/pipeline/lite_parsed_document_source.h7
-rw-r--r--src/mongo/db/pipeline/lite_parsed_pipeline.h7
-rw-r--r--src/mongo/db/pipeline/sharded_agg_helpers.cpp13
-rw-r--r--src/mongo/db/pipeline/sharded_agg_helpers.h3
-rw-r--r--src/mongo/s/commands/cluster_map_reduce_agg.cpp4
-rw-r--r--src/mongo/s/query/cluster_aggregate.cpp7
-rw-r--r--src/mongo/s/query/cluster_aggregation_planner.cpp13
-rw-r--r--src/mongo/s/query/cluster_aggregation_planner.h4
11 files changed, 91 insertions, 27 deletions
diff --git a/src/mongo/db/pipeline/dispatch_shard_pipeline_test.cpp b/src/mongo/db/pipeline/dispatch_shard_pipeline_test.cpp
index 069a7e2f0b2..8f04441de80 100644
--- a/src/mongo/db/pipeline/dispatch_shard_pipeline_test.cpp
+++ b/src/mongo/db/pipeline/dispatch_shard_pipeline_test.cpp
@@ -53,10 +53,11 @@ TEST_F(DispatchShardPipelineTest, DoesNotSplitPipelineIfTargetingOneShard) {
const Document serializedCommand = aggregation_request_helper::serializeToCommandDoc(
AggregateCommandRequest(expCtx()->ns, stages));
const bool hasChangeStream = false;
+ const bool startsWithDocuments = false;
auto future = launchAsync([&] {
auto results = sharded_agg_helpers::dispatchShardPipeline(
- serializedCommand, hasChangeStream, std::move(pipeline));
+ serializedCommand, hasChangeStream, startsWithDocuments, std::move(pipeline));
ASSERT_EQ(results.remoteCursors.size(), 1UL);
ASSERT(!results.splitPipeline);
});
@@ -84,10 +85,11 @@ TEST_F(DispatchShardPipelineTest, DoesSplitPipelineIfMatchSpansTwoShards) {
const Document serializedCommand = aggregation_request_helper::serializeToCommandDoc(
AggregateCommandRequest(expCtx()->ns, stages));
const bool hasChangeStream = false;
+ const bool startsWithDocuments = false;
auto future = launchAsync([&] {
auto results = sharded_agg_helpers::dispatchShardPipeline(
- serializedCommand, hasChangeStream, std::move(pipeline));
+ serializedCommand, hasChangeStream, startsWithDocuments, std::move(pipeline));
ASSERT_EQ(results.remoteCursors.size(), 2UL);
ASSERT(bool(results.splitPipeline));
});
@@ -118,10 +120,11 @@ TEST_F(DispatchShardPipelineTest, DispatchShardPipelineRetriesOnNetworkError) {
const Document serializedCommand = aggregation_request_helper::serializeToCommandDoc(
AggregateCommandRequest(expCtx()->ns, stages));
const bool hasChangeStream = false;
+ const bool startsWithDocuments = false;
auto future = launchAsync([&] {
// Shouldn't throw.
auto results = sharded_agg_helpers::dispatchShardPipeline(
- serializedCommand, hasChangeStream, std::move(pipeline));
+ serializedCommand, hasChangeStream, startsWithDocuments, std::move(pipeline));
ASSERT_EQ(results.remoteCursors.size(), 2UL);
ASSERT(bool(results.splitPipeline));
});
@@ -163,11 +166,14 @@ TEST_F(DispatchShardPipelineTest, DispatchShardPipelineDoesNotRetryOnStaleConfig
const Document serializedCommand = aggregation_request_helper::serializeToCommandDoc(
AggregateCommandRequest(expCtx()->ns, stages));
const bool hasChangeStream = false;
+ const bool startsWithDocuments = false;
+
auto future = launchAsync([&] {
- ASSERT_THROWS_CODE(sharded_agg_helpers::dispatchShardPipeline(
- serializedCommand, hasChangeStream, std::move(pipeline)),
- AssertionException,
- ErrorCodes::StaleConfig);
+ ASSERT_THROWS_CODE(
+ sharded_agg_helpers::dispatchShardPipeline(
+ serializedCommand, hasChangeStream, startsWithDocuments, std::move(pipeline)),
+ AssertionException,
+ ErrorCodes::StaleConfig);
});
// Mock out an error response.
@@ -197,15 +203,17 @@ TEST_F(DispatchShardPipelineTest, WrappedDispatchDoesRetryOnStaleConfigError) {
const Document serializedCommand = aggregation_request_helper::serializeToCommandDoc(
AggregateCommandRequest(expCtx()->ns, stages));
const bool hasChangeStream = false;
+ const bool startsWithDocuments = false;
auto future = launchAsync([&] {
// Shouldn't throw.
sharding::router::CollectionRouter router(getServiceContext(), kTestAggregateNss);
- auto results = router.route(operationContext(),
- "dispatch shard pipeline"_sd,
- [&](OperationContext* opCtx, const ChunkManager& cm) {
- return sharded_agg_helpers::dispatchShardPipeline(
- serializedCommand, hasChangeStream, pipeline->clone());
- });
+ auto results = router.route(
+ operationContext(),
+ "dispatch shard pipeline"_sd,
+ [&](OperationContext* opCtx, const ChunkManager& cm) {
+ return sharded_agg_helpers::dispatchShardPipeline(
+ serializedCommand, hasChangeStream, startsWithDocuments, pipeline->clone());
+ });
ASSERT_EQ(results.remoteCursors.size(), 1UL);
ASSERT(!bool(results.splitPipeline));
});
diff --git a/src/mongo/db/pipeline/document_source_documents.cpp b/src/mongo/db/pipeline/document_source_documents.cpp
index 5d4e02c71ea..934116a7c27 100644
--- a/src/mongo/db/pipeline/document_source_documents.cpp
+++ b/src/mongo/db/pipeline/document_source_documents.cpp
@@ -44,7 +44,7 @@ namespace mongo {
using boost::intrusive_ptr;
REGISTER_DOCUMENT_SOURCE(documents,
- LiteParsedDocumentSourceDefault::parse,
+ DocumentSourceDocuments::LiteParsed::parse,
DocumentSourceDocuments::createFromBson,
AllowedWithApiStrict::kAlways);
diff --git a/src/mongo/db/pipeline/document_source_documents.h b/src/mongo/db/pipeline/document_source_documents.h
index a6ab70e0c59..6048ed551bb 100644
--- a/src/mongo/db/pipeline/document_source_documents.h
+++ b/src/mongo/db/pipeline/document_source_documents.h
@@ -35,6 +35,30 @@
namespace mongo {
namespace DocumentSourceDocuments {
+class LiteParsed : public LiteParsedDocumentSource {
+public:
+ static std::unique_ptr<LiteParsed> parse(const NamespaceString& nss, const BSONElement& spec) {
+ return std::make_unique<LiteParsed>(spec.fieldName());
+ }
+
+ 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 {
+ return {};
+ }
+
+ bool isDocuments() const final {
+ return true;
+ }
+
+ bool allowedToPassthroughFromMongos() const final {
+ return false;
+ }
+};
static constexpr StringData kStageName = "$documents"_sd;
diff --git a/src/mongo/db/pipeline/lite_parsed_document_source.h b/src/mongo/db/pipeline/lite_parsed_document_source.h
index 34648576917..3e1dc6ea6c6 100644
--- a/src/mongo/db/pipeline/lite_parsed_document_source.h
+++ b/src/mongo/db/pipeline/lite_parsed_document_source.h
@@ -171,6 +171,13 @@ public:
}
/**
+ * Returns true if this is a $documents stage.
+ */
+ virtual bool isDocuments() const {
+ return false;
+ }
+
+ /**
* Returns true if this stage does not require an input source.
*/
virtual bool isInitialSource() const {
diff --git a/src/mongo/db/pipeline/lite_parsed_pipeline.h b/src/mongo/db/pipeline/lite_parsed_pipeline.h
index 712d23c32bd..d02ff8ee70e 100644
--- a/src/mongo/db/pipeline/lite_parsed_pipeline.h
+++ b/src/mongo/db/pipeline/lite_parsed_pipeline.h
@@ -123,6 +123,13 @@ public:
}
/**
+ * Returns true if the pipeline begins with a $documents stage.
+ */
+ bool startsWithDocuments() const {
+ return !_stageSpecs.empty() && _stageSpecs.front()->isDocuments();
+ }
+
+ /**
* Returns true if the pipeline has a $changeStream stage.
*/
bool hasChangeStream() const {
diff --git a/src/mongo/db/pipeline/sharded_agg_helpers.cpp b/src/mongo/db/pipeline/sharded_agg_helpers.cpp
index fed03ec5079..7940fc0b97b 100644
--- a/src/mongo/db/pipeline/sharded_agg_helpers.cpp
+++ b/src/mongo/db/pipeline/sharded_agg_helpers.cpp
@@ -769,9 +769,11 @@ std::unique_ptr<Pipeline, PipelineDeleter> targetShardsAndAddMergeCursors(
LiteParsedPipeline liteParsedPipeline(aggRequest);
auto hasChangeStream = liteParsedPipeline.hasChangeStream();
+ auto startsWithDocuments = liteParsedPipeline.startsWithDocuments();
auto shardDispatchResults =
dispatchShardPipeline(aggregation_request_helper::serializeToCommandDoc(aggRequest),
hasChangeStream,
+ startsWithDocuments,
std::move(pipeline),
shardTargetingPolicy,
std::move(readConcern));
@@ -1007,6 +1009,7 @@ BSONObj createCommandForTargetedShards(const boost::intrusive_ptr<ExpressionCont
DispatchShardPipelineResults dispatchShardPipeline(
Document serializedCommand,
bool hasChangeStream,
+ bool startsWithDocuments,
std::unique_ptr<Pipeline, PipelineDeleter> pipeline,
ShardTargetingPolicy shardTargetingPolicy,
boost::optional<BSONObj> readConcern) {
@@ -1051,7 +1054,7 @@ DispatchShardPipelineResults dispatchShardPipeline(
: expCtx->getCollatorBSON();
// Determine whether we can run the entire aggregation on a single shard.
- const bool mustRunOnAll = mustRunOnAllShards(expCtx->ns, hasChangeStream);
+ const bool mustRunOnAll = mustRunOnAllShards(expCtx->ns, hasChangeStream, startsWithDocuments);
std::set<ShardId> shardIds = getTargetedShards(
expCtx, mustRunOnAll, executionNsRoutingInfo, shardQuery, shardTargetingCollation);
@@ -1486,9 +1489,11 @@ BSONObj targetShardsForExplain(Pipeline* ownedPipeline) {
AggregateCommandRequest aggRequest(expCtx->ns, rawStages);
LiteParsedPipeline liteParsedPipeline(aggRequest);
auto hasChangeStream = liteParsedPipeline.hasChangeStream();
+ auto startsWithDocuments = liteParsedPipeline.startsWithDocuments();
auto shardDispatchResults =
dispatchShardPipeline(aggregation_request_helper::serializeToCommandDoc(aggRequest),
hasChangeStream,
+ startsWithDocuments,
std::move(pipeline));
BSONObjBuilder explainBuilder;
auto appendStatus =
@@ -1523,11 +1528,13 @@ Shard::RetryPolicy getDesiredRetryPolicy(OperationContext* opCtx) {
return Shard::RetryPolicy::kIdempotent;
}
-bool mustRunOnAllShards(const NamespaceString& nss, bool hasChangeStream) {
+bool mustRunOnAllShards(const NamespaceString& nss,
+ bool hasChangeStream,
+ bool startsWithDocuments) {
// The following aggregations must be routed to all shards:
// - Any collectionless aggregation, such as non-localOps $currentOp.
// - Any aggregation which begins with a $changeStream stage.
- return nss.isCollectionlessAggregateNS() || hasChangeStream;
+ return !startsWithDocuments && (nss.isCollectionlessAggregateNS() || hasChangeStream);
}
std::unique_ptr<Pipeline, PipelineDeleter> attachCursorToPipeline(
diff --git a/src/mongo/db/pipeline/sharded_agg_helpers.h b/src/mongo/db/pipeline/sharded_agg_helpers.h
index 5f30f6f6a93..f69d422da0c 100644
--- a/src/mongo/db/pipeline/sharded_agg_helpers.h
+++ b/src/mongo/db/pipeline/sharded_agg_helpers.h
@@ -124,6 +124,7 @@ SplitPipeline splitPipeline(std::unique_ptr<Pipeline, PipelineDeleter> pipeline)
DispatchShardPipelineResults dispatchShardPipeline(
Document serializedCommand,
bool hasChangeStream,
+ bool startsWithDocuments,
std::unique_ptr<Pipeline, PipelineDeleter> pipeline,
ShardTargetingPolicy shardTargetingPolicy = ShardTargetingPolicy::kAllowed,
boost::optional<BSONObj> readConcern = boost::none);
@@ -179,7 +180,7 @@ StatusWith<ChunkManager> getExecutionNsRoutingInfo(OperationContext* opCtx,
/**
* Returns true if an aggregation over 'nss' must run on all shards.
*/
-bool mustRunOnAllShards(const NamespaceString& nss, bool hasChangeStream);
+bool mustRunOnAllShards(const NamespaceString& nss, bool hasChangeStream, bool startsWithDocuments);
/**
* Retrieves the desired retry policy based on whether the default writeConcern is set on 'opCtx'.
diff --git a/src/mongo/s/commands/cluster_map_reduce_agg.cpp b/src/mongo/s/commands/cluster_map_reduce_agg.cpp
index 52a61ab9325..3591aa20b17 100644
--- a/src/mongo/s/commands/cluster_map_reduce_agg.cpp
+++ b/src/mongo/s/commands/cluster_map_reduce_agg.cpp
@@ -187,6 +187,7 @@ bool runAggregationMapReduce(OperationContext* opCtx,
cm,
involvedNamespaces,
false, // hasChangeStream
+ false, // startsWithDocuments
true, // allowedToPassthrough
false); // perShardCursor
try {
@@ -230,7 +231,8 @@ bool runAggregationMapReduce(OperationContext* opCtx,
namespaces,
privileges,
&tempResults,
- false)); // hasChangeStream
+ false, // hasChangeStream
+ false)); // startsWithDocuments
break;
}
diff --git a/src/mongo/s/query/cluster_aggregate.cpp b/src/mongo/s/query/cluster_aggregate.cpp
index 7c09e31d1ef..6a0b135fef4 100644
--- a/src/mongo/s/query/cluster_aggregate.cpp
+++ b/src/mongo/s/query/cluster_aggregate.cpp
@@ -310,6 +310,7 @@ Status ClusterAggregate::runAggregate(OperationContext* opCtx,
auto hasChangeStream = liteParsedPipeline.hasChangeStream();
auto involvedNamespaces = liteParsedPipeline.getInvolvedNamespaces();
auto shouldDoFLERewrite = ::mongo::shouldDoFLERewrite(request);
+ auto startsWithDocuments = liteParsedPipeline.startsWithDocuments();
// If the routing table is not already taken by the higher level, fill it now.
if (!cm) {
@@ -339,7 +340,7 @@ Status ClusterAggregate::runAggregate(OperationContext* opCtx,
if (executionNsRoutingInfoStatus.isOK()) {
cm = std::move(executionNsRoutingInfoStatus.getValue());
- } else if (!(hasChangeStream &&
+ } else if (!((hasChangeStream || startsWithDocuments) &&
executionNsRoutingInfoStatus == ErrorCodes::NamespaceNotFound)) {
appendEmptyResultSetWithStatus(
opCtx, namespaces.requestedNss, executionNsRoutingInfoStatus.getStatus(), result);
@@ -403,6 +404,7 @@ Status ClusterAggregate::runAggregate(OperationContext* opCtx,
cm,
involvedNamespaces,
hasChangeStream,
+ startsWithDocuments,
allowedToPassthrough,
request.getPassthroughToShard().has_value());
@@ -477,7 +479,8 @@ Status ClusterAggregate::runAggregate(OperationContext* opCtx,
namespaces,
privileges,
result,
- hasChangeStream);
+ hasChangeStream,
+ startsWithDocuments);
}
case cluster_aggregation_planner::AggregationTargeter::TargetingPolicy::
kSpecificShardOnly: {
diff --git a/src/mongo/s/query/cluster_aggregation_planner.cpp b/src/mongo/s/query/cluster_aggregation_planner.cpp
index 3f970f2f8c1..5788b30a7d4 100644
--- a/src/mongo/s/query/cluster_aggregation_planner.cpp
+++ b/src/mongo/s/query/cluster_aggregation_planner.cpp
@@ -563,6 +563,7 @@ AggregationTargeter AggregationTargeter::make(
boost::optional<ChunkManager> cm,
stdx::unordered_set<NamespaceString> involvedNamespaces,
bool hasChangeStream,
+ bool startsWithDocuments,
bool allowedToPassthrough,
bool perShardCursor) {
if (perShardCursor) {
@@ -583,10 +584,11 @@ AggregationTargeter AggregationTargeter::make(
// Determine whether this aggregation must be dispatched to all shards in the cluster.
const bool mustRunOnAll =
- sharded_agg_helpers::mustRunOnAllShards(executionNss, hasChangeStream);
+ sharded_agg_helpers::mustRunOnAllShards(executionNss, hasChangeStream, startsWithDocuments);
- // If we don't have a routing table, then this is a $changeStream which must run on all shards.
- invariant(cm || (mustRunOnAll && hasChangeStream));
+ // If we don't have a routing table, then this is either a $changeStream which must run on all
+ // shards or a $documents stage which must not.
+ invariant(cm || (mustRunOnAll && hasChangeStream) || (startsWithDocuments && !mustRunOnAll));
// A pipeline is allowed to passthrough to the primary shard iff the following conditions are
// met:
@@ -663,11 +665,12 @@ Status dispatchPipelineAndMerge(OperationContext* opCtx,
const ClusterAggregate::Namespaces& namespaces,
const PrivilegeVector& privileges,
BSONObjBuilder* result,
- bool hasChangeStream) {
+ bool hasChangeStream,
+ bool startsWithDocuments) {
auto expCtx = targeter.pipeline->getContext();
// If not, split the pipeline as necessary and dispatch to the relevant shards.
auto shardDispatchResults = sharded_agg_helpers::dispatchShardPipeline(
- serializedCommand, hasChangeStream, std::move(targeter.pipeline));
+ serializedCommand, hasChangeStream, startsWithDocuments, std::move(targeter.pipeline));
// If the operation is an explain, then we verify that it succeeded on all targeted
// shards, write the results to the output builder, and return immediately.
diff --git a/src/mongo/s/query/cluster_aggregation_planner.h b/src/mongo/s/query/cluster_aggregation_planner.h
index 046d8eab6ca..78c919192db 100644
--- a/src/mongo/s/query/cluster_aggregation_planner.h
+++ b/src/mongo/s/query/cluster_aggregation_planner.h
@@ -82,6 +82,7 @@ struct AggregationTargeter {
boost::optional<ChunkManager> cm,
stdx::unordered_set<NamespaceString> involvedNamespaces,
bool hasChangeStream,
+ bool startsWithDocuments,
bool allowedToPassthrough,
bool perShardCursor);
@@ -125,7 +126,8 @@ Status dispatchPipelineAndMerge(OperationContext* opCtx,
const ClusterAggregate::Namespaces& namespaces,
const PrivilegeVector& privileges,
BSONObjBuilder* result,
- bool hasChangeStream);
+ bool hasChangeStream,
+ bool startsWithDocuments);
/**
* Similar to runPipelineOnPrimaryShard but allows $changeStreams. Intended for use by per shard