summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/currentop_transaction_metrics.js
blob: d676167c2c27760649fbbc25db9f1e9141caebbd (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
/**
 * Tests that the time-tracking metrics in the 'transaction' object in currentOp() are being tracked
 * correctly.
 * @tags: [uses_transactions, uses_prepare_transaction]
 */

(function() {
    'use strict';
    load("jstests/core/txns/libs/prepare_helpers.js");

    const rst = new ReplSetTest({nodes: 1});
    rst.startSet();
    rst.initiate();

    const collName = 'currentop_transaction_metrics';
    const testDB = rst.getPrimary().getDB('test');
    const adminDB = rst.getPrimary().getDB('admin');
    testDB.runCommand({drop: collName, writeConcern: {w: "majority"}});
    assert.commandWorked(testDB[collName].insert({x: 1}, {writeConcern: {w: "majority"}}));

    const session = adminDB.getMongo().startSession({causalConsistency: false});
    const sessionDB = session.getDatabase('test');

    session.startTransaction();
    // Run a few operations so that the transaction goes through several active/inactive periods.
    assert.commandWorked(sessionDB[collName].update({}, {a: 1}));
    assert.commandWorked(sessionDB[collName].insert({_id: "insert-1"}));
    assert.commandWorked(sessionDB[collName].insert({_id: "insert-2"}));
    assert.commandWorked(sessionDB[collName].insert({_id: "insert-3"}));

    const transactionFilter = {
        active: false,
        'lsid': {$exists: true},
        'transaction.parameters.txnNumber': {$eq: 0},
        'transaction.parameters.autocommit': {$eq: false},
        'transaction.timePreparedMicros': {$exists: false}
    };

    let currentOp = adminDB.aggregate([{$currentOp: {}}, {$match: transactionFilter}]).toArray();
    assert.eq(currentOp.length, 1);

    // Check that the currentOp's transaction subdocument's fields align with our expectations.
    let transactionDocument = currentOp[0].transaction;
    assert.gte(transactionDocument.timeOpenMicros,
               transactionDocument.timeActiveMicros + transactionDocument.timeInactiveMicros);

    // Check that preparing the transaction enables the 'timePreparedMicros' field in currentOp.
    const prepareTimestamp = PrepareHelpers.prepareTransaction(session);

    const prepareTransactionFilter = {
        active: false,
        'lsid': {$exists: true},
        'transaction.parameters.txnNumber': {$eq: 0},
        'transaction.parameters.autocommit': {$eq: false},
        'transaction.timePreparedMicros': {$exists: true}
    };

    currentOp = adminDB.aggregate([{$currentOp: {}}, {$match: prepareTransactionFilter}]).toArray();
    assert.eq(currentOp.length, 1);

    // Check that the currentOp's transaction subdocument's fields align with our expectations.
    const prepareTransactionDocument = currentOp[0].transaction;
    assert.gte(prepareTransactionDocument.timeOpenMicros,
               prepareTransactionDocument.timeActiveMicros +
                   prepareTransactionDocument.timeInactiveMicros);
    assert.gte(prepareTransactionDocument.timePreparedMicros, 0);

    PrepareHelpers.commitTransaction(session, prepareTimestamp);
    session.endSession();

    rst.stopSet();
})();