summaryrefslogtreecommitdiff
path: root/jstests/concurrency/fsm_workload_helpers/cleanup_txns.js
blob: c3e135c98d57df42c38250abbbf2e2c343346b8e (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
/**
 * Helpers for aborting transactions in concurrency workloads.
 */

/**
 * Abort the transaction on the session and return result.
 */
function abortTransaction(sessionAwareDB, txnNumber, errorCodes) {
    assert(sessionAwareDB.getSession() != null);

    // Don't use the given session as it might be in a state we don't want to be and
    // because we are trying to abort with arbitrary txnNumber.
    let rawDB = sessionAwareDB.getSession().getClient().getDB(sessionAwareDB.getName());

    const abortCmd = {
        abortTransaction: 1,
        lsid: sessionAwareDB.getSession().getSessionId(),
        txnNumber: NumberLong(txnNumber),
        autocommit: false
    };
    const res = rawDB.adminCommand(abortCmd);
    return assert.commandWorkedOrFailedWithCode(res, errorCodes, () => `cmd: ${tojson(abortCmd)}`);
}

/**
 * This function operates on the last iteration of each thread to abort any active transactions.
 */
var {cleanupOnLastIteration} = (function() {
    function cleanupOnLastIteration(data, func, abortErrorCodes) {
        let lastIteration = ++data.iteration >= data.iterations;
        let activeException = null;

        try {
            func();
        } catch (e) {
            lastIteration = true;
            activeException = e;

            throw e;
        } finally {
            if (lastIteration) {
                // Abort the latest transactions for this session as some may have been skipped due
                // to incrementing data.txnNumber. Go in increasing order, so as to avoid bumping
                // the txnNumber on the server past that of an in-progress transaction. See
                // SERVER-36847.
                for (let i = 0; i <= data.txnNumber; i++) {
                    try {
                        let res = abortTransaction(data.sessionDb, i, abortErrorCodes);
                        if (res.ok === 1) {
                            break;
                        }
                    } catch (exceptionDuringAbort) {
                        if (activeException !== null) {
                            print('Exception occurred: in finally block while another exception ' +
                                  'is active: ' + tojson(activeException));
                            print('Original exception stack trace: ' + activeException.stack);
                        }

                        throw exceptionDuringAbort;
                    }
                }
            }
        }
    }

    return {cleanupOnLastIteration};
})();