protected String getPrompt() { return "[zk: " + host + "("+zk.getState()+")" + " " + commandCount + "] "; }
public synchronized States getState() { return zk == null ? null : zk.getState(); }
protected String getPrompt() { return "[zk: " + host + "("+zk.getState()+")" + " " + commandCount + "] "; }
private ZooKeeper getZk() throws IOException { // may be closed when session expired if (zookeeper == null || !zookeeper.getState().isAlive()) { zookeeper = new ZooKeeper(connectString, sessionTimeoutMs, e -> {}); } return zookeeper; }
synchronized void ensureConnected(){ while(zk.getState()!=ZooKeeper.States.CONNECTED){ try { wait(); } catch (InterruptedException e) { return; } } }
public static void waitForOne(ZooKeeper zk, ZooKeeper.States state) throws InterruptedException { int iterations = ClientBase.CONNECTION_TIMEOUT / 500; while (zk.getState() != state) { if (iterations-- == 0) { throw new RuntimeException("Waiting too long " + zk.getState() + " != " + state); } Thread.sleep(500); } }
/** {@inheritDoc} */ @Override public void run() { try { zkClient.exists(paths.clusterDir, false); } catch (Throwable t) { if (zkClient.getState().isAlive()) U.warn(log, "Failed to ping Zookeeper.", t); else scheduler.cancel(); } }
private void waitForOne(ZooKeeper zk, ArrayList<States> states) throws InterruptedException { int iterations = ClientBase.CONNECTION_TIMEOUT / 500; while (!states.contains(zk.getState())) { if (iterations-- == 0) { LOG.info("state is {}", zk.getState()); throw new RuntimeException("Waiting too long"); } Thread.sleep(500); } }
@Override public ZooKeeper.States getZookeeperState() { try { return curator.getZookeeperClient().getZooKeeper().getState(); } catch ( Exception e ) { throw new RuntimeException(e); } }
private void checkConnect() { if (zk == null || !zk.getState().isConnected()) { lock.lock(); try { conditionConnected.await(10000, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { LOGGER.error(e); } finally { lock.unlock(); } if (zk == null || !zk.getState().isConnected()) { throw new ZkException("zk not connected, please wait"); } } }
private void checkConnect() { if (zk == null || !zk.getState().isConnected()) { lock.lock(); try { conditionConnected.await(10000, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { LOGGER.error(e); } finally { lock.unlock(); } if (zk == null || !zk.getState().isConnected()) { throw new ZkException("zk not connected, please wait"); } } }
public static void logStates(ZooKeeper[] zks) { StringBuilder sbBuilder = new StringBuilder("Connection States: {"); for (int i = 0; i < zks.length; i++) { sbBuilder.append(i + " : " + zks[i].getState() + ", "); } sbBuilder.append('}'); LOG.error(sbBuilder.toString()); }
protected void connectToZK(String newHost) throws InterruptedException, IOException { if (zk != null && zk.getState().isAlive()) { zk.close(); } host = newHost; boolean readOnly = cl.getOption("readonly") != null; if (cl.getOption("secure") != null) { System.setProperty(ZKClientConfig.SECURE_CLIENT, "true"); System.out.println("Secure connection is enabled"); } zk = new ZooKeeperAdmin(host, Integer.parseInt(cl.getOption("timeout")), new MyWatcher(), readOnly); }
protected void connectToZK(String newHost) throws InterruptedException, IOException { if (zk != null && zk.getState().isAlive()) { zk.close(); } host = newHost; boolean readOnly = cl.getOption("readonly") != null; zk = new ZooKeeper(host, Integer.parseInt(cl.getOption("timeout")), new MyWatcher(), readOnly); }
/** * String representation of this ZooKeeper client. Suitable for things * like logging. * * Do NOT count on the format of this string, it may change without * warning. * * @since 3.3.0 */ @Override public String toString() { States state = getState(); return ("State:" + state.toString() + (state.isConnected() ? " Timeout:" + getSessionTimeout() + " " : " ") + cnxn); }
@Override public ZooKeeper.States getState() throws KeeperException { assertClusterIdFlagTrue(); try { return client.getZookeeperClient().getZooKeeper().getState(); } catch (Exception e) { throwIfInstanceOf(e, KeeperException.class); throw new RuntimeException(e); } }
public static void waitForAll(ZooKeeper[] zks, ZooKeeper.States state) throws InterruptedException { int iterations = ClientBase.CONNECTION_TIMEOUT / 1000; boolean someoneNotConnected = true; while (someoneNotConnected) { if (iterations-- == 0) { logStates(zks); ClientBase.logAllStackTraces(); throw new RuntimeException("Waiting too long"); } someoneNotConnected = false; for (ZooKeeper zk : zks) { if (zk.getState() != state) { someoneNotConnected = true; break; } } Thread.sleep(1000); } }
@Test public void testClientRetry() throws IOException, InterruptedException, TimeoutException{ CountdownWatcher cdw1 = new CountdownWatcher(); CountdownWatcher cdw2 = new CountdownWatcher(); ZooKeeper zk = new ZooKeeper(hostPort, 10000, cdw1); try { cdw1.waitForConnected(CONNECTION_TIMEOUT); ZooKeeper zk2 = new ZooKeeper(hostPort, 10000, cdw2); try { States s1 = zk.getState(); States s2 = zk2.getState(); Assert.assertSame(s1,States.CONNECTED); Assert.assertSame(s2,States.CONNECTING); cdw1.reset(); zk.close(); cdw1.waitForDisconnected(CONNECTION_TIMEOUT); cdw2.waitForConnected(CONNECTION_TIMEOUT); Assert.assertSame(zk2.getState(),States.CONNECTED); } finally { zk2.close(); } } finally { zk.close(); } } }
@Test(timeout = 60000) public void testZooKeeperReconnection() throws Exception { int sessionTimeoutMs = 100; ZooKeeperClient zkc = clientBuilder(sessionTimeoutMs).zkAclId(null).build(); ZooKeeper zk = zkc.get(); long sessionId = zk.getSessionId(); ZooKeeperClientUtils.expireSession(zkc, zkServers, 2 * sessionTimeoutMs); ZooKeeper newZk = zkc.get(); while (!ZooKeeper.States.CONNECTED.equals(newZk.getState())) { TimeUnit.MILLISECONDS.sleep(sessionTimeoutMs / 2); } long newSessionId = newZk.getSessionId(); assertTrue(newZk == zk); assertFalse(sessionId == newSessionId); }