Search in sources :

Example 1 with States

use of org.apache.zookeeper.ZooKeeper.States in project zookeeper by apache.

the class ClientRetryTest method testClientRetry.

/*
     * This is a simple test - try to connect two clients to a server
     * accepting a maximum of one connection from each address. Check that
     * only one is accepted. Close that connection, and check that the other
     * eventually connects.
     *
     * There is a possibility of a false positive here, as when zk2 is tested
     * for having connected it might not have been given enough time, and finish
     * connecting after the test is done. Since the
     * server doesn't tell the client why it hasn't connected, there's no
     * obvious way to detect the difference.
     */
@Test
public void testClientRetry() throws IOException, InterruptedException, TimeoutException {
    CountdownWatcher cdw1 = new CountdownWatcher();
    CountdownWatcher cdw2 = new CountdownWatcher();
    ZooKeeper zk = new ZooKeeper(hostPort, 10000, cdw1);
    try {
        cdw1.waitForConnected(CONNECTION_TIMEOUT);
        ZooKeeper zk2 = new ZooKeeper(hostPort, 10000, cdw2);
        try {
            States s1 = zk.getState();
            States s2 = zk2.getState();
            Assert.assertSame(s1, States.CONNECTED);
            Assert.assertSame(s2, States.CONNECTING);
            cdw1.reset();
            zk.close();
            cdw1.waitForDisconnected(CONNECTION_TIMEOUT);
            cdw2.waitForConnected(CONNECTION_TIMEOUT);
            Assert.assertSame(zk2.getState(), States.CONNECTED);
        } finally {
            zk2.close();
        }
    } finally {
        zk.close();
    }
}
Also used : States(org.apache.zookeeper.ZooKeeper.States) ZooKeeper(org.apache.zookeeper.ZooKeeper) Test(org.junit.Test)

Example 2 with States

use of org.apache.zookeeper.ZooKeeper.States in project coprhd-controller by CoprHD.

the class CoordinatorClientExt method blockUntilZookeeperIsWritableConnected.

public void blockUntilZookeeperIsWritableConnected(long sleepInterval, long maxRetryTimes) {
    int retryTimes = 0;
    while (true) {
        try {
            States state = getConnectionState();
            if (state.equals(States.CONNECTED))
                return;
            _log.info("ZK connection state is {}, wait for connected", state);
        } catch (Exception e) {
            _log.error("Can't get Zk state {}", e);
        }
        if (retryTimes > maxRetryTimes) {
            _log.error("Unable to connect to zookeeper server side after retrying {} times", retryTimes);
            throw new IllegalStateException("Unable to connect to zookeeper server");
        }
        try {
            Thread.sleep(sleepInterval);
        } catch (InterruptedException e) {
        // Ingore
        }
        retryTimes++;
    }
}
Also used : States(org.apache.zookeeper.ZooKeeper.States) APIException(com.emc.storageos.svcs.errorhandling.resources.APIException) CoordinatorException(com.emc.storageos.coordinator.exceptions.CoordinatorException) SyssvcException(com.emc.storageos.systemservices.exceptions.SyssvcException) InvalidLockOwnerException(com.emc.storageos.systemservices.exceptions.InvalidLockOwnerException) IOException(java.io.IOException) CoordinatorClientException(com.emc.storageos.systemservices.exceptions.CoordinatorClientException)

Example 3 with States

use of org.apache.zookeeper.ZooKeeper.States in project hbase by apache.

the class TestZooKeeper method testClientSessionExpired.

/**
   * See HBASE-1232 and http://hbase.apache.org/book.html#trouble.zookeeper.
   * @throws IOException
   * @throws InterruptedException
   */
// fails frequently, disabled for now, see HBASE-6406
//@Test
public void testClientSessionExpired() throws Exception {
    Configuration c = new Configuration(TEST_UTIL.getConfiguration());
    // We don't want to share the connection as we will check its state
    c.set(HConstants.HBASE_CLIENT_INSTANCE_ID, "1111");
    Connection connection = ConnectionFactory.createConnection(c);
    ZooKeeperWatcher connectionZK = getZooKeeperWatcher(connection);
    LOG.info("ZooKeeperWatcher= 0x" + Integer.toHexString(connectionZK.hashCode()));
    LOG.info("getRecoverableZooKeeper= 0x" + Integer.toHexString(connectionZK.getRecoverableZooKeeper().hashCode()));
    LOG.info("session=" + Long.toHexString(connectionZK.getRecoverableZooKeeper().getSessionId()));
    TEST_UTIL.expireSession(connectionZK);
    LOG.info("Before using zkw state=" + connectionZK.getRecoverableZooKeeper().getState());
    // provoke session expiration by doing something with ZK
    try {
        connectionZK.getRecoverableZooKeeper().getZooKeeper().exists("/1/1", false);
    } catch (KeeperException ignored) {
    }
    // Check that the old ZK connection is closed, means we did expire
    States state = connectionZK.getRecoverableZooKeeper().getState();
    LOG.info("After using zkw state=" + state);
    LOG.info("session=" + Long.toHexString(connectionZK.getRecoverableZooKeeper().getSessionId()));
    // It's asynchronous, so we may have to wait a little...
    final long limit1 = System.currentTimeMillis() + 3000;
    while (System.currentTimeMillis() < limit1 && state != States.CLOSED) {
        state = connectionZK.getRecoverableZooKeeper().getState();
    }
    LOG.info("After using zkw loop=" + state);
    LOG.info("ZooKeeper should have timed out");
    LOG.info("session=" + Long.toHexString(connectionZK.getRecoverableZooKeeper().getSessionId()));
    // It's surprising but sometimes we can still be in connected state.
    // As it's known (even if not understood) we don't make the the test fail
    // for this reason.)
    // Assert.assertTrue("state=" + state, state == States.CLOSED);
    // Check that the client recovered
    ZooKeeperWatcher newConnectionZK = getZooKeeperWatcher(connection);
    States state2 = newConnectionZK.getRecoverableZooKeeper().getState();
    LOG.info("After new get state=" + state2);
    // As it's an asynchronous event we may got the same ZKW, if it's not
    //  yet invalidated. Hence this loop.
    final long limit2 = System.currentTimeMillis() + 3000;
    while (System.currentTimeMillis() < limit2 && state2 != States.CONNECTED && state2 != States.CONNECTING) {
        newConnectionZK = getZooKeeperWatcher(connection);
        state2 = newConnectionZK.getRecoverableZooKeeper().getState();
    }
    LOG.info("After new get state loop=" + state2);
    Assert.assertTrue(state2 == States.CONNECTED || state2 == States.CONNECTING);
    connection.close();
}
Also used : States(org.apache.zookeeper.ZooKeeper.States) Configuration(org.apache.hadoop.conf.Configuration) ZooKeeperWatcher(org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher) Connection(org.apache.hadoop.hbase.client.Connection) KeeperException(org.apache.zookeeper.KeeperException)

Aggregations

States (org.apache.zookeeper.ZooKeeper.States)3 CoordinatorException (com.emc.storageos.coordinator.exceptions.CoordinatorException)1 APIException (com.emc.storageos.svcs.errorhandling.resources.APIException)1 CoordinatorClientException (com.emc.storageos.systemservices.exceptions.CoordinatorClientException)1 InvalidLockOwnerException (com.emc.storageos.systemservices.exceptions.InvalidLockOwnerException)1 SyssvcException (com.emc.storageos.systemservices.exceptions.SyssvcException)1 IOException (java.io.IOException)1 Configuration (org.apache.hadoop.conf.Configuration)1 Connection (org.apache.hadoop.hbase.client.Connection)1 ZooKeeperWatcher (org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher)1 KeeperException (org.apache.zookeeper.KeeperException)1 ZooKeeper (org.apache.zookeeper.ZooKeeper)1 Test (org.junit.Test)1