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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
|
/**
* This test is labeled resource intensive because its total io_write is 90MB compared to a median
* of 5MB across all sharding tests in wiredTiger.
* @tags: [resource_intensive]
*/
function shardSetup(shardConfig, dbName, collName) {
var st = new ShardingTest(shardConfig);
var db = st.getDB(dbName);
// Set the balancer mode to only balance on autoSplit
assert.writeOK(st.s.getDB('config').settings.update(
{_id: 'balancer'},
{'$unset': {stopped: ''}, '$set': {mode: 'autoSplitOnly'}},
{writeConcern: {w: 'majority'}}));
return st;
}
function getShardWithTopChunk(configDB, lowOrHigh, ns) {
// lowOrHigh: 1 low "top chunk", -1 high "top chunk"
print(ns);
print(configDB.chunks.count({"ns": ns}));
print(configDB.chunks.count());
print(JSON.stringify(configDB.chunks.findOne()));
print(JSON.stringify(configDB.chunks.findOne({"ns": {$ne: "config.system.sessions"}})));
return configDB.chunks.find({"ns": ns}).sort({min: lowOrHigh}).limit(1).next().shard;
}
function getNumberOfChunks(configDB) {
return configDB.chunks.count();
}
function runTest(test) {
jsTest.log('Running: ' + tojson(test));
// Setup
// Shard collection
assert.commandWorked(db.adminCommand({shardCollection: coll + "", key: {x: 1}}));
// Pre-split, move chunks & create tags
for (var i = 0; i < test.shards.length; i++) {
var startRange = test.shards[i].range.min;
var endRange = test.shards[i].range.max;
var chunkSize = Math.abs(endRange - startRange) / test.shards[i].chunks;
for (var j = startRange; j < endRange; j += chunkSize) {
// No split on highest chunk
if (j + chunkSize >= MAXVAL) {
continue;
}
assert.commandWorked(db.adminCommand({split: coll + "", middle: {x: j + chunkSize}}));
db.adminCommand({moveChunk: coll + "", find: {x: j}, to: test.shards[i].name});
}
// Make sure to move chunk when there's only 1 chunk in shard
db.adminCommand({moveChunk: coll + "", find: {x: startRange}, to: test.shards[i].name});
// Make sure to move highest chunk
if (test.shards[i].range.max == MAXVAL) {
db.adminCommand({moveChunk: coll + "", find: {x: MAXVAL}, to: test.shards[i].name});
}
// Add tags to each shard
var tags = test.shards[i].tags || [];
for (j = 0; j < tags.length; j++) {
sh.addShardTag(test.shards[i].name, tags[j]);
}
}
// Add tag ranges associated to a tag
var tagRanges = test.tagRanges || [];
for (var j = 0; j < tagRanges.length; j++) {
sh.addTagRange(db + "." + collName,
{x: tagRanges[j].range.min},
{x: tagRanges[j].range.max},
tagRanges[j].tag);
}
// Number of chunks before auto-split
var numChunks = getNumberOfChunks(configDB);
// End of setup
// Insert on top chunk to force auto-split
var largeStr = new Array(1000).join('x');
// Insert one doc at a time until first auto-split occurs on top chunk
var xval = test.inserts.value;
do {
var doc = {x: xval, val: largeStr};
coll.insert(doc);
xval += test.inserts.inc;
} while (getNumberOfChunks(configDB) <= numChunks);
printShardingStatus(configDB);
// Test for where new top chunk should reside
assert.eq(getShardWithTopChunk(configDB, test.lowOrHigh, db + "." + collName),
test.movedToShard,
test.name + " chunk in the wrong shard");
// Cleanup: Drop collection, tags & tag ranges
coll.drop();
for (var i = 0; i < test.shards.length; i++) {
var tags = test.shards[i].tags || [];
for (j = 0; j < tags.length; j++) {
sh.removeShardTag(test.shards[i].name, tags[j]);
}
}
assert.writeOK(configDB.tags.remove({ns: db + "." + collName}));
// End of test cleanup
}
// Define shard key ranges for each of the shard nodes
var MINVAL = -500;
var MAXVAL = 1500;
var lowChunkRange = {min: MINVAL, max: 0};
var midChunkRange1 = {min: 0, max: 500};
var midChunkRange2 = {min: 500, max: 1000};
var highChunkRange = {min: 1000, max: MAXVAL};
var lowChunkTagRange = {min: MinKey, max: 0};
var highChunkTagRange = {min: 1000, max: MaxKey};
var lowChunkInserts = {value: 0, inc: -1};
var midChunkInserts = {value: 1, inc: 1};
var highChunkInserts = {value: 1000, inc: 1};
var lowChunk = 1;
var highChunk = -1;
// Test objects:
// name - name of test
// lowOrHigh - 1 for low top chunk, -1 for high top chunk
// movedToShard - name of shard the new top chunk should reside on
// shards - array of shard objects
// name - name of shard
// range - range object of shard key
// chunks - number of chunks to pre-split on this shard
// tags - array of tags associated to this shard
// tagRanges - array of objects defining the tag range
// range - range object of shard key
// tag - tag associated to this range
// inserts - object for inserting on shard key from low to high
// low - low shard key value
// high - high shard key value
var tests = [
{
// Test auto-split on the "low" top chunk to another tagged shard
name: "low top chunk with tag move",
lowOrHigh: lowChunk,
movedToShard: "shard0002",
shards: [
{name: "shard0000", range: lowChunkRange, chunks: 20, tags: ["NYC"]},
{name: "shard0001", range: midChunkRange1, chunks: 20, tags: ["SF"]},
{name: "shard0002", range: highChunkRange, chunks: 5, tags: ["NYC"]},
{name: "shard0003", range: midChunkRange2, chunks: 1, tags: ["SF"]},
],
tagRanges: [
{range: lowChunkTagRange, tag: "NYC"},
{range: highChunkTagRange, tag: "NYC"},
{range: midChunkRange1, tag: "SF"},
{range: midChunkRange2, tag: "SF"}
],
inserts: lowChunkInserts
},
{
// Test auto-split on the "low" top chunk to same tagged shard
name: "low top chunk with tag no move",
lowOrHigh: lowChunk,
movedToShard: "shard0000",
shards: [
{name: "shard0000", range: lowChunkRange, chunks: 5, tags: ["NYC"]},
{name: "shard0001", range: midChunkRange1, chunks: 20, tags: ["SF"]},
{name: "shard0002", range: highChunkRange, chunks: 20, tags: ["NYC"]},
{name: "shard0003", range: midChunkRange2, chunks: 1, tags: ["SF"]},
],
tagRanges: [
{range: lowChunkTagRange, tag: "NYC"},
{range: highChunkTagRange, tag: "NYC"},
{range: midChunkRange1, tag: "SF"},
{range: midChunkRange2, tag: "SF"}
],
inserts: lowChunkInserts
},
{
// Test auto-split on the "low" top chunk to another shard
name: "low top chunk no tag move",
lowOrHigh: lowChunk,
movedToShard: "shard0003",
shards: [
{name: "shard0000", range: lowChunkRange, chunks: 20},
{name: "shard0001", range: midChunkRange1, chunks: 20},
{name: "shard0002", range: highChunkRange, chunks: 5},
{name: "shard0003", range: midChunkRange2, chunks: 1}
],
inserts: lowChunkInserts
},
{
// Test auto-split on the "high" top chunk to another tagged shard
name: "high top chunk with tag move",
lowOrHigh: highChunk,
movedToShard: "shard0000",
shards: [
{name: "shard0000", range: lowChunkRange, chunks: 5, tags: ["NYC"]},
{name: "shard0001", range: midChunkRange1, chunks: 20, tags: ["SF"]},
{name: "shard0002", range: highChunkRange, chunks: 20, tags: ["NYC"]},
{name: "shard0003", range: midChunkRange2, chunks: 1, tags: ["SF"]}
],
tagRanges: [
{range: lowChunkTagRange, tag: "NYC"},
{range: highChunkTagRange, tag: "NYC"},
{range: midChunkRange1, tag: "SF"},
{range: midChunkRange2, tag: "SF"}
],
inserts: highChunkInserts
},
{
// Test auto-split on the "high" top chunk to another shard
name: "high top chunk no tag move",
lowOrHigh: highChunk,
movedToShard: "shard0003",
shards: [
{name: "shard0000", range: lowChunkRange, chunks: 5},
{name: "shard0001", range: midChunkRange1, chunks: 20},
{name: "shard0002", range: highChunkRange, chunks: 20},
{name: "shard0003", range: midChunkRange2, chunks: 1}
],
inserts: highChunkInserts
},
{
// Test auto-split on the "high" top chunk to same tagged shard
name: "high top chunk with tag no move",
lowOrHigh: highChunk,
movedToShard: "shard0002",
shards: [
{name: "shard0000", range: lowChunkRange, chunks: 20, tags: ["NYC"]},
{name: "shard0001", range: midChunkRange1, chunks: 20, tags: ["SF"]},
{name: "shard0002", range: highChunkRange, chunks: 5, tags: ["NYC"]},
{name: "shard0003", range: midChunkRange2, chunks: 1, tags: ["SF"]}
],
tagRanges: [
{range: lowChunkTagRange, tag: "NYC"},
{range: highChunkTagRange, tag: "NYC"},
{range: midChunkRange1, tag: "SF"},
{range: midChunkRange2, tag: "SF"}
],
inserts: highChunkInserts
},
{
// Test auto-split on the "high" top chunk to same shard
name: "high top chunk no tag no move",
lowOrHigh: highChunk,
movedToShard: "shard0002",
shards: [
{name: "shard0000", range: lowChunkRange, chunks: 20},
{name: "shard0001", range: midChunkRange1, chunks: 20},
{name: "shard0002", range: highChunkRange, chunks: 1},
{name: "shard0003", range: midChunkRange2, chunks: 5}
],
inserts: highChunkInserts
},
];
var singleNodeTests = [
{
// Test auto-split on the "low" top chunk on single node shard
name: "single node shard - low top chunk",
lowOrHigh: lowChunk,
movedToShard: "shard0000",
shards: [{name: "shard0000", range: lowChunkRange, chunks: 2}],
inserts: lowChunkInserts
},
{
// Test auto-split on the "high" top chunk on single node shard
name: "single node shard - high top chunk",
lowOrHigh: highChunk,
movedToShard: "shard0000",
shards: [{name: "shard0000", range: highChunkRange, chunks: 2}],
inserts: highChunkInserts
},
];
var maxSizeTests = [
{
// Test auto-split on the "low" top chunk with maxSize on destination shard
name: "maxSize - low top chunk",
lowOrHigh: lowChunk,
movedToShard: "shard0000",
shards: [
{name: "shard0000", range: lowChunkRange, chunks: 10},
{name: "shard0001", range: highChunkRange, chunks: 1}
],
inserts: lowChunkInserts
},
{
// Test auto-split on the "high" top chunk with maxSize on destination shard
name: "maxSize - high top chunk",
lowOrHigh: highChunk,
movedToShard: "shard0000",
shards: [
{name: "shard0000", range: highChunkRange, chunks: 10},
{name: "shard0001", range: lowChunkRange, chunks: 1}
],
inserts: highChunkInserts
},
];
// Main
var dbName = "TopChunkDB";
var collName = "coll";
var st = shardSetup(
{name: "topchunk", shards: 4, chunkSize: 1, other: {enableAutoSplit: true}}, dbName, collName);
var db = st.getDB(dbName);
var coll = db[collName];
var configDB = st.s.getDB('config');
assert.commandWorked(db.adminCommand({enableSharding: dbName}));
st.ensurePrimaryShard(dbName, 'shard0000');
// Execute all test objects
for (var i = 0; i < tests.length; i++) {
runTest(tests[i]);
}
st.stop();
// Single node shard tests
st = shardSetup({name: "singleNode", shards: 1, chunkSize: 1, other: {enableAutoSplit: true}},
dbName,
collName);
db = st.getDB(dbName);
coll = db[collName];
configDB = st.s.getDB('config');
assert.commandWorked(db.adminCommand({enableSharding: dbName}));
st.ensurePrimaryShard(dbName, 'shard0000');
// Execute all test objects
for (var i = 0; i < singleNodeTests.length; i++) {
runTest(singleNodeTests[i]);
}
st.stop();
// maxSize test
// To set maxSize, must manually add the shards
st = shardSetup({
name: "maxSize",
shards: 2,
chunkSize: 1,
other: {manualAddShard: true, enableAutoSplit: true}
},
dbName,
collName);
db = st.getDB(dbName);
coll = db[collName];
configDB = st.s.getDB('config');
// maxSize on shard0000 - 5MB, on shard0001 - 1MB
assert.commandWorked(db.adminCommand({addshard: st.getConnNames()[0], maxSize: 5}));
assert.commandWorked(db.adminCommand({addshard: st.getConnNames()[1], maxSize: 1}));
// SERVER-17070 Auto split moves to shard node running WiredTiger, if exceeding maxSize
var unsupported = ["wiredTiger", "rocksdb", "inMemory", "ephemeralForTest"];
if (unsupported.indexOf(st.d0.adminCommand({serverStatus: 1}).storageEngine.name) == -1 &&
unsupported.indexOf(st.d1.adminCommand({serverStatus: 1}).storageEngine.name) == -1) {
assert.commandWorked(db.adminCommand({enableSharding: dbName}));
st.ensurePrimaryShard(dbName, 'shard0000');
// Execute all test objects
for (var i = 0; i < maxSizeTests.length; i++) {
runTest(maxSizeTests[i]);
}
}
st.stop();
|