summaryrefslogtreecommitdiff
path: root/jstests/replsets/retryable_commit_transaction_after_restart.js
blob: 802259661fb600ee8945c7c7ce9de9a3bc5ac363 (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
// Test committed transaction state is restored after restart.
// @tags: [uses_transactions, requires_persistence]
(function() {
    "use strict";

    load("jstests/replsets/rslib.js");

    const dbName = "test";
    const collName = "retryable_commit_transaction_after_restart";

    const rst = new ReplSetTest({nodes: 1});
    rst.startSet();
    rst.initiate();
    const testDB = rst.getPrimary().getDB(dbName);
    const testColl = testDB[collName];

    testDB.runCommand({drop: collName, writeConcern: {w: "majority"}});

    assert.commandWorked(
        testDB.createCollection(testColl.getName(), {writeConcern: {w: "majority"}}));
    let txnNumber = 0;
    let stmtId = 0;

    const sessionOptions = {causalConsistency: false};
    const session = testDB.getMongo().startSession(sessionOptions);
    const sessionDb = session.getDatabase(dbName);

    jsTest.log("commitTransaction command is retryable before restart");
    txnNumber++;
    stmtId = 0;
    assert.commandWorked(sessionDb.runCommand({
        insert: collName,
        documents: [{_id: "commit-txn-1"}],
        readConcern: {level: "snapshot"},
        txnNumber: NumberLong(txnNumber),
        stmtId: NumberInt(stmtId++),
        startTransaction: true,
        autocommit: false
    }));
    assert.commandWorked(sessionDb.adminCommand({
        commitTransaction: 1,
        txnNumber: NumberLong(txnNumber),
        stmtId: NumberInt(stmtId++),
        autocommit: false,
        writeConcern: {w: "majority"}
    }));

    // Retry commitTransaction.
    assert.commandWorked(sessionDb.adminCommand({
        commitTransaction: 1,
        txnNumber: NumberLong(txnNumber),
        stmtId: NumberInt(stmtId),
        autocommit: false,
        writeConcern: {w: "majority"}
    }));

    jsTest.log("restart the single node replset");
    rst.restart(0);
    // Wait until the node becomes a primary and reconnect.
    rst.getPrimary();
    reconnect(sessionDb);

    jsTest.log("commitTransaction command is retryable after restart");
    // Retry commitTransaction.
    assert.commandWorked(sessionDb.adminCommand({
        commitTransaction: 1,
        txnNumber: NumberLong(txnNumber),
        stmtId: NumberInt(stmtId),
        autocommit: false,
        writeConcern: {w: "majority"}
    }));

    jsTest.log("Attempt to abort a committed transaction after restart");
    // Cannot abort the committed transaction.
    assert.commandFailedWithCode(sessionDb.adminCommand({
        abortTransaction: 1,
        txnNumber: NumberLong(txnNumber),
        stmtId: NumberInt(stmtId++),
        autocommit: false,
        writeConcern: {w: "majority"}
    }),
                                 ErrorCodes.TransactionCommitted);

    jsTest.log("Attempt to continue a committed transaction after restart");
    assert.commandFailedWithCode(sessionDb.runCommand({
        insert: collName,
        documents: [{_id: "commit-txn-2"}],
        txnNumber: NumberLong(txnNumber),
        stmtId: NumberInt(stmtId++),
        autocommit: false
    }),
                                 ErrorCodes.TransactionCommitted);

    session.endSession();
    rst.stopSet();
}());