summaryrefslogtreecommitdiff
path: root/jstests/libs/txns/txn_override.js
blob: 920c359147aaded3c3fc5cea5d2a7318ca613fab (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/**
 * Override to run consecutive operations inside the same transaction. When an operation that
 * cannot be run inside of a transaction is encountered, the active transaction is committed
 * before running the next operation.
 */

(function() {
    'use strict';

    load("jstests/libs/override_methods/read_and_write_concern_helpers.js");
    load('jstests/libs/override_methods/override_helpers.js');
    load("jstests/libs/retryable_writes_util.js");

    const runCommandOriginal = Mongo.prototype.runCommand;

    const kCmdsSupportingTransactions = new Set([
        'aggregate',
        'delete',
        'find',
        'findAndModify',
        'findandmodify',
        'getMore',
        'insert',
        'update',
    ]);

    const kCmdsThatWrite = new Set([
        'insert',
        'update',
        'findAndModify',
        'findandmodify',
        'delete',
    ]);

    const kCmdsThatInsert = new Set([
        'insert',
        'update',
        'findAndModify',
        'findandmodify',
    ]);

    // Copied from ServerSession.TransactionStates.
    const TransactionStates = {
        kActive: 'active',
        kInactive: 'inactive',
    };

    // Array to hold pairs of (commandObj, makeFuncArgs) that will be iterated
    // over when retrying a command run in a txn on a network error.
    let ops = [];

    // Used to indicate whether the operation is being re-run, so we will not add
    // it to our ops array multple times.
    let retryOp = false;

    // Set the max number of operations to run in a transaction. Once we've
    // hit this number of operations, we will commit the transaction. This is to
    // prevent having to retry an extremely long running transaction.
    const maxOpsInTransaction = 10;

    // The last operation we logged upon failure. To avoid logging a command that
    // fails multiple times in a row each time it fails, we use this check if we've
    // just logged this command. This allows us to log failing commands to help with
    // debugging, but helps to avoid spamming logs.
    let lastLoggedOp;

    // The last TransientTransactionError on a commitTransaction that caused us to retry
    // the entire transaction. For help with debugging.
    let transientErrorToLog;

    function logFailedCommandAndError(cmdObj, cmdName, res) {
        if (cmdObj !== lastLoggedOp) {
            try {
                jsTestLog("Failed on cmd: " + tojson(cmdObj) + " with error: " + tojson(res));
            } catch (e) {
                jsTestLog("Failed on cmd: " + cmdName + " with error: " + tojson(res));
            }
            lastLoggedOp = cmdObj;

            if (transientErrorToLog) {
                jsTestLog("Error that caused retry of transaction " + tojson(transientErrorToLog));
            }
        }
    }

    function commandSupportsTxn(dbName, cmdName, cmdObj) {
        if (cmdName === 'commitTransaction' || cmdName === 'abortTransaction') {
            return true;
        }

        if (!kCmdsSupportingTransactions.has(cmdName)) {
            return false;
        }

        if (dbName === 'local' || dbName === 'config' || dbName === 'admin') {
            return false;
        }

        if (kCmdsThatWrite.has(cmdName)) {
            if (cmdObj[cmdName].startsWith('system.')) {
                return false;
            }
        }

        if (cmdObj.lsid === undefined) {
            return false;
        }

        return true;
    }

    function getTxnOptionsForClient(conn) {
        // We tack transaction options onto the client since we use one session per client.
        if (!conn.hasOwnProperty('txnOverrideOptions')) {
            conn.txnOverrideOptions = {
                stmtId: new NumberInt(0),
                autocommit: false,
                txnNumber: new NumberLong(-1),
            };
            conn.txnOverrideState = TransactionStates.kInactive;
        }
        return conn.txnOverrideOptions;
    }

    function incrementStmtIdBy(cmdName, cmdObjUnwrapped) {
        // Reserve the statement ids for batch writes.
        try {
            switch (cmdName) {
                case "insert":
                    return cmdObjUnwrapped.documents.length;
                case "update":
                    return cmdObjUnwrapped.updates.length;
                case "delete":
                    return cmdObjUnwrapped.deletes.length;
                default:
                    return 1;
            }
        } catch (e) {
            // Malformed command objects can cause errors to be thrown.
            return 1;
        }
    }

    function appendReadAndWriteConcern(conn, dbName, commandName, commandObj) {
        if (TestData.retryingOnNetworkError) {
            return;
        }

        let shouldForceReadConcern = kCommandsSupportingReadConcern.has(commandName);
        let shouldForceWriteConcern = kCommandsSupportingWriteConcern.has(commandName);

        if (commandObj.hasOwnProperty("autocommit")) {
            shouldForceReadConcern = false;
            if (commandObj.startTransaction === true) {
                shouldForceReadConcern = true;
            }
            if (!kCommandsSupportingWriteConcernInTransaction.has(commandName)) {
                shouldForceWriteConcern = false;
            }
        } else if (commandName === "aggregate") {
            if (OverrideHelpers.isAggregationWithListLocalSessionsStage(commandName, commandObj)) {
                // The $listLocalSessions stage can only be used with readConcern={level:
                // "local"}.
                shouldForceReadConcern = false;
            }

            if (OverrideHelpers.isAggregationWithOutStage(commandName, commandObj)) {
                // The $out stage can only be used with readConcern={level: "local"}.
                shouldForceReadConcern = false;
            } else {
                // A writeConcern can only be used with a $out stage.
                shouldForceWriteConcern = false;
            }

            if (commandObj.explain) {
                // Attempting to specify a readConcern while explaining an aggregation would
                // always return an error prior to SERVER-30582 and it is otherwise only
                // compatible with readConcern={level: "local"}.
                shouldForceReadConcern = false;
            }
        } else if (OverrideHelpers.isMapReduceWithInlineOutput(commandName, commandObj)) {
            // A writeConcern can only be used with non-inline output.
            shouldForceWriteConcern = false;
        }

        if (shouldForceReadConcern) {
            let readConcernLevel;
            if (commandObj.startTransaction === true) {
                readConcernLevel = "snapshot";
            } else if (jsTest.options().enableMajorityReadConcern !== false) {
                readConcernLevel = "majority";
            }

            if (commandObj.hasOwnProperty("readConcern") &&
                commandObj.readConcern.hasOwnProperty("level") &&
                commandObj.readConcern.level !== readConcernLevel) {
                throw new Error("refusing to override existing readConcern " +
                                commandObj.readConcern.level + " with readConcern " +
                                readConcernLevel);
            } else if (readConcernLevel) {
                commandObj.readConcern = {level: readConcernLevel};
            }

            // Only attach afterClusterTime if causal consistency is explicitly enabled. Note, it is
            // OK to send a readConcern with only afterClusterTime, which is interpreted as local
            // read concern by the server.
            if (TestData.hasOwnProperty("sessionOptions") &&
                TestData.sessionOptions.causalConsistency === true) {
                const driverSession = conn.getDB(dbName).getSession();
                const operationTime = driverSession.getOperationTime();
                if (operationTime !== undefined) {
                    if (commandObj.hasOwnProperty("readConcern")) {
                        commandObj.readConcern.afterClusterTime = operationTime;
                    } else {
                        commandObj.readConcern = {afterClusterTime: operationTime};
                    }
                }
            }
        }

        if (shouldForceWriteConcern) {
            if (commandObj.hasOwnProperty("writeConcern")) {
                let writeConcern = commandObj.writeConcern;
                if (typeof writeConcern !== "object" || writeConcern === null ||
                    (writeConcern.hasOwnProperty("w") &&
                     bsonWoCompare({_: writeConcern.w}, {_: "majority"}) !== 0)) {
                    throw new Error("Cowardly refusing to override write concern of command: " +
                                    tojson(commandObj));
                }
            }

            // Use a "signature" value that won't typically match a value assigned in normal
            // use. This way the wtimeout set by this override is distinguishable in the server
            // logs.
            commandObj.writeConcern = {w: "majority", wtimeout: 5 * 60 * 1000 + 456};
        }
    }

    function retryOnImplicitCollectionCreationIfNeeded(
        conn, dbName, commandName, commandObj, func, makeFuncArgs, res, txnOptions) {
        if (kCmdsThatInsert.has(commandName)) {
            // If the command inserted data and is not supported in a transaction, we assume it
            // failed because the collection did not exist. We will create the collection and
            // retry the command. If the collection did exist, we'll return the original
            // response because it failed for a different reason. Tests that expect collections
            // to not exist will have to be skipped.
            if (res.code === ErrorCodes.OperationNotSupportedInTransaction) {
                const createCmdRes = runCommandOriginal.call(conn,
                                                             dbName,
                                                             {
                                                               create: commandObj[commandName],
                                                               lsid: commandObj.lsid,
                                                               writeConcern: {w: 'majority'},
                                                             },
                                                             0);

                if (createCmdRes.ok !== 1) {
                    // If the error is retryable, we retry the entire transaction. Otherwise, we
                    // return the original error to the caller.
                    if (createCmdRes.code !== ErrorCodes.NamespaceExists &&
                        !RetryableWritesUtil.isRetryableCode(createCmdRes.code)) {
                        logFailedCommandAndError(commandObj, commandName, createCmdRes);
                        return res;
                    }
                } else {
                    assert.commandWorked(createCmdRes);
                }
            } else {
                // If the insert command failed for any other reason, we return the original
                // response without retrying.
                logFailedCommandAndError(commandObj, commandName, res);
                return res;
            }
            // We aborted the transaction, so we need to re-run every op in the transaction,
            // rather than just the current op.
            for (let op of ops) {
                retryOp = true;
                res = runCommandInTransactionIfNeeded(
                    conn, op.dbName, op.cmdName, op.cmdObj, func, op.makeFuncArgs);

                if (res.ok !== 1) {
                    logFailedCommandAndError(commandObj, commandName, res);
                    abortTransaction(conn, commandObj.lsid, txnOptions.txnNumber);
                    return res;
                }
            }
        }

        return res;
    }

    function updateAndGossipClusterTime(conn, dbName, commitRes, commandObj) {
        // Update the latest cluster time on the session manually after we commit so
        // that we will not read too early in the next transaction. At this point, we've
        // already run through the original processCommand path where we filled in the
        // clusterTime, so we will not update it otherwise.
        conn.getDB(dbName).getSession().processCommandResponse_forTesting(commitRes);

        // Gossip the later cluster time when we retry the command.
        if (commandObj.$clusterTime) {
            commandObj.$clusterTime = commitRes.$clusterTime;
        }
    }

    function commitTransaction(conn, lsid, txnNumber) {
        const res = conn.adminCommand({
            commitTransaction: 1,
            autocommit: false, lsid, txnNumber,
        });
        assert.commandWorked(res);
        conn.txnOverrideState = TransactionStates.kInactive;
        ops = [];

        return res;
    }

    function abortTransaction(conn, lsid, txnNumber) {
        // If there's been an error, we abort the transaction. It doesn't matter if the
        // abort call succeeds or not.
        runCommandOriginal.call(conn,
                                'admin',
                                {
                                  abortTransaction: 1,
                                  autocommit: false,
                                  lsid: lsid,
                                  txnNumber: txnNumber,
                                },
                                0);
        conn.txnOverrideState = TransactionStates.kInactive;
    }

    function continueTransaction(conn, txnOptions, dbName, cmdName, cmdObj, makeFuncArgs) {
        if (conn.txnOverrideState === TransactionStates.kInactive) {
            // First command in a transaction.
            txnOptions.txnNumber = new NumberLong(txnOptions.txnNumber + 1);
            txnOptions.stmtId = new NumberInt(0);

            cmdObj.startTransaction = true;

            conn.txnOverrideState = TransactionStates.kActive;
        }

        txnOptions.stmtId = new NumberInt(txnOptions.stmtId + incrementStmtIdBy(cmdName, cmdObj));

        cmdObj.txnNumber = txnOptions.txnNumber;
        cmdObj.stmtId = txnOptions.stmtId;
        cmdObj.autocommit = false;
        delete cmdObj.writeConcern;

        // We only want to add this op to the ops array if we have not already added it. If
        // retryingOnNetworkError is true, this op will already have been added. If retryOp
        // is false, this op is a write command that we are retrying thus this op has already
        // been added to the ops array.
        if (!TestData.retryingOnNetworkError && !retryOp) {
            // If the command object was created in a causally consistent session but did not
            // specify a readConcern level, it may have a readConcern object with only
            // afterClusterTime. The correct read concern options are added in
            // appendReadAndWriteConcern, so remove the readConcern before saving the operation in
            // this case.
            if (cmdObj.hasOwnProperty("readConcern")) {
                // Only remove the readConcern if it only contains afterClusterTime.
                const readConcernKeys = Object.keys(cmdObj.readConcern);
                if (readConcernKeys.length !== 1 || readConcernKeys[0] !== "afterClusterTime") {
                    throw new Error("Refusing to remove existing readConcern from command: " +
                                    tojson(cmdObj));
                }
                delete cmdObj.readConcern;
            }

            ops.push({dbName, cmdName, cmdObj, makeFuncArgs});
        }

        appendReadAndWriteConcern(conn, dbName, cmdName, cmdObj);
    }

    function runCommandInTransactionIfNeeded(
        conn, dbName, commandName, commandObj, func, makeFuncArgs) {
        let cmdObjUnwrapped = commandObj;
        let cmdNameUnwrapped = commandName;

        if (commandName === "query" || commandName === "$query") {
            commandObj[commandName] = Object.assign({}, cmdObjUnwrapped[commandName]);
            cmdObjUnwrapped = commandObj[commandName];
            cmdNameUnwrapped = Object.keys(cmdObjUnwrapped)[0];
        }

        const commandSupportsTransaction =
            commandSupportsTxn(dbName, cmdNameUnwrapped, cmdObjUnwrapped);

        const txnOptions = getTxnOptionsForClient(conn);
        if (commandSupportsTransaction) {
            if (cmdNameUnwrapped === "commitTransaction") {
                appendReadAndWriteConcern(conn, dbName, cmdNameUnwrapped, cmdObjUnwrapped);
                cmdObjUnwrapped.txnNumber = txnOptions.txnNumber;
            } else {
                // Commit the transaction if we've run `maxOpsInTransaction` commands as a part of
                // this transaction to avoid having to retry really long running transactions.
                if ((TestData.retryingOnNetworkError === false) &&
                    (ops.length >= maxOpsInTransaction) &&
                    (conn.txnOverrideState === TransactionStates.kActive)) {
                    let commitRes =
                        commitTransaction(conn, cmdObjUnwrapped.lsid, txnOptions.txnNumber);
                    updateAndGossipClusterTime(conn, dbName, commitRes, cmdObjUnwrapped);
                }

                continueTransaction(
                    conn, txnOptions, dbName, cmdNameUnwrapped, cmdObjUnwrapped, makeFuncArgs);
                retryOp = false;
            }
        } else {
            if (conn.txnOverrideState === TransactionStates.kActive) {
                let commitRes = commitTransaction(conn, cmdObjUnwrapped.lsid, txnOptions.txnNumber);
                updateAndGossipClusterTime(conn, dbName, commitRes, cmdObjUnwrapped);
            } else {
                ops = [];
            }

            appendReadAndWriteConcern(conn, dbName, cmdNameUnwrapped, cmdObjUnwrapped);
            if (commandName === 'drop' || commandName === 'convertToCapped') {
                // Convert all collection drops to w:majority so they won't prevent subsequent
                // operations in transactions from failing when failing to acquire collection locks.
                if (!cmdObjUnwrapped.writeConcern) {
                    cmdObjUnwrapped.writeConcern = {};
                }
                cmdObjUnwrapped.writeConcern.w = 'majority';
            }
        }

        let res = func.apply(conn, makeFuncArgs(commandObj));

        if ((res.ok !== 1) && (conn.txnOverrideState === TransactionStates.kActive)) {
            abortTransaction(conn, cmdObjUnwrapped.lsid, txnOptions.txnNumber);
            res = retryOnImplicitCollectionCreationIfNeeded(conn,
                                                            dbName,
                                                            cmdNameUnwrapped,
                                                            cmdObjUnwrapped,
                                                            func,
                                                            makeFuncArgs,
                                                            res,
                                                            txnOptions);
        }

        return res;
    }

    function retryEntireTransaction(conn, lsid, func) {
        let txnOptions = getTxnOptionsForClient(conn);
        let txnNumber = txnOptions.txnNumber;
        jsTestLog("Retrying entire transaction on TransientTransactionError for aborted txn " +
                  "with txnNum: " + txnNumber + " and lsid " + tojson(lsid));
        // Set the transactionState to inactive so continueTransaction() will bump the
        // txnNum.
        conn.txnOverrideState = TransactionStates.kInactive;

        // Re-run every command in the ops array.
        assert.gt(ops.length, 0);

        let res;
        for (let op of ops) {
            res = runCommandInTransactionIfNeeded(
                conn, op.dbName, op.cmdName, op.cmdObj, func, op.makeFuncArgs);

            if (res.hasOwnProperty('errorLabels') &&
                res.errorLabels.includes('TransientTransactionError')) {
                return retryEntireTransaction(conn, op.lsid, func);
            }
        }

        return res;
    }

    function retryCommitTransaction(conn, dbName, commandName, commandObj, func, makeFuncArgs) {
        let res;
        let retryCommit = false;
        jsTestLog("Retrying commitTransaction for txnNum: " + commandObj.txnNumber + " and lsid: " +
                  tojson(commandObj.lsid));
        do {
            res = runCommandInTransactionIfNeeded(
                conn, dbName, "commitTransaction", commandObj, func, makeFuncArgs);

            if (res.writeConcernError) {
                retryCommit = true;
                continue;
            }

            if (res.hasOwnProperty('errorLabels') &&
                res.errorLabels.includes('TransientTransactionError')) {
                transientErrorToLog = res;
                retryCommit = true;
                res = retryEntireTransaction(conn, commandObj.lsid, func);
            } else if (res.ok === 1) {
                retryCommit = false;
            }
        } while (retryCommit);

        return res;
    }

    function runCommandOnNetworkErrorRetry(
        conn, dbName, commandName, commandObj, func, makeFuncArgs) {
        transientErrorToLog = null;
        // If the ops array is empty, we failed on a command not being run in a
        // transaction and need to retry just this command.
        if (ops.length === 0) {
            // Set the transactionState to inactive so continueTransaction() will bump the
            // txnNum.
            conn.txnOverrideState = TransactionStates.kInactive;
            return runCommandInTransactionIfNeeded(
                conn, dbName, commandName, commandObj, func, makeFuncArgs);
        }

        if (commandName === "commitTransaction") {
            return retryCommitTransaction(
                conn, dbName, commandName, commandObj, func, makeFuncArgs);
        }

        return retryEntireTransaction(conn, commandObj.lsid, func);
    }

    function runCommandWithTransactionRetries(
        conn, dbName, commandName, commandObj, func, makeFuncArgs) {
        const driverSession = conn.getDB(dbName).getSession();
        if (driverSession.getSessionId() === null) {
            // Sessions is explicitly disabled for this command. So we skip overriding it to
            // use transactions.
            return func.apply(conn, makeFuncArgs(commandObj));
        }

        let res;
        if (TestData.retryingOnNetworkError !== true) {
            res = runCommandInTransactionIfNeeded(
                conn, dbName, commandName, commandObj, func, makeFuncArgs);

            if (commandName === "commitTransaction") {
                while (res.writeConcernError) {
                    res = runCommandInTransactionIfNeeded(
                        conn, dbName, commandName, commandObj, func, makeFuncArgs);
                }
            }

            return res;
        }

        res = runCommandOnNetworkErrorRetry(
            conn, dbName, commandName, commandObj, func, makeFuncArgs);

        return res;
    }

    startParallelShell = function() {
        throw new Error(
            "Cowardly refusing to run test with transaction override enabled when it uses" +
            "startParalleShell()");
    };

    OverrideHelpers.overrideRunCommand(runCommandWithTransactionRetries);
})();