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

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

    // 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 5 seconds works fine locally because the
                // threads which attempt to run the drop command are spawned quickly enough. This
                // might not be the case for Evergreen hosts and may need to be tuned accordingly.
                transactionLifetimeLimitSeconds: 5,
            }
        }
    });
    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 ScopedThread(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
    // eventually cause it to be aborted.
    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.
    assert.commandFailedWithCode(session.abortTransaction_forTesting(),
                                 ErrorCodes.NoSuchTransaction);

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