summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/transaction_write_with_snapshot_unavailable.js
blob: 20bf20a55ca217803f3b886eed0653b7897f1b9f (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
/**
 * Test that write errors in a transaction due to SnapshotUnavailable are labelled
 * TransientTransactionError and the error is reported at the top level, not in a writeErrors array.
 *
 * Other transient transaction errors are tested elsewhere: WriteConflict is tested in
 * transactions_write_conflicts.js, NotWritablePrimary is tested in transient_txn_error_labels.js,
 * and NoSuchTransaction is tested in transient_txn_error_labels_with_write_concern.js.
 *
 * @tags: [uses_transactions]
 */
(function() {
"use strict";

load("jstests/libs/feature_flag_util.js");

const name = "transaction_write_with_snapshot_unavailable";
const replTest = new ReplSetTest({name: name, nodes: 1});
replTest.startSet();
replTest.initiate();

const dbName = name;
const dbNameB = dbName + "B";
const collName = "collection";
const collNameB = collName + "B";

const primary = replTest.getPrimary();
const primaryDB = primary.getDB(dbName);

if (FeatureFlagUtil.isEnabled(primaryDB, "PointInTimeCatalogLookups")) {
    // With the PointInTimeCatalogLookups feature this test doesn't make sense as the
    // SnapshotUnavailable error will be removed
    replTest.stopSet();
    return;
}

assert.commandWorked(primaryDB[collName].insertOne({}, {writeConcern: {w: "majority"}}));

function testOp(cmd) {
    let op = Object.getOwnPropertyNames(cmd)[0];
    let session = primary.startSession();
    let sessionDB = session.getDatabase(name);

    jsTestLog(`Testing that WriteConflict during ${op} is labelled TransientTransactionError`);

    session.startTransaction({readConcern: {level: "snapshot"}});
    assert.commandWorked(sessionDB.runCommand({insert: collName, documents: [{}]}));
    // Create collection outside transaction, cannot write to it in the transaction
    assert.commandWorked(primaryDB.getSiblingDB(dbNameB).runCommand({create: collNameB}));

    let res;
    try {
        res = sessionDB.getSiblingDB(dbNameB).runCommand(cmd);
        assert.commandFailedWithCode(res, ErrorCodes.SnapshotUnavailable);
        assert.eq(res.ok, 0);
        assert(!res.hasOwnProperty("writeErrors"));
        assert.eq(res.errorLabels, ["TransientTransactionError"]);
    } catch (ex) {
        printjson(cmd);
        printjson(res);
        throw ex;
    }

    assert.commandFailedWithCode(session.abortTransaction_forTesting(),
                                 ErrorCodes.NoSuchTransaction);
    assert.commandWorked(primaryDB.getSiblingDB(dbNameB).runCommand(
        {dropDatabase: 1, writeConcern: {w: "majority"}}));
}

testOp({insert: collNameB, documents: [{_id: 0}]});
testOp({update: collNameB, updates: [{q: {}, u: {$set: {x: 1}}}]});
testOp({delete: collNameB, deletes: [{q: {_id: 0}, limit: 1}]});

replTest.stopSet();
})();