diff options
author | Jordi Serra Torrens <jordi.serra-torrens@mongodb.com> | 2020-11-17 15:06:24 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2020-11-25 14:38:04 +0000 |
commit | 82adc7b5986076fba2de43fe9ce32d7c425123be (patch) | |
tree | 37cb06ff527b20f997ed49b454f13bbb198ef623 /src/mongo/db/s | |
parent | 403c9577ea2a447c2660715af6540e66da8fc4c8 (diff) | |
download | mongo-82adc7b5986076fba2de43fe9ce32d7c425123be.tar.gz |
SERVER-52766: Support timestamp in the catalog cache loader
Diffstat (limited to 'src/mongo/db/s')
10 files changed, 85 insertions, 22 deletions
diff --git a/src/mongo/db/s/config/initial_split_policy.cpp b/src/mongo/db/s/config/initial_split_policy.cpp index 60c719d521d..c7915293740 100644 --- a/src/mongo/db/s/config/initial_split_policy.cpp +++ b/src/mongo/db/s/config/initial_split_policy.cpp @@ -123,7 +123,7 @@ InitialSplitPolicy::ShardCollectionConfig createChunks(const ShardKeyPattern& sh appendChunk(params.nss, min, max, &version, validAfter, shardId, &chunks); } - return {std::move(chunks)}; + return {std::move(chunks), validAfter}; } } // namespace @@ -281,6 +281,7 @@ InitialSplitPolicy::ShardCollectionConfig SingleChunkOnPrimarySplitPolicy::creat currentTime.clusterTime().asTimestamp(), params.primaryShardId, &initialChunks.chunks); + initialChunks.creationTime = currentTime.clusterTime().asTimestamp(); return initialChunks; } @@ -434,7 +435,7 @@ InitialSplitPolicy::ShardCollectionConfig AbstractTagsBasedSplitPolicy::createFi &chunks); } - return {std::move(chunks)}; + return {std::move(chunks), validAfter}; } AbstractTagsBasedSplitPolicy::SplitInfo PresplitHashedZonesSplitPolicy::buildSplitInfoForTag( diff --git a/src/mongo/db/s/config/initial_split_policy.h b/src/mongo/db/s/config/initial_split_policy.h index 41171b3b552..48332fb929d 100644 --- a/src/mongo/db/s/config/initial_split_policy.h +++ b/src/mongo/db/s/config/initial_split_policy.h @@ -76,6 +76,7 @@ public: struct ShardCollectionConfig { std::vector<ChunkType> chunks; + Timestamp creationTime; const auto& collVersion() const { return chunks.back().getVersion(); diff --git a/src/mongo/db/s/config/sharding_catalog_manager_collection_operations.cpp b/src/mongo/db/s/config/sharding_catalog_manager_collection_operations.cpp index 24f14e1efa2..ef74db38062 100644 --- a/src/mongo/db/s/config/sharding_catalog_manager_collection_operations.cpp +++ b/src/mongo/db/s/config/sharding_catalog_manager_collection_operations.cpp @@ -54,6 +54,7 @@ #include "mongo/db/query/collation/collator_factory_interface.h" #include "mongo/db/repl/repl_client_info.h" #include "mongo/db/s/sharding_logging.h" +#include "mongo/db/vector_clock.h" #include "mongo/executor/network_interface.h" #include "mongo/executor/task_executor.h" #include "mongo/logv2/log.h" @@ -70,6 +71,7 @@ #include "mongo/s/request_types/set_shard_version_request.h" #include "mongo/s/shard_key_pattern.h" #include "mongo/s/shard_util.h" +#include "mongo/s/sharded_collections_ddl_parameters_gen.h" #include "mongo/s/write_ops/batched_command_request.h" #include "mongo/s/write_ops/batched_command_response.h" #include "mongo/transport/service_entry_point.h" @@ -584,6 +586,12 @@ void ShardingCatalogManager::refineCollectionShardKey(OperationContext* opCtx, collType.setEpoch(newEpoch); collType.setKeyPattern(newShardKeyPattern.getKeyPattern()); + if (feature_flags::gShardingFullDDLSupport.isEnabled(serverGlobalParams.featureCompatibility)) { + auto now = VectorClock::get(opCtx)->getTime(); + auto newClusterTime = now.clusterTime().asTimestamp(); + collType.setTimestamp(newClusterTime); + } + auto updateCollectionAndChunksFn = [&](OperationContext* opCtx, TxnNumber txnNumber) { // Update the config.collections entry for the given namespace. updateShardingCatalogEntryForCollectionInTxn( diff --git a/src/mongo/db/s/resharding/resharding_coordinator_service.cpp b/src/mongo/db/s/resharding/resharding_coordinator_service.cpp index be8ff0ee705..84195d92ef9 100644 --- a/src/mongo/db/s/resharding/resharding_coordinator_service.cpp +++ b/src/mongo/db/s/resharding/resharding_coordinator_service.cpp @@ -38,11 +38,13 @@ #include "mongo/db/repl/primary_only_service.h" #include "mongo/db/s/config/sharding_catalog_manager.h" #include "mongo/db/s/resharding_util.h" +#include "mongo/db/vector_clock.h" #include "mongo/logv2/log.h" #include "mongo/rpc/get_status_from_command_result.h" #include "mongo/s/catalog/type_chunk.h" #include "mongo/s/grid.h" #include "mongo/s/shard_id.h" +#include "mongo/s/sharded_collections_ddl_parameters_gen.h" #include "mongo/s/write_ops/batched_command_response.h" #include "mongo/util/future_util.h" #include "mongo/util/string_map.h" @@ -150,7 +152,8 @@ void writeToCoordinatorStateNss(OperationContext* opCtx, BSONObj createReshardingFieldsUpdateForOriginalNss( OperationContext* opCtx, const ReshardingCoordinatorDocument& coordinatorDoc, - boost::optional<OID> newCollectionEpoch) { + boost::optional<OID> newCollectionEpoch, + boost::optional<Timestamp> newCollectionTimestamp) { auto nextState = coordinatorDoc.getState(); switch (nextState) { case CoordinatorStateEnum::kPreparingToDonate: { @@ -166,19 +169,26 @@ BSONObj createReshardingFieldsUpdateForOriginalNss( << opCtx->getServiceContext()->getPreciseClockSource()->now() << CollectionType::kAllowMigrationsFieldName << false)); } - case CoordinatorStateEnum::kCommitted: + case CoordinatorStateEnum::kCommitted: { // Update the config.collections entry for the original nss to reflect // the new sharded collection. Set 'uuid' to the reshardingUUID, 'key' to the new shard - // key, and 'lastmodEpoch' to newCollectionEpoch. Also update the 'state' field in the + // key, 'lastmodEpoch' to newCollectionEpoch, and 'timestamp' to + // newCollectionTimestamp (if newCollectionTimestamp has a value; i.e. when the + // shardingFullDDLSupport feature flag is enabled). Also update the 'state' field in the // 'reshardingFields' section - return BSON("$set" << BSON( - "uuid" - << coordinatorDoc.get_id() << "key" + BSONObj setFields = + BSON("uuid" << coordinatorDoc.get_id() << "key" << coordinatorDoc.getReshardingKey().toBSON() << "lastmodEpoch" << newCollectionEpoch.get() << "lastmod" << opCtx->getServiceContext()->getPreciseClockSource()->now() << "reshardingFields.state" - << CoordinatorState_serializer(coordinatorDoc.getState()).toString())); + << CoordinatorState_serializer(coordinatorDoc.getState()).toString()); + if (newCollectionTimestamp.has_value()) { + setFields = setFields.addFields(BSON("timestamp" << newCollectionTimestamp.get())); + } + + return BSON("$set" << setFields); + } case mongo::CoordinatorStateEnum::kDone: // Remove 'reshardingFields' from the config.collections entry return BSON( @@ -199,9 +209,10 @@ BSONObj createReshardingFieldsUpdateForOriginalNss( void updateConfigCollectionsForOriginalNss(OperationContext* opCtx, const ReshardingCoordinatorDocument& coordinatorDoc, boost::optional<OID> newCollectionEpoch, + boost::optional<Timestamp> newCollectionTimestamp, TxnNumber txnNumber) { - auto writeOp = - createReshardingFieldsUpdateForOriginalNss(opCtx, coordinatorDoc, newCollectionEpoch); + auto writeOp = createReshardingFieldsUpdateForOriginalNss( + opCtx, coordinatorDoc, newCollectionEpoch, newCollectionTimestamp); auto request = buildUpdateOp(CollectionType::ConfigNS, @@ -525,7 +536,8 @@ void persistInitialStateAndCatalogUpdates(OperationContext* opCtx, // Update the config.collections entry for the original collection to include // 'reshardingFields' - updateConfigCollectionsForOriginalNss(opCtx, coordinatorDoc, boost::none, txnNumber); + updateConfigCollectionsForOriginalNss( + opCtx, coordinatorDoc, boost::none, boost::none, txnNumber); // Insert the config.collections entry for the temporary resharding collection. The // chunks all have the same epoch, so picking the last chunk here is arbitrary. @@ -541,7 +553,8 @@ void persistInitialStateAndCatalogUpdates(OperationContext* opCtx, void persistCommittedState(OperationContext* opCtx, const ReshardingCoordinatorDocument& coordinatorDoc, - OID newCollectionEpoch) { + OID newCollectionEpoch, + boost::optional<Timestamp> newCollectionTimestamp) { executeStateTransitionAndMetadataChangesInTxn( opCtx, coordinatorDoc, [&](OperationContext* opCtx, TxnNumber txnNumber) { // Update the config.reshardingOperations entry @@ -554,7 +567,7 @@ void persistCommittedState(OperationContext* opCtx, // Update the config.collections entry for the original namespace to reflect the new // shard key, new epoch, and new UUID updateConfigCollectionsForOriginalNss( - opCtx, coordinatorDoc, newCollectionEpoch, txnNumber); + opCtx, coordinatorDoc, newCollectionEpoch, newCollectionTimestamp, txnNumber); // Remove all chunk and tag documents associated with the original collection, then // update the chunk and tag docs currently associated with the temp nss to be associated @@ -579,7 +592,8 @@ void persistStateTransitionAndCatalogUpdatesThenBumpShardVersions( writeToCoordinatorStateNss(opCtx, coordinatorDoc, txnNumber); // Update the config.collections entry for the original collection - updateConfigCollectionsForOriginalNss(opCtx, coordinatorDoc, boost::none, txnNumber); + updateConfigCollectionsForOriginalNss( + opCtx, coordinatorDoc, boost::none, boost::none, txnNumber); // Update the config.collections entry for the temporary resharding collection. If we've // already committed this operation, we've removed the entry for the temporary @@ -611,7 +625,8 @@ void removeCoordinatorDocAndReshardingFields(OperationContext* opCtx, writeToCoordinatorStateNss(opCtx, coordinatorDoc, txnNumber); // Remove the resharding fields from the config.collections entry - updateConfigCollectionsForOriginalNss(opCtx, coordinatorDoc, boost::none, txnNumber); + updateConfigCollectionsForOriginalNss( + opCtx, coordinatorDoc, boost::none, boost::none, txnNumber); }); } } // namespace resharding @@ -848,10 +863,17 @@ Future<void> ReshardingCoordinatorService::ReshardingCoordinator::_commit( auto opCtx = cc().makeOperationContext(); - // The new epoch to use for the resharded collection to indicate that the collection is a - // new incarnation of the namespace + // The new epoch and timestamp to use for the resharded collection to indicate that the + // collection is a new incarnation of the namespace auto newCollectionEpoch = OID::gen(); - resharding::persistCommittedState(opCtx.get(), updatedCoordinatorDoc, newCollectionEpoch); + boost::optional<Timestamp> newCollectionTimestamp; + if (feature_flags::gShardingFullDDLSupport.isEnabled(serverGlobalParams.featureCompatibility)) { + auto now = VectorClock::get(opCtx.get())->getTime(); + newCollectionTimestamp = now.clusterTime().asTimestamp(); + } + + resharding::persistCommittedState( + opCtx.get(), updatedCoordinatorDoc, newCollectionEpoch, newCollectionTimestamp); // Update the in memory state _coordinatorDoc = updatedCoordinatorDoc; diff --git a/src/mongo/db/s/resharding/resharding_coordinator_service.h b/src/mongo/db/s/resharding/resharding_coordinator_service.h index 833a9f4450d..1bb6c17f9d9 100644 --- a/src/mongo/db/s/resharding/resharding_coordinator_service.h +++ b/src/mongo/db/s/resharding/resharding_coordinator_service.h @@ -55,7 +55,8 @@ void persistInitialStateAndCatalogUpdates(OperationContext* opCtx, void persistCommittedState(OperationContext* opCtx, const ReshardingCoordinatorDocument& coordinatorDoc, - OID newCollectionEpoch); + OID newCollectionEpoch, + boost::optional<Timestamp> newCollectionTimestamp); void persistStateTransitionAndCatalogUpdatesThenBumpShardVersions( OperationContext* opCtx, const ReshardingCoordinatorDocument& coordinatorDoc); diff --git a/src/mongo/db/s/resharding/resharding_coordinator_test.cpp b/src/mongo/db/s/resharding/resharding_coordinator_test.cpp index 3c455384ceb..af86c07a826 100644 --- a/src/mongo/db/s/resharding/resharding_coordinator_test.cpp +++ b/src/mongo/db/s/resharding/resharding_coordinator_test.cpp @@ -489,7 +489,8 @@ protected: Timestamp fetchTimestamp, std::vector<ChunkType> expectedChunks, std::vector<TagsType> expectedZones) { - resharding::persistCommittedState(operationContext(), expectedCoordinatorDoc, _finalEpoch); + resharding::persistCommittedState( + operationContext(), expectedCoordinatorDoc, _finalEpoch, _finalTimestamp); // Check that config.reshardingOperations and config.collections entries are updated // correctly @@ -555,6 +556,8 @@ protected: OID _tempEpoch = OID::gen(); OID _finalEpoch = OID::gen(); + boost::optional<Timestamp> + _finalTimestamp; // TODO: SERVER-53066 Initialize it with a Timestamp. ShardKeyPattern _oldShardKey = ShardKeyPattern(BSON("oldSK" << 1)); ShardKeyPattern _newShardKey = ShardKeyPattern(BSON("newSK" << 1)); diff --git a/src/mongo/db/s/shard_server_catalog_cache_loader.cpp b/src/mongo/db/s/shard_server_catalog_cache_loader.cpp index 77704557cb9..aa2bfa2b029 100644 --- a/src/mongo/db/s/shard_server_catalog_cache_loader.cpp +++ b/src/mongo/db/s/shard_server_catalog_cache_loader.cpp @@ -96,6 +96,7 @@ Status persistCollectionAndChangedChunks(OperationContext* opCtx, // Update the collections collection entry for 'nss' in case there are any new updates. ShardCollectionType update(nss, collAndChunks.epoch, + collAndChunks.creationTime, *collAndChunks.uuid, collAndChunks.shardKeyPattern, collAndChunks.shardKeyIsUnique); @@ -231,6 +232,7 @@ CollectionAndChangedChunks getPersistedMetadataSinceVersion(OperationContext* op readShardChunks(opCtx, nss, diff.query, diff.sort, boost::none, startingVersion.epoch())); return CollectionAndChangedChunks{shardCollectionEntry.getEpoch(), + shardCollectionEntry.getTimestamp(), shardCollectionEntry.getUuid(), shardCollectionEntry.getKeyPattern().toBSON(), shardCollectionEntry.getDefaultCollation(), diff --git a/src/mongo/db/s/shardsvr_shard_collection.cpp b/src/mongo/db/s/shardsvr_shard_collection.cpp index 0901824f773..9ec584192de 100644 --- a/src/mongo/db/s/shardsvr_shard_collection.cpp +++ b/src/mongo/db/s/shardsvr_shard_collection.cpp @@ -63,6 +63,7 @@ #include "mongo/s/request_types/clone_collection_options_from_primary_shard_gen.h" #include "mongo/s/request_types/shard_collection_gen.h" #include "mongo/s/shard_util.h" +#include "mongo/s/sharded_collections_ddl_parameters_gen.h" #include "mongo/util/fail_point.h" #include "mongo/util/scopeguard.h" #include "mongo/util/str.h" @@ -471,8 +472,13 @@ void updateShardingCatalogEntryForCollection( ->makeFromBSON(defaultCollation)); } + boost::optional<Timestamp> creationTime; + if (feature_flags::gShardingFullDDLSupport.isEnabled(serverGlobalParams.featureCompatibility)) { + creationTime = initialChunks.creationTime; + } + CollectionType coll( - nss, initialChunks.collVersion().epoch(), Date_t::now(), prerequisites.uuid); + nss, initialChunks.collVersion().epoch(), creationTime, Date_t::now(), prerequisites.uuid); coll.setKeyPattern(prerequisites.shardKeyPattern.toBSON()); if (defaultCollator) { coll.setDefaultCollation(defaultCollator->getSpec().toBSON()); diff --git a/src/mongo/db/s/type_shard_collection.cpp b/src/mongo/db/s/type_shard_collection.cpp index a4720650021..74d40fcc940 100644 --- a/src/mongo/db/s/type_shard_collection.cpp +++ b/src/mongo/db/s/type_shard_collection.cpp @@ -38,6 +38,17 @@ ShardCollectionType::ShardCollectionType( : ShardCollectionTypeBase( std::move(nss), std::move(epoch), std::move(uuid), std::move(keyPattern), unique) {} +ShardCollectionType::ShardCollectionType(NamespaceString nss, + OID epoch, + boost::optional<Timestamp> creationTime, + UUID uuid, + KeyPattern keyPattern, + bool unique) + : ShardCollectionTypeBase( + std::move(nss), std::move(epoch), std::move(uuid), std::move(keyPattern), unique) { + setTimestamp(std::move(creationTime)); +} + ShardCollectionType::ShardCollectionType(const BSONObj& obj) { ShardCollectionTypeBase::parseProtected(IDLParserErrorContext("ShardCollectionType"), obj); diff --git a/src/mongo/db/s/type_shard_collection.h b/src/mongo/db/s/type_shard_collection.h index de71f2aab37..bbdbb0ae4a7 100644 --- a/src/mongo/db/s/type_shard_collection.h +++ b/src/mongo/db/s/type_shard_collection.h @@ -57,6 +57,7 @@ public: using ShardCollectionTypeBase::getNss; using ShardCollectionTypeBase::getRefreshing; using ShardCollectionTypeBase::getReshardingFields; + using ShardCollectionTypeBase::getTimestamp; using ShardCollectionTypeBase::getUnique; using ShardCollectionTypeBase::getUuid; using ShardCollectionTypeBase::setDefaultCollation; @@ -73,6 +74,13 @@ public: ShardCollectionType( NamespaceString nss, OID epoch, UUID uuid, KeyPattern keyPattern, bool unique); + ShardCollectionType(NamespaceString nss, + OID epoch, + boost::optional<Timestamp> creationTime, + UUID uuid, + KeyPattern keyPattern, + bool unique); + explicit ShardCollectionType(const BSONObj& obj); ShardCollectionType() = default; |