Search in sources :

Example 26 with PathChildrenCache

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

the class RemoteTaskRunnerTest method testStatusListenerEventDataNullShouldNotThrowException.

@Test
public void testStatusListenerEventDataNullShouldNotThrowException() throws Exception {
    // Set up mock emitter to verify log alert when exception is thrown inside the status listener
    Worker worker = EasyMock.createMock(Worker.class);
    EasyMock.expect(worker.getHost()).andReturn("host").atLeastOnce();
    EasyMock.replay(worker);
    ServiceEmitter emitter = EasyMock.createMock(ServiceEmitter.class);
    Capture<EmittingLogger.EmittingAlertBuilder> capturedArgument = Capture.newInstance();
    emitter.emit(EasyMock.capture(capturedArgument));
    EasyMock.expectLastCall().atLeastOnce();
    EmittingLogger.registerEmitter(emitter);
    EasyMock.replay(emitter);
    PathChildrenCache cache = new PathChildrenCache(cf, "/test", true);
    testStartWithNoWorker();
    cache.getListenable().addListener(remoteTaskRunner.getStatusListener(worker, new ZkWorker(worker, cache, jsonMapper), null));
    cache.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT);
    // Status listener will recieve event with null data
    Assert.assertTrue(TestUtils.conditionValid(() -> cache.getCurrentData().size() == 1));
    // Verify that the log emitter was called
    EasyMock.verify(worker);
    EasyMock.verify(emitter);
    Map<String, Object> alertDataMap = capturedArgument.getValue().build(null).getDataMap();
    Assert.assertTrue(alertDataMap.containsKey("znode"));
    Assert.assertNull(alertDataMap.get("znode"));
// Status listener should successfully completes without throwing exception
}
Also used : NoopServiceEmitter(org.apache.druid.server.metrics.NoopServiceEmitter) ServiceEmitter(org.apache.druid.java.util.emitter.service.ServiceEmitter) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) Worker(org.apache.druid.indexing.worker.Worker) Test(org.junit.Test)

Example 27 with PathChildrenCache

use of org.apache.curator.framework.recipes.cache.PathChildrenCache in project Mycat-Server by MyCATApache.

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 28 with PathChildrenCache

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

the class CuratorWatcherTest method main.

public static void main(String[] args) throws Exception {
    // 1.Connect to zk
    CuratorFramework client = CuratorFrameworkFactory.newClient(ZK_ADDRESS, new RetryNTimes(10, 5000));
    client.start();
    System.out.println("zk client start successfully!");
    // 2.Register watcher
    PathChildrenCache watcher = new PathChildrenCache(client, ZK_PATH, true);
    watcher.getListenable().addListener((client1, event) -> {
        ChildData data = event.getData();
        if (data == null) {
            System.out.println("No data in event[" + event + "]");
        } else {
            System.out.println(// 
            "Receive event: " + "type=[" + event.getType() + "]" + // 
            ", " + "path=[" + data.getPath() + "]" + // 
            ", " + "data=[" + new String(data.getData()) + "]" + // 
            ", " + "stat=[" + data.getStat() + "]");
        }
    });
    watcher.start(StartMode.BUILD_INITIAL_CACHE);
    System.out.println("Register zk watcher successfully!");
    client.create().creatingParentsIfNeeded().forPath(ZK_CHILDREN_PATH, "hello".getBytes());
    client.setData().forPath(ZK_CHILDREN_PATH, "world".getBytes());
    client.delete().deletingChildrenIfNeeded().forPath(ZK_CHILDREN_PATH);
    watcher.close();
    Thread.sleep(100);
    client.close();
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) CuratorFramework(org.apache.curator.framework.CuratorFramework) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) ChildData(org.apache.curator.framework.recipes.cache.ChildData)

Example 29 with PathChildrenCache

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

the class ZkRegistryBase method ensureInstancesCache.

// Bogus warnings despite closeQuietly.
@SuppressWarnings("resource")
protected final synchronized PathChildrenCache ensureInstancesCache(long clusterReadyTimeoutMs) throws IOException {
    Preconditions.checkArgument(zooKeeperClient != null && zooKeeperClient.getState() == CuratorFrameworkState.STARTED, "client is not started");
    // lazily create PathChildrenCache
    PathChildrenCache instancesCache = this.instancesCache;
    if (instancesCache != null)
        return instancesCache;
    ExecutorService tp = Executors.newFixedThreadPool(1, new ThreadFactoryBuilder().setDaemon(true).setNameFormat("StateChangeNotificationHandler").build());
    long startTimeNs = System.nanoTime(), deltaNs = clusterReadyTimeoutMs * 1000000L;
    long sleepTimeMs = Math.min(16, clusterReadyTimeoutMs);
    while (true) {
        instancesCache = new PathChildrenCache(zooKeeperClient, workersPath, true);
        instancesCache.getListenable().addListener(new InstanceStateChangeListener(), tp);
        try {
            instancesCache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
            this.instancesCache = instancesCache;
            return instancesCache;
        } catch (InvalidACLException e) {
            // PathChildrenCache tried to mkdir when the znode wasn't there, and failed.
            CloseableUtils.closeQuietly(instancesCache);
            long elapsedNs = System.nanoTime() - startTimeNs;
            if (deltaNs == 0 || deltaNs <= elapsedNs) {
                LOG.error("Unable to start curator PathChildrenCache", e);
                throw new IOException(e);
            }
            LOG.warn("The cluster is not started yet (InvalidACL); will retry");
            try {
                Thread.sleep(Math.min(sleepTimeMs, (deltaNs - elapsedNs) / 1000000L));
            } catch (InterruptedException e1) {
                LOG.error("Interrupted while retrying the PathChildrenCache startup");
                throw new IOException(e1);
            }
            sleepTimeMs = sleepTimeMs << 1;
        } catch (Exception e) {
            CloseableUtils.closeQuietly(instancesCache);
            LOG.error("Unable to start curator PathChildrenCache", e);
            throw new IOException(e);
        }
    }
}
Also used : ServiceInstanceStateChangeListener(org.apache.hadoop.hive.registry.ServiceInstanceStateChangeListener) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) ExecutorService(java.util.concurrent.ExecutorService) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) IOException(java.io.IOException) InvalidACLException(org.apache.zookeeper.KeeperException.InvalidACLException) IOException(java.io.IOException) InvalidACLException(org.apache.zookeeper.KeeperException.InvalidACLException) NodeExistsException(org.apache.zookeeper.KeeperException.NodeExistsException)

Example 30 with PathChildrenCache

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

the class TezAmRegistryImpl method populateCache.

public void populateCache(boolean doInvokeListeners) throws IOException {
    PathChildrenCache pcc = ensureInstancesCache(0);
    populateCache(pcc, doInvokeListeners);
}
Also used : 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