2010-11-17 18:44:32 -05:00
|
|
|
/* test removing a node from a replica set
|
|
|
|
|
*
|
2012-04-09 15:00:12 -04:00
|
|
|
* Start set with two nodes
|
2010-11-17 18:44:32 -05:00
|
|
|
* Initial sync
|
2012-04-09 15:00:12 -04:00
|
|
|
* Remove secondary
|
|
|
|
|
* Bring secondary back up
|
|
|
|
|
* Add it back as secondary
|
|
|
|
|
* Make sure both nodes are either primary or secondary
|
2010-11-17 18:44:32 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
load("jstests/replsets/rslib.js");
|
|
|
|
|
var name = "removeNodes";
|
|
|
|
|
var host = getHostName();
|
|
|
|
|
|
2012-04-09 15:00:12 -04:00
|
|
|
print("Start set with two nodes");
|
2011-05-31 10:33:33 -04:00
|
|
|
var replTest = new ReplSetTest( {name: name, nodes: 2} );
|
2010-11-17 18:44:32 -05:00
|
|
|
var nodes = replTest.startSet();
|
|
|
|
|
replTest.initiate();
|
2015-11-25 11:20:43 -05:00
|
|
|
var master = replTest.getPrimary();
|
2015-09-28 05:47:57 -04:00
|
|
|
var secondary = replTest.getSecondary();
|
2010-11-17 18:44:32 -05:00
|
|
|
|
|
|
|
|
print("Initial sync");
|
|
|
|
|
master.getDB("foo").bar.baz.insert({x:1});
|
|
|
|
|
|
|
|
|
|
replTest.awaitReplication();
|
|
|
|
|
|
2012-04-09 15:00:12 -04:00
|
|
|
print("Remove secondary");
|
2010-11-17 18:44:32 -05:00
|
|
|
var config = replTest.getReplSetConfig();
|
2015-09-28 05:47:57 -04:00
|
|
|
for (var i = 0; i < config.members.length; i++) {
|
|
|
|
|
if (config.members[i].host == secondary.host) {
|
|
|
|
|
config.members.splice(i, 1);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
};
|
2010-11-17 18:44:32 -05:00
|
|
|
config.version = 2;
|
2011-12-20 12:05:38 -05:00
|
|
|
|
2015-09-28 05:47:57 -04:00
|
|
|
assert.eq(secondary.getDB("admin").runCommand({ping:1}).ok,
|
|
|
|
|
1,
|
|
|
|
|
"we should be connected to the secondary");
|
2011-12-20 12:05:38 -05:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
master.getDB("admin").runCommand({replSetReconfig:config});
|
|
|
|
|
}
|
|
|
|
|
catch(e) {
|
|
|
|
|
print(e);
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-28 05:47:57 -04:00
|
|
|
// This tests that the secondary disconnects us when it picks up the new config.
|
2013-02-22 15:48:24 -05:00
|
|
|
assert.soon(
|
|
|
|
|
function() {
|
|
|
|
|
try {
|
2015-09-28 05:47:57 -04:00
|
|
|
secondary.getDB("admin").runCommand({ping:1});
|
2013-02-22 15:48:24 -05:00
|
|
|
} catch (e) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2015-09-28 05:47:57 -04:00
|
|
|
// Now we should successfully reconnect to the secondary.
|
|
|
|
|
assert.eq(secondary.getDB("admin").runCommand({ping:1}).ok, 1,
|
|
|
|
|
"we aren't connected to the secondary");
|
2011-12-20 12:05:38 -05:00
|
|
|
|
|
|
|
|
reconnect(master);
|
|
|
|
|
|
2011-05-06 10:43:39 -04:00
|
|
|
assert.soon(function() {
|
|
|
|
|
var c = master.getDB("local").system.replset.findOne();
|
|
|
|
|
return c.version == 2;
|
2011-12-20 12:05:38 -05:00
|
|
|
});
|
2010-11-17 18:44:32 -05:00
|
|
|
|
2012-04-09 15:00:12 -04:00
|
|
|
print("Add it back as a secondary");
|
2015-09-28 05:47:57 -04:00
|
|
|
config.members.push({_id:2, host : secondary.host});
|
2011-05-10 17:32:20 -04:00
|
|
|
config.version = 3;
|
2015-11-04 15:32:27 -05:00
|
|
|
// Need to keep retrying reconfig here, as it will not work at first due to the primary's
|
|
|
|
|
// perception that the secondary is still "down".
|
|
|
|
|
assert.soon(function() { try {
|
|
|
|
|
reconfig(replTest, config);
|
2010-11-18 09:56:21 -05:00
|
|
|
return true;
|
2015-11-04 15:32:27 -05:00
|
|
|
} catch (e) {
|
|
|
|
|
return false;
|
|
|
|
|
} });
|
2015-11-25 11:20:43 -05:00
|
|
|
master = replTest.getPrimary();
|
2015-11-04 15:32:27 -05:00
|
|
|
printjson(master.getDB("admin").runCommand({replSetGetStatus:1}));
|
|
|
|
|
var newConfig = master.getDB("local").system.replset.findOne();
|
|
|
|
|
print("newConfig: " + tojson(newConfig));
|
|
|
|
|
assert.eq(newConfig.version, 3);
|
2010-11-17 18:44:32 -05:00
|
|
|
|
2011-06-14 16:22:51 -04:00
|
|
|
print("reconfig with minority");
|
2015-09-28 05:47:57 -04:00
|
|
|
replTest.stop(secondary);
|
2011-06-14 16:22:51 -04:00
|
|
|
|
|
|
|
|
assert.soon(function() {
|
2011-07-25 19:03:56 -04:00
|
|
|
try {
|
|
|
|
|
return master.getDB("admin").runCommand({isMaster : 1}).secondary;
|
|
|
|
|
}
|
|
|
|
|
catch(e) {
|
|
|
|
|
print("trying to get master: "+e);
|
|
|
|
|
}
|
2012-04-09 15:00:12 -04:00
|
|
|
},"waiting for primary to step down",(60*1000),1000);
|
2011-06-14 16:22:51 -04:00
|
|
|
|
|
|
|
|
config.version = 4;
|
|
|
|
|
config.members.pop();
|
|
|
|
|
try {
|
|
|
|
|
master.getDB("admin").runCommand({replSetReconfig : config, force : true});
|
|
|
|
|
}
|
|
|
|
|
catch(e) {
|
|
|
|
|
print(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reconnect(master);
|
|
|
|
|
assert.soon(function() {
|
|
|
|
|
return master.getDB("admin").runCommand({isMaster : 1}).ismaster;
|
2012-04-09 15:00:12 -04:00
|
|
|
},"waiting for old primary to accept reconfig and step up",(60*1000),1000);
|
2011-06-14 16:22:51 -04:00
|
|
|
|
|
|
|
|
config = master.getDB("local").system.replset.findOne();
|
|
|
|
|
printjson(config);
|
2014-10-13 13:34:58 -04:00
|
|
|
assert.gt(config.version, 4);
|
2011-06-14 16:22:51 -04:00
|
|
|
|
2010-11-17 18:44:32 -05:00
|
|
|
replTest.stopSet();
|