summaryrefslogtreecommitdiff
path: root/jstests/sharding/move_chunk_update_shard_key_in_retryable_write.js
blob: de6a067a24698bccf7f9fed4cbc16f5c39569f17 (plain)
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/**
 * When we update a shard key such that a document moves from one shard to another, and the
 * update is sent as a retryable write, the update is converted into a multi-statement
 * transaction with the same transaction number. The new transaction contains a delete on the source
 * shard and an insert on the destination shard. If original update is retried, it should receive an
 * error saying that the write can't be retried since it was upgraded to a transaction as part of
 * the update. This should be true whether or not a migration occurs on the chunk containing the
 * original value of the document's shard key. This file tests that behavior.
 * @tags: [uses_transactions, uses_multi_shard_transaction,]
 */
(function() {

"use strict";

load('jstests/sharding/libs/sharded_transactions_helpers.js');
load('jstests/libs/chunk_manipulation_util.js');

// For startParallelOps to write its state
let staticMongod = MongoRunner.runMongod({});

let st = new ShardingTest({
    mongos: 2,
    shards: 3,
    rs: {nodes: 2},
    rsOptions:
        {setParameter: {maxTransactionLockRequestTimeoutMillis: ReplSetTest.kDefaultTimeoutMS}}
});

const dbName = "test";
const collName = "foo";
const ns = dbName + "." + collName;
let mongos0TestDB = st.s0.getDB(dbName);
let mongos0TestColl = mongos0TestDB.getCollection(collName);
let mongos1TestDB = st.s1.getDB(dbName);

// Create a sharded collection with three chunks:
//     [-inf, -10), [-10, 10), [10, inf)
assert.commandWorked(st.s0.adminCommand({enableSharding: dbName}));
assert.commandWorked(st.s0.adminCommand({movePrimary: dbName, to: st.shard0.shardName}));
assert.commandWorked(st.s0.adminCommand({shardCollection: ns, key: {x: 1}}));
assert.commandWorked(st.s0.adminCommand({split: ns, middle: {x: -10}}));
assert.commandWorked(st.s0.adminCommand({split: ns, middle: {x: 10}}));

/**
 * Sets up a test by moving chunks to such that one chunk is on each
 * shard, with the following distribution:
 *     shard0: [-inf, -10)
 *     shard1: [-10, 10)
 *     shard2: [10, inf)
 */
function setUp() {
    assert.commandWorked(
        st.s0.adminCommand({moveChunk: ns, find: {x: -100}, to: st.shard0.shardName}));
    assert.commandWorked(
        st.s0.adminCommand({moveChunk: ns, find: {x: 0}, to: st.shard1.shardName}));
    assert.commandWorked(
        st.s0.adminCommand({moveChunk: ns, find: {x: 1000}, to: st.shard2.shardName}));

    flushRoutersAndRefreshShardMetadata(st, {ns});
}

/**
 * Tears down a test by dropping all documents from the test collection.
 */
function tearDown() {
    assert.commandWorked(mongos0TestColl.deleteMany({}));
}

/**
 * Generic function to run a test. 'description' is a description of the test for logging
 * purposes and 'testBody' is the test function.
 */
function test(description, testBody) {
    jsTest.log(`Running Test Setup: ${description}`);
    setUp();
    jsTest.log(`Running Test Body: ${description}`);
    testBody();
    jsTest.log(`Running Test Tear-Down: ${description}`);
    tearDown();
    jsTest.log(`Finished Running Test: ${description}`);
}

const shardKeyValueOnShard0 = -100;
const shardKeyValueOnShard1 = 0;

// Test commands that change the shard key of a document in the test collection from
// shardKeyValueOnShard0 to shardKeyValueOnShard1. Note we don't test the remove:true case
// because the document can't move shards if it is being deleted.
const updateCmdObjBase = {
    update: collName,
    updates: [
        {q: {x: shardKeyValueOnShard0}, u: {$set: {x: shardKeyValueOnShard1}}},
    ],
    ordered: false,
};

const findAndModifyUpdateCmdObjBase = {
    findAndModify: collName,
    query: {x: shardKeyValueOnShard0},
    update: {$set: {x: shardKeyValueOnShard1}},
};

const findAndModifyUpsertCmdObjBase = {
    findAndModify: collName,
    query: {x: shardKeyValueOnShard0},
    update: {$set: {x: shardKeyValueOnShard1}},
    upsert: true,
};

function attachTxnFields(cmdObj) {
    const cmdObjWithTxnFields = Object.assign({}, cmdObj);
    cmdObjWithTxnFields.lsid = {id: UUID()};
    cmdObjWithTxnFields.txnNumber = NumberLong(35);
    return cmdObjWithTxnFields;
}

{
    // Run the given command and assert the response is as expected.
    function runCommandOnInitialTry(cmdObj) {
        // Insert a single document on shard0. Skip in the upsert case to get coverage where there
        // is no pre-image.
        if (!cmdObj.upsert) {
            mongos0TestColl.insert({x: shardKeyValueOnShard0});
        }

        // Update the document shard key. The document should now be on shard1.
        const result = assert.commandWorked(mongos0TestDB.runCommand(cmdObj));
        if (cmdObj.findAndModify) {
            if (!cmdObj.upsert) {
                assert.eq(result.lastErrorObject.n, 1, tojson(result));
                assert.eq(result.lastErrorObject.updatedExisting, true, tojson(result));
            } else {
                assert.eq(result.lastErrorObject.n, 1, tojson(result));
                assert.eq(result.lastErrorObject.updatedExisting, false, tojson(result));
                assert(result.lastErrorObject.upserted, tojson(result));
            }
        } else {  // update
            assert.eq(result.n, 1, tojson(result));
            assert.eq(result.nModified, 1, tojson(result));
        }
        assert.eq(mongos0TestColl.find({x: shardKeyValueOnShard1}).itcount(), 1);

        assert.commandWorked(mongos0TestColl.deleteMany({}));  // Clean up for the next test case.
    }

    test("Updating shard key in retryable write receives error on retry", () => {
        const updateCmdObj = attachTxnFields(updateCmdObjBase);
        const findAndModifyUpdateCmdObj = attachTxnFields(findAndModifyUpdateCmdObjBase);
        const findAndModifyUpsertCmdObj = attachTxnFields(findAndModifyUpsertCmdObjBase);

        runCommandOnInitialTry(updateCmdObj);
        runCommandOnInitialTry(findAndModifyUpdateCmdObj);
        runCommandOnInitialTry(findAndModifyUpsertCmdObj);

        // Retry the commands. They should run against shard0, which should throw
        // IncompleteTransactionHistory.
        assert.commandFailedWithCode(mongos0TestDB.runCommand(updateCmdObj),
                                     ErrorCodes.IncompleteTransactionHistory);
        assert.commandFailedWithCode(mongos0TestDB.runCommand(findAndModifyUpdateCmdObj),
                                     ErrorCodes.IncompleteTransactionHistory);
        assert.commandFailedWithCode(mongos0TestDB.runCommand(findAndModifyUpsertCmdObj),
                                     ErrorCodes.IncompleteTransactionHistory);
    });

    test("Updating shard key in retryable write receives error on retry when the original chunk " +
             "has been migrated to a new shard",
         () => {
             const updateCmdObj = attachTxnFields(updateCmdObjBase);
             const findAndModifyUpdateCmdObj = attachTxnFields(findAndModifyUpdateCmdObjBase);
             const findAndModifyUpsertCmdObj = attachTxnFields(findAndModifyUpsertCmdObjBase);

             runCommandOnInitialTry(updateCmdObj);
             runCommandOnInitialTry(findAndModifyUpdateCmdObj);
             runCommandOnInitialTry(findAndModifyUpsertCmdObj);

             // Move the chunk that contained the original document to shard1.
             assert.commandWorked(st.s0.adminCommand(
                 {moveChunk: ns, find: {x: shardKeyValueOnShard0}, to: st.shard1.shardName}));

             // Retry the command. This should retry against shard1, which should throw
             // IncompleteTransactionHistory.
             assert.commandFailedWithCode(mongos0TestDB.runCommand(updateCmdObj),
                                          ErrorCodes.IncompleteTransactionHistory);
             assert.commandFailedWithCode(mongos0TestDB.runCommand(findAndModifyUpdateCmdObj),
                                          ErrorCodes.IncompleteTransactionHistory);
             assert.commandFailedWithCode(mongos0TestDB.runCommand(findAndModifyUpsertCmdObj),
                                          ErrorCodes.IncompleteTransactionHistory);
         });

    test("Updating shard key in retryable write receives error on retry when the original chunk " +
             "has been migrated to a new shard and then to a third shard",
         () => {
             const updateCmdObj = attachTxnFields(updateCmdObjBase);
             const findAndModifyUpdateCmdObj = attachTxnFields(findAndModifyUpdateCmdObjBase);
             const findAndModifyUpsertCmdObj = attachTxnFields(findAndModifyUpsertCmdObjBase);

             runCommandOnInitialTry(updateCmdObj);
             runCommandOnInitialTry(findAndModifyUpdateCmdObj);
             runCommandOnInitialTry(findAndModifyUpsertCmdObj);

             // Move the chunk that contained the original document to shard1.
             assert.commandWorked(st.s0.adminCommand(
                 {moveChunk: ns, find: {x: shardKeyValueOnShard0}, to: st.shard1.shardName}));

             // Then move the same chunk that contained the original document to shard2.
             assert.commandWorked(st.s0.adminCommand(
                 {moveChunk: ns, find: {x: shardKeyValueOnShard0}, to: st.shard2.shardName}));

             // Retry the command. This should retry against shard1, which should throw
             // IncompleteTransactionHistory.
             assert.commandFailedWithCode(mongos0TestDB.runCommand(updateCmdObj),
                                          ErrorCodes.IncompleteTransactionHistory);
             assert.commandFailedWithCode(mongos0TestDB.runCommand(findAndModifyUpdateCmdObj),
                                          ErrorCodes.IncompleteTransactionHistory);
             assert.commandFailedWithCode(mongos0TestDB.runCommand(findAndModifyUpsertCmdObj),
                                          ErrorCodes.IncompleteTransactionHistory);
         });

    test("Updating shard key in retryable write receives error on retry when the original chunk " +
             "has been migrated to a shard without knowledge of the transaction",
         () => {
             const updateCmdObj = attachTxnFields(updateCmdObjBase);
             const findAndModifyUpdateCmdObj = attachTxnFields(findAndModifyUpdateCmdObjBase);
             const findAndModifyUpsertCmdObj = attachTxnFields(findAndModifyUpsertCmdObjBase);

             runCommandOnInitialTry(updateCmdObj);
             runCommandOnInitialTry(findAndModifyUpdateCmdObj);
             runCommandOnInitialTry(findAndModifyUpsertCmdObj);

             // Move the chunk that contained the original document to shard2, which does not know
             // about the transaction.
             assert.commandWorked(st.s0.adminCommand(
                 {moveChunk: ns, find: {x: shardKeyValueOnShard0}, to: st.shard2.shardName}));

             // Retry the command. This should retry against shard2, which should throw
             // IncompleteTransactionHistory.
             assert.commandFailedWithCode(mongos0TestDB.runCommand(updateCmdObj),
                                          ErrorCodes.IncompleteTransactionHistory);
             assert.commandFailedWithCode(mongos0TestDB.runCommand(findAndModifyUpdateCmdObj),
                                          ErrorCodes.IncompleteTransactionHistory);
             assert.commandFailedWithCode(mongos0TestDB.runCommand(findAndModifyUpsertCmdObj),
                                          ErrorCodes.IncompleteTransactionHistory);
         });
}

test(
    "config.transactions entries for single-shard transactions which commit during transferMods phase are successfully migrated as dead-end sentinels",
    () => {
        const shardKeyValueOnShard0 = -100;
        const lsid = {id: UUID()};
        const txnNumber = 35;

        // Insert a single document on shard0.
        assert.commandWorked(mongos0TestColl.insert({x: shardKeyValueOnShard0}));

        const cmdToRunInTransaction = {
            update: collName,
            updates: [
                // Add a new field.
                {q: {x: shardKeyValueOnShard0}, u: {$set: {a: 4}}},
            ],
            ordered: false,
            lsid: lsid,
            txnNumber: NumberLong(txnNumber),
            startTransaction: true,
            autocommit: false
        };

        const fakeRetryCmd = {
            update: collName,
            updates: [
                // Add a new field.
                {q: {x: shardKeyValueOnShard0}, u: {$set: {a: 4}}},
            ],
            ordered: false,
            lsid: lsid,
            txnNumber: NumberLong(txnNumber)
        };

        pauseMoveChunkAtStep(st.shard0, moveChunkStepNames.reachedSteadyState);

        let joinMoveChunk = moveChunkParallel(
            staticMongod, st.s0.host, {x: shardKeyValueOnShard0}, null, ns, st.shard1.shardName);

        waitForMoveChunkStep(st.shard0, moveChunkStepNames.reachedSteadyState);

        // Update a document being migrated.
        const result = assert.commandWorked(mongos0TestDB.runCommand(cmdToRunInTransaction));
        assert.eq(result.n, 1);
        assert.eq(result.nModified, 1);

        assert.commandWorked(mongos0TestDB.adminCommand({
            commitTransaction: 1,
            writeConcern: {w: "majority"},
            lsid: lsid,
            txnNumber: NumberLong(txnNumber),
            autocommit: false
        }));

        // Check that the update from the transaction succeeded.
        const resultingDoc = mongos0TestColl.findOne({x: shardKeyValueOnShard0});
        assert.neq(resultingDoc, null);
        assert.eq(resultingDoc["a"], 4);

        unpauseMoveChunkAtStep(st.shard0, moveChunkStepNames.reachedSteadyState);

        // Wait for moveChunk to complete
        joinMoveChunk();

        st.printShardingStatus();
        // Retry the command. This should retry against shard1, which should throw
        // IncompleteTransactionHistory.
        assert.commandFailedWithCode(mongos0TestDB.runCommand(fakeRetryCmd),
                                     ErrorCodes.IncompleteTransactionHistory);
    });

{
    // Run the given command as during the transferMods phase of chunk migration is migrated
    // successfully to shard2, which is not involved in the shard key update.
    function runCommandDuringChunkMigration(cmdObj) {
        const docId = 0;

        // Insert a single document on shard0. Skip in the upsert case to get coverage where there
        // is no pre-image.
        if (!cmdObj.upsert) {
            mongos0TestColl.insert({_id: docId, x: shardKeyValueOnShard0});
        }

        pauseMoveChunkAtStep(st.shard0, moveChunkStepNames.reachedSteadyState);

        // We're going to do a shard key update to move a document from shard0 to shard1, so
        // here we move the chunk from shard0 to shard2, which won't be involved in the
        // transaction created by the shard key update.
        let joinMoveChunk = moveChunkParallel(
            staticMongod, st.s0.host, {x: shardKeyValueOnShard0}, null, ns, st.shard2.shardName);

        waitForMoveChunkStep(st.shard0, moveChunkStepNames.reachedSteadyState);

        // Update the document shard key so that the document will move from shard0 to shard1.
        const result = assert.commandWorked(mongos0TestDB.runCommand(cmdObj));
        if (cmdObj.findAndModify) {
            if (!cmdObj.upsert) {
                assert.eq(result.lastErrorObject.n, 1, tojson(result));
                assert.eq(result.lastErrorObject.updatedExisting, true, tojson(result));
            } else {
                assert.eq(result.lastErrorObject.n, 1, tojson(result));
                assert.eq(result.lastErrorObject.updatedExisting, false, tojson(result));
                assert(result.lastErrorObject.upserted, tojson(result));
            }
        } else {  // update
            assert.eq(result.n, 1, tojson(result));
            assert.eq(result.nModified, 1, tojson(result));
        }

        unpauseMoveChunkAtStep(st.shard0, moveChunkStepNames.reachedSteadyState);

        // Wait for moveChunk to complete
        joinMoveChunk();

        st.printShardingStatus();
    }

    test("Update shard key in retryable write during transferMods phase of chunk migration " +
             "is migrated successfully to a node not involved in the shard key update",
         () => {
             const updateCmdObj = attachTxnFields(updateCmdObjBase);
             runCommandDuringChunkMigration(updateCmdObj);
             // Retry the command. This should retry against shard2, which should throw
             // IncompleteTransactionHistory.
             assert.commandFailedWithCode(mongos0TestDB.runCommand(updateCmdObj),
                                          ErrorCodes.IncompleteTransactionHistory);
         });

    test("findAndModify update shard key in retryable write during transferMods phase of chunk " +
             "migration is migrated successfully to a node not involved in the shard key update",
         () => {
             const findAndModifyUpdateCmdObj = attachTxnFields(findAndModifyUpdateCmdObjBase);
             runCommandDuringChunkMigration(findAndModifyUpdateCmdObj);
             // Retry the command. This should retry against shard2, which should throw
             // IncompleteTransactionHistory.
             assert.commandFailedWithCode(mongos0TestDB.runCommand(findAndModifyUpdateCmdObj),
                                          ErrorCodes.IncompleteTransactionHistory);
         });

    test("findAndModify update shard key with upsert=true in retryable write during " +
             "transferMods phase of chunk migration is migrated successfully to a node not " +
             "involved in the shard key update",
         () => {
             const findAndModifyUpsertCmdObj = attachTxnFields(findAndModifyUpsertCmdObjBase);
             runCommandDuringChunkMigration(findAndModifyUpsertCmdObj);
             // Retry the command. This should retry against shard2.
             if (isUpdateDocumentShardKeyUsingTransactionApiEnabled(st.s0)) {
                 // If internal transactions are enabled, shard2 is expected to throw
                 // IncompleteTransactionHistory since it should have the WouldChangeOwningShard
                 // noop oplog entry copied from shard0 during the migration.
                 assert.commandFailedWithCode(mongos0TestDB.runCommand(findAndModifyUpsertCmdObj),
                                              ErrorCodes.IncompleteTransactionHistory);
             } else {
                 // If internal transactions are not enabled, shard2 is expected not to throw an
                 // error since this is an upsert so on shard0 the applyOps oplog entry would not
                 // have any operations in it and therefore shard2 would not receive a dead-end
                 // sentinel oplog entry for this transaction.

                 // If the retry is done against the original mongos, it should fail with
                 // ConflictingOperationInProgress on the mongos itself when the mongos tries to
                 // convert the retryable write into a transaction again.
                 assert.commandFailedWithCode(mongos0TestDB.runCommand(findAndModifyUpsertCmdObj),
                                              ErrorCodes.ConflictingOperationInProgress);
                 // If the retry is done against a different mongos, it should fail on shard1
                 // when the shard receives a transaction statement with a txnNumber that is already
                 // committed.
                 assert.commandFailedWithCode(mongos1TestDB.runCommand(findAndModifyUpsertCmdObj),
                                              50911);
             }
         });
}

// TODO (SERVER-40815) This test currently fails with DuplicateKeyError on _id.
//
// test(
//    "Update to shard key in retryable write during transfer mods phase of chunk migration is
//    migrated successfully ",
//    () => {
//        const shardKeyValueOnShard0 = -100;
//        const shardKeyValueOnShard1 = 0;
//        const docId = 0;

//        // Insert a single document on shard 0.
//        assert.commandWorked(mongos0TestColl.insert({_id: docId, x: shardKeyValueOnShard0}));

//        const cmdObj = {
//            update: collName,
//            updates: [
//                {q: {x: shardKeyValueOnShard0}, u: {$set: {x: shardKeyValueOnShard1}}},
//            ],
//            ordered: false,
//            lsid: {id: UUID()},
//            txnNumber: NumberLong(35),
//        };

//        pauseMoveChunkAtStep(st.shard0, moveChunkStepNames.reachedSteadyState);

//        let joinMoveChunk = moveChunkParallel(
//            staticMongod, st.s0.host, {x: shardKeyValueOnShard0}, null, ns,
//            st.shard1.shardName);

//        waitForMoveChunkStep(st.shard0, moveChunkStepNames.reachedSteadyState);

//        // Update the document shard key.

//        // THIS CURRENTLY FAILS WITH DuplicateKeyError on _id
//        const result = assert.commandWorked(mongos0TestDB.runCommand(cmdObj));
//        assert.eq(result.n, 1);
//        assert.eq(result.nModified, 1);
//        assert.eq(mongos0TestColl.find({x: shardKeyValueOnShard1}).itcount(), 1);

//        unpauseMoveChunkAtStep(st.shard0, moveChunkStepNames.reachedSteadyState);

//        // Wait for moveChunk to complete
//        joinMoveChunk();

//        st.printShardingStatus();
//        // Retry the command. This should retry against shard 1, which should throw
//        // IncompleteTransactionHistory.
//        assert.commandFailedWithCode(mongos0TestDB.runCommand(cmdObj),
//                                     ErrorCodes.IncompleteTransactionHistory);
//    });

st.stop();
MongoRunner.stopMongod(staticMongod);
})();