Search in sources :

Example 26 with NodeCache

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.NodeCache in project dubbo by alibaba.

the class CuratorZookeeperClient method removeTargetDataListener.

@Override
protected void removeTargetDataListener(String path, CuratorZookeeperClient.NodeCacheListenerImpl nodeCacheListener) {
    NodeCache nodeCache = nodeCacheMap.get(path);
    if (nodeCache != null) {
        nodeCache.getListenable().removeListener(nodeCacheListener);
    }
    nodeCacheListener.dataListener = null;
}
Also used : NodeCache(org.apache.curator.framework.recipes.cache.NodeCache)

Example 27 with NodeCache

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.NodeCache in project dubbo by alibaba.

the class Curator5ZookeeperClient method addTargetDataListener.

@Override
protected void addTargetDataListener(String path, Curator5ZookeeperClient.NodeCacheListenerImpl nodeCacheListener, Executor executor) {
    try {
        NodeCache nodeCache = new NodeCache(client, path);
        if (nodeCacheMap.putIfAbsent(path, nodeCache) != null) {
            return;
        }
        if (executor == null) {
            nodeCache.getListenable().addListener(nodeCacheListener);
        } else {
            nodeCache.getListenable().addListener(nodeCacheListener, executor);
        }
        nodeCache.start();
    } catch (Exception e) {
        throw new IllegalStateException("Add nodeCache listener for path:" + path, e);
    }
}
Also used : NodeCache(org.apache.curator.framework.recipes.cache.NodeCache) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) NodeExistsException(org.apache.zookeeper.KeeperException.NodeExistsException)

Example 28 with NodeCache

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.NodeCache in project canal by alibaba.

the class SyncSwitch method startListen.

@SuppressWarnings("resource")
private synchronized void startListen(String destination, BooleanMutex mutex) {
    try {
        String path = SYN_SWITCH_ZK_NODE + destination;
        CuratorFramework curator = curatorClient.getCurator();
        NodeCache nodeCache = new NodeCache(curator, path);
        nodeCache.start();
        nodeCache.getListenable().addListener(() -> initMutex(curator, destination, mutex));
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage());
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) NodeCache(org.apache.curator.framework.recipes.cache.NodeCache) TimeoutException(java.util.concurrent.TimeoutException)

Example 29 with NodeCache

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.NodeCache in project iris by chicc999.

the class MetaManager method doStart.

@Override
public void doStart() throws Exception {
    zkClient.start();
    // 注册broker存活
    zkClient.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(BROKER_LIVE_PATH + System.getProperty(ServerType.Broker.nameKey()) + "_", System.getProperty(ServerType.Broker.nameKey()).getBytes("utf-8"));
    // 检测并初始化topic路径
    if (null == this.zkClient.checkExists().forPath(this.TOPIC_PATH)) {
        this.zkClient.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(this.TOPIC_PATH, "[]".getBytes("utf-8"));
    }
    // 注册监听器
    if (topicCache == null) {
        topicCache = new NodeCache(this.zkClient, this.TOPIC_PATH, false);
        topicCache.getListenable().addListener(new NodeCacheListener() {

            @Override
            public void nodeChanged() {
                try {
                    updateTopic(topicCache.getCurrentData().getData());
                    // 添加topic更新事件
                    clusterEventManager.add(new TopicUpdateEvent());
                } catch (IOException e) {
                    clusterEventManager.add(new UpdateExceptionEvent(e));
                }
            }
        });
    }
    // 启动监听器
    topicCache.start();
    // 检测并初始化broker路径
    if (null == this.zkClient.checkExists().forPath(this.BROKER_PATH)) {
        this.zkClient.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(this.BROKER_PATH, "[]".getBytes("utf-8"));
    }
    // 注册监听器
    if (brokerCache == null) {
        brokerCache = new NodeCache(this.zkClient, this.BROKER_PATH, false);
        brokerCache.getListenable().addListener(new NodeCacheListener() {

            @Override
            public void nodeChanged() {
                try {
                    updateBroker(brokerCache.getCurrentData().getData());
                    // 集群事件管理器添加broker更新事件
                    clusterEventManager.add(new AllBrokerUpdateEvent());
                } catch (IOException e) {
                    clusterEventManager.add(new UpdateExceptionEvent(e));
                }
            }
        });
    }
    // 启动监听器
    brokerCache.start();
    clusterEventManager.start();
}
Also used : UpdateExceptionEvent(pers.cy.iris.commons.cluster.event.UpdateExceptionEvent) NodeCache(org.apache.curator.framework.recipes.cache.NodeCache) AllBrokerUpdateEvent(pers.cy.iris.commons.cluster.event.AllBrokerUpdateEvent) TopicUpdateEvent(pers.cy.iris.commons.cluster.event.TopicUpdateEvent) NodeCacheListener(org.apache.curator.framework.recipes.cache.NodeCacheListener) IOException(java.io.IOException)

Example 30 with NodeCache

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.NodeCache in project flink by apache.

the class ZooKeeperLeaderElectionTest method testEphemeralZooKeeperNodes.

/**
 * Tests that there is no information left in the ZooKeeper cluster after the ZooKeeper client
 * has terminated. In other words, checks that the ZooKeeperLeaderElection service uses
 * ephemeral nodes.
 */
@Test
public void testEphemeralZooKeeperNodes() throws Exception {
    ZooKeeperLeaderElectionDriver leaderElectionDriver = null;
    LeaderRetrievalDriver leaderRetrievalDriver = null;
    final TestingLeaderElectionEventHandler electionEventHandler = new TestingLeaderElectionEventHandler(LEADER_ADDRESS);
    final TestingLeaderRetrievalEventHandler retrievalEventHandler = new TestingLeaderRetrievalEventHandler();
    CuratorFrameworkWithUnhandledErrorListener curatorFrameworkWrapper = null;
    CuratorFrameworkWithUnhandledErrorListener curatorFrameworkWrapper2 = null;
    NodeCache cache = null;
    try {
        curatorFrameworkWrapper = ZooKeeperUtils.startCuratorFramework(configuration, testingFatalErrorHandlerResource.getFatalErrorHandler());
        curatorFrameworkWrapper2 = ZooKeeperUtils.startCuratorFramework(configuration, testingFatalErrorHandlerResource.getFatalErrorHandler());
        leaderElectionDriver = createAndInitLeaderElectionDriver(curatorFrameworkWrapper.asCuratorFramework(), electionEventHandler);
        leaderRetrievalDriver = ZooKeeperUtils.createLeaderRetrievalDriverFactory(curatorFrameworkWrapper2.asCuratorFramework()).createLeaderRetrievalDriver(retrievalEventHandler, retrievalEventHandler::handleError);
        cache = new NodeCache(curatorFrameworkWrapper2.asCuratorFramework(), leaderElectionDriver.getConnectionInformationPath());
        ExistsCacheListener existsListener = new ExistsCacheListener(cache);
        DeletedCacheListener deletedCacheListener = new DeletedCacheListener(cache);
        cache.getListenable().addListener(existsListener);
        cache.start();
        electionEventHandler.waitForLeader(timeout);
        retrievalEventHandler.waitForNewLeader(timeout);
        Future<Boolean> existsFuture = existsListener.nodeExists();
        existsFuture.get(timeout, TimeUnit.MILLISECONDS);
        cache.getListenable().addListener(deletedCacheListener);
        leaderElectionDriver.close();
        // now stop the underlying client
        curatorFrameworkWrapper.close();
        Future<Boolean> deletedFuture = deletedCacheListener.nodeDeleted();
        // make sure that the leader node has been deleted
        deletedFuture.get(timeout, TimeUnit.MILLISECONDS);
        try {
            retrievalEventHandler.waitForNewLeader(1000L);
            fail("TimeoutException was expected because there is no leader registered and " + "thus there shouldn't be any leader information in ZooKeeper.");
        } catch (TimeoutException e) {
        // that was expected
        }
    } finally {
        electionEventHandler.close();
        if (leaderRetrievalDriver != null) {
            leaderRetrievalDriver.close();
        }
        if (cache != null) {
            cache.close();
        }
        if (curatorFrameworkWrapper2 != null) {
            curatorFrameworkWrapper2.close();
        }
    }
}
Also used : ZooKeeperLeaderRetrievalDriver(org.apache.flink.runtime.leaderretrieval.ZooKeeperLeaderRetrievalDriver) LeaderRetrievalDriver(org.apache.flink.runtime.leaderretrieval.LeaderRetrievalDriver) NodeCache(org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.NodeCache) CuratorFrameworkWithUnhandledErrorListener(org.apache.flink.runtime.highavailability.zookeeper.CuratorFrameworkWithUnhandledErrorListener) TestingLeaderRetrievalEventHandler(org.apache.flink.runtime.leaderretrieval.TestingLeaderRetrievalEventHandler) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Aggregations

NodeCache (org.apache.curator.framework.recipes.cache.NodeCache)32 NodeCacheListener (org.apache.curator.framework.recipes.cache.NodeCacheListener)14 IOException (java.io.IOException)6 KeeperException (org.apache.zookeeper.KeeperException)6 TimeoutException (java.util.concurrent.TimeoutException)3 ArkRuntimeException (com.alipay.sofa.ark.exception.ArkRuntimeException)2 ConsoleThreadFactory (com.vip.saturn.job.console.utils.ConsoleThreadFactory)2 SaturnJobException (com.vip.saturn.job.exception.SaturnJobException)2 SaturnThreadFactory (com.vip.saturn.job.threads.SaturnThreadFactory)2 ChildData (org.apache.curator.framework.recipes.cache.ChildData)2 NoNodeException (org.apache.zookeeper.KeeperException.NoNodeException)2 NodeExistsException (org.apache.zookeeper.KeeperException.NodeExistsException)2 SaturnExecutorException (com.vip.saturn.job.exception.SaturnExecutorException)1 Entry (java.util.Map.Entry)1 ExecutionException (java.util.concurrent.ExecutionException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 SneakyThrows (lombok.SneakyThrows)1 CuratorFramework (org.apache.curator.framework.CuratorFramework)1 CuratorFrameworkWithUnhandledErrorListener (org.apache.flink.runtime.highavailability.zookeeper.CuratorFrameworkWithUnhandledErrorListener)1 LeaderRetrievalDriver (org.apache.flink.runtime.leaderretrieval.LeaderRetrievalDriver)1