use of org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent in project twister2 by DSC-SPIDAL.
the class ZKMasterController method addPersChildrenCacheListener.
/**
* create the listener for persistent worker znodes to determine worker status changes
*/
private void addPersChildrenCacheListener(PathChildrenCache cache) {
PathChildrenCacheListener listener = new PathChildrenCacheListener() {
public void childEvent(CuratorFramework clientOfEvent, PathChildrenCacheEvent event) {
switch(event.getType()) {
case CHILD_UPDATED:
childZnodeUpdated(event);
break;
default:
}
}
};
cache.getListenable().addListener(listener);
}
use of org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent in project twister2 by DSC-SPIDAL.
the class ZKBarrierHandler method addBarrierChildrenCacheListener.
/**
* create the listener for barrier event znodes in the barrier directory
* to determine whether all workers arrived on the barrier
*/
private void addBarrierChildrenCacheListener(PathChildrenCache cache, JobMasterAPI.BarrierType barrierType) {
PathChildrenCacheListener listener = new PathChildrenCacheListener() {
public void childEvent(CuratorFramework clientOfEvent, PathChildrenCacheEvent event) {
String childPath = event.getData().getPath();
int workerID = ZKUtils.getWorkerIDFromPersPath(childPath);
long timeout = Longs.fromByteArray(event.getData().getData());
switch(event.getType()) {
case CHILD_ADDED:
if (barrierType == JobMasterAPI.BarrierType.DEFAULT) {
barrierMonitor.arrivedAtDefault(workerID, timeout);
} else if (barrierType == JobMasterAPI.BarrierType.INIT) {
barrierMonitor.arrivedAtInit(workerID, timeout);
}
break;
case CHILD_REMOVED:
if (barrierType == JobMasterAPI.BarrierType.DEFAULT) {
barrierMonitor.removedFromDefault(workerID);
} else if (barrierType == JobMasterAPI.BarrierType.INIT) {
barrierMonitor.removedFromInit(workerID);
}
break;
default:
}
}
};
cache.getListenable().addListener(listener);
}
use of org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent in project drill by axbaretto.
the class EventDispatcher method childEvent.
@Override
public void childEvent(final CuratorFramework client, final PathChildrenCacheEvent event) throws Exception {
final PathChildrenCacheEvent.Type original = event.getType();
final TransientStoreEventType mapped = MAPPINGS.get(original);
if (mapped != null) {
// dispatch the event to listeners only if it can be mapped
final String path = event.getData().getPath();
final byte[] bytes = event.getData().getData();
final V value = store.getConfig().getSerializer().deserialize(bytes);
store.fireListeners(TransientStoreEvent.of(mapped, path, value));
}
}
use of org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent in project turbo-rpc by hank-whu.
the class ZooKeeperRegister method addRegisterWatcher.
/**
* 当断掉连接又重新连接上时起作用
*
* @param group
* @param app
* @param protocol
* @param serverAddress
* @param serverWeight
*/
private synchronized void addRegisterWatcher(String group, String app, Protocol protocol, HostPort serverAddress, int serverWeight) {
final String path = "/turbo/" + group + "/" + app + "/" + protocol + "/" + HexUtils.toHex(serverAddress.toString().getBytes(StandardCharsets.UTF_8));
if (watcherMap.containsKey(path)) {
return;
}
PathChildrenCache watcher = new PathChildrenCache(client, path, false);
PathChildrenCacheListener pathChildrenCacheListener = new PathChildrenCacheListener() {
private volatile boolean waitForInitializedEvent = true;
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
switch(event.getType()) {
case INITIALIZED:
waitForInitializedEvent = false;
break;
case CONNECTION_RECONNECTED:
if (waitForInitializedEvent) {
return;
}
if (logger.isInfoEnabled()) {
logger.info("获得zk连接尝试重新注册, " + path + ", " + serverAddress + "@" + serverWeight);
}
ZooKeeperRegister.this.register(group, app, protocol, serverAddress, serverWeight);
break;
default:
break;
}
}
};
watcher.getListenable().addListener(pathChildrenCacheListener);
try {
watcher.start(StartMode.POST_INITIALIZED_EVENT);
watcherMap.put(path, watcher);
} catch (Exception e) {
if (logger.isErrorEnabled()) {
logger.error("zk监听失败, " + path, e);
}
}
}
use of org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent in project x-pipe by ctripcorp.
the class DefaultKeeperElectorManager method observerShardLeader.
protected void observerShardLeader(final Long clusterDbId, final Long shardDbId) {
logger.info("[observerShardLeader]cluster_{},shard_{}", clusterDbId, shardDbId);
final CuratorFramework client = zkClient.get();
if (currentMetaManager.watchIfNotWatched(clusterDbId, shardDbId)) {
try {
List<PathChildrenCache> pathChildrenCaches = new ArrayList<>();
pathChildrenCaches.add(buildPathChildrenCacheByDbId(clusterDbId, shardDbId, client));
ReentrantLock lock = new ReentrantLock();
pathChildrenCaches.forEach(pathChildrenCache -> {
pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {
private AtomicBoolean isFirst = new AtomicBoolean(true);
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
if (isFirst.get()) {
isFirst.set(false);
try {
logger.info("[childEvent][first sleep]{}", FIRST_PATH_CHILDREN_CACHE_SLEEP_MILLI);
TimeUnit.MILLISECONDS.sleep(FIRST_PATH_CHILDREN_CACHE_SLEEP_MILLI);
} catch (Exception e) {
logger.error("[childEvent]", e);
}
}
try {
lock.lock();
logger.info("[childEvent]cluster_{}, shard_{}, {}, {}", clusterDbId, shardDbId, event.getType(), ZkUtils.toString(event.getData()));
updateShardLeader(aggregateChildData(pathChildrenCaches), clusterDbId, shardDbId);
} finally {
lock.unlock();
}
}
});
try {
pathChildrenCache.start();
} catch (Exception e) {
logger.info("[observerShardLeader][cluster{}][shard_{}] start cache fail {}", clusterDbId, shardDbId, pathChildrenCache, e);
}
});
registerJob(clusterDbId, shardDbId, new Releasable() {
@Override
public void release() throws Exception {
pathChildrenCaches.forEach(pathChildrenCache -> {
try {
logger.info("[release][release children cache]cluster_{}, shard_{}", clusterDbId, shardDbId);
pathChildrenCache.close();
} catch (Exception e) {
logger.error(String.format("[release][release children cache]cluster_%s, shard_%s", clusterDbId, shardDbId), e);
}
});
}
});
} catch (Exception e) {
logger.error("[observerShardLeader]cluster_" + clusterDbId + " shard_" + shardDbId, e);
}
} else {
logger.warn("[observerShardLeader][already watched]cluster_{}, shard_{}", clusterDbId, shardDbId);
}
}
Aggregations