private synchronized void ensureStarted() { switch (mClient.getState()) { case LATENT: mClient.start(); return; case STARTED: return; case STOPPED: throw new IllegalStateException("Client has already been closed"); default: throw new IllegalStateException("Unknown state: " + mClient.getState()); } }
private CuratorFramework getAndCheckZkClient() { if (zkClient == null || zkClient.getState() != CuratorFrameworkState.STARTED) { throw new SofaRpcRuntimeException("Zookeeper client is not available"); } return zkClient; }
private CuratorFramework getAndCheckZkClient() { if (zkClient == null || zkClient.getState() != CuratorFrameworkState.STARTED) { throw new SofaRpcRuntimeException("Zookeeper client is not available"); } return zkClient; }
private CuratorFramework getSession() { if (zkSession == null || zkSession.getState() == CuratorFrameworkState.STOPPED) { synchronized (this) { if (zkSession == null || zkSession.getState() == CuratorFrameworkState.STOPPED) { zkSession = CuratorFrameworkFactory.builder().connectString(zkConnectString) .connectionTimeoutMs(connectTimeoutMillis).aclProvider(aclDefaultProvider) .retryPolicy(new ExponentialBackoffRetry(1000, 3)).build(); zkSession.start(); } } } return zkSession; }
@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("Failed to start zookeeper zkClient", e); } return zkClient.getState() == CuratorFrameworkState.STARTED; }
@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("Failed to start zookeeper zkClient", e); } return zkClient.getState() == CuratorFrameworkState.STARTED; }
@Override public void destroy() { closePathChildrenCache(INTERFACE_CONFIG_CACHE); closePathChildrenCache(INTERFACE_OVERRIDE_CACHE); if (zkClient != null && zkClient.getState() == CuratorFrameworkState.STARTED) { zkClient.close(); } providerUrls.clear(); consumerUrls.clear(); }
@Override public void destroy() { closePathChildrenCache(INTERFACE_CONFIG_CACHE); closePathChildrenCache(INTERFACE_OVERRIDE_CACHE); if (zkClient != null && zkClient.getState() == CuratorFrameworkState.STARTED) { zkClient.close(); } providerUrls.clear(); consumerUrls.clear(); }
@Override public void start() { if (client.getState() != CuratorFrameworkState.STARTED) { client.start(); if (clusterId != null) { client.getConnectionStateListenable().addListener(connectionStateListener); checkClusterIdExists(clusterId, "start"); } } }
long clusterReadyTimeoutMs) throws IOException { Preconditions.checkArgument(zooKeeperClient != null && zooKeeperClient.getState() == CuratorFrameworkState.STARTED, "client is not started");
/** Initializes this IP Finder by creating the appropriate Curator objects. */ private void init() { if (!initGuard.compareAndSet(false, true)) return; String sysPropZkConnString = System.getProperty(PROP_ZK_CONNECTION_STRING); if (sysPropZkConnString != null && !sysPropZkConnString.trim().isEmpty()) zkConnectionString = sysPropZkConnString; if (log.isInfoEnabled()) log.info("Initializing ZooKeeper IP Finder."); if (curator == null) { A.notNullOrEmpty(zkConnectionString, String.format("ZooKeeper URL (or system property %s) cannot be null " + "or empty if a CuratorFramework object is not provided explicitly", PROP_ZK_CONNECTION_STRING)); curator = CuratorFrameworkFactory.newClient(zkConnectionString, retryPolicy); } if (curator.getState() == CuratorFrameworkState.LATENT) curator.start(); A.ensure(curator.getState() == CuratorFrameworkState.STARTED, "CuratorFramework can't be started."); discovery = ServiceDiscoveryBuilder.builder(IgniteInstanceDetails.class) .client(curator) .basePath(basePath) .serializer(new JsonInstanceSerializer<>(IgniteInstanceDetails.class)) .build(); }
/** {@inheritDoc} */ @Override public void unregisterAddresses(Collection<InetSocketAddress> addrs) throws IgniteSpiException { // if curator is not STARTED, we have nothing to unregister, because we are using ephemeral nodes, // which means that our addresses will only be registered in ZK as long as our connection is alive if (curator.getState() != CuratorFrameworkState.STARTED) return; if (log.isInfoEnabled()) log.info("Unregistering addresses with ZooKeeper IP Finder: " + addrs); for (InetSocketAddress addr : addrs) { ServiceInstance<IgniteInstanceDetails> si = ourInstances.get(addr); if (si == null) { log.warning("Asked to unregister address from ZooKeeper IP Finder, but no match was found in local " + "instance map for: " + addrs); continue; } try { discovery.unregisterService(si); } catch (Exception e) { log.warning("Error while unregistering an address from ZooKeeper IP Finder: " + addr, e); } } }
@Override public void close() throws IOException { if (client == null) { return; } // close all node cache for (Map.Entry<String, NodeCache> entry : nodeCaches.entrySet()) { NodeCache value = entry.getValue(); value.close(); } // close all children cache for (Map.Entry<String, PathChildrenCache> entry : nodeChildrenCache.entrySet()) { PathChildrenCache value = entry.getValue(); value.close(); } // close all tree cache for (Map.Entry<String, TreeCache> entry : nodeTreeCache.entrySet()) { TreeCache value = entry.getValue(); value.close(); } if (client.getState() == CuratorFrameworkState.STARTED) { client.close(); } }
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 Throwables.propagate(t); } } }
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); } }
void addFailedOperation(T details) { if ( debugListener != null ) { debugListener.pathAddedForGuaranteedOperation(details); } if ( client.getState() == CuratorFrameworkState.STARTED ) { log.debug("Details being added to guaranteed operation set: " + details); try { executeGuaranteedOperationInBackground(details); } catch ( Exception e ) { ThreadUtils.checkInterrupted(e); addFailedOperation(details); } } }
/** * Uploads configuration to Zookeeper. * @throws Exception */ private void upload() throws Exception { assert zookeeperURL != null; try(CuratorFramework client = getClient(zookeeperURL)) { if(client.getState() != CuratorFrameworkState.STARTED) { client.start(); } if (globals != null) { writeGlobalConfigToZookeeper(globals, client); } } }
/** * Start the registration of the {@link #candidate} for leader election. */ @Override public void start() { synchronized (this.lifecycleMonitor) { 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; logger.debug("Started LeaderInitiator"); } } }
@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(); }
public ClusterZKImpl(CuratorFramework zkClient, String clusterName) { this.client = zkClient; this.clusterName = clusterName; if (client.getState().equals(CuratorFrameworkState.LATENT)) { client.start(); } }