summaryrefslogtreecommitdiff
path: root/jstests
diff options
context:
space:
mode:
authorPol Pinol Castuera <pol.pinol@mongodb.com>2022-11-11 11:04:57 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2022-11-11 11:52:04 +0000
commitdd6c21b4f7c6d32161a0c262dbd63a8c86b2a44d (patch)
treef606aac7e94a6359cf698d09bd44a64180642d38 /jstests
parent29e83c1741e5a27d950576f10077cd972d6285fc (diff)
downloadmongo-dd6c21b4f7c6d32161a0c262dbd63a8c86b2a44d.tar.gz
SERVER-68769 If the shard key index cannot be dropped, it cannot be hidden.
Diffstat (limited to 'jstests')
-rw-r--r--jstests/sharding/hidden_index.js80
-rw-r--r--jstests/sharding/timeseries_coll_mod.js9
2 files changed, 84 insertions, 5 deletions
diff --git a/jstests/sharding/hidden_index.js b/jstests/sharding/hidden_index.js
new file mode 100644
index 00000000000..9b4cce7d516
--- /dev/null
+++ b/jstests/sharding/hidden_index.js
@@ -0,0 +1,80 @@
+/**
+ * Test to validate that a shard key index cannot be hidden if it cannot be dropped.
+ * @tags: [
+ * requires_fcv_62,
+ * ]
+ */
+
+(function() {
+'use strict';
+load("jstests/libs/index_catalog_helpers.js"); // For IndexCatalogHelpers.findByName.
+
+// Test to validate the correct behaviour of hiding an index in a sharded cluster with a shard key.
+function validateHiddenIndexBehaviour() {
+ let index_type = 1;
+ let index_name = "a_" + index_type;
+ assert.commandWorked(coll.createIndex({"a": index_type}));
+
+ let idxSpec = IndexCatalogHelpers.findByName(coll.getIndexes(), index_name);
+ assert.eq(idxSpec.hidden, undefined);
+
+ assert.commandWorked(coll.hideIndex(index_name));
+ idxSpec = IndexCatalogHelpers.findByName(coll.getIndexes(), index_name);
+ assert(idxSpec.hidden);
+
+ assert.commandWorked(coll.unhideIndex(index_name));
+ idxSpec = IndexCatalogHelpers.findByName(coll.getIndexes(), index_name);
+ assert.eq(idxSpec.hidden, undefined);
+
+ assert.commandWorked(coll.dropIndex(index_name));
+ assert.commandWorked(coll.createIndex({"a": index_type}, {hidden: true}));
+
+ idxSpec = IndexCatalogHelpers.findByName(coll.getIndexes(), index_name);
+ assert(idxSpec.hidden);
+ assert.commandWorked(coll.dropIndexes());
+}
+
+// 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(
+ testDb.runCommand({"collMod": coll.getName(), "index": {"name": "skey_1", "hidden": true}}),
+ ErrorCodes.InvalidOptions);
+}
+
+// Check that command will fail when we try to hide or drop an index that is the last shard key
+// index of the collection
+function validateDifferentHiddenIndexesBehaviour() {
+ // Create index on skey
+ assert.commandWorked(coll.createIndex({skey: 1, anotherkey: 1}));
+
+ assert.commandWorked(testDb.runCommand(
+ {"collMod": coll.getName(), "index": {"name": "skey_1", "hidden": true}}));
+
+ assert.commandFailedWithCode(
+ testDb.runCommand(
+ {"collMod": coll.getName(), "index": {"name": "skey_1_anotherkey_1", "hidden": true}}),
+ ErrorCodes.InvalidOptions);
+
+ assert.commandFailed(coll.dropIndex("skey_1_anotherkey_1"));
+}
+
+// Configure initial sharded cluster
+const st = new ShardingTest({shards: 2});
+const mongos = st.s;
+const testDb = mongos.getDB("test");
+const coll = testDb.getCollection("foo");
+
+// Enable sharding at collection 'foo' and create a new shard key
+assert.commandWorked(st.s.adminCommand({enableSharding: testDb.getName()}));
+
+// Crate a new shard key
+assert.commandWorked(
+ st.s.adminCommand({shardcollection: testDb.getName() + '.' + coll.getName(), key: {skey: 1}}));
+
+validateHiddenIndexBehaviour();
+validateOneShardKeyHiddenIndexBehaviour();
+validateDifferentHiddenIndexesBehaviour();
+
+st.stop();
+})();
diff --git a/jstests/sharding/timeseries_coll_mod.js b/jstests/sharding/timeseries_coll_mod.js
index 08e882ef64a..67be3a51bfc 100644
--- a/jstests/sharding/timeseries_coll_mod.js
+++ b/jstests/sharding/timeseries_coll_mod.js
@@ -60,11 +60,10 @@ function runBasicTest() {
key: {[metaField]: 1},
}));
- // Normal collMod commands works for the sharded time-series collection.
- assert.commandWorked(
- db.runCommand({collMod: collName, index: {name: indexName, hidden: true}}));
- assert.commandWorked(
- db.runCommand({collMod: collName, index: {name: indexName, hidden: false}}));
+ // Check that collMod commands works for the sharded time-series collection.
+ assert.commandWorked(db[collName].createIndex({'a': 1}));
+ assert.commandWorked(db.runCommand({collMod: collName, index: {name: 'a_1', hidden: true}}));
+ assert.commandWorked(db.runCommand({collMod: collName, index: {name: 'a_1', hidden: false}}));
// Granularity update works for sharded time-series collection, when we're using DDL
// coordinator logic.