summaryrefslogtreecommitdiff
path: root/jstests/replsets/ismaster1.js
blob: 8acefc875d5c232c5191a8434e088a8ca193b456 (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
/**
 * check all the easily testable fields in the document returned by the isMaster() command
 * also checks that fields that should not be in the document are absent
 */

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;
};

// function to check a single result
var checkMember = function (memberInfo) {
    // run isMaster on the connection
    result = memberInfo.conn.getDB("admin").runCommand({isMaster:1});

    // 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 = "ismaster";
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].slaveDelay = 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 primary = undefined;
    var lastSetVersion = setVersion; 
    for ( var i = 0; i < nodes.length; i++ ) {
        try {
            var isMasterResult = nodes[i].getDB( "admin" ).runCommand({ isMaster : 1 });
        }
        catch (e) {
            // handle reconnect errors due to step downs
            print("Error while calling isMaster on " + nodes[i] + ": " + e);
            return false;
        }

        printjson( isMasterResult );
        if ( !primary ) primary = isMasterResult.primary;
        if ( !lastSetVersion ) lastSetVersion = isMasterResult.setVersion;
        if ( isMasterResult.primary != primary || !primary ) return false;
        if ( isMasterResult.setVersion != lastSetVersion ) return false;
    }
    
    return true;
};

var master = replTest.getPrimary();
assert.soon( function() { return agreeOnPrimaryAndSetVersion( 1 ); },
             "Nodes did not initiate in less than a minute", 60000 );

// check to see if the information from isMaster() is correct at each node
// the checker only checks that the field exists when its value is "has"
checkMember({ conn: master,
              name: "master",
              goodValues: {
                  setName: "ismaster",
                  setVersion: 1,
                  ismaster: true,
                  secondary: false,
                  ok: 1
              },
              wantedFields: ["hosts", "passives", "arbiters", "primary", "me", "maxBsonObjectSize",
                             "localTime"],
              unwantedFields: ["arbiterOnly", "passive", "slaveDelay", "hidden", "tags",
                               "buildIndexes"]
            });

checkMember({ conn: replTest.liveNodes.slaves[0],
              name: "slave",
              goodValues: {
                  setName: "ismaster",
                  setVersion: 1,
                  ismaster: false,
                  secondary: true,
                  passive: true,
                  ok: 1
              },
              wantedFields: ["hosts", "passives", "arbiters", "primary", "me", "maxBsonObjectSize",
                             "localTime"],
              unwantedFields: ["arbiterOnly", "slaveDelay", "hidden", "tags", "buildIndexes"]
            });

checkMember({ conn: replTest.liveNodes.slaves[1],
              name: "delayed_slave",
              goodValues: {
                  setName: "ismaster",
                  setVersion: 1,
                  ismaster: false,
                  secondary: true,
                  passive: true,
                  slaveDelay: 3,
                  buildIndexes: false,
                  ok: 1
              },
              wantedFields: ["hosts", "passives", "arbiters", "primary", "me", "maxBsonObjectSize",
                             "localTime"],
              unwantedFields: ["arbiterOnly", "tags"]
            });

checkMember({ conn: replTest.liveNodes.slaves[2],
              name: "arbiter",
              goodValues: {
                  setName: "ismaster",
                  setVersion: 1,
                  ismaster: false,
                  secondary: false,
                  arbiterOnly: true,
                  ok: 1
              },
              wantedFields: ["hosts", "passives", "arbiters", "primary", "me", "maxBsonObjectSize",
                             "localTime"],
              unwantedFields: ["slaveDelay", "hidden", "tags", "buildIndexes", "passive"]
            });

// reconfigure and make sure the changes show up in ismaster on all members
config = master.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].slaveDelay = 300000;
config.members[2].tags = {disk: "hdd"};
try {
    result = master.getDB("admin").runCommand({replSetReconfig : config});
}
catch(e) {
    print(e);
}

master = replTest.getPrimary();
assert.soon( function() { return agreeOnPrimaryAndSetVersion( 2 ); },
             "Nodes did not sync in less than a minute", 60000 );

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

checkMember({ conn: replTest.liveNodes.slaves[0],
              name: "first_slave",
              goodValues: {
                  setName: "ismaster",
                  setVersion: 2,
                  ismaster: false,
                  secondary: true,
                  tags: {"disk": "ssd"},
                  passive: true,
                  hidden: true,
                  ok: 1
              },
              wantedFields: ["hosts", "arbiters", "primary", "me", "maxBsonObjectSize",
                             "localTime"],
              unwantedFields: ["arbiterOnly", "passives", "slaveDelayed", "buildIndexes"]
            });

checkMember({ conn: replTest.liveNodes.slaves[1],
              name: "very_delayed_slave",
              goodValues: {
                  setName: "ismaster",
                  setVersion: 2,
                  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: ["arbiterOnly", "passives"]
            });

checkMember({ conn: replTest.liveNodes.slaves[2],
              name: "arbiter",
              goodValues: {
                  setName: "ismaster",
                  setVersion: 2,
                  ismaster: false,
                  secondary: false,
                  arbiterOnly: true,
                  ok: 1
              },
              wantedFields: ["hosts", "arbiters", "primary", "me", "maxBsonObjectSize",
                             "localTime"],
              unwantedFields: ["slaveDelay", "hidden", "tags", "buildIndexes", "passive"]
            });

// force reconfig and ensure all have the same setVersion afterwards
config = master.getDB("local").system.replset.findOne();
master.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();