@Test(expected = RuntimeException.class, timeout = MINIMUM_TIMEOUT * 4) public void testUnknownHost() throws Exception { ZooKeeper session = ZooSession.connect(UNKNOWN_HOST, MINIMUM_TIMEOUT, null, null, null); session.close(); }
public static ZooKeeper connect(String host, int timeout, String scheme, byte[] auth, Watcher watcher) { final int TIME_BETWEEN_CONNECT_CHECKS_MS = 100; int connectTimeWait = Math.min(10 * 1000, timeout); boolean tryAgain = true; long sleepTime = 100; ZooKeeper zooKeeper = null; long startTime = System.nanoTime(); while (tryAgain) { try { zooKeeper = new ZooKeeper(host, timeout, watcher); for (int i = 0; i < connectTimeWait / TIME_BETWEEN_CONNECT_CHECKS_MS && tryAgain; i++) { if (zooKeeper.getState().equals(States.CONNECTED)) { if (auth != null) zooKeeper.addAuthInfo(scheme, auth); tryAgain = false; } else UtilWaitThread.sleep(TIME_BETWEEN_CONNECT_CHECKS_MS); } } catch (IOException e) { if (e instanceof UnknownHostException) { int ttl = AddressUtil.getAddressCacheNegativeTtl((UnknownHostException) e); sleepTime = Math.max(sleepTime, (ttl + 1) * 1000L); } log.warn("Connection to zooKeeper failed, will try again in " + String.format("%.2f secs", sleepTime / 1000.0), e); } finally { if (tryAgain && zooKeeper != null) try { zooKeeper.close(); zooKeeper = null; } catch (InterruptedException e) { log.warn("interrupted", e); } } long stopTime = System.nanoTime(); long duration = NANOSECONDS.toMillis(stopTime - startTime); if (duration > 2L * timeout) { throw new RuntimeException("Failed to connect to zookeeper (" + host + ") within 2x zookeeper timeout period " + timeout); } if (tryAgain) { if (2L * timeout < duration + sleepTime + connectTimeWait) sleepTime = 2L * timeout - duration - connectTimeWait; if (sleepTime < 0) { connectTimeWait -= sleepTime; sleepTime = 0; } UtilWaitThread.sleep(sleepTime); if (sleepTime < 10000) sleepTime = sleepTime + (long) (sleepTime * secureRandom.nextDouble()); } } return zooKeeper; }
ZooSession { public static ZooKeeper connect(String host, int timeout, String scheme, byte[] auth, Watcher watcher) { final int TIME_BETWEEN_CONNECT_CHECKS_MS = 100; int connectTimeWait = Math.min(10 * 1000, timeout); boolean tryAgain = true; long sleepTime = 100; ZooKeeper zooKeeper = null; long startTime = System.nanoTime(); while (tryAgain) { try { zooKeeper = new ZooKeeper(host, timeout, watcher); for (int i = 0; i < connectTimeWait / TIME_BETWEEN_CONNECT_CHECKS_MS && tryAgain; i++) { if (zooKeeper.getState().equals(States.CONNECTED)) { if (auth != null) zooKeeper.addAuthInfo(scheme, auth); tryAgain = false; } else UtilWaitThread.sleep(TIME_BETWEEN_CONNECT_CHECKS_MS); } } catch (IOException e) { if (e instanceof UnknownHostException) { int ttl = AddressUtil.getAddressCacheNegativeTtl((UnknownHostException) e); sleepTime = Math.max(sleepTime, (ttl + 1) * 1000L); } log.warn("Connection to zooKeeper failed, will try again in " + String.format("%.2f secs", sleepTime / 1000.0), e); } finally { if (tryAgain && zooKeeper != null) try { zooKeeper.close(); zooKeeper = null; } catch (InterruptedException e) { log.warn("interrupted", e); } } long stopTime = System.nanoTime(); long duration = NANOSECONDS.toMillis(stopTime - startTime); if (duration > 2L * timeout) { throw new RuntimeException("Failed to connect to zookeeper (" + host + ") within 2x zookeeper timeout period " + timeout); } if (tryAgain) { if (2L * timeout < duration + sleepTime + connectTimeWait) sleepTime = 2L * timeout - duration - connectTimeWait; if (sleepTime < 0) { connectTimeWait -= sleepTime; sleepTime = 0; } UtilWaitThread.sleep(sleepTime); if (sleepTime < 10000) sleepTime = sleepTime + (long) (sleepTime * secureRandom.nextDouble()); } } return zooKeeper; } }
ZooSession { public static ZooKeeper connect(String host, int timeout, String scheme, byte[] auth, Watcher watcher) { final int TIME_BETWEEN_CONNECT_CHECKS_MS = 100; int connectTimeWait = Math.min(10 * 1000, timeout); boolean tryAgain = true; long sleepTime = 100; ZooKeeper zooKeeper = null; long startTime = System.nanoTime(); while (tryAgain) { try { zooKeeper = new ZooKeeper(host, timeout, watcher); for (int i = 0; i < connectTimeWait / TIME_BETWEEN_CONNECT_CHECKS_MS && tryAgain; i++) { if (zooKeeper.getState().equals(States.CONNECTED)) { if (auth != null) zooKeeper.addAuthInfo(scheme, auth); tryAgain = false; } else UtilWaitThread.sleep(TIME_BETWEEN_CONNECT_CHECKS_MS); } } catch (IOException e) { if (e instanceof UnknownHostException) { int ttl = AddressUtil.getAddressCacheNegativeTtl((UnknownHostException) e); sleepTime = Math.max(sleepTime, (ttl + 1) * 1000L); } log.warn("Connection to zooKeeper failed, will try again in " + String.format("%.2f secs", sleepTime / 1000.0), e); } finally { if (tryAgain && zooKeeper != null) try { zooKeeper.close(); zooKeeper = null; } catch (InterruptedException e) { log.warn("interrupted", e); } } long stopTime = System.nanoTime(); long duration = NANOSECONDS.toMillis(stopTime - startTime); if (duration > 2L * timeout) { throw new RuntimeException("Failed to connect to zookeeper (" + host + ") within 2x zookeeper timeout period " + timeout); } if (tryAgain) { if (2L * timeout < duration + sleepTime + connectTimeWait) sleepTime = 2L * timeout - duration - connectTimeWait; if (sleepTime < 0) { connectTimeWait -= sleepTime; sleepTime = 0; } UtilWaitThread.sleep(sleepTime); if (sleepTime < 10000) sleepTime = sleepTime + (long) (sleepTime * secureRandom.nextDouble()); } } return zooKeeper; }  }
ZooSession { public static ZooKeeper connect(String host, int timeout, String scheme, byte[] auth, Watcher watcher) { final int TIME_BETWEEN_CONNECT_CHECKS_MS = 100; int connectTimeWait = Math.min(10 * 1000, timeout); boolean tryAgain = true; long sleepTime = 100; ZooKeeper zooKeeper = null; long startTime = System.nanoTime(); while (tryAgain) { try { zooKeeper = new ZooKeeper(host, timeout, watcher); for (int i = 0; i < connectTimeWait / TIME_BETWEEN_CONNECT_CHECKS_MS && tryAgain; i++) { if (zooKeeper.getState().equals(States.CONNECTED)) { if (auth != null) zooKeeper.addAuthInfo(scheme, auth); tryAgain = false; } else UtilWaitThread.sleep(TIME_BETWEEN_CONNECT_CHECKS_MS); } } catch (IOException e) { if (e instanceof UnknownHostException) { int ttl = AddressUtil.getAddressCacheNegativeTtl((UnknownHostException) e); sleepTime = Math.max(sleepTime, (ttl + 1) * 1000L); } log.warn("Connection to zooKeeper failed, will try again in " + String.format("%.2f secs", sleepTime / 1000.0), e); } finally { if (tryAgain && zooKeeper != null) try { zooKeeper.close(); zooKeeper = null; } catch (InterruptedException e) { log.warn("interrupted", e); } } long stopTime = System.nanoTime(); long duration = NANOSECONDS.toMillis(stopTime - startTime); if (duration > 2L * timeout) { throw new RuntimeException("Failed to connect to zookeeper (" + host + ") within 2x zookeeper timeout period " + timeout); } if (tryAgain) { if (2L * timeout < duration + sleepTime + connectTimeWait) sleepTime = 2L * timeout - duration - connectTimeWait; if (sleepTime < 0) { connectTimeWait -= sleepTime; sleepTime = 0; } UtilWaitThread.sleep(sleepTime); if (sleepTime < 10000) sleepTime = sleepTime + (long) (sleepTime * secureRandom.nextDouble()); } } return zooKeeper; }  static ZooKeeper connect(String host, int timeout, String scheme, byte[] auth,
      Watcher watcher); static ZooKeeper getAuthenticatedSession(String zooKeepers, int timeout, String scheme,
      byte[] auth); static ZooKeeper getAnonymousSession(String zooKeepers, int timeout); }
ZooSession { public static ZooKeeper connect(String host, int timeout, String scheme, byte[] auth, Watcher watcher) { final int TIME_BETWEEN_CONNECT_CHECKS_MS = 100; int connectTimeWait = Math.min(10 * 1000, timeout); boolean tryAgain = true; long sleepTime = 100; ZooKeeper zooKeeper = null; long startTime = System.nanoTime(); while (tryAgain) { try { zooKeeper = new ZooKeeper(host, timeout, watcher); for (int i = 0; i < connectTimeWait / TIME_BETWEEN_CONNECT_CHECKS_MS && tryAgain; i++) { if (zooKeeper.getState().equals(States.CONNECTED)) { if (auth != null) zooKeeper.addAuthInfo(scheme, auth); tryAgain = false; } else UtilWaitThread.sleep(TIME_BETWEEN_CONNECT_CHECKS_MS); } } catch (IOException e) { if (e instanceof UnknownHostException) { int ttl = AddressUtil.getAddressCacheNegativeTtl((UnknownHostException) e); sleepTime = Math.max(sleepTime, (ttl + 1) * 1000L); } log.warn("Connection to zooKeeper failed, will try again in " + String.format("%.2f secs", sleepTime / 1000.0), e); } finally { if (tryAgain && zooKeeper != null) try { zooKeeper.close(); zooKeeper = null; } catch (InterruptedException e) { log.warn("interrupted", e); } } long stopTime = System.nanoTime(); long duration = NANOSECONDS.toMillis(stopTime - startTime); if (duration > 2L * timeout) { throw new RuntimeException("Failed to connect to zookeeper (" + host + ") within 2x zookeeper timeout period " + timeout); } if (tryAgain) { if (2L * timeout < duration + sleepTime + connectTimeWait) sleepTime = 2L * timeout - duration - connectTimeWait; if (sleepTime < 0) { connectTimeWait -= sleepTime; sleepTime = 0; } UtilWaitThread.sleep(sleepTime); if (sleepTime < 10000) sleepTime = sleepTime + (long) (sleepTime * secureRandom.nextDouble()); } } return zooKeeper; }  static ZooKeeper connect(String host, int timeout, String scheme, byte[] auth,
      Watcher watcher); static ZooKeeper getAuthenticatedSession(String zooKeepers, int timeout, String scheme,
      byte[] auth); static ZooKeeper getAnonymousSession(String zooKeepers, int timeout);  }