summaryrefslogtreecommitdiff
path: root/jstests/replsets/disallow_shardsvr_transactions_wcMajorityJournal_false.js
blob: 12ebc3eb40d15c6b3dbb6e6a21e8098988554681 (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
/**
 * Test that transactions are not allowed on shard servers that have
 * writeConcernMajorityJournalDefault = false.
 *
 * @tags: [uses_transactions]
 */

(function() {
    "use strict";

    // A testing exemption was made to allow transactions on shard server even if
    // writeConcernMajorityJournalDefault = false. So we need to disable the exemption in this test
    // in order to test the behavior.
    jsTest.setOption('enableTestCommands', false);

    // The following two options by default do not support enableTestCommands=false, change them
    // accordingly so this test can run.
    TestData.roleGraphInvalidationIsFatal = false;
    TestData.authenticationDatabase = "local";

    // Start the replica set with --shardsvr.
    const replSet = new ReplSetTest({nodes: 1, nodeOptions: {shardsvr: ""}});
    replSet.startSet();
    let conf = replSet.getReplSetConfig();
    conf.writeConcernMajorityJournalDefault = false;
    replSet.initiate(conf);

    const primary = replSet.getPrimary();
    const session = primary.startSession();
    const sessionDb = session.getDatabase("test");
    const sessionColl = sessionDb.getCollection("foo");

    jsTestLog("Test that non-transactional operations are allowed.");
    assert.commandWorked(sessionColl.insert({_id: 1}));

    jsTestLog("Test that transactions are not allowed.");
    session.startTransaction();
    assert.commandFailedWithCode(sessionColl.insert({_id: 2}),
                                 ErrorCodes.OperationNotSupportedInTransaction);
    // All commands are not allowed including abortTransaction.
    assert.commandFailedWithCode(session.abortTransaction_forTesting(),
                                 ErrorCodes.OperationNotSupportedInTransaction);

    jsTestLog("Test that retryable writes are allowed.");
    assert.commandWorked(
        sessionDb.runCommand({insert: "foo", documents: [{_id: 3}], txnNumber: NumberLong(1)}));

    // Assert documents inserted.
    assert.docEq(sessionColl.find().sort({_id: 1}).toArray(), [{_id: 1}, {_id: 3}]);

    replSet.stopSet();
}());