Files
mongo/jstests/sharding/reshard_collection_existing_sk_index_not_duplicated.js
Zac 591928c619 SERVER-108478 JS formatted by prettier and remove clang-format (#39656)
GitOrigin-RevId: 6c8f6aded47f260aa4f7c231b17dae3302cb1e04
2025-08-21 17:27:09 +00:00

68 lines
2.4 KiB
JavaScript

//
// Basic tests asserting that an existing index on the new key prevents resharding from creating
// a new index.
//
// @tags: [
// uses_atclustertime,
// ]
//
import {ReshardingTest} from "jstests/sharding/libs/resharding_test_fixture.js";
const reshardingTest = new ReshardingTest();
reshardingTest.setup();
const testCases = [
{ns: "reshardingDb.no_compatible_index"},
{ns: "reshardingDb.has_compatible_index", indexToCreateBeforeResharding: {newKey: 1}},
{
ns: "reshardingDb.compatible_index_with_extra",
indexToCreateBeforeResharding: {newKey: 1, extra: 1},
},
];
for (let {ns, indexToCreateBeforeResharding} of testCases) {
const donorShardNames = reshardingTest.donorShardNames;
const sourceCollection = reshardingTest.createShardedCollection({
ns,
shardKeyPattern: {oldKey: 1},
chunks: [{min: {oldKey: MinKey}, max: {oldKey: MaxKey}, shard: donorShardNames[0]}],
});
if (indexToCreateBeforeResharding !== undefined) {
assert.commandWorked(sourceCollection.createIndex(indexToCreateBeforeResharding));
}
// Create an index which won't be compatible with the {newKey: 1} shard key pattern but should
// still exist post-resharding.
assert.commandWorked(sourceCollection.createIndex({extra: 1}));
const indexesBeforeResharding = sourceCollection.getIndexes();
const recipientShardNames = reshardingTest.recipientShardNames;
reshardingTest.withReshardingInBackground({
newShardKeyPattern: {newKey: 1},
newChunks: [{min: {newKey: MinKey}, max: {newKey: MaxKey}, shard: recipientShardNames[0]}],
});
const indexesAfterResharding = sourceCollection.getIndexes();
if (indexToCreateBeforeResharding !== undefined) {
assert.sameMembers(indexesBeforeResharding, indexesAfterResharding);
} else {
const shardKeyIndexPos = indexesAfterResharding.findIndex((indexInfo) =>
bsonBinaryEqual(indexInfo.key, {newKey: 1}),
);
assert.lte(
0,
shardKeyIndexPos,
`resharding didn't create index on new shard key pattern: ${tojson(indexesAfterResharding)}`,
);
const indexesAfterReshardingToCompare = indexesAfterResharding.slice();
indexesAfterReshardingToCompare.splice(shardKeyIndexPos, 1);
assert.sameMembers(indexesBeforeResharding, indexesAfterReshardingToCompare);
}
}
reshardingTest.teardown();