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();
}
}
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++;
}
}
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();
}
Aggregations