@Override public Listenable<ConnectionStateListener> getConnectionStateListenable() { return client.getConnectionStateListenable(); }
@Override public void close() { if (clusterId != null) { client.getConnectionStateListenable().removeListener(connectionStateListener); } client.close(); }
private void startClient() { CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder(); curator = builder.connectString(zkServers).retryPolicy(new ExponentialBackoffRetry(1000,100)).build(); curator.getConnectionStateListenable().addListener(this); curator.start(); }
@Override public void stateChanged(CuratorFramework client, ConnectionState newState) { if (newState == ConnectionState.CONNECTED) { initialConnection.countDown(); client.getConnectionStateListenable().removeListener(this); } }
private void addConnectionStateListener() { client.getConnectionStateListenable().addListener((cli, newState) -> { if (newState == ConnectionState.RECONNECTED) { ephemeralNodes.forEach(this::reRegisterEphemeral); ephemeralSequentialNodes.forEach(this::reRegisterEphemeralSequential); } Logs.RSD.warn("zk connection state changed new state={}, isConnected={}", newState, newState.isConnected()); }); }
/** * 注册连接状态监听器. * * @param listener 连接状态监听器 */ public void addConnectionStateListener(final ConnectionStateListener listener) { getClient().getConnectionStateListenable().addListener(listener); }
public boolean start(long timeout, TimeUnit unit) throws Exception { Preconditions.checkState(state.compareAndSet(State.LATENT, State.INITIAL_SELECTION), "Already started"); CountDownLatch localLatch = initialCreateLatch.get(); client.getConnectionStateListenable().addListener(connectionStateListener); chooseSlotToTake(); startCreateCurrentNode(); return localLatch.await(timeout, unit); }
public CuratorZookeeperClient(URL url) { super(url); try { int timeout = url.getParameter(Constants.TIMEOUT_KEY, 5000); CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder() .connectString(url.getBackupAddress()) .retryPolicy(new RetryNTimes(1, 1000)) .connectionTimeoutMs(timeout); String authority = url.getAuthority(); if (authority != null && authority.length() > 0) { builder = builder.authorization("digest", authority.getBytes()); } client = builder.build(); client.getConnectionStateListenable().addListener(new ConnectionStateListener() { @Override public void stateChanged(CuratorFramework client, ConnectionState state) { if (state == ConnectionState.LOST) { CuratorZookeeperClient.this.stateChanged(StateListener.DISCONNECTED); } else if (state == ConnectionState.CONNECTED) { CuratorZookeeperClient.this.stateChanged(StateListener.CONNECTED); } else if (state == ConnectionState.RECONNECTED) { CuratorZookeeperClient.this.stateChanged(StateListener.RECONNECTED); } } }); client.start(); } catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } }
public CuratorZookeeperClient(URL url) { super(url); try { int timeout = url.getParameter(Constants.TIMEOUT_KEY, 5000); CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder() .connectString(url.getBackupAddress()) .retryPolicy(new RetryNTimes(1, 1000)) .connectionTimeoutMs(timeout); String authority = url.getAuthority(); if (authority != null && authority.length() > 0) { builder = builder.authorization("digest", authority.getBytes()); } client = builder.build(); client.getConnectionStateListenable().addListener(new ConnectionStateListener() { @Override public void stateChanged(CuratorFramework client, ConnectionState state) { if (state == ConnectionState.LOST) { CuratorZookeeperClient.this.stateChanged(StateListener.DISCONNECTED); } else if (state == ConnectionState.CONNECTED) { CuratorZookeeperClient.this.stateChanged(StateListener.CONNECTED); } else if (state == ConnectionState.RECONNECTED) { CuratorZookeeperClient.this.stateChanged(StateListener.RECONNECTED); } } }); client.start(); } catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } }
@Override public void close() throws IOException { State currentState = state.getAndSet(State.CLOSED); if (currentState == State.CLOSED || currentState == State.LATENT) return; client.getConnectionStateListenable().removeListener(connectionStateListener); String localNodePath = nodePath.getAndSet(null); if (localNodePath == null) return; try { client.delete().guaranteed().forPath(localNodePath); } catch (KeeperException.NoNodeException ignore) { } catch (Exception e) { LOG.error("Deleting node: " + localNodePath, e); throw new IOException(e); } }
/** * @param timeout - timeout for locking in milliseconds * @return DistributedLockService backed up by curator instance */ public DistributedLockService getDistributedLock(int timeout) { try { DistributedLockServiceCuratorImpl lock = new DistributedLockServiceCuratorImpl(curatorFramework, lockBasePath, timeout); curatorFramework.getConnectionStateListenable().addListener(lock); return lock; } catch (Exception e) { throw new RuntimeException(e); } }
@Override public CuratorFramework newClient(final String connectString, final int sessionTimeoutMs, final int connectionTimeoutMs, final RetryPolicy retryPolicy, final ACLProvider aclProvider, final List<AuthInfo> authorization) { final CuratorFramework curator = mock(CuratorFramework.class); final RetryLoop retryLoop = mock(RetryLoop.class); when(retryLoop.shouldContinue()).thenReturn(false); final CuratorZookeeperClient czkClient = mock(CuratorZookeeperClient.class); when(czkClient.newRetryLoop()).thenReturn(retryLoop); when(curator.getZookeeperClient()).thenReturn(czkClient); @SuppressWarnings("unchecked") final Listenable<ConnectionStateListener> mockListener = (Listenable<ConnectionStateListener>) mock(Listenable.class); when(curator.getConnectionStateListenable()).thenReturn(mockListener); final GetChildrenBuilder builder = mock(GetChildrenBuilder.class); when(curator.getChildren()).thenReturn(builder); try { when(builder.forPath(anyString())).thenThrow( new KeeperException.ConnectionLossException()); } catch (Exception ignored) { // never throws } when(curator.newNamespaceAwareEnsurePath(anyString())).thenReturn(mock(EnsurePath.class)); return curator; } }
@Override public void start() { if (client.getState() != CuratorFrameworkState.STARTED) { client.start(); if (clusterId != null) { client.getConnectionStateListenable().addListener(connectionStateListener); checkClusterIdExists(clusterId, "start"); } } }
@Override public void connectToRegistryServer(String connectString) { checkNotNull(connectString, "connectString"); configClient = CuratorFrameworkFactory.newClient( connectString, sessionTimeoutMs, connectionTimeoutMs, new ExponentialBackoffRetry(500, 20)); configClient.getConnectionStateListenable().addListener(new ConnectionStateListener() { @Override public void stateChanged(CuratorFramework client, ConnectionState newState) { logger.info("Zookeeper connection state changed {}.", newState); if (newState == ConnectionState.RECONNECTED) { logger.info("Zookeeper connection has been re-established, will re-subscribe and re-register."); // 重新订阅 for (RegisterMeta.ServiceMeta serviceMeta : getSubscribeSet()) { doSubscribe(serviceMeta); } // 重新发布服务 for (RegisterMeta meta : getRegisterMetaMap().keySet()) { ZookeeperRegistryService.super.register(meta); } } } }); configClient.start(); }
public void close() { if (zkCoord == null) { return; } zkCoord.getCurator().getConnectionStateListenable() .removeListener(stateListener); try { zkCoord.close(); } catch (Exception e) { ZKClusterCoordinator.logger.error("Error occurred on ZK close, ignored", e); } zkCoord = null; }
public CuratorZkClient(Config config) { String registryAddress = NodeRegistryUtils.getRealRegistryAddress(config.getRegistryAddress()); CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder() .connectString(registryAddress) .retryPolicy(new RetryNTimes(Integer.MAX_VALUE, 1000)) .connectionTimeoutMs(5000); client = builder.build(); client.getConnectionStateListenable().addListener(new ConnectionStateListener() { public void stateChanged(CuratorFramework client, ConnectionState state) { if (state == ConnectionState.LOST) { CuratorZkClient.this.stateChanged(StateListener.DISCONNECTED); } else if (state == ConnectionState.CONNECTED) { CuratorZkClient.this.stateChanged(StateListener.CONNECTED); } else if (state == ConnectionState.RECONNECTED) { CuratorZkClient.this.stateChanged(StateListener.RECONNECTED); } else if (state == ConnectionState.SUSPENDED) { CuratorZkClient.this.stateChanged(StateListener.DISCONNECTED); } } }); zkSerializer = new SerializableSerializer(); client.start(); }
public CuratorZkClient(Config config) { String registryAddress = NodeRegistryUtils.getRealRegistryAddress(config.getRegistryAddress()); CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder() .connectString(registryAddress) .retryPolicy(new RetryNTimes(Integer.MAX_VALUE, 1000)) .connectionTimeoutMs(5000); client = builder.build(); client.getConnectionStateListenable().addListener(new ConnectionStateListener() { public void stateChanged(CuratorFramework client, ConnectionState state) { if (state == ConnectionState.LOST) { CuratorZkClient.this.stateChanged(StateListener.DISCONNECTED); } else if (state == ConnectionState.CONNECTED) { CuratorZkClient.this.stateChanged(StateListener.CONNECTED); } else if (state == ConnectionState.RECONNECTED) { CuratorZkClient.this.stateChanged(StateListener.RECONNECTED); } else if (state == ConnectionState.SUSPENDED) { CuratorZkClient.this.stateChanged(StateListener.DISCONNECTED); } } }); zkSerializer = new SerializableSerializer(); client.start(); }
public ZKClusterCoordinator(String connect, String zkRoot, String clusterId, int retryCount, int retryDelayMs, int connectTimeoutMs) throws IOException { logger.debug("ZK connect: " + connect + ", zkRoot: " + zkRoot + ", clusterId: " + clusterId); this.serviceName = clusterId; RetryPolicy rp = new RetryNTimes(retryCount, retryDelayMs); curator = CuratorFrameworkFactory.builder().namespace(zkRoot) .connectionTimeoutMs(connectTimeoutMs).retryPolicy(rp) .connectString(connect).build(); curator.getConnectionStateListenable() .addListener(new InitialConnectionListener()); curator.start(); discovery = newDiscovery(); factory = CachingTransientStoreFactory .of(new ZkTransientStoreFactory(curator)); }
public PersistentPathChildrenCache(final CuratorFramework curator, final String path, final String clusterId, final Path snapshotFile, final JavaType valueType) throws IOException, InterruptedException { this.curator = curator; this.path = path; this.clusterId = clusterId; this.valueType = valueType; final MapType mapType = Json.typeFactory().constructMapType(HashMap.class, Json.type(String.class), valueType); final Supplier<Map<String, T>> empty = Suppliers.ofInstance(Collections.<String, T>emptyMap()); this.snapshot = PersistentAtomicReference.create(snapshotFile, mapType, empty); this.reactor = new DefaultReactor("zk-ppcc:" + path, new Update(), REFRESH_INTERVAL_MILLIS); curator.getConnectionStateListenable().addListener(new ConnectionListener()); }
public void afterPropertiesSet() throws Exception { if (this.initializeEnabled) { this.initializeIndexIfNecessary(); } this.curatorFramework.blockUntilConnected(); this.curatorFramework.getConnectionStateListenable().addListener(this); this.initializeClusterInstancesDirectory(); this.initializeClusterInstanceVersion(); this.initializeClusterInstanceConfig(true); }