Search in sources :

Example 1 with ZooKeeperAdmin

use of org.apache.zookeeper.admin.ZooKeeperAdmin in project zookeeper by apache.

the class ReconfigMisconfigTest method instantiateZKAdmin.

private void instantiateZKAdmin() throws InterruptedException {
    String cnxString;
    ClientBase.CountdownWatcher watcher = new ClientBase.CountdownWatcher();
    try {
        cnxString = "127.0.0.1:" + qu.getPeer(1).peer.getClientPort();
        zkAdmin = new ZooKeeperAdmin(cnxString, ClientBase.CONNECTION_TIMEOUT, watcher);
    } catch (IOException e) {
        fail("Fail to create ZooKeeperAdmin handle.");
        return;
    }
    try {
        watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT);
    } catch (InterruptedException | TimeoutException e) {
        fail("ZooKeeper admin client can not connect to " + cnxString);
    }
}
Also used : IOException(java.io.IOException) ZooKeeperAdmin(org.apache.zookeeper.admin.ZooKeeperAdmin) TimeoutException(java.util.concurrent.TimeoutException)

Example 2 with ZooKeeperAdmin

use of org.apache.zookeeper.admin.ZooKeeperAdmin in project zookeeper by apache.

the class ReconfigTest method testPortChange.

@Test
public void testPortChange() throws Exception {
    // create 3 servers
    qu = new QuorumUtil(1);
    qu.disableJMXTest = true;
    qu.startAll();
    zkArr = createHandles(qu);
    zkAdminArr = createAdminHandles(qu);
    List<String> joiningServers = new ArrayList<String>();
    int leaderIndex = getLeaderId(qu);
    int followerIndex = leaderIndex == 1 ? 2 : 1;
    // modify follower's client port
    int quorumPort = qu.getPeer(followerIndex).peer.getQuorumAddress().getAllPorts().get(0);
    int electionPort = qu.getPeer(followerIndex).peer.getElectionAddress().getAllPorts().get(0);
    int oldClientPort = qu.getPeer(followerIndex).peer.getClientPort();
    int newClientPort = PortAssignment.unique();
    joiningServers.add("server." + followerIndex + "=localhost:" + quorumPort + ":" + electionPort + ":participant;localhost:" + newClientPort);
    // create a /test znode and check that read/write works before
    // any reconfig is invoked
    testNormalOperation(zkArr[followerIndex], zkArr[leaderIndex]);
    reconfig(zkAdminArr[followerIndex], joiningServers, null, null, -1);
    try {
        for (int i = 0; i < 20; i++) {
            Thread.sleep(1000);
            zkArr[followerIndex].setData("/test", "teststr".getBytes(), -1);
        }
    } catch (KeeperException.ConnectionLossException e) {
        fail("Existing client disconnected when client port changed!");
    }
    zkArr[followerIndex].close();
    zkArr[followerIndex] = new ZooKeeper("127.0.0.1:" + oldClientPort, ClientBase.CONNECTION_TIMEOUT, DummyWatcher.INSTANCE);
    zkAdminArr[followerIndex].close();
    zkAdminArr[followerIndex] = new ZooKeeperAdmin("127.0.0.1:" + oldClientPort, ClientBase.CONNECTION_TIMEOUT, DummyWatcher.INSTANCE);
    zkAdminArr[followerIndex].addAuthInfo("digest", "super:test".getBytes());
    for (int i = 0; i < 10; i++) {
        try {
            Thread.sleep(1000);
            zkArr[followerIndex].setData("/test", "teststr".getBytes(), -1);
            fail("New client connected to old client port!");
        } catch (KeeperException.ConnectionLossException e) {
        }
    }
    zkArr[followerIndex].close();
    zkArr[followerIndex] = new ZooKeeper("127.0.0.1:" + newClientPort, ClientBase.CONNECTION_TIMEOUT, DummyWatcher.INSTANCE);
    zkAdminArr[followerIndex].close();
    zkAdminArr[followerIndex] = new ZooKeeperAdmin("127.0.0.1:" + newClientPort, ClientBase.CONNECTION_TIMEOUT, DummyWatcher.INSTANCE);
    zkAdminArr[followerIndex].addAuthInfo("digest", "super:test".getBytes());
    testNormalOperation(zkArr[followerIndex], zkArr[leaderIndex]);
    testServerHasConfig(zkArr[followerIndex], joiningServers, null);
    assertEquals(newClientPort, qu.getPeer(followerIndex).peer.getClientPort());
    joiningServers.clear();
    // change leader's leading port - should renounce leadership
    int newQuorumPort = PortAssignment.unique();
    joiningServers.add("server." + leaderIndex + "=localhost:" + newQuorumPort + ":" + qu.getPeer(leaderIndex).peer.getElectionAddress().getAllPorts().get(0) + ":participant;localhost:" + qu.getPeer(leaderIndex).peer.getClientPort());
    reconfig(zkAdminArr[leaderIndex], joiningServers, null, null, -1);
    testNormalOperation(zkArr[followerIndex], zkArr[leaderIndex]);
    assertEquals((int) qu.getPeer(leaderIndex).peer.getQuorumAddress().getAllPorts().get(0), newQuorumPort);
    joiningServers.clear();
    for (int i = 1; i <= 3; i++) {
        joiningServers.add("server." + i + "=localhost:" + qu.getPeer(i).peer.getQuorumAddress().getAllPorts().get(0) + ":" + PortAssignment.unique() + ":participant;localhost:" + qu.getPeer(i).peer.getClientPort());
    }
    reconfig(zkAdminArr[1], joiningServers, null, null, -1);
    leaderIndex = getLeaderId(qu);
    int follower1 = leaderIndex == 1 ? 2 : 1;
    int follower2 = 1;
    while (follower2 == leaderIndex || follower2 == follower1) {
        follower2++;
    }
    // lets kill the leader and see if a new one is elected
    qu.shutdown(getLeaderId(qu));
    testNormalOperation(zkArr[follower2], zkArr[follower1]);
    testServerHasConfig(zkArr[follower1], joiningServers, null);
    testServerHasConfig(zkArr[follower2], joiningServers, null);
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) ArrayList(java.util.ArrayList) KeeperException(org.apache.zookeeper.KeeperException) ZooKeeperAdmin(org.apache.zookeeper.admin.ZooKeeperAdmin) Test(org.junit.jupiter.api.Test)

Example 3 with ZooKeeperAdmin

use of org.apache.zookeeper.admin.ZooKeeperAdmin in project zookeeper by apache.

the class ReconfigTest method testJMXBeanAfterRemoveAddOne.

/**
 * Tests verifies the jmx attributes of local and remote peer bean - remove
 * one quorum peer and again adding it back
 */
@Test
public void testJMXBeanAfterRemoveAddOne() throws Exception {
    // create 3 servers
    qu = new QuorumUtil(1);
    qu.disableJMXTest = true;
    qu.startAll();
    zkArr = createHandles(qu);
    zkAdminArr = createAdminHandles(qu);
    List<String> leavingServers = new ArrayList<String>();
    List<String> joiningServers = new ArrayList<String>();
    // assert remotePeerBean.1 of ReplicatedServer_2
    int leavingIndex = 1;
    int replica2 = 2;
    QuorumPeer peer2 = qu.getPeer(replica2).peer;
    QuorumServer leavingQS2 = peer2.getView().get(Long.valueOf(leavingIndex));
    String remotePeerBean2 = MBeanRegistry.DOMAIN + ":name0=ReplicatedServer_id" + replica2 + ",name1=replica." + leavingIndex;
    assertRemotePeerMXBeanAttributes(leavingQS2, remotePeerBean2);
    // assert remotePeerBean.1 of ReplicatedServer_3
    int replica3 = 3;
    QuorumPeer peer3 = qu.getPeer(replica3).peer;
    QuorumServer leavingQS3 = peer3.getView().get(Long.valueOf(leavingIndex));
    String remotePeerBean3 = MBeanRegistry.DOMAIN + ":name0=ReplicatedServer_id" + replica3 + ",name1=replica." + leavingIndex;
    assertRemotePeerMXBeanAttributes(leavingQS3, remotePeerBean3);
    ZooKeeper zk = zkArr[leavingIndex];
    ZooKeeperAdmin zkAdmin = zkAdminArr[leavingIndex];
    leavingServers.add(Integer.toString(leavingIndex));
    // remember this server so we can add it back later
    joiningServers.add("server." + leavingIndex + "=127.0.0.1:" + qu.getPeer(leavingIndex).peer.getQuorumAddress().getAllPorts().get(0) + ":" + qu.getPeer(leavingIndex).peer.getElectionAddress().getAllPorts().get(0) + ":participant;127.0.0.1:" + qu.getPeer(leavingIndex).peer.getClientPort());
    // Remove ReplicatedServer_1 from the ensemble
    reconfig(zkAdmin, null, leavingServers, null, -1);
    // localPeerBean.1 of ReplicatedServer_1
    QuorumPeer removedPeer = qu.getPeer(leavingIndex).peer;
    String localPeerBean = MBeanRegistry.DOMAIN + ":name0=ReplicatedServer_id" + leavingIndex + ",name1=replica." + leavingIndex;
    assertLocalPeerMXBeanAttributes(removedPeer, localPeerBean, false);
    // remotePeerBean.1 shouldn't exists in ReplicatedServer_2
    JMXEnv.ensureNone(remotePeerBean2);
    // remotePeerBean.1 shouldn't exists in ReplicatedServer_3
    JMXEnv.ensureNone(remotePeerBean3);
    // Add ReplicatedServer_1 back to the ensemble
    reconfig(zkAdmin, joiningServers, null, null, -1);
    // localPeerBean.1 of ReplicatedServer_1
    assertLocalPeerMXBeanAttributes(removedPeer, localPeerBean, true);
    // assert remotePeerBean.1 of ReplicatedServer_2
    leavingQS2 = peer2.getView().get(Long.valueOf(leavingIndex));
    assertRemotePeerMXBeanAttributes(leavingQS2, remotePeerBean2);
    // assert remotePeerBean.1 of ReplicatedServer_3
    leavingQS3 = peer3.getView().get(Long.valueOf(leavingIndex));
    assertRemotePeerMXBeanAttributes(leavingQS3, remotePeerBean3);
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) QuorumServer(org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer) ArrayList(java.util.ArrayList) QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) ZooKeeperAdmin(org.apache.zookeeper.admin.ZooKeeperAdmin) Test(org.junit.jupiter.api.Test)

Example 4 with ZooKeeperAdmin

use of org.apache.zookeeper.admin.ZooKeeperAdmin in project zookeeper by apache.

the class StandaloneTest method testStandaloneReconfigFails.

/**
 * Verify that reconfiguration in standalone mode fails with
 * KeeperException.UnimplementedException.
 */
@Test
public void testStandaloneReconfigFails() throws Exception {
    ClientBase.setupTestEnv();
    final int CLIENT_PORT = PortAssignment.unique();
    final String HOSTPORT = "127.0.0.1:" + CLIENT_PORT;
    File tmpDir = ClientBase.createTmpDir();
    ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
    ServerCnxnFactory f = ServerCnxnFactory.createFactory(CLIENT_PORT, -1);
    f.startup(zks);
    assertTrue(ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT), "waiting for server being up ");
    CountdownWatcher watcher = new CountdownWatcher();
    ZooKeeper zk = new ZooKeeper(HOSTPORT, CONNECTION_TIMEOUT, watcher);
    ZooKeeperAdmin zkAdmin = new ZooKeeperAdmin(HOSTPORT, CONNECTION_TIMEOUT, watcher);
    watcher.waitForConnected(CONNECTION_TIMEOUT);
    List<String> joiners = new ArrayList<String>();
    joiners.add("server.2=localhost:1234:1235;1236");
    // generate some transactions that will get logged
    try {
        zkAdmin.addAuthInfo("digest", "super:test".getBytes());
        zkAdmin.reconfigure(joiners, null, null, -1, new Stat());
        fail("Reconfiguration in standalone should trigger " + "UnimplementedException");
    } catch (KeeperException.UnimplementedException ex) {
    // expected
    }
    zk.close();
    zks.shutdown();
    f.shutdown();
    assertTrue(ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT), "waiting for server being down ");
}
Also used : CountdownWatcher(org.apache.zookeeper.test.ClientBase.CountdownWatcher) ArrayList(java.util.ArrayList) ZooKeeperAdmin(org.apache.zookeeper.admin.ZooKeeperAdmin) ZooKeeper(org.apache.zookeeper.ZooKeeper) Stat(org.apache.zookeeper.data.Stat) ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) File(java.io.File) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) KeeperException(org.apache.zookeeper.KeeperException) Test(org.junit.jupiter.api.Test)

Example 5 with ZooKeeperAdmin

use of org.apache.zookeeper.admin.ZooKeeperAdmin in project zookeeper by apache.

the class ReconfigTest method testJMXBeanAfterRoleChange.

/**
 * Tests verifies the jmx attributes of local and remote peer bean - change
 * participant to observer role
 */
@Test
public void testJMXBeanAfterRoleChange() throws Exception {
    // create 3 servers
    qu = new QuorumUtil(1);
    qu.disableJMXTest = true;
    qu.startAll();
    zkArr = createHandles(qu);
    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>();
    // assert remotePeerBean.1 of ReplicatedServer_2
    int changingIndex = 1;
    int replica2 = 2;
    QuorumPeer peer2 = qu.getPeer(replica2).peer;
    QuorumServer changingQS2 = peer2.getView().get(Long.valueOf(changingIndex));
    String remotePeerBean2 = MBeanRegistry.DOMAIN + ":name0=ReplicatedServer_id" + replica2 + ",name1=replica." + changingIndex;
    assertRemotePeerMXBeanAttributes(changingQS2, remotePeerBean2);
    // assert remotePeerBean.1 of ReplicatedServer_3
    int replica3 = 3;
    QuorumPeer peer3 = qu.getPeer(replica3).peer;
    QuorumServer changingQS3 = peer3.getView().get(Long.valueOf(changingIndex));
    String remotePeerBean3 = MBeanRegistry.DOMAIN + ":name0=ReplicatedServer_id" + replica3 + ",name1=replica." + changingIndex;
    assertRemotePeerMXBeanAttributes(changingQS3, remotePeerBean3);
    String newRole = "observer";
    ZooKeeper zk = zkArr[changingIndex];
    ZooKeeperAdmin zkAdmin = zkAdminArr[changingIndex];
    // exactly as it is now, except for role change
    joiningServers.add("server." + changingIndex + "=127.0.0.1:" + qu.getPeer(changingIndex).peer.getQuorumAddress().getAllPorts().get(0) + ":" + qu.getPeer(changingIndex).peer.getElectionAddress().getAllPorts().get(0) + ":" + newRole + ";127.0.0.1:" + qu.getPeer(changingIndex).peer.getClientPort());
    reconfig(zkAdmin, joiningServers, null, null, -1);
    testNormalOperation(zkArr[changingIndex], zk);
    assertTrue(qu.getPeer(changingIndex).peer.observer != null && qu.getPeer(changingIndex).peer.follower == null && qu.getPeer(changingIndex).peer.leader == null);
    assertTrue(qu.getPeer(changingIndex).peer.getPeerState() == ServerState.OBSERVING);
    QuorumPeer qp = qu.getPeer(changingIndex).peer;
    String localPeerBeanName = MBeanRegistry.DOMAIN + ":name0=ReplicatedServer_id" + changingIndex + ",name1=replica." + changingIndex;
    // localPeerBean.1 of ReplicatedServer_1
    assertLocalPeerMXBeanAttributes(qp, localPeerBeanName, true);
    // assert remotePeerBean.1 of ReplicatedServer_2
    changingQS2 = peer2.getView().get(Long.valueOf(changingIndex));
    assertRemotePeerMXBeanAttributes(changingQS2, remotePeerBean2);
    // assert remotePeerBean.1 of ReplicatedServer_3
    changingQS3 = peer3.getView().get(Long.valueOf(changingIndex));
    assertRemotePeerMXBeanAttributes(changingQS3, remotePeerBean3);
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) QuorumServer(org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer) ArrayList(java.util.ArrayList) QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) ZooKeeperAdmin(org.apache.zookeeper.admin.ZooKeeperAdmin) Test(org.junit.jupiter.api.Test)

Aggregations

ZooKeeperAdmin (org.apache.zookeeper.admin.ZooKeeperAdmin)30 ZooKeeper (org.apache.zookeeper.ZooKeeper)17 Test (org.junit.jupiter.api.Test)17 ArrayList (java.util.ArrayList)15 ReconfigTest (org.apache.zookeeper.test.ReconfigTest)12 KeeperException (org.apache.zookeeper.KeeperException)11 IOException (java.io.IOException)4 QuorumUtil (org.apache.zookeeper.test.QuorumUtil)4 File (java.io.File)3 Stat (org.apache.zookeeper.data.Stat)3 CountdownWatcher (org.apache.zookeeper.test.ClientBase.CountdownWatcher)3 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 Test (org.junit.Test)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 ValueSource (org.junit.jupiter.params.provider.ValueSource)2 InetSocketAddress (java.net.InetSocketAddress)1 ServerSocket (java.net.ServerSocket)1