Search in sources :

Example 26 with ZooKeeperAdmin

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

the class ReconfigExceptionTest method resetZKAdmin.

// Utility method that recreates a new ZooKeeperAdmin handle, and wait for the handle to connect to
// quorum servers.
private void resetZKAdmin() throws InterruptedException {
    String cnxString;
    ClientBase.CountdownWatcher watcher = new ClientBase.CountdownWatcher();
    try {
        cnxString = "127.0.0.1:" + qu.getPeer(1).peer.getClientPort();
        if (zkAdmin != null) {
            zkAdmin.close();
        }
        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 27 with ZooKeeperAdmin

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

the class ReconfigTest method createAdminHandles.

public static ZooKeeperAdmin[] createAdminHandles(QuorumUtil qu) throws IOException {
    // create an extra handle, so we can index the handles from 1 to qu.ALL
    // using the server id.
    ZooKeeperAdmin[] zkAdminArr = new ZooKeeperAdmin[qu.ALL + 1];
    // not used.
    zkAdminArr[0] = null;
    for (int i = 1; i <= qu.ALL; i++) {
        // server ids are 1, 2 and 3
        zkAdminArr[i] = new ZooKeeperAdmin("127.0.0.1:" + qu.getPeer(i).peer.getClientPort(), ClientBase.CONNECTION_TIMEOUT, DummyWatcher.INSTANCE);
        zkAdminArr[i].addAuthInfo("digest", "super:test".getBytes());
    }
    return zkAdminArr;
}
Also used : ZooKeeperAdmin(org.apache.zookeeper.admin.ZooKeeperAdmin)

Example 28 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();
    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>();
    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().getAllPorts().get(0) + ":" + qu.getPeer(changingIndex).peer.getElectionAddress().getAllPorts().get(0) + ":" + newRole + ";localhost:" + qu.getPeer(changingIndex).peer.getClientPort());
        reconfig(zkAdmin1, joiningServers, null, null, -1);
        testNormalOperation(zkArr[changingIndex], zk1);
        if (newRole.equals("observer")) {
            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);
        } else {
            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.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;
        }
    }
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) ArrayList(java.util.ArrayList) ZooKeeperAdmin(org.apache.zookeeper.admin.ZooKeeperAdmin) Test(org.junit.jupiter.api.Test)

Example 29 with ZooKeeperAdmin

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

the class ReconfigTest method testRemoveAddOne.

@Test
public void testRemoveAddOne() 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>();
    int leaderIndex = getLeaderId(qu);
    // during first iteration, leavingIndex will correspond to a follower
    // during second iteration leavingIndex will be the index of the leader
    int leavingIndex = (leaderIndex == 1) ? 2 : 1;
    for (int i = 0; i < 2; 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 = (leavingIndex == leaderIndex) ? zkArr[leaderIndex] : zkArr[(leaderIndex % qu.ALL) + 1];
        ZooKeeper zk2 = (leavingIndex == leaderIndex) ? zkArr[(leaderIndex % qu.ALL) + 1] : zkArr[leaderIndex];
        ZooKeeperAdmin zkAdmin1 = (leavingIndex == leaderIndex) ? zkAdminArr[leaderIndex] : zkAdminArr[(leaderIndex % qu.ALL) + 1];
        ZooKeeperAdmin zkAdmin2 = (leavingIndex == leaderIndex) ? zkAdminArr[(leaderIndex % qu.ALL) + 1] : zkAdminArr[leaderIndex];
        leavingServers.add(Integer.toString(leavingIndex));
        // remember this server so we can add it back later
        joiningServers.add("server." + leavingIndex + "=localhost:" + qu.getPeer(leavingIndex).peer.getQuorumAddress().getAllPorts().get(0) + ":" + qu.getPeer(leavingIndex).peer.getElectionAddress().getAllPorts().get(0) + ":participant;localhost:" + qu.getPeer(leavingIndex).peer.getClientPort());
        String configStr = reconfig(zkAdmin1, null, leavingServers, null, -1);
        testServerHasConfig(zk2, null, leavingServers);
        testNormalOperation(zk2, zk1);
        QuorumVerifier qv = qu.getPeer(1).peer.configFromString(configStr);
        long version = qv.getVersion();
        // checks that conditioning on version works properly
        try {
            reconfig(zkAdmin2, joiningServers, null, null, version + 1);
            fail("reconfig succeeded even though version condition was incorrect!");
        } catch (KeeperException.BadVersionException e) {
        }
        reconfig(zkAdmin2, joiningServers, null, null, version);
        testNormalOperation(zk1, zk2);
        testServerHasConfig(zk1, joiningServers, null);
        // second iteration of the loop will remove the leader
        // and add it back (as follower)
        leavingIndex = leaderIndex = getLeaderId(qu);
        leavingServers.clear();
        joiningServers.clear();
    }
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) ArrayList(java.util.ArrayList) ZooKeeperAdmin(org.apache.zookeeper.admin.ZooKeeperAdmin) QuorumVerifier(org.apache.zookeeper.server.quorum.flexible.QuorumVerifier) KeeperException(org.apache.zookeeper.KeeperException) Test(org.junit.jupiter.api.Test)

Example 30 with ZooKeeperAdmin

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

the class ObserverMasterTest method testDynamicReconfig.

// This test is known to be flaky and fail due to "reconfig already in progress".
// TODO: Investigate intermittent testDynamicReconfig failures.
@ParameterizedTest
@ValueSource(booleans = { true, false })
@Disabled
public void testDynamicReconfig(boolean testObserverMaster) throws InterruptedException, IOException, KeeperException {
    if (!testObserverMaster) {
        return;
    }
    ClientBase.setupTestEnv();
    // create a quorum running with different observer master port
    // to make it easier to choose which server the observer is
    // following with
    // 
    // we have setObserverMaster function but it's broken, use this
    // solution before we fixed that
    int clientPort1 = PortAssignment.unique();
    int clientPort2 = PortAssignment.unique();
    int omPort1 = PortAssignment.unique();
    int omPort2 = PortAssignment.unique();
    String quorumCfgSection = createServerString("participant", 1, clientPort1) + "\n" + createServerString("participant", 2, clientPort2);
    MainThread s1 = new MainThread(1, clientPort1, quorumCfgSection, String.format("observerMasterPort=%d%n", omPort1));
    MainThread s2 = new MainThread(2, clientPort2, quorumCfgSection, String.format("observerMasterPort=%d%n", omPort2));
    s1.start();
    s2.start();
    waitServerUp(clientPort1);
    waitServerUp(clientPort2);
    // create observer to follow non-leader observer master
    long nonLeaderOMPort = s1.getQuorumPeer().leader == null ? omPort1 : omPort2;
    int observerClientPort = PortAssignment.unique();
    int observerId = 10;
    MainThread observer = new MainThread(observerId, observerClientPort, quorumCfgSection + "\n" + createServerString("observer", observerId, observerClientPort), String.format("observerMasterPort=%d%n", nonLeaderOMPort));
    LOG.info("starting observer");
    observer.start();
    waitServerUp(observerClientPort);
    // create a client to the observer
    final LinkedBlockingQueue<KeeperState> states = new LinkedBlockingQueue<KeeperState>();
    ZooKeeper observerClient = new ZooKeeper("127.0.0.1:" + observerClientPort, ClientBase.CONNECTION_TIMEOUT, event -> {
        try {
            states.put(event.getState());
        } catch (InterruptedException ignore) {
        }
    });
    // wait for connected
    KeeperState state = states.poll(1000, TimeUnit.MILLISECONDS);
    assertEquals(KeeperState.SyncConnected, state);
    // issue reconfig command
    ArrayList<String> newServers = new ArrayList<String>();
    String server = "server.3=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ":participant;localhost:" + PortAssignment.unique();
    newServers.add(server);
    ZooKeeperAdmin admin = createAdmin(clientPort1);
    ReconfigTest.reconfig(admin, newServers, null, null, -1);
    // make sure the observer has the new config
    ReconfigTest.testServerHasConfig(observerClient, newServers, null);
    // shouldn't be disconnected during reconfig, so expect to not
    // receive any new event
    state = states.poll(1000, TimeUnit.MILLISECONDS);
    assertNull(state);
    admin.close();
    observerClient.close();
    observer.shutdown();
    s2.shutdown();
    s1.shutdown();
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) ArrayList(java.util.ArrayList) KeeperState(org.apache.zookeeper.Watcher.Event.KeeperState) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ZooKeeperAdmin(org.apache.zookeeper.admin.ZooKeeperAdmin) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Disabled(org.junit.jupiter.api.Disabled)

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