use of org.apache.zookeeper.Watcher in project distributedlog by twitter.
the class LocalDLMEmulator method connectZooKeeper.
public static ZooKeeper connectZooKeeper(String zkHost, int zkPort, int zkTimeoutSec) throws IOException, KeeperException, InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
final String zkHostPort = zkHost + ":" + zkPort;
ZooKeeper zkc = new ZooKeeper(zkHostPort, zkTimeoutSec * 1000, new Watcher() {
public void process(WatchedEvent event) {
if (event.getState() == Event.KeeperState.SyncConnected) {
latch.countDown();
}
}
});
if (!latch.await(zkTimeoutSec, TimeUnit.SECONDS)) {
throw new IOException("Zookeeper took too long to connect");
}
return zkc;
}
use of org.apache.zookeeper.Watcher in project distributedlog by twitter.
the class ZooKeeperClient method registerExpirationHandler.
/**
* Clients that need to re-establish state after session expiration can register an
* {@code onExpired} command to execute.
*
* @param onExpired the {@code Command} to register
* @return the new {@link Watcher} which can later be passed to {@link #unregister} for
* removal.
*/
public Watcher registerExpirationHandler(final ZooKeeperSessionExpireNotifier onExpired) {
Watcher watcher = new Watcher() {
@Override
public void process(WatchedEvent event) {
if (event.getType() == EventType.None && event.getState() == KeeperState.Expired) {
try {
onExpired.notifySessionExpired();
} catch (Exception exc) {
// do nothing
}
}
}
};
register(watcher);
return watcher;
}
use of org.apache.zookeeper.Watcher in project distributedlog by twitter.
the class ZooKeeperClient method buildZooKeeper.
private ZooKeeper buildZooKeeper() throws ZooKeeperConnectionException, InterruptedException {
Watcher watcher = new Watcher() {
@Override
public void process(WatchedEvent event) {
switch(event.getType()) {
case None:
switch(event.getState()) {
case Expired:
if (null == retryPolicy) {
LOG.info("ZooKeeper {}' session expired. Event: {}", name, event);
closeInternal();
}
authenticated = false;
break;
case Disconnected:
if (null == retryPolicy) {
LOG.info("ZooKeeper {} is disconnected from zookeeper now," + " but it is OK unless we received EXPIRED event.", name);
}
// Mark as not authenticated if expired or disconnected. In both cases
// we lose any attached auth info. Relying on Expired/Disconnected is
// sufficient since all Expired/Disconnected events are processed before
// all SyncConnected events, and the underlying member is not updated until
// SyncConnected is received.
authenticated = false;
break;
default:
break;
}
}
try {
for (Watcher watcher : watchers) {
try {
watcher.process(event);
} catch (Throwable t) {
LOG.warn("Encountered unexpected exception from watcher {} : ", watcher, t);
}
}
} catch (Throwable t) {
LOG.warn("Encountered unexpected exception when firing watched event {} : ", event, t);
}
}
};
Set<Watcher> watchers = new HashSet<Watcher>();
watchers.add(watcher);
ZooKeeper zk;
try {
RetryPolicy opRetryPolicy = null == retryPolicy ? new BoundExponentialBackoffRetryPolicy(sessionTimeoutMs, sessionTimeoutMs, 0) : retryPolicy;
RetryPolicy connectRetryPolicy = null == retryPolicy ? new BoundExponentialBackoffRetryPolicy(sessionTimeoutMs, sessionTimeoutMs, 0) : new BoundExponentialBackoffRetryPolicy(sessionTimeoutMs, sessionTimeoutMs, Integer.MAX_VALUE);
zk = org.apache.bookkeeper.zookeeper.ZooKeeperClient.newBuilder().connectString(zooKeeperServers).sessionTimeoutMs(sessionTimeoutMs).watchers(watchers).operationRetryPolicy(opRetryPolicy).connectRetryPolicy(connectRetryPolicy).statsLogger(statsLogger).retryThreadCount(retryThreadCount).requestRateLimit(requestRateLimit).build();
} catch (KeeperException e) {
throw new ZooKeeperConnectionException("Problem connecting to servers: " + zooKeeperServers, e);
} catch (IOException e) {
throw new ZooKeeperConnectionException("Problem connecting to servers: " + zooKeeperServers, e);
}
return zk;
}
use of org.apache.zookeeper.Watcher in project heron by twitter.
the class CuratorStateManager method getNodeData.
@Override
protected <M extends Message> ListenableFuture<M> getNodeData(WatchCallback watcher, String path, final Message.Builder builder) {
final SettableFuture<M> future = SettableFuture.create();
Watcher wc = ZkWatcherCallback.makeZkWatcher(watcher);
BackgroundCallback cb = new BackgroundCallback() {
@Override
// we don't know what M is until runtime
@SuppressWarnings("unchecked")
public void processResult(CuratorFramework aClient, CuratorEvent event) throws Exception {
byte[] data;
if (event != null & (data = event.getData()) != null) {
builder.mergeFrom(data);
future.set((M) builder.build());
} else {
future.setException(new RuntimeException("Failed to fetch data from path: " + event.getPath()));
}
}
};
try {
client.getData().usingWatcher(wc).inBackground(cb).forPath(path);
// Suppress it since forPath() throws Exception
// SUPPRESS CHECKSTYLE IllegalCatch
} catch (Exception e) {
future.setException(new RuntimeException("Could not getNodeData", e));
}
return future;
}
use of org.apache.zookeeper.Watcher in project lucene-solr by apache.
the class ZkSolrClientTest method testWatchChildren.
public void testWatchChildren() throws Exception {
try (ZkConnection conn = new ZkConnection()) {
final SolrZkClient zkClient = conn.getClient();
final AtomicInteger cnt = new AtomicInteger();
final CountDownLatch latch = new CountDownLatch(1);
zkClient.makePath("/collections", true);
zkClient.getChildren("/collections", new Watcher() {
@Override
public void process(WatchedEvent event) {
cnt.incrementAndGet();
// remake watch
try {
zkClient.getChildren("/collections", this, true);
latch.countDown();
} catch (KeeperException | InterruptedException e) {
throw new RuntimeException(e);
}
}
}, true);
zkClient.makePath("/collections/collection99/shards", true);
//wait until watch has been re-created
latch.await();
zkClient.makePath("collections/collection99/config=collection1", true);
zkClient.makePath("collections/collection99/config=collection3", true);
zkClient.makePath("/collections/collection97/shards", true);
// pause for the watches to fire
Thread.sleep(700);
if (cnt.intValue() < 2) {
// wait a bit more
Thread.sleep(4000);
}
if (cnt.intValue() < 2) {
// wait a bit more
Thread.sleep(4000);
}
assertEquals(2, cnt.intValue());
}
}
Aggregations