diff options
author | Pol Pinol Castuera <pol.pinol@mongodb.com> | 2022-12-19 07:59:00 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2022-12-19 08:51:35 +0000 |
commit | 24f7086280a10a2d3f8a19ae42900a270c455bba (patch) | |
tree | 729eb925750db74d6dd77db623733e99d83d8f30 | |
parent | fd2dd311397aa3671e69ae4e4d98056e8006620f (diff) | |
download | mongo-24f7086280a10a2d3f8a19ae42900a270c455bba.tar.gz |
SERVER-71638 Don't let to hide the last shard key index using a key pattern
-rw-r--r-- | etc/backports_required_for_multiversion_tests.yml | 4 | ||||
-rw-r--r-- | jstests/noPassthrough/collmod_convert_to_unique_sharded.js | 9 | ||||
-rw-r--r-- | jstests/sharding/hidden_index.js | 6 | ||||
-rw-r--r-- | src/mongo/db/catalog/coll_mod.cpp | 2 |
4 files changed, 15 insertions, 6 deletions
diff --git a/etc/backports_required_for_multiversion_tests.yml b/etc/backports_required_for_multiversion_tests.yml index 6834273a58a..8819f0e52bd 100644 --- a/etc/backports_required_for_multiversion_tests.yml +++ b/etc/backports_required_for_multiversion_tests.yml @@ -290,6 +290,8 @@ last-continuous: ticket: SERVER-71667 - test_file: jstests/sharding/drop_collection.js ticket: SERVER-71689 + - test_file: jstests/sharding/hidden_index.js + ticket: SERVER-71638 suites: null last-lts: all: @@ -655,4 +657,6 @@ last-lts: ticket: SERVER-71667 - test_file: jstests/sharding/drop_collection.js ticket: SERVER-71689 + - test_file: jstests/sharding/hidden_index.js + ticket: SERVER-71638 suites: null diff --git a/jstests/noPassthrough/collmod_convert_to_unique_sharded.js b/jstests/noPassthrough/collmod_convert_to_unique_sharded.js index 0a02ffd1c1e..5f486497b75 100644 --- a/jstests/noPassthrough/collmod_convert_to_unique_sharded.js +++ b/jstests/noPassthrough/collmod_convert_to_unique_sharded.js @@ -57,8 +57,7 @@ assert.commandFailedWithCode(shardedColl.insert({_id: 4, a: 2}), ErrorCodes.Dupl // Try converting the index to unique and make sure no indexes are converted on any shards. assert.commandFailedWithCode( - db.runCommand( - {collMod: shardedColl.getName(), index: {keyPattern: {a: 1}, hidden: true, unique: true}}), + db.runCommand({collMod: shardedColl.getName(), index: {keyPattern: {a: 1}, unique: true}}), ErrorCodes.CannotConvertIndexToUnique); const s0Coll = st.shard0.getDB(jsTestName()).getCollection("sharded"); @@ -78,8 +77,8 @@ assert.eq(countPrepareUniqueIndexes(s1Coll, {a: 1}), // Remove the duplicate and confirm the indexes are converted. assert.commandWorked(shardedColl.deleteOne({_id: 2})); -assert.commandWorked(db.runCommand( - {collMod: shardedColl.getName(), index: {keyPattern: {a: 1}, hidden: true, unique: true}})); +assert.commandWorked( + db.runCommand({collMod: shardedColl.getName(), index: {keyPattern: {a: 1}, unique: true}})); assert.eq(countUniqueIndexes(s0Coll, {a: 1}), 1, 'index should be unique: ' + tojson(s0Coll.getIndexes())); @@ -88,4 +87,4 @@ assert.eq(countUniqueIndexes(s1Coll, {a: 1}), 'index should be unique: ' + tojson(s1Coll.getIndexes())); st.stop(); -})();
\ No newline at end of file +})(); diff --git a/jstests/sharding/hidden_index.js b/jstests/sharding/hidden_index.js index 6bf2c00cdf5..5dfdce03dfb 100644 --- a/jstests/sharding/hidden_index.js +++ b/jstests/sharding/hidden_index.js @@ -36,6 +36,7 @@ function validateHiddenIndexBehaviour() { // Check that command will fail when we try to hide the only shard key index of the collection function validateOneShardKeyHiddenIndexBehaviour() { + assert.commandFailedWithCode(coll.hideIndex({skey: 1}), ErrorCodes.InvalidOptions); assert.commandFailedWithCode(coll.hideIndex("skey_1"), ErrorCodes.InvalidOptions); assert.commandFailedWithCode( testDb.runCommand({"collMod": coll.getName(), "index": {"name": "skey_1", "hidden": true}}), @@ -48,6 +49,11 @@ function validateDifferentHiddenIndexesBehaviour() { // Create index on skey assert.commandWorked(coll.createIndex({skey: 1, anotherkey: 1})); + // Check that is possible to hide a shard key index using its key pattern + assert.commandWorked(coll.hideIndex({skey: 1})); + assert.commandWorked(coll.unhideIndex({skey: 1})); + + // Check that is possible to hide a shard key index using its name assert.commandWorked(testDb.runCommand( {"collMod": coll.getName(), "index": {"name": "skey_1", "hidden": true}})); diff --git a/src/mongo/db/catalog/coll_mod.cpp b/src/mongo/db/catalog/coll_mod.cpp index d0daf7d9872..a085acab5fa 100644 --- a/src/mongo/db/catalog/coll_mod.cpp +++ b/src/mongo/db/catalog/coll_mod.cpp @@ -300,7 +300,7 @@ StatusWith<std::pair<ParsedCollModRequest, BSONObj>> parseCollModRequest(Operati if (isLastNonHiddenShardKeyIndex(opCtx, coll, coll->getIndexCatalog(), - indexName.toString(), + cmrIndex->idx->indexName(), shardedColl.getKeyPattern().toBSON())) { return {ErrorCodes::InvalidOptions, "Cannot hide the only compatible index for this collection's shard " |