summaryrefslogtreecommitdiff
path: root/jstests/replsets/transient_txn_error_labels.js
blob: ad1420a167ef9b4489f2a631fff1594d70b4d470 (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
// Test TransientTransactionErrors error label in transactions.
// @tags: [uses_transactions]
(function() {
    "use strict";

    load("jstests/libs/write_concern_util.js");
    load("jstests/libs/parallelTester.js");  // For ScopedThread.

    const dbName = "test";
    const collName = "no_error_labels_outside_txn";
    const rst = new ReplSetTest({name: collName, nodes: 2});
    const config = rst.getReplSetConfig();
    config.members[1].priority = 0;
    rst.startSet();
    rst.initiate(config);

    const primary = rst.getPrimary();
    const secondary = rst.getSecondary();
    const testDB = primary.getDB(dbName);
    const adminDB = testDB.getSiblingDB("admin");
    const testColl = testDB.getCollection(collName);

    const sessionOptions = {causalConsistency: false};
    let session = primary.startSession(sessionOptions);
    let sessionDb = session.getDatabase(dbName);
    let sessionColl = sessionDb.getCollection(collName);
    let secondarySession = secondary.startSession(sessionOptions);
    let secondarySessionDb = secondarySession.getDatabase(dbName);

    assert.commandWorked(testDB.createCollection(collName, {writeConcern: {w: "majority"}}));

    jsTest.log("Insert inside a transaction on secondary should fail but return error labels");
    let txnNumber = 0;
    let res = secondarySessionDb.runCommand({
        insert: collName,
        documents: [{_id: "insert-1"}],
        readConcern: {level: "snapshot"},
        txnNumber: NumberLong(txnNumber),
        startTransaction: true,
        autocommit: false
    });
    assert.commandFailedWithCode(res, ErrorCodes.NotMaster);
    assert.eq(res.errorLabels, ["TransientTransactionError"]);

    jsTest.log("Insert outside a transaction on secondary should fail but not return error labels");
    txnNumber++;
    // Insert as a retryable write.
    res = secondarySessionDb.runCommand(
        {insert: collName, documents: [{_id: "insert-1"}], txnNumber: NumberLong(txnNumber)});

    assert.commandFailedWithCode(res, ErrorCodes.NotMaster);
    assert(!res.hasOwnProperty("errorLabels"));
    secondarySession.endSession();

    jsTest.log("failCommand should be able to return errors with TransientTransactionError");
    assert.commandWorked(testDB.adminCommand({
        configureFailPoint: "failCommand",
        mode: "alwaysOn",
        data: {errorCode: ErrorCodes.WriteConflict, failCommands: ["insert"]}
    }));
    session.startTransaction();
    jsTest.log("WriteCommandError should have error labels inside transactions.");
    res = sessionColl.insert({_id: "write-fail-point"});
    assert.commandFailedWithCode(res, ErrorCodes.WriteConflict);
    assert(res instanceof WriteCommandError);
    assert.eq(res.errorLabels, ["TransientTransactionError"]);
    res = testColl.insert({_id: "write-fail-point-outside-txn"});
    jsTest.log("WriteCommandError should not have error labels outside transactions.");
    // WriteConflict will not be returned outside transactions in real cases, but it's fine for
    // testing purpose.
    assert.commandFailedWithCode(res, ErrorCodes.WriteConflict);
    assert(res instanceof WriteCommandError);
    assert(!res.hasOwnProperty("errorLabels"));
    assert.commandWorked(testDB.adminCommand({configureFailPoint: "failCommand", mode: "off"}));
    session.abortTransaction();

    jsTest.log("WriteConflict returned by commitTransaction command is TransientTransactionError");
    session.startTransaction();
    assert.commandWorked(sessionColl.insert({_id: "commitTransaction-fail-point"}));
    assert.commandWorked(testDB.adminCommand({
        configureFailPoint: "failCommand",
        mode: "alwaysOn",
        data: {errorCode: ErrorCodes.WriteConflict, failCommands: ["commitTransaction"]}
    }));
    res = session.commitTransaction_forTesting();
    assert.commandFailedWithCode(res, ErrorCodes.WriteConflict);
    assert.eq(res.errorLabels, ["TransientTransactionError"]);
    assert.commandWorked(testDB.adminCommand({configureFailPoint: "failCommand", mode: "off"}));

    jsTest.log("NotMaster returned by commitTransaction command is not TransientTransactionError");
    session.startTransaction();
    assert.commandWorked(sessionColl.insert({_id: "commitTransaction-fail-point"}));
    assert.commandWorked(testDB.adminCommand({
        configureFailPoint: "failCommand",
        mode: "alwaysOn",
        data: {errorCode: ErrorCodes.NotMaster, failCommands: ["commitTransaction"]}
    }));
    res = session.commitTransaction_forTesting();
    assert.commandFailedWithCode(res, ErrorCodes.NotMaster);
    assert(!res.hasOwnProperty("errorLabels"));
    assert.commandWorked(testDB.adminCommand({configureFailPoint: "failCommand", mode: "off"}));

    jsTest.log("ShutdownInProgress returned by write commands is TransientTransactionError");
    session.startTransaction();
    assert.commandWorked(testDB.adminCommand({
        configureFailPoint: "failCommand",
        mode: "alwaysOn",
        data: {errorCode: ErrorCodes.ShutdownInProgress, failCommands: ["insert"]}
    }));
    res = sessionColl.insert({_id: "commitTransaction-fail-point"});
    assert.commandFailedWithCode(res, ErrorCodes.ShutdownInProgress);
    assert(res instanceof WriteCommandError);
    assert.eq(res.errorLabels, ["TransientTransactionError"]);
    assert.commandWorked(testDB.adminCommand({configureFailPoint: "failCommand", mode: "off"}));
    session.abortTransaction();

    jsTest.log(
        "ShutdownInProgress returned by commitTransaction command is not TransientTransactionError");
    session.startTransaction();
    assert.commandWorked(sessionColl.insert({_id: "commitTransaction-fail-point"}));
    assert.commandWorked(testDB.adminCommand({
        configureFailPoint: "failCommand",
        mode: "alwaysOn",
        data: {errorCode: ErrorCodes.ShutdownInProgress, failCommands: ["commitTransaction"]}
    }));
    res = session.commitTransaction_forTesting();
    assert.commandFailedWithCode(res, ErrorCodes.ShutdownInProgress);
    assert(!res.hasOwnProperty("errorLabels"));
    assert.commandWorked(testDB.adminCommand({configureFailPoint: "failCommand", mode: "off"}));

    jsTest.log("LockTimeout should be TransientTransactionError");
    // Start a transaction to hold the DBLock in IX mode so that drop will be blocked.
    session.startTransaction();
    assert.commandWorked(sessionColl.insert({_id: "lock-timeout-1"}));
    function dropCmdFunc(testData, primaryHost, dbName, collName) {
        // Pass the TestData into the new shell so that jsTest.authenticate() can use the correct
        // credentials in auth test suites.
        TestData = testData;
        const primary = new Mongo(primaryHost);
        return primary.getDB(dbName).runCommand({drop: collName, writeConcern: {w: "majority"}});
    }
    const thread = new ScopedThread(dropCmdFunc, TestData, primary.host, dbName, collName);
    thread.start();
    // Wait for the drop to have a pending MODE_X lock on the database.
    assert.soon(
        function() {
            return adminDB
                       .aggregate([
                           {$currentOp: {}},
                           {$match: {"command.drop": collName, waitingForLock: true}}
                       ])
                       .itcount() === 1;
        },
        function() {
            return "Failed to find drop in currentOp output: " +
                tojson(adminDB.aggregate([{$currentOp: {}}]).toArray());
        });
    // Start another transaction in a new session, which cannot acquire the database lock in time.
    let sessionOther = primary.startSession(sessionOptions);
    sessionOther.startTransaction();
    res = sessionOther.getDatabase(dbName).getCollection(collName).insert({_id: "lock-timeout-2"});
    assert.commandFailedWithCode(res, ErrorCodes.LockTimeout);
    assert(res instanceof WriteCommandError);
    assert.eq(res.errorLabels, ["TransientTransactionError"]);
    sessionOther.abortTransaction();
    session.abortTransaction();
    thread.join();
    assert.commandWorked(thread.returnData());

    session.endSession();

    rst.stopSet();
}());