summaryrefslogtreecommitdiff
path: root/jstests/noPassthroughWithMongod/fcv_changes_close_connections.js
blob: f78d3fcf41e241f6ebaad7850ef1a6081c5f1ad7 (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
// Test closing connections from internal clients on featureCompatibilityVersion changes.
(function() {
"use strict";

load("jstests/libs/parallel_shell_helpers.js");
load("jstests/libs/fail_point_util.js");

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

checkFCV(adminDB, latestFCV);
assert.commandWorked(testDB.runCommand({insert: collName, documents: [{x: 1}]}));

function testFCVChange({fcvDoc, expectConnectionClosed = true} = {}) {
    let findCmdFailPoint = configureFailPoint(
        db, "waitInFindBeforeMakingBatch", {nss: "test.coll", shouldCheckForInterrupt: true});

    // Mimic running a find command from an internal client with a lower binary version.
    function findWithLowerBinaryInternalClient(expectConnectionClosed) {
        const testDB = db.getSiblingDB("test");

        // The value of the 'internalClient.maxWireVersion' does not matter for the purpose of this
        // test as long as it is less than the latest server's maxWireVersion. The server will tag
        // this connection as an internal client connection with a lower binary version on receiving
        // a 'hello' with 'internalClient.maxWireVersion' less than the server's maxWireVersion.
        // Connections with such a tag are closed by the server on certain FCV changes.
        //
        // The 'hello' command below will succeed because this is run through the shell and the
        // shell is always compatible talking to the server. In reality though, a real internal
        // client with a lower binary version is expected to hang up immediately after receiving the
        // response to the 'hello' command from a latest server with an upgraded FCV.
        assert.commandWorked(testDB.adminCommand({
            hello: 1,
            internalClient: {minWireVersion: NumberInt(9), maxWireVersion: NumberInt(9)}
        }));

        // Since mongod expects other cluster members to always include explicit read/write concern
        // (on commands that accept read/write concern), this test must be careful to mimic this
        // behavior.
        if (expectConnectionClosed) {
            const e = assert.throws(
                () => testDB.runCommand({find: "coll", readConcern: {level: "local"}}));
            assert.includes(e.toString(), "network error while attempting to run command");
        } else {
            assert.commandWorked(testDB.runCommand({find: "coll", readConcern: {level: "local"}}));
        }
    }

    const joinFindThread = startParallelShell(
        funWithArgs(findWithLowerBinaryInternalClient, expectConnectionClosed), db.getMongo().port);

    jsTestLog("Waiting for waitInFindBeforeMakingBatch failpoint");
    findCmdFailPoint.wait();

    jsTestLog("Updating featureCompatibilityVersion document to: " + tojson(fcvDoc));
    assert.commandWorked(
        adminDB.system.version.update({_id: "featureCompatibilityVersion"}, fcvDoc));

    jsTestLog("Turning off waitInFindBeforeMakingBatch failpoint");
    findCmdFailPoint.off();

    jsTestLog("Joining findThread");
    joinFindThread();
}

function runTest(oldVersion) {
    jsTestLog("Testing with oldVersion: " + oldVersion);

    // Downgrading to oldVersion.
    // Test that connections from internal clients with lower binary versions are closed on FCV
    // downgrade. In reality, there shouldn't be any long-lived open connections from internal
    // clients with lower binary versions before the downgrade, because they are expected to hang up
    // immediately after receiving the response to the initial 'hello' command from an incompatible
    // server. But we still want to test that the server also try to close those incompatible
    // connections proactively on FCV downgrade.
    testFCVChange({
        fcvDoc: {version: oldVersion, targetVersion: oldVersion, previousVersion: latestFCV},
    });

    // Downgraded to oldVersion.
    // Test that connections from internal clients with lower binary versions are not closed on FCV
    // fully downgraded to oldVersion.
    testFCVChange({fcvDoc: {version: oldVersion}, expectConnectionClosed: false});

    // Upgrading from oldVersion to 'latest'.
    // Test that connections from internal clients with lower binary versions are closed on FCV
    // upgrade.
    testFCVChange({fcvDoc: {version: oldVersion, targetVersion: latestFCV}});

    // Upgraded to 'latest'.
    // Test that connections from internal clients with lower binary versions are closed on FCV
    // fully upgraded to 'latest'.
    testFCVChange({fcvDoc: {version: latestFCV}});
}

// Test upgrade/downgrade between 'latest' and 'last-continuous' if 'last-continuous' is not
// 'last-lts'.
if (lastContinuousFCV !== lastLTSFCV) {
    runTest(lastContinuousFCV);

    // Upgrading from last-lts to last-continuous. This FCV transition is allowed through the
    // setFeatureCompatibilityVersion command with fromConfigServer: true.
    testFCVChange({fcvDoc: {version: lastLTSFCV, targetVersion: lastContinuousFCV}});
}

// Test upgrade/downgrade between 'latest' and 'last-lts'.
runTest(lastLTSFCV);
})();