summaryrefslogtreecommitdiff
path: root/jstests/replsets/commit_prepared_transaction_before_stable_timestamp.js
blob: 9490544b7efa8e7d0c5f6e62cdee584dd7dc166d (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
/**
 * Test that we can successfully commit a prepared transaction before the stable timestamp.
 *
 * @tags: [uses_transactions, uses_prepare_transaction]
 */

(function() {
"use strict";
load("jstests/aggregation/extras/utils.js");
load("jstests/core/txns/libs/prepare_helpers.js");

const replTest = new ReplSetTest({nodes: 1});
replTest.startSet();
replTest.initiate();

const primary = replTest.getPrimary();

const dbName = "test";
const collName = "commit_prepared_transaction_before_stable_timestamp";
const testDB = primary.getDB(dbName);
const testColl = testDB.getCollection(collName);

assert.commandWorked(testDB.runCommand({create: collName}));

// Make sure there is no lag between the oldest timestamp and the stable timestamp so we can
// test that committing a prepared transaction behind the oldest timestamp succeeds.
assert.commandWorked(primary.adminCommand({
    "configureFailPoint": 'WTSetOldestTSToStableTS',
    "mode": 'alwaysOn',
}));

const session = primary.startSession({causalConsistency: false});
const sessionDB = session.getDatabase(dbName);
const sessionColl = sessionDB.getCollection(collName);

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

jsTestLog("Do a majority write to advance the stable timestamp past the prepareTimestamp");
// Doing a majority write after preparing the transaction ensures that the stable timestamp is
// past the prepare timestamp because this write must be in the committed snapshot.
assert.commandWorked(
    testColl.runCommand("insert", {documents: [{_id: 2}]}, {writeConcern: {w: "majority"}}));

jsTestLog("Committing the transaction before the stable timestamp");

// Since we have advanced the stableTimestamp to be after the prepareTimestamp, when we commit
// at the prepareTimestamp, we are certain that we are committing behind the stableTimestamp.
assert.commandWorked(PrepareHelpers.commitTransaction(session, prepareTimestamp));

// Make sure we can see the insert from the prepared transaction.
assert.sameMembers(sessionColl.find().toArray(), [{_id: 1}, {_id: 2}]);

assert.commandWorked(
    primary.adminCommand({configureFailPoint: 'WTSetOldestTSToStableTS', mode: 'off'}));

replTest.stopSet();
}());