org.apache.curator.framework.imps.CuratorFrameworkState Java Examples
The following examples show how to use
org.apache.curator.framework.imps.CuratorFrameworkState.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ZkUtils.java From DDMQ with Apache License 2.0 | 6 votes |
public static void init() { try { curatorClient = CuratorFrameworkFactory .builder() .connectString(zkConfig.getZkAddrs()) .sessionTimeoutMs(zkConfig.getZkSessionTimeoutMs()) .retryPolicy(new BoundedExponentialBackoffRetry(zkConfig.getBaseSleepTimeMs(), zkConfig.getMaxSleepMs(), zkConfig.getMaxRetries())) .build(); if (curatorClient.getState() == CuratorFrameworkState.LATENT) { curatorClient.start(); } ZooKeeperConfigurationSource zkConfigSource = new ZooKeeperConfigurationSource(curatorClient, Constants.META_BASE_ZK_PATH); zkConfigSource.start(); DynamicWatchedConfiguration zkDynamicConfig = new DynamicWatchedConfiguration(zkConfigSource); ConfigurationManager.install(zkDynamicConfig); } catch (Exception e) { LOGGER.error("ZkUtils getCuratorClient err:{}", e.getMessage(), e); } }
Example #2
Source File: EventContainer.java From DBus with Apache License 2.0 | 6 votes |
/** * 在拉完全量后将此schema的kafka consumer的offset设置为最新 * * @param dbSchema */ /*public void setKafkaOffsetToLargest(String targetTopic){ if(targetTopic==null) return; TopicPartition partition0 = new TopicPartition(targetTopic, 0); KafkaConsumerContainer.getInstances().getConsumer(targetTopic).seekToEnd(Arrays.asList(partition0)); }*/ protected <T> T deserialize(String path, Class<T> clazz) throws Exception { T packet = null; CuratorFramework curator = CuratorContainer.getInstance().getCurator(); if (curator.getState() == CuratorFrameworkState.STOPPED) { LOG.info("[EventContainer] CuratorFrameworkState:{}", CuratorFrameworkState.STOPPED.name()); } else { byte[] bytes = curator.getData().forPath(path); if (bytes != null && bytes.length != 0) { packet = JsonUtil.fromJson(new String(bytes, Charset.forName("UTF-8")), clazz); } } return packet; }
Example #3
Source File: LeaderInitiator.java From spring-cloud-cluster with Apache License 2.0 | 6 votes |
/** * Start the registration of the {@link #candidate} for leader election. */ @Override public synchronized void start() { if (!this.running) { if (this.client.getState() != CuratorFrameworkState.STARTED) { // we want to do curator start here because it needs to // be started before leader selector and it gets a little // complicated to control ordering via beans so that // curator is fully started. this.client.start(); } this.leaderSelector = new LeaderSelector(this.client, buildLeaderPath(), new LeaderListener()); this.leaderSelector.setId(this.candidate.getId()); this.leaderSelector.autoRequeue(); this.leaderSelector.start(); this.running = true; } }
Example #4
Source File: ZKUtil.java From kylin with Apache License 2.0 | 6 votes |
private static CuratorFramework getZookeeperClient(final String zkString, final RetryPolicy retryPolicy) { if (StringUtils.isEmpty(zkString)) { throw new IllegalArgumentException("ZOOKEEPER_QUORUM is empty!"); } try { CuratorFramework instance = CACHE.get(zkString, new Callable<CuratorFramework>() { @Override public CuratorFramework call() throws Exception { return newZookeeperClient(zkString, retryPolicy); } }); // during test, curator may be closed by others, remove it from CACHE and reinitialize a new one if (instance.getState() != CuratorFrameworkState.STARTED) { logger.warn("curator for {} is closed by others unexpectedly, reinitialize a new one", zkString); CACHE.invalidate(zkString); instance = getZookeeperClient(zkString, retryPolicy); } return instance; } catch (Throwable e) { throw new RuntimeException(e); } }
Example #5
Source File: ConfigPublisher.java From AsuraFramework with Apache License 2.0 | 6 votes |
/** * 初始化 * * @author zhangshaobin * @created 2013-6-26 上午10:55:30 */ private void init() { applicationName = DynamicPropertyFactory.getInstance().getStringProperty(CommonConstant.CONFIG_APPLICATION_NAME_KEY, null).get(); String zkConfigEnsemble = DynamicPropertyFactory.getInstance().getStringProperty(CommonConstant.ZK_ENSEMABLE_KEY, null).get(); Integer zkConfigSessionTimeout = DynamicPropertyFactory.getInstance().getIntProperty(CommonConstant.ZK_SESSION_TIMEOUT_KEY, 15000).get(); Integer zkConfigConnTimeout = DynamicPropertyFactory.getInstance().getIntProperty(CommonConstant.ZK_CONN_TIMEOUT_KEY, 5000).get(); if (Check.NuNStr(zkConfigEnsemble)) { logger.warn("ZooKeeper configuration running in file mode, zk is not enabled since not configured"); return; } try { client = createAndStartZKClient(zkConfigEnsemble, zkConfigSessionTimeout, zkConfigConnTimeout); if (client.getState() != CuratorFrameworkState.STARTED) { throw new RuntimeException("ZooKeeper located at " + zkConfigEnsemble + " is not started."); } } catch (Exception e) { e.printStackTrace(); logger.error("连接配置中心服务器超时,时间5000毫秒。", e.getCause()); System.exit(1); } System.out.println(new SimpleDateFormat("[yyyy-MM-dd HH:mm:ss] ").format(new Date()) + applicationName + " connected to cofnig server(" + zkConfigEnsemble + ")."); logger.info(applicationName + " connected to cofnig server(" + zkConfigEnsemble + ")."); }
Example #6
Source File: ZookeeperRegistry.java From sofa-rpc with Apache License 2.0 | 6 votes |
@Override public synchronized boolean start() { if (zkClient == null) { LOGGER.warn("Start zookeeper registry must be do init first!"); return false; } if (zkClient.getState() == CuratorFrameworkState.STARTED) { return true; } try { zkClient.start(); } catch (Exception e) { throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_ZOOKEEPER_CLIENT_START), e); } return zkClient.getState() == CuratorFrameworkState.STARTED; }
Example #7
Source File: ZookeeperHealthIndicator.java From spring-cloud-zookeeper with Apache License 2.0 | 6 votes |
@Override protected void doHealthCheck(Health.Builder builder) throws Exception { try { CuratorFrameworkState state = this.curator.getState(); if (state != CuratorFrameworkState.STARTED) { builder.down().withDetail("error", "Client not started"); } else if (this.curator.checkExists().forPath("/") == null) { builder.down().withDetail("error", "Root for namespace does not exist"); } else { builder.up(); } builder.withDetail("connectionString", this.curator.getZookeeperClient().getCurrentConnectionString()) .withDetail("state", state); } catch (Exception e) { builder.down(e); } }
Example #8
Source File: ZkClient.java From xio with Apache License 2.0 | 6 votes |
public void registerUpdater(ConfigurationUpdater updater) { NodeCache cache = getOrCreateNodeCache(updater.getPath()); if (client.getState().equals(CuratorFrameworkState.STARTED)) { startNodeCache(cache); } cache .getListenable() .addListener( new NodeCacheListener() { @Override public void nodeChanged() { updater.update(cache.getCurrentData().getData()); } }); }
Example #9
Source File: ZooKeeperConfigurationTest.java From curator-extensions with Apache License 2.0 | 6 votes |
@Test public void testNewManagedCurator() { ZooKeeperConfiguration config = parse(ImmutableMap.of("retryPolicy", ImmutableMap.builder() .put("type", "untilElapsed") .put("maxElapsedTimeMs", 1000) .put("sleepMsBetweenRetries", 50) .build())); LifecycleEnvironment env = mock(LifecycleEnvironment.class); CuratorFramework curator = config.newManagedCurator(env); assertNotNull(curator); assertEquals(CuratorFrameworkState.LATENT, curator.getState()); verify(env).manage(any(ManagedCuratorFramework.class)); }
Example #10
Source File: ZkUtils.java From DDMQ with Apache License 2.0 | 6 votes |
public static void init() { try { curatorClient = CuratorFrameworkFactory .builder() .connectString(zkConfig.getZkAddrs()) .sessionTimeoutMs(zkConfig.getZkSessionTimeoutMs()) .retryPolicy(new BoundedExponentialBackoffRetry(zkConfig.getBaseSleepTimeMs(), zkConfig.getMaxSleepMs(), zkConfig.getMaxRetries())) .build(); if (curatorClient.getState() == CuratorFrameworkState.LATENT) { curatorClient.start(); } ZooKeeperConfigurationSource zkConfigSource = new ZooKeeperConfigurationSource(curatorClient, Constants.META_BASE_ZK_PATH); zkConfigSource.start(); DynamicWatchedConfiguration zkDynamicConfig = new DynamicWatchedConfiguration(zkConfigSource); ConfigurationManager.install(zkDynamicConfig); } catch (Exception e) { LOGGER.error("ZkUtils getCuratorClient err:{}", e.getMessage(), e); } }
Example #11
Source File: PersistentEphemeralNode.java From curator-extensions with Apache License 2.0 | 6 votes |
/** * Create the ephemeral node in ZooKeeper. If the node cannot be created in a timely fashion then an exception will * be thrown. * * @param curator Client to manage ZooKeeper nodes with. * @param basePath Path to parent node this node should be created in. * @param data Data to store in the node. * @param mode Node creation mode. */ public PersistentEphemeralNode(CuratorFramework curator, String basePath, byte[] data, CreateMode mode) { Objects.requireNonNull(curator); checkArgument(curator.getState() == CuratorFrameworkState.STARTED); Objects.requireNonNull(basePath); Objects.requireNonNull(data); Objects.requireNonNull(mode); checkArgument(mode == CreateMode.EPHEMERAL || mode == CreateMode.EPHEMERAL_SEQUENTIAL); // TODO: Share this executor across multiple persistent ephemeral nodes in a way that guarantees that it is a // TODO: single thread executor. _executor = Executors.newSingleThreadScheduledExecutor(THREAD_FACTORY); _async = new Async(_executor, new Sync(curator, basePath, data, mode)); CountDownLatch latch = new CountDownLatch(1); _async.createNode(latch); await(latch, CREATION_WAIT_IN_SECONDS, TimeUnit.SECONDS); }
Example #12
Source File: MasterSlaveNode.java From niubi-job with Apache License 2.0 | 6 votes |
@Override public void relinquishLeadership() { try { if (nodeCache != null) { nodeCache.close(); } LoggerHelper.info("node cache has been closed."); } catch (Throwable e) { LoggerHelper.warn("node cache close failed.", e); } if (client.getState() == CuratorFrameworkState.STARTED) { MasterSlaveNodeData.Data nodeData = new MasterSlaveNodeData.Data(getNodeIp()); releaseJobs(nodePath, nodeData); nodeData.setNodeState("Slave"); masterSlaveApiFactory.nodeApi().updateNode(nodePath, nodeData); } LoggerHelper.info("clear node successfully."); }
Example #13
Source File: ZKUtils.java From Decision with Apache License 2.0 | 6 votes |
private ZKUtils(String zookeeperCluster, String groupId) throws Exception { this.groupId = groupId; // ZOOKEPER CONNECTION client = CuratorFrameworkFactory.newClient(zookeeperCluster, 25 * 1000, 10 * 1000, new ExponentialBackoffRetry( 1000, 3)); client.start(); client.getZookeeperClient().blockUntilConnectedOrTimedOut(); if (client.getState().compareTo(CuratorFrameworkState.STARTED) != 0) { throw new Exception("Connection to Zookeeper timed out after seconds"); } else { backgroundZookeeperCleanerTasks = Executors.newFixedThreadPool(1); backgroundZookeeperCleanerTasks.submit(new ZookeeperBackgroundCleaner(client, groupId)); } }
Example #14
Source File: StandbyNode.java From niubi-job with Apache License 2.0 | 6 votes |
@Override public void relinquishLeadership() { try { if (jobCache != null) { jobCache.close(); } LoggerHelper.info("job cache has been closed."); } catch (Throwable e) { LoggerHelper.warn("job cache close failed.", e); } LoggerHelper.info("begin stop scheduler manager."); schedulerManager.shutdown(); if (client.getState() == CuratorFrameworkState.STARTED) { StandbyNodeData.Data data = new StandbyNodeData.Data(getNodeIp()); standbyApiFactory.nodeApi().updateNode(nodePath, data); LoggerHelper.info(getNodeIp() + " has been shutdown. [" + data + "]"); } LoggerHelper.info("clear node successfully."); }
Example #15
Source File: ZKUtil.java From kylin-on-parquet-v2 with Apache License 2.0 | 6 votes |
private static CuratorFramework getZookeeperClient(final String zkString, final RetryPolicy retryPolicy) { if (StringUtils.isEmpty(zkString)) { throw new IllegalArgumentException("ZOOKEEPER_QUORUM is empty!"); } try { CuratorFramework instance = CACHE.get(zkString, new Callable<CuratorFramework>() { @Override public CuratorFramework call() throws Exception { return newZookeeperClient(zkString, retryPolicy); } }); // during test, curator may be closed by others, remove it from CACHE and reinitialize a new one if (instance.getState() != CuratorFrameworkState.STARTED) { logger.warn("curator for {} is closed by others unexpectedly, reinitialize a new one", zkString); CACHE.invalidate(zkString); instance = getZookeeperClient(zkString, retryPolicy); } return instance; } catch (Throwable e) { throw new RuntimeException(e); } }
Example #16
Source File: StateManager.java From Singularity with Apache License 2.0 | 6 votes |
public void save(SingularityHostState hostState) throws InterruptedException { final String path = ZKPaths.makePath(ROOT_PATH, hostState.getHostname()); final byte[] data = hostStateTranscoder.toBytes(hostState); if (curator.getState() == CuratorFrameworkState.STARTED) { try { if (exists(path)) { curator.setData().forPath(path, data); } else { curator .create() .creatingParentsIfNeeded() .withMode(CreateMode.EPHEMERAL) .forPath(path, data); } } catch (Throwable t) { throw new RuntimeException(t); } } }
Example #17
Source File: NodeDiscovery.java From curator-extensions with Apache License 2.0 | 6 votes |
/** * Creates an instance of {@code ZooKeeperNodeDiscovery}. * * @param curator Curator framework reference. * @param nodePath The path in ZooKeeper to watch. * @param parser The strategy to convert from ZooKeeper {@code byte[]} to {@code T}. */ public NodeDiscovery(CuratorFramework curator, String nodePath, NodeDataParser<T> parser) { Objects.requireNonNull(curator); Objects.requireNonNull(nodePath); Objects.requireNonNull(parser); checkArgument(curator.getState() == CuratorFrameworkState.STARTED); checkArgument(!"".equals(nodePath)); ThreadFactory threadFactory = new ThreadFactoryBuilder() .setNameFormat(getClass().getSimpleName() + "(" + nodePath + ")-%d") .setDaemon(true) .build(); _nodes = new ConcurrentHashMap<>(); _listeners = Collections.newSetFromMap(new ConcurrentHashMap<>()); _curator = curator; _executor = Executors.newSingleThreadScheduledExecutor(threadFactory); _pathCache = new PathChildrenCache(curator, nodePath, true, false, _executor); _nodeDataParser = parser; _closed = false; }
Example #18
Source File: Curator4ZookeeperRegistry.java From spring-cloud-sofastack-samples with Apache License 2.0 | 6 votes |
@Override public synchronized boolean start() { if (zkClient == null) { LOGGER.warn("Start zookeeper registry must be do init first!"); return false; } if (zkClient.getState() == CuratorFrameworkState.STARTED) { return true; } try { zkClient.start(); } catch (Throwable t) { throw new SofaRpcRuntimeException("Failed to start zookeeper zkClient", t); } return zkClient.getState() == CuratorFrameworkState.STARTED; }
Example #19
Source File: ZookeeperClientFactoryBean.java From easyooo-framework with Apache License 2.0 | 5 votes |
@Override public void destroy() throws Exception { if(zkClient != null && zkClient.getState() != CuratorFrameworkState.STOPPED){ zkClient.close(); } }
Example #20
Source File: DefaultZooKeeperClient.java From helios with Apache License 2.0 | 5 votes |
@Override public void start() { if (client.getState() != CuratorFrameworkState.STARTED) { client.start(); if (clusterId != null) { client.getConnectionStateListenable().addListener(connectionStateListener); checkClusterIdExists(clusterId, "start"); } } }
Example #21
Source File: ZookeeperDistributedLock.java From kylin with Apache License 2.0 | 5 votes |
@Override public boolean lock(String lockPath) { logger.debug("{} trying to lock {}", client, lockPath); // curator closed in some case(like Expired),restart it if (curator.getState() != CuratorFrameworkState.STARTED) { curator = ZKUtil.getZookeeperClient(KylinConfig.getInstanceFromEnv()); } lockInternal(lockPath); String lockOwner; try { lockOwner = peekLock(lockPath); if (client.equals(lockOwner)) { logger.info("{} acquired lock at {}", client, lockPath); return true; } else { logger.debug("{} failed to acquire lock at {}, which is held by {}", client, lockPath, lockOwner); return false; } } catch (ZkPeekLockInterruptException zpie) { logger.error("{} peek owner of lock interrupt while acquire lock at {}, check to release lock", client, lockPath); lockOwner = peekLock(lockPath); try { unlockInternal(lockOwner, lockPath); } catch (Exception anyEx) { // it's safe to swallow any exception here because here already been interrupted logger.warn("Exception caught to release lock when lock operation has been interrupted.", anyEx); } throw zpie; } }
Example #22
Source File: ZKUtils.java From Decision with Apache License 2.0 | 5 votes |
@Override public void run() { String zkPath = STREAMING.ZK_BASE_PATH; /* if (groupId != null){ zkPath = zkPath.concat("/").concat(groupId); } */ while (!Thread.currentThread().isInterrupted()) { try { if (client.getState().compareTo(CuratorFrameworkState.STARTED) == 0) { int childsRemoved = removeOldChildZnodes(zkPath); logger.debug(childsRemoved + " old zNodes removed from ZK"); } Thread.sleep(CLEAN_INTERVAL); } catch (InterruptedException ie) { // no need to clean anything, as client is shared logger.info("Shutting down Zookeeper Background Cleaner"); } catch (Exception e) { logger.info("Error on Zookeeper Background Cleaner: " + e.getMessage()); } } }
Example #23
Source File: DefaultScheduler.java From Kylin with Apache License 2.0 | 5 votes |
private void releaseLock() { try { if (zkClient.getState().equals(CuratorFrameworkState.STARTED)) { // client.setData().forPath(ZOOKEEPER_LOCK_PATH, null); if (zkClient.checkExists().forPath(schedulerId()) != null) { zkClient.delete().guaranteed().deletingChildrenIfNeeded().forPath(schedulerId()); } } } catch (Exception e) { logger.error("error release lock:" + schedulerId()); throw new RuntimeException(e); } }
Example #24
Source File: CuratorFrameworkBuilderTest.java From chassis with Apache License 2.0 | 5 votes |
@Test public void zookeeperStarted() throws Exception { testingServer = new TestingServer(SocketUtils.findAvailableTcpPort()); try (CuratorFramework curatorFramework = new CuratorFrameworkBuilder(true).withZookeeper(testingServer.getConnectString()).build()) { Assert.assertEquals(CuratorFrameworkState.STARTED, curatorFramework.getState()); Assert.assertNull(curatorFramework.checkExists().forPath("/test")); curatorFramework.create().forPath("/test"); Assert.assertNotNull(curatorFramework.checkExists().forPath("/test")); } }
Example #25
Source File: ManagedCuratorFrameworkTest.java From curator-extensions with Apache License 2.0 | 5 votes |
@Test public void testStartsCuratorOnStart() throws Exception { CuratorFramework curator = mock(CuratorFramework.class); when(curator.getState()).thenReturn(CuratorFrameworkState.LATENT); ManagedCuratorFramework managed = new ManagedCuratorFramework(curator); managed.start(); verify(curator).start(); }
Example #26
Source File: JobHistorySpoutTest.java From eagle with Apache License 2.0 | 5 votes |
@After public void tearDown() throws Exception { try { if (zookeeper != null) { if (!zookeeper.getState().equals(CuratorFrameworkState.STOPPED)) { zookeeper.close(); } } } finally { if (server != null) { server.close(); } } }
Example #27
Source File: CuratorFrameworkFactoryBeanTests.java From spring-cloud-cluster with Apache License 2.0 | 5 votes |
@Test public void test() throws Exception { TestingServer testingServer = new TestingServer(); CuratorFrameworkFactoryBean fb = new CuratorFrameworkFactoryBean(testingServer.getConnectString()); CuratorFramework client = fb.getObject(); fb.start(); assertTrue(client.getState().equals(CuratorFrameworkState.STARTED)); fb.stop(); assertTrue(client.getState().equals(CuratorFrameworkState.STOPPED)); testingServer.close(); }
Example #28
Source File: ZookeeperClientFactoryBean.java From easyooo-framework with Apache License 2.0 | 5 votes |
@Override public void afterPropertiesSet() { // 优先系统参数中的ZK配置,如果VM启动参数中没有该参数,则取Spring注入的属性 connectionString = System.getProperty("zkConnectionString", connectionString); notNull(connectionString, "Property 'connectionString' is required"); this.zkClient = createZookeeperInstance(); this.zkClient.start(); if(this.zkClient.getState() == CuratorFrameworkState.STARTED){ logger.info("ZK connection is successful."); }else{ logger.error("ZK connection is unsuccessful."); } }
Example #29
Source File: ClusterZKImpl.java From pravega with Apache License 2.0 | 5 votes |
public ClusterZKImpl(CuratorFramework zkClient, String clusterName) { this.client = zkClient; this.clusterName = clusterName; if (client.getState().equals(CuratorFrameworkState.LATENT)) { client.start(); } }
Example #30
Source File: ZooKeeperConfigurationTest.java From curator-extensions with Apache License 2.0 | 5 votes |
@Test public void testNewCurator() { ZooKeeperConfiguration config = parse(ImmutableMap.of("retryPolicy", ImmutableMap.builder() .put("type", "untilElapsed") .put("maxElapsedTimeMs", 1000) .put("sleepMsBetweenRetries", 50) .build())); CuratorFramework curator = config.newCurator(); assertNotNull(curator); assertEquals(CuratorFrameworkState.LATENT, curator.getState()); }