/
update_shard_key_doc_moves_shards.js
373 lines (317 loc) · 16.3 KB
/
update_shard_key_doc_moves_shards.js
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
/*
* Tests that changing the shard key value of a document using update and findAndModify works
* correctly when the doc will change shards.
* @tags: [uses_transactions, uses_multi_shard_transaction]
*/
(function() {
'use strict';
load("jstests/sharding/libs/update_shard_key_helpers.js");
var st = new ShardingTest({mongos: 1, shards: {rs0: {nodes: 3}, rs1: {nodes: 3}}});
var kDbName = 'db';
var mongos = st.s0;
var shard0 = st.shard0.shardName;
var shard1 = st.shard1.shardName;
var ns = kDbName + '.foo';
assert.commandWorked(mongos.adminCommand({enableSharding: kDbName}));
st.ensurePrimaryShard(kDbName, shard0);
// TODO SERVER-39158: Add tests that replaement style updates work as well.
function changeShardKeyWhenFailpointsSet(session, sessionDB, runInTxn, isFindAndModify) {
let docsToInsert = [{"x": 4, "a": 3}, {"x": 100}, {"x": 300, "a": 3}, {"x": 500, "a": 6}];
shardCollectionMoveChunks(st, kDbName, ns, {"x": 1}, docsToInsert, {"x": 100}, {"x": 300});
cleanupOrphanedDocs(st, ns);
// TODO: Remove once SERVER-37677 is done. Read so mongos doesn't get ssv causing shard to
// abort txn
mongos.getDB(kDbName).foo.insert({"x": 505});
// Assert that the document is not updated when the delete fails
assert.commandWorked(st.rs1.getPrimary().getDB(kDbName).adminCommand({
configureFailPoint: "failCommand",
mode: "alwaysOn",
data: {
errorCode: ErrorCodes.WriteConflict,
failCommands: ["delete"],
failInternalCommands: true
}
}));
if (isFindAndModify) {
runFindAndModifyCmdFail(
st, kDbName, session, sessionDB, runInTxn, {"x": 300}, {"$set": {"x": 30}}, false);
} else {
runUpdateCmdFail(st,
kDbName,
session,
sessionDB,
runInTxn,
{"x": 300},
{"$set": {"x": 30}},
false,
ErrorCodes.WriteConflict);
}
assert.commandWorked(st.rs1.getPrimary().getDB(kDbName).adminCommand({
configureFailPoint: "failCommand",
mode: "off",
}));
// Assert that the document is not updated when the insert fails
assert.commandWorked(st.rs0.getPrimary().getDB(kDbName).adminCommand({
configureFailPoint: "failCommand",
mode: "alwaysOn",
data: {
errorCode: ErrorCodes.NamespaceNotFound,
failCommands: ["insert"],
failInternalCommands: true
}
}));
if (isFindAndModify) {
runFindAndModifyCmdFail(
st, kDbName, session, sessionDB, runInTxn, {"x": 300}, {"$set": {"x": 30}}, false);
} else {
runUpdateCmdFail(st,
kDbName,
session,
sessionDB,
runInTxn,
{"x": 300},
{"$set": {"x": 30}},
false,
ErrorCodes.NamespaceNotFound);
}
assert.commandWorked(st.rs0.getPrimary().getDB(kDbName).adminCommand({
configureFailPoint: "failCommand",
mode: "off",
}));
// Assert that the shard key update is not committed when there are no write errors and the
// transaction is explicity aborted.
if (runInTxn) {
session.startTransaction();
if (isFindAndModify) {
sessionDB.foo.findAndModify({query: {"x": 300}, update: {"$set": {"x": 30}}});
} else {
assert.commandWorked(sessionDB.foo.update({"x": 300}, {"$set": {"x": 30}}));
}
session.abortTransaction_forTesting();
assert.eq(1, mongos.getDB(kDbName).foo.find({"x": 300}).itcount());
assert.eq(0, mongos.getDB(kDbName).foo.find({"x": 30}).itcount());
}
mongos.getDB(kDbName).foo.drop();
}
// Test that changing the shard key works correctly when either the update or findAndModify
// command is used and when the command is run either as a retryable write or in a transaction.
// Pairs represent [shouldRunCommandInTxn, runUpdateAsFindAndModifyCmd]
let changeShardKeyOptions = [[false, false], [true, false], [false, true], [true, true]];
changeShardKeyOptions.forEach(function(updatePair) {
let runInTxn = updatePair[0];
let isFindAndModify = updatePair[1];
jsTestLog("Testing changing the shard key using " +
(isFindAndModify ? "findAndModify command " : "update command ") +
(runInTxn ? "in transaction " : "as retryable write"));
let session = st.s.startSession({retryWrites: runInTxn ? false : true});
let sessionDB = session.getDatabase(kDbName);
// Modify updates
// upsert : false
assertCanUpdatePrimitiveShardKey(st,
kDbName,
ns,
session,
sessionDB,
runInTxn,
isFindAndModify,
[{"x": 300}, {"x": 4}],
[{"$set": {"x": 30}}, {"$set": {"x": 600}}],
false);
assertCanUpdateDottedPath(st,
kDbName,
ns,
session,
sessionDB,
runInTxn,
isFindAndModify,
[{"x.a": 300}, {"x.a": 4}],
[{"$set": {"x": {"a": 30}}}, {"$set": {"x": {"a": 600}}}],
false);
assertCanUpdatePartialShardKey(st,
kDbName,
ns,
session,
sessionDB,
runInTxn,
isFindAndModify,
[{"x": 300, "y": 80}, {"x": 4, "y": 3}],
[{"$set": {"x": 30}}, {"$set": {"x": 600}}],
false);
// upsert : true
assertCanUpdatePrimitiveShardKey(st,
kDbName,
ns,
session,
sessionDB,
runInTxn,
isFindAndModify,
[{"x": 300}, {"x": 4}],
[{"$set": {"x": 30}}, {"$set": {"x": 600}}],
true);
assertCanUpdateDottedPath(st,
kDbName,
ns,
session,
sessionDB,
runInTxn,
isFindAndModify,
[{"x.a": 300}, {"x.a": 4}],
[{"$set": {"x": {"a": 30}}}, {"$set": {"x": {"a": 600}}}],
true);
assertCanUpdatePartialShardKey(st,
kDbName,
ns,
session,
sessionDB,
runInTxn,
isFindAndModify,
[{"x": 300, "y": 80}, {"x": 4, "y": 3}],
[{"$set": {"x": 30}}, {"$set": {"x": 600}}],
true);
// failing cases
assertCannotUpdate_id(
st, kDbName, ns, session, sessionDB, runInTxn, isFindAndModify, {"_id": 300}, {
"$set": {"_id": 30}
});
assertCannotUpdate_idDottedPath(
st, kDbName, ns, session, sessionDB, runInTxn, isFindAndModify, {"_id.a": 300}, {
"$set": {"_id": {"a": 30}}
});
assertCannotUpdateSKToArray(
st, kDbName, ns, session, sessionDB, runInTxn, isFindAndModify, {"x": 300}, {
"$set": {"x": [30]}
});
assertCannotUnsetSKField(
st, kDbName, ns, session, sessionDB, runInTxn, isFindAndModify, {"x": 300}, {
"$unset": {"x": 1}
});
if (!isFindAndModify) {
assertCannotUpdateWithMultiTrue(
st, kDbName, ns, session, sessionDB, runInTxn, {"x": 300}, {"$set": {"x": 30}});
}
changeShardKeyWhenFailpointsSet(session, sessionDB, runInTxn, isFindAndModify);
});
let session = st.s.startSession({retryWrites: true});
let sessionDB = session.getDatabase(kDbName);
let docsToInsert =
[{"x": 4, "a": 3}, {"x": 78}, {"x": 100}, {"x": 300, "a": 3}, {"x": 500, "a": 6}];
// ----Assert correct error when changing a doc shard key conflicts with an orphan----
shardCollectionMoveChunks(st, kDbName, ns, {"x": 1}, docsToInsert, {"x": 100}, {"x": 300});
// TODO: Remove once SERVER-37677 is done. Read so mongos doesn't get ssv causing shard to abort
// txn
mongos.getDB(kDbName).foo.insert({"x": 505});
let res = sessionDB.foo.update({"x": 500}, {"$set": {"x": 20}});
assert.commandFailedWithCode(res, ErrorCodes.DuplicateKey);
assert(res.getWriteError().errmsg.includes(
"There is either an orphan for this document or _id for this collection is not globally unique."));
session = st.s.startSession({retryWrites: true});
sessionDB = session.getDatabase(kDbName);
session.startTransaction();
res = sessionDB.foo.update({"x": 500}, {"$set": {"x": 20}});
assert.commandFailedWithCode(res, ErrorCodes.DuplicateKey);
assert(res.errmsg.includes(
"There is either an orphan for this document or _id for this collection is not globally unique."));
session.abortTransaction_forTesting();
mongos.getDB(kDbName).foo.drop();
// ----Assert retryable write result has WCE when the internal commitTransaction fails----
shardCollectionMoveChunks(st, kDbName, ns, {"x": 1}, docsToInsert, {"x": 100}, {"x": 300});
cleanupOrphanedDocs(st, ns);
// TODO: Remove once SERVER-37677 is done. Read so mongos doesn't get ssv causing shard to
// abort txn
mongos.getDB(kDbName).foo.insert({"x": 505});
session = st.s.startSession({retryWrites: true});
sessionDB = session.getDatabase(kDbName);
// Turn on failcommand fail point to fail CoordinateCommitTransaction
assert.commandWorked(st.rs0.getPrimary().getDB(kDbName).adminCommand({
configureFailPoint: "failCommand",
mode: "alwaysOn",
data: {
writeConcernError: {code: NumberInt(12345), errmsg: "dummy error"},
failCommands: ["coordinateCommitTransaction"],
failInternalCommands: true
}
}));
res = sessionDB.foo.update({x: 4}, {$set: {x: 1000}});
assert.commandWorkedIgnoringWriteConcernErrors(res);
assert.eq(12345, res.getWriteConcernError().code);
let findAndModCmd = {
findAndModify: 'foo',
query: {x: 78},
update: {$set: {x: 250}},
lsid: {id: UUID()},
txnNumber: NumberLong(1),
};
res = sessionDB.runCommand(findAndModCmd);
assert.commandWorkedIgnoringWriteConcernErrors(res);
assert.eq(res.writeConcernError.code, 12345);
assert(res.writeConcernError.errmsg.includes("dummy error"));
assert.commandWorked(st.rs0.getPrimary().getDB(kDbName).adminCommand({
configureFailPoint: "failCommand",
mode: "off",
}));
mongos.getDB(kDbName).foo.drop();
// ----Assert that updating the shard key in a batch with size > 1 fails----
assertCannotUpdateInBulkOpWhenDocsMoveShards(st, kDbName, ns, session, sessionDB, false, true);
assertCannotUpdateInBulkOpWhenDocsMoveShards(st, kDbName, ns, session, sessionDB, false, false);
session = st.s.startSession({retryWrites: false});
sessionDB = session.getDatabase(kDbName);
assertCannotUpdateInBulkOpWhenDocsMoveShards(st, kDbName, ns, session, sessionDB, true, true);
assertCannotUpdateInBulkOpWhenDocsMoveShards(st, kDbName, ns, session, sessionDB, true, false);
// ----Multiple writes in txn-----
// Update two docs, updating one twice
shardCollectionMoveChunks(st, kDbName, ns, {"x": 1}, docsToInsert, {"x": 100}, {"x": 300});
cleanupOrphanedDocs(st, ns);
// TODO: Remove once SERVER-37677 is done. Read so mongos doesn't get ssv causing shard to abort
// txn
mongos.getDB(kDbName).foo.insert({"x": 505});
session.startTransaction();
let id = mongos.getDB(kDbName).foo.find({"x": 500}).toArray()[0]._id;
assert.commandWorked(sessionDB.foo.update({"x": 500}, {"$set": {"x": 30}}));
assert.commandWorked(sessionDB.foo.update({"x": 30}, {"$set": {"x": 600}}));
assert.commandWorked(sessionDB.foo.update({"x": 4}, {"$set": {"x": 50}}));
assert.commandWorked(session.commitTransaction_forTesting());
assert.eq(0, mongos.getDB(kDbName).foo.find({"x": 500}).itcount());
assert.eq(0, mongos.getDB(kDbName).foo.find({"x": 30}).itcount());
assert.eq(1, mongos.getDB(kDbName).foo.find({"x": 600}).itcount());
assert.eq(id, mongos.getDB(kDbName).foo.find({"x": 600}).toArray()[0]._id);
assert.eq(0, mongos.getDB(kDbName).foo.find({"x": 4}).itcount());
assert.eq(1, mongos.getDB(kDbName).foo.find({"x": 50}).itcount());
mongos.getDB(kDbName).foo.drop();
// Check that doing $inc on doc A, then updating shard key for doc A, then $inc again only incs
// once
shardCollectionMoveChunks(st, kDbName, ns, {"x": 1}, docsToInsert, {"x": 100}, {"x": 300});
cleanupOrphanedDocs(st, ns);
// TODO: Remove once SERVER-37677 is done. Read so mongos doesn't get ssv causing shard to abort
// txn
mongos.getDB(kDbName).foo.insert({"x": 505});
session.startTransaction();
assert.commandWorked(sessionDB.foo.update({"x": 500}, {"$inc": {"a": 1}}));
assert.commandWorked(sessionDB.foo.update({"x": 500}, {"$set": {"x": 30}}));
assert.commandWorked(sessionDB.foo.update({"x": 500}, {"$inc": {"a": 1}}));
assert.commandWorked(session.commitTransaction_forTesting());
assert.eq(0, mongos.getDB(kDbName).foo.find({"x": 500}).itcount());
assert.eq(1, mongos.getDB(kDbName).foo.find({"x": 30}).itcount());
assert.eq(1, mongos.getDB(kDbName).foo.find({"a": 7}).itcount());
assert.eq(1, mongos.getDB(kDbName).foo.find({"x": 30, "a": 7}).itcount());
mongos.getDB(kDbName).foo.drop();
shardCollectionMoveChunks(st, kDbName, ns, {"x": 1}, docsToInsert, {"x": 100}, {"x": 300});
cleanupOrphanedDocs(st, ns);
// TODO: Remove once SERVER-37677 is done. Read so mongos doesn't get ssv causing shard to abort
// txn
mongos.getDB(kDbName).foo.insert({"x": 505});
// Insert and $inc before moving doc
session.startTransaction();
id = mongos.getDB(kDbName).foo.find({"x": 500}).toArray()[0]._id;
assert.commandWorked(sessionDB.foo.insert({"x": 1, "a": 1}));
assert.commandWorked(sessionDB.foo.update({"x": 500}, {"$inc": {"a": 1}}));
sessionDB.foo.findAndModify({query: {"x": 500}, update: {$set: {"x": 20}}});
assert.commandWorked(session.commitTransaction_forTesting());
assert.eq(0, mongos.getDB(kDbName).foo.find({"x": 500}).toArray().length);
assert.eq(1, mongos.getDB(kDbName).foo.find({"x": 20}).toArray().length);
assert.eq(20, mongos.getDB(kDbName).foo.find({"_id": id}).toArray()[0].x);
assert.eq(1, mongos.getDB(kDbName).foo.find({"a": 7}).toArray().length);
assert.eq(1, mongos.getDB(kDbName).foo.find({"x": 20, "a": 7}).toArray().length);
assert.eq(1, mongos.getDB(kDbName).foo.find({"x": 1}).toArray().length);
mongos.getDB(kDbName).foo.drop();
st.stop();
})();