Search in sources :

Example 21 with ZooKeeperAdmin

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);
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) ArrayList(java.util.ArrayList) ZooKeeperAdmin(org.apache.zookeeper.admin.ZooKeeperAdmin) Test(org.junit.Test)

Aggregations

ZooKeeperAdmin (org.apache.zookeeper.admin.ZooKeeperAdmin)21 ZooKeeper (org.apache.zookeeper.ZooKeeper)15 ArrayList (java.util.ArrayList)14 Test (org.junit.Test)14 KeeperException (org.apache.zookeeper.KeeperException)10 ReconfigTest (org.apache.zookeeper.test.ReconfigTest)7 QuorumUtil (org.apache.zookeeper.test.QuorumUtil)4 WatchedEvent (org.apache.zookeeper.WatchedEvent)3 Watcher (org.apache.zookeeper.Watcher)3 Stat (org.apache.zookeeper.data.Stat)3 File (java.io.File)2 IOException (java.io.IOException)2 Properties (java.util.Properties)2 TimeoutException (java.util.concurrent.TimeoutException)2 QuorumPeer (org.apache.zookeeper.server.quorum.QuorumPeer)2 QuorumServer (org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer)2 CountdownWatcher (org.apache.zookeeper.test.ClientBase.CountdownWatcher)2 InetSocketAddress (java.net.InetSocketAddress)1 ServerSocket (java.net.ServerSocket)1 HashSet (java.util.HashSet)1