use of org.apache.zookeeper.WatchedEvent 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.WatchedEvent 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.WatchedEvent 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.WatchedEvent 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());
}
}
use of org.apache.zookeeper.WatchedEvent in project lucene-solr by apache.
the class ZkStateReader method createClusterStateWatchersAndUpdate.
public synchronized void createClusterStateWatchersAndUpdate() throws KeeperException, InterruptedException {
// We need to fetch the current cluster state and the set of live nodes
LOG.debug("Updating cluster state from ZooKeeper... ");
// Sanity check ZK structure.
if (!zkClient.exists(CLUSTER_STATE, true)) {
throw new SolrException(ErrorCode.SERVICE_UNAVAILABLE, "Cannot connect to cluster at " + zkClient.getZkServerAddress() + ": cluster not found/not ready");
}
// on reconnect of SolrZkClient force refresh and re-add watches.
loadClusterProperties();
refreshLiveNodes(new LiveNodeWatcher());
refreshLegacyClusterState(new LegacyClusterStateWatcher());
refreshStateFormat2Collections();
refreshCollectionList(new CollectionsChildWatcher());
synchronized (ZkStateReader.this.getUpdateLock()) {
constructState(Collections.emptySet());
zkClient.exists(ALIASES, new Watcher() {
@Override
public void process(WatchedEvent event) {
// session events are not change events, and do not remove the watcher
if (EventType.None.equals(event.getType())) {
return;
}
try {
synchronized (ZkStateReader.this.getUpdateLock()) {
LOG.debug("Updating aliases... ");
// remake watch
final Watcher thisWatch = this;
final Stat stat = new Stat();
final byte[] data = zkClient.getData(ALIASES, thisWatch, stat, true);
ZkStateReader.this.aliases = ClusterState.load(data);
LOG.debug("New alias definition is: " + ZkStateReader.this.aliases.toString());
}
} catch (KeeperException.ConnectionLossException | KeeperException.SessionExpiredException e) {
LOG.warn("ZooKeeper watch triggered, but Solr cannot talk to ZK: [{}]", e.getMessage());
} catch (KeeperException e) {
LOG.error("A ZK error has occurred", e);
throw new ZooKeeperException(SolrException.ErrorCode.SERVER_ERROR, "A ZK error has occurred", e);
} catch (InterruptedException e) {
// Restore the interrupted status
Thread.currentThread().interrupt();
LOG.warn("Interrupted", e);
}
}
}, true);
}
updateAliases();
if (securityNodeListener != null) {
addSecuritynodeWatcher(pair -> {
ConfigData cd = new ConfigData();
cd.data = pair.first() == null || pair.first().length == 0 ? EMPTY_MAP : Utils.getDeepCopy((Map) fromJSON(pair.first()), 4, false);
cd.version = pair.second() == null ? -1 : pair.second().getVersion();
securityData = cd;
securityNodeListener.run();
});
securityData = getSecurityProps(true);
}
}
Aggregations