diff options
Diffstat (limited to 'src/mongo')
-rw-r--r-- | src/mongo/db/commands/mr.cpp | 10 | ||||
-rw-r--r-- | src/mongo/db/exec/shard_filter.cpp | 4 | ||||
-rw-r--r-- | src/mongo/db/exec/shard_filter.h | 2 | ||||
-rw-r--r-- | src/mongo/db/exec/shard_filterer_impl.cpp | 14 | ||||
-rw-r--r-- | src/mongo/db/exec/shard_filterer_impl.h | 6 | ||||
-rw-r--r-- | src/mongo/db/pipeline/pipeline_d.cpp | 10 | ||||
-rw-r--r-- | src/mongo/db/pipeline/process_interface_shardsvr.cpp | 6 | ||||
-rw-r--r-- | src/mongo/db/query/get_executor.cpp | 2 | ||||
-rw-r--r-- | src/mongo/db/query/stage_builder.cpp | 2 | ||||
-rw-r--r-- | src/mongo/db/s/collection_metadata_filtering_test.cpp | 93 | ||||
-rw-r--r-- | src/mongo/db/s/collection_sharding_runtime.cpp | 2 | ||||
-rw-r--r-- | src/mongo/db/s/collection_sharding_runtime.h | 2 | ||||
-rw-r--r-- | src/mongo/db/s/collection_sharding_state.h | 11 | ||||
-rw-r--r-- | src/mongo/db/s/collection_sharding_state_factory_embedded.cpp | 2 | ||||
-rw-r--r-- | src/mongo/db/s/op_observer_sharding_impl.cpp | 9 | ||||
-rw-r--r-- | src/mongo/db/s/scoped_collection_metadata.h | 31 |
16 files changed, 132 insertions, 74 deletions
diff --git a/src/mongo/db/commands/mr.cpp b/src/mongo/db/commands/mr.cpp index 8bc31f78e46..006969061c8 100644 --- a/src/mongo/db/commands/mr.cpp +++ b/src/mongo/db/commands/mr.cpp @@ -1415,10 +1415,10 @@ bool runMapReduce(OperationContext* opCtx, uassert(16149, "cannot run map reduce without the js engine", getGlobalScriptEngine()); - const auto metadata = [&] { + const auto collectionFilter = [&] { AutoGetCollectionForReadCommand autoColl(opCtx, config.nss); return CollectionShardingState::get(opCtx, config.nss) - ->getOrphansFilter(opCtx, autoColl.getCollection()); + ->getOwnershipFilter(opCtx, autoColl.getCollection()); }(); bool shouldHaveData = false; @@ -1513,9 +1513,9 @@ bool runMapReduce(OperationContext* opCtx, // Check to see if this is a new object we don't own yet because of a chunk // migration - if (metadata->isSharded()) { - ShardKeyPattern kp(metadata->getKeyPattern()); - if (!metadata->keyBelongsToMe(kp.extractShardKeyFromDoc(o))) { + if (collectionFilter.isSharded()) { + ShardKeyPattern kp(collectionFilter.getKeyPattern()); + if (!collectionFilter.keyBelongsToMe(kp.extractShardKeyFromDoc(o))) { continue; } } diff --git a/src/mongo/db/exec/shard_filter.cpp b/src/mongo/db/exec/shard_filter.cpp index c670ef205b2..fe2435a54fb 100644 --- a/src/mongo/db/exec/shard_filter.cpp +++ b/src/mongo/db/exec/shard_filter.cpp @@ -51,10 +51,10 @@ using std::vector; const char* ShardFilterStage::kStageType = "SHARDING_FILTER"; ShardFilterStage::ShardFilterStage(OperationContext* opCtx, - ScopedCollectionMetadata metadata, + ScopedCollectionFilter collectionFilter, WorkingSet* ws, std::unique_ptr<PlanStage> child) - : PlanStage(kStageType, opCtx), _ws(ws), _shardFilterer(std::move(metadata)) { + : PlanStage(kStageType, opCtx), _ws(ws), _shardFilterer(std::move(collectionFilter)) { _children.emplace_back(std::move(child)); } diff --git a/src/mongo/db/exec/shard_filter.h b/src/mongo/db/exec/shard_filter.h index 5fdb2a2a247..b902e3f54ee 100644 --- a/src/mongo/db/exec/shard_filter.h +++ b/src/mongo/db/exec/shard_filter.h @@ -72,7 +72,7 @@ namespace mongo { class ShardFilterStage final : public PlanStage { public: ShardFilterStage(OperationContext* opCtx, - ScopedCollectionMetadata metadata, + ScopedCollectionFilter collectionFilter, WorkingSet* ws, std::unique_ptr<PlanStage> child); ~ShardFilterStage(); diff --git a/src/mongo/db/exec/shard_filterer_impl.cpp b/src/mongo/db/exec/shard_filterer_impl.cpp index cbd7947903c..dbe4c057c7d 100644 --- a/src/mongo/db/exec/shard_filterer_impl.cpp +++ b/src/mongo/db/exec/shard_filterer_impl.cpp @@ -37,9 +37,9 @@ namespace mongo { -ShardFiltererImpl::ShardFiltererImpl(ScopedCollectionMetadata md) : _metadata(std::move(md)) { - if (_metadata->isSharded()) { - _keyPattern = ShardKeyPattern(_metadata->getKeyPattern()); +ShardFiltererImpl::ShardFiltererImpl(ScopedCollectionFilter cf) : _collectionFilter(std::move(cf)) { + if (_collectionFilter.isSharded()) { + _keyPattern = ShardKeyPattern(_collectionFilter.getKeyPattern()); } } @@ -49,14 +49,14 @@ ShardFilterer::DocumentBelongsResult ShardFiltererImpl::_shardKeyBelongsToMe( return DocumentBelongsResult::kNoShardKey; } - return _metadata->keyBelongsToMe(shardKey) ? DocumentBelongsResult::kBelongs - : DocumentBelongsResult::kDoesNotBelong; + return _collectionFilter.keyBelongsToMe(shardKey) ? DocumentBelongsResult::kBelongs + : DocumentBelongsResult::kDoesNotBelong; } ShardFilterer::DocumentBelongsResult ShardFiltererImpl::documentBelongsToMe( const WorkingSetMember& wsm) const { - if (!_metadata->isSharded()) { + if (!_collectionFilter.isSharded()) { return DocumentBelongsResult::kBelongs; } @@ -76,7 +76,7 @@ ShardFilterer::DocumentBelongsResult ShardFiltererImpl::documentBelongsToMe( ShardFilterer::DocumentBelongsResult ShardFiltererImpl::documentBelongsToMe( const Document& doc) const { - if (!_metadata->isSharded()) { + if (!_collectionFilter.isSharded()) { return DocumentBelongsResult::kBelongs; } return _shardKeyBelongsToMe(_keyPattern->extractShardKeyFromDoc(doc.toBson())); diff --git a/src/mongo/db/exec/shard_filterer_impl.h b/src/mongo/db/exec/shard_filterer_impl.h index 7808f821d8c..c0ae10529d1 100644 --- a/src/mongo/db/exec/shard_filterer_impl.h +++ b/src/mongo/db/exec/shard_filterer_impl.h @@ -37,13 +37,13 @@ namespace mongo { class ShardFiltererImpl : public ShardFilterer { public: - ShardFiltererImpl(ScopedCollectionMetadata md); + ShardFiltererImpl(ScopedCollectionFilter collectionFilter); DocumentBelongsResult documentBelongsToMe(const WorkingSetMember& wsm) const override; DocumentBelongsResult documentBelongsToMe(const Document& doc) const override; bool isCollectionSharded() const override { - return _metadata->isSharded(); + return _collectionFilter.isSharded(); } const KeyPattern& getKeyPattern() const override { @@ -53,7 +53,7 @@ public: private: DocumentBelongsResult _shardKeyBelongsToMe(BSONObj shardKey) const; - ScopedCollectionMetadata _metadata; + ScopedCollectionFilter _collectionFilter; boost::optional<ShardKeyPattern> _keyPattern; }; } // namespace mongo diff --git a/src/mongo/db/pipeline/pipeline_d.cpp b/src/mongo/db/pipeline/pipeline_d.cpp index f8a3132b061..1358b6a9857 100644 --- a/src/mongo/db/pipeline/pipeline_d.cpp +++ b/src/mongo/db/pipeline/pipeline_d.cpp @@ -129,14 +129,14 @@ StatusWith<unique_ptr<PlanExecutor, PlanExecutor::Deleter>> createRandomCursorEx // If the incoming operation is sharded, use the CSS to infer the filtering metadata for the // collection, otherwise treat it as unsharded - auto shardMetadata = - CollectionShardingState::get(opCtx, coll->ns())->getOrphansFilter(opCtx, coll); + auto collectionFilter = + CollectionShardingState::get(opCtx, coll->ns())->getOwnershipFilter(opCtx, coll); // Because 'numRecords' includes orphan documents, our initial decision to optimize the $sample // cursor may have been mistaken. For sharded collections, build a TRIAL plan that will switch // to a collection scan if the ratio of orphaned to owned documents encountered over the first // 100 works() is such that we would have chosen not to optimize. - if (shardMetadata->isSharded()) { + if (collectionFilter.isSharded()) { // The ratio of owned to orphaned documents must be at least equal to the ratio between the // requested sampleSize and the maximum permitted sampleSize for the original constraints to // be satisfied. For instance, if there are 200 documents and the sampleSize is 5, then at @@ -147,12 +147,12 @@ StatusWith<unique_ptr<PlanExecutor, PlanExecutor::Deleter>> createRandomCursorEx sampleSize / (numRecords * kMaxSampleRatioForRandCursor), kMaxSampleRatioForRandCursor); // The trial plan is SHARDING_FILTER-MULTI_ITERATOR. auto randomCursorPlan = - std::make_unique<ShardFilterStage>(opCtx, shardMetadata, ws.get(), std::move(root)); + std::make_unique<ShardFilterStage>(opCtx, collectionFilter, ws.get(), std::move(root)); // The backup plan is SHARDING_FILTER-COLLSCAN. std::unique_ptr<PlanStage> collScanPlan = std::make_unique<CollectionScan>( opCtx, coll, CollectionScanParams{}, ws.get(), nullptr); collScanPlan = std::make_unique<ShardFilterStage>( - opCtx, shardMetadata, ws.get(), std::move(collScanPlan)); + opCtx, collectionFilter, ws.get(), std::move(collScanPlan)); // Place a TRIAL stage at the root of the plan tree, and pass it the trial and backup plans. root = std::make_unique<TrialStage>(opCtx, ws.get(), diff --git a/src/mongo/db/pipeline/process_interface_shardsvr.cpp b/src/mongo/db/pipeline/process_interface_shardsvr.cpp index 096c43cdaa0..562cd16fe3a 100644 --- a/src/mongo/db/pipeline/process_interface_shardsvr.cpp +++ b/src/mongo/db/pipeline/process_interface_shardsvr.cpp @@ -209,9 +209,9 @@ unique_ptr<Pipeline, PipelineDeleter> MongoInterfaceShardServer::attachCursorSou std::unique_ptr<ShardFilterer> MongoInterfaceShardServer::getShardFilterer( const boost::intrusive_ptr<ExpressionContext>& expCtx) const { const bool aggNsIsCollection = expCtx->uuid != boost::none; - auto shardingMetadata = CollectionShardingState::get(expCtx->opCtx, expCtx->ns) - ->getOrphansFilter(expCtx->opCtx, aggNsIsCollection); - return std::make_unique<ShardFiltererImpl>(std::move(shardingMetadata)); + auto collectionFilter = CollectionShardingState::get(expCtx->opCtx, expCtx->ns) + ->getOwnershipFilter(expCtx->opCtx, aggNsIsCollection); + return std::make_unique<ShardFiltererImpl>(std::move(collectionFilter)); } void MongoInterfaceShardServer::renameIfOptionsAndIndexesHaveNotChanged( diff --git a/src/mongo/db/query/get_executor.cpp b/src/mongo/db/query/get_executor.cpp index 3dcd0e2aa40..a658397ec1a 100644 --- a/src/mongo/db/query/get_executor.cpp +++ b/src/mongo/db/query/get_executor.cpp @@ -389,7 +389,7 @@ StatusWith<PrepareExecutionResult> prepareExecution(OperationContext* opCtx, root = std::make_unique<ShardFilterStage>( opCtx, CollectionShardingState::get(opCtx, canonicalQuery->nss()) - ->getOrphansFilter(opCtx, collection), + ->getOwnershipFilter(opCtx, collection), ws, std::move(root)); } diff --git a/src/mongo/db/query/stage_builder.cpp b/src/mongo/db/query/stage_builder.cpp index 8f5906716c3..751ac0d4d2b 100644 --- a/src/mongo/db/query/stage_builder.cpp +++ b/src/mongo/db/query/stage_builder.cpp @@ -285,7 +285,7 @@ std::unique_ptr<PlanStage> buildStages(OperationContext* opCtx, auto css = CollectionShardingState::get(opCtx, collection->ns()); return std::make_unique<ShardFilterStage>( - opCtx, css->getOrphansFilter(opCtx, collection), ws, std::move(childStage)); + opCtx, css->getOwnershipFilter(opCtx, collection), ws, std::move(childStage)); } case STAGE_DISTINCT_SCAN: { const DistinctNode* dn = static_cast<const DistinctNode*>(root); diff --git a/src/mongo/db/s/collection_metadata_filtering_test.cpp b/src/mongo/db/s/collection_metadata_filtering_test.cpp index a9c251f325f..8cbad7290e8 100644 --- a/src/mongo/db/s/collection_metadata_filtering_test.cpp +++ b/src/mongo/db/s/collection_metadata_filtering_test.cpp @@ -122,14 +122,14 @@ protected: TEST_F(CollectionMetadataFilteringTest, FilterDocumentsInTheFuture) { prepareTestData(); - const auto testFn = [](const ScopedCollectionMetadata& metadata) { - ASSERT_TRUE(metadata->keyBelongsToMe(BSON("_id" << -500))); - ASSERT_TRUE(metadata->keyBelongsToMe(BSON("_id" << 50))); - ASSERT_FALSE(metadata->keyBelongsToMe(BSON("_id" << -50))); - ASSERT_FALSE(metadata->keyBelongsToMe(BSON("_id" << 500))); - }; - { + const auto testFilterFn = [](const ScopedCollectionFilter& collectionFilter) { + ASSERT_TRUE(collectionFilter.keyBelongsToMe(BSON("_id" << -500))); + ASSERT_TRUE(collectionFilter.keyBelongsToMe(BSON("_id" << 50))); + ASSERT_FALSE(collectionFilter.keyBelongsToMe(BSON("_id" << -50))); + ASSERT_FALSE(collectionFilter.keyBelongsToMe(BSON("_id" << 500))); + }; + BSONObj readConcern = BSON("readConcern" << BSON("level" << "snapshot" << "atClusterTime" << Timestamp(100, 0))); @@ -139,10 +139,17 @@ TEST_F(CollectionMetadataFilteringTest, FilterDocumentsInTheFuture) { AutoGetCollection autoColl(operationContext(), kNss, MODE_IS); auto* const css = CollectionShardingState::get(operationContext(), kNss); - testFn(css->getOrphansFilter(operationContext(), true /* isCollection */)); + testFilterFn(css->getOwnershipFilter(operationContext(), true /* isCollection */)); } { + const auto testFn = [](const ScopedCollectionMetadata& metadata) { + ASSERT_TRUE(metadata->keyBelongsToMe(BSON("_id" << -500))); + ASSERT_TRUE(metadata->keyBelongsToMe(BSON("_id" << 50))); + ASSERT_FALSE(metadata->keyBelongsToMe(BSON("_id" << -50))); + ASSERT_FALSE(metadata->keyBelongsToMe(BSON("_id" << 500))); + }; + const auto scm = _manager->getActiveMetadata(LogicalTime(Timestamp(100, 0))); testFn(scm); } @@ -152,14 +159,14 @@ TEST_F(CollectionMetadataFilteringTest, FilterDocumentsInTheFuture) { TEST_F(CollectionMetadataFilteringTest, FilterDocumentsInThePast) { prepareTestData(); - const auto testFn = [](const ScopedCollectionMetadata& metadata) { - ASSERT_FALSE(metadata->keyBelongsToMe(BSON("_id" << -500))); - ASSERT_FALSE(metadata->keyBelongsToMe(BSON("_id" << 50))); - ASSERT_TRUE(metadata->keyBelongsToMe(BSON("_id" << -50))); - ASSERT_TRUE(metadata->keyBelongsToMe(BSON("_id" << 500))); - }; - { + const auto testFilterFn = [](const ScopedCollectionFilter& collectionFilter) { + ASSERT_FALSE(collectionFilter.keyBelongsToMe(BSON("_id" << -500))); + ASSERT_FALSE(collectionFilter.keyBelongsToMe(BSON("_id" << 50))); + ASSERT_TRUE(collectionFilter.keyBelongsToMe(BSON("_id" << -50))); + ASSERT_TRUE(collectionFilter.keyBelongsToMe(BSON("_id" << 500))); + }; + BSONObj readConcern = BSON("readConcern" << BSON("level" << "snapshot" << "atClusterTime" << Timestamp(50, 0))); @@ -169,10 +176,17 @@ TEST_F(CollectionMetadataFilteringTest, FilterDocumentsInThePast) { AutoGetCollection autoColl(operationContext(), kNss, MODE_IS); auto* const css = CollectionShardingState::get(operationContext(), kNss); - testFn(css->getOrphansFilter(operationContext(), true /* isCollection */)); + testFilterFn(css->getOwnershipFilter(operationContext(), true /* isCollection */)); } { + const auto testFn = [](const ScopedCollectionMetadata& metadata) { + ASSERT_FALSE(metadata->keyBelongsToMe(BSON("_id" << -500))); + ASSERT_FALSE(metadata->keyBelongsToMe(BSON("_id" << 50))); + ASSERT_TRUE(metadata->keyBelongsToMe(BSON("_id" << -50))); + ASSERT_TRUE(metadata->keyBelongsToMe(BSON("_id" << 500))); + }; + const auto scm = _manager->getActiveMetadata(LogicalTime(Timestamp(50, 0))); testFn(scm); } @@ -182,22 +196,22 @@ TEST_F(CollectionMetadataFilteringTest, FilterDocumentsInThePast) { TEST_F(CollectionMetadataFilteringTest, FilterDocumentsTooFarInThePastThrowsStaleChunkHistory) { prepareTestData(); - const auto testFn = [](const ScopedCollectionMetadata& metadata) { - ASSERT_THROWS_CODE(metadata->keyBelongsToMe(BSON("_id" << -500)), - AssertionException, - ErrorCodes::StaleChunkHistory); - ASSERT_THROWS_CODE(metadata->keyBelongsToMe(BSON("_id" << 50)), - AssertionException, - ErrorCodes::StaleChunkHistory); - ASSERT_THROWS_CODE(metadata->keyBelongsToMe(BSON("_id" << -50)), - AssertionException, - ErrorCodes::StaleChunkHistory); - ASSERT_THROWS_CODE(metadata->keyBelongsToMe(BSON("_id" << 500)), - AssertionException, - ErrorCodes::StaleChunkHistory); - }; - { + const auto testFilterFn = [](const ScopedCollectionFilter& collectionFilter) { + ASSERT_THROWS_CODE(collectionFilter.keyBelongsToMe(BSON("_id" << -500)), + AssertionException, + ErrorCodes::StaleChunkHistory); + ASSERT_THROWS_CODE(collectionFilter.keyBelongsToMe(BSON("_id" << 50)), + AssertionException, + ErrorCodes::StaleChunkHistory); + ASSERT_THROWS_CODE(collectionFilter.keyBelongsToMe(BSON("_id" << -50)), + AssertionException, + ErrorCodes::StaleChunkHistory); + ASSERT_THROWS_CODE(collectionFilter.keyBelongsToMe(BSON("_id" << 500)), + AssertionException, + ErrorCodes::StaleChunkHistory); + }; + BSONObj readConcern = BSON("readConcern" << BSON("level" << "snapshot" << "atClusterTime" << Timestamp(10, 0))); @@ -207,10 +221,25 @@ TEST_F(CollectionMetadataFilteringTest, FilterDocumentsTooFarInThePastThrowsStal AutoGetCollection autoColl(operationContext(), kNss, MODE_IS); auto* const css = CollectionShardingState::get(operationContext(), kNss); - testFn(css->getOrphansFilter(operationContext(), true /* isCollection */)); + testFilterFn(css->getOwnershipFilter(operationContext(), true /* isCollection */)); } { + const auto testFn = [](const ScopedCollectionMetadata& metadata) { + ASSERT_THROWS_CODE(metadata->keyBelongsToMe(BSON("_id" << -500)), + AssertionException, + ErrorCodes::StaleChunkHistory); + ASSERT_THROWS_CODE(metadata->keyBelongsToMe(BSON("_id" << 50)), + AssertionException, + ErrorCodes::StaleChunkHistory); + ASSERT_THROWS_CODE(metadata->keyBelongsToMe(BSON("_id" << -50)), + AssertionException, + ErrorCodes::StaleChunkHistory); + ASSERT_THROWS_CODE(metadata->keyBelongsToMe(BSON("_id" << 500)), + AssertionException, + ErrorCodes::StaleChunkHistory); + }; + const auto scm = _manager->getActiveMetadata(LogicalTime(Timestamp(10, 0))); testFn(scm); } diff --git a/src/mongo/db/s/collection_sharding_runtime.cpp b/src/mongo/db/s/collection_sharding_runtime.cpp index bef293677d8..81d650cf537 100644 --- a/src/mongo/db/s/collection_sharding_runtime.cpp +++ b/src/mongo/db/s/collection_sharding_runtime.cpp @@ -124,7 +124,7 @@ CollectionShardingRuntime* CollectionShardingRuntime::get_UNSAFE(ServiceContext* return checked_cast<CollectionShardingRuntime*>(css); } -ScopedCollectionMetadata CollectionShardingRuntime::getOrphansFilter(OperationContext* opCtx, +ScopedCollectionFilter CollectionShardingRuntime::getOwnershipFilter(OperationContext* opCtx, bool isCollection) { const auto atClusterTime = repl::ReadConcernArgs::get(opCtx).getArgsAtClusterTime(); auto optMetadata = _getMetadataWithVersionCheckAt(opCtx, atClusterTime, isCollection); diff --git a/src/mongo/db/s/collection_sharding_runtime.h b/src/mongo/db/s/collection_sharding_runtime.h index 9e0e3a7f9e4..b817ca582f5 100644 --- a/src/mongo/db/s/collection_sharding_runtime.h +++ b/src/mongo/db/s/collection_sharding_runtime.h @@ -82,7 +82,7 @@ public: const UUID& collectionUuid, ChunkRange orphanRange); - ScopedCollectionMetadata getOrphansFilter(OperationContext* opCtx, bool isCollection) override; + ScopedCollectionFilter getOwnershipFilter(OperationContext* opCtx, bool isCollection) override; ScopedCollectionMetadata getCurrentMetadata() override; diff --git a/src/mongo/db/s/collection_sharding_state.h b/src/mongo/db/s/collection_sharding_state.h index 30fac48cedc..981fb4d3ffb 100644 --- a/src/mongo/db/s/collection_sharding_state.h +++ b/src/mongo/db/s/collection_sharding_state.h @@ -87,27 +87,28 @@ public: static void report(OperationContext* opCtx, BSONObjBuilder* builder); /** - * Returns the orphan chunk filtering metadata that the current operation should be using for + * Returns the chunk filtering object that the current operation should be using for * the collection. * - * If the operation context contains an 'atClusterTime', the returned filtering metadata will be + * If the operation context contains an 'atClusterTime', the returned filtering object will be * tied to a specific point in time. Otherwise, it will reference the latest time available. If * the operation is not associated with a shard version (refer to * OperationShardingState::isOperationVersioned for more info on that), returns an UNSHARDED * metadata object. * - * The intended users of this method are callers which need to perform orphan filtering. Use + * The intended users of this method are callers which need to perform filtering. Use * 'getCurrentMetadata' for other cases, like obtaining information about sharding-related * properties of the collection are necessary that won't change under collection IX/IS lock * (e.g., isSharded or the shard key). * * The returned object is safe to access even after the collection lock has been dropped. */ - virtual ScopedCollectionMetadata getOrphansFilter(OperationContext* opCtx, + + virtual ScopedCollectionFilter getOwnershipFilter(OperationContext* opCtx, bool isCollection) = 0; /** - * See the comments for 'getOrphansFilter' above for more information on this method. + * See the comments for 'getOwnershipFilter' above for more information on this method. */ virtual ScopedCollectionMetadata getCurrentMetadata() = 0; diff --git a/src/mongo/db/s/collection_sharding_state_factory_embedded.cpp b/src/mongo/db/s/collection_sharding_state_factory_embedded.cpp index 95614739d0d..81a0a033afd 100644 --- a/src/mongo/db/s/collection_sharding_state_factory_embedded.cpp +++ b/src/mongo/db/s/collection_sharding_state_factory_embedded.cpp @@ -53,7 +53,7 @@ const auto kUnshardedCollection = std::make_shared<UnshardedCollection>(); class CollectionShardingStateStandalone final : public CollectionShardingState { public: - ScopedCollectionMetadata getOrphansFilter(OperationContext*, bool) override { + ScopedCollectionFilter getOwnershipFilter(OperationContext*, bool) override { return {kUnshardedCollection}; } diff --git a/src/mongo/db/s/op_observer_sharding_impl.cpp b/src/mongo/db/s/op_observer_sharding_impl.cpp index ab0022dd860..7a815b67c4a 100644 --- a/src/mongo/db/s/op_observer_sharding_impl.cpp +++ b/src/mongo/db/s/op_observer_sharding_impl.cpp @@ -55,15 +55,14 @@ void assertIntersectingChunkHasNotMoved(OperationContext* opCtx, if (!repl::ReadConcernArgs::get(opCtx).getArgsAtClusterTime()) return; - const auto metadata = csr->getOrphansFilter(opCtx, true /* isCollection */); - if (!metadata->isSharded()) + const auto collectionFilter = csr->getOwnershipFilter(opCtx, true /* isCollection */); + if (!collectionFilter.isSharded()) return; - auto chunkManager = metadata->getChunkManager(); - auto shardKey = chunkManager->getShardKeyPattern().extractShardKeyFromDoc(doc); + auto shardKey = collectionFilter.extractShardKeyFromDoc(doc); // We can assume the simple collation because shard keys do not support non-simple collations. - auto chunk = chunkManager->findIntersectingChunkWithSimpleCollation(shardKey); + auto chunk = collectionFilter.findIntersectingChunkWithSimpleCollation(shardKey); // Throws if the chunk has moved since the timestamp of the running transaction's atClusterTime // read concern parameter. diff --git a/src/mongo/db/s/scoped_collection_metadata.h b/src/mongo/db/s/scoped_collection_metadata.h index 31ef088066f..946170074c4 100644 --- a/src/mongo/db/s/scoped_collection_metadata.h +++ b/src/mongo/db/s/scoped_collection_metadata.h @@ -64,8 +64,37 @@ public: return get(); } -private: + bool isSharded() const { + return _impl->get().isSharded(); + } + + const BSONObj& getKeyPattern() const { + return _impl->get().getKeyPattern(); + } + + const BSONObj extractShardKeyFromDoc(const BSONObj& doc) const { + return _impl->get().getChunkManager()->getShardKeyPattern().extractShardKeyFromDoc(doc); + } + +protected: std::shared_ptr<Impl> _impl; }; +class ScopedCollectionFilter : public ScopedCollectionMetadata { +public: + ScopedCollectionFilter(std::shared_ptr<Impl> impl) + : ScopedCollectionMetadata(std::move(impl)) {} + + ScopedCollectionFilter(ScopedCollectionMetadata&& scopedMetadata) + : ScopedCollectionMetadata(std::move(scopedMetadata)) {} + + bool keyBelongsToMe(const BSONObj& key) const { + return _impl->get().keyBelongsToMe(key); + } + + Chunk findIntersectingChunkWithSimpleCollation(const BSONObj& shardKey) const { + return _impl->get().getChunkManager()->findIntersectingChunkWithSimpleCollation(shardKey); + } +}; + } // namespace mongo |