summaryrefslogtreecommitdiff
path: root/jstests/sharding/mongos_not_retry_commands_in_transactions.js
blob: 60e74860f5e79ffd17968c88fb790bf4c82facf8 (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
/*
 * Tests that mongos doesn't retry commands with startTransaction=true.
 * @tags: [requires_fcv_47]
 */
(function() {
'use strict';

const setCommandToFail = (nodeConnection, command, namespace) => {
    return nodeConnection.adminCommand({
        configureFailPoint: 'failCommand',
        mode: {times: 1},
        data: {
            errorCode: ErrorCodes.InterruptedDueToReplStateChange,
            failCommands: [command],
            namespace,
            failInternalCommands: true
        }
    });
};

const kDbName = "testDb";
const kCollName = "testColl";
const kNs = `${kDbName}.${kCollName}`;

const kDoc0 = {
    _id: 0
};
const kDoc1 = {
    _id: 1
};

let transactionNumber = 1;

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

// Initializes test and inserts dummy document
jsTest.log("Inserting test document.");
const mongosDB = st.s0.startSession().getDatabase(kDbName);
const primaryConnection = st.rs0.getPrimary();

assert.commandWorked(mongosDB.runCommand({
    insert: kCollName,
    documents: [kDoc0],
}));

// Set the failCommand failpoint to make the next 'find' command fail once due to a failover.
// Start a transaction & execute a find command.
// It should fail once due to the 'failCommand' failpoint and should not be retried.
jsTest.log(
    "Testing that mongos doesn't retry the read command with startTransaction=true on replication set failover.");
assert.commandWorked(setCommandToFail(primaryConnection, "find", kNs));

assert.commandFailedWithCode(mongosDB.runCommand({
    find: kCollName,
    filter: kDoc0,
    startTransaction: true,
    txnNumber: NumberLong(transactionNumber++),
    stmtId: NumberInt(0),
    autocommit: false
}),
                             ErrorCodes.InterruptedDueToReplStateChange);

jsTest.log("Testing that mongos retries retryable writes on failover.");
assert.commandWorked(setCommandToFail(primaryConnection, "insert", kNs));

assert.commandWorked(mongosDB.runCommand(
    {insert: kCollName, documents: [kDoc1], txnNumber: NumberLong(transactionNumber++)}));

st.stop();
})();