summaryrefslogtreecommitdiff
path: root/jstests/replsets/dbhash_lock_acquisition.js
blob: e8b7ff9cbad3dd66e67be9213d46b7fdf7fdd633 (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
/**
 * Tests that the dbHash command acquires IX mode locks on the global, database, and collection
 * resources when running inside a multi-statement transaction.
 */
(function() {
    "use strict";

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

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

    if (!db.serverStatus().storageEngine.supportsSnapshotReadConcern) {
        rst.stopSet();
        return;
    }

    const session = primary.startSession({causalConsistency: false});
    const sessionDB = session.getDatabase(db.getName());

    function assertTransactionAcquiresIXLocks(txnOptions) {
        session.startTransaction(txnOptions);

        assert.commandWorked(sessionDB.runCommand({dbHash: 1}));
        const ops = db.currentOp({"lsid.id": session.getSessionId().id}).inprog;
        assert.eq(1,
                  ops.length,
                  () => "Failed to find session in currentOp() output: " + tojson(db.currentOp()));
        assert.eq(ops[0].locks, {Global: "w", Database: "w", Collection: "w"});

        session.abortTransaction();
    }

    // We insert a document so the dbHash command has a collection to process.
    assert.commandWorked(db.mycoll.insert({}, {writeConcern: {w: "majority"}}));

    assertTransactionAcquiresIXLocks({});
    assertTransactionAcquiresIXLocks({readConcern: {level: "local"}});
    assertTransactionAcquiresIXLocks({readConcern: {level: "snapshot"}});
    assertTransactionAcquiresIXLocks({
        readConcern: {
            level: "snapshot",
            atClusterTime: session.getOperationTime(),
        }
    });

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