List of usage examples for org.apache.zookeeper WatchedEvent getState
public KeeperState getState()
From source file:org.jc.zk.dpw.DataMonitor.java
@Override public void process(WatchedEvent event) { String path = event.getPath(); switch (event.getType()) { case NodeDataChanged: if (path.equals(this.znodeTime)) { //Time has been updated this.listener.timeZnodeChanged(); } else if (path.equals(this.znodeMaster)) { this.listener.masterZnodeChanged(); } else if (path.equals(this.processHeartBeatZnode)) { this.listener.processHeartBeatZnodeUpdate(); } else if (this.znodeToCreateForUpdates == null && this.cmwUpdateZnodes.contains(event.getPath())) { this.listener.cmwUpdatedUpdateZnode(event.getPath()); } else if (this.znodeToCreateForUpdates != null && this.znodeToCreateForUpdates.equals(event.getPath())) { this.bindToZnodes(this.isActiveMaster); } else if (path.equals(this.amwRequestKillZnode)) { this.listener.amwRequestKillZnodeChanged(); }/*from w w w . java2 s .com*/ break; case None: if (event.getState() == Event.KeeperState.Expired || event.getState() == Event.KeeperState.Disconnected) { this.listener.disconnected(KeeperException.Code.CONNECTIONLOSS.intValue()); } else if (event.getState() == Event.KeeperState.SyncConnected) { this.listener.connected(); } break; case NodeDeleted: if (event.getPath().equals(this.znodeTime)) { this.listener.timeZnodeRemoved(); } else if (event.getPath().equals(this.znodeProcessObserved)) { this.listener.processObservedZnodeRemoved(); } else if (this.cmwUpdateZnodes.contains(event.getPath())) { this.listener.cmwUpdateZnodeRemoved(); } break; case NodeCreated: if (this.ownCMWFailoverZnode != null && this.ownCMWFailoverZnode.equals(event.getPath())) { this.listener.cmwFailoverZnodeCreated(event.getPath()); } else if (this.znodeToCreateForUpdates != null && this.znodeToCreateForUpdates.equals(event.getPath())) { /** * To comply with the old model, where CMWs created update * znode, instead of waiting for AMW to create them, once CMW * detects that update znode was created by AMW, it will * automatically read the data from znode, that is, instead * of invoking callback to notify about the creation of znode * by master, it will proceed to read the content and later * notify CMW about the new data. */ Map<String, String> ctx = new HashMap<>(); ctx.put(ZNODE_TYPE, CMW_UPDATE_NODE); ctx.put(UPDATE_NODE_EVENT, UPDATE_EVENT_IS_CREATE); this.zk.getData(path, this, this, ctx); } else if (this.znodeToCreateForUpdates == null && this.cmwUpdateZnodes.contains(event.getPath())) { this.bindToZnodes(this.isActiveMaster); } else if (event.getPath().equals(this.znodeMaster)) { this.listener.masterZnodeCreated(); } else if (event.getPath().equals(this.znodeTime)) { this.listener.timeZnodeCreated(); } else if (event.getPath().equals(this.znodeProcessObserved)) { this.listener.processObservedZnodeCreated(); } break; } }
From source file:org.jc.zk.dpw.DataMonitorProcesses.java
@Override public void process(WatchedEvent event) { switch (event.getType()) { case NodeCreated: if (event.getPath().equals(this.znodeObserved)) { }// www . j av a2 s . c om break; case NodeDataChanged: if (event.getPath().equals(this.znodeTime)) { this.readTimeZnode(); } break; case None: if (event.getState() == Event.KeeperState.Disconnected || event.getState() == Event.KeeperState.Expired) { this.listener.closing(KeeperException.Code.CONNECTIONLOSS.intValue()); } break; case NodeDeleted: if (event.getPath().equals(this.znodeObserved) || event.getPath().equals(this.znodeTime)) { this.listener.closing(KeeperException.Code.CONNECTIONLOSS.intValue()); } break; } }
From source file:org.jc.zk.dpw.TimeDataMonitor.java
@Override public void process(WatchedEvent event) { System.out.print(event.toString()); switch (event.getType()) { case NodeCreated: if (event.getPath().equals(this.znodeForTimeListeners)) { this.listener.timeListenersZnodeCreated(); } else if (event.getPath().equals(this.timeZnodeRemovedFlagZnode)) { this.listener.notificationZnodeCreated(); }//from w w w. ja v a 2 s . c o m break; case NodeDeleted: if (event.getPath().equals(this.timeZnode)) { this.listener.timeZnodeDeleted(); } else if (event.getPath().equals(this.znodeForTimeListeners)) { this.listener.timeListenersZnodeDeleted(); } else if (event.getPath().equals(this.timeZnodeRemovedFlagZnode)) { this.listener.notificationZnodeRemoved(); } break; case NodeDataChanged: if (event.getPath().equals(this.timeZnode)) { this.listener.timeZnodeChanged(); //this.zk.getData(this.timeZnode, null, this, this.ctx); } else if (event.getPath().equals(this.znodeForTimeListeners)) { this.listener.timeListenersZnodeChanged(); } else if (event.getPath().equals(this.requestAMWKillZnode)) { this.listener.requestAMWKillZnodeChanged(); } break; case None: if (event.getState() == Event.KeeperState.SyncConnected) { this.listener.connected(); } else if (event.getState() == Event.KeeperState.Disconnected || event.getState() == Event.KeeperState.Expired) { this.listener.disconnected(KeeperException.Code.CONNECTIONLOSS.intValue()); } break; } }
From source file:org.jc.zk.process.ProcessWrapperMonitor.java
@Override public void process(WatchedEvent event) { switch (event.getType()) { case None://ww w . ja v a 2s .co m if (event.getState() == Event.KeeperState.Disconnected || event.getState() == Event.KeeperState.Expired) { this.listener.disconnected(); } else if (event.getState() == Event.KeeperState.SyncConnected) { this.listener.connected(); } break; case NodeCreated: if (event.getPath().equals(this.heartBeatZnode)) { this.listener.pHeartBeatZnodeCreated(); } break; case NodeDeleted: if (event.getPath().equals(this.heartBeatZnode)) { this.listener.pHeartBeatZnodeRemoved(); } break; } }
From source file:org.lable.oss.dynamicconfig.provider.zookeeper.NodeWatcher.java
License:Apache License
@Override public void process(WatchedEvent watchedEvent) { if (state != State.LIVE) return;// w w w. ja v a2 s . c o m Event.KeeperState state = watchedEvent.getState(); Event.EventType type = watchedEvent.getType(); // This switch handles all relevant states, and tries to reset the watch on the znode after it is // triggered. switch (state) { case SyncConnected: case ConnectedReadOnly: resetRetryCounters(); switch (type) { case NodeCreated: case NodeDataChanged: // Configuration znode changed, let the callback know. zk.getData(path, this, callback, null); break; case None: registerWatcher(path); break; case NodeDeleted: logger.error("Our configuration znode was deleted. Waiting for it to be recreated"); registerWatcher(path); break; } break; case Disconnected: logger.warn("Disconnected from Zookeeper quorum, reconnecting"); // The Zookeeper instance will automatically attempt reconnection. waitBeforeRetrying(); break; case Expired: logger.warn("Connection to Zookeeper quorum expired. Attempting to reconnect"); // The Zookeeper instance is no longer valid. We have to reconnect ourselves. connect(); break; case SaslAuthenticated: case AuthFailed: // Probably not relevant to us. break; } }
From source file:org.lable.oss.uniqueid.zookeeper.connection.ZooKeeperConnection.java
License:Apache License
/** * Connect to the ZooKeeper quorum, or timeout if it is unreachable. * * @throws IOException Thrown when connecting to the ZooKeeper quorum fails. *//*from ww w. j a v a 2 s . c o m*/ private static void connect() throws IOException { final CountDownLatch latch = new CountDownLatch(1); ZooKeeper zookeeper; // Connect to the quorum and wait for the successful connection callback.; zookeeper = new ZooKeeper(quorumAddresses, (int) SECONDS.toMillis(10), new Watcher() { @Override public void process(WatchedEvent watchedEvent) { if (watchedEvent.getState() == Event.KeeperState.SyncConnected) { // Signal that the Zookeeper connection is established. latch.countDown(); } } }); boolean successfullyConnected = false; try { successfullyConnected = latch.await(11, SECONDS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } if (!successfullyConnected) { throw new IOException(String.format("Connection to ZooKeeper quorum timed out after %d seconds.", CONNECTION_TIMEOUT)); } zookeeper.register(new ConnectionWatcher()); INSTANCE.zookeeper = zookeeper; }
From source file:org.lable.oss.uniqueid.zookeeper.ZooKeeperInstance.java
License:Apache License
@Override protected void before() throws Throwable { tempFolder = createTempDir();/*from w ww. j a v a 2 s. c o m*/ String zookeeperHost = "localhost:" + serverPort; ServerConfig config = new ServerConfig(); config.parse(new String[] { serverPort, tempFolder.getAbsolutePath() }); zkThread = new ZooKeeperThread(config); new Thread(zkThread).start(); final CountDownLatch latch = new CountDownLatch(1); // Connect to the quorum and wait for the successful connection callback. zookeeper = new ZooKeeper(zookeeperHost, (int) TimeUnit.SECONDS.toMillis(10), new Watcher() { @Override public void process(WatchedEvent watchedEvent) { if (watchedEvent.getState() == Event.KeeperState.SyncConnected) { // Signal that the Zookeeper connection is established. latch.countDown(); } } }); // Wait for the connection to be established. boolean successfullyConnected = latch.await(12, TimeUnit.SECONDS); if (!successfullyConnected) { tempFolder.delete(); throw new Exception("Could not start a local ZooKeeper quorum for testing."); } }
From source file:org.lilyproject.hadooptestfw.CleanupUtil.java
License:Apache License
public void cleanZooKeeper() throws Exception { int sessionTimeout = 10000; ZooKeeper zk = new ZooKeeper(zkConnectString, sessionTimeout, new Watcher() { @Override/*from w w w .j a va 2s . com*/ 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("/lily", false) != null) { System.out.println("----------------- Clearing '/lily' node in ZooKeeper -------------------"); List<String> paths = new ArrayList<String>(); collectChildren("/lily", zk, paths); paths.add("/lily"); for (String path : paths) { zk.delete(path, -1, null, null); } long startWait = System.currentTimeMillis(); while (zk.exists("/lily", null) != null) { Thread.sleep(5); if (System.currentTimeMillis() - startWait > 120000) { throw new RuntimeException("State was not cleared in ZK within the expected timeout"); } } System.out.println("Deleted " + paths.size() + " paths from ZooKeeper"); System.out.println("------------------------------------------------------------------------"); } zk.close(); }
From source file:org.lilyproject.testfw.HBaseProxy.java
License:Apache License
public void cleanZooKeeper() throws Exception { int sessionTimeout = 10000; ZooKeeper zk = new ZooKeeper(getZkConnectString(), sessionTimeout, new Watcher() { 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."); }// w ww .j a v a 2 s.c o m } }); long waitUntil = System.currentTimeMillis() + sessionTimeout; while (zk.getState() != CONNECTED && waitUntil > System.currentTimeMillis()) { try { Thread.sleep(100); } catch (InterruptedException e) { break; } } if (zk.getState() != CONNECTED) { throw new RuntimeException("Failed to connect to ZK within " + sessionTimeout + "ms."); } if (zk.exists("/lily", false) != null) { System.out.println("----------------- Clearing '/lily' node in ZooKeeper -------------------"); deleteChildren("/lily", zk); zk.delete("/lily", -1); System.out.println("------------------------------------------------------------------------"); } zk.close(); }
From source file:org.linkedin.zookeeper.client.ZKClient.java
License:Apache License
@Override public void process(WatchedEvent event) { synchronized (_lock) { if (event.getState() != null) { if (log.isDebugEnabled()) log.debug("event: " + event.getState()); switch (event.getState()) { case SyncConnected: changeState(State.CONNECTED); break; case Disconnected: if (_state != State.NONE) { changeState(State.RECONNECTING); }/*from w w w .j a v a 2s .c o m*/ break; case Expired: // when expired, the zookeeper object is invalid and we need to recreate a new one _zk = null; changeState(State.NONE); try { log.warn("Expiration detected: trying to restart..."); start(); } catch (Throwable e) { log.warn("Error while restarting:", e); if (_expiredSessionRecovery == null) { _expiredSessionRecovery = new ExpiredSessionRecovery(); _expiredSessionRecovery.setDaemon(true); _expiredSessionRecovery.start(); } } break; default: log.warn("unprocessed event state: " + event.getState()); } } } }