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;
}
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);
}
}
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());
}
}
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();
}
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();
}
}
}
Aggregations