use of org.apache.zookeeper.admin.ZooKeeperAdmin in project zookeeper by apache.
the class ReconfigTest method testRoleChange.
@Test
public void testRoleChange() throws Exception {
// create 3 servers
qu = new QuorumUtil(1);
qu.disableJMXTest = true;
qu.startAll();
ZooKeeper[] zkArr = createHandles(qu);
ZooKeeperAdmin[] zkAdminArr = createAdminHandles(qu);
// changing a server's role / port is done by "adding" it with the same
// id but different role / port
List<String> joiningServers = new ArrayList<String>();
int leaderIndex = getLeaderId(qu);
// during first and second iteration, leavingIndex will correspond to a
// follower
// during third and fouth iteration leavingIndex will be the index of
// the leader
int changingIndex = (leaderIndex == 1) ? 2 : 1;
// first convert participant to observer, then observer to participant,
// and so on
String newRole = "observer";
for (int i = 0; i < 4; i++) {
// some of the operations will be executed by a client connected to
// the removed server
// while others are invoked by a client connected to some other
// server.
// when we're removing the leader, zk1 will be the client connected
// to removed server
ZooKeeper zk1 = (changingIndex == leaderIndex) ? zkArr[leaderIndex] : zkArr[(leaderIndex % qu.ALL) + 1];
ZooKeeperAdmin zkAdmin1 = (changingIndex == leaderIndex) ? zkAdminArr[leaderIndex] : zkAdminArr[(leaderIndex % qu.ALL) + 1];
// exactly as it is now, except for role change
joiningServers.add("server." + changingIndex + "=localhost:" + qu.getPeer(changingIndex).peer.getQuorumAddress().getPort() + ":" + qu.getPeer(changingIndex).peer.getElectionAddress().getPort() + ":" + newRole + ";localhost:" + qu.getPeer(changingIndex).peer.getClientPort());
reconfig(zkAdmin1, joiningServers, null, null, -1);
testNormalOperation(zkArr[changingIndex], zk1);
if (newRole.equals("observer")) {
Assert.assertTrue(qu.getPeer(changingIndex).peer.observer != null && qu.getPeer(changingIndex).peer.follower == null && qu.getPeer(changingIndex).peer.leader == null);
Assert.assertTrue(qu.getPeer(changingIndex).peer.getPeerState() == ServerState.OBSERVING);
} else {
Assert.assertTrue(qu.getPeer(changingIndex).peer.observer == null && (qu.getPeer(changingIndex).peer.follower != null || qu.getPeer(changingIndex).peer.leader != null));
Assert.assertTrue(qu.getPeer(changingIndex).peer.getPeerState() == ServerState.FOLLOWING || qu.getPeer(changingIndex).peer.getPeerState() == ServerState.LEADING);
}
joiningServers.clear();
if (newRole.equals("observer")) {
newRole = "participant";
} else {
// lets change leader to observer
newRole = "observer";
leaderIndex = getLeaderId(qu);
changingIndex = leaderIndex;
}
}
closeAllHandles(zkArr, zkAdminArr);
}
Aggregations