Search in sources :

Example 46 with PathChildrenCache

use of org.apache.curator.framework.recipes.cache.PathChildrenCache in project xian by happyyangyuan.

the class PathCacheExample method main.

public static void main(String[] args) throws Exception {
    TestingServer server = new TestingServer();
    CuratorFramework client = null;
    PathChildrenCache cache = null;
    try {
        client = CuratorFrameworkFactory.newClient(server.getConnectString(), new ExponentialBackoffRetry(1000, 3));
        client.start();
        // in this example we will cache data. Notice that this is optional.
        cache = new PathChildrenCache(client, PATH, true);
        cache.start();
        processCommands(client, cache);
    } finally {
        CloseableUtils.closeQuietly(cache);
        CloseableUtils.closeQuietly(client);
        CloseableUtils.closeQuietly(server);
    }
}
Also used : TestingServer(org.apache.curator.test.TestingServer) CuratorFramework(org.apache.curator.framework.CuratorFramework) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry)

Example 47 with PathChildrenCache

use of org.apache.curator.framework.recipes.cache.PathChildrenCache in project x-pipe by ctripcorp.

the class AbstractClusterServers method doStart.

@Override
protected void doStart() throws Exception {
    CuratorFramework client = zkClient.get();
    serversCache = new PathChildrenCache(client, MetaZkConfig.getMetaServerRegisterPath(), true, XpipeThreadFactory.create(String.format("PathChildrenCache(%d)", currentServer.getServerId())));
    serversCache.getListenable().addListener(new ChildrenChanged());
    serversCache.start();
    future = scheduled.scheduleWithFixedDelay(new AbstractExceptionLogTask() {

        @Override
        public void doRun() {
            try {
                childrenChanged();
            } catch (Throwable th) {
                logger.error("[doStart]", th);
            }
        }
    }, 1000, metaServerConfig.getClusterServersRefreshMilli(), TimeUnit.MILLISECONDS);
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) AbstractExceptionLogTask(com.ctrip.xpipe.concurrent.AbstractExceptionLogTask)

Example 48 with PathChildrenCache

use of 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 49 with PathChildrenCache

use of 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 50 with PathChildrenCache

use of 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)

Aggregations

PathChildrenCache (org.apache.curator.framework.recipes.cache.PathChildrenCache)68 IOException (java.io.IOException)25 CuratorFramework (org.apache.curator.framework.CuratorFramework)21 PathChildrenCacheListener (org.apache.curator.framework.recipes.cache.PathChildrenCacheListener)20 PathChildrenCacheEvent (org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent)14 KeeperException (org.apache.zookeeper.KeeperException)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)8 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 ConcurrentMap (java.util.concurrent.ConcurrentMap)4 ExecutorService (java.util.concurrent.ExecutorService)4 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)4 Slf4j (lombok.extern.slf4j.Slf4j)4 ZKPaths (org.apache.curator.utils.ZKPaths)4 Preconditions (com.google.common.base.Preconditions)3 StreamImpl (io.pravega.client.stream.impl.StreamImpl)3 CompletableFuture (java.util.concurrent.CompletableFuture)3