summaryrefslogtreecommitdiff
path: root/jstests/sharding/write_transactions_during_migration.js
blob: 7c52628e05ecbce7745dabfe9d666af57fdbe3d2 (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
/**
 * Tests that session information are properly transferred to the destination shard while
 * new writes are being sent to the source shard.
 */

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

/**
 * Test outline:
 * 1. Pause migration.
 * 2. Perform writes and allow it to be capture via OpObserver
 * 3. Unpause migration.
 * 4. Retry writes and confirm that writes are not duplicated.
 */
(function() {

    "use strict";

    var staticMongod = MongoRunner.runMongod({});  // For startParallelOps.

    var st = new ShardingTest({shards: {rs0: {nodes: 1}, rs1: {nodes: 1}}});
    st.adminCommand({enableSharding: 'test'});
    st.ensurePrimaryShard('test', st.shard0.shardName);
    st.adminCommand({shardCollection: 'test.user', key: {x: 1}});

    pauseMoveChunkAtStep(st.shard0, moveChunkStepNames.reachedSteadyState);
    var joinMoveChunk =
        moveChunkParallel(staticMongod, st.s.host, {x: 0}, null, 'test.user', st.shard1.shardName);

    var insertCmd = {
        insert: 'user',
        documents: [{x: 10}, {x: 30}],
        ordered: false,
        lsid: {id: UUID()},
        txnNumber: NumberLong(34),
    };

    var testDB = st.getDB('test');
    var insertResult = assert.commandWorked(testDB.runCommand(insertCmd));

    var findAndModCmd = {
        findAndModify: 'user',
        query: {x: 30},
        update: {$inc: {y: 1}},
        new: true,
        upsert: true,
        lsid: {id: UUID()},
        txnNumber: NumberLong(37),
    };

    var findAndModifyResult = assert.commandWorked(testDB.runCommand(findAndModCmd));

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

    var insertRetryResult = assert.commandWorked(testDB.runCommand(insertCmd));

    assert.eq(insertResult.ok, insertRetryResult.ok);
    assert.eq(insertResult.n, insertRetryResult.n);
    assert.eq(insertResult.writeErrors, insertRetryResult.writeErrors);
    assert.eq(insertResult.writeConcernErrors, insertRetryResult.writeConcernErrors);

    assert.eq(1, testDB.user.find({x: 10}).itcount());
    assert.eq(1, testDB.user.find({x: 30}).itcount());

    var findAndModifyRetryResult = assert.commandWorked(testDB.runCommand(findAndModCmd));

    assert.eq(findAndModifyResult.ok, findAndModifyRetryResult.ok);
    assert.eq(findAndModifyResult.value, findAndModifyRetryResult.value);

    // TODO: SERVER-30532: after adding upserted, just compare the entire lastErrorObject
    var expectedLE = findAndModifyResult.lastErrorObject;
    var toCheckLE = findAndModifyRetryResult.lastErrorObject;

    assert.neq(null, toCheckLE);
    assert.eq(findAndModifyResult.updatedExisting, findAndModifyRetryResult.updatedExisting);
    assert.eq(findAndModifyResult.n, findAndModifyRetryResult.n);

    assert.eq(1, testDB.user.findOne({x: 30}).y);

    st.stop();

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