@Override public boolean isConnected() { if (_zkclient == null) { return false; } ZkConnection zkconnection = (ZkConnection) _zkclient.getConnection(); if (zkconnection != null) { States state = zkconnection.getZookeeperState(); return state == States.CONNECTED; } return false; }
/** * wait until we get a non-zero session-id. note that we might lose zkconnection right after we * read session-id. but it's ok to get stale session-id and we will have another * handle-new-session callback to correct this. */ void waitUntilConnected() { boolean isConnected; do { isConnected = _zkclient.waitUntilConnected(ZkClient.DEFAULT_CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS); if (!isConnected) { LOG.error( "fail to connect zkserver: " + _zkAddress + " in " + ZkClient.DEFAULT_CONNECTION_TIMEOUT + "ms. expiredSessionId: " + _sessionId + ", clusterName: " + _clusterName); continue; } ZkConnection zkConnection = ((ZkConnection) _zkclient.getConnection()); _sessionId = Long.toHexString(zkConnection.getZookeeper().getSessionId()); /** * at the time we read session-id, zkconnection might be lost again wait until we get a * non-zero session-id */ } while ("0".equals(_sessionId)); LOG.info( "Handling new session, session id: " + _sessionId + ", instance: " + _instanceName + ", instanceTye: " + _instanceType + ", cluster: " + _clusterName + ", zkconnection: " + ((ZkConnection) _zkclient.getConnection()).getZookeeper()); }
@Override public void handleStateChanged(KeeperState state) throws Exception { switch (state) { case SyncConnected: ZkConnection zkConnection = (ZkConnection) _zkclient.getConnection(); LOG.info("KeeperState: " + state + ", zookeeper:" + zkConnection.getZookeeper()); break; case Disconnected: LOG.info( "KeeperState:" + state + ", disconnectedSessionId: " + _sessionId + ", instance: " + _instanceName + ", type: " + _instanceType); /** * Track the time stamp that the disconnected happens, then check history and see if we * should disconnect the helix-manager */ _disconnectTimeHistory.add(System.currentTimeMillis()); if (isFlapping()) { LOG.error( "instanceName: " + _instanceName + " is flapping. diconnect it. " + " maxDisconnectThreshold: " + _maxDisconnectThreshold + " disconnects in " + _flappingTimeWindowMs + "ms."); disconnect(); } break; case Expired: LOG.info( "KeeperState:" + state + ", expiredSessionId: " + _sessionId + ", instance: " + _instanceName + ", type: " + _instanceType); break; default: break; } }