2010-07-28 10:25:31 -04:00
|
|
|
// should check that election happens in priority order
|
|
|
|
|
|
2014-10-14 11:23:33 -04:00
|
|
|
(function() {
|
|
|
|
|
"use strict";
|
2016-03-09 12:17:50 -05:00
|
|
|
var replTest = new ReplSetTest({name: 'testSet', nodes: 3});
|
2014-10-14 11:23:33 -04:00
|
|
|
var nodenames = replTest.nodeList();
|
2010-07-28 10:25:31 -04:00
|
|
|
|
2014-10-14 11:23:33 -04:00
|
|
|
var nodes = replTest.startSet();
|
2016-03-09 12:17:50 -05:00
|
|
|
replTest.initiate({
|
|
|
|
|
"_id": "testSet",
|
|
|
|
|
"members": [
|
|
|
|
|
{"_id": 0, "host": nodenames[0], "priority": 1},
|
|
|
|
|
{"_id": 1, "host": nodenames[1], "priority": 2},
|
|
|
|
|
{"_id": 2, "host": nodenames[2], "priority": 3}
|
|
|
|
|
]
|
|
|
|
|
});
|
2010-07-28 10:25:31 -04:00
|
|
|
|
2016-04-11 15:17:51 -04:00
|
|
|
// 2 should be master (give this a while to happen, as other nodes might first be elected)
|
2015-12-10 10:21:51 -05:00
|
|
|
replTest.waitForState(nodes[2], ReplSetTest.State.PRIMARY, 120000);
|
2016-04-11 15:17:51 -04:00
|
|
|
// wait for 1 to not appear to be master (we are about to make it master and need a clean slate
|
|
|
|
|
// here)
|
|
|
|
|
replTest.waitForState(nodes[1], ReplSetTest.State.SECONDARY, 60000);
|
2010-07-28 10:25:31 -04:00
|
|
|
|
2016-04-21 15:16:43 -04:00
|
|
|
// Wait for election oplog entry to be replicated, to ensure 0 will vote for 1 after stopping 2.
|
|
|
|
|
replTest.awaitReplication();
|
|
|
|
|
|
2010-07-28 10:25:31 -04:00
|
|
|
// kill 2, 1 should take over
|
2012-02-09 16:19:15 -05:00
|
|
|
replTest.stop(2);
|
2010-07-28 10:25:31 -04:00
|
|
|
|
2014-10-14 11:23:33 -04:00
|
|
|
// 1 should eventually be master
|
2015-12-10 10:21:51 -05:00
|
|
|
replTest.waitForState(nodes[1], ReplSetTest.State.PRIMARY, 60000);
|
2016-03-09 12:17:50 -05:00
|
|
|
|
2012-02-09 16:19:15 -05:00
|
|
|
// do some writes on 1
|
2015-11-25 11:20:43 -05:00
|
|
|
var master = replTest.getPrimary();
|
2016-03-09 12:17:50 -05:00
|
|
|
for (var i = 0; i < 1000; i++) {
|
2016-03-17 14:41:31 -04:00
|
|
|
assert.writeOK(master.getDB("foo").bar.insert({i: i}, {writeConcern: {w: 'majority'}}));
|
2012-02-09 16:19:15 -05:00
|
|
|
}
|
2010-07-28 10:25:31 -04:00
|
|
|
|
2016-03-09 12:17:50 -05:00
|
|
|
for (i = 0; i < 1000; i++) {
|
2016-03-17 14:41:31 -04:00
|
|
|
assert.writeOK(master.getDB("bar").baz.insert({i: i}, {writeConcern: {w: 'majority'}}));
|
2012-02-09 16:19:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// bring 2 back up, 2 should wait until caught up and then become master
|
|
|
|
|
replTest.restart(2);
|
2015-12-10 10:21:51 -05:00
|
|
|
replTest.waitForState(nodes[2], ReplSetTest.State.PRIMARY, 60000);
|
2012-02-09 16:19:15 -05:00
|
|
|
|
|
|
|
|
// make sure nothing was rolled back
|
2015-11-25 11:20:43 -05:00
|
|
|
master = replTest.getPrimary();
|
2016-03-09 12:17:50 -05:00
|
|
|
for (i = 0; i < 1000; i++) {
|
|
|
|
|
assert(master.getDB("foo").bar.findOne({i: i}) != null, 'checking ' + i);
|
|
|
|
|
assert(master.getDB("bar").baz.findOne({i: i}) != null, 'checking ' + i);
|
2012-02-09 16:19:15 -05:00
|
|
|
}
|
2014-10-27 13:31:30 -04:00
|
|
|
}());
|