summaryrefslogtreecommitdiff
path: root/jstests/replsets/invalidate_sessions_on_stepdown.js
blob: 50916865780acd257d3e475192733a10b7f5bf22 (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
/**
 * Tests that a txnNumber for a transaction that was aborted in-memory can be reused by a new
 * primary. Mongos does this as an optimization.
 * @tags: [uses_transactions, uses_prepare_transaction]
 */
(function() {
"use strict";

const rst = new ReplSetTest({name: "invalidate_sessions_on_stepdown", nodes: 2});
rst.startSet();
rst.initiateWithHighElectionTimeout();

const dbName = "test";
const collName = "coll";
const node0 = rst.getPrimary();
const node1 = rst.getSecondary();
const node0Session = node0.startSession();
const sessionId = node0Session.getSessionId();
const node0SessionDB = node0Session.getDatabase(dbName);

assert.commandWorked(node0SessionDB.coll.insert({a: 1}, {writeConcern: {"w": "majority"}}));

jsTestLog("Run a transaction with txnNumber 0 on the primary.");
assert.commandWorked(node0SessionDB.runCommand({
    insert: collName,
    documents: [{b: 1}],
    txnNumber: NumberLong(0),
    startTransaction: true,
    autocommit: false
}));

jsTestLog("Step up the secondary. The primary will abort the transaction when it steps down.");
assert.commandWorked(node1.adminCommand({replSetStepUp: 1}));
assert.eq(node1, rst.getPrimary());

const node1DB = node1.getDB(dbName);

jsTestLog("Run a transaction with txnNumber 0 and the same session ID on the new primary.");
assert.commandWorked(node1DB.runCommand({
    insert: collName,
    documents: [{c: 1}],
    lsid: sessionId,
    txnNumber: NumberLong(0),
    startTransaction: true,
    autocommit: false
}));
let res = assert.commandWorked(node1DB.adminCommand({
    prepareTransaction: 1,
    lsid: sessionId,
    txnNumber: NumberLong(0),
    autocommit: false,
    writeConcern: {w: "majority"}
}));
assert.commandWorked(node1DB.adminCommand({
    commitTransaction: 1,
    commitTimestamp: res.prepareTimestamp,
    lsid: sessionId,
    txnNumber: NumberLong(0),
    autocommit: false,
    writeConcern: {w: "majority"}
}));
assert.eq(2, node0SessionDB.coll.find().itcount());
assert.eq(0, node0SessionDB.coll.find({b: 1}).itcount());
assert.eq(1, node0SessionDB.coll.find({c: 1}).itcount());

rst.stopSet();
})();