SERVER-100072 Assert shard key is the same when forceRedistribution passed to reshardCollection (#34686)

GitOrigin-RevId: c3efe38c902eb66274623f1ffaf4babb513e7292
This commit is contained in:
Janna Golden 2025-04-14 12:56:55 -04:00 committed by MongoDB Bot
parent d314d0604d
commit c9700d311e
3 changed files with 31 additions and 4 deletions

View File

@ -120,7 +120,7 @@ assert.commandFailedWithCode(db.adminCommand({
assert.commandFailedWithCode(db.adminCommand({
reshardCollection: ns,
key: {newKey: 1},
key: {oldKey: 1},
forceRedistribution: true,
zones: [
{zone: newZoneName, min: {newKey: 'MinKey'}, max: {newKey: '0'}},
@ -139,6 +139,23 @@ if (!TestData.implicitlyTrackUnshardedCollectionOnCreation) {
[ErrorCodes.NamespaceNotFound, ErrorCodes.NamespaceNotSharded]);
}
// TODO SERVER-103461 Remove version check when 9.0 becomes last LTS.
const binVersion = assert
.commandWorked(db.adminCommand({
serverStatus: 1,
}))
.version;
if (MongoRunner.compareBinVersions(binVersion, "8.2") >= 0) {
jsTest.log(
"Fail if using forceRedistribution and provide a different key than the existing shard key.");
assert.commandFailedWithCode(db.adminCommand({
reshardCollection: ns,
key: {newKey: 1},
forceRedistribution: true,
}),
ErrorCodes.InvalidOptions);
}
/**
* Success cases
*/

View File

@ -118,9 +118,9 @@ const testShardDistribution = (mongos) => {
"reshardCollection cmd should fail when the shardId in shardDistribution is a shard url.");
assert.commandFailedWithCode(mongos.adminCommand({
reshardCollection: ns,
key: {newKey: 1},
forceRedistribution: true,
shardDistribution: [{shard: shardHosts[0], min: {newKey: MinKey}, max: {newKey: MaxKey}}]
key: {oldKey: 1},
forceRedistribution: false,
shardDistribution: [{shard: shardHosts[0], min: {oldKey: MinKey}, max: {oldKey: MaxKey}}]
}),
ErrorCodes.ShardNotFound);

View File

@ -218,6 +218,16 @@ ExecutorFuture<void> ReshardCollectionCoordinator::_runImpl(
uassert(ErrorCodes::NamespaceNotSharded,
"Collection has to be a sharded collection.",
cmOld.isSharded());
if (_doc.getForceRedistribution() && *_doc.getForceRedistribution()) {
uassert(ErrorCodes::InvalidOptions,
str::stream()
<< "The new shard key must be the same as the original shard key "
"when using the forceRedistribution option. The "
"forceRedistribution option is meant for redistributing the "
"collection to a different set of shards.",
cmOld.getShardKeyPattern().isShardKey(_doc.getKey()));
}
}
configsvrReshardCollection.setProvenance(provenance);