diff options
author | Marcos José Grillo Ramirez <marcos.grillo@mongodb.com> | 2021-10-15 11:20:01 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2021-10-18 13:22:40 +0000 |
commit | 0c95864a269d7a66223eafe07906f8c87dacc1b7 (patch) | |
tree | dd1f333db24d830edee03cbcd016b012ad1f1ff5 | |
parent | f3c95248ef4a080a423af14dd5d08d6eb6b0c8e6 (diff) | |
download | mongo-0c95864a269d7a66223eafe07906f8c87dacc1b7.tar.gz |
SERVER-60420 Release the critical section early in the shard collection slow path
(cherry picked from commit e7c0fc2c3eac5d5fd35b29fcc7d1d5d52946c293)
-rw-r--r-- | src/mongo/db/s/create_collection_coordinator.cpp | 22 | ||||
-rw-r--r-- | src/mongo/db/s/create_collection_coordinator.h | 7 |
2 files changed, 22 insertions, 7 deletions
diff --git a/src/mongo/db/s/create_collection_coordinator.cpp b/src/mongo/db/s/create_collection_coordinator.cpp index 1b0621f2a94..f9892052a46 100644 --- a/src/mongo/db/s/create_collection_coordinator.cpp +++ b/src/mongo/db/s/create_collection_coordinator.cpp @@ -512,18 +512,16 @@ ExecutorFuture<void> CreateCollectionCoordinator::_runImpl( } } - _collectionEmpty = checkIfCollectionIsEmpty(opCtx, nss()); - + _createPolicy(opCtx); _createCollectionAndIndexes(opCtx); - _createPolicyAndChunks(opCtx); - audit::logShardCollection(opCtx->getClient(), nss().ns(), *_doc.getShardKey(), _doc.getUnique().value_or(false)); if (_splitPolicy->isOptimized()) { + _createChunks(opCtx); // Block reads/writes from here on if we need to create // the collection on other shards, this way we prevent // reads/writes that should be redirected to another @@ -548,10 +546,16 @@ ExecutorFuture<void> CreateCollectionCoordinator::_runImpl( _commit(opCtx); } + // End of the critical section, from now on, read and writes are permitted. RecoverableCriticalSectionService::get(opCtx)->releaseRecoverableCriticalSection( opCtx, nss(), _critSecReason, ShardingCatalogClient::kMajorityWriteConcern); + // Slow path. Create chunks (which might incur in an index scan) and commit must be + // done outside of the critical section to prevent writes from stalling in unsharded + // collections. if (!_splitPolicy->isOptimized()) { + _createChunks(opCtx); + _commit(opCtx); } @@ -723,8 +727,10 @@ void CreateCollectionCoordinator::_createCollectionAndIndexes(OperationContext* _collectionUUID = *sharding_ddl_util::getCollectionUUID(opCtx, nss()); } -void CreateCollectionCoordinator::_createPolicyAndChunks(OperationContext* opCtx) { - LOGV2_DEBUG(5277904, 2, "Create collection _createChunks", "namespace"_attr = nss()); +void CreateCollectionCoordinator::_createPolicy(OperationContext* opCtx) { + LOGV2_DEBUG(6042001, 2, "Create collection _createPolicy", "namespace"_attr = nss()); + + _collectionEmpty = checkIfCollectionIsEmpty(opCtx, nss()); _splitPolicy = InitialSplitPolicy::calculateOptimizationStrategy( opCtx, @@ -735,6 +741,10 @@ void CreateCollectionCoordinator::_createPolicyAndChunks(OperationContext* opCtx getTagsAndValidate(opCtx, nss(), _shardKeyPattern->toBSON()), getNumShards(opCtx), *_collectionEmpty); +} + +void CreateCollectionCoordinator::_createChunks(OperationContext* opCtx) { + LOGV2_DEBUG(5277904, 2, "Create collection _createChunks", "namespace"_attr = nss()); _initialChunks = _splitPolicy->createFirstChunks( opCtx, *_shardKeyPattern, {*_collectionUUID, ShardingState::get(opCtx)->shardId()}); diff --git a/src/mongo/db/s/create_collection_coordinator.h b/src/mongo/db/s/create_collection_coordinator.h index 2ef5b0a08a4..3ca202c1778 100644 --- a/src/mongo/db/s/create_collection_coordinator.h +++ b/src/mongo/db/s/create_collection_coordinator.h @@ -103,9 +103,14 @@ private: void _createCollectionAndIndexes(OperationContext* opCtx); /** + * Creates the appropiate split policy. + */ + void _createPolicy(OperationContext* opCtx); + + /** * Given the appropiate split policy, create the initial chunks. */ - void _createPolicyAndChunks(OperationContext* opCtx); + void _createChunks(OperationContext* opCtx); /** * If the optimized path can be taken, ensure the collection is already created in all the |