@Override public void stateChanged(CuratorFramework client, ConnectionState newState) { if (newState == ConnectionState.CONNECTED) { isConnected.set(true); if (!isFirstConnection.get()) { for (ConnectionStateListener listener : listenerStateProxy.getListeners()) { listener.stateChanged(client, ConnectionState.RECONNECTED); } } return; } if (newState == ConnectionState.LOST) { isConnected.set(false); isFirstConnection.set(false); retryConnection(); } }
/** * This implementation is done in parallel because some listeners can hang the thread for a few minutes * while waiting for the others. * @param client * @param newState */ @Override public void stateChanged(CuratorFramework client, ConnectionState newState) { if (this.client instanceof RedirectorCuratorFramework) { if (((RedirectorCuratorFramework) this.client).getConnection().equals(client)) { currentConnectionState = newState; } } else { if (this.client.equals(client)) { currentConnectionState = newState; } } Metrics.reportConnectionState(newState.name()); connectionListeners.forEach(listener -> listenerParallelRunner.submit( (Runnable) () -> listener.stateChanged(getConnectorState(newState)))); }
public CuratorZookeeperClient(URL url) { super(url); try { Builder builder = CuratorFrameworkFactory.builder() .connectString(url.getBackupAddress()) .retryPolicy(new RetryNTimes(Integer.MAX_VALUE, 1000)) .connectionTimeoutMs(5000); String authority = url.getAuthority(); if (authority != null && authority.length() > 0) { builder = builder.authorization("digest", authority.getBytes()); } client = builder.build(); client.getConnectionStateListenable().addListener((client, 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); } }
/** * some state make effect to the lock, we have to handle it. * * @param client curator * @param newState the state of connection to the io.vertx.spi.cluster.impl.zookeeper.zookeeper. */ @Override public void stateChanged(CuratorFramework client, ConnectionState newState) { switch (newState) { case LOST: //release locks and clean locks locks.values().stream().forEach(ZKLock::release); locks.clear(); break; case SUSPENDED: //just release locks on this node. locks.values().stream().forEach(ZKLock::release); break; case RECONNECTED: break; } }
@Override public void stateChanged(CuratorFramework client, ConnectionState newState) { switch (newState) { case LOST: case SUSPENDED: Collection<InterProcessMutex> oldLocks = new ArrayList<>(locks.values()); locks.clear(); oldLocks.stream().parallel().forEach(lock -> { try { lock.release(); } catch (Exception e) { logger.trace("Can't release lock on " + newState); } }); break; default: } }
@Override public void destroy() { if (connectionState == ConnectionState.CONNECTED) { for (ServiceDescription service : services.values()) { String path = pathForProvider(service.getName(), service.getId()); try { client.delete().forPath(path); } catch (Exception ignore) { // ignore } } } services.clear(); client.getConnectionStateListenable().removeListener(connectionStateListener); client.close(); }
/** * {@inheritDoc} * @see org.apache.curator.framework.state.ConnectionStateListener#stateChanged(org.apache.curator.framework.CuratorFramework, org.apache.curator.framework.state.ConnectionState) */ @Override public void stateChanged(final CuratorFramework client, final ConnectionState newState) { SimpleLogger.log("ZK Connection State Change to [%s]", newState.name()); connected.set(newState.isConnected()); switch(newState) { case CONNECTED: registerPending(); dropConnectLatches(); break; case LOST: setAllPending(); break; case READ_ONLY: break; case RECONNECTED: registerPending(); dropConnectLatches(); break; case SUSPENDED: break; default: break; } }
/** * {@inheritDoc} * @see org.apache.curator.framework.state.ConnectionStateListener#stateChanged(org.apache.curator.framework.CuratorFramework, org.apache.curator.framework.state.ConnectionState) */ @Override public void stateChanged(final CuratorFramework client, final ConnectionState newState) { log.info("ZK Connection State Change to [{}]", newState.name()); connected.set(newState.isConnected()); switch(newState) { case CONNECTED: registerPending(); break; case LOST: setAllPending(); break; case READ_ONLY: break; case RECONNECTED: registerPending(); break; case SUSPENDED: break; default: break; } }
/** * {@inheritDoc} * @see org.apache.curator.framework.state.ConnectionStateListener#stateChanged(org.apache.curator.framework.CuratorFramework, org.apache.curator.framework.state.ConnectionState) */ @Override public void stateChanged(final CuratorFramework client, final ConnectionState newState) { log.info("ZK Connection State Change to [{}]", newState.name()); connected.set(newState.isConnected()); switch(newState) { case CONNECTED: sendNotification(new Notification(NOTIF_SERVICE_CONNECT, OBJECT_NAME, notifSerial.incrementAndGet(), System.currentTimeMillis(), "EndpointListener connected to Zookeeper at [" + zkConnect + "]")); break; case LOST: final int lostEndpoints = registered.size(); registered.clear(); sendNotification(new Notification(NOTIF_SERVICE_DISCONNECT, OBJECT_NAME, notifSerial.incrementAndGet(), System.currentTimeMillis(), "EndpointListener lost connection to Zookeeper [" + zkConnect + "]. Lost [" + lostEndpoints + "] endpoints")); break; case READ_ONLY: break; case RECONNECTED: sendNotification(new Notification(NOTIF_SERVICE_RECONNECT, OBJECT_NAME, notifSerial.incrementAndGet(), System.currentTimeMillis(), "EndpointListener re-connected to Zookeeper at [" + zkConnect + "]")); break; case SUSPENDED: break; default: break; } }
/** * {@inheritDoc} * @see org.apache.curator.framework.state.ConnectionStateListener#stateChanged(org.apache.curator.framework.CuratorFramework, org.apache.curator.framework.state.ConnectionState) */ @Override public void stateChanged(final CuratorFramework client, final ConnectionState newState) { final ConnectionState cs = cfState.getAndSet(newState); log.info("cfState transition: [{}] --> [{}]", cs, newState); switch(newState) { case CONNECTED: break; case LOST: break; case READ_ONLY: break; case RECONNECTED: break; case SUSPENDED: break; default: break; } }
@Override public void stateChanged(CuratorFramework client, ConnectionState newState) { logger.debug("StateChanged: {}", newState); switch (newState) { case SUSPENDED: resignLeadership(); break; case RECONNECTED: try { takeLeadership(); } catch (InterruptedException e) { logger.error("While handling RECONNECTED", e); } break; case LOST: resignLeadership(); break; } }
@Override public void stateChanged(CuratorFramework framework, ConnectionState connectionState) { switch (connectionState) { case CONNECTED: metricUpdater.incZKConnected(); break; case SUSPENDED: metricUpdater.incZKSuspended(); break; case RECONNECTED: metricUpdater.incZKReconnected(); break; case LOST: metricUpdater.incZKConnectionLost(); break; case READ_ONLY: // NOTE: Should not be relevant for configserver. break; } }
@Override public void stateChanged(final CuratorFramework client, final ConnectionState newState) { JobScheduleController jobScheduleController = JobRegistry.getInstance().getJobScheduleController(jobName); if (ConnectionState.LOST == newState) { jobScheduleController.pauseJob(); } else if (ConnectionState.RECONNECTED == newState) { if (!leaderElectionService.hasLeader()) { leaderElectionService.leaderElection(); } serverService.persistServerOnline(); executionService.clearRunningInfo(shardingService.getLocalHostShardingItems()); if (!serverService.isJobPausedManually()) { jobScheduleController.resumeJob(); } } }
@Override public long getAndIncrement() throws Exception { while (true) { ConnectionState connState = connStateListener.getLastState(); if (connState != null) { throw new IllegalStateException("Connection state: " + connState); } VersionedValue<Integer> current = sharedCount.getVersionedValue(); int newCount = current.getValue() + 1; if (newCount < 0) { // overflow and wrap around throw new Exception("Checkpoint counter overflow. ZooKeeper checkpoint counter only supports " + "checkpoints Ids up to " + Integer.MAX_VALUE); } if (sharedCount.trySetCount(current, newCount)) { return current.getValue(); } } }
@Override public void setCount(long newId) throws Exception { ConnectionState connState = connStateListener.getLastState(); if (connState != null) { throw new IllegalStateException("Connection state: " + connState); } if (newId > Integer.MAX_VALUE) { throw new IllegalArgumentException("ZooKeeper checkpoint counter only supports " + "checkpoints Ids up to " + Integer.MAX_VALUE + ", but given value is" + newId); } sharedCount.setCount((int) newId); }
protected void handleStateChange(ConnectionState newState) { switch (newState) { case CONNECTED: LOG.debug("Connected to ZooKeeper quorum. Leader election can start."); break; case SUSPENDED: LOG.warn("Connection to ZooKeeper suspended. The contender " + leaderContender.getAddress() + " no longer participates in the leader election."); break; case RECONNECTED: LOG.info("Connection to ZooKeeper was reconnected. Leader election can be restarted."); break; case LOST: // Maybe we have to throw an exception here to terminate the JobManager LOG.warn("Connection to ZooKeeper lost. The contender " + leaderContender.getAddress() + " no longer participates in the leader election."); break; } }
protected void handleStateChange(ConnectionState newState) { switch (newState) { case CONNECTED: LOG.debug("Connected to ZooKeeper quorum. Leader retrieval can start."); break; case SUSPENDED: LOG.warn("Connection to ZooKeeper suspended. Can no longer retrieve the leader from " + "ZooKeeper."); break; case RECONNECTED: LOG.info("Connection to ZooKeeper was reconnected. Leader retrieval can be restarted."); break; case LOST: LOG.warn("Connection to ZooKeeper lost. Can no longer retrieve the leader from " + "ZooKeeper."); break; } }
@Override public void stateChanged(CuratorFramework client, ConnectionState state) { switch (state) { case RECONNECTED: { logger.warn("Reconnection to zookeeper " + zkServers); } break; case LOST: { logger.error("Connection lost to zookeeper " + zkServers); } break; case CONNECTED: { logger.info("Connection to zookeeper " + zkServers); } break; case SUSPENDED: { logger.error("Connection suspended to zookeeper " + zkServers); } break; } }
@Override public ListenableFuture<Boolean> setMetricsCacheLocation( TopologyMaster.MetricsCacheLocation location, String topologyName) { client.getConnectionStateListenable().addListener(new ConnectionStateListener() { @Override public void stateChanged(CuratorFramework arg0, ConnectionState arg1) { if (arg1 != ConnectionState.CONNECTED) { // if not the first time successful connection, fail fast throw new RuntimeException("Unexpected state change to: " + arg1.name()); } } }); return createNode( StateLocation.METRICSCACHE_LOCATION, topologyName, location.toByteArray(), true); }
@Override protected void before() throws Throwable { ts = new TestingServer(); curator = CuratorFrameworkFactory.builder() .namespace("ezkr") .connectString(ts.getConnectString()) .retryPolicy(new BoundedExponentialBackoffRetry(10, 100, 7)) .build(); curator.getConnectionStateListenable().addListener(new ConnectionStateListener() { @Override public void stateChanged(final CuratorFramework client, final ConnectionState newState) { } }); curator.start(); }
void validateConnection(Watcher.Event.KeeperState state) { if ( state == Watcher.Event.KeeperState.Disconnected ) { internalConnectionHandler.suspendConnection(this); } else if ( state == Watcher.Event.KeeperState.Expired ) { connectionStateManager.addStateChange(ConnectionState.LOST); } else if ( state == Watcher.Event.KeeperState.SyncConnected ) { internalConnectionHandler.checkNewConnection(this); connectionStateManager.addStateChange(ConnectionState.RECONNECTED); unSleepBackgroundOperations(); } else if ( state == Watcher.Event.KeeperState.ConnectedReadOnly ) { internalConnectionHandler.checkNewConnection(this); connectionStateManager.addStateChange(ConnectionState.READ_ONLY); } }
@Test public void testInjectedWatchedEvent() throws Exception { Assert.assertEquals(states.poll(timing.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.CONNECTED); final CountDownLatch latch = new CountDownLatch(1); Watcher watcher = new Watcher() { @Override public void process(WatchedEvent event) { if ( event.getType() == Event.EventType.None ) { if ( event.getState() == Event.KeeperState.Expired ) { latch.countDown(); } } } }; client.checkExists().usingWatcher(watcher).forPath("/"); server.stop(); Assert.assertTrue(timing.forSessionSleep().awaitLatch(latch)); }
@Test public void testReconnectWithoutExpiration() throws Exception { Assert.assertEquals(states.poll(timing.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.CONNECTED); server.stop(); try { client.checkExists().forPath("/"); // any API call that will invoke the retry policy, etc. } catch ( KeeperException.ConnectionLossException ignore ) { } Assert.assertEquals(states.poll(timing.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.SUSPENDED); server.restart(); client.checkExists().forPath("/"); Assert.assertEquals(states.poll(timing.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.RECONNECTED); }
@Override public void addListener(final SharedCountListener listener, Executor executor) { SharedValueListener valueListener = new SharedValueListener() { @Override public void valueHasChanged(SharedValueReader sharedValue, byte[] newValue) throws Exception { listener.countHasChanged(SharedCount.this, fromBytes(newValue)); } @Override public void stateChanged(CuratorFramework client, ConnectionState newState) { listener.stateChanged(client, newState); } }; sharedValue.getListenable().addListener(valueListener, executor); listeners.put(listener, valueListener); }
private BlockingQueueConsumer<String> makeConsumer(final CountDownLatch latch) { ConnectionStateListener connectionStateListener = new ConnectionStateListener() { @Override public void stateChanged(CuratorFramework client, ConnectionState newState) { } }; return new BlockingQueueConsumer<String>(connectionStateListener) { @Override public void consumeMessage(String message) throws Exception { if ( latch != null ) { latch.await(); } super.consumeMessage(message); } }; }
@Override public void stateChanged(CuratorFramework client, ConnectionState newState) { if ( (newState == ConnectionState.RECONNECTED) || (newState == ConnectionState.CONNECTED) ) { try { log.debug("Re-registering due to reconnection"); reRegisterServices(); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } catch ( Exception e ) { log.error("Could not re-register instances after reconnection", e); } } }
private CuratorFramework buildCuratorWithZookeeperDirectly(Configuration configuration) { LOGGER.debug("configuring direct zookeeper connection."); CuratorFramework curator = CuratorFrameworkFactory.newClient( this.zookeeperConnectionString, configuration.getInt(ZOOKEEPER_SESSION_TIMEOUT_MILLIS.getPropertyName()), configuration.getInt(ZOOKEEPER_CONNECTION_TIMEOUT_MILLIS.getPropertyName()), buildZookeeperRetryPolicy(configuration)); curator.getConnectionStateListenable().addListener(new ConnectionStateListener() { public void stateChanged(CuratorFramework client, ConnectionState newState) { LOGGER.debug("Connection state to ZooKeeper changed: " + newState); } }); return curator; }
private static QueueConsumer<String> createQueueConsumer() { return new QueueConsumer<String>(){ @Override public void stateChanged(CuratorFramework client, ConnectionState newState) { System.out.println("connection new state: " + newState.name()); } @Override public void consumeMessage(String message) throws Exception { System.out.println("consume one message: " + message); } }; }
private static QueueConsumer<String> createQueueConsumer() { return new QueueConsumer<String>() { @Override public void stateChanged(CuratorFramework client, ConnectionState newState) { System.out.println("connection new state: " + newState.name()); } @Override public void consumeMessage(String message) throws Exception { System.out.println(new Date().getTime() + ": consume one message: " + message); } }; }
private ZookeeperClient(){ //1000 是重试间隔时间基数,3 是重试次数 RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); zkClient = createWithOptions(zkConnectionString, retryPolicy, 2000, 10000); zkClient.getConnectionStateListenable().addListener(new ConnectionStateListener() { @Override public void stateChanged(CuratorFramework client, ConnectionState newState) { logger.info("CuratorFramework state changed: {}", newState); if(newState == ConnectionState.CONNECTED || newState == ConnectionState.RECONNECTED){ for (String key : listeners.keySet()) { System.out.println(key); IZoopkeeperListener listener = listeners.get(key); listener.execute(client); } } } }); zkClient.getUnhandledErrorListenable().addListener(new UnhandledErrorListener() { @Override public void unhandledError(String message, Throwable e) { logger.info("CuratorFramework unhandledError: {}", message); } }); zkClient.start(); }
public RestablishingKeeper(String hostList) { RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 5); client = CuratorFrameworkFactory.newClient(hostList, retryPolicy); client.getConnectionStateListenable().addListener(new ConnectionStateListener(){ @Override public void stateChanged(CuratorFramework framework, ConnectionState state) { LOGGER.debug("State change "+ state); if (state.equals(ConnectionState.CONNECTED) || state.equals(ConnectionState.RECONNECTED)){ reEstablished.incrementAndGet(); try { onReconnect(framework.getZookeeperClient().getZooKeeper(), framework); } catch (Exception e) { throw new RuntimeException(e); } } }}); }
@Override public void stateChanged(CuratorFramework client, ConnectionState newState) { mIsLeader.set(false); if ((newState == ConnectionState.LOST) || (newState == ConnectionState.SUSPENDED)) { if (mCurrentMasterThread != null) { mCurrentMasterThread.interrupt(); } } else { try { LOG.info("The current leader is " + LEADER_SELECTOR.getLeader().getId()); } catch (Exception e) { LOG.error(e.getMessage(), e); } } }
@Override protected Result check() throws Exception { final ConnectionState currentConnectionState = connectionState.get(); if (currentConnectionState == null) { return Result.unhealthy("Connection state is null"); } switch (currentConnectionState) { case CONNECTED: case RECONNECTED: return Result.healthy(currentConnectionState.name()); default: return Result.unhealthy(currentConnectionState.name()); } }
@Inject public StatusResource(LocalLbAdapter adapter, LoadBalancerConfiguration loadBalancerConfiguration, BaragonAgentMetadata agentMetadata, AtomicReference<BaragonAgentState> agentState, @Named(BaragonAgentServiceModule.AGENT_LEADER_LATCH) LeaderLatch leaderLatch, @Named(BaragonAgentServiceModule.AGENT_MOST_RECENT_REQUEST_ID) AtomicReference<String> mostRecentRequestId, @Named(BaragonDataModule.BARAGON_ZK_CONNECTION_STATE) AtomicReference<ConnectionState> connectionState, @Named(BaragonAgentServiceModule.CONFIG_ERROR_MESSAGE) AtomicReference<Optional<String>> errorMessage) { this.adapter = adapter; this.loadBalancerConfiguration = loadBalancerConfiguration; this.leaderLatch = leaderLatch; this.mostRecentRequestId = mostRecentRequestId; this.connectionState = connectionState; this.agentMetadata = agentMetadata; this.errorMessage = errorMessage; this.agentState = agentState; }
@GET @NoAuth public BaragonAgentStatus getStatus(@DefaultValue("false") @QueryParam("skipCache") boolean skipCache) { if (skipCache) { try { adapter.checkConfigs(); errorMessage.set(Optional.<String>absent()); } catch (InvalidConfigException e) { errorMessage.set(Optional.of(e.getMessage())); } } final ConnectionState currentConnectionState = connectionState.get(); final String connectionStateString = currentConnectionState == null ? "UNKNOWN" : currentConnectionState.name(); Optional<String> currentErrorMessage = errorMessage.get(); return new BaragonAgentStatus(loadBalancerConfiguration.getName(), !currentErrorMessage.isPresent(), currentErrorMessage, leaderLatch.hasLeadership(), mostRecentRequestId.get(), connectionStateString, agentMetadata, agentState.get()); }