summaryrefslogtreecommitdiff
path: root/jstests/core/txns/prepared_transactions_do_not_block_non_conflicting_ddl.js
blob: a5bf806987d15a8397b452d19ceb8792433f1f59 (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
// Test that prepared transactions don't block DDL operations on the non-conflicting collections.
// @tags: [uses_transactions, uses_prepare_transaction]
(function() {
    "use strict";

    load("jstests/core/txns/libs/prepare_helpers.js");
    const dbName = "prepared_transactions_do_not_block_non_conflicting_ddl";
    const collName = "transactions_collection";
    const otherDBName = "prepared_transactions_do_not_block_non_conflicting_ddl_other";
    const otherCollName = "transactions_collection_other";
    const testDB = db.getSiblingDB(dbName);
    const otherDB = db.getSiblingDB(otherDBName);

    const session = testDB.getMongo().startSession({causalConsistency: false});
    const sessionDB = session.getDatabase(dbName);
    const sessionColl = sessionDB[collName];

    // Setup.
    testDB.dropDatabase();
    otherDB.dropDatabase();
    assert.commandWorked(sessionColl.insert({_id: 1, x: 0}));

    /**
     * Tests that DDL operations on non-conflicting namespaces don't block on transactions.
     */
    function testSuccess(cmdDBName, ddlCmd) {
        session.startTransaction();
        assert.commandWorked(sessionColl.update({_id: 1}, {$inc: {x: 1}}));
        const prepareTimestamp = PrepareHelpers.prepareTransaction(session);
        assert.commandWorked(testDB.getSiblingDB(cmdDBName).runCommand(ddlCmd));
        assert.commandWorked(
            PrepareHelpers.commitTransactionAfterPrepareTS(session, prepareTimestamp));
    }

    jsTest.log("Test 'create'.");
    const createCmd = {create: collName};
    testSuccess(otherDBName, createCmd);

    jsTest.log("Test 'createIndexes'.");
    const createIndexesCmd = {createIndexes: collName, indexes: [{key: {x: 1}, name: "x_1"}]};
    testSuccess(otherDBName, createIndexesCmd);

    jsTest.log("Test 'dropIndexes'.");
    const dropIndexesCmd = {dropIndexes: collName, index: "x_1"};
    testSuccess(otherDBName, dropIndexesCmd);

    sessionColl.createIndex({multiKeyField: 1});
    jsTest.log("Test 'insert' that enables multi-key index on the same collection.");
    const insertAndSetMultiKeyCmd = {insert: collName, documents: [{multiKeyField: [1, 2]}]};
    testSuccess(dbName, insertAndSetMultiKeyCmd);

    jsTest.log("Test 'drop'.");
    const dropCmd = {drop: collName};
    testSuccess(otherDBName, dropCmd);

    jsTest.log("Test 'renameCollection'.");
    assert.commandWorked(otherDB.getCollection(collName).insert({x: "doc-for-rename-collection"}));
    otherDB.runCommand({drop: otherCollName});
    const renameCollectionCmd = {
        renameCollection: otherDBName + "." + collName,
        to: otherDBName + "." + otherCollName
    };
    testSuccess("admin", renameCollectionCmd);

    session.endSession();
}());