summaryrefslogtreecommitdiff
path: root/jstests/replsets/retryable_prepared_commit_transaction_after_failover.js
blob: ba3a15b83aa49a94b12e803837cb3f7885903219 (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
/**
 * Tests that a failing commitTransaction of a prepared transaction doesn't block stepdown. This is
 * a regression test for SERVER-41838.
 *
 * @tags: [uses_transactions, uses_prepare_transaction]
 */
(function() {
"use strict";

load("jstests/core/txns/libs/prepare_helpers.js");

const dbName = "test";
const collName = "foo";

const rst = new ReplSetTest({nodes: 2});
rst.startSet();

const config = rst.getReplSetConfig();
// Increase the election timeout so that we do not accidentally trigger an election while
// stepping up the old secondary.
config.settings = {
    "electionTimeoutMillis": 12 * 60 * 60 * 1000
};
rst.initiate(config);

const priConn = rst.getPrimary();
const secConn = rst.getSecondary();
assert.commandWorked(priConn.getDB(dbName).runCommand({create: collName}));

const priSession = priConn.startSession();
const priSessionDB = priSession.getDatabase(dbName);
const priSessionColl = priSessionDB.getCollection(collName);

jsTestLog("Prepare a transaction");
priSession.startTransaction();
assert.commandWorked(priSessionColl.insert({_id: 1}));
const prepareTimestamp1 = PrepareHelpers.prepareTransaction(priSession);

jsTestLog("Error committing the transaction");
// This will error in the "commit unprepared transaction" code path.
assert.commandFailedWithCode(priSessionDB.adminCommand({commitTransaction: 1}),
                             ErrorCodes.InvalidOptions);

// This will error in the "commit prepared transaction" code path.
const tooEarlyTS1 = Timestamp(prepareTimestamp1.getTime() - 1, 1);
assert.commandFailedWithCode(
    priSessionDB.adminCommand({commitTransaction: 1, commitTimestamp: tooEarlyTS1}),
    ErrorCodes.InvalidOptions);

jsTestLog("Step up the secondary");
rst.stepUp(secConn);
assert.eq(secConn, rst.getPrimary());
rst.waitForState(priConn, ReplSetTest.State.SECONDARY);

jsTestLog("commitTransaction command is retryable after failover");

const secSession = new _DelegatingDriverSession(secConn, priSession);
const secSessionDB = secSession.getDatabase(dbName);
const secSessionColl = secSessionDB.getCollection(collName);
assert.commandWorked(PrepareHelpers.commitTransaction(secSession, prepareTimestamp1));

assert.eq(secConn.getDB(dbName)[collName].count(), 1);
assert.eq(secConn.getDB(dbName)[collName].find().itcount(), 1);

rst.awaitReplication();

assert.eq(priConn.getDB(dbName)[collName].count(), 1);
assert.eq(priConn.getDB(dbName)[collName].find().itcount(), 1);

jsTestLog("Prepare a second transaction");
secSession.startTransaction();
assert.commandWorked(secSessionColl.insert({_id: 2}));
const prepareTimestamp2 = PrepareHelpers.prepareTransaction(secSession);

jsTestLog("Error committing the transaction");
assert.commandFailedWithCode(secSessionDB.adminCommand({commitTransaction: 1}),
                             ErrorCodes.InvalidOptions);
const tooEarlyTS2 = Timestamp(prepareTimestamp2.getTime() - 1, 1);
assert.commandFailedWithCode(
    secSessionDB.adminCommand({commitTransaction: 1, commitTimestamp: tooEarlyTS2}),
    ErrorCodes.InvalidOptions);

jsTestLog("Step up the original primary");
rst.stepUp(priConn);
assert.eq(priConn, rst.getPrimary());
rst.waitForState(secConn, ReplSetTest.State.SECONDARY);

jsTestLog("Step up the original secondary immediately");
rst.stepUp(secConn);
assert.eq(secConn, rst.getPrimary());
rst.waitForState(priConn, ReplSetTest.State.SECONDARY);

assert.commandWorked(PrepareHelpers.commitTransaction(secSession, prepareTimestamp2));

assert.eq(secConn.getDB(dbName)[collName].count(), 2);
assert.eq(secConn.getDB(dbName)[collName].find().itcount(), 2);

rst.awaitReplication();

assert.eq(priConn.getDB(dbName)[collName].count(), 2);
assert.eq(priConn.getDB(dbName)[collName].find().itcount(), 2);

rst.stopSet();
}());