summaryrefslogtreecommitdiff
path: root/jstests/sharding/move_chunk_update_shard_key_in_retryable_write.js
blob: 32a33e21a82b527b47606d0430aecbbad2bf0708 (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
/**
 * 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/libs/retryable_writes_util.js");
load('jstests/sharding/libs/sharded_transactions_helpers.js');
load('./jstests/libs/chunk_manipulation_util.js');

if (!RetryableWritesUtil.storageEngineSupportsRetryableWrites(jsTest.options().storageEngine)) {
    jsTestLog("Retryable writes are not supported, skipping test");
    return;
}

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

let st = new ShardingTest({shards: {rs0: {nodes: 2}, rs1: {nodes: 2}, rs2: {nodes: 2}}});

const dbName = "test";
const collName = "foo";
const ns = dbName + "." + collName;
let testDB = st.s.getDB(dbName);
let testColl = testDB.foo;

// Create a sharded collection with three chunks:
//     [-inf, -10), [-10, 10), [10, inf)
assert.commandWorked(st.s.adminCommand({enableSharding: dbName}));
assert.commandWorked(st.s.adminCommand({movePrimary: dbName, to: st.shard0.shardName}));
assert.commandWorked(st.s.adminCommand({shardCollection: ns, key: {x: 1}}));
assert.commandWorked(st.s.adminCommand({split: ns, middle: {x: -10}}));
assert.commandWorked(st.s.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.s.adminCommand({moveChunk: ns, find: {x: -100}, to: st.shard0.shardName}));
    assert.commandWorked(st.s.adminCommand({moveChunk: ns, find: {x: 0}, to: st.shard1.shardName}));
    assert.commandWorked(
        st.s.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(testColl.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}`);
}

test("Updating shard key in retryable write receives error on retry", () => {
    const shardKeyValueOnShard0 = -100;
    const shardKeyValueOnShard1 = 0;

    // Insert a single document on shard 0.
    testColl.insert({x: shardKeyValueOnShard0});

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

    // Update the document shard key. The document should now be on shard 1.
    const result = assert.commandWorked(testDB.runCommand(cmdObj));
    assert.eq(result.n, 1);
    assert.eq(result.nModified, 1);
    assert.eq(testColl.find({x: shardKeyValueOnShard1}).itcount(), 1);

    // Retry the command. This should retry against shard 0, which should throw
    // IncompleteTransactionHistory.
    assert.commandFailedWithCode(testDB.runCommand(cmdObj),
                                 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 shardKeyValueOnShard0 = -100;
        const shardKeyValueOnShard1 = 0;

        // Insert a single document on shard 0.
        testColl.insert({x: shardKeyValueOnShard0});

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

        // Update the document shard key. The document should now be on shard 1.
        const result = assert.commandWorked(testDB.runCommand(cmdObj));
        assert.eq(result.n, 1);
        assert.eq(result.nModified, 1);
        assert.eq(testColl.find({x: shardKeyValueOnShard1}).itcount(), 1);

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

        // Retry the command. This should retry against shard 1, which should throw
        // IncompleteTransactionHistory.
        assert.commandFailedWithCode(testDB.runCommand(cmdObj),
                                     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 shardKeyValueOnShard0 = -100;
        const shardKeyValueOnShard1 = 0;

        // Insert a single document on shard 0.
        testColl.insert({x: shardKeyValueOnShard0});

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

        // Update the document shard key. The document should now be on shard 1.
        const result = assert.commandWorked(testDB.runCommand(cmdObj));
        assert.eq(result.n, 1);
        assert.eq(result.nModified, 1);
        assert.eq(testColl.find({x: shardKeyValueOnShard1}).itcount(), 1);

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

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

        // Retry the command. This should retry against shard 1, which should throw
        // IncompleteTransactionHistory.
        assert.commandFailedWithCode(testDB.runCommand(cmdObj),
                                     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 shardKeyValueOnShard0 = -100;
        const shardKeyValueOnShard1 = 0;

        // Insert a single document on shard 0.
        testColl.insert({x: shardKeyValueOnShard0});

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

        // Update the document shard key. The document should now be on shard 1.
        const result = assert.commandWorked(testDB.runCommand(cmdObj));
        assert.eq(result.n, 1);
        assert.eq(result.nModified, 1);
        assert.eq(testColl.find({x: shardKeyValueOnShard1}).itcount(), 1);

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

        // Retry the command. This should retry against shard 2, which should throw
        // IncompleteTransactionHistory.
        assert.commandFailedWithCode(testDB.runCommand(cmdObj),
                                     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 anotherShardKeyValueOnShard0 = -101;
        const shardKeyValueOnShard1 = 0;
        const lsid = {id: UUID()};
        const txnNumber = 35;

        // Insert a single document on shard 0.
        assert.commandWorked(testColl.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.s.host, {x: shardKeyValueOnShard0}, null, ns, st.shard1.shardName);

        waitForMoveChunkStep(st.shard0, moveChunkStepNames.reachedSteadyState);

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

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

        // Check that the update from the transaction succeeded.
        const resultingDoc = testColl.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 shard 1, which should throw
        // IncompleteTransactionHistory.
        assert.commandFailedWithCode(testDB.runCommand(fakeRetryCmd),
                                     ErrorCodes.IncompleteTransactionHistory);
    });

test(
    "Update to 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 shardKeyValueOnShard0 = -100;
        const shardKeyValueOnShard1 = 0;
        const docId = 0;

        // Insert a single document on shard 0.
        assert.commandWorked(testColl.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);

        // We're going to do a shard key update to move a document from shard 0 to shard 1, so
        // here we move the chunk from shard 0 to shard 2, which won't be involved in the
        // transaction created by the shard key update.
        let joinMoveChunk = moveChunkParallel(
            staticMongod, st.s.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 shard 0 to shard 1.
        const result = assert.commandWorked(testDB.runCommand(cmdObj));
        assert.eq(result.n, 1);
        assert.eq(result.nModified, 1);
        assert.eq(testColl.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 2, which should throw
        // IncompleteTransactionHistory.
        assert.commandFailedWithCode(testDB.runCommand(cmdObj),
                                     ErrorCodes.IncompleteTransactionHistory);
    });

// 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(testColl.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.s.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(testDB.runCommand(cmdObj));
//        assert.eq(result.n, 1);
//        assert.eq(result.nModified, 1);
//        assert.eq(testColl.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(testDB.runCommand(cmdObj),
//                                     ErrorCodes.IncompleteTransactionHistory);
//    });

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