Permalink
Browse files

Check optimes before stepping down due to priority SERVER-4810

  • Loading branch information...
kchodorow authored and amschwerin committed Feb 9, 2012
1 parent bea6d4b commit 49144209b1c537692945594d909713a2fc669db0
Showing with 45 additions and 35 deletions.
  1. +4 −1 db/repl/manager.cpp
  2. +41 −34 jstests/replsets/replsetprio1.js
View
@@ -98,7 +98,10 @@ namespace mongo {
const Member *primary = rs->box.getPrimary();
if (primary && highestPriority &&
- highestPriority->config().priority > primary->config().priority) {
+ highestPriority->config().priority > primary->config().priority &&
+ // if we're stepping down to allow another member to become primary, we
+ // better have another member (otherOp), and it should be up-to-date
+ otherOp != 0 && highestPriority->hbinfo().opTime.getSecs() >= otherOp - 10) {
log() << "stepping down " << primary->fullName() << endl;
if (primary->h().isSelf()) {
@@ -1,4 +1,3 @@
-// FAILING TEST
// should check that election happens in priority order
doTest = function( signal ) {
@@ -7,47 +6,55 @@ doTest = function( signal ) {
var nodes = replTest.nodeList();
replTest.startSet();
- replTest.node[0].initiate({"_id" : "unicomplex",
+ replTest.initiate({"_id" : "testSet",
"members" : [
- {"_id" : 0, "host" : nodes[0], "priority" : 1},
- {"_id" : 1, "host" : nodes[1], "priority" : 2},
+ {"_id" : 0, "host" : nodes[0], "priority" : 1},
+ {"_id" : 1, "host" : nodes[1], "priority" : 2},
{"_id" : 2, "host" : nodes[2], "priority" : 3}]});
- sleep(10000);
-
- // 2 should be master
- var m3 = replTest.nodes[2].runCommand({ismaster:1})
-
- // FAILS: node[0] is elected master, regardless of priority
- assert(m3.ismaster, 'highest priority is master');
+ // 2 should be master (give this a while to happen, as 0 will be elected, then demoted)
+ assert.soon(function() {
+ var m2 = replTest.nodes[2].getDB("admin").runCommand({ismaster:1});
+ return m2.ismaster;
+ }, 'highest priority is master', 120000);
// kill 2, 1 should take over
- var m3Id = replTest.getNodeId(nodes[2]);
- replTest.stop(m3Id);
-
- sleep(10000);
-
- var m2 = replTest.nodes[1].runCommand({ismaster:1})
- assert(m2.ismaster, 'node 2 is master');
+ replTest.stop(2);
- // bring 2 back up, nothing should happen
- replTest.start(m3Id);
+ // do some writes on 1
+ master = replTest.getMaster();
+ for (i=0; i<1000; i++) {
+ master.getDB("foo").bar.insert({i:i});
+ }
sleep(10000);
- m2 = replTest.nodes[1].runCommand({ismaster:1})
- assert(m2.ismaster, 'node 2 is still master');
-
- // kill 1, 2 should become master
- var m2Id = replTest.getNodeId(nodes[1]);
- replTest.stop(m2Id);
-
- sleep(10000);
-
- m3 = replTest.nodes[2].runCommand({ismaster:1})
- assert(m3.ismaster, 'node 3 is master');
-
- replTest.stopSet( signal );
+ for (i=0; i<1000; i++) {
+ master.getDB("bar").baz.insert({i:i});
+ }
+
+ var m1 = replTest.nodes[1].getDB("admin").runCommand({ismaster:1})
+ assert(m1.ismaster, 'node 2 is master');
+
+ // bring 2 back up, 2 should wait until caught up and then become master
+ replTest.restart(2);
+ assert.soon(function() {
+ try {
+ m2 = replTest.nodes[2].getDB("admin").runCommand({ismaster:1})
+ return m2.ismaster;
+ }
+ catch (e) {
+ print(e);
+ }
+ return false;
+ }, 'node 2 is master again');
+
+ // make sure nothing was rolled back
+ master = replTest.getMaster();
+ 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);
+ }
}
-//doTest( 15 );
+doTest( 15 );

0 comments on commit 4914420

Please sign in to comment.