summaryrefslogtreecommitdiff
path: root/jstests/replsets/check_replication_hello_response_fields.js
blob: d58ee13936ae7aae8f29b5975fdeb8e271aeede2 (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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/**
 * Checks all the easily testable fields in the response object returned by the hello() command and
 * its aliases, isMaster() and ismaster(). This test also checks that fields that should not be in
 * the document are absent.
 * @tags: [
 * ]
 */

// Skip db hash check because node 2 is slave delayed and may time out on awaitReplication.
TestData.skipCheckDBHashes = true;

load("jstests/replsets/rslib.js");

// function create the error message if an assert fails
var generateErrorString = function(badFields, missingFields, badValues, result) {
    var str = "\nThe result was:\n" + tojson(result);
    if (badFields.length !== 0) {
        str += "\nIt had the following fields which it shouldn't have: ";
        str += badFields;
    }
    if (missingFields.length !== 0) {
        str += "\nIt lacked the following fields which it should have contained: ";
        str += missingFields;
    }
    if (badValues.length !== 0) {
        for (i = 0; i < badValues.length; i += 3) {
            str += "\nIts value for " + badValues[i] + " is " + badValues[i + 1];
            str += " but should be " + badValues[i + 2];
        }
    }
    return str;
};

// This function calls checkResponseFields with the isMaster and hello commands.
var runHelloCmdAndAliases = function(memberInfo) {
    checkResponseFields(memberInfo, "ismaster");
    checkResponseFields(memberInfo, "isMaster");
    checkResponseFields(memberInfo, "hello");
};

// This function runs either the isMaster or hello command, and validates that the response is what
// we expect.
var checkResponseFields = function(memberInfo, cmd) {
    // run the passed in command on the connection
    var result = memberInfo.conn.getDB("admin").runCommand(cmd);
    // If we are running the hello command, we must modify the expected fields. We expect
    // "isWritablePrimary" and "secondaryDelaySecs" instead of "ismaster" and "slaveDelay" in the
    // hello command response.
    if (cmd === "hello") {
        memberInfo.goodValues.isWritablePrimary = memberInfo.goodValues.ismaster;
        delete memberInfo.goodValues.ismaster;
        memberInfo.unwantedFields.push("ismaster");
        memberInfo.unwantedFields =
            memberInfo.unwantedFields.filter(f => f !== "isWritablePrimary");

        if (memberInfo.goodValues.hasOwnProperty("slaveDelay")) {
            memberInfo.goodValues.secondaryDelaySecs = memberInfo.goodValues.slaveDelay;
            delete memberInfo.goodValues.slaveDelay;
            memberInfo.unwantedFields.push("slaveDelay");
            memberInfo.unwantedFields =
                memberInfo.unwantedFields.filter(f => f !== "secondaryDelaySecs");
        }
    }

    // make sure result doesn't contain anything it shouldn't
    var badFields = [];
    for (field in result) {
        if (!result.hasOwnProperty(field)) {
            continue;
        }
        if (Array.contains(memberInfo.unwantedFields, field)) {
            badFields.push(field);
        }
    }

    // make sure result contains the fields we want
    var missingFields = [];
    for (i = 0; i < memberInfo.wantedFields.length; i++) {
        field = memberInfo.wantedFields[i];
        if (!result.hasOwnProperty(field)) {
            missingFields.push(field);
            print(field);
        }
    }

    // make sure the result has proper values for fields with known values
    var badValues = [];  // each mistake will be saved as three entries (key, badvalue, goodvalue)
    for (field in memberInfo.goodValues) {
        if (typeof (memberInfo.goodValues[field]) === "object") {
            // assumes nested obj is disk in tags this is currently true, but may change
            if (result[field].disk !== memberInfo.goodValues[field].disk) {
                badValues.push("tags.disk");
                badValues.push(result[field].disk);
                badValues.push(memberInfo.goodValues[field].disk);
            }
        } else {
            if (result[field] !== memberInfo.goodValues[field]) {
                badValues.push(field);
                badValues.push(result[field]);
                badValues.push(memberInfo.goodValues[field]);
            }
        }
    }
    assert(badFields.length === 0 && missingFields.length === 0 && badValues.length === 0,
           memberInfo.name + " had the following problems." +
               generateErrorString(badFields, missingFields, badValues, result));
};

// start of test code
var name = "hello_and_aliases";
var replTest = new ReplSetTest({name: name, nodes: 4});
var nodes = replTest.startSet();
var config = replTest.getReplSetConfig();
config.members[1].priority = 0;
config.members[2].priority = 0;
config.members[2].hidden = true;
config.members[2].secondaryDelaySecs = 3;
config.members[2].buildIndexes = false;
config.members[3].arbiterOnly = true;
replTest.initiate(config);

var agreeOnPrimaryAndSetVersion = function(setVersion) {
    print("Waiting for primary and replica set version " + setVersion);

    var nodes = replTest.nodes;
    var currPrimary = undefined;
    var lastSetVersion = setVersion;
    for (var i = 0; i < nodes.length; i++) {
        try {
            var helloResult = nodes[i].getDB("admin").runCommand({hello: 1});
        } catch (e) {
            // handle reconnect errors due to step downs
            print("Error while calling hello on " + nodes[i] + ": " + e);
            return false;
        }

        printjson(helloResult);
        if (!currPrimary)
            currPrimary = helloResult.primary;
        if (!lastSetVersion)
            lastSetVersion = helloResult.setVersion;
        if (helloResult.primary != currPrimary || !currPrimary)
            return false;
        if (helloResult.setVersion != lastSetVersion)
            return false;
    }

    return true;
};

var primary = replTest.getPrimary();
var secondaries = replTest.getSecondaries();
var expectedVersion = replTest.getReplSetConfigFromNode().version;
assert.soon(function() {
    return agreeOnPrimaryAndSetVersion(expectedVersion);
}, "Nodes did not initiate in less than a minute", 60000);

// Check to see if the information from hello() and its aliases are correct at each node.
// The checker only checks that the field exists when its value is "has".
runHelloCmdAndAliases({
    conn: primary,
    name: "primary",
    goodValues: {
        setName: "hello_and_aliases",
        setVersion: expectedVersion,
        ismaster: true,
        secondary: false,
        ok: 1
    },
    wantedFields:
        ["hosts", "passives", "arbiters", "primary", "me", "maxBsonObjectSize", "localTime"],
    unwantedFields: [
        "isWritablePrimary",
        "arbiterOnly",
        "passive",
        "slaveDelay",
        "secondaryDelaySecs",
        "hidden",
        "tags",
        "buildIndexes"
    ]
});

runHelloCmdAndAliases({
    conn: secondaries[0],
    name: "secondary",
    goodValues: {
        setName: "hello_and_aliases",
        setVersion: expectedVersion,
        ismaster: false,
        secondary: true,
        passive: true,
        ok: 1
    },
    wantedFields:
        ["hosts", "passives", "arbiters", "primary", "me", "maxBsonObjectSize", "localTime"],
    unwantedFields: [
        "isWritablePrimary",
        "arbiterOnly",
        "slaveDelay",
        "secondaryDelaySecs",
        "hidden",
        "tags",
        "buildIndexes"
    ]
});

runHelloCmdAndAliases({
    conn: secondaries[1],
    name: "delayed_secondary",
    goodValues: {
        setName: "hello_and_aliases",
        setVersion: expectedVersion,
        ismaster: false,
        secondary: true,
        passive: true,
        slaveDelay: 3,
        buildIndexes: false,
        ok: 1
    },
    wantedFields:
        ["hosts", "passives", "arbiters", "primary", "me", "maxBsonObjectSize", "localTime"],
    unwantedFields: ["isWritablePrimary", "arbiterOnly", "tags", "secondaryDelaySecs"]
});

runHelloCmdAndAliases({
    conn: secondaries[2],
    name: "arbiter",
    goodValues: {
        setName: "hello_and_aliases",
        setVersion: expectedVersion,
        ismaster: false,
        secondary: false,
        arbiterOnly: true,
        ok: 1
    },
    wantedFields:
        ["hosts", "passives", "arbiters", "primary", "me", "maxBsonObjectSize", "localTime"],
    unwantedFields: [
        "isWritablePrimary",
        "slaveDelay",
        "secondaryDelaySecs",
        "hidden",
        "tags",
        "buildIndexes",
        "passive"
    ]
});

// Reconfigure the replset and make sure the changes are present on all members.
config = primary.getDB("local").system.replset.findOne();
config.version = config.version + 1;
config.members[0].tags = {
    disk: "ssd"
};
config.members[1].tags = {
    disk: "ssd"
};
config.members[1].hidden = true;
config.members[2].secondaryDelaySecs = 300000;
config.members[2].tags = {
    disk: "hdd"
};
try {
    result = primary.getDB("admin").runCommand({replSetReconfig: config});
} catch (e) {
    print(e);
}

primary = replTest.getPrimary();
secondaries = replTest.getSecondaries();
expectedVersion = config.version;
assert.soon(function() {
    return agreeOnPrimaryAndSetVersion(expectedVersion);
}, "Nodes did not sync in less than a minute", 60000);

// check nodes for their new settings
runHelloCmdAndAliases({
    conn: primary,
    name: "primary2",
    goodValues: {
        setName: "hello_and_aliases",
        setVersion: expectedVersion,
        ismaster: true,
        secondary: false,
        tags: {"disk": "ssd"},
        ok: 1
    },
    wantedFields: ["hosts", "arbiters", "primary", "me", "maxBsonObjectSize", "localTime"],
    unwantedFields: [
        "isWritablePrimary",
        "arbiterOnly",
        "passives",
        "passive",
        "slaveDelay",
        "secondaryDelaySecs",
        "hidden",
        "buildIndexes"
    ]
});

runHelloCmdAndAliases({
    conn: secondaries[0],
    name: "first_secondary",
    goodValues: {
        setName: "hello_and_aliases",
        setVersion: expectedVersion,
        ismaster: false,
        secondary: true,
        tags: {"disk": "ssd"},
        passive: true,
        hidden: true,
        ok: 1
    },
    wantedFields: ["hosts", "arbiters", "primary", "me", "maxBsonObjectSize", "localTime"],
    unwantedFields: [
        "isWritablePrimary",
        "arbiterOnly",
        "passives",
        "slaveDelay",
        "secondaryDelaySecs",
        "buildIndexes"
    ]
});

runHelloCmdAndAliases({
    conn: secondaries[1],
    name: "very_delayed_secondary",
    goodValues: {
        setName: "hello_and_aliases",
        setVersion: expectedVersion,
        ismaster: false,
        secondary: true,
        tags: {"disk": "hdd"},
        passive: true,
        slaveDelay: 300000,
        buildIndexes: false,
        hidden: true,
        ok: 1
    },
    wantedFields: ["hosts", "arbiters", "primary", "me", "maxBsonObjectSize", "localTime"],
    unwantedFields: ["isWritablePrimary", "arbiterOnly", "passives", "secondaryDelaySecs"]
});

runHelloCmdAndAliases({
    conn: secondaries[2],
    name: "arbiter",
    goodValues: {
        setName: "hello_and_aliases",
        setVersion: expectedVersion,
        ismaster: false,
        secondary: false,
        arbiterOnly: true,
        ok: 1
    },
    wantedFields: ["hosts", "arbiters", "primary", "me", "maxBsonObjectSize", "localTime"],
    unwantedFields: [
        "isWritablePrimary",
        "slaveDelay",
        "secondaryDelaySecs",
        "hidden",
        "tags",
        "buildIndexes",
        "passive"
    ]
});

// force reconfig and ensure all have the same setVersion afterwards
config = primary.getDB("local").system.replset.findOne();
primary.getDB("admin").runCommand({replSetReconfig: config, force: true});

assert.soon(function() {
    return agreeOnPrimaryAndSetVersion();
}, "Nodes did not sync in less than a minute after forced reconfig", 60000);

replTest.stopSet();