@Override public IZkConnection getConnection() { if (isClosed()) { return IDLE_CONNECTION; } return super.getConnection(); }
public static ZkClient getZkClient(String zkServer) { // happy path that we cache the zkclient and it's still connected if (_zkClientMap.containsKey(zkServer)) { ZkClient zkClient = _zkClientMap.get(zkServer); if (zkClient.getConnection().getZookeeperState() == States.CONNECTED) { return zkClient; } } synchronized (_zkClientMap) { // if we cache a stale zkclient, purge it if (_zkClientMap.containsKey(zkServer)) { ZkClient zkClient = _zkClientMap.get(zkServer); if (zkClient.getConnection().getZookeeperState() != States.CONNECTED) { _zkClientMap.remove(zkServer); } } // get a new zkclient if (!_zkClientMap.containsKey(zkServer)) { ZkClient zkClient = new ZkClient(zkServer, DEFAULT_SESSION_TIMEOUT, ZkClient.DEFAULT_CONNECTION_TIMEOUT, new ZNRecordSerializer()); _zkClientMap.put(zkServer, zkClient); } return _zkClientMap.get(zkServer); } }
public static ZkClient getZkClient(String zkServer) { // happy path that we cache the zkclient and it's still connected if (_zkClientMap.containsKey(zkServer)) { ZkClient zkClient = _zkClientMap.get(zkServer); if (zkClient.getConnection().getZookeeperState() == States.CONNECTED) { return zkClient; } } synchronized (_zkClientMap) { // if we cache a stale zkclient, purge it if (_zkClientMap.containsKey(zkServer)) { ZkClient zkClient = _zkClientMap.get(zkServer); if (zkClient.getConnection().getZookeeperState() != States.CONNECTED) { _zkClientMap.remove(zkServer); } } // get a new zkclient if (!_zkClientMap.containsKey(zkServer)) { ZkClient zkClient = new ZkClient(zkServer, DEFAULT_SESSION_TIMEOUT, ZkClient.DEFAULT_CONNECTION_TIMEOUT, new ZNRecordSerializer()); _zkClientMap.put(zkServer, zkClient); } return _zkClientMap.get(zkServer); } }
/** * 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 (!isConnected || "0".equals(_sessionId)); LOG.info("Handling new session, session id: " + _sessionId + ", instance: " + _instanceName + ", instanceTye: " + _instanceType + ", cluster: " + _clusterName + ", zkconnection: " + ((ZkConnection) _zkclient.getConnection()).getZookeeper()); }
if (_zkClient != null && _zkClient.getConnection() != null)
switch (state) { case SyncConnected: ZkConnection zkConnection = (ZkConnection) _zkclient.getConnection(); LOG.info("KeeperState: " + state + ", instance: " + _instanceName + ", type: " + _instanceType + ", zookeeper:" + zkConnection.getZookeeper());
@Override public void handleNewSession() throws Exception { // make sure zkclient is connected again zkClient.waitUntilConnected(HelixZkClient.DEFAULT_CONNECTION_TIMEOUT, TimeUnit.SECONDS); ZkConnection connection = ((ZkConnection) zkClient.getConnection()); ZooKeeper curZookeeper = connection.getZookeeper(); LOG.info("handleNewSession. sessionId: " + Long.toHexString(curZookeeper.getSessionId())); waitNewSession.countDown(); }
/** * Get zk connection session id * @param client * @return */ public static String getSessionId(HelixZkClient client) { ZkConnection connection = (ZkConnection) ((ZkClient) client).getConnection(); ZooKeeper curZookeeper = connection.getZookeeper(); return Long.toHexString(curZookeeper.getSessionId()); }
@Override public void handleNewSession() throws Exception { // make sure zkclient is connected again zkClient.waitUntilConnected(HelixZkClient.DEFAULT_CONNECTION_TIMEOUT, TimeUnit.SECONDS); ZkConnection connection = ((ZkConnection) zkClient.getConnection()); ZooKeeper curZookeeper = connection.getZookeeper(); LOG.info("handleNewSession. sessionId: " + Long.toHexString(curZookeeper.getSessionId())); }
public static Map<String, List<String>> getZkWatch(HelixZkClient client) throws Exception { Map<String, List<String>> lists = new HashMap<String, List<String>>(); ZkClient zkClient = (ZkClient) client; ZkConnection connection = ((ZkConnection) zkClient.getConnection()); ZooKeeper zk = connection.getZookeeper(); java.lang.reflect.Field field = getField(zk.getClass(), "watchManager"); field.setAccessible(true); Object watchManager = field.get(zk); java.lang.reflect.Field field2 = getField(watchManager.getClass(), "dataWatches"); field2.setAccessible(true); HashMap<String, Set<Watcher>> dataWatches = (HashMap<String, Set<Watcher>>) field2.get(watchManager); field2 = getField(watchManager.getClass(), "existWatches"); field2.setAccessible(true); HashMap<String, Set<Watcher>> existWatches = (HashMap<String, Set<Watcher>>) field2.get(watchManager); field2 = getField(watchManager.getClass(), "childWatches"); field2.setAccessible(true); HashMap<String, Set<Watcher>> childWatches = (HashMap<String, Set<Watcher>>) field2.get(watchManager); lists.put("dataWatches", new ArrayList<>(dataWatches.keySet())); lists.put("existWatches", new ArrayList<>(existWatches.keySet())); lists.put("childWatches", new ArrayList<>(childWatches.keySet())); return lists; }
/** * expire zk session asynchronously * @param client * @throws Exception */ public static void asyncExpireSession(HelixZkClient client) throws Exception { final ZkClient zkClient = (ZkClient) client; ZkConnection connection = ((ZkConnection) zkClient.getConnection()); ZooKeeper curZookeeper = connection.getZookeeper(); LOG.info("Before expiry. sessionId: " + Long.toHexString(curZookeeper.getSessionId())); Watcher watcher = new Watcher() { @Override public void process(WatchedEvent event) { LOG.info("Process watchEvent: " + event); } }; final ZooKeeper dupZookeeper = new ZooKeeper(connection.getServers(), curZookeeper.getSessionTimeout(), watcher, curZookeeper.getSessionId(), curZookeeper.getSessionPasswd()); // wait until connected, then close while (dupZookeeper.getState() != States.CONNECTED) { Thread.sleep(10); } dupZookeeper.close(); connection = (ZkConnection) zkClient.getConnection(); curZookeeper = connection.getZookeeper(); // System.err.println("zk: " + oldZookeeper); LOG.info("After expiry. sessionId: " + Long.toHexString(curZookeeper.getSessionId())); }
ZkConnection connection = ((ZkConnection) _zkClient.getConnection()); ZooKeeper zookeeper = connection.getZookeeper(); System.out.println("old sessionId= " + zookeeper.getSessionId()); newZookeeper.close(); Thread.sleep(10000); connection = ((ZkConnection) _zkClient.getConnection()); zookeeper = connection.getZookeeper(); System.out.println("After session expiry sessionId= " + zookeeper.getSessionId());
ZkConnection connection = ((ZkConnection) zkClient.getConnection()); ZooKeeper oldZookeeper = connection.getZookeeper(); LOG.info("Old sessionId = " + oldZookeeper.getSessionId()); connection = (ZkConnection) zkClient.getConnection(); oldZookeeper = connection.getZookeeper(); LOG.info("After session expiry sessionId = " + oldZookeeper.getSessionId());
+ idealState); } finally { if (zkClient != null && zkClient.getConnection() != null)
ZkConnection connection = ((ZkConnection) zkClient.getConnection()); ZooKeeper curZookeeper = connection.getZookeeper(); String oldSessionId = Long.toHexString(curZookeeper.getSessionId()); zkClient.unsubscribeStateChanges(listener); connection = (ZkConnection) zkClient.getConnection(); curZookeeper = connection.getZookeeper();
ZkConnection connection = ((ZkConnection) zkClient.getConnection()); ZooKeeper curZookeeper = connection.getZookeeper(); LOG.info("Before expiry. sessionId: " + Long.toHexString(curZookeeper.getSessionId())); connection = (ZkConnection) zkClient.getConnection(); curZookeeper = connection.getZookeeper(); zkClient.unsubscribeStateChanges(listener);