Search in sources :

Example 1 with NodeCache

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

the class ZktoXmlMain method runWatch.

/**
     * 进行zk的watch操作
    * 方法描述
    * @param zkConn zk的连接信息
    * @param path 路径信息
    * @param zkListen 监控路径信息
    * @throws Exception
    * @创建日期 2016年9月20日
    */
private static NodeCache runWatch(final CuratorFramework zkConn, final String path, final ZookeeperProcessListen zkListen) throws Exception {
    final NodeCache cache = new NodeCache(zkConn, path);
    NodeCacheListener listen = new NodeCacheListener() {

        @Override
        public void nodeChanged() throws Exception {
            LOGGER.info("ZktoxmlMain runWatch  process path  event start ");
            LOGGER.info("NodeCache changed, path is: " + cache.getCurrentData().getPath());
            String notPath = cache.getCurrentData().getPath();
            // 进行通知更新
            zkListen.notifly(notPath);
            LOGGER.info("ZktoxmlMain runWatch  process path  event over");
        }
    };
    // 添加监听
    cache.getListenable().addListener(listen);
    return cache;
}
Also used : NodeCache(org.apache.curator.framework.recipes.cache.NodeCache) NodeCacheListener(org.apache.curator.framework.recipes.cache.NodeCacheListener)

Example 2 with NodeCache

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

the class ZktoXmlMain method loadZkWatch.

private static void loadZkWatch(Set<String> setPaths, final CuratorFramework zkConn, final ZookeeperProcessListen zkListen) throws Exception {
    if (null != setPaths && !setPaths.isEmpty()) {
        for (String path : setPaths) {
            // 进行本地节点的监控操作
            NodeCache node = runWatch(zkConn, path, zkListen);
            node.start();
            LOGGER.info("ZktoxmlMain loadZkWatch path:" + path + " regist success");
        }
    }
}
Also used : NodeCache(org.apache.curator.framework.recipes.cache.NodeCache)

Example 3 with NodeCache

use of org.apache.curator.framework.recipes.cache.NodeCache in project zipkin by openzipkin.

the class ZooKeeperCollectorSampler method targetStoreRate.

/** read-only */
static AtomicInteger targetStoreRate(CuratorFramework client, Builder builder, Closer closer) {
    String targetStoreRatePath = ensureExists(client, builder.basePath + "/targetStoreRate");
    NodeCache cache = closer.register(new NodeCache(client, targetStoreRatePath));
    try {
        cache.start();
    } catch (Exception e) {
        throw new IllegalStateException("Error starting cache for " + targetStoreRatePath, e);
    }
    AtomicInteger targetStoreRate = new AtomicInteger();
    cache.getListenable().addListener(() -> {
        byte[] bytes = cache.getCurrentData().getData();
        if (bytes.length == 0)
            return;
        try {
            targetStoreRate.set(Integer.valueOf(new String(bytes, UTF_8)));
        } catch (NumberFormatException e) {
            log.warn("Error parsing target store rate {}", e.getMessage());
            return;
        }
    });
    return targetStoreRate;
}
Also used : NodeCache(org.apache.curator.framework.recipes.cache.NodeCache) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IOException(java.io.IOException)

Example 4 with NodeCache

use of 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 all JobManagers
	 * have terminated. In other words, checks that the ZooKeeperLeaderElection service uses
	 * ephemeral nodes.
	 */
@Test
public void testEphemeralZooKeeperNodes() throws Exception {
    Configuration configuration = new Configuration();
    configuration.setString(HighAvailabilityOptions.HA_ZOOKEEPER_QUORUM, testingServer.getConnectString());
    configuration.setString(HighAvailabilityOptions.HA_MODE, "zookeeper");
    ZooKeeperLeaderElectionService leaderElectionService;
    ZooKeeperLeaderRetrievalService leaderRetrievalService = null;
    TestingContender testingContender;
    TestingListener listener;
    CuratorFramework client = null;
    NodeCache cache = null;
    try {
        leaderElectionService = ZooKeeperUtils.createLeaderElectionService(configuration);
        leaderRetrievalService = ZooKeeperUtils.createLeaderRetrievalService(configuration);
        testingContender = new TestingContender(TEST_URL, leaderElectionService);
        listener = new TestingListener();
        client = ZooKeeperUtils.startCuratorFramework(configuration);
        final String leaderPath = configuration.getString(ConfigConstants.HA_ZOOKEEPER_LEADER_PATH, ConfigConstants.DEFAULT_ZOOKEEPER_LEADER_PATH);
        cache = new NodeCache(client, leaderPath);
        ExistsCacheListener existsListener = new ExistsCacheListener(cache);
        DeletedCacheListener deletedCacheListener = new DeletedCacheListener(cache);
        cache.getListenable().addListener(existsListener);
        cache.start();
        leaderElectionService.start(testingContender);
        testingContender.waitForLeader(timeout.toMillis());
        Future<Boolean> existsFuture = existsListener.nodeExists();
        Await.result(existsFuture, timeout);
        cache.getListenable().addListener(deletedCacheListener);
        leaderElectionService.stop();
        Future<Boolean> deletedFuture = deletedCacheListener.nodeDeleted();
        // make sure that the leader node has been deleted
        Await.result(deletedFuture, timeout);
        leaderRetrievalService.start(listener);
        try {
            listener.waitForNewLeader(1000);
            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 {
        if (leaderRetrievalService != null) {
            leaderRetrievalService.stop();
        }
        if (cache != null) {
            cache.close();
        }
        if (client != null) {
            client.close();
        }
    }
}
Also used : NodeCache(org.apache.curator.framework.recipes.cache.NodeCache) Configuration(org.apache.flink.configuration.Configuration) CuratorFramework(org.apache.curator.framework.CuratorFramework) ZooKeeperLeaderRetrievalService(org.apache.flink.runtime.leaderretrieval.ZooKeeperLeaderRetrievalService) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 5 with NodeCache

use of 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)

Aggregations

NodeCache (org.apache.curator.framework.recipes.cache.NodeCache)5 IOException (java.io.IOException)2 NodeCacheListener (org.apache.curator.framework.recipes.cache.NodeCacheListener)2 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 CuratorFramework (org.apache.curator.framework.CuratorFramework)1 Configuration (org.apache.flink.configuration.Configuration)1 ZooKeeperLeaderRetrievalService (org.apache.flink.runtime.leaderretrieval.ZooKeeperLeaderRetrievalService)1 Test (org.junit.Test)1 AllBrokerUpdateEvent (pers.cy.iris.commons.cluster.event.AllBrokerUpdateEvent)1 TopicUpdateEvent (pers.cy.iris.commons.cluster.event.TopicUpdateEvent)1 UpdateExceptionEvent (pers.cy.iris.commons.cluster.event.UpdateExceptionEvent)1