summaryrefslogtreecommitdiff
path: root/jstests/core/check_shard_index.js
blob: 2beb3c12891d8c20fe4a791dcf3c70fb66d2cc13 (plain)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// @tags: [
//   requires_fastcount,
//   requires_non_retryable_writes,
//   requires_sharding,
//   no_selinux,
// ]

// -------------------------
//  CHECKSHARDINGINDEX TEST UTILS
// -------------------------

f = db.jstests_shardingindex;
f.drop();

// -------------------------
// Case 1: all entries filled or empty should make a valid index
//

f.drop();
f.createIndex({x: 1, y: 1});
assert.eq(0, f.count(), "1. initial count should be zero");

res = db.runCommand({checkShardingIndex: "test.jstests_shardingindex", keyPattern: {x: 1, y: 1}});
assert.eq(true, res.ok, "1a");

f.save({x: 1, y: 1});
assert.eq(1, f.count(), "1. count after initial insert should be 1");
res = db.runCommand({checkShardingIndex: "test.jstests_shardingindex", keyPattern: {x: 1, y: 1}});
assert.eq(true, res.ok, "1b");

// -------------------------
// Case 2: entry with null values would make an index suitable
//

f.drop();
f.createIndex({x: 1, y: 1});
assert.eq(0, f.count(), "2. initial count should be zero");

f.save({x: 1, y: 1});
f.save({x: null, y: 1});

res = db.runCommand({checkShardingIndex: "test.jstests_shardingindex", keyPattern: {x: 1, y: 1}});
assert.eq(true, res.ok, "2a " + tojson(res));

f.save({y: 2});
assert.eq(3, f.count(), "2. count after initial insert should be 3");
res = db.runCommand({checkShardingIndex: "test.jstests_shardingindex", keyPattern: {x: 1, y: 1}});
assert.eq(true, res.ok, "2b " + tojson(res));

// Check _id index
res = db.runCommand({checkShardingIndex: "test.jstests_shardingindex", keyPattern: {_id: 1}});
assert.eq(true, res.ok, "2c " + tojson(res));
assert(res.idskip, "2d " + tojson(res));

// -------------------------
// Case 3: entry with array values would make an index unsuitable
//

f.drop();
f.createIndex({x: 1, y: 1});
assert.eq(0, f.count(), "3. initial count should be zero");

f.save({x: 1, y: 1});
f.save({x: [1, 2], y: 2});

assert.eq(2, f.count(), "3. count after initial insert should be 2");
res = db.runCommand({checkShardingIndex: "test.jstests_shardingindex", keyPattern: {x: 1, y: 1}});
assert.eq(false, res.ok, "3a " + tojson(res));

f.remove({y: 2});
f.dropIndex({x: 1, y: 1});
f.createIndex({x: 1, y: 1});

assert.eq(1, f.count(), "3. count after removing array value should be 1");
res = db.runCommand({checkShardingIndex: "test.jstests_shardingindex", keyPattern: {x: 1, y: 1}});
assert.eq(true, res.ok, "3b " + tojson(res));

f.save({x: 2, y: [1, 2]});

assert.eq(2, f.count(), "3. count after adding array value should be 2");
res = db.runCommand({checkShardingIndex: "test.jstests_shardingindex", keyPattern: {x: 1, y: 1}});
assert.eq(false, res.ok, "3c " + tojson(res));

// -------------------------
// Case 4: Handles prefix shard key indexes.
//

f.drop();
f.createIndex({x: 1, y: 1, z: 1});
assert.eq(0, f.count(), "4. initial count should be zero");

f.save({x: 1, y: 1, z: 1});

assert.eq(1, f.count(), "4. count after initial insert should be 1");
res = db.runCommand({checkShardingIndex: "test.jstests_shardingindex", keyPattern: {x: 1}});
assert.eq(true, res.ok, "4a " + tojson(res));

res = db.runCommand({checkShardingIndex: "test.jstests_shardingindex", keyPattern: {x: 1, y: 1}});
assert.eq(true, res.ok, "4b " + tojson(res));

f.save({x: [1, 2], y: 2, z: 2});

assert.eq(2, f.count(), "4. count after adding array value should be 2");
res = db.runCommand({checkShardingIndex: "test.jstests_shardingindex", keyPattern: {x: 1}});
assert.eq(false, res.ok, "4c " + tojson(res));
res = db.runCommand({checkShardingIndex: "test.jstests_shardingindex", keyPattern: {x: 1, y: 1}});
assert.eq(false, res.ok, "4d " + tojson(res));
res = db.runCommand(
    {checkShardingIndex: "test.jstests_shardingindex", keyPattern: {x: 1, y: 1, z: 1}});
assert.eq(false, res.ok, "4e " + tojson(res));

f.remove({y: 2});
f.dropIndex({x: 1, y: 1, z: 1});
f.createIndex({x: 1, y: 1, z: 1});

assert.eq(1, f.count(), "4. count after removing array value should be 1");
res = db.runCommand(
    {checkShardingIndex: "test.jstests_shardingindex", keyPattern: {x: 1, y: 1, z: 1}});
assert.eq(true, res.ok, "4f " + tojson(res));

f.save({x: 3, y: [1, 2], z: 3});

assert.eq(2, f.count(), "4. count after adding array value on second key should be 2");
res = db.runCommand({checkShardingIndex: "test.jstests_shardingindex", keyPattern: {x: 1}});
assert.eq(false, res.ok, "4g " + tojson(res));
res = db.runCommand({checkShardingIndex: "test.jstests_shardingindex", keyPattern: {x: 1, y: 1}});
assert.eq(false, res.ok, "4h " + tojson(res));
res = db.runCommand(
    {checkShardingIndex: "test.jstests_shardingindex", keyPattern: {x: 1, y: 1, z: 1}});
assert.eq(false, res.ok, "4i " + tojson(res));

f.remove({x: 3});
// Necessary so that the index is no longer marked as multikey
f.dropIndex({x: 1, y: 1, z: 1});
f.createIndex({x: 1, y: 1, z: 1});

assert.eq(1, f.count(), "4. count after removing array value should be 1 again");
res = db.runCommand(
    {checkShardingIndex: "test.jstests_shardingindex", keyPattern: {x: 1, y: 1, z: 1}});
assert.eq(true, res.ok, "4e " + tojson(res));

f.save({x: 4, y: 4, z: [1, 2]});

assert.eq(2, f.count(), "4. count after adding array value on third key should be 2");
res = db.runCommand({checkShardingIndex: "test.jstests_shardingindex", keyPattern: {x: 1}});
assert.eq(false, res.ok, "4c " + tojson(res));
res = db.runCommand({checkShardingIndex: "test.jstests_shardingindex", keyPattern: {x: 1, y: 1}});
assert.eq(false, res.ok, "4d " + tojson(res));
res = db.runCommand(
    {checkShardingIndex: "test.jstests_shardingindex", keyPattern: {x: 1, y: 1, z: 1}});
assert.eq(false, res.ok, "4e " + tojson(res));

// -------------------------
// Test error messages of checkShardingIndex failing:

// Shard key is not a prefix of index key:
f.drop();
f.createIndex({x: 1});
res = db.runCommand({checkShardingIndex: "test.jstests_shardingindex", keyPattern: {y: 1}});
assert.eq(false, res.ok);
assert(res.errmsg.includes("Shard key is not a prefix of index key."));

// Index key is partial:
f.drop();
f.createIndex({x: 1, y: 1}, {partialFilterExpression: {y: {$gt: 0}}});
res = db.runCommand({checkShardingIndex: "test.jstests_shardingindex", keyPattern: {x: 1, y: 1}});
assert.eq(false, res.ok);
assert(res.errmsg.includes("Index key is partial."));

// Index key is sparse:
f.drop();
f.createIndex({x: 1, y: 1}, {sparse: true});
res = db.runCommand({checkShardingIndex: "test.jstests_shardingindex", keyPattern: {x: 1, y: 1}});
assert.eq(false, res.ok);
assert(res.errmsg.includes("Index key is sparse."));

// Index key is multikey:
f.drop();
f.createIndex({x: 1, y: 1});
f.save({y: [1, 2, 3, 4, 5]});
res = db.runCommand({checkShardingIndex: "test.jstests_shardingindex", keyPattern: {x: 1, y: 1}});
assert.eq(false, res.ok);
assert(res.errmsg.includes("Index key is multikey."));

// Index key has a non-simple collation:
f.drop();
f.createIndex({x: 1, y: 1}, {collation: {locale: "en"}});
res = db.runCommand({checkShardingIndex: "test.jstests_shardingindex", keyPattern: {x: 1, y: 1}});
assert.eq(false, res.ok);
assert(res.errmsg.includes("Index has a non-simple collation."));

// Index key is sparse and index has non-simple collation:
f.drop();
f.createIndex({x: 1, y: 1}, {sparse: true, collation: {locale: "en"}});
res = db.runCommand({checkShardingIndex: "test.jstests_shardingindex", keyPattern: {x: 1, y: 1}});
assert.eq(false, res.ok);
assert(res.errmsg.includes("Index key is sparse.") &&
       res.errmsg.includes("Index has a non-simple collation."));

// Multiple incompatible indexes: Index key is multikey and is partial:
f.drop();
f.createIndex({x: 1, y: 1}, {name: "index_1_part", partialFilterExpression: {x: {$gt: 0}}});
f.createIndex({x: 1, y: 1}, {name: "index_2"});
f.save({y: [1, 2, 3, 4, 5]});
res = db.runCommand({checkShardingIndex: "test.jstests_shardingindex", keyPattern: {x: 1, y: 1}});
assert.eq(false, res.ok);
assert(res.errmsg.includes("Index key is multikey.") &&
       res.errmsg.includes("Index key is partial."));

// Multiple incompatible indexes: Index key is partial and sparse:
f.drop();
f.createIndex({x: 1, y: 1}, {name: "index_1_part", partialFilterExpression: {x: {$gt: 0}}});
f.createIndex({x: 1, y: 1}, {name: "index_2_sparse", sparse: true});
res = db.runCommand({checkShardingIndex: "test.jstests_shardingindex", keyPattern: {x: 1, y: 1}});
assert.eq(false, res.ok);
assert(res.errmsg.includes("Index key is partial.") && res.errmsg.includes("Index key is sparse."));

print("PASSED");