1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
/**
* Test to validate that a shard key index cannot be hidden if it cannot be dropped.
* @tags: [
* requires_fcv_61,
* ]
*/
(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();
})();
|