summaryrefslogtreecommitdiff
path: root/jstests/replsets/rollback_aborted_prepared_transaction.js
blob: b8bdc857992d964273fbf06936e751890de33fbb (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
/**
 * Test that rollback can successfully roll back an aborted prepared transaction.
 * Also test that it is impossible to commit a prepared transaction whose prepare oplog entry has
 * not yet been majority committed. This case should never happen when prepareTransaction is driven
 * by the transaction coordinator because transaction coordinator should always call
 * prepareTransaction with {w : "majority"}.
 *
 * @tags: [uses_transactions, uses_prepare_transaction]
 */
(function() {
"use strict";

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

const dbName = "test";
const collName = "rollback_aborted_prepared_transaction";

const rollbackTest = new RollbackTest(dbName);
let primary = rollbackTest.getPrimary();

// Create collection we're using beforehand.
let testDB = primary.getDB(dbName);
let testColl = testDB.getCollection(collName);

testDB.runCommand({drop: collName});
assert.commandWorked(testDB.runCommand({create: collName}));
assert.commandWorked(testColl.insert({_id: 0}));

// Start two sessions on the primary.
let session = primary.startSession();
const sessionID = session.getSessionId();
let sessionDB = session.getDatabase(dbName);
let sessionColl = sessionDB.getCollection(collName);

let session2 = primary.startSession();
let sessionColl2 = session2.getDatabase(dbName).getCollection(collName);

// The following transaction will be rolled back.
rollbackTest.transitionToRollbackOperations();

// Prepare the transaction on the session.
session.startTransaction();
assert.commandWorked(sessionColl.insert({_id: 1}));
PrepareHelpers.prepareTransaction(session, {w: 1});

assert.eq(testColl.find().itcount(), 1);
// This characterizes the current fastcount behavior, which is that active prepared transactions
// contribute to the fastcount.
assert.eq(testColl.count(), 2);

// Abort the transaction explicitly.
assert.commandWorked(session.abortTransaction_forTesting());

assert.eq(testColl.find().itcount(), 1);
assert.eq(testColl.count(), 1);

// Test that it is impossible to commit a prepared transaction whose prepare oplog entry has not
// yet majority committed. This also aborts the transaction.
session2.startTransaction();
assert.commandWorked(sessionColl2.insert({_id: 2}));
let prepareTimestamp = PrepareHelpers.prepareTransaction(session2, {w: 1});
let res = assert.commandFailedWithCode(PrepareHelpers.commitTransaction(session2, prepareTimestamp),
                                       ErrorCodes.InvalidOptions);
assert(
    res.errmsg.includes("cannot be run before its prepare oplog entry has been majority committed"),
    res);
assert.eq(testColl.find().itcount(), 1);
assert.eq(testColl.count(), 1);

// Check that we have two transactions in the transactions table.
assert.eq(primary.getDB('config')['transactions'].find().itcount(), 2);

rollbackTest.transitionToSyncSourceOperationsBeforeRollback();
rollbackTest.transitionToSyncSourceOperationsDuringRollback();
rollbackTest.transitionToSteadyStateOperations();

// Make sure there are no transactions in the transactions table. This is because both the abort
// and prepare operations are rolled back, and the entry in the transactions table is only made
// durable when a transaction is prepared.
assert.eq(primary.getDB('config')['transactions'].find().itcount(), 0);

// Make sure the first collection only has one document since the prepared insert was rolled
// back.
assert.eq(sessionColl.find().itcount(), 1);
assert.eq(sessionColl.count(), 1);

// Get the new primary after the topology changes.
primary = rollbackTest.getPrimary();
testDB = primary.getDB(dbName);
testColl = testDB.getCollection(collName);

// Make sure we can successfully run a prepared transaction on the same session after going
// through rollback.
session = PrepareHelpers.createSessionWithGivenId(primary, sessionID);
sessionDB = session.getDatabase(dbName);
sessionColl = sessionDB.getCollection(collName);

session.startTransaction();
assert.commandWorked(sessionColl.insert({_id: 1}));
prepareTimestamp = PrepareHelpers.prepareTransaction(session);
PrepareHelpers.commitTransaction(session, prepareTimestamp);

assert.eq(testColl.find().itcount(), 2);
assert.eq(testColl.count(), 2);

rollbackTest.stop();
}());