Search in sources :

Example 6 with PathChildrenCacheListener

use of org.apache.curator.framework.recipes.cache.PathChildrenCacheListener in project drill by apache.

the class TestEphemeralStore method testStoreRegistersDispatcherAndStartsItsClient.

/**
   * This test ensures store subscribes to receive events from underlying client. Dispatcher tests ensures listeners
   * are fired on incoming events. These two sets of tests ensure observer pattern in {@code TransientStore} works fine.
   */
@Test
public void testStoreRegistersDispatcherAndStartsItsClient() throws Exception {
    final StoreWithMockClient<String> store = new StoreWithMockClient<>(config, curator);
    final PathChildrenCache cache = Mockito.mock(PathChildrenCache.class);
    final ZookeeperClient client = store.getClient();
    Mockito.when(client.getCache()).thenReturn(cache);
    final ListenerContainer<PathChildrenCacheListener> container = Mockito.mock(ListenerContainer.class);
    Mockito.when(cache.getListenable()).thenReturn(container);
    store.start();
    Mockito.verify(container).addListener(store.dispatcher);
    Mockito.verify(client).start();
}
Also used : PathChildrenCacheListener(org.apache.curator.framework.recipes.cache.PathChildrenCacheListener) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) Test(org.junit.Test)

Example 7 with PathChildrenCacheListener

use of org.apache.curator.framework.recipes.cache.PathChildrenCacheListener in project exhibitor by soabase.

the class TestZookeeperConfigProvider method testConcurrentModification.

@Test
public void testConcurrentModification() throws Exception {
    ZookeeperConfigProvider config1 = new ZookeeperConfigProvider(client, "/foo", new Properties(), "foo");
    ZookeeperConfigProvider config2 = new ZookeeperConfigProvider(client, "/foo", new Properties(), "foo");
    try {
        config1.start();
        config2.start();
        final Semaphore cacheUpdate2 = new Semaphore(0);
        config2.getPathChildrenCache().getListenable().addListener(new PathChildrenCacheListener() {

            @Override
            public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
                cacheUpdate2.release();
            }
        });
        Properties properties = new Properties();
        properties.setProperty(PropertyBasedInstanceConfig.toName(StringConfigs.ZOO_CFG_EXTRA, PropertyBasedInstanceConfig.ROOT_PROPERTY_PREFIX), "1,2,3");
        LoadedInstanceConfig loaded1 = config1.storeConfig(new PropertyBasedInstanceConfig(properties, new Properties()), -1);
        Assert.assertTrue(timing.acquireSemaphore(cacheUpdate2));
        timing.sleepABit();
        LoadedInstanceConfig loaded2 = config2.loadConfig();
        Assert.assertEquals("1,2,3", loaded2.getConfig().getRootConfig().getString(StringConfigs.ZOO_CFG_EXTRA));
        properties.setProperty(PropertyBasedInstanceConfig.toName(StringConfigs.ZOO_CFG_EXTRA, PropertyBasedInstanceConfig.ROOT_PROPERTY_PREFIX), "4,5,6");
        config2.storeConfig(new PropertyBasedInstanceConfig(properties, new Properties()), loaded2.getVersion());
        Assert.assertNull(config1.storeConfig(new PropertyBasedInstanceConfig(properties, new Properties()), loaded1.getVersion()));
        LoadedInstanceConfig newLoaded1 = config1.loadConfig();
        Assert.assertNotEquals(loaded1.getVersion(), newLoaded1.getVersion());
    } finally {
        CloseableUtils.closeQuietly(config2);
        CloseableUtils.closeQuietly(config1);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) PathChildrenCacheListener(org.apache.curator.framework.recipes.cache.PathChildrenCacheListener) PathChildrenCacheEvent(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent) PropertyBasedInstanceConfig(com.netflix.exhibitor.core.config.PropertyBasedInstanceConfig) Semaphore(java.util.concurrent.Semaphore) Properties(java.util.Properties) LoadedInstanceConfig(com.netflix.exhibitor.core.config.LoadedInstanceConfig) Test(org.testng.annotations.Test)

Example 8 with PathChildrenCacheListener

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

the class ZKStreamMetadataStore method registerBucketOwnershipListener.

@Override
@SneakyThrows
public void registerBucketOwnershipListener(BucketOwnershipListener listener) {
    Preconditions.checkNotNull(listener);
    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()));
                listener.notify(new BucketNotification(bucketId, BucketNotification.NotificationType.BucketAvailable));
                break;
            case CONNECTION_LOST:
                listener.notify(new BucketNotification(Integer.MIN_VALUE, BucketNotification.NotificationType.ConnectivityError));
                break;
            default:
                log.warn("Received unknown event {}", event.getType());
        }
    };
    bucketOwnershipCacheRef.compareAndSet(null, new PathChildrenCache(storeHelper.getClient(), ZKStoreHelper.BUCKET_OWNERSHIP_PATH, true));
    bucketOwnershipCacheRef.get().getListenable().addListener(bucketListener);
    bucketOwnershipCacheRef.get().start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
    log.info("bucket ownership listener registered");
}
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) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) BucketNotification(io.pravega.controller.server.retention.BucketOwnershipListener.BucketNotification) SneakyThrows(lombok.SneakyThrows)

Example 9 with PathChildrenCacheListener

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

use of org.apache.curator.framework.recipes.cache.PathChildrenCacheListener in project druid by druid-io.

the class ZkCoordinator method start.

@LifecycleStart
public void start() throws IOException {
    synchronized (lock) {
        if (started) {
            return;
        }
        log.info("Starting zkCoordinator for server[%s]", me.getName());
        final String loadQueueLocation = ZKPaths.makePath(zkPaths.getLoadQueuePath(), me.getName());
        final String servedSegmentsLocation = ZKPaths.makePath(zkPaths.getServedSegmentsPath(), me.getName());
        final String liveSegmentsLocation = ZKPaths.makePath(zkPaths.getLiveSegmentsPath(), me.getName());
        loadQueueCache = new PathChildrenCache(curator, loadQueueLocation, true, true, Execs.multiThreaded(config.getNumLoadingThreads(), "ZkCoordinator-%s"));
        try {
            curator.newNamespaceAwareEnsurePath(loadQueueLocation).ensure(curator.getZookeeperClient());
            curator.newNamespaceAwareEnsurePath(servedSegmentsLocation).ensure(curator.getZookeeperClient());
            curator.newNamespaceAwareEnsurePath(liveSegmentsLocation).ensure(curator.getZookeeperClient());
            loadLocalCache();
            loadQueueCache.getListenable().addListener(new PathChildrenCacheListener() {

                @Override
                public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
                    final ChildData child = event.getData();
                    switch(event.getType()) {
                        case CHILD_ADDED:
                            final String path = child.getPath();
                            final DataSegmentChangeRequest request = jsonMapper.readValue(child.getData(), DataSegmentChangeRequest.class);
                            log.info("New request[%s] with zNode[%s].", request.asString(), path);
                            try {
                                request.go(getDataSegmentChangeHandler(), new DataSegmentChangeCallback() {

                                    boolean hasRun = false;

                                    @Override
                                    public void execute() {
                                        try {
                                            if (!hasRun) {
                                                curator.delete().guaranteed().forPath(path);
                                                log.info("Completed request [%s]", request.asString());
                                                hasRun = true;
                                            }
                                        } catch (Exception e) {
                                            try {
                                                curator.delete().guaranteed().forPath(path);
                                            } catch (Exception e1) {
                                                log.error(e1, "Failed to delete zNode[%s], but ignoring exception.", path);
                                            }
                                            log.error(e, "Exception while removing zNode[%s]", path);
                                            throw Throwables.propagate(e);
                                        }
                                    }
                                });
                            } catch (Exception e) {
                                try {
                                    curator.delete().guaranteed().forPath(path);
                                } catch (Exception e1) {
                                    log.error(e1, "Failed to delete zNode[%s], but ignoring exception.", path);
                                }
                                log.makeAlert(e, "Segment load/unload: uncaught exception.").addData("node", path).addData("nodeProperties", request).emit();
                            }
                            break;
                        case CHILD_REMOVED:
                            log.info("zNode[%s] was removed", event.getData().getPath());
                            break;
                        default:
                            log.info("Ignoring event[%s]", event);
                    }
                }
            });
            loadQueueCache.start();
        } catch (Exception e) {
            Throwables.propagateIfPossible(e, IOException.class);
            throw Throwables.propagate(e);
        }
        started = true;
    }
}
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) ChildData(org.apache.curator.framework.recipes.cache.ChildData) IOException(java.io.IOException) SegmentLoadingException(io.druid.segment.loading.SegmentLoadingException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) LifecycleStart(io.druid.java.util.common.lifecycle.LifecycleStart)

Aggregations

PathChildrenCacheListener (org.apache.curator.framework.recipes.cache.PathChildrenCacheListener)14 CuratorFramework (org.apache.curator.framework.CuratorFramework)13 PathChildrenCacheEvent (org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent)11 IOException (java.io.IOException)8 PathChildrenCache (org.apache.curator.framework.recipes.cache.PathChildrenCache)8 KeeperException (org.apache.zookeeper.KeeperException)4 Test (org.junit.Test)4 DataSegment (io.druid.timeline.DataSegment)3 List (java.util.List)3 ConcurrentMap (java.util.concurrent.ConcurrentMap)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 ZKPaths (org.apache.curator.utils.ZKPaths)3 Preconditions (com.google.common.base.Preconditions)2 RetentionPolicy (io.pravega.client.stream.RetentionPolicy)2 StreamImpl (io.pravega.client.stream.impl.StreamImpl)2 BucketChangeListener (io.pravega.controller.server.retention.BucketChangeListener)2 StreamNotification (io.pravega.controller.server.retention.BucketChangeListener.StreamNotification)2 NotificationType (io.pravega.controller.server.retention.BucketChangeListener.StreamNotification.NotificationType)2 BucketOwnershipListener (io.pravega.controller.server.retention.BucketOwnershipListener)2