summaryrefslogtreecommitdiff
path: root/jstests/core/txns/await_prepared_transactions_on_FCV_downgrade.js
blob: e2c1fe3f3852e1e7690a7f651cfffa57491ee389 (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
/**
 * Test that we wait for prepared transactions to finish during downgrade to FCV 4.0.
 * @tags: [uses_transactions, uses_prepare_transaction, uses_testing_only_commands,
 *         assumes_superuser_permissions]
 */
(function() {
"use strict";
load("jstests/libs/feature_compatibility_version.js");
load("jstests/core/txns/libs/prepare_helpers.js");

const dbName = "test";
const collName = "await_prepared_transactions_on_FCV_downgrade";
const testDB = db.getSiblingDB(dbName);
const adminDB = db.getSiblingDB("admin");

testDB[collName].drop({writeConcern: {w: "majority"}});
assert.commandWorked(testDB.runCommand({create: collName, writeConcern: {w: "majority"}}));

const session = testDB.getMongo().startSession();
const sessionDB = session.getDatabase(dbName);

try {
    jsTestLog("Start a transaction.");
    session.startTransaction();
    assert.commandWorked(sessionDB[collName].insert({"a": 1}));

    jsTestLog("Put that transaction into a prepared state.");
    let prepareTimestamp = PrepareHelpers.prepareTransaction(session);

    // The setFCV command will need to acquire a global S lock to complete. The global
    // lock is currently held by prepare, so that will block. We use a failpoint to make that
    // command fail immediately when it tries to get the lock.
    assert.commandWorked(testDB.adminCommand(
        {configureFailPoint: "failNonIntentLocksIfWaitNeeded", mode: "alwaysOn"}));

    jsTestLog("Attempt to downgrade the featureCompatibilityVersion.");
    assert.commandFailedWithCode(
        testDB.adminCommand({setFeatureCompatibilityVersion: lastStableFCV}),
        ErrorCodes.LockTimeout);

    assert.commandWorked(
        testDB.adminCommand({configureFailPoint: "failNonIntentLocksIfWaitNeeded", mode: "off"}));

    jsTestLog("Verify that the setFCV command set the target version to 'lastStable'.");
    checkFCV(adminDB, lastStableFCV, lastStableFCV);

    jsTestLog("Commit the prepared transaction.");
    assert.commandWorked(PrepareHelpers.commitTransaction(session, prepareTimestamp));

    jsTestLog("Rerun the setFCV command and let it complete successfully.");
    assert.commandWorked(testDB.adminCommand({setFeatureCompatibilityVersion: lastStableFCV}));
    checkFCV(adminDB, lastStableFCV);

    jsTestLog("Verify that we are not allowed to prepare a transaction after downgrading.");
    session.startTransaction();
    assert.commandWorked(sessionDB[collName].insert({"b": 2}));
    assert.commandFailedWithCode(sessionDB.adminCommand({prepareTransaction: 1}),
                                 ErrorCodes.CommandNotSupported);
    assert.commandFailedWithCode(session.abortTransaction_forTesting(),
                                 ErrorCodes.NoSuchTransaction);
} finally {
    assert.commandWorked(
        testDB.adminCommand({configureFailPoint: "failNonIntentLocksIfWaitNeeded", mode: "off"}));

    jsTestLog("Restore the original featureCompatibilityVersion.");
    assert.commandWorked(testDB.adminCommand({setFeatureCompatibilityVersion: latestFCV}));
    checkFCV(adminDB, latestFCV);
}

session.endSession();
}());