use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.PathChildrenCache in project turbo-rpc by hank-whu.
the class ZooKeeperDiscover method close.
@Override
public void close() throws IOException {
for (int i = 0; i < watchers.size(); i++) {
PathChildrenCache watcher = watchers.get(i);
try {
watcher.close();
} catch (Exception e) {
if (logger.isErrorEnabled()) {
logger.error("watcher关闭失败 ", e);
}
}
}
watchers = null;
client.close();
client = null;
}
use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.PathChildrenCache in project turbo-rpc by hank-whu.
the class ZooKeeperDiscover method addListener.
@Override
public void addListener(String group, String app, Protocol protocol, final DiscoverListener listener) {
Objects.requireNonNull(listener, "listener is null");
Objects.requireNonNull(client, "call init first");
final String path = "/turbo/" + group + "/" + app + "/" + protocol;
final PathChildrenCache watcher = new PathChildrenCache(client, path, true);
PathChildrenCacheListener pathChildrenCacheListener = new PathChildrenCacheListener() {
private final ConcurrentMap<HostPort, Integer> serverWithWeight = new ConcurrentHashMap<>();
private volatile boolean waitForInitializedEvent = true;
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
if (logger.isInfoEnabled()) {
logger.info("zk监控列表发生变化, " + path + ", " + event.getType());
}
boolean isChanged = true;
switch(event.getType()) {
case INITIALIZED:
waitForInitializedEvent = false;
if (logger.isInfoEnabled()) {
logger.info("完成初始化: " + path);
}
break;
case CHILD_ADDED:
{
AddressWithWeight kv = new AddressWithWeight(event.getData().getData());
serverWithWeight.put(kv.address, kv.weight);
if (logger.isInfoEnabled()) {
logger.info("新增节点: " + kv);
}
break;
}
case CHILD_REMOVED:
{
AddressWithWeight kv = new AddressWithWeight(event.getData().getData());
serverWithWeight.remove(kv.address);
if (logger.isInfoEnabled()) {
logger.info("删除节点: " + kv);
}
break;
}
case CHILD_UPDATED:
{
AddressWithWeight kv = new AddressWithWeight(event.getData().getData());
serverWithWeight.put(kv.address, kv.weight);
if (logger.isInfoEnabled()) {
logger.info("更新节点: " + kv);
}
break;
}
default:
isChanged = false;
if (logger.isInfoEnabled()) {
logger.info("忽略, " + path + ", " + event.getType());
}
}
if (!waitForInitializedEvent && isChanged) {
try {
listener.onChange(serverWithWeight);
} catch (Throwable t) {
if (logger.isWarnEnabled()) {
logger.warn("Discover监听处理失败", t);
}
}
}
}
};
watcher.getListenable().addListener(pathChildrenCacheListener);
try {
watcher.start(StartMode.POST_INITIALIZED_EVENT);
watchers.add(watcher);
} catch (Exception e) {
if (logger.isErrorEnabled()) {
logger.error("zk监听失败, " + path, e);
}
}
}
use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.PathChildrenCache in project twister2 by DSC-SPIDAL.
the class ZKController method initialize.
/**
* connect to the server
* get a workerID for this worker
* create an ephemeral znode for this client
* @return
*/
public boolean initialize() {
String zkServerAddress = ZKContext.zooKeeperServerIP(config);
String zkServerPort = ZKContext.zooKeeperServerPort(config);
zkAddress = zkServerAddress + ":" + zkServerPort;
try {
client = CuratorFrameworkFactory.newClient(zkAddress, new ExponentialBackoffRetry(1000, 3));
client.start();
dai = new DistributedAtomicInteger(client, daiPath, new ExponentialBackoffRetry(1000, 3));
// if the job node does not exists, it means that this is not rejoining
if (client.checkExists().forPath(jobPath) == null) {
int workerID = createWorkerID();
workerNetworkInfo = new WorkerNetworkInfo(hostAndPort, workerID);
createWorkerZnode();
appendWorkerInfo();
// if the parent exists, check whether this worker joined the job before
// whether it is coming from a failure
} else {
byte[] parentData = client.getData().forPath(jobPath);
String parentStr = new String(parentData);
if (parentStr.indexOf(hostAndPort) < 0) {
int workerID = createWorkerID();
workerNetworkInfo = new WorkerNetworkInfo(hostAndPort, workerID);
createWorkerZnode();
appendWorkerInfo();
// if this worker is coming from a failure, get the ID from the parent content
} else {
int workerID = getWorkerIDFromParentData(parentStr);
workerNetworkInfo = new WorkerNetworkInfo(hostAndPort, workerID);
createWorkerZnode();
}
}
// We childrenCache children data for parent path.
// So we will listen for all workers in the job
childrenCache = new PathChildrenCache(client, jobPath, true);
childrenCache.start();
LOG.log(Level.INFO, "This worker: " + workerNetworkInfo + " initialized successfully.");
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.PathChildrenCache in project dble by actiontech.
the class ZKUtils method addViewPathCache.
public static void addViewPathCache(String path, PathChildrenCacheListener listener) {
try {
// watch the child status
final PathChildrenCache childrenCache = new PathChildrenCache(getConnection(), path, true);
childrenCache.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT);
childrenCache.getListenable().addListener(listener);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.PathChildrenCache in project druid by druid-io.
the class RemoteTaskRunner method addWorker.
/**
* When a new worker appears, listeners are registered for status changes associated with tasks assigned to
* the worker. Status changes indicate the creation or completion of a task.
* The RemoteTaskRunner updates state according to these changes.
*
* @param worker contains metadata for a worker that has appeared in ZK
* @return future that will contain a fully initialized worker
*/
private ListenableFuture<ZkWorker> addWorker(final Worker worker) {
log.info("Worker[%s] reportin' for duty!", worker.getHost());
try {
cancelWorkerCleanup(worker.getHost());
final String workerStatusPath = JOINER.join(indexerZkConfig.getStatusPath(), worker.getHost());
final PathChildrenCache statusCache = workerStatusPathChildrenCacheFactory.make(cf, workerStatusPath);
final SettableFuture<ZkWorker> retVal = SettableFuture.create();
final ZkWorker zkWorker = new ZkWorker(worker, statusCache, jsonMapper);
// Add status listener to the watcher for status changes
zkWorker.addListener(getStatusListener(worker, zkWorker, retVal));
zkWorker.start();
return retVal;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Aggregations