summaryrefslogtreecommitdiff
path: root/jstests/core/txns/create_indexes.js
blob: 59f407a349f1e3517aa70636649fbfc942b947f7 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
 * Tests simple cases of creating indexes inside a multi-document transaction, both
 * committing and aborting.
 *
 * @tags: [
 *   uses_transactions,
 * ]
 */
(function() {
"use strict";

load("jstests/libs/auto_retry_transaction_in_sharding.js");
load("jstests/libs/create_index_txn_helpers.js");

let doCreateIndexesTest = function(explicitCollectionCreate, multikeyIndex) {
    const session = db.getMongo().startSession();
    const collName = "create_new_indexes";
    const secondCollName = collName + "_second";

    let sessionDB = session.getDatabase("test");
    let sessionColl = sessionDB[collName];
    let secondSessionColl = sessionDB[secondCollName];
    sessionColl.drop({writeConcern: {w: "majority"}});
    secondSessionColl.drop({writeConcern: {w: "majority"}});

    jsTest.log("Testing createIndexes in a transaction");
    withTxnAndAutoRetryOnMongos(session, function() {
        createIndexAndCRUDInTxn(sessionDB, collName, explicitCollectionCreate, multikeyIndex);
    }, {writeConcern: {w: "majority"}});
    assert.eq(sessionColl.find({}).itcount(), 1);
    assert.eq(sessionColl.getIndexes().length, 2);
    sessionColl.drop({writeConcern: {w: "majority"}});

    jsTest.log("Testing multiple createIndexess in a transaction");
    withTxnAndAutoRetryOnMongos(session, function() {
        createIndexAndCRUDInTxn(sessionDB, collName, explicitCollectionCreate, multikeyIndex);
        createIndexAndCRUDInTxn(sessionDB, secondCollName, explicitCollectionCreate, multikeyIndex);
    }, {writeConcern: {w: "majority"}});
    assert.eq(sessionColl.find({}).itcount(), 1);
    assert.eq(secondSessionColl.find({}).itcount(), 1);
    assert.eq(sessionColl.getIndexes().length, 2);
    assert.eq(secondSessionColl.getIndexes().length, 2);

    sessionColl.drop({writeConcern: {w: "majority"}});
    secondSessionColl.drop({writeConcern: {w: "majority"}});

    jsTest.log("Testing createIndexes in a transaction that aborts");
    session.startTransaction({writeConcern: {w: "majority"}});
    retryOnceOnTransientAndRestartTxnOnMongos(session, () => {
        createIndexAndCRUDInTxn(sessionDB, collName, explicitCollectionCreate, multikeyIndex);
    }, {writeConcern: {w: "majority"}});
    session.abortTransaction();

    assert.eq(sessionColl.find({}).itcount(), 0);
    assert.eq(sessionColl.getIndexes().length, 0);

    jsTest.log("Testing multiple createIndexes in a transaction that aborts");
    session.startTransaction({writeConcern: {w: "majority"}});
    retryOnceOnTransientAndRestartTxnOnMongos(session, () => {
        createIndexAndCRUDInTxn(sessionDB, collName, explicitCollectionCreate, multikeyIndex);
        createIndexAndCRUDInTxn(sessionDB, secondCollName, explicitCollectionCreate, multikeyIndex);
    }, {writeConcern: {w: "majority"}});
    session.abortTransaction();
    assert.eq(sessionColl.find({}).itcount(), 0);
    assert.eq(sessionColl.getIndexes().length, 0);
    assert.eq(secondSessionColl.find({}).itcount(), 0);
    assert.eq(secondSessionColl.getIndexes().length, 0);

    sessionColl.drop({writeConcern: {w: "majority"}});

    jsTest.log(
        "Testing createIndexes with conflicting index specs in a transaction that aborts (SHOULD FAIL)");
    session.startTransaction({writeConcern: {w: "majority"}});
    retryOnceOnTransientAndRestartTxnOnMongos(session, () => {
        createIndexAndCRUDInTxn(sessionDB, collName, explicitCollectionCreate, multikeyIndex);
    }, {writeConcern: {w: "majority"}});
    assert.commandFailedWithCode(
        sessionColl.runCommand({createIndexes: collName, indexes: [conflictingIndexSpecs]}),
        ErrorCodes.IndexKeySpecsConflict);
    assert.commandFailedWithCode(session.abortTransaction_forTesting(),
                                 ErrorCodes.NoSuchTransaction);
    assert.eq(sessionColl.find({}).itcount(), 0);
    assert.eq(sessionColl.getIndexes().length, 0);

    sessionColl.drop({writeConcern: {w: "majority"}});

    jsTest.log(
        "Testing createIndexes on a non-empty collection created in the same transaction (SHOULD FAIL)");
    session.startTransaction({writeConcern: {w: "majority"}});
    assert.commandWorked(sessionDB.runCommand({create: collName}));
    assert.commandWorked(sessionColl.insert({a: 1}));

    assert.commandFailedWithCode(
        sessionColl.runCommand({createIndexes: collName, indexes: [indexSpecs]}),
        ErrorCodes.OperationNotSupportedInTransaction);
    assert.commandFailedWithCode(session.abortTransaction_forTesting(),
                                 ErrorCodes.NoSuchTransaction);

    assert.eq(sessionColl.find({}).itcount(), 0);
    assert.eq(sessionColl.getIndexes().length, 0);

    session.endSession();
};

doCreateIndexesTest(false /*explicitCollectionCreate*/, false /*multikeyIndex*/);
doCreateIndexesTest(true /*explicitCollectionCreate*/, false /*multikeyIndex*/);
doCreateIndexesTest(false /*explicitCollectionCreate*/, true /*multikeyIndex*/);
doCreateIndexesTest(true /*explicitCollectionCreate*/, true /*multikeyIndex*/);
}());