Search in sources :

Example 46 with PathChildrenCache

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;
}
Also used : PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) IOException(java.io.IOException)

Example 47 with PathChildrenCache

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);
        }
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) PathChildrenCacheListener(org.apache.curator.framework.recipes.cache.PathChildrenCacheListener) PathChildrenCacheEvent(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) ConcurrentMap(java.util.concurrent.ConcurrentMap) AddressWithWeight(rpc.turbo.config.AddressWithWeight) IOException(java.io.IOException)

Example 48 with PathChildrenCache

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;
    }
}
Also used : DistributedAtomicInteger(org.apache.curator.framework.recipes.atomic.DistributedAtomicInteger) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache)

Example 49 with PathChildrenCache

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);
    }
}
Also used : PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache)

Example 50 with PathChildrenCache

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);
    }
}
Also used : PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) TimeoutException(java.util.concurrent.TimeoutException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

PathChildrenCache (org.apache.curator.framework.recipes.cache.PathChildrenCache)58 IOException (java.io.IOException)22 CuratorFramework (org.apache.curator.framework.CuratorFramework)20 PathChildrenCacheListener (org.apache.curator.framework.recipes.cache.PathChildrenCacheListener)17 PathChildrenCacheEvent (org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent)13 KeeperException (org.apache.zookeeper.KeeperException)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 ChildData (org.apache.curator.framework.recipes.cache.ChildData)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 ExponentialBackoffRetry (org.apache.curator.retry.ExponentialBackoffRetry)6 Before (org.junit.Before)5 Test (org.junit.Test)5 ExecutorService (java.util.concurrent.ExecutorService)4 ConcurrentMap (java.util.concurrent.ConcurrentMap)3 ExecutionException (java.util.concurrent.ExecutionException)3 ZKPaths (org.apache.curator.utils.ZKPaths)3 Duration (org.joda.time.Duration)3 Preconditions (com.google.common.base.Preconditions)2 MapMaker (com.google.common.collect.MapMaker)2 CommandPathListener (io.mycat.config.loader.zkprocess.zktoxml.command.CommandPathListener)2