use of org.apache.zookeeper.Watcher in project commons by twitter.
the class ZooKeeperClient method get.
/**
* Returns the current active ZK connection or establishes a new one if none has yet been
* established or a previous connection was disconnected or had its session time out. This
* method will attempt to re-use sessions when possible.
*
* @param connectionTimeout the maximum amount of time to wait for the connection to the ZK
* cluster to be established; 0 to wait forever
* @return a connected ZooKeeper client
* @throws ZooKeeperConnectionException if there was a problem connecting to the ZK cluster
* @throws InterruptedException if interrupted while waiting for a connection to be established
* @throws TimeoutException if a connection could not be established within the configured
* session timeout
*/
public synchronized ZooKeeper get(Amount<Long, Time> connectionTimeout) throws ZooKeeperConnectionException, InterruptedException, TimeoutException {
if (zooKeeper == null) {
final CountDownLatch connected = new CountDownLatch(1);
Watcher watcher = new Watcher() {
@Override
public void process(WatchedEvent event) {
switch(event.getType()) {
// the client outside our control.
case None:
switch(event.getState()) {
case Expired:
LOG.info("Zookeeper session expired. Event: " + event);
close();
break;
case SyncConnected:
connected.countDown();
break;
}
}
eventQueue.offer(event);
}
};
try {
zooKeeper = (sessionState != null) ? new ZooKeeper(zooKeeperServers, sessionTimeoutMs, watcher, sessionState.sessionId, sessionState.sessionPasswd) : new ZooKeeper(zooKeeperServers, sessionTimeoutMs, watcher);
} catch (IOException e) {
throw new ZooKeeperConnectionException("Problem connecting to servers: " + zooKeeperServers, e);
}
if (connectionTimeout.getValue() > 0) {
if (!connected.await(connectionTimeout.as(Time.MILLISECONDS), TimeUnit.MILLISECONDS)) {
close();
throw new TimeoutException("Timed out waiting for a ZK connection after " + connectionTimeout);
}
} else {
try {
connected.await();
} catch (InterruptedException ex) {
LOG.info("Interrupted while waiting to connect to zooKeeper");
close();
throw ex;
}
}
credentials.authenticate(zooKeeper);
sessionState = new SessionState(zooKeeper.getSessionId(), zooKeeper.getSessionPasswd());
}
return zooKeeper;
}
use of org.apache.zookeeper.Watcher in project commons by twitter.
the class ZooKeeperMap method addChild.
// TODO(Adam Samet) - Make this use the ZooKeeperNode class.
private void addChild(final String child) throws InterruptedException, KeeperException, ZooKeeperConnectionException {
final Watcher nodeWatcher = new Watcher() {
@Override
public void process(WatchedEvent event) {
if (event.getType() == Watcher.Event.EventType.NodeDataChanged) {
try {
tryAddChild(child);
} catch (InterruptedException e) {
LOG.log(Level.WARNING, "Interrupted while trying to add a child.", e);
Thread.currentThread().interrupt();
}
} else if (event.getType() == Watcher.Event.EventType.NodeDeleted) {
removeEntry(child);
}
}
};
try {
V value = deserializer.apply(zkClient.get().getData(makePath(child), nodeWatcher, null));
putEntry(child, value);
} catch (KeeperException.NoNodeException e) {
// This node doesn't exist anymore, remove it from the map and we're done.
removeEntry(child);
}
}
use of org.apache.zookeeper.Watcher in project commons by twitter.
the class ZooKeeperMap method watchChildren.
private synchronized void watchChildren() throws InterruptedException, KeeperException, ZooKeeperConnectionException {
/*
* Add a watch on the parent node itself, and attempt to rewatch if it
* gets deleted
*/
zkClient.get().exists(nodePath, new Watcher() {
@Override
public void process(WatchedEvent event) {
if (event.getType() == Watcher.Event.EventType.NodeDeleted) {
// If the parent node no longer exists
localMap.clear();
try {
tryWatchChildren();
} catch (InterruptedException e) {
LOG.log(Level.WARNING, "Interrupted while trying to watch children.", e);
Thread.currentThread().interrupt();
}
}
}
});
final Watcher childWatcher = new Watcher() {
@Override
public void process(WatchedEvent event) {
if (event.getType() == Watcher.Event.EventType.NodeChildrenChanged) {
try {
tryWatchChildren();
} catch (InterruptedException e) {
LOG.log(Level.WARNING, "Interrupted while trying to watch children.", e);
Thread.currentThread().interrupt();
}
}
}
};
List<String> children = zkClient.get().getChildren(nodePath, childWatcher);
updateChildren(Sets.newHashSet(children));
}
use of org.apache.zookeeper.Watcher 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;
}
}
use of org.apache.zookeeper.Watcher in project distributedlog by twitter.
the class TestZooKeeperClient method expireZooKeeperSession.
private void expireZooKeeperSession(ZooKeeper zk, int timeout) throws IOException, InterruptedException, KeeperException {
final CountDownLatch latch = new CountDownLatch(1);
ZooKeeper newZk = new ZooKeeper(zkServers, timeout, new Watcher() {
@Override
public void process(WatchedEvent event) {
if (event.getType() == EventType.None && event.getState() == KeeperState.SyncConnected) {
latch.countDown();
}
}
}, zk.getSessionId(), zk.getSessionPasswd());
if (!latch.await(timeout, TimeUnit.MILLISECONDS)) {
throw KeeperException.create(KeeperException.Code.CONNECTIONLOSS);
}
newZk.close();
}
Aggregations