summaryrefslogtreecommitdiff
path: root/src/mongo/db/repl/check_quorum_for_config_change_test.cpp
blob: 4a1396fb7801cbad99ace4cca6decb708020876d (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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
/**
 *    Copyright (C) 2014 MongoDB Inc.
 *
 *    This program is free software: you can redistribute it and/or  modify
 *    it under the terms of the GNU Affero General Public License, version 3,
 *    as published by the Free Software Foundation.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the GNU Affero General Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */

#include "mongo/platform/basic.h"

#include <boost/thread.hpp>
#include <boost/scoped_ptr.hpp>

#include "mongo/base/status.h"
#include "mongo/db/jsobj.h"
#include "mongo/db/repl/check_quorum_for_config_change.h"
#include "mongo/db/repl/network_interface_mock.h"
#include "mongo/db/repl/replica_set_config.h"
#include "mongo/db/repl/replication_executor.h"
#include "mongo/stdx/functional.h"
#include "mongo/unittest/unittest.h"

#define ASSERT_REASON_CONTAINS(STATUS, PATTERN) do {                    \
        const mongo::Status s_ = (STATUS);                              \
        ASSERT_FALSE(s_.reason().find(PATTERN) == std::string::npos) << \
            #STATUS ".reason() == " << s_.reason();                     \
    } while (false)

#define ASSERT_NOT_REASON_CONTAINS(STATUS, PATTERN) do {                \
        const mongo::Status s_ = (STATUS);                              \
        ASSERT_TRUE(s_.reason().find(PATTERN) == std::string::npos) <<  \
            #STATUS ".reason() == " << s_.reason();                     \
    } while (false)

namespace mongo {
namespace repl {
namespace {

    typedef ReplicationExecutor::RemoteCommandRequest RemoteCommandRequest;

    class CheckQuorumTest : public mongo::unittest::Test {
    protected:
        NetworkInterfaceMock* _net;
        boost::scoped_ptr<ReplicationExecutor> _executor;
        boost::scoped_ptr<boost::thread> _executorThread;

    private:
        void setUp();
        void tearDown();
    };

    class CheckQuorumForInitiate : public CheckQuorumTest {};
    class CheckQuorumForReconfig : public CheckQuorumTest {};

    void CheckQuorumTest::setUp() {
        _net = new NetworkInterfaceMock;
        _executor.reset(new ReplicationExecutor(_net));
        _executorThread.reset(new boost::thread(stdx::bind(&ReplicationExecutor::run,
                                                           _executor.get())));
    }

    void CheckQuorumTest::tearDown() {
        _net->unblockAll();
        _executor->shutdown();
        _executorThread->join();
    }

    ReplicaSetConfig assertMakeRSConfig(const BSONObj& configBson) {
        ReplicaSetConfig config;
        ASSERT_OK(config.initialize(configBson));
        ASSERT_OK(config.validate());
        return config;
    }

    TEST_F(CheckQuorumForInitiate, ValidSingleNodeSet) {
        ReplicaSetConfig config = assertMakeRSConfig(
                BSON("_id" << "rs0" <<
                     "version" << 1 <<
                     "members" << BSON_ARRAY(
                             BSON("_id" << 1 << "host" << "h1"))));
        ASSERT_OK(checkQuorumForInitiate(_executor.get(), config, 0));
    }

    TEST_F(CheckQuorumForInitiate, QuorumCheckCanceledByShutdown) {
        _executor->shutdown();
        ReplicaSetConfig config = assertMakeRSConfig(
                BSON("_id" << "rs0" <<
                     "version" << 1 <<
                     "members" << BSON_ARRAY(
                             BSON("_id" << 1 << "host" << "h1"))));
        ASSERT_EQUALS(ErrorCodes::ShutdownInProgress,
                      checkQuorumForInitiate(_executor.get(), config, 0));
    }

    TEST_F(CheckQuorumForInitiate, QuorumCheckFailedDueToSeveralDownNodes) {
        // In this test, "we" are host "h3:1".  All other nodes time out on
        // their heartbeat request, and so the quorum check for initiate
        // will fail because some members were unavailable.
        ReplicaSetConfig config = assertMakeRSConfig(
                BSON("_id" << "rs0" <<
                     "version" << 1 <<
                     "members" << BSON_ARRAY(
                             BSON("_id" << 1 << "host" << "h1:1") <<
                             BSON("_id" << 2 << "host" << "h2:1") <<
                             BSON("_id" << 3 << "host" << "h3:1") <<
                             BSON("_id" << 4 << "host" << "h4:1") <<
                             BSON("_id" << 5 << "host" << "h5:1"))));
        Status status = checkQuorumForInitiate(_executor.get(), config, 2);
        ASSERT_EQUALS(ErrorCodes::NodeNotFound, status);
        ASSERT_REASON_CONTAINS(
                status, "Could not contact the following nodes during replica set initiation");
        ASSERT_REASON_CONTAINS(status, "h1:1");
        ASSERT_REASON_CONTAINS(status, "h2:1");
        ASSERT_NOT_REASON_CONTAINS(status, "h3:1");
        ASSERT_REASON_CONTAINS(status, "h4:1");
        ASSERT_REASON_CONTAINS(status, "h5:1");
    }

    const BSONObj makeHeartbeatRequest(const ReplicaSetConfig& rsConfig, int myConfigIndex) {
        const MemberConfig& myConfig = rsConfig.getMemberAt(myConfigIndex);
        return BSON(
                "replSetHeartbeat" << rsConfig.getReplSetName() <<
                "v" << rsConfig.getConfigVersion() <<
                "pv" << 1 <<
                "checkEmpty" << (rsConfig.getConfigVersion() == 1) <<
                "from" << myConfig.getHostAndPort().toString() <<
                "fromId" << myConfig.getId());
    }

    TEST_F(CheckQuorumForInitiate, QuorumCheckSuccessForFiveNodes) {
        // In this test, "we" are host "h3:1".  All nodes respond successfully to their heartbeat
        // requests, and the quorum check succeeds.

        const ReplicaSetConfig rsConfig = assertMakeRSConfig(
                BSON("_id" << "rs0" <<
                     "version" << 1 <<
                     "members" << BSON_ARRAY(
                             BSON("_id" << 1 << "host" << "h1:1") <<
                             BSON("_id" << 2 << "host" << "h2:1") <<
                             BSON("_id" << 3 << "host" << "h3:1") <<
                             BSON("_id" << 4 << "host" << "h4:1") <<
                             BSON("_id" << 5 << "host" << "h5:1"))));
        const int myConfigIndex = 2;
        const BSONObj hbRequest = makeHeartbeatRequest(rsConfig, myConfigIndex);

        _net->addResponse(RemoteCommandRequest(HostAndPort("h1", 1),
                                               "admin",
                                               hbRequest),
                          StatusWith<BSONObj>(BSON("ok" << 1)));
        _net->addResponse(RemoteCommandRequest(HostAndPort("h2", 1),
                                               "admin",
                                               hbRequest),
                          StatusWith<BSONObj>(BSON("ok" << 1)));
        _net->addResponse(RemoteCommandRequest(HostAndPort("h4", 1),
                                               "admin",
                                               hbRequest),
                          StatusWith<BSONObj>(BSON("ok" << 1)));
        _net->addResponse(RemoteCommandRequest(HostAndPort("h5", 1),
                                               "admin",
                                               hbRequest),
                          StatusWith<BSONObj>(BSON("ok" << 1)));

        ASSERT_OK(checkQuorumForInitiate(_executor.get(), rsConfig, myConfigIndex));
    }

    TEST_F(CheckQuorumForInitiate, QuorumCheckFailedDueToOneDownNode) {
        // In this test, "we" are host "h3:1".  All nodes except "h2:1" respond
        // successfully to their heartbeat requests, but quorum check fails because
        // all nodes must be available for initiate.  This is so even though "h2"
        // is neither voting nor electable.

        const ReplicaSetConfig rsConfig = assertMakeRSConfig(
                BSON("_id" << "rs0" <<
                     "version" << 1 <<
                     "members" << BSON_ARRAY(
                             BSON("_id" << 1 << "host" << "h1:1") <<
                             BSON("_id" << 2 << "host" << "h2:1" <<
                                  "priority" << 0 << "votes" << 0) <<
                             BSON("_id" << 3 << "host" << "h3:1") <<
                             BSON("_id" << 4 << "host" << "h4:1") <<
                             BSON("_id" << 5 << "host" << "h5:1") <<
                             BSON("_id" << 6 << "host" << "h6:1"))));
        const int myConfigIndex = 2;
        const BSONObj hbRequest = makeHeartbeatRequest(rsConfig, myConfigIndex);

        _net->addResponse(RemoteCommandRequest(HostAndPort("h1", 1),
                                               "admin",
                                               hbRequest),
                          StatusWith<BSONObj>(BSON("ok" << 1)));
        _net->addResponse(RemoteCommandRequest(HostAndPort("h4", 1),
                                               "admin",
                                               hbRequest),
                          StatusWith<BSONObj>(BSON("ok" << 1)));
        _net->addResponse(RemoteCommandRequest(HostAndPort("h5", 1),
                                               "admin",
                                               hbRequest),
                          StatusWith<BSONObj>(BSON("ok" << 1)));
        _net->addResponse(RemoteCommandRequest(HostAndPort("h6", 1),
                                               "admin",
                                               hbRequest),
                          StatusWith<BSONObj>(BSON("ok" << 1)));

        Status status = checkQuorumForInitiate(_executor.get(), rsConfig, myConfigIndex);
        ASSERT_EQUALS(ErrorCodes::NodeNotFound, status);
        ASSERT_REASON_CONTAINS(
                status, "Could not contact the following nodes during replica set initiation");
        ASSERT_NOT_REASON_CONTAINS(status, "h1:1");
        ASSERT_REASON_CONTAINS(status, "h2:1");
        ASSERT_NOT_REASON_CONTAINS(status, "h3:1");
        ASSERT_NOT_REASON_CONTAINS(status, "h4:1");
        ASSERT_NOT_REASON_CONTAINS(status, "h5:1");
        ASSERT_NOT_REASON_CONTAINS(status, "h6:1");
    }

    TEST_F(CheckQuorumForInitiate, QuorumCheckFailedDueToSetNameMismatch) {
        // In this test, "we" are host "h3:1".  All nodes respond
        // successfully to their heartbeat requests, but quorum check fails because
        // "h4" declares that the requested replica set name was not what it expected.

        const ReplicaSetConfig rsConfig = assertMakeRSConfig(
                BSON("_id" << "rs0" <<
                     "version" << 1 <<
                     "members" << BSON_ARRAY(
                             BSON("_id" << 1 << "host" << "h1:1") <<
                             BSON("_id" << 2 << "host" << "h2:1") <<
                             BSON("_id" << 3 << "host" << "h3:1") <<
                             BSON("_id" << 4 << "host" << "h4:1") <<
                             BSON("_id" << 5 << "host" << "h5:1"))));
        const int myConfigIndex = 2;
        const BSONObj hbRequest = makeHeartbeatRequest(rsConfig, myConfigIndex);

        _net->addResponse(RemoteCommandRequest(HostAndPort("h1", 1),
                                               "admin",
                                               hbRequest),
                          StatusWith<BSONObj>(BSON("ok" << 1)));
        _net->addResponse(RemoteCommandRequest(HostAndPort("h2", 1),
                                               "admin",
                                               hbRequest),
                          StatusWith<BSONObj>(BSON("ok" << 1)));
        _net->addResponse(RemoteCommandRequest(HostAndPort("h4", 1),
                                               "admin",
                                               hbRequest),
                          StatusWith<BSONObj>(BSON("ok" << 0 << "mismatch" << true)));
        _net->addResponse(RemoteCommandRequest(HostAndPort("h5", 1),
                                               "admin",
                                               hbRequest),
                          StatusWith<BSONObj>(BSON("ok" << 1)));

        Status status = checkQuorumForInitiate(_executor.get(), rsConfig, myConfigIndex);
        ASSERT_EQUALS(ErrorCodes::NewReplicaSetConfigurationIncompatible, status);
        ASSERT_REASON_CONTAINS(
                status, "Our set name did not match");
        ASSERT_NOT_REASON_CONTAINS(status, "h1:1");
        ASSERT_NOT_REASON_CONTAINS(status, "h2:1");
        ASSERT_NOT_REASON_CONTAINS(status, "h3:1");
        ASSERT_REASON_CONTAINS(status, "h4:1");
        ASSERT_NOT_REASON_CONTAINS(status, "h5:1");
    }

    TEST_F(CheckQuorumForInitiate, QuorumCheckFailedDueToInitializedNode) {
        // In this test, "we" are host "h3:1".  All nodes respond
        // successfully to their heartbeat requests, but quorum check fails because
        // "h5" declares that it is already initialized.

        const ReplicaSetConfig rsConfig = assertMakeRSConfig(
                BSON("_id" << "rs0" <<
                     "version" << 1 <<
                     "members" << BSON_ARRAY(
                             BSON("_id" << 1 << "host" << "h1:1") <<
                             BSON("_id" << 2 << "host" << "h2:1") <<
                             BSON("_id" << 3 << "host" << "h3:1") <<
                             BSON("_id" << 4 << "host" << "h4:1") <<
                             BSON("_id" << 5 << "host" << "h5:1"))));
        const int myConfigIndex = 2;
        const BSONObj hbRequest = makeHeartbeatRequest(rsConfig, myConfigIndex);

        _net->addResponse(RemoteCommandRequest(HostAndPort("h1", 1),
                                               "admin",
                                               hbRequest),
                          StatusWith<BSONObj>(BSON("ok" << 1)));
        _net->addResponse(RemoteCommandRequest(HostAndPort("h2", 1),
                                               "admin",
                                               hbRequest),
                          StatusWith<BSONObj>(BSON("ok" << 1)));
        _net->addResponse(RemoteCommandRequest(HostAndPort("h4", 1),
                                               "admin",
                                               hbRequest),
                          StatusWith<BSONObj>(BSON("ok" << 1)));
        _net->addResponse(RemoteCommandRequest(HostAndPort("h5", 1),
                                               "admin",
                                               hbRequest),
                          StatusWith<BSONObj>(BSON("ok" << 0 << "set" << "rs0" << "v" << 1)));

        Status status = checkQuorumForInitiate(_executor.get(), rsConfig, myConfigIndex);
        ASSERT_EQUALS(ErrorCodes::NewReplicaSetConfigurationIncompatible, status);
        ASSERT_REASON_CONTAINS(
                status, "Our config version of");
        ASSERT_REASON_CONTAINS(
                status, "is no larger than the version");
        ASSERT_NOT_REASON_CONTAINS(status, "h1:1");
        ASSERT_NOT_REASON_CONTAINS(status, "h2:1");
        ASSERT_NOT_REASON_CONTAINS(status, "h3:1");
        ASSERT_NOT_REASON_CONTAINS(status, "h4:1");
        ASSERT_REASON_CONTAINS(status, "h5:1");
    }

    TEST_F(CheckQuorumForInitiate, QuorumCheckFailedDueToInitializedNodeOnlyOneRespondent) {
        // In this test, "we" are host "h3:1".  Only node "h5" responds before the test completes,
        // and quorum check fails because "h5" declares that it is already initialized.
        //
        // Compare to QuorumCheckFailedDueToInitializedNode, above.

        const ReplicaSetConfig rsConfig = assertMakeRSConfig(
                BSON("_id" << "rs0" <<
                     "version" << 1 <<
                     "members" << BSON_ARRAY(
                             BSON("_id" << 1 << "host" << "h1:1") <<
                             BSON("_id" << 2 << "host" << "h2:1") <<
                             BSON("_id" << 3 << "host" << "h3:1") <<
                             BSON("_id" << 4 << "host" << "h4:1") <<
                             BSON("_id" << 5 << "host" << "h5:1"))));
        const int myConfigIndex = 2;
        const BSONObj hbRequest = makeHeartbeatRequest(rsConfig, myConfigIndex);

        // Responses from nodes h1, h2 and h4 and blocked until after the test
        // completes.
        _net->addResponse(RemoteCommandRequest(HostAndPort("h1", 1),
                                               "admin",
                                               hbRequest),
                          StatusWith<BSONObj>(BSON("ok" << 1)),
                          true);
        _net->addResponse(RemoteCommandRequest(HostAndPort("h2", 1),
                                               "admin",
                                               hbRequest),
                          StatusWith<BSONObj>(BSON("ok" << 1)),
                          true);
        _net->addResponse(RemoteCommandRequest(HostAndPort("h4", 1),
                                               "admin",
                                               hbRequest),
                          StatusWith<BSONObj>(BSON("ok" << 1)),
                          true);

        // h5 responds, with a version incompatibility.
        _net->addResponse(RemoteCommandRequest(HostAndPort("h5", 1),
                                               "admin",
                                               hbRequest),
                          StatusWith<BSONObj>(BSON("ok" << 0 << "set" << "rs0" << "v" << 1)));

        Status status = checkQuorumForInitiate(_executor.get(), rsConfig, myConfigIndex);
        ASSERT_EQUALS(ErrorCodes::NewReplicaSetConfigurationIncompatible, status);
        ASSERT_REASON_CONTAINS(
                status, "Our config version of");
        ASSERT_REASON_CONTAINS(
                status, "is no larger than the version");
        ASSERT_NOT_REASON_CONTAINS(status, "h1:1");
        ASSERT_NOT_REASON_CONTAINS(status, "h2:1");
        ASSERT_NOT_REASON_CONTAINS(status, "h3:1");
        ASSERT_NOT_REASON_CONTAINS(status, "h4:1");
        ASSERT_REASON_CONTAINS(status, "h5:1");
    }

    TEST_F(CheckQuorumForReconfig, QuorumCheckVetoedDueToHigherConfigVersion) {
        // In this test, "we" are host "h3:1".  The request to "h2" times out,
        // and the request to "h1" comes back indicating a higher config version.

        const ReplicaSetConfig rsConfig = assertMakeRSConfig(
                BSON("_id" << "rs0" <<
                     "version" << 2 <<
                     "members" << BSON_ARRAY(
                             BSON("_id" << 1 << "host" << "h1:1") <<
                             BSON("_id" << 2 << "host" << "h2:1") <<
                             BSON("_id" << 3 << "host" << "h3:1"))));
        const int myConfigIndex = 2;
        const BSONObj hbRequest = makeHeartbeatRequest(rsConfig, myConfigIndex);

        _net->addResponse(RemoteCommandRequest(HostAndPort("h1", 1),
                                               "admin",
                                               hbRequest),
                          StatusWith<BSONObj>(BSON("ok" << 0 << "set" << "rs0" << "v" << 5)));

        Status status = checkQuorumForReconfig(_executor.get(), rsConfig, myConfigIndex);
        ASSERT_EQUALS(ErrorCodes::NewReplicaSetConfigurationIncompatible, status);
        ASSERT_REASON_CONTAINS(
                status, "Our config version of");
        ASSERT_REASON_CONTAINS(
                status, "is no larger than the version");
        ASSERT_REASON_CONTAINS(status, "h1:1");
        ASSERT_NOT_REASON_CONTAINS(status, "h2:1");
        ASSERT_NOT_REASON_CONTAINS(status, "h3:1");
    }

    TEST_F(CheckQuorumForReconfig, QuorumCheckVetoedDueToIncompatibleSetName) {
        // In this test, "we" are host "h3:1".  The request to "h1" times out,
        // and the request to "h2" comes back indicating an incompatible set name.

        const ReplicaSetConfig rsConfig = assertMakeRSConfig(
                BSON("_id" << "rs0" <<
                     "version" << 2 <<
                     "members" << BSON_ARRAY(
                             BSON("_id" << 1 << "host" << "h1:1") <<
                             BSON("_id" << 2 << "host" << "h2:1") <<
                             BSON("_id" << 3 << "host" << "h3:1"))));
        const int myConfigIndex = 2;
        const BSONObj hbRequest = makeHeartbeatRequest(rsConfig, myConfigIndex);

        _net->addResponse(RemoteCommandRequest(HostAndPort("h2", 1),
                                               "admin",
                                               hbRequest),
                          StatusWith<BSONObj>(BSON("ok" << 0 << "mismatch" << true)));

        Status status = checkQuorumForReconfig(_executor.get(), rsConfig, myConfigIndex);
        ASSERT_EQUALS(ErrorCodes::NewReplicaSetConfigurationIncompatible, status);
        ASSERT_REASON_CONTAINS(status, "Our set name did not match");
        ASSERT_NOT_REASON_CONTAINS(status, "h1:1");
        ASSERT_REASON_CONTAINS(status, "h2:1");
        ASSERT_NOT_REASON_CONTAINS(status, "h3:1");

    }

    TEST_F(CheckQuorumForReconfig, QuorumCheckFailsDueToInsufficientVoters) {
        // In this test, "we" are host "h4".  Only "h1", "h2" and "h3" are voters,
        // and of the voters, only "h1" responds.  As a result, quorum check fails.

        const ReplicaSetConfig rsConfig = assertMakeRSConfig(
                BSON("_id" << "rs0" <<
                     "version" << 2 <<
                     "members" << BSON_ARRAY(
                             BSON("_id" << 1 << "host" << "h1:1") <<
                             BSON("_id" << 2 << "host" << "h2:1") <<
                             BSON("_id" << 3 << "host" << "h3:1") <<
                             BSON("_id" << 4 << "host" << "h4:1" << "votes" << 0) <<
                             BSON("_id" << 5 << "host" << "h5:1" << "votes" << 0))));
        const int myConfigIndex = 3;
        const BSONObj hbRequest = makeHeartbeatRequest(rsConfig, myConfigIndex);

        _net->addResponse(RemoteCommandRequest(HostAndPort("h1", 1),
                                               "admin",
                                               hbRequest),
                          StatusWith<BSONObj>(BSON("ok" << 1)));
        _net->addResponse(RemoteCommandRequest(HostAndPort("h5", 1),
                                               "admin",
                                               hbRequest),
                          StatusWith<BSONObj>(BSON("ok" << 1)));
        Status status = checkQuorumForReconfig(_executor.get(), rsConfig, myConfigIndex);
        ASSERT_EQUALS(ErrorCodes::NodeNotFound, status);
        ASSERT_REASON_CONTAINS(status, "not enough voting nodes responded; required 2 but only");
        ASSERT_REASON_CONTAINS(status, "h1:1");
        ASSERT_NOT_REASON_CONTAINS(status, "h2:1");
        ASSERT_NOT_REASON_CONTAINS(status, "h3:1");
        ASSERT_NOT_REASON_CONTAINS(status, "h4:1");
        ASSERT_NOT_REASON_CONTAINS(status, "h5:1");
    }

    TEST_F(CheckQuorumForReconfig, QuorumCheckFailsDueToNoElectableNodeResponding) {
        // In this test, "we" are host "h4".  Only "h1", "h2" and "h3" are electable,
        // and none of them respond.

        const ReplicaSetConfig rsConfig = assertMakeRSConfig(
                BSON("_id" << "rs0" <<
                     "version" << 2 <<
                     "members" << BSON_ARRAY(
                             BSON("_id" << 1 << "host" << "h1:1") <<
                             BSON("_id" << 2 << "host" << "h2:1") <<
                             BSON("_id" << 3 << "host" << "h3:1") <<
                             BSON("_id" << 4 << "host" << "h4:1" << "priority" << 0) <<
                             BSON("_id" << 5 << "host" << "h5:1" << "priority" << 0))));
        const int myConfigIndex = 3;
        const BSONObj hbRequest = makeHeartbeatRequest(rsConfig, myConfigIndex);

        _net->addResponse(RemoteCommandRequest(HostAndPort("h5", 1),
                                               "admin",
                                               hbRequest),
                          StatusWith<BSONObj>(BSON("ok" << 1)));
        Status status = checkQuorumForReconfig(_executor.get(), rsConfig, myConfigIndex);
        ASSERT_EQUALS(ErrorCodes::NodeNotFound, status);
        ASSERT_REASON_CONTAINS(status, "no electable nodes responded");
    }

    // TODO: Succeed with minimal quorum.
    TEST_F(CheckQuorumForReconfig, QuorumCheckSucceedsWithAsSoonAsPossible) {
        // In this test, "we" are host "h4".  Only "h1", "h2" and "h3" can vote.
        // This test should succeed as soon as h1 and h2 respond, so we block
        // h3 and h5 from responding or timing out until the test completes.

        const ReplicaSetConfig rsConfig = assertMakeRSConfig(
                BSON("_id" << "rs0" <<
                     "version" << 2 <<
                     "members" << BSON_ARRAY(
                             BSON("_id" << 1 << "host" << "h1:1") <<
                             BSON("_id" << 2 << "host" << "h2:1") <<
                             BSON("_id" << 3 << "host" << "h3:1") <<
                             BSON("_id" << 4 << "host" << "h4:1" << "votes" << 0) <<
                             BSON("_id" << 5 << "host" << "h5:1" << "votes" << 0))));
        const int myConfigIndex = 3;
        const BSONObj hbRequest = makeHeartbeatRequest(rsConfig, myConfigIndex);

        _net->addResponse(RemoteCommandRequest(HostAndPort("h1", 1),
                                               "admin",
                                               hbRequest),
                          StatusWith<BSONObj>(BSON("ok" << 1)));
        _net->addResponse(RemoteCommandRequest(HostAndPort("h2", 1),
                                               "admin",
                                               hbRequest),
                          StatusWith<BSONObj>(BSON("ok" << 1)));

        // If this message arrived, the reconfig would be vetoed, but it is delayed
        // until the quorum check completes, and so has no effect.
        _net->addResponse(RemoteCommandRequest(HostAndPort("h3", 1),
                                               "admin",
                                               hbRequest),
                          StatusWith<BSONObj>(BSON("ok" << 0 << "set" << "rs0" << "v" << 3)),
                          true);

        // Ditto RE veto and no effect.
        _net->addResponse(RemoteCommandRequest(HostAndPort("h5", 1),
                                               "admin",
                                               hbRequest),
                          StatusWith<BSONObj>(BSON("ok" << 0 << "set" << "rs0" << "v" << 3)),
                          true);
        ASSERT_OK(checkQuorumForReconfig(_executor.get(), rsConfig, myConfigIndex));
    }

}  // namespace
}  // namespace repl
}  // namespace mongo