@Override public <T> T retryOperation(ZooKeeperOperation<T> operation) throws InterruptedException, KeeperException { if (isCurrentThreadEventThread()) { throw new RuntimeException("retryOperation should not be called from within the ZooKeeper event thread."); } int tryCount = 0; while (true) { tryCount++; try { return operation.execute(); } catch (KeeperException.ConnectionLossException e) { // ok } if (tryCount > 3) { log.warn("ZooKeeper operation attempt " + tryCount + " failed due to connection loss."); } waitForConnection(); } }
private void endProcess(String message) { if (stopping) { return; } if (endProcessHook != null) { endProcessHook.run(); } super.shutdown(); log.error(message); System.err.println(message); System.exit(1); }
public static ZooKeeperItf connect(String connectString, int sessionTimeout) throws ZkConnectException { ZooKeeperImpl zooKeeper; try { zooKeeper = new ZooKeeperImpl(connectString, sessionTimeout); } catch (IOException e) { throw new ZkConnectException("Failed to connect with Zookeeper @ '" + connectString + "'", e); } long waitUntil = System.currentTimeMillis() + sessionTimeout; boolean connected = (States.CONNECTED).equals(zooKeeper.getState()); while (!connected && waitUntil > System.currentTimeMillis()) { try { Thread.sleep(100); } catch (InterruptedException e) { connected = (States.CONNECTED).equals(zooKeeper.getState()); break; } connected = (States.CONNECTED).equals(zooKeeper.getState()); } if (!connected) { System.out.println("Failed to connect to Zookeeper within timeout: Dumping stack: "); Thread.dumpStack(); zooKeeper.close(); throw new ZkConnectException("Failed to connect with Zookeeper @ '" + connectString + "' within timeout " + sessionTimeout); } return zooKeeper; }
@Override public void waitForConnection() throws InterruptedException { if (isCurrentThreadEventThread()) { throw new RuntimeException("waitForConnection should not be called from within the ZooKeeper event thread."); } synchronized (connectedMonitor) { while (!connected && !stop) { connectedMonitor.wait(); } } if (stop) { throw new InterruptedException("This ZooKeeper handle is shutting down."); } }
@Override public void process(WatchedEvent event) { zkEventThread = Thread.currentThread(); if (event.getState() == Watcher.Event.KeeperState.Disconnected) { System.err.println("ZooKeeper disconnected at " + new Date()); printConnectMsg = true; } else if (event.getState() == Event.KeeperState.Expired) { System.err.println("ZooKeeper session expired at " + new Date()); printConnectMsg = true; } else if (event.getState() == Event.KeeperState.SyncConnected) { if (printConnectMsg) { System.out.println("ZooKeeper connected at " + new Date()); } } setConnectedState(event); for (Watcher watcher : additionalDefaultWatchers) { watcher.process(event); } } }
public static ZooKeeperItf connect(String connectString, int sessionTimeout) throws ZkConnectException { ZooKeeperImpl zooKeeper; try { zooKeeper = new ZooKeeperImpl(connectString, sessionTimeout, new DefaultACLProvider()); } catch (IOException e) { throw new ZkConnectException("Failed to connect with Zookeeper @ '" + connectString + "'", e); } long waitUntil = System.currentTimeMillis() + sessionTimeout; boolean connected = (States.CONNECTED).equals(zooKeeper.getState()); while (!connected && waitUntil > System.currentTimeMillis()) { try { Thread.sleep(100); } catch (InterruptedException e) { connected = (States.CONNECTED).equals(zooKeeper.getState()); break; } connected = (States.CONNECTED).equals(zooKeeper.getState()); } if (!connected) { System.out.println("Failed to connect to Zookeeper within timeout: Dumping stack: "); Thread.dumpStack(); zooKeeper.close(); throw new ZkConnectException("Failed to connect with Zookeeper @ '" + connectString + "' within timeout " + sessionTimeout); } return zooKeeper; }
@Override public void waitForConnection() throws InterruptedException { if (isCurrentThreadEventThread()) { throw new RuntimeException("waitForConnection should not be called from within the ZooKeeper event thread."); } synchronized (connectedMonitor) { while (!connected && !stop) { connectedMonitor.wait(); } } if (stop) { throw new InterruptedException("This ZooKeeper handle is shutting down."); } }
@Override public void process(WatchedEvent event) { zkEventThread = Thread.currentThread(); if (event.getState() == Watcher.Event.KeeperState.Disconnected) { System.err.println("ZooKeeper disconnected at " + new Date()); printConnectMsg = true; } else if (event.getState() == Event.KeeperState.Expired) { System.err.println("ZooKeeper session expired at " + new Date()); printConnectMsg = true; } else if (event.getState() == Event.KeeperState.SyncConnected) { if (printConnectMsg) { System.out.println("ZooKeeper connected at " + new Date()); } } setConnectedState(event); for (Watcher watcher : additionalDefaultWatchers) { watcher.process(event); } } }
@Override public <T> T retryOperation(ZooKeeperOperation<T> operation) throws InterruptedException, KeeperException { if (isCurrentThreadEventThread()) { throw new RuntimeException("retryOperation should not be called from within the ZooKeeper event thread."); } int tryCount = 0; while (true) { tryCount++; try { return operation.execute(); } catch (KeeperException.ConnectionLossException e) { // ok } if (tryCount > 3) { log.warn("ZooKeeper operation attempt " + tryCount + " failed due to connection loss."); } waitForConnection(); } }
private void endProcess(String message) { if (stopping) { return; } if (endProcessHook != null) { endProcessHook.run(); } super.shutdown(); log.error(message); System.err.println(message); System.exit(1); }
@Override @PreDestroy public void shutdown() { super.shutdown(); stopping = true; if (stateWatcherThread != null) { stateWatcherThread.interrupt(); } close(); }
@Override @PreDestroy public void shutdown() { super.shutdown(); stopping = true; if (stateWatcherThread != null) { stateWatcherThread.interrupt(); } close(); }