summaryrefslogtreecommitdiff
path: root/jstests/sharding/internal_transactions_for_retryable_writes_retry_without_internal_transactions.js
blob: 68f66f20da5d71f43dc71c6a9292842225ad1ac2 (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
/*
 * Test that retryable writes executed using or without using internal transactions execute exactly
 * once regardless of how they are retried.
 *
 * @tags: [requires_fcv_52, featureFlagInternalTransactions]
 */
(function() {
"use strict";

load("jstests/sharding/libs/sharded_transactions_helpers.js");

const st = new ShardingTest({shards: 1, rs: {nodes: 2}});

const kDbName = "testDb";
const kCollName = "testColl";
const testDB = st.s.getDB(kDbName);
const testColl = testDB.getCollection(kCollName);

const kTestMode = {
    kNonRecovery: 1,
    kRestart: 2,
    kFailover: 3
};

function setUpTestMode(mode) {
    if (mode == kTestMode.kRestart) {
        st.rs0.stopSet(null /* signal */, true /*forRestart */);
        st.rs0.startSet({restart: true});
        const newPrimary = st.rs0.getPrimary();
    } else if (mode == kTestMode.kFailover) {
        const oldPrimary = st.rs0.getPrimary();
        assert.commandWorked(
            oldPrimary.adminCommand({replSetStepDown: ReplSetTest.kForeverSecs, force: true}));
        assert.commandWorked(oldPrimary.adminCommand({replSetFreeze: 0}));
        const newPrimary = st.rs0.getPrimary();
    }
}

const sessionUUID = UUID();
const parentLsid = {
    id: sessionUUID
};
let currentParentTxnNumber = NumberLong(35);

{
    jsTest.log(
        "Test that retryable writes executed using internal transactions do not re-execute " +
        "when they are retried after they no longer need to be executed using internal transactions");

    let runTest = (testMode) => {
        const parentTxnNumber = NumberLong(currentParentTxnNumber++);
        const stmtId1 = NumberInt(1);
        const stmtId2 = NumberInt(2);

        assert.commandWorked(testColl.insert([{x: 1, y: 0}, {x: 2, y: 0}]));

        const childLsid1 = {id: parentLsid.id, txnNumber: parentTxnNumber, txnUUID: UUID()};
        const childTxnNumber1 = NumberLong(0);
        jsTest.log(`Running an update inside a retryable internal transaction with lsid ${
            tojson(childLsid1)}`);
        assert.commandWorked(testDB.runCommand({
            update: kCollName,
            updates: [{q: {x: 1}, u: {$inc: {y: 1}}}],
            lsid: childLsid1,
            txnNumber: childTxnNumber1,
            startTransaction: true,
            autocommit: false,
            stmtId: stmtId1,
        }));
        assert.commandWorked(
            testDB.adminCommand(makeCommitTransactionCmdObj(childLsid1, childTxnNumber1)));
        assert.eq(testColl.find({x: 1, y: 1}).itcount(), 1);

        const childLsid2 = {id: parentLsid.id, txnNumber: parentTxnNumber, txnUUID: UUID()};
        const childTxnNumber2 = NumberLong(0);
        jsTest.log(`Running an update inside a retryable internal transaction with lsid ${
            tojson(childLsid2)}`);
        assert.commandWorked(testDB.runCommand({
            update: kCollName,
            updates: [{q: {x: 2}, u: {$inc: {y: 1}}}],
            lsid: childLsid2,
            txnNumber: childTxnNumber2,
            startTransaction: true,
            autocommit: false,
            stmtId: stmtId2,
        }));
        assert.commandWorked(
            testDB.adminCommand(makeCommitTransactionCmdObj(childLsid2, childTxnNumber2)));
        assert.eq(testColl.find({x: 2, y: 1}).itcount(), 1);

        setUpTestMode(testMode);

        jsTest.log(`Retrying both updates as retryable writes with lsid ${tojson(parentLsid)}`);
        const retryRes = assert.commandWorked(testDB.runCommand({
            update: kCollName,
            updates: [{q: {x: 1}, u: {$inc: {y: 1}}}, {q: {x: 2}, u: {$inc: {y: 1}}}],
            lsid: parentLsid,
            txnNumber: parentTxnNumber,
            stmtIds: [stmtId1, stmtId2]
        }));
        assert.eq(retryRes.n, 2);
        assert.eq(testColl.find({x: 1, y: 1}).itcount(), 1);
        assert.eq(testColl.find({x: 2, y: 1}).itcount(), 1);

        assert.commandWorked(testColl.remove({}));
    };

    for (let modeName in kTestMode) {
        jsTest.log("Testing retry with testMode " + modeName);
        runTest(kTestMode[modeName]);
    }
}

{
    jsTest.log(
        "Test that retryable writes executed using internal transactions do not re-execute " +
        "when they are retried in internal transactions with lsids with different 'txnUUID'");

    let runTest = (testMode) => {
        const parentTxnNumber = NumberLong(currentParentTxnNumber++);
        const stmtId = NumberInt(1);

        assert.commandWorked(testColl.insert([{x: 1, y: 0}]));

        const childLsid1 = {id: parentLsid.id, txnNumber: parentTxnNumber, txnUUID: UUID()};
        const childTxnNumber1 = NumberLong(0);
        jsTest.log(`Running an update in a retryable internal transaction with lsid ${
            tojson(childLsid1)}`);
        assert.commandWorked(testDB.runCommand({
            update: kCollName,
            updates: [{q: {x: 1}, u: {$inc: {y: 1}}}],
            lsid: childLsid1,
            txnNumber: childTxnNumber1,
            startTransaction: true,
            autocommit: false,
            stmtId: stmtId,
        }));
        assert.commandWorked(
            testDB.adminCommand(makeCommitTransactionCmdObj(childLsid1, childTxnNumber1)));
        assert.eq(testColl.find({x: 1, y: 1}).itcount(), 1);

        setUpTestMode(testMode);

        const childLsid2 = {id: parentLsid.id, txnNumber: parentTxnNumber, txnUUID: UUID()};
        const childTxnNumber2 = NumberLong(0);
        jsTest.log(`Retrying the update in a retryable internal transaction with lsid ${
            tojson(childLsid2)}`);
        const retryRes = assert.commandWorked(testDB.runCommand({
            update: kCollName,
            updates: [{q: {x: 1}, u: {$inc: {y: 1}}}],
            lsid: childLsid2,
            txnNumber: childTxnNumber2,
            startTransaction: true,
            autocommit: false,
            stmtId: stmtId,
        }));
        assert.commandWorked(
            testDB.adminCommand(makeCommitTransactionCmdObj(childLsid2, childTxnNumber2)));
        assert.eq(retryRes.n, 1);
        assert.eq(testColl.find({x: 1, y: 1}).itcount(), 1);

        assert.commandWorked(testColl.remove({}));
    };

    for (let modeName in kTestMode) {
        jsTest.log("Testing retry with testMode " + modeName);
        runTest(kTestMode[modeName]);
    }
}

{
    jsTest.log("Test that retryable writes executed without using internal transactions do not " +
               "re-execute when they are retried while they need to be executed using internal " +
               "transactions");

    let runTest = (testMode) => {
        const parentTxnNumber = NumberLong(currentParentTxnNumber++);
        const stmtId1 = NumberInt(1);
        const stmtId2 = NumberInt(2);

        assert.commandWorked(testColl.insert([{x: 1, y: 0}, {x: 2, y: 0}]));

        jsTest.log(`Running updates as retryable writes with lsid ${tojson(parentLsid)}`);
        assert.commandWorked(testDB.runCommand({
            update: kCollName,
            updates: [{q: {x: 1}, u: {$inc: {y: 1}}}, {q: {x: 2}, u: {$inc: {y: 1}}}],
            lsid: parentLsid,
            txnNumber: parentTxnNumber,
            stmtIds: [stmtId1, stmtId2]
        }));
        assert.eq(testColl.find({x: 1, y: 1}).itcount(), 1);
        assert.eq(testColl.find({x: 2, y: 1}).itcount(), 1);

        setUpTestMode(testMode);

        const childLsid1 = {id: parentLsid.id, txnNumber: parentTxnNumber, txnUUID: UUID()};
        const childTxnNumber1 = NumberLong(0);
        jsTest.log(`Retrying one of the updates in a retryable internal transaction with lsid ${
            tojson(childLsid1)}`);
        const retryRes1 = assert.commandWorked(testDB.runCommand({
            update: kCollName,
            updates: [{q: {x: 1}, u: {$inc: {y: 1}}}],
            lsid: childLsid1,
            txnNumber: childTxnNumber1,
            startTransaction: true,
            autocommit: false,
            stmtId: stmtId1,
        }));
        assert.commandWorked(
            testDB.adminCommand(makeCommitTransactionCmdObj(childLsid1, childTxnNumber1)));
        assert.eq(retryRes1.n, 1);
        assert.eq(testColl.find({x: 1, y: 1}).itcount(), 1);

        const childLsid2 = {id: parentLsid.id, txnNumber: parentTxnNumber, txnUUID: UUID()};
        const childTxnNumber2 = NumberLong(0);
        jsTest.log(`Retrying one of the updates in a retryable internal transaction with lsid ${
            tojson(childLsid2)}`);
        const retryRes2 = assert.commandWorked(testDB.runCommand({
            update: kCollName,
            updates: [{q: {x: 2}, u: {$inc: {y: 1}}}],
            lsid: childLsid2,
            txnNumber: childTxnNumber2,
            startTransaction: true,
            autocommit: false,
            stmtId: stmtId2,
        }));
        assert.commandWorked(
            testDB.adminCommand(makeCommitTransactionCmdObj(childLsid2, childTxnNumber2)));
        assert.eq(retryRes2.n, 1);
        assert.eq(testColl.find({x: 2, y: 1}).itcount(), 1);

        assert.commandWorked(testColl.remove({}));
    };

    for (let modeName in kTestMode) {
        jsTest.log("Testing retry with testMode " + modeName);
        runTest(kTestMode[modeName]);
    }
}

st.stop();
})();