Skip to content

Commit 1a6a031

Browse files
committed
Delete ShardNameStage and move functionality to ShardFilterStage.
1 parent 189caf8 commit 1a6a031

File tree

12 files changed

+39
-245
lines changed

12 files changed

+39
-245
lines changed

src/mongo/db/SConscript

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1027,7 +1027,6 @@ env.Library(
10271027
'exec/return_key.cpp',
10281028
'exec/shard_filter.cpp',
10291029
'exec/shard_filterer_impl.cpp',
1030-
'exec/shard_name.cpp',
10311030
'exec/skip.cpp',
10321031
'exec/sort.cpp',
10331032
'exec/sort_key_generator.cpp',

src/mongo/db/exec/shard_filter.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "mongo/db/exec/filter.h"
3939
#include "mongo/db/exec/scoped_timer.h"
4040
#include "mongo/db/exec/working_set_common.h"
41+
#include "mongo/db/s/sharding_state.h"
4142
#include "mongo/s/shard_key_pattern.h"
4243
#include "mongo/util/log.h"
4344

@@ -53,8 +54,10 @@ const char* ShardFilterStage::kStageType = "SHARDING_FILTER";
5354
ShardFilterStage::ShardFilterStage(OperationContext* opCtx,
5455
ScopedCollectionMetadata metadata,
5556
WorkingSet* ws,
56-
std::unique_ptr<PlanStage> child)
57-
: PlanStage(kStageType, opCtx), _ws(ws), _shardFilterer(std::move(metadata)) {
57+
std::unique_ptr<PlanStage> child,
58+
bool wantShardName)
59+
: PlanStage(kStageType, opCtx), _ws(ws), _shardFilterer(std::move(metadata)),
60+
_wantShardName(wantShardName) {
5861
_children.emplace_back(std::move(child));
5962
}
6063

@@ -104,6 +107,13 @@ PlanStage::StageState ShardFilterStage::doWork(WorkingSetID* out) {
104107
++_specificStats.chunkSkips;
105108
return PlanStage::NEED_TIME;
106109
}
110+
111+
if(wantShardName()) {
112+
auto sharding = ShardingState::get(this->getOpCtx());
113+
114+
// Populate the working set member with the shard name and return it.
115+
member->metadata().setShardName(sharding->shardId());
116+
}
107117
}
108118

109119
// If we're here either we have shard state and our doc passed, or we have no shard

src/mongo/db/exec/shard_filter.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ class ShardFilterStage final : public PlanStage {
7474
ShardFilterStage(OperationContext* opCtx,
7575
ScopedCollectionMetadata metadata,
7676
WorkingSet* ws,
77-
std::unique_ptr<PlanStage> child);
77+
std::unique_ptr<PlanStage> child,
78+
bool wantShardName);
7879
~ShardFilterStage();
7980

8081
bool isEOF() final;
@@ -91,6 +92,10 @@ class ShardFilterStage final : public PlanStage {
9192
static const char* kStageType;
9293

9394
private:
95+
bool wantShardName() const {
96+
return _wantShardName;
97+
}
98+
9499
WorkingSet* _ws;
95100

96101
// Stats
@@ -103,6 +108,8 @@ class ShardFilterStage final : public PlanStage {
103108
// ScopedCollectionMetadata for the entire query, it'd be possible for data which the query
104109
// needs to read to be deleted while it's still running.
105110
ShardFiltererImpl _shardFilterer;
111+
112+
bool _wantShardName;
106113
};
107114

108115
} // namespace mongo

src/mongo/db/exec/shard_name.cpp

Lines changed: 0 additions & 104 deletions
This file was deleted.

src/mongo/db/exec/shard_name.h

Lines changed: 0 additions & 60 deletions
This file was deleted.

src/mongo/db/pipeline/pipeline_d.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,12 @@ StatusWith<unique_ptr<PlanExecutor, PlanExecutor::Deleter>> createRandomCursorEx
167167
sampleSize / (numRecords * kMaxSampleRatioForRandCursor), kMaxSampleRatioForRandCursor);
168168
// The trial plan is SHARDING_FILTER-MULTI_ITERATOR.
169169
auto randomCursorPlan =
170-
std::make_unique<ShardFilterStage>(opCtx, shardMetadata, ws.get(), std::move(root));
170+
std::make_unique<ShardFilterStage>(opCtx, shardMetadata, ws.get(), std::move(root), false);
171171
// The backup plan is SHARDING_FILTER-COLLSCAN.
172172
std::unique_ptr<PlanStage> collScanPlan = std::make_unique<CollectionScan>(
173173
opCtx, coll, CollectionScanParams{}, ws.get(), nullptr);
174174
collScanPlan = std::make_unique<ShardFilterStage>(
175-
opCtx, shardMetadata, ws.get(), std::move(collScanPlan));
175+
opCtx, shardMetadata, ws.get(), std::move(collScanPlan), false);
176176
// Place a TRIAL stage at the root of the plan tree, and pass it the trial and backup plans.
177177
root = std::make_unique<TrialStage>(opCtx,
178178
ws.get(),

src/mongo/db/query/get_executor.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,12 +382,14 @@ StatusWith<PrepareExecutionResult> prepareExecution(OperationContext* opCtx,
382382

383383
// Might have to filter out orphaned docs.
384384
if (plannerParams.options & QueryPlannerParams::INCLUDE_SHARD_FILTER) {
385+
auto wantShardName = canonicalQuery->metadataDeps()[DocumentMetadataFields::kShardName];
385386
root = std::make_unique<ShardFilterStage>(
386387
opCtx,
387388
CollectionShardingState::get(opCtx, canonicalQuery->nss())
388389
->getOrphansFilter(opCtx, collection),
389390
ws,
390-
std::move(root));
391+
std::move(root),
392+
wantShardName);
391393
}
392394

393395
const auto* cqProjection = canonicalQuery->getProj();

src/mongo/db/query/planner_analysis.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -384,12 +384,6 @@ std::unique_ptr<ProjectionNode> analyzeProjection(const CanonicalQuery& query,
384384
}
385385
}
386386
}
387-
auto wantShardName = query.metadataDeps()[DocumentMetadataFields::kShardName];
388-
if (wantShardName) {
389-
auto shardNameNode = std::make_unique<ShardNameNode>();
390-
shardNameNode->children.push_back(solnRoot.release());
391-
solnRoot = std::move(shardNameNode);
392-
}
393387
return std::make_unique<ProjectionNodeDefault>(
394388
addSortKeyGeneratorStageIfNeeded(query, hasSortStage, std::move(solnRoot)),
395389
*query.root(),
@@ -767,7 +761,8 @@ std::unique_ptr<QuerySolution> QueryPlannerAnalysis::analyzeDataAccess(
767761
}
768762
}
769763

770-
ShardingFilterNode* sfn = new ShardingFilterNode();
764+
auto wantShardName = query.metadataDeps()[DocumentMetadataFields::kShardName];
765+
ShardingFilterNode* sfn = new ShardingFilterNode(wantShardName);
771766
sfn->children.push_back(solnRoot.release());
772767
solnRoot.reset(sfn);
773768
}

src/mongo/db/query/query_solution.cpp

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,32 +1125,9 @@ QuerySolutionNode* GeoNear2DSphereNode::clone() const {
11251125
void ShardingFilterNode::appendToString(str::stream* ss, int indent) const {
11261126
addIndent(ss, indent);
11271127
*ss << "SHARDING_FILTER\n";
1128-
if (nullptr != filter) {
1129-
addIndent(ss, indent + 1);
1130-
StringBuilder sb;
1131-
*ss << "filter:\n";
1132-
filter->debugString(sb, indent + 2);
1133-
*ss << sb.str();
1128+
if (wantShardName()) {
1129+
*ss << "with shard name\n";
11341130
}
1135-
addCommon(ss, indent);
1136-
addIndent(ss, indent + 1);
1137-
*ss << "Child:" << '\n';
1138-
children[0]->appendToString(ss, indent + 2);
1139-
}
1140-
1141-
QuerySolutionNode* ShardingFilterNode::clone() const {
1142-
ShardingFilterNode* copy = new ShardingFilterNode();
1143-
cloneBaseData(copy);
1144-
return copy;
1145-
}
1146-
1147-
//
1148-
// ShardNameNode
1149-
//
1150-
1151-
void ShardNameNode::appendToString(str::stream* ss, int indent) const {
1152-
addIndent(ss, indent);
1153-
*ss << "SHARD_NAME\n";
11541131
if (nullptr != filter) {
11551132
addIndent(ss, indent + 1);
11561133
StringBuilder sb;
@@ -1164,8 +1141,8 @@ void ShardNameNode::appendToString(str::stream* ss, int indent) const {
11641141
children[0]->appendToString(ss, indent + 2);
11651142
}
11661143

1167-
QuerySolutionNode* ShardNameNode::clone() const {
1168-
ShardNameNode* copy = new ShardNameNode();
1144+
QuerySolutionNode* ShardingFilterNode::clone() const {
1145+
ShardingFilterNode* copy = new ShardingFilterNode(this->wantShardName());
11691146
cloneBaseData(copy);
11701147
return copy;
11711148
}

src/mongo/db/query/query_solution.h

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ struct GeoNear2DSphereNode : public QuerySolutionNode {
915915
* through the pipeline.
916916
*/
917917
struct ShardingFilterNode : public QuerySolutionNode {
918-
ShardingFilterNode() {}
918+
ShardingFilterNode(bool wantShardName): _wantShardName(wantShardName) {}
919919
virtual ~ShardingFilterNode() {}
920920

921921
virtual StageType getType() const {
@@ -935,37 +935,14 @@ struct ShardingFilterNode : public QuerySolutionNode {
935935
const BSONObjSet& getSort() const {
936936
return children[0]->getSort();
937937
}
938-
939-
QuerySolutionNode* clone() const;
940-
};
941-
942-
/**
943-
* If we're answering a query on a that has shardName metadata, docs must be appended with
944-
* the shard name.
945-
*/
946-
struct ShardNameNode : public QuerySolutionNode {
947-
ShardNameNode() {}
948-
virtual ~ShardNameNode() {}
949-
950-
virtual StageType getType() const {
951-
return STAGE_SHARD_NAME;
952-
}
953-
virtual void appendToString(str::stream* ss, int indent) const;
954-
955-
bool fetched() const {
956-
return children[0]->fetched();
957-
}
958-
bool hasField(const std::string& field) const {
959-
return children[0]->hasField(field);
960-
}
961-
bool sortedByDiskLoc() const {
962-
return children[0]->sortedByDiskLoc();
963-
}
964-
const BSONObjSet& getSort() const {
965-
return children[0]->getSort();
938+
bool wantShardName() const {
939+
return _wantShardName;
966940
}
967941

968942
QuerySolutionNode* clone() const;
943+
944+
private:
945+
bool _wantShardName;
969946
};
970947

971948
/**

0 commit comments

Comments
 (0)