void createClient() throws Exception { PathBasedZkSerializer zkSerializer = ChainedPathZkSerializer.builder(new ZNRecordStreamingSerializer()).build(); _zkclient = new ZkClient( _zkAddress, _sessionTimeout, ZkClient.DEFAULT_CONNECTION_TIMEOUT, zkSerializer); _baseDataAccessor = createBaseDataAccessor(); _dataAccessor = new ZKHelixDataAccessor(_clusterName, _instanceType, _baseDataAccessor); _configAccessor = new ConfigAccessor(_zkclient); int retryCount = 0; _zkclient.subscribeStateChanges(this); while (retryCount < 3) { try { _zkclient.waitUntilConnected(_sessionTimeout, TimeUnit.MILLISECONDS); handleStateChanged(KeeperState.SyncConnected); handleNewSession(); break; } catch (HelixException e) { LOG.error("fail to createClient.", e); throw e; } catch (Exception e) { retryCount++; LOG.error("fail to createClient. retry " + retryCount, e); if (retryCount == 3) { throw e; } } } }
/** * 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()); }