summaryrefslogtreecommitdiff
path: root/jstests/replsets/transactions_reaped_with_tickets_exhausted.js
blob: 24664bcc03c7e99dc62b4f1cf1130bc4dd11b5a0 (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
/**
 * Test ensures that exhausting the number of write tickets in the system does not prevent
 * transactions from being reaped/aborted.
 *
 * @tags: [uses_transactions]
 */
(function() {
"use strict";

load("jstests/libs/parallelTester.js");  // for Thread

// We set the number of write tickets to be a small value in order to avoid needing to spawn a
// large number of threads to exhaust all of the available ones.
const kNumWriteTickets = 5;

const rst = new ReplSetTest({
    nodes: 1,
    nodeOptions: {
        setParameter: {
            wiredTigerConcurrentWriteTransactions: kNumWriteTickets,

            // Setting a transaction lifetime of 1 hour to make sure the transaction reaper
            // doesn't abort the transaction.
            transactionLifetimeLimitSeconds: 3600,
        }
    }
});
rst.startSet();
rst.initiate();

const primary = rst.getPrimary();
const db = primary.getDB("test");

const session = primary.startSession({causalConsistency: false});
const sessionDb = session.getDatabase("test");

assert.commandWorked(db.runCommand({create: "mycoll"}));

session.startTransaction();
assert.commandWorked(sessionDb.mycoll.insert({}));

const threads = [];

for (let i = 0; i < kNumWriteTickets; ++i) {
    const thread = new Thread(function(host) {
        try {
            const conn = new Mongo(host);
            const db = conn.getDB("test");

            // Dropping a collection requires a database X lock and therefore blocks behind the
            // transaction committing or aborting.
            db.mycoll.drop();

            return {ok: 1};
        } catch (e) {
            return {ok: 0, error: e.toString(), stack: e.stack};
        }
    }, primary.host);

    threads.push(thread);
    thread.start();
}

// We wait until all of the drop commands are waiting for a lock to know that we've exhausted
// all of the available write tickets.
assert.soon(
    () => {
        const ops = db.currentOp({"command.drop": "mycoll", waitingForLock: true});
        return ops.inprog.length === kNumWriteTickets;
    },
    () => {
        return `Didn't find ${kNumWriteTickets} drop commands running: ` + tojson(db.currentOp());
    });

// Attempting to perform another operation inside of the transaction will block and should
// cause it to be aborted implicity.
assert.commandFailedWithCode(sessionDb.mycoll.insert({}), ErrorCodes.LockTimeout);

for (let thread of threads) {
    thread.join();
}

for (let thread of threads) {
    assert.commandWorked(thread.returnData());
}

// Transaction should already be aborted.
let res = assert.commandFailedWithCode(session.abortTransaction_forTesting(),
                                       ErrorCodes.NoSuchTransaction);
assert(res.errmsg.match(/Transaction .* has been aborted/), res.errmsg);

session.endSession();
rst.stopSet();
})();