summaryrefslogtreecommitdiff
path: root/jstests/core/txns/commands_not_allowed_in_txn.js
blob: 256228e7d277f5f0f3d96c429094bc9cdbbcb0c8 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Test commands that are not allowed in multi-document transactions.
// @tags: [
//   sbe_incompatible,
//   uses_snapshot_read_concern,
//   uses_transactions,
// ]
(function() {
"use strict";

// TODO (SERVER-39704): Remove the following load after SERVER-397074 is completed
// For retryOnceOnTransientOnMongos.
load('jstests/libs/auto_retry_transaction_in_sharding.js');

const dbName = "test";
const collName = "commands_not_allowed_in_txn";
const testDB = db.getSiblingDB(dbName);
const testColl = testDB[collName];

testDB.runCommand({drop: collName, writeConcern: {w: "majority"}});
let txnNumber = 0;

const sessionOptions = {
    causalConsistency: false
};
const session = db.getMongo().startSession(sessionOptions);
const sessionDb = session.getDatabase(dbName);

const isMongos = assert.commandWorked(db.runCommand("hello")).msg === "isdbgrid";

assert.commandWorked(testDB.createCollection(testColl.getName(), {writeConcern: {w: "majority"}}));
assert.commandWorked(testDB.runCommand({
    createIndexes: collName,
    indexes: [{name: "geo_2d", key: {geo: "2d"}}],
    writeConcern: {w: "majority"}
}));

function setup() {
    testColl.dropIndex({a: 1});
    testDB.runCommand({drop: "create_collection", writeConcern: {w: "majority"}});
    testDB.runCommand({drop: "drop_collection", writeConcern: {w: "majority"}});
    assert.commandWorked(
        testDB.createCollection("drop_collection", {writeConcern: {w: "majority"}}));
}

function testCommand(command) {
    jsTest.log("Testing command: " + tojson(command));
    const errmsgRegExp = new RegExp(
        'Cannot run .* in a multi-document transaction.\|This command is not supported in transactions');

    // Check that the command runs successfully outside transactions.
    setup();
    assert.commandWorked(sessionDb.runCommand(command));

    // Check that the command cannot be used to start a transaction.
    setup();
    let res = assert.commandFailedWithCode(sessionDb.runCommand(Object.assign({}, command, {
        readConcern: {level: "snapshot"},
        txnNumber: NumberLong(++txnNumber),
        stmtId: NumberInt(0),
        startTransaction: true,
        autocommit: false
    })),
                                           ErrorCodes.OperationNotSupportedInTransaction);
    // Check that the command fails with expected error message.
    assert(res.errmsg.match(errmsgRegExp), res);

    // Mongos has special handling for commitTransaction to support commit recovery.
    if (!isMongos) {
        assert.commandFailedWithCode(sessionDb.adminCommand({
            commitTransaction: 1,
            txnNumber: NumberLong(txnNumber),
            stmtId: NumberInt(1),
            autocommit: false
        }),
                                     ErrorCodes.NoSuchTransaction);
    }

    // Check that the command fails inside a transaction, but does not abort the transaction.
    setup();
    // TODO (SERVER-39704): We use the retryOnceOnTransientOnMongos
    // function to handle how MongoS will propagate a StaleShardVersion error as a
    // TransientTransactionError. After SERVER-39704 is completed the
    // retryOnceOnTransientOnMongos can be removed
    retryOnceOnTransientOnMongos(session, () => {
        assert.commandWorked(sessionDb.runCommand({
            insert: collName,
            documents: [{}],
            readConcern: {level: "snapshot"},
            txnNumber: NumberLong(++txnNumber),
            stmtId: NumberInt(0),
            startTransaction: true,
            autocommit: false
        }));
    });

    res = assert.commandFailedWithCode(
        sessionDb.runCommand(Object.assign(
            {},
            command,
            {txnNumber: NumberLong(txnNumber), stmtId: NumberInt(1), autocommit: false})),
        ErrorCodes.OperationNotSupportedInTransaction);
    // Check that the command fails with expected error message.
    assert(res.errmsg.match(errmsgRegExp), res);
    assert.commandWorked(sessionDb.adminCommand({
        commitTransaction: 1,
        txnNumber: NumberLong(txnNumber),
        stmtId: NumberInt(2),
        autocommit: false
    }));
}

//
// Test a selection of commands that are not allowed in transactions.
//

const commands = [
    {count: collName},
    {count: collName, query: {a: 1}},
    {explain: {find: collName}},
    {filemd5: 1, root: "fs"},
    {isMaster: 1},
    {buildInfo: 1},
    {ping: 1},
    {listCommands: 1},
    {drop: "drop_collection", writeConcern: {w: "majority"}},
    // Output inline so the implicitly shard accessed collections override won't drop the
    // output collection during the active transaction test case, which would hang indefinitely
    // waiting for a database exclusive lock.
    {mapReduce: collName, map: function() {}, reduce: function(key, vals) {}, out: {inline: 1}},
];

// There is no applyOps command on mongos.
if (!isMongos) {
    commands.push(
        {applyOps: [{op: "u", ns: testColl.getFullName(), o2: {_id: 0}, o: {$set: {a: 5}}}]});
}

commands.forEach(testCommand);

//
// Test that a find command with the read-once cursor option is not allowed in a transaction.
//
assert.commandFailedWithCode(sessionDb.runCommand({
    find: collName,
    readOnce: true,
    readConcern: {level: "snapshot"},
    txnNumber: NumberLong(++txnNumber),
    stmtId: NumberInt(0),
    startTransaction: true,
    autocommit: false
}),
                             ErrorCodes.OperationNotSupportedInTransaction);

// Mongos has special handling for commitTransaction to support commit recovery.
if (!isMongos) {
    // The failed find should abort the transaction so a commit should fail.
    assert.commandFailedWithCode(sessionDb.adminCommand({
        commitTransaction: 1,
        autocommit: false,
        txnNumber: NumberLong(txnNumber),
        stmtId: NumberInt(1),
    }),
                                 ErrorCodes.NoSuchTransaction);
}

session.endSession();
}());