Java Code Examples for org.apache.zookeeper.ZooKeeper#getState()
The following examples show how to use
org.apache.zookeeper.ZooKeeper#getState() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ZooKeeperFactory.java From h2o-2 with Apache License 2.0 | 6 votes |
public static ZooKeeper makeZk(String zk) throws Exception { ZooKeeper z = new ZooKeeper(zk, Constants.SESSION_TIMEOUT_MILLIS, null); int trial = 0; while (true) { trial++; if (trial > 10) { throw new Exception("ZooKeeper tried too many times to reach CONNECTED state"); } if (z.getState() == ZooKeeper.States.CONNECTED) { return z; } Thread.sleep(1000); } }
Example 2
Source File: ZooKeeperConnection.java From javatech with Creative Commons Attribution Share Alike 4.0 International | 5 votes |
public static void main(String[] args) throws IOException, InterruptedException { ZooKeeperConnection zooKeeperConnection = new ZooKeeperConnection(); ZooKeeper zk = zooKeeperConnection.connect(HOST); States state = zk.getState(); System.out.println("ZooKeeper isAlive:" + state.isAlive()); zk.close(); }
Example 3
Source File: ZkTestHelper.java From helix with Apache License 2.0 | 5 votes |
/** * expire zk session asynchronously * @param client * @throws Exception */ public static void asyncExpireSession(RealmAwareZkClient 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())); }
Example 4
Source File: ZkTestHelper.java From helix with Apache License 2.0 | 5 votes |
/** * expire zk session asynchronously * @param client * @throws Exception */ public static void asyncExpireSession(RealmAwareZkClient 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())); }
Example 5
Source File: StateWatchingZooKeeper.java From hbase-indexer with Apache License 2.0 | 5 votes |
public StateWatchingZooKeeper(String connectString, int sessionTimeout, int startupTimeOut) throws IOException { this.requestedSessionTimeout = sessionTimeout; this.sessionTimeout = sessionTimeout; ZooKeeper zk = new ZooKeeper(connectString, sessionTimeout, new MyWatcher()); setDelegate(zk); ready = true; // Wait for connection to come up: if we fail to connect to ZK now, we do not want to continue // starting up the Indexer node. long waitUntil = System.currentTimeMillis() + startupTimeOut; int count = 0; while (zk.getState() != CONNECTED && waitUntil > System.currentTimeMillis()) { try { Thread.sleep(100); } catch (InterruptedException e) { break; } count++; if (count == 30) { // Output a message every 3s log.info("Waiting for ZooKeeper connection to be established"); count = 0; } } if (zk.getState() != CONNECTED) { stopping = true; try { zk.close(); } catch (Throwable t) { // ignore } throw new IOException("Failed to connect with Zookeeper within timeout " + startupTimeOut + ", connection string: " + connectString); } log.info("ZooKeeper session ID is 0x" + Long.toHexString(zk.getSessionId())); }
Example 6
Source File: ConfigurationGenerator.java From examples with Apache License 2.0 | 4 votes |
private ConfigurationGenerator() { try { ZooKeeper zk = new ZooKeeper(Constants.ZOOKEEPER_CONNECTION_STRING, 5000, this); // Wait for the zookeeper servers to get ready... which can several seconds when // using docker while(zk.getState() != ZooKeeper.States.CONNECTED) { synchronized (mutex) { mutex.wait(5000); } } // With docker we will always need to create the root, but with reconfiguration // you might first need to lookup the currently saved configuration in zookeeper Stat s = zk.exists(Constants.CONFIGURATION_PATH, false); int version = configVersion.get(); if (s == null) { zk.create(Constants.CONFIGURATION_PATH, Integer.toString(version).getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } else { byte[] rawData = zk.getData(Constants.CONFIGURATION_PATH, false, s); configVersion.set(new Integer(new String(rawData)).intValue()); } // Every 5-10 seconds, create a new configuration Random rand = new Random(); while (true) { synchronized (mutex) { mutex.wait(5000 + rand.nextInt(5000)); } version = configVersion.incrementAndGet(); System.out.println("ConfigurationGenerator: Updating version to " + version); // the order of these two calls is important zk.create(Constants.CONFIGURATION_PATH + "/" + version, ConfigurationGenerator.createConfiguration(version).encode().getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); zk.setData(Constants.CONFIGURATION_PATH, Integer.toString(version).getBytes(), version - 1); // Delete configuration versions while keeping one prior version. if(version > 2) { zk.delete(Constants.CONFIGURATION_PATH + "/" + (version - 2), -1); } } } catch (IOException | InterruptedException | KeeperException e) { e.printStackTrace(); } }
Example 7
Source File: HBaseTestingUtility.java From hbase with Apache License 2.0 | 4 votes |
/** * Expire a ZooKeeper session as recommended in ZooKeeper documentation * http://hbase.apache.org/book.html#trouble.zookeeper * There are issues when doing this: * [1] http://www.mail-archive.com/[email protected]/msg01942.html * [2] https://issues.apache.org/jira/browse/ZOOKEEPER-1105 * * @param nodeZK - the ZK watcher to expire * @param checkStatus - true to check if we can create a Table with the * current configuration. */ public void expireSession(ZKWatcher nodeZK, boolean checkStatus) throws Exception { Configuration c = new Configuration(this.conf); String quorumServers = ZKConfig.getZKQuorumServersString(c); ZooKeeper zk = nodeZK.getRecoverableZooKeeper().getZooKeeper(); byte[] password = zk.getSessionPasswd(); long sessionID = zk.getSessionId(); // Expiry seems to be asynchronous (see comment from P. Hunt in [1]), // so we create a first watcher to be sure that the // event was sent. We expect that if our watcher receives the event // other watchers on the same machine will get is as well. // When we ask to close the connection, ZK does not close it before // we receive all the events, so don't have to capture the event, just // closing the connection should be enough. ZooKeeper monitor = new ZooKeeper(quorumServers, 1000, new org.apache.zookeeper.Watcher(){ @Override public void process(WatchedEvent watchedEvent) { LOG.info("Monitor ZKW received event="+watchedEvent); } } , sessionID, password); // Making it expire ZooKeeper newZK = new ZooKeeper(quorumServers, 1000, EmptyWatcher.instance, sessionID, password); //ensure that we have connection to the server before closing down, otherwise //the close session event will be eaten out before we start CONNECTING state long start = System.currentTimeMillis(); while (newZK.getState() != States.CONNECTED && System.currentTimeMillis() - start < 1000) { Thread.sleep(1); } newZK.close(); LOG.info("ZK Closed Session 0x" + Long.toHexString(sessionID)); // Now closing & waiting to be sure that the clients get it. monitor.close(); if (checkStatus) { getConnection().getTable(TableName.META_TABLE_NAME).close(); } }
Example 8
Source File: ZkTestHelper.java From helix with Apache License 2.0 | 4 votes |
/** * Expire current zk session and wait for {@link IZkStateListener#handleNewSession(String)} invoked * @param client * @throws Exception */ public static void disconnectSession(HelixZkClient client) throws Exception { final ZkClient zkClient = (ZkClient) client; IZkStateListener listener = new IZkStateListener() { @Override public void handleStateChanged(KeeperState state) throws Exception { // System.err.println("disconnectSession handleStateChanged. state: " + state); } @Override public void handleNewSession(final String sessionId) throws Exception { // make sure zkclient is connected again zkClient.waitUntilConnected(HelixZkClient.DEFAULT_CONNECTION_TIMEOUT, TimeUnit.SECONDS); LOG.info("handleNewSession. sessionId: {}.", sessionId); } @Override public void handleSessionEstablishmentError(Throwable var1) throws Exception { } }; zkClient.subscribeStateChanges(listener); 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(); zkClient.unsubscribeStateChanges(listener); // System.err.println("zk: " + oldZookeeper); LOG.info("After expiry. sessionId: " + Long.toHexString(curZookeeper.getSessionId())); }
Example 9
Source File: ZkTestHelper.java From helix with Apache License 2.0 | 4 votes |
public static void expireSession(RealmAwareZkClient client) throws Exception { final CountDownLatch waitNewSession = new CountDownLatch(1); final ZkClient zkClient = (ZkClient) client; IZkStateListener listener = new IZkStateListener() { @Override public void handleStateChanged(KeeperState state) throws Exception { LOG.info("IZkStateListener#handleStateChanged, state: " + state); } @Override public void handleNewSession(final String sessionId) throws Exception { // make sure zkclient is connected again zkClient.waitUntilConnected(HelixZkClient.DEFAULT_CONNECTION_TIMEOUT, TimeUnit.SECONDS); LOG.info("handleNewSession. sessionId: {}.", sessionId); waitNewSession.countDown(); } @Override public void handleSessionEstablishmentError(Throwable var1) throws Exception { } }; zkClient.subscribeStateChanges(listener); ZkConnection connection = ((ZkConnection) zkClient.getConnection()); ZooKeeper curZookeeper = connection.getZookeeper(); String oldSessionId = Long.toHexString(curZookeeper.getSessionId()); LOG.info("Before session expiry. sessionId: " + oldSessionId + ", zk: " + curZookeeper); Watcher watcher = new Watcher() { @Override public void process(WatchedEvent event) { LOG.info("Watcher#process, event: " + 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); } Assert.assertEquals(dupZookeeper.getState(), States.CONNECTED, "Fail to connect to zk using current session info"); dupZookeeper.close(); // make sure session expiry really happens waitNewSession.await(); zkClient.unsubscribeStateChanges(listener); connection = (ZkConnection) zkClient.getConnection(); curZookeeper = connection.getZookeeper(); String newSessionId = Long.toHexString(curZookeeper.getSessionId()); LOG.info("After session expiry. sessionId: " + newSessionId + ", zk: " + curZookeeper); Assert.assertFalse(newSessionId.equals(oldSessionId), "Fail to expire current session, zk: " + curZookeeper); }
Example 10
Source File: ZkTestHelper.java From helix with Apache License 2.0 | 4 votes |
/** * Expire current zk session and wait for {@link IZkStateListener#handleNewSession(String)} invoked * @param client * @throws Exception */ public static void disconnectSession(HelixZkClient client) throws Exception { final ZkClient zkClient = (ZkClient) client; IZkStateListener listener = new IZkStateListener() { @Override public void handleStateChanged(KeeperState state) throws Exception { // System.err.println("disconnectSession handleStateChanged. state: " + state); } @Override public void handleNewSession(final String sessionId) throws Exception { // make sure zkclient is connected again zkClient.waitUntilConnected(HelixZkClient.DEFAULT_CONNECTION_TIMEOUT, TimeUnit.SECONDS); LOG.info("handleNewSession. sessionId: {}.", sessionId); } @Override public void handleSessionEstablishmentError(Throwable var1) throws Exception { } }; zkClient.subscribeStateChanges(listener); 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(); zkClient.unsubscribeStateChanges(listener); // System.err.println("zk: " + oldZookeeper); LOG.info("After expiry. sessionId: " + Long.toHexString(curZookeeper.getSessionId())); }
Example 11
Source File: ZkTestHelper.java From helix with Apache License 2.0 | 4 votes |
public static void expireSession(RealmAwareZkClient client) throws Exception { final CountDownLatch waitNewSession = new CountDownLatch(1); final ZkClient zkClient = (ZkClient) client; IZkStateListener listener = new IZkStateListener() { @Override public void handleStateChanged(KeeperState state) throws Exception { LOG.info("IZkStateListener#handleStateChanged, state: " + state); } @Override public void handleNewSession(final String sessionId) throws Exception { // make sure zkclient is connected again zkClient.waitUntilConnected(HelixZkClient.DEFAULT_CONNECTION_TIMEOUT, TimeUnit.SECONDS); LOG.info("handleNewSession. sessionId: {}.", sessionId); waitNewSession.countDown(); } @Override public void handleSessionEstablishmentError(Throwable var1) throws Exception { } }; zkClient.subscribeStateChanges(listener); ZkConnection connection = ((ZkConnection) zkClient.getConnection()); ZooKeeper curZookeeper = connection.getZookeeper(); String oldSessionId = Long.toHexString(curZookeeper.getSessionId()); LOG.info("Before session expiry. sessionId: " + oldSessionId + ", zk: " + curZookeeper); Watcher watcher = new Watcher() { @Override public void process(WatchedEvent event) { LOG.info("Watcher#process, event: " + 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); } Assert.assertEquals(dupZookeeper.getState(), States.CONNECTED, "Fail to connect to zk using current session info"); dupZookeeper.close(); // make sure session expiry really happens waitNewSession.await(); zkClient.unsubscribeStateChanges(listener); connection = (ZkConnection) zkClient.getConnection(); curZookeeper = connection.getZookeeper(); String newSessionId = Long.toHexString(curZookeeper.getSessionId()); LOG.info("After session expiry. sessionId: " + newSessionId + ", zk: " + curZookeeper); Assert.assertFalse(newSessionId.equals(oldSessionId), "Fail to expire current session, zk: " + curZookeeper); }
Example 12
Source File: IndexerIT.java From hbase-indexer with Apache License 2.0 | 4 votes |
private void cleanZooKeeper(String zkConnectString, String rootToDelete) throws Exception { int sessionTimeout = 10000; ZooKeeper zk = new ZooKeeper(zkConnectString, sessionTimeout, new Watcher() { @Override public void process(WatchedEvent event) { if (event.getState() == Watcher.Event.KeeperState.Disconnected) { System.err.println("ZooKeeper Disconnected."); } else if (event.getState() == Event.KeeperState.Expired) { System.err.println("ZooKeeper session expired."); } } }); long waitUntil = System.currentTimeMillis() + sessionTimeout; while (zk.getState() != CONNECTED && waitUntil > System.currentTimeMillis()) { try { Thread.sleep(20); } catch (InterruptedException e) { break; } } if (zk.getState() != CONNECTED) { throw new RuntimeException("Failed to connect to ZK within " + sessionTimeout + "ms."); } if (zk.exists(rootToDelete, false) != null) { List<String> paths = new ArrayList<String>(); collectChildren(rootToDelete, zk, paths); paths.add(rootToDelete); for (String path : paths) { zk.delete(path, -1, null, null); } // The above deletes are async, wait for them to be finished long startWait = System.currentTimeMillis(); while (zk.exists(rootToDelete, null) != null) { Thread.sleep(5); if (System.currentTimeMillis() - startWait > 120000) { throw new RuntimeException("State was not cleared in ZK within the expected timeout"); } } } zk.close(); }