summaryrefslogtreecommitdiff
path: root/jstests/replsets/awaitable_ismaster_fcv_change.js
blob: df2175a93fb403ba137cefe6aafceca195d983d0 (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
/**
 * This tests that upgrading and downgrading FCV will unblock and reply to waiting isMaster
 * requests.
 *
 * @tags: [multiversion_incompatible]
 */

(function() {
"use strict";
load("jstests/libs/parallel_shell_helpers.js");
load("jstests/libs/fail_point_util.js");

const rst = new ReplSetTest({nodes: [{}, {rsConfig: {priority: 0, votes: 0}}]});
rst.startSet();
rst.initiate();

const primary = rst.getPrimary();
const secondary = rst.getSecondary();
const primaryAdminDB = primary.getDB("admin");
const secondaryAdminDB = secondary.getDB("admin");

function runAwaitableIsMasterBeforeFCVChange(
    topologyVersionField, targetFCV, isPrimary, prevMinWireVersion, serverMaxWireVersion) {
    db.getMongo().setSecondaryOk();
    let response = assert.commandWorked(db.runCommand({
        isMaster: 1,
        topologyVersion: topologyVersionField,
        maxAwaitTimeMS: 99999999,
        internalClient:
            {minWireVersion: NumberInt(0), maxWireVersion: NumberInt(serverMaxWireVersion)},
    }));

    // We only expect to increment the server TopologyVersion when the minWireVersion has changed.
    // This can only happen in two scenarios:
    // 1. Setting featureCompatibilityVersion from downgrading to fullyDowngraded.
    // 2. Setting featureCompatibilityVersion from fullyDowngraded to upgrading.
    assert.eq(topologyVersionField.counter + 1, response.topologyVersion.counter, response);
    const expectedIsMasterValue = isPrimary;
    const expectedSecondaryValue = !isPrimary;

    assert.eq(expectedIsMasterValue, response.ismaster, response);
    assert.eq(expectedSecondaryValue, response.secondary, response);

    const minWireVersion = response.minWireVersion;
    const maxWireVersion = response.maxWireVersion;
    assert.neq(prevMinWireVersion, minWireVersion);
    if (targetFCV === latestFCV) {
        // minWireVersion should always equal maxWireVersion if we have not fully downgraded FCV.
        assert.eq(minWireVersion, maxWireVersion, response);
    } else if (targetFCV === lastContinuousFCV) {
        assert.eq(minWireVersion + 1, maxWireVersion, response);
    } else {
        assert.eq(minWireVersion + numVersionsSinceLastLTS, maxWireVersion, response);
    }
}

function runTest(downgradeFCV) {
    jsTestLog("Running test with downgradeFCV: " + downgradeFCV);

    // This test manually runs isMaster with the 'internalClient' field, which means that to the
    // mongod, the connection appears to be from another server. This makes mongod to return an
    // isMaster response with a real 'minWireVersion' for internal clients instead of 0.
    //
    // The value of 'internalClient.maxWireVersion' in the isMaster command does not matter for the
    // purpose of this test and the isMaster command will succeed regardless 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 isMaster command from a latest server with an upgraded
    // FCV.
    //
    // And we need to use a side connection to do so in order to prevent the test connection from
    // being closed on FCV changes.
    function isMasterAsInternalClient() {
        let connInternal = new Mongo(primary.host);
        const res = assert.commandWorked(connInternal.adminCommand({
            isMaster: 1,
            internalClient: {minWireVersion: NumberInt(0), maxWireVersion: NumberInt(9)}
        }));
        connInternal.close();
        return res;
    }

    // Get the server topologyVersion, minWireVersion, and maxWireversion.
    const primaryResult = isMasterAsInternalClient();
    assert(primaryResult.hasOwnProperty("topologyVersion"), tojson(primaryResult));

    const maxWireVersion = primaryResult.maxWireVersion;
    const initMinWireVersion = primaryResult.minWireVersion;
    assert.eq(maxWireVersion, initMinWireVersion);

    const secondaryResult = assert.commandWorked(secondaryAdminDB.runCommand({isMaster: 1}));
    assert(secondaryResult.hasOwnProperty("topologyVersion"), tojson(secondaryResult));
    const primaryTopologyVersion = primaryResult.topologyVersion;
    assert(primaryTopologyVersion.hasOwnProperty("processId"), tojson(primaryTopologyVersion));
    assert(primaryTopologyVersion.hasOwnProperty("counter"), tojson(primaryTopologyVersion));

    const secondaryTopologyVersion = secondaryResult.topologyVersion;
    assert(secondaryTopologyVersion.hasOwnProperty("processId"), tojson(secondaryTopologyVersion));
    assert(secondaryTopologyVersion.hasOwnProperty("counter"), tojson(secondaryTopologyVersion));

    // A failpoint signalling that the servers have received the isMaster request and are waiting
    // for a topology change.
    let primaryFailPoint = configureFailPoint(primary, "waitForIsMasterResponse");
    let secondaryFailPoint = configureFailPoint(secondary, "waitForIsMasterResponse");

    // Send an awaitable isMaster request. This will block until maxAwaitTimeMS has elapsed or a
    // topology change happens.
    let awaitIsMasterBeforeDowngradeFCVOnPrimary =
        startParallelShell(funWithArgs(runAwaitableIsMasterBeforeFCVChange,
                                       primaryTopologyVersion,
                                       downgradeFCV,
                                       true /* isPrimary */,
                                       initMinWireVersion,
                                       maxWireVersion),
                           primary.port);
    let awaitIsMasterBeforeDowngradeFCVOnSecondary =
        startParallelShell(funWithArgs(runAwaitableIsMasterBeforeFCVChange,
                                       secondaryTopologyVersion,
                                       downgradeFCV,
                                       false /* isPrimary */,
                                       initMinWireVersion,
                                       maxWireVersion),
                           secondary.port);
    primaryFailPoint.wait();
    secondaryFailPoint.wait();

    // Each node has one isMaster request waiting on a topology change.
    let numAwaitingTopologyChangeOnPrimary =
        primaryAdminDB.serverStatus().connections.awaitingTopologyChanges;
    let numAwaitingTopologyChangeOnSecondary =
        secondaryAdminDB.serverStatus().connections.awaitingTopologyChanges;
    assert.eq(1, numAwaitingTopologyChangeOnPrimary);
    assert.eq(1, numAwaitingTopologyChangeOnSecondary);

    // Setting the FCV to the same version will not trigger an isMaster response.
    assert.commandWorked(primaryAdminDB.runCommand({setFeatureCompatibilityVersion: latestFCV}));
    checkFCV(primaryAdminDB, latestFCV);
    checkFCV(secondaryAdminDB, latestFCV);

    // Each node still has one isMaster request waiting on a topology change.
    numAwaitingTopologyChangeOnPrimary =
        primaryAdminDB.serverStatus().connections.awaitingTopologyChanges;
    numAwaitingTopologyChangeOnSecondary =
        secondaryAdminDB.serverStatus().connections.awaitingTopologyChanges;
    assert.eq(1, numAwaitingTopologyChangeOnPrimary);
    assert.eq(1, numAwaitingTopologyChangeOnSecondary);

    jsTestLog("Downgrade the featureCompatibilityVersion.");
    // Downgrading the FCV will cause the isMaster requests to respond on both primary and
    // secondary.
    assert.commandWorked(primaryAdminDB.runCommand({setFeatureCompatibilityVersion: downgradeFCV}));
    awaitIsMasterBeforeDowngradeFCVOnPrimary();
    awaitIsMasterBeforeDowngradeFCVOnSecondary();
    // Ensure the featureCompatibilityVersion document update has been replicated.
    rst.awaitReplication();
    checkFCV(primaryAdminDB, downgradeFCV);
    checkFCV(secondaryAdminDB, downgradeFCV);

    // All isMaster requests should have been responded to after the FCV change.
    numAwaitingTopologyChangeOnPrimary =
        primaryAdminDB.serverStatus().connections.awaitingTopologyChanges;
    numAwaitingTopologyChangeOnSecondary =
        secondaryAdminDB.serverStatus().connections.awaitingTopologyChanges;
    assert.eq(0, numAwaitingTopologyChangeOnPrimary);
    assert.eq(0, numAwaitingTopologyChangeOnSecondary);

    // Get the new topologyVersion.
    let primaryResponseAfterDowngrade = isMasterAsInternalClient();
    assert(primaryResponseAfterDowngrade.hasOwnProperty("topologyVersion"),
           tojson(primaryResponseAfterDowngrade));
    let primaryTopologyVersionAfterDowngrade = primaryResponseAfterDowngrade.topologyVersion;
    let minWireVersionAfterDowngrade = primaryResponseAfterDowngrade.minWireVersion;

    let secondaryResponseAfterDowngrade =
        assert.commandWorked(secondaryAdminDB.runCommand({isMaster: 1}));
    assert(secondaryResponseAfterDowngrade.hasOwnProperty("topologyVersion"),
           tojson(secondaryResponseAfterDowngrade));
    let secondaryTopologyVersionAfterDowngrade = secondaryResponseAfterDowngrade.topologyVersion;

    if (downgradeFCV === lastLTSFCV && lastLTSFCV !== lastContinuousFCV) {
        // Test upgrading from last-lts to last-continuous FCV. We allow this upgrade path via the
        // setFeatureCompatibilityVersion command with fromConfigServer: true.

        // Reconfigure the failpoint to refresh the number of times the failpoint has been entered.
        primaryFailPoint = configureFailPoint(primary, "waitForIsMasterResponse");
        secondaryFailPoint = configureFailPoint(secondary, "waitForIsMasterResponse");
        let awaitIsMasterBeforeUpgradeOnPrimary =
            startParallelShell(funWithArgs(runAwaitableIsMasterBeforeFCVChange,
                                           primaryTopologyVersionAfterDowngrade,
                                           lastContinuousFCV,
                                           true /* isPrimary */,
                                           minWireVersionAfterDowngrade,
                                           maxWireVersion),
                               primary.port);
        let awaitIsMasterBeforeUpgradeOnSecondary =
            startParallelShell(funWithArgs(runAwaitableIsMasterBeforeFCVChange,
                                           secondaryTopologyVersionAfterDowngrade,
                                           lastContinuousFCV,
                                           false /* isPrimary */,
                                           minWireVersionAfterDowngrade,
                                           maxWireVersion),
                               secondary.port);
        primaryFailPoint.wait();
        secondaryFailPoint.wait();

        // Each node has one isMaster request waiting on a topology change.
        numAwaitingTopologyChangeOnPrimary =
            primaryAdminDB.serverStatus().connections.awaitingTopologyChanges;
        numAwaitingTopologyChangeOnSecondary =
            secondaryAdminDB.serverStatus().connections.awaitingTopologyChanges;
        assert.eq(1, numAwaitingTopologyChangeOnPrimary);
        assert.eq(1, numAwaitingTopologyChangeOnSecondary);

        // Upgrade the FCV to last-continuous.
        assert.commandWorked(primaryAdminDB.runCommand(
            {setFeatureCompatibilityVersion: lastContinuousFCV, fromConfigServer: true}));
        awaitIsMasterBeforeUpgradeOnPrimary();
        awaitIsMasterBeforeUpgradeOnSecondary();

        // Ensure the featureCompatibilityVersion document update has been replicated.
        rst.awaitReplication();
        checkFCV(primaryAdminDB, lastContinuousFCV);
        checkFCV(secondaryAdminDB, lastContinuousFCV);

        // All isMaster requests should have been responded to after the FCV change.
        numAwaitingTopologyChangeOnPrimary =
            primaryAdminDB.serverStatus().connections.awaitingTopologyChanges;
        numAwaitingTopologyChangeOnSecondary =
            secondaryAdminDB.serverStatus().connections.awaitingTopologyChanges;
        assert.eq(0, numAwaitingTopologyChangeOnPrimary);
        assert.eq(0, numAwaitingTopologyChangeOnSecondary);

        // Reset the FCV back to last-lts and the get the new isMaster parameters.
        // We must upgrade to latestFCV first since downgrading from last-continuous to last-stable
        // is forbidden.
        assert.commandWorked(
            primaryAdminDB.runCommand({setFeatureCompatibilityVersion: latestFCV}));
        assert.commandWorked(
            primaryAdminDB.runCommand({setFeatureCompatibilityVersion: lastLTSFCV}));
        rst.awaitReplication();
        checkFCV(primaryAdminDB, lastLTSFCV);
        checkFCV(secondaryAdminDB, lastLTSFCV);

        primaryResponseAfterDowngrade = isMasterAsInternalClient();
        assert(primaryResponseAfterDowngrade.hasOwnProperty("topologyVersion"),
               tojson(primaryResponseAfterDowngrade));
        primaryTopologyVersionAfterDowngrade = primaryResponseAfterDowngrade.topologyVersion;
        minWireVersionAfterDowngrade = primaryResponseAfterDowngrade.minWireVersion;

        secondaryResponseAfterDowngrade =
            assert.commandWorked(secondaryAdminDB.runCommand({isMaster: 1}));
        assert(secondaryResponseAfterDowngrade.hasOwnProperty("topologyVersion"),
               tojson(secondaryResponseAfterDowngrade));
        secondaryTopologyVersionAfterDowngrade = secondaryResponseAfterDowngrade.topologyVersion;
    }

    // Reconfigure the failpoint to refresh the number of times the failpoint has been entered.
    primaryFailPoint = configureFailPoint(primary, "waitForIsMasterResponse");
    secondaryFailPoint = configureFailPoint(secondary, "waitForIsMasterResponse");
    let awaitIsMasterBeforeUpgradeFCVOnPrimary =
        startParallelShell(funWithArgs(runAwaitableIsMasterBeforeFCVChange,
                                       primaryTopologyVersionAfterDowngrade,
                                       latestFCV,
                                       true /* isPrimary */,
                                       minWireVersionAfterDowngrade,
                                       maxWireVersion),
                           primary.port);
    let awaitIsMasterBeforeUpgradeFCVOnSecondary =
        startParallelShell(funWithArgs(runAwaitableIsMasterBeforeFCVChange,
                                       secondaryTopologyVersionAfterDowngrade,
                                       latestFCV,
                                       false /* isPrimary */,
                                       minWireVersionAfterDowngrade,
                                       maxWireVersion),
                           secondary.port);
    primaryFailPoint.wait();
    secondaryFailPoint.wait();

    // Each node has one isMaster request waiting on a topology change.
    numAwaitingTopologyChangeOnPrimary =
        primaryAdminDB.serverStatus().connections.awaitingTopologyChanges;
    numAwaitingTopologyChangeOnSecondary =
        secondaryAdminDB.serverStatus().connections.awaitingTopologyChanges;
    assert.eq(1, numAwaitingTopologyChangeOnPrimary);
    assert.eq(1, numAwaitingTopologyChangeOnSecondary);

    // Setting the FCV to the same version will not trigger an isMaster response.
    assert.commandWorked(primaryAdminDB.runCommand({setFeatureCompatibilityVersion: downgradeFCV}));
    checkFCV(primaryAdminDB, downgradeFCV);
    checkFCV(secondaryAdminDB, downgradeFCV);

    // Each node still has one isMaster request waiting on a topology change.
    numAwaitingTopologyChangeOnPrimary =
        primaryAdminDB.serverStatus().connections.awaitingTopologyChanges;
    numAwaitingTopologyChangeOnSecondary =
        secondaryAdminDB.serverStatus().connections.awaitingTopologyChanges;
    assert.eq(1, numAwaitingTopologyChangeOnPrimary);
    assert.eq(1, numAwaitingTopologyChangeOnSecondary);

    jsTestLog("Upgrade the featureCompatibilityVersion.");
    // Upgrading the FCV will cause the isMaster requests to respond on both primary and secondary.
    assert.commandWorked(primaryAdminDB.runCommand({setFeatureCompatibilityVersion: latestFCV}));
    awaitIsMasterBeforeUpgradeFCVOnPrimary();
    awaitIsMasterBeforeUpgradeFCVOnSecondary();
    // Ensure the featureCompatibilityVersion document update has been replicated.
    rst.awaitReplication();
    checkFCV(primaryAdminDB, latestFCV);
    checkFCV(secondaryAdminDB, latestFCV);

    // All isMaster requests should have been responded to after the FCV change.
    numAwaitingTopologyChangeOnPrimary =
        primaryAdminDB.serverStatus().connections.awaitingTopologyChanges;
    numAwaitingTopologyChangeOnSecondary =
        secondaryAdminDB.serverStatus().connections.awaitingTopologyChanges;
    assert.eq(0, numAwaitingTopologyChangeOnPrimary);
    assert.eq(0, numAwaitingTopologyChangeOnSecondary);
}

runTest(lastLTSFCV);
runTest(lastContinuousFCV);

rst.stopSet();
})();