use of com.twitter.common.zookeeper.ZooKeeperClient.ZooKeeperConnectionException in project commons by twitter.
the class ZooKeeperNode method init.
/**
* Initialize zookeeper tracking for this {@link Supplier}. Once this call returns, this object
* will be tracking data in zookeeper.
*
* @throws InterruptedException if the underlying zookeeper server transaction is interrupted
* @throws KeeperException if the server signals an error
* @throws ZooKeeperConnectionException if there was a problem connecting to the zookeeper
* cluster
*/
@VisibleForTesting
void init() throws InterruptedException, KeeperException, ZooKeeperConnectionException {
Watcher watcher = zkClient.registerExpirationHandler(new Command() {
@Override
public void execute() {
try {
synchronized (safeToRewatchLock) {
if (safeToRewatch) {
tryWatchDataNode();
}
}
} catch (InterruptedException e) {
LOG.log(Level.WARNING, "Interrupted while trying to re-establish watch.", e);
Thread.currentThread().interrupt();
}
}
});
try {
/*
* Synchronize to prevent the race of watchDataNode completing and then the session expiring
* before we update safeToRewatch.
*/
synchronized (safeToRewatchLock) {
watchDataNode();
safeToRewatch = true;
}
} catch (InterruptedException e) {
zkClient.unregister(watcher);
throw e;
} catch (KeeperException e) {
zkClient.unregister(watcher);
throw e;
} catch (ZooKeeperConnectionException e) {
zkClient.unregister(watcher);
throw e;
}
}
use of com.twitter.common.zookeeper.ZooKeeperClient.ZooKeeperConnectionException in project commons by twitter.
the class ZooKeeperClientTest method testGet.
@Test
public void testGet() throws Exception {
final ZooKeeperClient zkClient = createZkClient();
shutdownNetwork();
try {
zkClient.get(Amount.of(50L, Time.MILLISECONDS));
fail("Expected client connection to timeout while network down");
} catch (TimeoutException e) {
assertTrue(zkClient.isClosed());
}
assertNull(zkClient.getZooKeeperClientForTests());
final CountDownLatch blockingGetComplete = new CountDownLatch(1);
final AtomicReference<ZooKeeper> client = new AtomicReference<ZooKeeper>();
new Thread(new Runnable() {
@Override
public void run() {
try {
client.set(zkClient.get());
} catch (ZooKeeperConnectionException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
blockingGetComplete.countDown();
}
}
}).start();
restartNetwork();
// Hung blocking connects should succeed when server connection comes up
blockingGetComplete.await();
assertNotNull(client.get());
// New connections should succeed now that network is back up
long sessionId = zkClient.get().getSessionId();
// While connected the same client should be reused (no new connections while healthy)
assertSame(client.get(), zkClient.get());
shutdownNetwork();
// Our client doesn't know the network is down yet so we should be able to get()
ZooKeeper zooKeeper = zkClient.get();
try {
zooKeeper.exists("/", false);
fail("Expected client operation to fail while network down");
} catch (ConnectionLossException e) {
// expected
}
restartNetwork();
assertEquals("Expected connection to be re-established with existing session", sessionId, zkClient.get().getSessionId());
}
use of com.twitter.common.zookeeper.ZooKeeperClient.ZooKeeperConnectionException in project commons by twitter.
the class ZooKeeperClientTest method testGetInterrupted.
/**
* Test that if a blocking get() call gets interrupted, after a connection has been created
* but before it's connected, the zk connection gets closed.
*/
@Test
public void testGetInterrupted() throws Exception {
final ZooKeeperClient zkClient = createZkClient();
shutdownNetwork();
final CountDownLatch blockingGetComplete = new CountDownLatch(1);
final AtomicBoolean interrupted = new AtomicBoolean();
final AtomicReference<ZooKeeper> client = new AtomicReference<ZooKeeper>();
Thread getThread = new Thread(new Runnable() {
@Override
public void run() {
try {
client.set(zkClient.get());
} catch (ZooKeeperConnectionException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
interrupted.set(true);
throw new RuntimeException(e);
} finally {
blockingGetComplete.countDown();
}
}
});
getThread.start();
while (zkClient.getZooKeeperClientForTests() == null) {
Thread.sleep(100);
}
getThread.interrupt();
blockingGetComplete.await();
assertNull("The zk connection should have been closed", zkClient.getZooKeeperClientForTests());
assertTrue("The waiter thread should have been interrupted", interrupted.get());
assertTrue(zkClient.isClosed());
}
use of com.twitter.common.zookeeper.ZooKeeperClient.ZooKeeperConnectionException in project commons by twitter.
the class ZooKeeperMap method init.
/**
* Initialize zookeeper tracking for this {@link Map}. Once this call returns, this object
* will be tracking data in zookeeper.
*
* @throws InterruptedException if the underlying zookeeper server transaction is interrupted
* @throws KeeperException if the server signals an error
* @throws ZooKeeperConnectionException if there was a problem connecting to the zookeeper
* cluster
*/
@VisibleForTesting
void init() throws InterruptedException, KeeperException, ZooKeeperConnectionException {
Watcher watcher = zkClient.registerExpirationHandler(new Command() {
@Override
public void execute() {
/*
* First rewatch all of our locally cached children. Some of them may not exist anymore,
* which will lead to caught KeeperException.NoNode whereafter we'll remove that child
* from the cached map.
*
* Next, we'll establish our top level child watch and add any new nodes that might exist.
*/
try {
synchronized (safeToRewatchLock) {
if (safeToRewatch) {
rewatchDataNodes();
tryWatchChildren();
}
}
} catch (InterruptedException e) {
LOG.log(Level.WARNING, "Interrupted while trying to re-establish watch.", e);
Thread.currentThread().interrupt();
}
}
});
try {
// before we update safeToRewatch.
synchronized (safeToRewatchLock) {
watchChildren();
safeToRewatch = true;
}
} catch (InterruptedException e) {
zkClient.unregister(watcher);
throw e;
} catch (KeeperException e) {
zkClient.unregister(watcher);
throw e;
} catch (ZooKeeperConnectionException e) {
zkClient.unregister(watcher);
throw e;
}
}
Aggregations