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
|
//
// Tests sharding-related batch write protocol functionality
// NOTE: Basic write functionality is tested via the passthrough tests, this file should contain
// *only* mongos-specific tests.
//
// Checking UUID and index consistency involves talking to the config server primary, but there is
// no config server primary by the end of this test.
TestData.skipCheckingUUIDsConsistentAcrossCluster = true;
TestData.skipCheckingIndexesConsistentAcrossCluster = true;
TestData.skipCheckOrphans = true;
(function() {
"use strict";
var st = new ShardingTest({shards: 2, mongos: 1});
var mongos = st.s0;
var admin = mongos.getDB("admin");
var config = mongos.getDB("config");
var configConnStr = st._configDB;
jsTest.log("Starting sharding batch write tests...");
var request;
var result;
// NOTE: ALL TESTS BELOW SHOULD BE SELF-CONTAINED, FOR EASIER DEBUGGING
//
//
// Mongos _id autogeneration tests for sharded collections
var coll = mongos.getCollection("foo.bar");
assert.commandWorked(admin.runCommand({enableSharding: coll.getDB().toString()}));
st.ensurePrimaryShard(coll.getDB().getName(), st.shard1.shardName);
assert.commandWorked(admin.runCommand({shardCollection: coll.toString(), key: {_id: 1}}));
//
// Basic insert no _id
coll.remove({});
printjson(request = {
insert: coll.getName(),
documents: [{a: 1}]
});
printjson(result = coll.runCommand(request));
assert(result.ok);
assert.eq(1, result.n);
assert.eq(1, coll.count());
//
// Multi insert some _ids
coll.remove({});
printjson(request = {
insert: coll.getName(),
documents: [{_id: 0, a: 1}, {a: 2}]
});
printjson(result = coll.runCommand(request));
assert(result.ok);
assert.eq(2, result.n);
assert.eq(2, coll.count());
assert.eq(1, coll.count({_id: 0}));
//
// Ensure generating many _ids don't push us over limits
var maxDocSize = (16 * 1024 * 1024) / 1000;
var baseDocSize = Object.bsonsize({a: 1, data: ""});
var dataSize = maxDocSize - baseDocSize;
var data = "";
for (var i = 0; i < dataSize; i++)
data += "x";
var documents = [];
for (var i = 0; i < 1000; i++)
documents.push({a: i, data: data});
assert.commandWorked(coll.getMongo().getDB("admin").runCommand({setParameter: 1, logLevel: 4}));
coll.remove({});
request = {
insert: coll.getName(),
documents: documents
};
printjson(result = coll.runCommand(request));
assert(result.ok);
assert.eq(1000, result.n);
assert.eq(1000, coll.count());
//
//
// Config server upserts (against admin db, for example) require _id test
var adminColl = admin.getCollection(coll.getName());
//
// Without _id
adminColl.remove({});
printjson(request = {
update: adminColl.getName(),
updates: [{q: {a: 1}, u: {a: 1}, upsert: true}]
});
var result = adminColl.runCommand(request);
assert.commandWorked(result);
assert.eq(1, result.n);
assert.eq(1, adminColl.count());
//
// With _id
adminColl.remove({});
printjson(request = {
update: adminColl.getName(),
updates: [{q: {_id: 1, a: 1}, u: {a: 1}, upsert: true}]
});
assert.commandWorked(adminColl.runCommand(request));
assert.eq(1, result.n);
assert.eq(1, adminColl.count());
//
//
// Stale config progress tests
// Set up a new collection across two shards, then revert the chunks to an earlier state to put
// mongos and mongod permanently out of sync.
// START SETUP
var brokenColl = mongos.getCollection("broken.coll");
assert.commandWorked(admin.runCommand({enableSharding: brokenColl.getDB().toString()}));
st.ensurePrimaryShard(brokenColl.getDB().toString(), st.shard0.shardName);
assert.commandWorked(admin.runCommand({shardCollection: brokenColl.toString(), key: {_id: 1}}));
assert.commandWorked(admin.runCommand({split: brokenColl.toString(), middle: {_id: 0}}));
var oldChunks = config.chunks.find().toArray();
// Start a new mongos and bring it up-to-date with the chunks so far
var staleMongos = MongoRunner.runMongos({configdb: configConnStr});
brokenColl = staleMongos.getCollection(brokenColl.toString());
assert.commandWorked(brokenColl.insert({hello: "world"}));
// Modify the chunks to make shards at a higher version
assert.commandWorked(
admin.runCommand({moveChunk: brokenColl.toString(), find: {_id: 0}, to: st.shard1.shardName}));
// Rewrite the old chunks back to the config server
assert.commandWorked(config.chunks.remove({}));
for (var i = 0; i < oldChunks.length; i++) {
assert.commandWorked(config.chunks.insert(oldChunks[i]));
}
// Ensure that the inserts have propagated to all secondary nodes
st.configRS.awaitReplication();
// Stale mongos can no longer bring itself up-to-date!
// END SETUP
//
// Config server insert, repeatedly stale
printjson(request = {
insert: brokenColl.getName(),
documents: [{_id: -1}]
});
printjson(result = brokenColl.runCommand(request));
assert(result.ok);
assert.eq(0, result.n);
assert.eq(1, result.writeErrors.length);
assert.eq(0, result.writeErrors[0].index);
assert.eq(result.writeErrors[0].code, 82); // No Progress Made
//
// Config server insert to other shard, repeatedly stale
printjson(request = {
insert: brokenColl.getName(),
documents: [{_id: 1}]
});
printjson(result = brokenColl.runCommand(request));
assert(result.ok);
assert.eq(0, result.n);
assert.eq(1, result.writeErrors.length);
assert.eq(0, result.writeErrors[0].index);
assert.eq(result.writeErrors[0].code, 82); // No Progress Made
//
//
// Tests against config server
var configColl = config.getCollection("batch_write_protocol_sharded");
//
// Basic config server insert
configColl.remove({});
printjson(request = {
insert: configColl.getName(),
documents: [{a: 1}]
});
var result = configColl.runCommand(request);
assert.commandWorked(result);
assert.eq(1, result.n);
st.configRS.awaitReplication();
assert.eq(1, st.config0.getCollection(configColl + "").count());
assert.eq(1, st.config1.getCollection(configColl + "").count());
assert.eq(1, st.config2.getCollection(configColl + "").count());
//
// Basic config server update
configColl.remove({});
configColl.insert({a: 1});
printjson(request = {
update: configColl.getName(),
updates: [{q: {a: 1}, u: {$set: {b: 2}}}]
});
printjson(result = configColl.runCommand(request));
assert(result.ok);
assert.eq(1, result.n);
st.configRS.awaitReplication();
assert.eq(1, st.config0.getCollection(configColl + "").count({b: 2}));
assert.eq(1, st.config1.getCollection(configColl + "").count({b: 2}));
assert.eq(1, st.config2.getCollection(configColl + "").count({b: 2}));
//
// Basic config server delete
configColl.remove({});
configColl.insert({a: 1});
printjson(request = {
'delete': configColl.getName(),
deletes: [{q: {a: 1}, limit: 0}]
});
printjson(result = configColl.runCommand(request));
assert(result.ok);
assert.eq(1, result.n);
st.configRS.awaitReplication();
assert.eq(0, st.config0.getCollection(configColl + "").count());
assert.eq(0, st.config1.getCollection(configColl + "").count());
assert.eq(0, st.config2.getCollection(configColl + "").count());
MongoRunner.stopMongod(st.config1);
MongoRunner.stopMongod(st.config2);
st.configRS.awaitNoPrimary();
// Config server insert with no config PRIMARY
configColl.remove({});
printjson(request = {
insert: configColl.getName(),
documents: [{a: 1}]
});
printjson(result = configColl.runCommand(request));
assert(!result.ok);
assert(result.errmsg != null);
// Config server insert with no config PRIMARY
configColl.remove({});
configColl.insert({a: 1});
printjson(request = {
update: configColl.getName(),
updates: [{q: {a: 1}, u: {$set: {b: 2}}}]
});
printjson(result = configColl.runCommand(request));
assert(!result.ok);
assert(result.errmsg != null);
// Config server insert with no config PRIMARY
configColl.remove({});
configColl.insert({a: 1});
printjson(request = {
delete: configColl.getName(),
deletes: [{q: {a: 1}, limit: 0}]
});
printjson(result = configColl.runCommand(request));
assert(!result.ok);
assert(result.errmsg != null);
jsTest.log("DONE!");
MongoRunner.stopMongos(staleMongos);
st.stop();
}());
|