summaryrefslogtreecommitdiff
path: root/jstests/sharding/transactions_expiration.js
blob: e337a81632ceea6d72e005a4b86d826d91680c60 (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
// Tests that a transaction expires and is then aborted by the server. Uses the server parameter
// 'transactionLifetimeLimitSeconds' to lower the transaction lifetime for quicker transaction
// expiration. This is the mongos version for: jstests/core/txns/abort_expired_transaction.js.
//
// @tags: [uses_transactions, uses_multi_shard_transaction]

(function() {
"use strict";

let st = new ShardingTest({shards: 2});

assert.commandWorked(st.s.adminCommand({enableSharding: 'test'}));
st.ensurePrimaryShard('test', st.shard0.name);
assert.commandWorked(st.s.adminCommand({shardCollection: 'test.user', key: {x: 1}}));
assert.commandWorked(st.s.adminCommand({split: 'test.user', middle: {x: 0}}));
assert.commandWorked(st.s.adminCommand({moveChunk: 'test.user', find: {x: 0}, to: st.shard1.name}));

let lowerTxnTimeout = (conn) => {
    assert.commandWorked(
        conn.getDB('admin').runCommand({setParameter: 1, transactionLifetimeLimitSeconds: 1}));
};

st.rs0.nodes.forEach(lowerTxnTimeout);
st.rs1.nodes.forEach(lowerTxnTimeout);

let testDB = st.s.getDB('test');

// Create the collections in the shards outside the transactions.
assert.commandWorked(testDB.runCommand(
    {insert: 'user', documents: [{x: -1}, {x: 1}], writeConcern: {w: 'majority'}}));

const session = st.s.startSession();
const sessionDb = session.getDatabase('test');

let txnNumber = 0;

assert.commandWorked(sessionDb.runCommand({
    insert: 'user',
    documents: [{x: -10}, {x: 10}],
    txnNumber: NumberLong(txnNumber),
    startTransaction: true,
    autocommit: false,
}));

// We can deterministically wait for the transaction to be aborted by waiting for currentOp
// to cease reporting the inactive transaction: the transaction should disappear from the
// currentOp results once aborted.
assert.soon(
    function() {
        const sessionFilter = {
            active: false,
            opid: {$exists: false},
            desc: "inactive transaction",
            "transaction.parameters.txnNumber": NumberLong(txnNumber),
            "lsid.id": session.getSessionId().id
        };

        const priConn = st.rs0.getPrimary();
        const res = priConn.getDB("admin").aggregate(
            [{$currentOp: {allUsers: true, idleSessions: true}}, {$match: sessionFilter}]);

        return (res.itcount() == 0);
    },
    "currentOp reports that the idle transaction still exists, it has not been " +
        "aborted as expected.");

assert.commandFailedWithCode(sessionDb.runCommand({
    insert: 'user',
    documents: [{x: -100}, {x: 100}],
    txnNumber: NumberLong(txnNumber),
    autocommit: false,
}),
                             ErrorCodes.NoSuchTransaction);

session.endSession();

st.stop();
}());