diff options
44 files changed, 203 insertions, 103 deletions
diff --git a/src/mongo/db/index_builds_coordinator_mongod.cpp b/src/mongo/db/index_builds_coordinator_mongod.cpp index 8f54e8fd9be..12d4f6693c8 100644 --- a/src/mongo/db/index_builds_coordinator_mongod.cpp +++ b/src/mongo/db/index_builds_coordinator_mongod.cpp @@ -374,7 +374,9 @@ IndexBuildsCoordinatorMongod::_startIndexBuild(OperationContext* opCtx, std::move(impersonatedClientAttrs.roleNames)); } - ScopedSetShardRole scopedSetShardRole(opCtx.get(), nss, shardVersion, dbVersion); + boost::optional<ChunkVersion> chunkVersion = + shardVersion ? boost::make_optional((ChunkVersion)*shardVersion) : boost::none; + ScopedSetShardRole scopedSetShardRole(opCtx.get(), nss, chunkVersion, dbVersion); { stdx::unique_lock<Client> lk(*opCtx->getClient()); diff --git a/src/mongo/db/pipeline/document_source_merge.cpp b/src/mongo/db/pipeline/document_source_merge.cpp index ae413052ddf..2e200076bb9 100644 --- a/src/mongo/db/pipeline/document_source_merge.cpp +++ b/src/mongo/db/pipeline/document_source_merge.cpp @@ -460,7 +460,12 @@ boost::intrusive_ptr<DocumentSource> DocumentSourceMerge::createFromBson( auto fieldPaths = convertToFieldPaths(mergeSpec.getOn()); auto [mergeOnFields, targetCollectionVersion] = expCtx->mongoProcessInterface->ensureFieldsUniqueOrResolveDocumentKey( - expCtx, std::move(fieldPaths), mergeSpec.getTargetCollectionVersion(), targetNss); + expCtx, + std::move(fieldPaths), + mergeSpec.getTargetCollectionVersion() + ? boost::make_optional((ChunkVersion)*mergeSpec.getTargetCollectionVersion()) + : boost::none, + targetNss); return DocumentSourceMerge::create(std::move(targetNss), expCtx, @@ -529,7 +534,9 @@ Value DocumentSourceMerge::serialize(boost::optional<ExplainOptions::Verbosity> } return mergeOnFields; }()); - spec.setTargetCollectionVersion(_targetCollectionVersion); + spec.setTargetCollectionVersion( + _targetCollectionVersion ? boost::make_optional(ShardVersion(*_targetCollectionVersion)) + : boost::none); return Value(Document{{getSourceName(), spec.toBSON()}}); } diff --git a/src/mongo/db/pipeline/document_source_merge.idl b/src/mongo/db/pipeline/document_source_merge.idl index 4def6572ddb..5b4d268f75b 100644 --- a/src/mongo/db/pipeline/document_source_merge.idl +++ b/src/mongo/db/pipeline/document_source_merge.idl @@ -33,12 +33,12 @@ global: cpp_includes: - "mongo/db/namespace_string.h" - "mongo/db/pipeline/document_source_merge_spec.h" + - "mongo/s/shard_version.h" imports: - "mongo/db/pipeline/document_source_merge_modes.idl" - "mongo/idl/basic_types.idl" - - "mongo/s/chunk_version.idl" - + - "mongo/s/sharding_types.idl" types: MergeTargetNss: bson_serialization_type: any @@ -112,9 +112,9 @@ structs: do not match. targetCollectionVersion: - type: ChunkVersion + type: shard_version optional: true - description: If set, the collection's ChunkVersion found when parsed on mongos. Can + description: If set, the collection's ShardVersion found when parsed on mongos. Can be used to check if a collection has since been dropped and re-created, in which case the shard key may have changed, or had its shard key refined. This also can be used to detect if the collection has gone diff --git a/src/mongo/db/pipeline/process_interface/common_process_interface.cpp b/src/mongo/db/pipeline/process_interface/common_process_interface.cpp index 57e4c15f3cc..0987e83a6ee 100644 --- a/src/mongo/db/pipeline/process_interface/common_process_interface.cpp +++ b/src/mongo/db/pipeline/process_interface/common_process_interface.cpp @@ -186,13 +186,13 @@ bool CommonProcessInterface::keyPatternNamesExactPaths(const BSONObj& keyPattern return nFieldsMatched == uniqueKeyPaths.size(); } -boost::optional<ChunkVersion> CommonProcessInterface::refreshAndGetCollectionVersion( +boost::optional<ShardVersion> CommonProcessInterface::refreshAndGetCollectionVersion( const boost::intrusive_ptr<ExpressionContext>& expCtx, const NamespaceString& nss) const { const auto cm = uassertStatusOK(Grid::get(expCtx->opCtx) ->catalogCache() ->getCollectionRoutingInfoWithRefresh(expCtx->opCtx, nss)); - return cm.isSharded() ? boost::make_optional(cm.getVersion()) : boost::none; + return cm.isSharded() ? boost::make_optional(ShardVersion(cm.getVersion())) : boost::none; } std::vector<FieldPath> CommonProcessInterface::_shardKeyToDocumentKeyFields( diff --git a/src/mongo/db/pipeline/process_interface/common_process_interface.h b/src/mongo/db/pipeline/process_interface/common_process_interface.h index 513edd5a6f4..efaf292f796 100644 --- a/src/mongo/db/pipeline/process_interface/common_process_interface.h +++ b/src/mongo/db/pipeline/process_interface/common_process_interface.h @@ -66,7 +66,7 @@ public: virtual void updateClientOperationTime(OperationContext* opCtx) const final; - boost::optional<ChunkVersion> refreshAndGetCollectionVersion( + boost::optional<ShardVersion> refreshAndGetCollectionVersion( const boost::intrusive_ptr<ExpressionContext>& expCtx, const NamespaceString& nss) const override; diff --git a/src/mongo/db/pipeline/process_interface/mongo_process_interface.h b/src/mongo/db/pipeline/process_interface/mongo_process_interface.h index 0e36e13613f..9e2f0e96926 100644 --- a/src/mongo/db/pipeline/process_interface/mongo_process_interface.h +++ b/src/mongo/db/pipeline/process_interface/mongo_process_interface.h @@ -58,6 +58,7 @@ #include "mongo/db/storage/temporary_record_store.h" #include "mongo/executor/task_executor.h" #include "mongo/s/chunk_version.h" +#include "mongo/s/shard_version.h" namespace mongo { @@ -433,7 +434,7 @@ public: * request to be sent to the config servers. If another thread has already requested a refresh, * it will instead wait for that response. */ - virtual boost::optional<ChunkVersion> refreshAndGetCollectionVersion( + virtual boost::optional<ShardVersion> refreshAndGetCollectionVersion( const boost::intrusive_ptr<ExpressionContext>& expCtx, const NamespaceString& nss) const = 0; diff --git a/src/mongo/db/pipeline/process_interface/mongos_process_interface.cpp b/src/mongo/db/pipeline/process_interface/mongos_process_interface.cpp index c3dcee83248..117fa156d71 100644 --- a/src/mongo/db/pipeline/process_interface/mongos_process_interface.cpp +++ b/src/mongo/db/pipeline/process_interface/mongos_process_interface.cpp @@ -358,7 +358,9 @@ MongosProcessInterface::ensureFieldsUniqueOrResolveDocumentKey( // collection was dropped a long time ago. Because of this, we are okay with piggy-backing // off another thread's request to refresh the cache, simply waiting for that request to // return instead of forcing another refresh. - targetCollectionVersion = refreshAndGetCollectionVersion(expCtx, outputNs); + boost::optional<ShardVersion> targetVersion = refreshAndGetCollectionVersion(expCtx, outputNs); + targetCollectionVersion = + targetVersion ? boost::make_optional((ChunkVersion)*targetVersion) : boost::none; auto docKeyPaths = collectDocumentKeyFieldsActingAsRouter(expCtx->opCtx, outputNs); return {std::set<FieldPath>(std::make_move_iterator(docKeyPaths.begin()), diff --git a/src/mongo/db/pipeline/process_interface/non_shardsvr_process_interface.h b/src/mongo/db/pipeline/process_interface/non_shardsvr_process_interface.h index 138ba6f3b80..ac5000d84fd 100644 --- a/src/mongo/db/pipeline/process_interface/non_shardsvr_process_interface.h +++ b/src/mongo/db/pipeline/process_interface/non_shardsvr_process_interface.h @@ -64,7 +64,7 @@ public: return nullptr; } - boost::optional<ChunkVersion> refreshAndGetCollectionVersion( + boost::optional<ShardVersion> refreshAndGetCollectionVersion( const boost::intrusive_ptr<ExpressionContext>& expCtx, const NamespaceString& nss) const final { return boost::none; // Nothing is sharded here. diff --git a/src/mongo/db/pipeline/process_interface/stub_mongo_process_interface.h b/src/mongo/db/pipeline/process_interface/stub_mongo_process_interface.h index f406cdfb726..7b96bb53ed0 100644 --- a/src/mongo/db/pipeline/process_interface/stub_mongo_process_interface.h +++ b/src/mongo/db/pipeline/process_interface/stub_mongo_process_interface.h @@ -249,7 +249,7 @@ public: return true; } - boost::optional<ChunkVersion> refreshAndGetCollectionVersion( + boost::optional<ShardVersion> refreshAndGetCollectionVersion( const boost::intrusive_ptr<ExpressionContext>& expCtx, const NamespaceString& nss) const override { return boost::none; diff --git a/src/mongo/db/s/balancer/balancer_commands_scheduler_impl.h b/src/mongo/db/s/balancer/balancer_commands_scheduler_impl.h index bc955e99c07..537548850e5 100644 --- a/src/mongo/db/s/balancer/balancer_commands_scheduler_impl.h +++ b/src/mongo/db/s/balancer/balancer_commands_scheduler_impl.h @@ -291,7 +291,7 @@ public: .append(kEpoch, _version.epoch()) .append(kTimestamp, _version.getTimestamp()); - _version.serializeToBSON(ChunkVersion::kShardVersionField, &commandBuilder); + _version.serialize(ChunkVersion::kChunkVersionField, &commandBuilder); return commandBuilder.obj(); } @@ -366,7 +366,7 @@ public: .append(kMaxValue, _upperBoundKey) .append(kEstimatedValue, _estimatedValue); - _version.serializeToBSON(ChunkVersion::kShardVersionField, &commandBuilder); + _version.serialize(ChunkVersion::kChunkVersionField, &commandBuilder); return commandBuilder.obj(); } diff --git a/src/mongo/db/s/balancer/migration_test_fixture.cpp b/src/mongo/db/s/balancer/migration_test_fixture.cpp index d3bac026844..c5805470cfd 100644 --- a/src/mongo/db/s/balancer/migration_test_fixture.cpp +++ b/src/mongo/db/s/balancer/migration_test_fixture.cpp @@ -126,7 +126,7 @@ void MigrationTestFixture::setUpMigration(const NamespaceString& ns, builder.append(MigrationType::max(), chunk.getMax()); builder.append(MigrationType::toShard(), toShard.toString()); builder.append(MigrationType::fromShard(), chunk.getShard().toString()); - chunk.getVersion().serializeToBSON("chunkVersion", &builder); + chunk.getVersion().serialize("chunkVersion", &builder); builder.append(MigrationType::forceJumbo(), "doNotForceJumbo"); MigrationType migrationType = assertGet(MigrationType::fromBSON(builder.obj())); diff --git a/src/mongo/db/s/balancer/type_migration.cpp b/src/mongo/db/s/balancer/type_migration.cpp index 4da7deb522f..bbf703c0d13 100644 --- a/src/mongo/db/s/balancer/type_migration.cpp +++ b/src/mongo/db/s/balancer/type_migration.cpp @@ -174,7 +174,7 @@ BSONObj MigrationType::toBSON() const { builder.append(fromShard.name(), _fromShard.toString()); builder.append(toShard.name(), _toShard.toString()); - _chunkVersion.serializeToBSON(chunkVersion.name(), &builder); + _chunkVersion.serialize(chunkVersion.name(), &builder); builder.append(waitForDelete.name(), _waitForDelete); builder.append(forceJumbo.name(), _forceJumbo); diff --git a/src/mongo/db/s/balancer/type_migration_test.cpp b/src/mongo/db/s/balancer/type_migration_test.cpp index d8d7f695fad..f195b4e91a8 100644 --- a/src/mongo/db/s/balancer/type_migration_test.cpp +++ b/src/mongo/db/s/balancer/type_migration_test.cpp @@ -54,7 +54,7 @@ TEST(MigrationTypeTest, FromAndToBSONWithoutOptionalFields) { builder.append(MigrationType::max(), kMax); builder.append(MigrationType::fromShard(), kFromShard.toString()); builder.append(MigrationType::toShard(), kToShard.toString()); - version.serializeToBSON("chunkVersion", &builder); + version.serialize("chunkVersion", &builder); builder.append(MigrationType::waitForDelete(), kWaitForDelete); builder.append(MigrationType::forceJumbo(), ForceJumbo_serializer(ForceJumbo::kDoNotForce)); @@ -76,7 +76,7 @@ TEST(MigrationTypeTest, FromAndToBSONWitOptionalFields) { builder.append(MigrationType::max(), kMax); builder.append(MigrationType::fromShard(), kFromShard.toString()); builder.append(MigrationType::toShard(), kToShard.toString()); - version.serializeToBSON("chunkVersion", &builder); + version.serialize("chunkVersion", &builder); builder.append(MigrationType::waitForDelete(), kWaitForDelete); builder.append(MigrationType::forceJumbo(), ForceJumbo_serializer(ForceJumbo::kDoNotForce)); builder.append(MigrationType::maxChunkSizeBytes(), 512 * 1024 * 1024); @@ -97,7 +97,7 @@ TEST(MigrationTypeTest, MissingRequiredNamespaceField) { builder.append(MigrationType::max(), kMax); builder.append(MigrationType::fromShard(), kFromShard.toString()); builder.append(MigrationType::toShard(), kToShard.toString()); - version.serializeToBSON("chunkVersion", &builder); + version.serialize("chunkVersion", &builder); BSONObj obj = builder.obj(); @@ -114,7 +114,7 @@ TEST(MigrationTypeTest, MissingRequiredMinField) { builder.append(MigrationType::max(), kMax); builder.append(MigrationType::fromShard(), kFromShard.toString()); builder.append(MigrationType::toShard(), kToShard.toString()); - version.serializeToBSON("chunkVersion", &builder); + version.serialize("chunkVersion", &builder); BSONObj obj = builder.obj(); @@ -131,7 +131,7 @@ TEST(MigrationTypeTest, MissingRequiredMaxField) { builder.append(MigrationType::min(), kMin); builder.append(MigrationType::fromShard(), kFromShard.toString()); builder.append(MigrationType::toShard(), kToShard.toString()); - version.serializeToBSON("chunkVersion", &builder); + version.serialize("chunkVersion", &builder); BSONObj obj = builder.obj(); @@ -148,7 +148,7 @@ TEST(MigrationTypeTest, MissingRequiredFromShardField) { builder.append(MigrationType::min(), kMin); builder.append(MigrationType::max(), kMax); builder.append(MigrationType::toShard(), kToShard.toString()); - version.serializeToBSON("chunkVersion", &builder); + version.serialize("chunkVersion", &builder); BSONObj obj = builder.obj(); @@ -165,7 +165,7 @@ TEST(MigrationTypeTest, MissingRequiredToShardField) { builder.append(MigrationType::min(), kMin); builder.append(MigrationType::max(), kMax); builder.append(MigrationType::fromShard(), kFromShard.toString()); - version.serializeToBSON("chunkVersion", &builder); + version.serialize("chunkVersion", &builder); BSONObj obj = builder.obj(); diff --git a/src/mongo/db/s/collection_sharding_runtime.cpp b/src/mongo/db/s/collection_sharding_runtime.cpp index 18521003da0..de24504c1dd 100644 --- a/src/mongo/db/s/collection_sharding_runtime.cpp +++ b/src/mongo/db/s/collection_sharding_runtime.cpp @@ -63,7 +63,7 @@ private: const auto kUnshardedCollection = std::make_shared<UnshardedCollection>(); -boost::optional<ChunkVersion> getOperationReceivedVersion(OperationContext* opCtx, +boost::optional<ShardVersion> getOperationReceivedVersion(OperationContext* opCtx, const NamespaceString& nss) { // If there is a version attached to the OperationContext, use it as the received version. if (OperationShardingState::isComingFromRouter(opCtx)) { @@ -103,7 +103,7 @@ ScopedCollectionFilter CollectionShardingRuntime::getOwnershipFilter( OperationContext* opCtx, OrphanCleanupPolicy orphanCleanupPolicy, bool supportNonVersionedOperations) { - boost::optional<ChunkVersion> optReceivedShardVersion = boost::none; + boost::optional<ShardVersion> optReceivedShardVersion = boost::none; if (!supportNonVersionedOperations) { optReceivedShardVersion = getOperationReceivedVersion(opCtx, _nss); // No operations should be calling getOwnershipFilter without a shard version @@ -143,7 +143,8 @@ ScopedCollectionDescription CollectionShardingRuntime::getCollectionDescription( const auto receivedShardVersion{oss.getShardVersion(_nss)}; uassert( StaleConfigInfo(_nss, - receivedShardVersion ? *receivedShardVersion : ChunkVersion::IGNORED(), + receivedShardVersion ? (ChunkVersion)*receivedShardVersion + : ChunkVersion::IGNORED(), boost::none /* wantedVersion */, ShardingState::get(_serviceContext)->shardId()), str::stream() << "sharding status of collection " << _nss.ns() @@ -352,7 +353,7 @@ CollectionShardingRuntime::_getMetadataWithVersionCheckAt( // Assume that the received shard version was IGNORED if the current operation wasn't versioned const auto& receivedShardVersion = - optReceivedShardVersion ? *optReceivedShardVersion : ChunkVersion::IGNORED(); + optReceivedShardVersion ? (ChunkVersion)*optReceivedShardVersion : ChunkVersion::IGNORED(); auto csrLock = CSRLock::lockShared(opCtx, this); diff --git a/src/mongo/db/s/commit_chunk_migration.idl b/src/mongo/db/s/commit_chunk_migration.idl index 6484623cd5c..981598e6573 100644 --- a/src/mongo/db/s/commit_chunk_migration.idl +++ b/src/mongo/db/s/commit_chunk_migration.idl @@ -43,7 +43,7 @@ structs: fields: shardVersion: type: ChunkVersion - description: "Collection version at the end of the migration." + description: "Placement information at the end of the migration." MigratedChunkType: description: "ChunkType describing a migrated chunk" @@ -77,9 +77,9 @@ commands: description: "ChunkType describing a migrated chunk" fromShardCollectionVersion: - type: ChunkVersion + type: ChunkVersion description: "{ shardVersionField: <version> }" validAfter: type: timestamp - description: "The time after which this chunk is at the new shard"
\ No newline at end of file + description: "The time after which this chunk is at the new shard" diff --git a/src/mongo/db/s/config/configsvr_commit_chunk_migration_command.cpp b/src/mongo/db/s/config/configsvr_commit_chunk_migration_command.cpp index 30ecefe11c8..66b4416a343 100644 --- a/src/mongo/db/s/config/configsvr_commit_chunk_migration_command.cpp +++ b/src/mongo/db/s/config/configsvr_commit_chunk_migration_command.cpp @@ -146,7 +146,7 @@ public: auto chunkVersionObj = uassertStatusOK(chunkVersionResponse); - return Response{ChunkVersion::parse(chunkVersionObj[ChunkVersion::kShardVersionField])}; + return Response{ChunkVersion::parse(chunkVersionObj[ChunkVersion::kChunkVersionField])}; } private: diff --git a/src/mongo/db/s/config/configsvr_merge_chunks_command.cpp b/src/mongo/db/s/config/configsvr_merge_chunks_command.cpp index db155fa6bea..430cb5c25b9 100644 --- a/src/mongo/db/s/config/configsvr_merge_chunks_command.cpp +++ b/src/mongo/db/s/config/configsvr_merge_chunks_command.cpp @@ -97,7 +97,7 @@ public: request().getShard(), request().getValidAfter())); return ConfigSvrMergeResponse{ - ChunkVersion::parse(shardAndCollVers[ChunkVersion::kShardVersionField])}; + ChunkVersion::parse(shardAndCollVers[ChunkVersion::kChunkVersionField])}; } private: diff --git a/src/mongo/db/s/config/sharding_catalog_manager_chunk_operations.cpp b/src/mongo/db/s/config/sharding_catalog_manager_chunk_operations.cpp index 78af77db426..459fdef0061 100644 --- a/src/mongo/db/s/config/sharding_catalog_manager_chunk_operations.cpp +++ b/src/mongo/db/s/config/sharding_catalog_manager_chunk_operations.cpp @@ -83,7 +83,7 @@ void appendShortVersion(BufBuilder* out, const ChunkType& chunk) { bb.append(ChunkType::min(), chunk.getMin()); bb.append(ChunkType::max(), chunk.getMax()); if (chunk.isVersionSet()) { - chunk.getVersion().serializeToBSON(ChunkType::lastmod(), &bb); + chunk.getVersion().serialize(ChunkType::lastmod(), &bb); } bb.done(); } @@ -685,7 +685,7 @@ StatusWith<BSONObj> ShardingCatalogManager::commitChunkSplit( BSONObjBuilder b(logDetail.subobjStart("before")); b.append(ChunkType::min(), range.getMin()); b.append(ChunkType::max(), range.getMax()); - collVersion.serializeToBSON(ChunkType::lastmod(), &b); + collVersion.serialize(ChunkType::lastmod(), &b); } if (splitChunkResult.newChunks->size() == 2) { @@ -719,8 +719,8 @@ StatusWith<BSONObj> ShardingCatalogManager::commitChunkSplit( } BSONObjBuilder response; - splitChunkResult.currentMaxVersion.serializeToBSON(kCollectionVersionField, &response); - splitChunkResult.currentMaxVersion.serializeToBSON(ChunkVersion::kShardVersionField, &response); + splitChunkResult.currentMaxVersion.serialize(kCollectionVersionField, &response); + splitChunkResult.currentMaxVersion.serialize(ChunkVersion::kChunkVersionField, &response); return response.obj(); } @@ -908,9 +908,9 @@ StatusWith<BSONObj> ShardingCatalogManager::commitChunksMerge( << chunkRange.toString(), chunk.getRange() == chunkRange); BSONObjBuilder response; - collVersion.serializeToBSON(kCollectionVersionField, &response); + collVersion.serialize(kCollectionVersionField, &response); const auto currentShardVersion = getShardVersion(opCtx, coll, shardId, collVersion); - currentShardVersion.serializeToBSON(ChunkVersion::kShardVersionField, &response); + currentShardVersion.serialize(ChunkVersion::kChunkVersionField, &response); // Makes sure that the last thing we read in getCollectionVersion and getShardVersion gets // majority written before to return from this command, otherwise next RoutingInfo cache // refresh from the shard may not see those newest information. @@ -965,16 +965,16 @@ StatusWith<BSONObj> ShardingCatalogManager::commitChunksMerge( b.append(chunkToMerge.toConfigBSON()); } } - initialVersion.serializeToBSON("prevShardVersion", &logDetail); - mergeVersion.serializeToBSON("mergedVersion", &logDetail); + initialVersion.serialize("prevShardVersion", &logDetail); + mergeVersion.serialize("mergedVersion", &logDetail); logDetail.append("owningShard", shardId); ShardingLogging::get(opCtx)->logChange( opCtx, "merge", nss.ns(), logDetail.obj(), WriteConcernOptions()); BSONObjBuilder response; - mergeVersion.serializeToBSON(kCollectionVersionField, &response); - mergeVersion.serializeToBSON(ChunkVersion::kShardVersionField, &response); + mergeVersion.serialize(kCollectionVersionField, &response); + mergeVersion.serialize(ChunkVersion::kChunkVersionField, &response); return response.obj(); } @@ -1101,10 +1101,10 @@ StatusWith<BSONObj> ShardingCatalogManager::commitChunkMigration( if (currentChunk.getShard() == toShard) { // The commit was already done successfully BSONObjBuilder response; - currentCollectionVersion.serializeToBSON(kCollectionVersionField, &response); + currentCollectionVersion.serialize(kCollectionVersionField, &response); const auto currentShardVersion = getShardVersion(opCtx, coll, fromShard, currentCollectionVersion); - currentShardVersion.serializeToBSON(ChunkVersion::kShardVersionField, &response); + currentShardVersion.serialize(ChunkVersion::kChunkVersionField, &response); // Makes sure that the last thing we read in findChunkContainingRange, getShardVersion, and // getCollectionVersion gets majority written before to return from this command, otherwise // next RoutingInfo cache refresh from the shard may not see those newest information. @@ -1235,13 +1235,13 @@ StatusWith<BSONObj> ShardingCatalogManager::commitChunkMigration( BSONObjBuilder response; if (!newControlChunk) { // We migrated the last chunk from the donor shard. - newMigratedChunk->getVersion().serializeToBSON(kCollectionVersionField, &response); + newMigratedChunk->getVersion().serialize(kCollectionVersionField, &response); const ChunkVersion donorShardVersion( {currentCollectionVersion.epoch(), currentCollectionVersion.getTimestamp()}, {0, 0}); - donorShardVersion.serializeToBSON(ChunkVersion::kShardVersionField, &response); + donorShardVersion.serialize(ChunkVersion::kChunkVersionField, &response); } else { - newControlChunk->getVersion().serializeToBSON(kCollectionVersionField, &response); - newControlChunk->getVersion().serializeToBSON(ChunkVersion::kShardVersionField, &response); + newControlChunk->getVersion().serialize(kCollectionVersionField, &response); + newControlChunk->getVersion().serialize(ChunkVersion::kChunkVersionField, &response); } return response.obj(); } diff --git a/src/mongo/db/s/migration_coordinator_document.idl b/src/mongo/db/s/migration_coordinator_document.idl index 028cb194582..c9368dcedaa 100644 --- a/src/mongo/db/s/migration_coordinator_document.idl +++ b/src/mongo/db/s/migration_coordinator_document.idl @@ -95,7 +95,8 @@ structs: description: "The range being migrated." preMigrationChunkVersion: type: ChunkVersion - description: "The version, at the start of the migration, of the chunk being moved." + description: "The data placement, at the start of the migration, of the chunk being + moved." decision: type: Decision description: "Enumeration that defines whether the migration committed or aborted." diff --git a/src/mongo/db/s/operation_sharding_state.cpp b/src/mongo/db/s/operation_sharding_state.cpp index 0419fb1e895..6fd4ce5fc4f 100644 --- a/src/mongo/db/s/operation_sharding_state.cpp +++ b/src/mongo/db/s/operation_sharding_state.cpp @@ -61,13 +61,14 @@ void OperationShardingState::setShardRole(OperationContext* opCtx, auto& oss = OperationShardingState::get(opCtx); if (shardVersion) { - auto emplaceResult = oss._shardVersions.try_emplace(nss.ns(), *shardVersion); + ShardVersion fullShardVersion(*shardVersion); + auto emplaceResult = oss._shardVersions.try_emplace(nss.ns(), fullShardVersion); auto& tracker = emplaceResult.first->second; if (!emplaceResult.second) { uassert(640570, str::stream() << "Illegal attempt to change the expected shard version for " - << nss << " from " << tracker.v << " to " << *shardVersion, - tracker.v == *shardVersion); + << nss << " from " << tracker.v << " to " << fullShardVersion, + tracker.v == fullShardVersion); } invariant(++tracker.recursion > 0); } @@ -86,7 +87,7 @@ void OperationShardingState::setShardRole(OperationContext* opCtx, } } -boost::optional<ChunkVersion> OperationShardingState::getShardVersion(const NamespaceString& nss) { +boost::optional<ShardVersion> OperationShardingState::getShardVersion(const NamespaceString& nss) { const auto it = _shardVersions.find(nss.ns()); if (it != _shardVersions.end()) { return it->second.v; diff --git a/src/mongo/db/s/operation_sharding_state.h b/src/mongo/db/s/operation_sharding_state.h index 99bd516295e..dff0c8df630 100644 --- a/src/mongo/db/s/operation_sharding_state.h +++ b/src/mongo/db/s/operation_sharding_state.h @@ -33,8 +33,8 @@ #include "mongo/db/namespace_string.h" #include "mongo/db/operation_context.h" -#include "mongo/s/chunk_version.h" #include "mongo/s/database_version.h" +#include "mongo/s/shard_version.h" #include "mongo/util/future.h" #include "mongo/util/string_map.h" @@ -121,7 +121,7 @@ public: * operation. Documents in chunks which did not belong on this shard at this shard version * will be filtered out. */ - boost::optional<ChunkVersion> getShardVersion(const NamespaceString& nss); + boost::optional<ShardVersion> getShardVersion(const NamespaceString& nss); /** * Returns true if the client sent a databaseVersion for any namespace. @@ -170,11 +170,11 @@ private: // Stores the shard version expected for each collection that will be accessed struct ShardVersionTracker { - ShardVersionTracker(ChunkVersion v) : v(v) {} + ShardVersionTracker(ShardVersion v) : v(v) {} ShardVersionTracker(ShardVersionTracker&&) = default; ShardVersionTracker(const ShardVersionTracker&) = delete; ShardVersionTracker& operator=(const ShardVersionTracker&) = delete; - ChunkVersion v; + ShardVersion v; int recursion{0}; }; StringMap<ShardVersionTracker> _shardVersions; diff --git a/src/mongo/db/s/shardsvr_merge_chunks_command.cpp b/src/mongo/db/s/shardsvr_merge_chunks_command.cpp index 8b3892a907b..160f4014ec0 100644 --- a/src/mongo/db/s/shardsvr_merge_chunks_command.cpp +++ b/src/mongo/db/s/shardsvr_merge_chunks_command.cpp @@ -148,8 +148,8 @@ void mergeChunks(OperationContext* opCtx, auto shardVersionReceived = [&]() -> boost::optional<ChunkVersion> { // Old versions might not have the shardVersion field - if (cmdResponse.response[ChunkVersion::kShardVersionField]) { - return ChunkVersion::parse(cmdResponse.response[ChunkVersion::kShardVersionField]); + if (cmdResponse.response[ChunkVersion::kChunkVersionField]) { + return ChunkVersion::parse(cmdResponse.response[ChunkVersion::kChunkVersionField]); } return boost::none; }(); diff --git a/src/mongo/db/s/split_chunk.cpp b/src/mongo/db/s/split_chunk.cpp index 7b9067e7902..f76ae40c9fc 100644 --- a/src/mongo/db/s/split_chunk.cpp +++ b/src/mongo/db/s/split_chunk.cpp @@ -254,8 +254,8 @@ StatusWith<boost::optional<ChunkRange>> splitChunk( boost::optional<ChunkVersion> shardVersionReceived = [&]() -> boost::optional<ChunkVersion> { // old versions might not have the shardVersion field - if (cmdResponse.response[ChunkVersion::kShardVersionField]) { - return ChunkVersion::parse(cmdResponse.response[ChunkVersion::kShardVersionField]); + if (cmdResponse.response[ChunkVersion::kChunkVersionField]) { + return ChunkVersion::parse(cmdResponse.response[ChunkVersion::kChunkVersionField]); } return boost::none; }(); diff --git a/src/mongo/db/service_entry_point_common.cpp b/src/mongo/db/service_entry_point_common.cpp index 3bc08313324..b0a79bfde84 100644 --- a/src/mongo/db/service_entry_point_common.cpp +++ b/src/mongo/db/service_entry_point_common.cpp @@ -1679,8 +1679,8 @@ void ExecCommandDatabase::_initiateCommand() { : _invocation->ns(); boost::optional<ChunkVersion> shardVersion; - if (auto shardVersionElem = request.body[ChunkVersion::kShardVersionField]) { - shardVersion = ChunkVersion::parse(shardVersionElem); + if (auto shardVersionElem = request.body[ShardVersion::kShardVersionField]) { + shardVersion = ShardVersion::parse(shardVersionElem); } boost::optional<DatabaseVersion> databaseVersion; diff --git a/src/mongo/s/chunk_manager.cpp b/src/mongo/s/chunk_manager.cpp index 2c477bb3e56..31de7493b57 100644 --- a/src/mongo/s/chunk_manager.cpp +++ b/src/mongo/s/chunk_manager.cpp @@ -273,7 +273,7 @@ ChunkMap ChunkMap::createMerged( BSONObj ChunkMap::toBSON() const { BSONObjBuilder builder; - getVersion().serializeToBSON("startingVersion"_sd, &builder); + getVersion().serialize("startingVersion"_sd, &builder); builder.append("chunkCount", static_cast<int64_t>(_chunkMap.size())); { @@ -866,7 +866,7 @@ void ComparableChunkVersion::setChunkVersion(const ChunkVersion& version) { std::string ComparableChunkVersion::toString() const { BSONObjBuilder builder; if (_chunkVersion) - _chunkVersion->serializeToBSON("chunkVersion"_sd, &builder); + _chunkVersion->serialize("chunkVersion"_sd, &builder); else builder.append("chunkVersion"_sd, "None"); diff --git a/src/mongo/s/chunk_version.cpp b/src/mongo/s/chunk_version.cpp index 88d7833f10e..fa3931160ea 100644 --- a/src/mongo/s/chunk_version.cpp +++ b/src/mongo/s/chunk_version.cpp @@ -34,7 +34,7 @@ namespace mongo { -constexpr StringData ChunkVersion::kShardVersionField; +constexpr StringData ChunkVersion::kChunkVersionField; bool CollectionGeneration::isSameCollection(const CollectionGeneration& other) const { if (_timestamp == other._timestamp) { @@ -64,7 +64,7 @@ ChunkVersion ChunkVersion::parse(const BSONElement& element) { {version.getSecs(), version.getInc()}); } -void ChunkVersion::serializeToBSON(StringData field, BSONObjBuilder* builder) const { +void ChunkVersion::serialize(StringData field, BSONObjBuilder* builder) const { ChunkVersion60Format version; version.setGeneration({_epoch, _timestamp}); version.setPlacement(Timestamp(majorVersion(), minorVersion())); diff --git a/src/mongo/s/chunk_version.h b/src/mongo/s/chunk_version.h index 2a7911bfefe..f573c406822 100644 --- a/src/mongo/s/chunk_version.h +++ b/src/mongo/s/chunk_version.h @@ -114,10 +114,10 @@ protected: class ChunkVersion : public CollectionGeneration, public CollectionPlacement { public: /** - * The name for the shard version information field, which shard-aware commands should include - * if they want to convey shard version. + * The name for the chunk version information field, which ddl operations use to send only + * the placement information. String is shardVersion for compatibility with previous versions. */ - static constexpr StringData kShardVersionField = "shardVersion"_sd; + static constexpr StringData kChunkVersionField = "shardVersion"_sd; ChunkVersion(CollectionGeneration geneneration, CollectionPlacement placement) : CollectionGeneration(geneneration), CollectionPlacement(placement) {} @@ -221,7 +221,7 @@ public: } static ChunkVersion parse(const BSONElement& element); - void serializeToBSON(StringData field, BSONObjBuilder* builder) const; + void serialize(StringData field, BSONObjBuilder* builder) const; std::string toString() const; }; diff --git a/src/mongo/s/chunk_version.idl b/src/mongo/s/chunk_version.idl index f281cfb3df0..1528945ae44 100644 --- a/src/mongo/s/chunk_version.idl +++ b/src/mongo/s/chunk_version.idl @@ -76,5 +76,5 @@ types: bson_serialization_type: any description: An object representing a chunk version for a collection. cpp_type: ChunkVersion - serializer: ChunkVersion::serializeToBSON + serializer: ChunkVersion::serialize deserializer: ChunkVersion::parse diff --git a/src/mongo/s/cluster_commands_helpers.cpp b/src/mongo/s/cluster_commands_helpers.cpp index c720475eb99..3c4c12ff386 100644 --- a/src/mongo/s/cluster_commands_helpers.cpp +++ b/src/mongo/s/cluster_commands_helpers.cpp @@ -290,9 +290,9 @@ BSONObj appendDbVersionIfPresent(BSONObj cmdObj, DatabaseVersion dbVersion) { return cmdWithDbVersion.obj(); } -BSONObj appendShardVersion(BSONObj cmdObj, ChunkVersion version) { +BSONObj appendShardVersion(BSONObj cmdObj, ShardVersion version) { BSONObjBuilder cmdWithVersionBob(std::move(cmdObj)); - version.serializeToBSON(ChunkVersion::kShardVersionField, &cmdWithVersionBob); + version.serialize(ShardVersion::kShardVersionField, &cmdWithVersionBob); return cmdWithVersionBob.obj(); } diff --git a/src/mongo/s/cluster_commands_helpers.h b/src/mongo/s/cluster_commands_helpers.h index 80a9677930a..7e6d5921382 100644 --- a/src/mongo/s/cluster_commands_helpers.h +++ b/src/mongo/s/cluster_commands_helpers.h @@ -119,7 +119,7 @@ BSONObj appendDbVersionIfPresent(BSONObj cmdObj, DatabaseVersion dbVersion); /** * Returns a copy of 'cmdObj' with 'version' appended. */ -BSONObj appendShardVersion(BSONObj cmdObj, ChunkVersion version); +BSONObj appendShardVersion(BSONObj cmdObj, ShardVersion version); /** * Returns a copy of 'cmdObj' with the read/writeConcern from the OpCtx appended, unless the diff --git a/src/mongo/s/commands/cluster_merge_chunks_cmd.cpp b/src/mongo/s/commands/cluster_merge_chunks_cmd.cpp index 07199f02050..684184bf6b0 100644 --- a/src/mongo/s/commands/cluster_merge_chunks_cmd.cpp +++ b/src/mongo/s/commands/cluster_merge_chunks_cmd.cpp @@ -159,7 +159,7 @@ public: firstChunk.getShardId().toString()); remoteCmdObjB.append("epoch", shardVersion.epoch()); remoteCmdObjB.append("timestamp", shardVersion.getTimestamp()); - shardVersion.serializeToBSON(ChunkVersion::kShardVersionField, &remoteCmdObjB); + shardVersion.serialize(ChunkVersion::kChunkVersionField, &remoteCmdObjB); BSONObj remoteResult; diff --git a/src/mongo/s/commands/cluster_split_cmd.cpp b/src/mongo/s/commands/cluster_split_cmd.cpp index ae3f80eda56..9d9754a133f 100644 --- a/src/mongo/s/commands/cluster_split_cmd.cpp +++ b/src/mongo/s/commands/cluster_split_cmd.cpp @@ -66,7 +66,7 @@ BSONObj selectMedianKey(OperationContext* opCtx, cmd.append("keyPattern", shardKeyPattern.toBSON()); chunkRange.append(&cmd); cmd.appendBool("force", true); - chunkVersion.serializeToBSON(ChunkVersion::kShardVersionField, &cmd); + chunkVersion.serialize(ChunkVersion::kChunkVersionField, &cmd); auto shard = uassertStatusOK(Grid::get(opCtx)->shardRegistry()->getShard(opCtx, shardId)); diff --git a/src/mongo/s/query/cluster_find.cpp b/src/mongo/s/query/cluster_find.cpp index bf0030c54c9..187ee7289c0 100644 --- a/src/mongo/s/query/cluster_find.cpp +++ b/src/mongo/s/query/cluster_find.cpp @@ -186,10 +186,11 @@ std::vector<std::pair<ShardId, BSONObj>> constructRequestsForShards( findCommandToForward->serialize(BSONObj(), &cmdBuilder); if (cm.isSharded()) { - cm.getVersion(shardId).serializeToBSON(ChunkVersion::kShardVersionField, &cmdBuilder); + ShardVersion(cm.getVersion(shardId)) + .serialize(ShardVersion::kShardVersionField, &cmdBuilder); } else if (!query.nss().isOnInternalDb()) { - ChunkVersion::UNSHARDED().serializeToBSON(ChunkVersion::kShardVersionField, - &cmdBuilder); + ShardVersion(ChunkVersion::UNSHARDED()) + .serialize(ShardVersion::kShardVersionField, &cmdBuilder); cmdBuilder.append("databaseVersion", cm.dbVersion().toBSON()); } diff --git a/src/mongo/s/request_types/balance_chunk_request_type.cpp b/src/mongo/s/request_types/balance_chunk_request_type.cpp index f94b61db12e..236eccf931b 100644 --- a/src/mongo/s/request_types/balance_chunk_request_type.cpp +++ b/src/mongo/s/request_types/balance_chunk_request_type.cpp @@ -153,7 +153,7 @@ BSONObj BalanceChunkRequest::serializeToRebalanceCommandForConfig( range.append(&cmdBuilder); cmdBuilder.append(ChunkType::shard(), owningShard); collectionUUID.appendToBuilder(&cmdBuilder, ChunkType::collectionUUID()); - expectedChunkVersion.serializeToBSON(ChunkType::lastmod(), &cmdBuilder); + expectedChunkVersion.serialize(ChunkType::lastmod(), &cmdBuilder); cmdBuilder.append(WriteConcernOptions::kWriteConcernField, kMajorityWriteConcernNoTimeout.toBSON()); diff --git a/src/mongo/s/request_types/ensure_chunk_version_is_greater_than.idl b/src/mongo/s/request_types/ensure_chunk_version_is_greater_than.idl index 67aa936413f..a90b776fff7 100644 --- a/src/mongo/s/request_types/ensure_chunk_version_is_greater_than.idl +++ b/src/mongo/s/request_types/ensure_chunk_version_is_greater_than.idl @@ -54,8 +54,8 @@ commands: type: object optional: false version: - description: The version of the chunk, including major version, minor version, and - epoch. + description: The data placement of the chunk, including major version, minor + version, and epoch. type: ChunkVersion optional: false collectionUUID: diff --git a/src/mongo/s/request_types/merge_chunk_request.idl b/src/mongo/s/request_types/merge_chunk_request.idl index 55d4148902d..481becb962c 100644 --- a/src/mongo/s/request_types/merge_chunk_request.idl +++ b/src/mongo/s/request_types/merge_chunk_request.idl @@ -56,7 +56,7 @@ structs: fields: shardVersion: type: ChunkVersion - description: "Latest version of the shard." + description: "Latest data placement of the shard." commands: _configsvrCommitChunksMerge: diff --git a/src/mongo/s/request_types/sharded_ddl_commands.idl b/src/mongo/s/request_types/sharded_ddl_commands.idl index 001551acaf6..cc3b2b00691 100644 --- a/src/mongo/s/request_types/sharded_ddl_commands.idl +++ b/src/mongo/s/request_types/sharded_ddl_commands.idl @@ -32,6 +32,7 @@ global: cpp_namespace: "mongo" cpp_includes: - "mongo/s/catalog/type_collection.h" + - "mongo/s/shard_version.h" imports: - "mongo/db/commands/rename_collection.idl" @@ -41,7 +42,6 @@ imports: - "mongo/db/keypattern.idl" - "mongo/db/coll_mod.idl" - "mongo/idl/basic_types.idl" - - "mongo/s/chunk_version.idl" - "mongo/s/sharding_types.idl" - "mongo/s/resharding/common_types.idl" - "mongo/db/timeseries/timeseries.idl" @@ -110,7 +110,7 @@ structs: strict: false fields: collectionVersion: - type: ChunkVersion + type: shard_version description: "Latest version of the collection" optional: false @@ -176,7 +176,7 @@ structs: description: "UUID of the created collection" optional: true collectionVersion: - type: ChunkVersion + type: shard_version description: "Latest version of the collection" optional: false diff --git a/src/mongo/s/router.cpp b/src/mongo/s/router.cpp index cdcbcaa1ffb..5149289cb43 100644 --- a/src/mongo/s/router.cpp +++ b/src/mongo/s/router.cpp @@ -62,7 +62,7 @@ void DBPrimaryRouter::appendCRUDUnshardedRoutingTokenToCommand(const ShardId& sh BSONObjBuilder dbvBuilder(builder->subobjStart(DatabaseVersion::kDatabaseVersionField)); dbVersion.serialize(&dbvBuilder); } - ChunkVersion::UNSHARDED().serializeToBSON(ChunkVersion::kShardVersionField, builder); + ShardVersion(ChunkVersion::UNSHARDED()).serialize(ShardVersion::kShardVersionField, builder); } CachedDatabaseInfo DBPrimaryRouter::_getRoutingInfo(OperationContext* opCtx) const { @@ -115,7 +115,7 @@ void CollectionRouter::appendCRUDRoutingTokenToCommand(const ShardId& shardId, dbVersion.serialize(&dbvBuilder); } } - chunkVersion.serializeToBSON(ChunkVersion::kShardVersionField, builder); + ShardVersion(chunkVersion).serialize(ShardVersion::kShardVersionField, builder); } ChunkManager CollectionRouter::_getRoutingInfo(OperationContext* opCtx) const { diff --git a/src/mongo/s/shard_util.cpp b/src/mongo/s/shard_util.cpp index 77c0ec29481..67b5d4ed14f 100644 --- a/src/mongo/s/shard_util.cpp +++ b/src/mongo/s/shard_util.cpp @@ -208,7 +208,7 @@ StatusWith<boost::optional<ChunkRange>> splitChunkAtMultiplePoints( cmd.append("keyPattern", shardKeyPattern.toBSON()); cmd.append("epoch", epoch); cmd.append("timestamp", timestamp); - shardVersion.serializeToBSON(ChunkVersion::kShardVersionField, &cmd); + shardVersion.serialize(ChunkVersion::kChunkVersionField, &cmd); chunkRange.append(&cmd); cmd.append("splitKeys", splitPointsBeginIt, splitPointsEndIt); diff --git a/src/mongo/s/shard_version.h b/src/mongo/s/shard_version.h new file mode 100644 index 00000000000..f49cb10f83a --- /dev/null +++ b/src/mongo/s/shard_version.h @@ -0,0 +1,70 @@ +/** + * Copyright (C) 2022-present MongoDB, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the Server Side Public License, version 1, + * as published by MongoDB, Inc. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Server Side Public License for more details. + * + * You should have received a copy of the Server Side Public License + * along with this program. If not, see + * <http://www.mongodb.com/licensing/server-side-public-license>. + * + * As a special exception, the copyright holders give permission to link the + * code of portions of this program with the OpenSSL library under certain + * conditions as described in each individual source file and distribute + * linked combinations including the program with the OpenSSL library. You + * must comply with the Server Side Public License in all respects for + * all of the code used other than as permitted herein. If you modify file(s) + * with this exception, you may extend this exception to your version of the + * file(s), but you are not obligated to do so. If you do not wish to do so, + * delete this exception statement from your version. If you delete this + * exception statement from all source files in the program, then also delete + * it in the license file. + */ +#pragma once + +#include "mongo/s/chunk_version_gen.h" + +namespace mongo { + +/** + * This class is used to represent the shard version of a collection. + * + * It contains the chunk placement information through the ChunkVersion. This class is used for + * network requests and the shard versioning protocol. + * + */ +class ShardVersion : public ChunkVersion { +public: + /** + * The name for the shard version information field, which shard-aware commands should include + * if they want to convey shard version. + */ + static constexpr StringData kShardVersionField = "shardVersion"_sd; + + ShardVersion(ChunkVersion chunkVersion) : ChunkVersion(chunkVersion) {} + + ShardVersion() : ShardVersion(ChunkVersion()) {} + + static ShardVersion parse(const BSONElement& element) { + auto parsedVersion = + ChunkVersion60Format::parse(IDLParserContext("ShardVersion"), element.Obj()); + auto version = parsedVersion.getVersion(); + return ShardVersion(ChunkVersion({parsedVersion.getEpoch(), parsedVersion.getTimestamp()}, + {version.getSecs(), version.getInc()})); + } + + void serialize(StringData field, BSONObjBuilder* builder) const { + ChunkVersion60Format version; + version.setGeneration({_epoch, _timestamp}); + version.setPlacement(Timestamp(majorVersion(), minorVersion())); + builder->append(field, version.toBSON()); + } +}; + +} // namespace mongo diff --git a/src/mongo/s/sharding_types.idl b/src/mongo/s/sharding_types.idl index acf3509d0ff..0c09974412c 100644 --- a/src/mongo/s/sharding_types.idl +++ b/src/mongo/s/sharding_types.idl @@ -52,3 +52,10 @@ types: serializer: "mongo::DatabaseVersion::toBSON" deserializer: "mongo::DatabaseVersion" + shard_version: + bson_serialization_type: any + description: An object representing a shard version for a collection. + cpp_type: ShardVersion + serializer: "mongo::ShardVersion::serialize" + deserializer: "mongo::ShardVersion::parse" + diff --git a/src/mongo/s/stale_exception.cpp b/src/mongo/s/stale_exception.cpp index c88504b7df4..e15e54753f9 100644 --- a/src/mongo/s/stale_exception.cpp +++ b/src/mongo/s/stale_exception.cpp @@ -30,6 +30,7 @@ #include "mongo/s/stale_exception.h" #include "mongo/base/init.h" +#include "mongo/s/shard_version.h" #include "mongo/util/assert_util.h" namespace mongo { @@ -43,9 +44,12 @@ MONGO_INIT_REGISTER_ERROR_EXTRA_INFO(StaleDbRoutingVersion); void StaleConfigInfo::serialize(BSONObjBuilder* bob) const { bob->append("ns", _nss.ns()); - _received.serializeToBSON("vReceived", bob); - if (_wanted) - _wanted->serializeToBSON("vWanted", bob); + ShardVersion receivedShardVersion(_received); + receivedShardVersion.serialize("vReceived", bob); + if (_wanted) { + ShardVersion wantedShardVersion(*_wanted); + wantedShardVersion.serialize("vWanted", bob); + } invariant(_shardId != ""); bob->append("shardId", _shardId.toString()); @@ -55,12 +59,15 @@ std::shared_ptr<const ErrorExtraInfo> StaleConfigInfo::parse(const BSONObj& obj) auto shardId = obj["shardId"].String(); uassert(ErrorCodes::NoSuchKey, "The shardId field is missing", !shardId.empty()); + const ChunkVersion& receivedVersion = ShardVersion::parse(obj["vReceived"]); return std::make_shared<StaleConfigInfo>(NamespaceString(obj["ns"].String()), - ChunkVersion::parse(obj["vReceived"]), + receivedVersion, [&] { - if (auto vWantedElem = obj["vWanted"]) - return boost::make_optional( - ChunkVersion::parse(vWantedElem)); + if (auto vWantedElem = obj["vWanted"]) { + const ChunkVersion& wantedVersion = + ShardVersion::parse(vWantedElem); + return boost::make_optional(wantedVersion); + } return boost::optional<ChunkVersion>(); }(), ShardId(std::move(shardId))); diff --git a/src/mongo/s/write_ops/batched_command_request.cpp b/src/mongo/s/write_ops/batched_command_request.cpp index c03abf4db62..65017292f3c 100644 --- a/src/mongo/s/write_ops/batched_command_request.cpp +++ b/src/mongo/s/write_ops/batched_command_request.cpp @@ -44,9 +44,9 @@ template <class T> BatchedCommandRequest constructBatchedCommandRequest(const OpMsgRequest& request) { auto batchRequest = BatchedCommandRequest{T::parse(request)}; - auto shardVersionField = request.body[ChunkVersion::kShardVersionField]; + auto shardVersionField = request.body[ShardVersion::kShardVersionField]; if (!shardVersionField.eoo()) { - auto shardVersion = ChunkVersion::parse(shardVersionField); + auto shardVersion = ShardVersion::parse(shardVersionField); if (shardVersion == ChunkVersion::UNSHARDED()) { batchRequest.setDbVersion(DatabaseVersion(request.body)); } @@ -200,7 +200,7 @@ void BatchedCommandRequest::setWriteCommandRequestBase( void BatchedCommandRequest::serialize(BSONObjBuilder* builder) const { _visit([&](auto&& op) { op.serialize({}, builder); }); if (_shardVersion) { - _shardVersion->serializeToBSON(ChunkVersion::kShardVersionField, builder); + ShardVersion(*_shardVersion).serialize(ShardVersion::kShardVersionField, builder); } if (_dbVersion) { diff --git a/src/mongo/s/write_ops/batched_command_request.h b/src/mongo/s/write_ops/batched_command_request.h index ea2512da86d..e0b13566c3d 100644 --- a/src/mongo/s/write_ops/batched_command_request.h +++ b/src/mongo/s/write_ops/batched_command_request.h @@ -34,8 +34,8 @@ #include "mongo/db/ops/write_ops.h" #include "mongo/rpc/op_msg.h" -#include "mongo/s/chunk_version.h" #include "mongo/s/database_version.h" +#include "mongo/s/shard_version.h" #include "mongo/util/overloaded_visitor.h" namespace mongo { |