summaryrefslogtreecommitdiff
path: root/jstests/sharding/move_chunk_find_and_modify_with_write_retryability.js
blob: c7602b4f644ed05fed7e8038e9e7ea611cb85b8b (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
load("jstests/sharding/move_chunk_with_session_helper.js");

(function() {
"use strict";

load("jstests/libs/retryable_writes_util.js");

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

var checkFindAndModifyResult = function(expected, toCheck) {
    assert.eq(expected.ok, toCheck.ok);
    assert.eq(expected.value, toCheck.value);
    assert.eq(expected.lastErrorObject, toCheck.lastErrorObject);
};

var lsid = UUID();
var tests = [
    {
        coll: 'findAndMod-upsert',
        cmd: {
            findAndModify: 'findAndMod-upsert',
            query: {x: 60},
            update: {$inc: {y: 1}},
            new: true,
            upsert: true,
            lsid: {id: lsid},
            txnNumber: NumberLong(37),
        },
        setup: function(coll) {},
        checkRetryResult: function(result, retryResult) {
            checkFindAndModifyResult(result, retryResult);
        },
        checkDocuments: function(coll) {
            assert.eq(1, coll.findOne({x: 60}).y);
        },
    },
    {
        coll: 'findAndMod-update-preImage',
        cmd: {
            findAndModify: 'findAndMod-update-preImage',
            query: {x: 60},
            update: {$inc: {y: 1}},
            new: false,
            upsert: false,
            lsid: {id: lsid},
            txnNumber: NumberLong(38),
        },
        setup: function(coll) {
            coll.insert({x: 60});
        },
        checkRetryResult: function(result, retryResult) {
            checkFindAndModifyResult(result, retryResult);
        },
        checkDocuments: function(coll) {
            assert.eq(1, coll.findOne({x: 60}).y);
        },
    },
    {
        coll: 'findAndMod-update-postImage',
        cmd: {
            findAndModify: 'findAndMod-update-postImage',
            query: {x: 60},
            update: {$inc: {y: 1}},
            new: true,
            upsert: false,
            lsid: {id: lsid},
            txnNumber: NumberLong(39),
        },
        setup: function(coll) {
            coll.insert({x: 60});
        },
        checkRetryResult: function(result, retryResult) {
            checkFindAndModifyResult(result, retryResult);
        },
        checkDocuments: function(coll) {
            assert.eq(1, coll.findOne({x: 60}).y);
        },
    },
    {
        coll: 'findAndMod-delete',
        cmd: {
            findAndModify: 'findAndMod-delete',
            query: {x: 10},
            remove: true,
            lsid: {id: lsid},
            txnNumber: NumberLong(40),
        },
        setup: function(coll) {
            var bulk = coll.initializeUnorderedBulkOp();
            for (let i = 0; i < 10; i++) {
                bulk.insert({x: 10});
            }
            assert.writeOK(bulk.execute());
        },
        checkRetryResult: function(result, retryResult) {
            checkFindAndModifyResult(result, retryResult);
        },
        checkDocuments: function(coll) {
            assert.eq(9, coll.find({x: 10}).itcount());
        },
    },
];

// Prevent unnecessary elections in the first shard replica set. Shard 'rs1' shard will need its
// secondary to get elected, so we don't give it a zero priority.
var st = new ShardingTest({
    mongos: 2,
    shards: {
        rs0: {nodes: [{rsConfig: {}}, {rsConfig: {priority: 0}}]},
        rs1: {nodes: [{rsConfig: {}}, {rsConfig: {}}]}
    }
});
assert.commandWorked(st.s.adminCommand({enableSharding: 'test'}));
st.ensurePrimaryShard('test', st.shard0.shardName);

tests.forEach(function(test) {
    testMoveChunkWithSession(
        st, test.coll, test.cmd, test.setup, test.checkRetryResult, test.checkDocuments);
});

st.stop();
})();