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
|
/*
* Tests that during an upgrade from a replica set to a sharded cluster the CRUD and DDL command
* works. This implies testing those commands on a replica set directly when it is in a sharded
* cluster.
* @tags: [
* requires_persistence,
* multiversion_incompatible
* ]
*/
(function() {
'use strict';
load('jstests/replsets/rslib.js');
const expectedDocs = 1000;
const dbName = 'test';
const collName = 'foo';
const otherCollName = "bar";
const DDLDbName = 'DDLTest';
const DDLCollName = 'DDLFoo';
let str = 'a';
while (str.length < 8000) {
str += str;
}
let map = function() {
emit(this.i, this.j);
};
let reduce = function(key, values) {
let jCount = 0;
values.forEach(function(j) {
jCount += j;
});
return jCount;
};
const CRUDCommands = {
find: {
command: {find: collName},
assertFunc: (res, testDB) => {
assert.commandWorked(res);
assert.eq(expectedDocs, new DBCommandCursor(testDB, res).itcount());
}
},
count: {
command: {count: collName},
assertFunc: (res) => {
assert.commandWorked(res);
assert.eq(expectedDocs, res.n);
}
},
dbstats: {
command: {dbstats: 1},
assertFunc: (res) => {
assert.commandWorked(res);
}
},
collstats: {
command: {collstats: collName},
assertFunc: (res) => {
assert.commandWorked(res);
}
},
mapreduce: {
command: {mapreduce: collName, map: map, reduce: reduce, out: {inline: 1}},
assertFunc: (res) => {
assert.commandWorked(res);
assert.eq(100, res.results.length);
assert.eq(45, res.results[0].value);
}
},
aggregate: {
command: {
aggregate: collName,
pipeline: [{$project: {j: 1}}, {$group: {_id: 'j', sum: {$sum: '$j'}}}],
cursor: {}
},
assertFunc: (res) => {
assert.commandWorked(res);
assert.eq(4500, res.cursor.firstBatch[0].sum);
}
},
aggregateWithLookup: {
command: {
aggregate: collName,
pipeline: [
{$sort: {j: 1}},
{$lookup: {from: otherCollName, localField: "j", foreignField: "i", as: "lookedUp"}}
],
cursor: {}
},
assertFunc: (res) => {
assert.commandWorked(res);
assert.eq(res.cursor.firstBatch[0].lookedUp.length, 10);
}
},
aggregateWithGraphLookup: {
command: {
aggregate: collName,
pipeline: [
{$sort: {j: 1}},
{
$graphLookup: {
from: otherCollName,
startWith: "$j",
connectFromField: "j",
connectToField: "i",
as: "graphLookedUp",
maxDepth: 10
}
}
],
cursor: {}
},
assertFunc: (res) => {
assert.commandWorked(res);
assert.eq(res.cursor.firstBatch[0].graphLookedUp.length, 100);
}
},
insert: {
command: {insert: collName, documents: [{a: 1, i: 1, j: 1}]},
assertFunc: (res) => {
assert.commandWorked(res);
assert.eq(1, res.n);
}
},
update: {
command: {update: collName, updates: [{q: {a: 1, i: 1, j: 1}, u: {$set: {u: 1}}}]},
assertFunc: (res, testDB) => {
assert.commandWorked(res);
assert.eq(1, res.n);
assert.eq(1, testDB.foo.findOne({a: 1}).u);
}
},
findAndModify: {
command: {findAndModify: collName, query: {a: 1, i: 1, j: 1}, update: {$set: {b: 1}}},
assertFunc: (res, testDB) => {
assert.commandWorked(res);
assert.eq(1, res.value.a);
assert.eq(null, res.value.b);
assert.eq(1, testDB.foo.findOne({a: 1}).b);
}
},
remove: {
command: {delete: collName, deletes: [{q: {a: 1}, limit: 1}]},
assertFunc: (res) => {
assert.commandWorked(res);
assert.eq(1, res.n);
}
},
mapreduceWithWrite: {
command: {mapreduce: collName, map: map, reduce: reduce, out: 'mrOutput'},
assertFunc: (res, testDB) => {
assert.commandWorked(res);
assert.eq(100, testDB.mrOutput.count());
assert.eq(45, testDB.mrOutput.findOne().value);
},
post: (testDB) => {
testDB.mrOutput.remove({});
}
},
aggregateWithOut: {
command: {
aggregate: collName,
pipeline:
[{$project: {j: 1}}, {$group: {_id: 'j', sum: {$sum: '$j'}}}, {$out: 'aggOutput'}],
cursor: {}
},
assertFunc: (res, testDB) => {
assert.commandWorked(res);
assert.eq(4500, testDB.aggOutput.findOne().sum);
},
post: (testDB) => {
testDB.aggOutput.remove({});
}
},
aggregateWithMerge: {
command: {
aggregate: collName,
pipeline: [{
$merge: {
into: otherCollName,
whenMatched: [{$set: {merged: true}}],
whenNotMatched: "fail"
}
}],
cursor: {}
},
assertFunc: (res, testDB) => {
assert.commandWorked(res);
assert.eq(testDB[otherCollName].findOne().merged, true);
}
}
};
const DDLCommands = {
createCollection: {
command: {create: DDLCollName},
assertFunc: (res, testDB) => {
assert.commandWorked(res);
assert.eq(0, testDB.runCommand({count: DDLCollName}).n);
}
},
createIndex: {
pre: (testDB) => {
let res = testDB.runCommand({insert: DDLCollName, documents: [{a: 1}]});
assert.commandWorked(res);
assert.eq(1, res.n);
},
command: {createIndexes: DDLCollName, indexes: [{key: {a: 1}, name: 'a_1'}]},
assertFunc: (res) => {
assert.commandWorked(res);
}
},
dropIndex: {
command: {dropIndexes: DDLCollName, index: ['a_1']},
assertFunc: (res) => {
assert.commandWorked(res);
}
},
dropCollection: {
command: {drop: DDLCollName},
assertFunc: (res, testDB) => {
assert.commandWorked(res);
assert.eq(0, testDB.runCommand({count: DDLCollName}).n);
}
},
dropDatabase: {
command: {dropDatabase: 1},
assertFunc: (res, testDB) => {
assert.commandWorked(res);
assert.eq(0, testDB.runCommand({count: DDLCollName}).n);
}
}
};
let assertAddShardSucceeded = function(res, shardName) {
assert.commandWorked(res);
// If a shard name was specified, make sure that the name the addShard command reports the
// shard was added with matches the specified name.
if (shardName) {
assert.eq(shardName,
res.shardAdded,
"name returned by addShard does not match name specified in addShard");
}
// Make sure the shard shows up in config.shards with the shardName reported by the
// addShard command.
assert.neq(null,
st.s.getDB('config').shards.findOne({_id: res.shardAdded}),
"newly added shard " + res.shardAdded + " not found in config.shards");
};
var removeShardWithName = function(st, shardName) {
var res = st.s.adminCommand({removeShard: shardName});
assert.commandWorked(res);
assert.eq('started', res.state);
assert.soon(function() {
res = st.s.adminCommand({removeShard: shardName});
assert.commandWorked(res);
return ('completed' === res.state);
}, "removeShard never completed for shard " + shardName);
};
let checkCRUDCommands = function(testDB) {
for (let command in CRUDCommands) {
jsTestLog('Testing CRUD command: ' + command);
CRUDCommands[command].assertFunc(testDB.runCommand(CRUDCommands[command].command), testDB);
}
};
let checkDDLCommands = function(testDB) {
for (let command in DDLCommands) {
jsTestLog('Testing DDL command: ' + command);
if (DDLCommands[command].pre) {
DDLCommands[command].pre(testDB);
}
DDLCommands[command].assertFunc(testDB.runCommand(DDLCommands[command].command), testDB);
}
};
jsTest.log("Creating replica set.");
let rst0 = new ReplSetTest({name: 'rs0', nodes: 2});
rst0.startSet();
rst0.initiate();
waitForAllMembers(rst0.getPrimary().getDB(dbName));
let coll = rst0.getPrimary().getDB(dbName).getCollection(collName);
// Initial set up.
for (var i = 0; i < 100; i++) {
var bulk = coll.initializeUnorderedBulkOp();
for (var j = 0; j < 10; j++) {
bulk.insert({i: i, j: j, str: str});
}
assert.commandWorked(bulk.execute({w: "majority"}));
}
// Create collection 'otherCollName' as a duplicate of the original collection. This is just an easy
// way of providing a second collection for $lookup, $graphLookup and $merge aggregations.
assert.commandWorked(coll.runCommand("aggregate", {pipeline: [{$out: otherCollName}], cursor: {}}));
jsTest.log("Runing control tests.");
checkCRUDCommands(rst0.getPrimary().getDB(dbName));
checkDDLCommands(rst0.getPrimary().getDB(DDLDbName));
let st = new ShardingTest({
shards: 0,
mongos: 1,
});
rst0.restart(0, {shardsvr: ''});
rst0.restart(1, {shardsvr: ''});
rst0.awaitReplication();
let addShardRes = st.s.adminCommand({addShard: rst0.getURL(), name: rst0.name});
assertAddShardSucceeded(addShardRes, rst0.name);
jsTest.log("First test, using the rs connection directly.");
checkCRUDCommands(rst0.getPrimary().getDB(dbName));
checkDDLCommands(rst0.getPrimary().getDB(DDLDbName));
jsTest.log("Second test, using the router.");
checkCRUDCommands(st.s0.getDB(dbName));
checkDDLCommands(st.s0.getDB(DDLDbName));
// Cleaning up.
rst0.stopSet();
st.stop();
})();
|