Search in sources :

Example 31 with PathChildrenCache

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

the class HS2ActivePassiveHARegistry method populateCache.

private void populateCache() throws IOException {
    PathChildrenCache pcc = ensureInstancesCache(0);
    populateCache(pcc, false);
}
Also used : PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache)

Example 32 with PathChildrenCache

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

the class ZookeeperNodeListener method init.

/**
 * Init a PathChildrenCache to watch the given path.
 */
@Override
public void init() {
    checkParameters();
    super.init();
    if (client == null) {
        client = CuratorFrameworkFactory.builder().canBeReadOnly(true).connectionTimeoutMs(5000).connectString(zkConnectString).retryPolicy(new RetryForever(10000)).sessionTimeoutMs(30000).build();
        client.start();
        privateZkClient = true;
    }
    cache = new PathChildrenCache(client, path, true);
    cache.getListenable().addListener(new PathChildrenCacheListener() {

        @Override
        public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
            try {
                LOG.info("Receive an event: " + event.getType());
                lock.lock();
                PathChildrenCacheEvent.Type eventType = event.getType();
                switch(eventType) {
                    case CHILD_REMOVED:
                        updateSingleNode(event, NodeEventTypeEnum.DELETE);
                        break;
                    case CHILD_ADDED:
                        updateSingleNode(event, NodeEventTypeEnum.ADD);
                        break;
                    case CONNECTION_RECONNECTED:
                        refreshAllNodes();
                        break;
                    default:
                        // CHILD_UPDATED
                        // INITIALIZED
                        // CONNECTION_LOST
                        // CONNECTION_SUSPENDED
                        LOG.info("Received a PathChildrenCacheEvent, IGNORE it: " + event);
                }
            } finally {
                lock.unlock();
                LOG.info("Finish the processing of event: " + event.getType());
            }
        }
    });
    try {
        // Use BUILD_INITIAL_CACHE to force build cache in the current Thread.
        // We don't use POST_INITIALIZED_EVENT, so there's no INITIALIZED event.
        cache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
    } catch (Exception e) {
        LOG.error("Can't start PathChildrenCache", 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) RetryForever(org.apache.curator.retry.RetryForever) IOException(java.io.IOException) DruidRuntimeException(com.alibaba.druid.DruidRuntimeException)

Example 33 with PathChildrenCache

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

the class ZooKeeperBucketService method startBucketChangeListener.

@Override
public void startBucketChangeListener() {
    PathChildrenCacheListener bucketListener = (client, event) -> {
        StreamImpl stream;
        switch(event.getType()) {
            case CHILD_ADDED:
            case CHILD_UPDATED:
                stream = bucketStore.getStreamFromPath(event.getData().getPath());
                notify(new StreamNotification(stream.getScope(), stream.getStreamName(), NotificationType.StreamAdded));
                break;
            case CHILD_REMOVED:
                stream = bucketStore.getStreamFromPath(event.getData().getPath());
                notify(new StreamNotification(stream.getScope(), stream.getStreamName(), NotificationType.StreamRemoved));
                break;
            case CONNECTION_LOST:
                notify(new StreamNotification(null, null, NotificationType.ConnectivityError));
                break;
            default:
                log.warn("Received unknown event {} on bucket", event.getType(), getBucketId());
        }
    };
    PathChildrenCache pathChildrenCache = cacheRef.updateAndGet(existing -> {
        if (existing == null) {
            PathChildrenCache cache = bucketStore.getBucketPathChildrenCache(getServiceType(), getBucketId());
            cache.getListenable().addListener(bucketListener);
            log.info("bucket {} change notification listener registered", getBucketId());
            return cache;
        } else {
            return existing;
        }
    });
    try {
        pathChildrenCache.start(PathChildrenCache.StartMode.NORMAL);
    } catch (Exception e) {
        log.error("{}: Starting listener on bucket {} threw exception", getServiceType(), getBucketId(), e);
        throw Exceptions.sneakyThrow(e);
    }
}
Also used : BucketStore(io.pravega.controller.store.stream.BucketStore) Slf4j(lombok.extern.slf4j.Slf4j) StreamImpl(io.pravega.client.stream.impl.StreamImpl) ZookeeperBucketStore(io.pravega.controller.store.stream.ZookeeperBucketStore) Duration(java.time.Duration) Exceptions(io.pravega.common.Exceptions) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) IOException(java.io.IOException) PathChildrenCacheListener(org.apache.curator.framework.recipes.cache.PathChildrenCacheListener) AtomicReference(java.util.concurrent.atomic.AtomicReference) PathChildrenCacheListener(org.apache.curator.framework.recipes.cache.PathChildrenCacheListener) StreamImpl(io.pravega.client.stream.impl.StreamImpl) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) IOException(java.io.IOException)

Example 34 with PathChildrenCache

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

the class ZooKeeperBucketManager method stopBucketOwnershipListener.

@Override
public void stopBucketOwnershipListener() {
    PathChildrenCache pathChildrenCache = bucketOwnershipCacheMap.remove(getServiceType());
    if (pathChildrenCache != null) {
        try {
            pathChildrenCache.clear();
            pathChildrenCache.close();
        } catch (IOException e) {
            log.warn("unable to close listener for bucket ownership", e);
        }
    }
}
Also used : PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) IOException(java.io.IOException)

Example 35 with PathChildrenCache

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

the class ZooKeeperBucketManager method startBucketOwnershipListener.

@Override
public void startBucketOwnershipListener() {
    PathChildrenCache pathChildrenCache = bucketOwnershipCacheMap.computeIfAbsent(getServiceType(), x -> bucketStore.getServiceOwnershipPathChildrenCache(getServiceType()));
    PathChildrenCacheListener bucketListener = (client, event) -> {
        switch(event.getType()) {
            case CHILD_ADDED:
                // no action required
                break;
            case CHILD_REMOVED:
                int bucketId = Integer.parseInt(ZKPaths.getNodeFromPath(event.getData().getPath()));
                RetryHelper.withIndefiniteRetriesAsync(() -> tryTakeOwnership(bucketId), e -> log.warn("{}: exception while attempting to take ownership for bucket {}: {}", getServiceType(), bucketId, e.getMessage()), getExecutor());
                break;
            case CONNECTION_LOST:
                log.warn("{}: Received connectivity error", getServiceType());
                break;
            default:
                log.warn("Received unknown event {} on bucket root {} ", event.getType(), getServiceType());
        }
    };
    pathChildrenCache.getListenable().addListener(bucketListener);
    log.info("bucket ownership listener registered on bucket root {}", getServiceType());
    try {
        pathChildrenCache.start(PathChildrenCache.StartMode.NORMAL);
    } catch (Exception e) {
        log.error("Starting ownership listener for service {} threw exception", getServiceType(), e);
        throw Exceptions.sneakyThrow(e);
    }
}
Also used : Executor(java.util.concurrent.Executor) Exceptions(io.pravega.common.Exceptions) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IOException(java.io.IOException) CompletableFuture(java.util.concurrent.CompletableFuture) PathChildrenCacheListener(org.apache.curator.framework.recipes.cache.PathChildrenCacheListener) Function(java.util.function.Function) ConcurrentMap(java.util.concurrent.ConcurrentMap) BucketStore(io.pravega.controller.store.stream.BucketStore) Slf4j(lombok.extern.slf4j.Slf4j) ZKPaths(org.apache.curator.utils.ZKPaths) ZookeeperBucketStore(io.pravega.controller.store.stream.ZookeeperBucketStore) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) Preconditions(com.google.common.base.Preconditions) RetryHelper(io.pravega.controller.util.RetryHelper) PathChildrenCacheListener(org.apache.curator.framework.recipes.cache.PathChildrenCacheListener) 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