Search in sources :

Example 16 with PathChildrenCacheEvent

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

Example 17 with PathChildrenCacheEvent

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

Example 18 with PathChildrenCacheEvent

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));
    }
}
Also used : PathChildrenCacheEvent(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent) TransientStoreEventType(org.apache.drill.exec.coord.store.TransientStoreEventType)

Example 19 with PathChildrenCacheEvent

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);
        }
    }
}
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) IOException(java.io.IOException)

Example 20 with PathChildrenCacheEvent

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);
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) ShardMeta(com.ctrip.xpipe.redis.core.entity.ShardMeta) ClusterMetaComparator(com.ctrip.xpipe.redis.core.meta.comparator.ClusterMetaComparator) java.util(java.util) KeeperElectorManager(com.ctrip.xpipe.redis.meta.server.keeper.KeeperElectorManager) Autowired(org.springframework.beans.factory.annotation.Autowired) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PathChildrenCacheListener(org.apache.curator.framework.recipes.cache.PathChildrenCacheListener) Releasable(com.ctrip.xpipe.api.lifecycle.Releasable) KeeperActiveElectAlgorithm(com.ctrip.xpipe.redis.meta.server.keeper.KeeperActiveElectAlgorithm) XpipeThreadFactory(com.ctrip.xpipe.utils.XpipeThreadFactory) StandardLockInternalsDriver(org.apache.curator.framework.recipes.locks.StandardLockInternalsDriver) Observer(com.ctrip.xpipe.api.observer.Observer) LockInternals(org.apache.curator.framework.recipes.locks.LockInternals) ClusterType(com.ctrip.xpipe.cluster.ClusterType) ZkClient(com.ctrip.xpipe.zk.ZkClient) JsonCodec(com.ctrip.xpipe.codec.JsonCodec) MetaZkConfig(com.ctrip.xpipe.redis.core.meta.MetaZkConfig) AbstractCurrentMetaObserver(com.ctrip.xpipe.redis.meta.server.keeper.impl.AbstractCurrentMetaObserver) ReentrantLock(java.util.concurrent.locks.ReentrantLock) TopElement(com.ctrip.xpipe.api.lifecycle.TopElement) ClusterMeta(com.ctrip.xpipe.redis.core.entity.ClusterMeta) ZkUtils(com.ctrip.xpipe.zk.ZkUtils) KeeperActiveElectAlgorithmManager(com.ctrip.xpipe.redis.meta.server.keeper.KeeperActiveElectAlgorithmManager) TimeUnit(java.util.concurrent.TimeUnit) ChildData(org.apache.curator.framework.recipes.cache.ChildData) Component(org.springframework.stereotype.Component) CuratorFramework(org.apache.curator.framework.CuratorFramework) KeeperMeta(com.ctrip.xpipe.redis.core.entity.KeeperMeta) PathChildrenCacheEvent(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent) LockInternalsSorter(org.apache.curator.framework.recipes.locks.LockInternalsSorter) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) 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) Releasable(com.ctrip.xpipe.api.lifecycle.Releasable)

Aggregations

PathChildrenCacheEvent (org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent)65 CuratorFramework (org.apache.curator.framework.CuratorFramework)55 PathChildrenCacheListener (org.apache.curator.framework.recipes.cache.PathChildrenCacheListener)49 PathChildrenCache (org.apache.curator.framework.recipes.cache.PathChildrenCache)34 ChildData (org.apache.curator.framework.recipes.cache.ChildData)20 IOException (java.io.IOException)15 KeeperException (org.apache.zookeeper.KeeperException)11 CountDownLatch (java.util.concurrent.CountDownLatch)10 Test (org.junit.Test)10 DataSegment (org.apache.druid.timeline.DataSegment)5 ZKPaths (org.apache.curator.utils.ZKPaths)4 RedisConfig (cn.northpark.zookeeper.RedisConfig)3 SofaRpcRuntimeException (com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)3 ServiceInstance (com.baidu.brpc.client.channel.ServiceInstance)3 RpcException (com.baidu.brpc.exceptions.RpcException)3 DataSegment (io.druid.timeline.DataSegment)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Releasable (com.ctrip.xpipe.api.lifecycle.Releasable)2