Skip to content

Commit

Permalink
SERVER-27810 Guarantee that all nodes agree node 0 is primary after R…
Browse files Browse the repository at this point in the history
…eplSetTest.initiate()
  • Loading branch information
judahschvimer committed Feb 23, 2017
1 parent 4bd3616 commit 3823a20
Showing 1 changed file with 42 additions and 3 deletions.
45 changes: 42 additions & 3 deletions src/mongo/shell/replsettest.js
Expand Up @@ -697,12 +697,11 @@ var ReplSetTest = function(opts) {
this._updateConfigIfNotDurable(config);
};

this.initiate = function(cfg, initCmd, timeout) {
this.initiate = function(cfg, initCmd) {
var master = this.nodes[0].getDB("admin");
var config = cfg || this.getReplSetConfig();
var cmd = {};
var cmdKey = initCmd || 'replSetInitiate';
timeout = timeout || self.kDefaultTimeoutMS;

// Throw an exception if nodes[0] is unelectable in the given config.
if (!_isElectable(config.members[0])) {
Expand Down Expand Up @@ -758,7 +757,7 @@ var ReplSetTest = function(opts) {
return false;
}, "replSetReconfig during initiate failed", 3, 5 * 1000);

this.awaitSecondaryNodes(timeout);
this.stepUp(this.nodes[0]);
}

// Setup authentication if running test with authentication
Expand All @@ -768,6 +767,46 @@ var ReplSetTest = function(opts) {
}
};

this.stepUp = function(node) {
this.awaitSecondaryNodes();
this.awaitNodesAgreeOnPrimary();
if (this.getPrimary() === node) {
return;
}

if (this.protocolVersion === 0 || jsTestOptions().useLegacyReplicationProtocol) {
// Ensure the specified node is primary.
for (let i = 0; i < this.nodes.length; i++) {
let primary = this.getPrimary();
if (primary === node) {
break;
}
try {
// Make sure the nodes do not step back up for 10 minutes.
assert.commandWorked(
primary.adminCommand({replSetStepDown: 10 * 60, force: true}));
} catch (ex) {
print("Caught exception while stepping down node '" + tojson(node.host) +
"': " + tojson(ex));
}
this.awaitNodesAgreeOnPrimary();
}

// Reset the rest of the nodes so they can run for election during the test.
for (let i = 0; i < this.nodes.length; i++) {
// Cannot call replSetFreeze on the primary.
if (this.nodes[i] === node) {
continue;
}
assert.commandWorked(this.nodes[i].adminCommand({replSetFreeze: 0}));
}
} else {
assert.commandWorked(node.adminCommand({replSetStepUp: 1}));
this.awaitNodesAgreeOnPrimary();
}
assert.eq(this.getPrimary(), node, node.host + " was not primary after stepUp");
};

/**
* Gets the current replica set config from the specified node index. If no nodeId is specified,
* uses the primary node.
Expand Down

0 comments on commit 3823a20

Please sign in to comment.