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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
|
/**
* Test to demonstrate usage of $indexStats in an aggregation pipeline to detect inconsistent
* indexes in a sharded cluster.
*/
(function() {
"use strict";
load("jstests/aggregation/extras/utils.js"); // For documentEq.
// This test deliberately creates indexes in an inconsistent state.
TestData.skipCheckingIndexesConsistentAcrossCluster = true;
const testName = "detect_inconsistent_indexes";
const st = new ShardingTest({shards: 3});
const dbName = "test";
// Pipeline used to detect inconsistent indexes.
const pipeline = [
// Get indexes and the shards that they belong to.
{$indexStats: {}},
// Attach a list of all shards which reported indexes to each document from $indexStats.
{$group: {_id: null, indexDoc: {$push: "$$ROOT"}, allShards: {$addToSet: "$shard"}}},
// Unwind the generated array back into an array of index documents.
{$unwind: "$indexDoc"},
// Group by index name.
{
$group: {
"_id": "$indexDoc.name",
"shards": {$push: "$indexDoc.shard"},
// Index specs are stored as BSON objects and may have fields in any order, but there is
// currently no way to cleanly compare objects ignoring field order in an aggregation,
// so convert each spec into an array of its properties instead.
"specs": {$push: {$objectToArray: {$ifNull: ["$indexDoc.spec", {}]}}},
"allShards": {$first: "$allShards"}
}
},
// Compute which indexes are not present on all targeted shards and which index spec properties
// aren't the same across all shards.
{
$project: {
missingFromShards: {$setDifference: ["$allShards", "$shards"]},
inconsistentProperties: {
$setDifference: [
{$reduce: {
input: "$specs",
initialValue: {$arrayElemAt: ["$specs", 0]},
in: {$setUnion: ["$$value", "$$this"]}}},
{$reduce: {
input: "$specs",
initialValue: {$arrayElemAt: ["$specs", 0]},
in: {$setIntersection: ["$$value", "$$this"]}}}
]
}
}
},
// Only return output that indicates an index was inconsistent, i.e. either a shard was missing
// an index or a property on at least one shard was not the same on all others.
{
$match: {
$expr:
{$or: [
{$gt: [{$size: "$missingFromShards"}, 0]},
{$gt: [{$size: "$inconsistentProperties"}, 0]},
]
}
}
},
// Output relevant fields.
{$project: {_id: 0, indexName: "$$ROOT._id", inconsistentProperties: 1, missingFromShards: 1}}
];
assert.commandWorked(st.s.adminCommand({enableSharding: dbName}));
st.ensurePrimaryShard(dbName, st.shard0.shardName);
function shardCollectionWithChunkOnEachShard(collName) {
const ns = dbName + "." + collName;
assert.commandWorked(st.s.adminCommand({shardCollection: ns, key: {_id: 1}}));
assert.commandWorked(st.s.adminCommand({split: ns, middle: {_id: 0}}));
assert.commandWorked(st.s.adminCommand({split: ns, middle: {_id: 100}}));
assert.commandWorked(
st.s.adminCommand({moveChunk: ns, find: {_id: 0}, to: st.shard1.shardName}));
assert.commandWorked(
st.s.adminCommand({moveChunk: ns, find: {_id: 100}, to: st.shard2.shardName}));
}
//
// Cases with consistent indexes.
//
(() => {
jsTestLog("No indexes on any shard...");
const collName = "noIndexes";
shardCollectionWithChunkOnEachShard(collName);
const res = st.s.getDB(dbName)[collName].aggregate(pipeline).toArray();
assert.eq(res.length, 0, tojson(res));
})();
(() => {
jsTestLog("Index on each shard...");
const collName = "indexOnEachShard";
shardCollectionWithChunkOnEachShard(collName);
assert.commandWorked(st.s.getDB(dbName)[collName].createIndex({x: 1}));
const res = st.s.getDB(dbName)[collName].aggregate(pipeline).toArray();
assert.eq(res.length, 0, tojson(res));
})();
(() => {
jsTestLog("Index on each shard with chunks...");
const collName = "indexOnEachShardWithChunks";
shardCollectionWithChunkOnEachShard(collName);
// Move the chunk off shard2.
assert.commandWorked(st.s.adminCommand(
{moveChunk: dbName + "." + collName, find: {_id: 100}, to: st.shard1.shardName}));
assert.commandWorked(st.s.getDB(dbName)[collName].createIndex({x: 1}));
const res = st.s.getDB(dbName)[collName].aggregate(pipeline).toArray();
assert.eq(res.length, 0, tojson(res));
})();
(() => {
jsTestLog("Index on each shard with chunks not on primary shard...");
const collName = "indexOnEachShardWithChunksNotPrimary";
shardCollectionWithChunkOnEachShard(collName);
// Move the chunk off the primary shard.
assert.commandWorked(st.s.adminCommand(
{moveChunk: dbName + "." + collName, find: {_id: -1}, to: st.shard1.shardName}));
assert.commandWorked(st.s.getDB(dbName)[collName].createIndex({x: 1}));
const res = st.s.getDB(dbName)[collName].aggregate(pipeline).toArray();
assert.eq(res.length, 0, tojson(res));
})();
(() => {
jsTestLog("Index on each shard with expireAfterSeconds...");
const collName = "indexOnEachShardWithTTL";
shardCollectionWithChunkOnEachShard(collName);
assert.commandWorked(
st.s.getDB(dbName)[collName].createIndex({x: 1}, {expireAfterSeconds: 101}));
const res = st.s.getDB(dbName)[collName].aggregate(pipeline).toArray();
assert.eq(res.length, 0, tojson(res));
})();
(() => {
jsTestLog("Same options but in different orders...");
const collName = "sameOptionsDiffOrders";
shardCollectionWithChunkOnEachShard(collName);
assert.commandWorked(st.shard0.getDB(dbName)[collName].createIndex(
{_id: 1, x: 1},
{collation: {locale: "fr"}, partialFilterExpression: {x: {$gt: 50}}, unique: true}));
assert.commandWorked(st.shard1.getDB(dbName)[collName].createIndex(
{_id: 1, x: 1},
{partialFilterExpression: {x: {$gt: 50}}, unique: true, collation: {locale: "fr"}}));
assert.commandWorked(st.shard2.getDB(dbName)[collName].createIndex(
{_id: 1, x: 1},
{unique: true, partialFilterExpression: {x: {$gt: 50}}, collation: {locale: "fr"}}));
const res = st.s.getDB(dbName)[collName].aggregate(pipeline).toArray();
assert.eq(res.length, 0, tojson(res));
})();
//
// Cases with inconsistent indexes.
//
(() => {
jsTestLog("Not on one shard...");
const collName = "notOnOneShard";
shardCollectionWithChunkOnEachShard(collName);
assert.commandWorked(st.shard1.getDB(dbName)[collName].createIndex({x: 1}));
assert.commandWorked(st.shard2.getDB(dbName)[collName].createIndex({x: 1}));
const res = st.s.getDB(dbName)[collName].aggregate(pipeline).toArray();
assert.eq(res.length, 1, tojson(res));
assert(documentEq(res[0], {
indexName: "x_1",
missingFromShards: [st.shard0.shardName],
inconsistentProperties: [],
}),
tojson(res));
})();
(() => {
jsTestLog("Not on two shards...");
const collName = "notOnTwoShards";
shardCollectionWithChunkOnEachShard(collName);
assert.commandWorked(st.shard1.getDB(dbName)[collName].createIndex({x: 1}));
const res = st.s.getDB(dbName)[collName].aggregate(pipeline).toArray();
assert.eq(res.length, 1, tojson(res));
assert(documentEq(res[0], {
indexName: "x_1",
missingFromShards: [st.shard0.shardName, st.shard2.shardName],
inconsistentProperties: [],
}),
tojson(res));
})();
(() => {
jsTestLog("Different keys...");
const collName = "differentKeys";
shardCollectionWithChunkOnEachShard(collName);
assert.commandWorked(st.shard0.getDB(dbName)[collName].createIndex({x: 1}, {name: "diffKeys"}));
assert.commandWorked(st.shard1.getDB(dbName)[collName].createIndex({y: 1}, {name: "diffKeys"}));
assert.commandWorked(st.shard2.getDB(dbName)[collName].createIndex({z: 1}, {name: "diffKeys"}));
const res = st.s.getDB(dbName)[collName].aggregate(pipeline).toArray();
assert.eq(res.length, 1, tojson(res));
assert(documentEq(res[0], {
indexName: "diffKeys",
missingFromShards: [],
inconsistentProperties:
[{k: "key", v: {x: 1}}, {k: "key", v: {y: 1}}, {k: "key", v: {z: 1}}],
}),
tojson(res));
})();
(() => {
jsTestLog("Different property...");
const collName = "differentTTL";
shardCollectionWithChunkOnEachShard(collName);
assert.commandWorked(
st.shard0.getDB(dbName)[collName].createIndex({x: 1}, {expireAfterSeconds: 105}));
assert.commandWorked(
st.shard1.getDB(dbName)[collName].createIndex({x: 1}, {expireAfterSeconds: 106}));
assert.commandWorked(
st.shard2.getDB(dbName)[collName].createIndex({x: 1}, {expireAfterSeconds: 107}));
const res = st.s.getDB(dbName)[collName].aggregate(pipeline).toArray();
assert.eq(res.length, 1, tojson(res));
assert(documentEq(res[0], {
indexName: "x_1",
missingFromShards: [],
inconsistentProperties: [
{k: "expireAfterSeconds", v: 105},
{k: "expireAfterSeconds", v: 106},
{k: "expireAfterSeconds", v: 107}
],
}),
tojson(res));
})();
(() => {
jsTestLog("Missing property...");
const collName = "missingTTL";
shardCollectionWithChunkOnEachShard(collName);
assert.commandWorked(
st.shard0.getDB(dbName)[collName].createIndex({x: 1}, {expireAfterSeconds: 105}));
assert.commandWorked(st.shard1.getDB(dbName)[collName].createIndex({x: 1}));
assert.commandWorked(st.shard2.getDB(dbName)[collName].createIndex({x: 1}));
const res = st.s.getDB(dbName)[collName].aggregate(pipeline).toArray();
assert.eq(res.length, 1, tojson(res));
assert(documentEq(res[0], {
indexName: "x_1",
missingFromShards: [],
inconsistentProperties: [{k: "expireAfterSeconds", v: 105}],
}),
tojson(res));
})();
(() => {
jsTestLog("Multiple different parameters...");
const collName = "multipleDifferent";
shardCollectionWithChunkOnEachShard(collName);
assert.commandWorked(st.shard0.getDB(dbName)[collName].createIndex(
{x: 1}, {expireAfterSeconds: 100, partialFilterExpression: {x: {$gt: 50}}}));
assert.commandWorked(st.shard1.getDB(dbName)[collName].createIndex(
{x: 1}, {expireAfterSeconds: 101, partialFilterExpression: {x: {$gt: 51}}}));
assert.commandWorked(st.shard2.getDB(dbName)[collName].createIndex(
{x: 1}, {expireAfterSeconds: 102, partialFilterExpression: {x: {$gt: 52}}}));
const res = st.s.getDB(dbName)[collName].aggregate(pipeline).toArray();
assert.eq(res.length, 1, tojson(res));
assert(documentEq(res[0], {
indexName: "x_1",
missingFromShards: [],
inconsistentProperties: [
{k: "expireAfterSeconds", v: 100},
{k: "expireAfterSeconds", v: 101},
{k: "expireAfterSeconds", v: 102},
{k: "partialFilterExpression", v: {x: {$gt: 50}}},
{k: "partialFilterExpression", v: {x: {$gt: 51}}},
{k: "partialFilterExpression", v: {x: {$gt: 52}}}
],
}),
tojson(res));
})();
(() => {
jsTestLog("Missing and different parameters and missing from one shard...");
const collName = "missingDifferentParametersAndMissingFromShard";
shardCollectionWithChunkOnEachShard(collName);
assert.commandWorked(st.shard0.getDB(dbName)[collName].createIndex(
{x: 1}, {expireAfterSeconds: 101, partialFilterExpression: {x: {$gt: 50}}}));
assert.commandWorked(
st.shard1.getDB(dbName)[collName].createIndex({x: 1}, {expireAfterSeconds: 100}));
const res = st.s.getDB(dbName)[collName].aggregate(pipeline).toArray();
assert.eq(res.length, 1, tojson(res));
assert(documentEq(res[0], {
indexName: "x_1",
missingFromShards: [st.shard2.shardName],
inconsistentProperties: [
{k: "expireAfterSeconds", v: 100},
{k: "expireAfterSeconds", v: 101},
{k: "partialFilterExpression", v: {x: {$gt: 50}}},
],
}),
tojson(res));
})();
st.stop();
})();
|