diff options
author | Faustoleyva54 <fausto.leyva@mongodb.com> | 2022-02-07 14:28:35 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2022-02-07 16:53:18 +0000 |
commit | 59e19dc9bff797fc1cff45d56942e5e61da36f1a (patch) | |
tree | 086234ff19df114172cc18a3811ef05d23154e65 | |
parent | c99e4833db76bba9842cf9ee9add83aaba0c340a (diff) | |
download | mongo-59e19dc9bff797fc1cff45d56942e5e61da36f1a.tar.gz |
SERVER-62453 Add collectionUUID parameter to refineCollectionShardKey command
5 files changed, 89 insertions, 0 deletions
diff --git a/jstests/sharding/collection_uuid_refine_collection_shard_key.js b/jstests/sharding/collection_uuid_refine_collection_shard_key.js new file mode 100644 index 00000000000..8634801b943 --- /dev/null +++ b/jstests/sharding/collection_uuid_refine_collection_shard_key.js @@ -0,0 +1,73 @@ +/** + * Tests the collectionUUID parameter of the refineCollectionShardKey command. + * + * @tags: [ + * featureFlagCommandsAcceptCollectionUUID, + * ] + */ +(function() { +'use strict'; + +const st = new ShardingTest({shards: 1}); +const mongos = st.s0; +const db = mongos.getDB(jsTestName()); +const coll = db['coll']; +assert.commandWorked(mongos.adminCommand({enableSharding: db.getName()})); + +const oldKeyDoc = { + _id: 1, + a: 1 +}; +const newKeyDoc = { + _id: 1, + a: 1, + b: 1, + c: 1 +}; + +const resetColl = function(shardedColl) { + shardedColl.drop(); + assert.commandWorked(shardedColl.insert({_id: 0, a: 1, b: 2, c: 3})); + assert.commandWorked(mongos.getCollection(shardedColl.getFullName()).createIndex(newKeyDoc)); + assert.commandWorked( + mongos.adminCommand({shardCollection: shardedColl.getFullName(), key: oldKeyDoc})); +}; + +const uuid = function() { + return assert.commandWorked(db.runCommand({listCollections: 1})) + .cursor.firstBatch.find(c => c.name === coll.getName()) + .info.uuid; +}; + +resetColl(coll); + +// The command succeeds when provided with the correct collection UUID. +assert.commandWorked(mongos.adminCommand( + {refineCollectionShardKey: coll.getFullName(), key: newKeyDoc, collectionUUID: uuid()})); + +// The command fails when provided with a UUID with no corresponding collection. +resetColl(coll); +const nonexistentUUID = UUID(); +let res = assert.commandFailedWithCode(mongos.adminCommand({ + refineCollectionShardKey: coll.getFullName(), + key: newKeyDoc, + collectionUUID: nonexistentUUID, +}), + ErrorCodes.CollectionUUIDMismatch); +assert.eq(res.collectionUUID, nonexistentUUID); +assert.eq(res.actualNamespace, ""); + +// The command fails when provided with a different collection's UUID. +const coll2 = db['coll_2']; +resetColl(coll2); +res = assert.commandFailedWithCode(mongos.adminCommand({ + refineCollectionShardKey: coll2.getFullName(), + key: newKeyDoc, + collectionUUID: uuid(), +}), + ErrorCodes.CollectionUUIDMismatch); +assert.eq(res.collectionUUID, uuid()); +assert.eq(res.actualNamespace, coll.getFullName()); + +st.stop(); +})(); diff --git a/src/mongo/db/s/refine_collection_shard_key_coordinator.cpp b/src/mongo/db/s/refine_collection_shard_key_coordinator.cpp index e4f7b101cdd..45ab58a3889 100644 --- a/src/mongo/db/s/refine_collection_shard_key_coordinator.cpp +++ b/src/mongo/db/s/refine_collection_shard_key_coordinator.cpp @@ -31,7 +31,9 @@ #include "mongo/db/s/refine_collection_shard_key_coordinator.h" +#include "mongo/db/catalog/collection_uuid_mismatch.h" #include "mongo/db/commands.h" +#include "mongo/db/db_raii.h" #include "mongo/db/s/dist_lock_manager.h" #include "mongo/db/s/sharding_ddl_util.h" #include "mongo/logv2/log.h" @@ -134,6 +136,11 @@ ExecutorFuture<void> RefineCollectionShardKeyCoordinator::_runImpl( sharding_ddl_util::stopMigrations(opCtx, nss(), boost::none); } + { + AutoGetCollection coll{opCtx, nss(), MODE_IS}; + checkCollectionUUIDMismatch(opCtx, *coll, _doc.getCollectionUUID()); + } + const auto cmdResponse = uassertStatusOK(configShard->runCommand( opCtx, ReadPreferenceSetting(ReadPreference::PrimaryOnly), diff --git a/src/mongo/s/commands/cluster_refine_collection_shard_key_cmd.cpp b/src/mongo/s/commands/cluster_refine_collection_shard_key_cmd.cpp index ce3fa693d57..ddd2628bef0 100644 --- a/src/mongo/s/commands/cluster_refine_collection_shard_key_cmd.cpp +++ b/src/mongo/s/commands/cluster_refine_collection_shard_key_cmd.cpp @@ -68,6 +68,7 @@ public: // Send it to the primary shard RefineCollectionShardKeyRequest requestParamObj; requestParamObj.setNewShardKey(request().getKey()); + requestParamObj.setCollectionUUID(request().getCollectionUUID()); ShardsvrRefineCollectionShardKey refineCollectionShardKeyCommand(nss); refineCollectionShardKeyCommand.setRefineCollectionShardKeyRequest(requestParamObj); diff --git a/src/mongo/s/request_types/refine_collection_shard_key.idl b/src/mongo/s/request_types/refine_collection_shard_key.idl index 3bff6d38279..62bd493aa64 100644 --- a/src/mongo/s/request_types/refine_collection_shard_key.idl +++ b/src/mongo/s/request_types/refine_collection_shard_key.idl @@ -47,6 +47,10 @@ commands: type: object description: "The index specification document to use as the new shard key." optional: false + collectionUUID: + type: uuid + description: "The expected UUID of the collection." + optional: true _configsvrRefineCollectionShardKey: command_name: _configsvrRefineCollectionShardKey diff --git a/src/mongo/s/request_types/sharded_ddl_commands.idl b/src/mongo/s/request_types/sharded_ddl_commands.idl index f2938874265..0f4b46d655f 100644 --- a/src/mongo/s/request_types/sharded_ddl_commands.idl +++ b/src/mongo/s/request_types/sharded_ddl_commands.idl @@ -161,6 +161,10 @@ structs: type: KeyPattern description: "The index specification document to use as the new shard key." optional: false + collectionUUID: + type: uuid + description: "The expected UUID of the collection." + optional: true ReshardCollectionRequest: description: "Parameters for the reshard collection command" |