Search in sources :

Example 6 with PathChildrenCache

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

the class ZKStreamMetadataStore method unregisterBucketListener.

@Override
public void unregisterBucketListener(int bucket) {
    PathChildrenCache cache = bucketCacheMap.remove(bucket);
    if (cache != null) {
        try {
            cache.clear();
            cache.close();
        } catch (IOException e) {
            log.warn("unable to close watch on bucket {} with exception {}", bucket, e);
        }
    }
}
Also used : PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) IOException(java.io.IOException)

Example 7 with PathChildrenCache

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

the class ZKStreamMetadataStore method registerBucketChangeListener.

@Override
@SneakyThrows
public void registerBucketChangeListener(int bucket, BucketChangeListener listener) {
    Preconditions.checkNotNull(listener);
    PathChildrenCacheListener bucketListener = (client, event) -> {
        StreamImpl stream;
        switch(event.getType()) {
            case CHILD_ADDED:
                stream = getStreamFromPath(event.getData().getPath());
                listener.notify(new StreamNotification(stream.getScope(), stream.getStreamName(), NotificationType.StreamAdded));
                break;
            case CHILD_REMOVED:
                stream = getStreamFromPath(event.getData().getPath());
                listener.notify(new StreamNotification(stream.getScope(), stream.getStreamName(), NotificationType.StreamRemoved));
                break;
            case CHILD_UPDATED:
                stream = getStreamFromPath(event.getData().getPath());
                listener.notify(new StreamNotification(stream.getScope(), stream.getStreamName(), NotificationType.StreamUpdated));
                break;
            case CONNECTION_LOST:
                listener.notify(new StreamNotification(null, null, NotificationType.ConnectivityError));
                break;
            default:
                log.warn("Received unknown event {} on bucket", event.getType(), bucket);
        }
    };
    String bucketRoot = String.format(ZKStoreHelper.BUCKET_PATH, bucket);
    bucketCacheMap.put(bucket, new PathChildrenCache(storeHelper.getClient(), bucketRoot, true));
    PathChildrenCache pathChildrenCache = bucketCacheMap.get(bucket);
    pathChildrenCache.getListenable().addListener(bucketListener);
    pathChildrenCache.start(PathChildrenCache.StartMode.NORMAL);
    log.info("bucket {} change notification listener registered", bucket);
}
Also used : SneakyThrows(lombok.SneakyThrows) StreamImpl(io.pravega.client.stream.impl.StreamImpl) RetentionPolicy(io.pravega.client.stream.RetentionPolicy) SerializationUtils(org.apache.commons.lang3.SerializationUtils) CompletableFuture(java.util.concurrent.CompletableFuture) StreamNotification(io.pravega.controller.server.retention.BucketChangeListener.StreamNotification) PathChildrenCacheListener(org.apache.curator.framework.recipes.cache.PathChildrenCacheListener) AtomicReference(java.util.concurrent.atomic.AtomicReference) ConcurrentMap(java.util.concurrent.ConcurrentMap) BucketOwnershipListener(io.pravega.controller.server.retention.BucketOwnershipListener) ZKPaths(org.apache.curator.utils.ZKPaths) BucketNotification(io.pravega.controller.server.retention.BucketOwnershipListener.BucketNotification) Data(io.pravega.controller.store.stream.tables.Data) NotificationType(io.pravega.controller.server.retention.BucketChangeListener.StreamNotification.NotificationType) BucketChangeListener(io.pravega.controller.server.retention.BucketChangeListener) Executor(java.util.concurrent.Executor) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) Collectors(java.util.stream.Collectors) Slf4j(lombok.extern.slf4j.Slf4j) Base64(java.util.Base64) List(java.util.List) CuratorFramework(org.apache.curator.framework.CuratorFramework) Config(io.pravega.controller.util.Config) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) Preconditions(com.google.common.base.Preconditions) ZKHostIndex(io.pravega.controller.store.index.ZKHostIndex) PathChildrenCacheListener(org.apache.curator.framework.recipes.cache.PathChildrenCacheListener) StreamImpl(io.pravega.client.stream.impl.StreamImpl) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) StreamNotification(io.pravega.controller.server.retention.BucketChangeListener.StreamNotification) SneakyThrows(lombok.SneakyThrows)

Example 8 with PathChildrenCache

use of org.apache.curator.framework.recipes.cache.PathChildrenCache 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 9 with PathChildrenCache

use of org.apache.curator.framework.recipes.cache.PathChildrenCache in project Mycat_plus by coderczp.

the class ZktoXmlMain method runCommandWatch.

/**
 * 进行命令的监听操作
 * 方法描述
 * @param zkConn zk的连接信息
 * @param path 路径信息
 * @param ZKLISTENER 监控路径信息
 * @throws Exception
 * @创建日期 2016年9月20日
 */
@SuppressWarnings("resource")
private static void runCommandWatch(final CuratorFramework zkConn, final String path) throws Exception {
    PathChildrenCache children = new PathChildrenCache(zkConn, path, true);
    CommandPathListener commandListener = new CommandPathListener();
    // 移除原来的监听再进行添加
    children.getListenable().addListener(commandListener);
    children.start();
}
Also used : PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) CommandPathListener(io.mycat.config.loader.zkprocess.zktoxml.command.CommandPathListener)

Example 10 with PathChildrenCache

use of org.apache.curator.framework.recipes.cache.PathChildrenCache in project Mycat_plus by coderczp.

the class ZKUtils method addChildPathCache.

public static void addChildPathCache(String path, PathChildrenCacheListener listener) {
    NameableExecutor businessExecutor = MycatServer.getInstance().getBusinessExecutor();
    ExecutorService executor = businessExecutor == null ? Executors.newFixedThreadPool(5) : businessExecutor;
    try {
        /**
         * 监听子节点的变化情况
         */
        final PathChildrenCache childrenCache = new PathChildrenCache(getConnection(), path, true);
        childrenCache.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT);
        childrenCache.getListenable().addListener(listener, executor);
        watchMap.put(path, childrenCache);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) IOException(java.io.IOException)

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