private Meter connectionStateMeter(final ConnectionState state) { return registry.meter(prefix + "connection_state_" + state.name()); } }
public ZooKeeperMetricsImpl(final String group, final MetricRegistry registry) { this.prefix = MetricRegistry.name(group, TYPE) + "."; this.registry = registry; this.transientErrorMeter = registry.meter(prefix + "transient_error_meter"); this.connectionStateChanged = registry.meter(prefix + "connection_state_changed"); // create all of the meter instances immediately so that we report 0 values after a restart for (final ConnectionState state : ConnectionState.values()) { connectionStateMeter(state); } }
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()); }); }
@Override public void stateChanged(CuratorFramework client, ConnectionState newState) { if (!newState.isConnected()) { LOG.error( " connection to zookeeper broken. It is in "+ newState.name()+" state."); } } };
@Override public Void apply(ConnectionStateListener listener) { listener.stateChanged(client, newState); return null; } }
void addStateChange(ConnectionState newConnectionState) { connectionStateManager.addStateChange(newConnectionState); }
/** * Change to {@link ConnectionState#SUSPENDED} only if not already suspended and not lost * * @return true if connection is set to SUSPENDED */ public synchronized boolean setToSuspended() { if ( state.get() != State.STARTED ) { return false; } if ( (currentConnectionState == ConnectionState.LOST) || (currentConnectionState == ConnectionState.SUSPENDED) ) { return false; } setCurrentConnectionState(ConnectionState.SUSPENDED); postState(ConnectionState.SUSPENDED); return true; }
@Override public boolean blockUntilConnected(int maxWaitTime, TimeUnit units) throws InterruptedException { return connectionStateManager.blockUntilConnected(maxWaitTime, units); }
@Override public Listenable<ConnectionStateListener> getConnectionStateListenable() { return connectionStateManager.getListenable(); }
boolean setToSuspended() { return connectionStateManager.setToSuspended(); }
connectionStateManager.close(); client.close();
public synchronized boolean blockUntilConnected(int maxWaitTime, TimeUnit units) throws InterruptedException { long startTime = System.currentTimeMillis(); boolean hasMaxWait = (units != null); long maxWaitTimeMs = hasMaxWait ? TimeUnit.MILLISECONDS.convert(maxWaitTime, units) : 0; while ( !isConnected() ) { if ( hasMaxWait ) { long waitTime = maxWaitTimeMs - (System.currentTimeMillis() - startTime); if ( waitTime <= 0 ) { return isConnected(); } wait(waitTime); } else { wait(); } } return isConnected(); }
int useSessionTimeoutMs = getUseSessionTimeoutMs(); long elapsedMs = startOfSuspendedEpoch == 0 ? useSessionTimeoutMs / 2 : System.currentTimeMillis() - startOfSuspendedEpoch; long pollMaxMs = useSessionTimeoutMs - elapsedMs; checkSessionExpiration();
int useSessionTimeoutMs = getUseSessionTimeoutMs(); if ( elapsedMs >= useSessionTimeoutMs )
public synchronized boolean isConnected() { return (currentConnectionState != null) && currentConnectionState.isConnected(); }
@Override public void stateChanged(final CuratorFramework client, final ConnectionState newState) { logger.info("{} Connection State changed to {}", this, newState.name()); if (newState == ConnectionState.SUSPENDED || newState == ConnectionState.LOST) { if (leader == true) { logger.info("Because Connection State was changed to {}, will relinquish leadership for role '{}'", newState, roleName); } leader = false; } super.stateChanged(client, newState); }
void checkInstanceIndex() { long instanceIndex = client.getInstanceIndex(); long newInstanceIndex = currentInstanceIndex.getAndSet(instanceIndex); if ( (newInstanceIndex >= 0) && (instanceIndex != newInstanceIndex) ) // currentInstanceIndex is initially -1 - ignore this { connectionStateManager.addStateChange(ConnectionState.LOST); } }
/** * Post a state change. If the manager is already in that state the change * is ignored. Otherwise the change is queued for listeners. * * @param newConnectionState new state * @return true if the state actually changed, false if it was already at that state */ public synchronized boolean addStateChange(ConnectionState newConnectionState) { if ( state.get() != State.STARTED ) { return false; } ConnectionState previousState = currentConnectionState; if ( previousState == newConnectionState ) { return false; } setCurrentConnectionState(newConnectionState); ConnectionState localState = newConnectionState; boolean isNegativeMessage = ((newConnectionState == ConnectionState.LOST) || (newConnectionState == ConnectionState.SUSPENDED) || (newConnectionState == ConnectionState.READ_ONLY)); if ( !isNegativeMessage && initialConnectMessageSent.compareAndSet(false, true) ) { localState = ConnectionState.CONNECTED; } postState(localState); return true; }
@Override public void stateChanged(CuratorFramework curatorFramework, ConnectionState connectionState) { if (!connectionState.isConnected() && executorService != null) { executorService.shutdown(); executorService = null; } } }
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); } }