List of usage examples for org.apache.zookeeper WatchedEvent getType
public EventType getType()
From source file:io.druid.server.coordinator.LoadQueuePeon.java
License:Apache License
private void doNext() { synchronized (lock) { if (currentlyProcessing == null) { if (!segmentsToDrop.isEmpty()) { currentlyProcessing = segmentsToDrop.firstEntry().getValue(); log.info("Server[%s] dropping [%s]", basePath, currentlyProcessing.getSegmentIdentifier()); } else if (!segmentsToLoad.isEmpty()) { currentlyProcessing = segmentsToLoad.firstEntry().getValue(); log.info("Server[%s] loading [%s]", basePath, currentlyProcessing.getSegmentIdentifier()); } else { return; }//from w w w . j av a2s . c o m zkWritingExecutor.execute(new Runnable() { @Override public void run() { synchronized (lock) { try { // expected when the coordinator looses leadership and LoadQueuePeon is stopped. if (currentlyProcessing == null) { if (!stopped) { log.makeAlert("Crazy race condition! server[%s]", basePath).emit(); } actionCompleted(); doNext(); return; } log.info("Server[%s] processing segment[%s]", basePath, currentlyProcessing.getSegmentIdentifier()); final String path = ZKPaths.makePath(basePath, currentlyProcessing.getSegmentIdentifier()); final byte[] payload = jsonMapper .writeValueAsBytes(currentlyProcessing.getChangeRequest()); curator.create().withMode(CreateMode.EPHEMERAL).forPath(path, payload); zkWritingExecutor.schedule(new Runnable() { @Override public void run() { try { if (curator.checkExists().forPath(path) != null) { failAssign(new ISE("%s was never removed! Failing this operation!", path)); } } catch (Exception e) { failAssign(e); } } }, config.getLoadTimeoutDelay().getMillis(), TimeUnit.MILLISECONDS); final Stat stat = curator.checkExists().usingWatcher(new CuratorWatcher() { @Override public void process(WatchedEvent watchedEvent) throws Exception { switch (watchedEvent.getType()) { case NodeDeleted: entryRemoved(watchedEvent.getPath()); } } }).forPath(path); if (stat == null) { final byte[] noopPayload = jsonMapper .writeValueAsBytes(new SegmentChangeRequestNoop()); // Create a node and then delete it to remove the registered watcher. This is a work-around for // a zookeeper race condition. Specifically, when you set a watcher, it fires on the next event // that happens for that node. If no events happen, the watcher stays registered foreverz. // Couple that with the fact that you cannot set a watcher when you create a node, but what we // want is to create a node and then watch for it to get deleted. The solution is that you *can* // set a watcher when you check to see if it exists so, we first create the node and then set a // watcher on its existence. However, if already does not exist by the time the existence check // returns, then the watcher that was set will never fire (nobody will ever create the node // again) and thus lead to a slow, but real, memory leak. So, we create another node to cause // that watcher to fire and delete it right away. // // We do not create the existence watcher first, because then it will fire when we create the // node and we'll have the same race when trying to refresh that watcher. curator.create().withMode(CreateMode.EPHEMERAL).forPath(path, noopPayload); entryRemoved(path); } } catch (Exception e) { failAssign(e); } } } }); } else { log.info("Server[%s] skipping doNext() because something is currently loading[%s].", basePath, currentlyProcessing.getSegmentIdentifier()); } } }
From source file:io.flood.registry.zk.ZookeeperRegistry.java
License:Apache License
private void subscribeRealService(final Resource resource, final NotifyListener listener) throws Exception { final String resourcePath = Paths.get(resource); client.getChildren().usingWatcher(new CuratorWatcher() { @Override//w w w . ja va2s. c om public void process(WatchedEvent event) throws Exception { switch (event.getType()) { case NodeDeleted: listener.notifyResourceDeleted(resource); break; case NodeDataChanged: Resource newResource = GsonFactory.fromJson(client.getData().forPath(resourcePath), Resource.class); localService.replace(resourcePath, newResource); listener.notifyResourceChaneged(newResource); } } }).forPath(resourcePath); }
From source file:io.flood.registry.zk.ZookeeperRegistry.java
License:Apache License
@Override public void subscribe(final String name, final String group, final NotifyListener listener) { try {//from ww w . j a va2s . c om String serviceDir = Paths.get(name, group); client.getChildren().usingWatcher(new Watcher() { @Override public void process(WatchedEvent watchedEvent) { switch (watchedEvent.getType()) { case NodeChildrenChanged: { try { final List<String> nodes = client.getChildren().forPath(serviceDir); List<Resource> services = Lists.newArrayListWithCapacity(nodes.size()); String servicePath = null; for (String node : nodes) { servicePath = Paths.get(name, group, node); byte[] buffer = client.getData().forPath(servicePath); Resource resource = GsonFactory.fromJson(buffer, Resource.class); if (!localService.containsKey(servicePath)) { LOG.info("new serice {},{}", servicePath, resource); listener.notifyResourceCreated(resource); subscribeRealService(resource, listener); localService.put(servicePath, resource); } } Maps.filterEntries(localService, entry -> { if (nodes.contains(entry.getKey())) { return true; } listener.notifyResourceDeleted(entry.getValue()); return false; }); } catch (Exception e) { LOG.warn("node child changed ,but process error ", e); } } } } }).forPath(Paths.get(name, group)); LOG.info("subscribe {}", Paths.get(name, group)); } catch (Exception e) { throw new IllegalStateException(e); } }
From source file:io.reign.AbstractZkEventHandler.java
License:Apache License
@Override public void process(WatchedEvent event) { /** log event **/ // log if TRACE if (logger.isTraceEnabled()) { logger.trace("***** Received ZooKeeper Event: {}", ReflectionToStringBuilder.toString(event, ToStringStyle.DEFAULT_STYLE)); }/*ww w .j av a 2 s .c o m*/ /** check if we are filtering this event **/ if (filterWatchedEvent(event)) { return; } /** process events **/ switch (event.getType()) { case NodeChildrenChanged: nodeChildrenChanged(event); break; case NodeCreated: nodeCreated(event); break; case NodeDataChanged: nodeDataChanged(event); break; case NodeDeleted: nodeDeleted(event); break; case None: Event.KeeperState eventState = event.getState(); if (eventState == Event.KeeperState.SyncConnected) { connected(event); } else if (eventState == Event.KeeperState.Disconnected) { disconnected(event); } else if (eventState == Event.KeeperState.Expired) { sessionExpired(event); } else { logger.warn("Unhandled event state: eventType={}; eventState={}", event.getType(), event.getState()); } break; default: logger.warn("Unhandled event type: eventType={}; eventState={}", event.getType(), event.getState()); } }
From source file:io.reign.coord.ZkLockWatcher.java
License:Apache License
@Override public void process(WatchedEvent event) { // log if DEBUG if (logger.isDebugEnabled()) { logger.debug("***** Received ZooKeeper Event: {}", ReflectionToStringBuilder.toString(event, ToStringStyle.DEFAULT_STYLE)); }// w ww . jav a 2s . c o m // process events switch (event.getType()) { case NodeCreated: case NodeChildrenChanged: case NodeDataChanged: case NodeDeleted: synchronized (this) { this.notifyAll(); if (logger.isDebugEnabled()) { logger.debug("Notifying threads waiting on LockWatcher: lockWatcher.hashcode()=" + this.hashCode() + "; instancesOutstanding=" + instancesOutstanding.get() + "; lockName=" + lockPath + "; lockReservation=" + lockReservationPath); } } break; case None: Event.KeeperState eventState = event.getState(); if (eventState == Event.KeeperState.SyncConnected) { // connection event: check children synchronized (this) { this.notifyAll(); } } else if (eventState == Event.KeeperState.Disconnected || eventState == Event.KeeperState.Expired) { // disconnected: notifyAll so we can check children again on // reconnection synchronized (this) { this.notifyAll(); } } else { logger.warn("Unhandled event state: " + ReflectionToStringBuilder.toString(event, ToStringStyle.DEFAULT_STYLE)); } break; default: logger.warn("Unhandled event type: " + ReflectionToStringBuilder.toString(event, ToStringStyle.DEFAULT_STYLE)); } // }// if }
From source file:io.reign.ObserverManager.java
License:Apache License
void scheduleCheck(final WatchedEvent event) { final String path = event.getPath(); // do not schedule a check if we have scheduled a check recently synchronized (observerScheduledCheckTimestampMap) { Long scheduledCheckTimestamp = observerScheduledCheckTimestampMap.get(path); if (scheduledCheckTimestamp != null) { long timeToCheck = scheduledCheckTimestamp - System.currentTimeMillis(); if (timeToCheck > this.sweeperIntervalMillis / 2) { logger.trace(// ww w. ja v a2 s .c o m "Ignoring: re-check already scheduled: path={}; eventType={}; timeToCheckMillis={}", event.getPath(), event.getType(), timeToCheck); return; } } // update scheduled timestamp observerScheduledCheckTimestampMap.put(path, System.currentTimeMillis() + sweeperIntervalMillis); } logger.debug("Scheduling re-check after watch triggered: path={}; eventType={}; timeToCheckMillis={}", event.getPath(), event.getType(), sweeperIntervalMillis); this.scheduledExecutorService.schedule(new Runnable() { @Override public void run() { try { // sync data to get most recent zkClientUtil.syncPath(zkClient, path, this); // fetch info about node Stat zkStat = zkClient.exists(path, true); Stat updatedStat = new Stat(); byte[] zkData = zkClient.getData(path, true, updatedStat); List<String> zkChildList = zkClient.getChildren(path, true); Set<T> observerSet = getObserverSet(path, false); for (T observer : observerSet) { List<String> observerChildList = observer.getChildList(); byte[] observerData = observer.getData(); if (zkStat != null) { // check children if (childListsDiffer(observerChildList, zkChildList)) { observer.setChildList(zkChildList); updateObserver(path, observer); logger.warn("RECHECK: NODE CHILDREN CHANGED: updated={}; previous={}", zkChildList, observerChildList); observer.nodeChildrenChanged(zkChildList, observerChildList); } // check data if (!Arrays.equals(observerData, zkData)) { observer.setData(zkData); updateObserver(path, observer); logger.warn("RECHECK: NODE DATA CHANGED: updated={}; previous={}", zkData, observerData); observer.nodeDataChanged(zkData, observerData); observer.nodeDataChanged(zkData, observerData, updatedStat); } } else { // node deleted observer.setData(null); observer.setChildList(Collections.EMPTY_LIST); observer.nodeDeleted(observerData, observerChildList); } } } catch (Exception e) { logger.warn("Unable to check event: path=" + path, e); } } }, this.sweeperIntervalMillis, TimeUnit.MILLISECONDS); }
From source file:io.reign.Reign.java
License:Apache License
@Override public void process(WatchedEvent event) { // log if TRACE if (logger.isTraceEnabled()) { logger.trace("***** Received ZooKeeper Event: {}", ReflectionToStringBuilder.toString(event, ToStringStyle.DEFAULT_STYLE)); }/*from w ww. jav a2 s . c om*/ if (shutdown) { logger.warn("Already shutdown: ignoring event: type={}; path={}", event.getType(), event.getPath()); return; } for (Watcher watcher : watcherList) { watcher.process(event); } }
From source file:io.reign.zk.ResilientZkClient.java
License:Apache License
@Override public void process(final WatchedEvent event) { /***** log event *****/ // log if TRACE if (logger.isTraceEnabled()) { logger.trace("***** Received ZooKeeper Event: {}", ReflectionToStringBuilder.toString(event, ToStringStyle.DEFAULT_STYLE)); }/*from w ww. jav a 2 s . co m*/ /***** pass notifications on to registered Watchers *****/ // if (event.getType() != EventType.None) { if (shutdown) { logger.warn("Already shutdown: not passing event to registered watchers: type={}; path={}", event.getType(), event.getPath()); return; } else { for (Watcher watcher : watcherSet) { watcher.process(event); } } // } /***** process events *****/ switch (event.getType()) { case NodeChildrenChanged: this.childWatchesMap.remove(event.getPath()); break; case NodeCreated: case NodeDataChanged: case NodeDeleted: this.dataWatchesMap.remove(event.getPath()); break; case None: Event.KeeperState eventState = event.getState(); if (eventState == Event.KeeperState.SyncConnected) { // this.connected = true; if (currentSessionId == null) { if (logger.isInfoEnabled()) { logger.info( "Restoring watches as necessary: sessionId={}; connectString={}; sessionTimeout={}", new Object[] { this.zooKeeper.getSessionId(), getConnectString(), getSessionTimeout() }); } restoreWatches(); } this.currentSessionId = this.zooKeeper.getSessionId(); this.sessionPassword = this.zooKeeper.getSessionPasswd(); logger.info("SyncConnected: notifying all waiters: currentSessionId={}; connectString={}", currentSessionId, getConnectString()); synchronized (this) { // notify waiting threads that connection has been // established this.notifyAll(); } logger.info("SyncConnected: notified all waiters: currentSessionId={}; connectString={}", currentSessionId, getConnectString()); } else if (eventState == Event.KeeperState.Disconnected) { // this.connected = false; } else if (eventState == Event.KeeperState.Expired) { // expired session; close ZK connection and reconnect if (!this.shutdown) { // if session has been expired, clear out the existing ID logger.info( "Session has been expired by ZooKeeper cluster: reconnecting to establish new session: oldSessionId={}; connectString={}", currentSessionId, getConnectString()); // null out current session ID this.currentSessionId = null; // do connection in another thread so as to not block the ZK event thread spawnReconnectThread(); } } else { logger.warn("Unhandled state: eventType=" + event.getType() + "; eventState=" + eventState); } break; default: logger.warn("Unhandled event type: eventType=" + event.getType() + "; eventState=" + event.getState()); } }
From source file:io.reign.zk.ResilientZkClientWithCache.java
License:Apache License
synchronized void handleNodeUpdate(WatchedEvent event) { String path = event.getPath(); logger.info("Updating cache entry: path={}; eventType={}", path, event.getType()); try {//from w w w. ja v a2 s . c om byte[] bytes = null; List<String> children = null; Stat stat = null; /** see what we have in cache **/ PathCacheEntry cacheEntry = pathCache.get(path); if (cacheEntry != null) { bytes = cacheEntry.getData(); children = cacheEntry.getChildList(); stat = cacheEntry.getStat(); } if (stat == null) { stat = new Stat(); } /** get update data from zk **/ if (event.getType() == EventType.NodeDataChanged || event.getType() == EventType.NodeCreated) { logger.debug("Change detected: updating path data: path={}", path); bytes = zkClient.getData(path, true, stat); if (cacheEntry == null) { children = zkClient.getChildren(path, true); } } if (event.getType() == EventType.NodeChildrenChanged || event.getType() == EventType.NodeCreated) { logger.debug("Change detected: updating path children: path={}", path); children = zkClient.getChildren(path, true); if (cacheEntry == null) { bytes = zkClient.getData(path, true, stat); } } /** update cache **/ pathCache.put(path, stat, bytes, children); } catch (KeeperException e) { logger.error(this.getClass().getSimpleName() + ": error while trying to update cache entry: " + e + ": path=" + path, e); } catch (InterruptedException e) { logger.warn(this.getClass().getSimpleName() + ": interrupted while trying to update cache entry: " + e + ": path=" + path, e); } }
From source file:io.seldon.api.state.ZkABTestingUpdater.java
License:Apache License
@Override public void run() { logger.info("Starting"); try {/*from www . j av a2 s . c om*/ while (keepRunning) { boolean error = false; try { CuratorFramework client = null; boolean ok = false; for (int attempts = 0; attempts < 4 && !ok; attempts++) { client = curatorHandler.getCurator().usingNamespace(clientName); logger.info("Waiting until zookeeper connected"); ok = client.getZookeeperClient().blockUntilConnectedOrTimedOut(); if (ok) logger.info("zookeeper connected on attempt " + attempts); else { logger.error("Timed out waiting for zookeeper connect : attempt " + attempts); } } if (!ok) { logger.error("Failed to connect to zookeeper after multiple attempts - STOPPING"); return; } queue = new LinkedBlockingQueue<>(); Watcher watcher = new Watcher() { boolean expired; @Override public void process(WatchedEvent event) { try { if (event.getPath() != null) queue.put(event.getPath()); else { logger.warn("Unexpected event " + event.getType().name() + " -> " + event.toString()); switch (event.getState()) { case SyncConnected: { } break; case Expired: { queue.put(""); } break; case Disconnected: { } break; } } } catch (InterruptedException e) { throw new Error(e); } } }; logger.info("Checking path " + DYNAMIC_PARAM_PATH + " exists"); if (client.checkExists().forPath(DYNAMIC_PARAM_PATH) == null) { logger.warn("Path " + DYNAMIC_PARAM_PATH + " does not exist for client " + clientName + " creating..."); client.create().forPath(DYNAMIC_PARAM_PATH); } logger.info("Checking path " + AB_ALG_PATH + " exists"); if (client.checkExists().forPath(AB_ALG_PATH) == null) { logger.warn("Path " + AB_ALG_PATH + " does not exist for client " + clientName + " creating..."); client.create().forPath(AB_ALG_PATH); } client.getData().usingWatcher(watcher).forPath(DYNAMIC_PARAM_PATH); client.getData().usingWatcher(watcher).forPath(AB_ALG_PATH); boolean restart = false; while (keepRunning && !restart) { String path = queue.take(); if (!StringUtils.isEmpty(path)) { logger.info("Alg Path changed " + path); System.out.println("Alg Path changed " + path); if (path.endsWith(DYNAMIC_PARAM_PATH)) { try { byte[] bytes = client.getData().forPath(DYNAMIC_PARAM_PATH); ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes)); DynamicParameterBean bean = (DynamicParameterBean) in.readObject(); in.close(); logger.info("Updating dynamic parameter: " + bean.getName() + " and setting to value: " + bean.getValue() + " for client " + clientName); DynamicParameterServer.setParameterBean(clientName, bean); numUpdates++; } catch (ClassNotFoundException e) { logger.error("Can't find class ", e); } catch (IOException e) { logger.error("Failed to deserialize algorithm for client " + clientName, e); } finally { client.getData().usingWatcher(watcher).forPath(DYNAMIC_PARAM_PATH); } } else if (path.endsWith(AB_ALG_PATH)) { try { byte[] bytes = client.getData().forPath(AB_ALG_PATH); ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes)); ABTest abTest = (ABTest) in.readObject(); in.close(); logger.info("Updating Algorithm percentage for client " + clientName + " to " + abTest.toString()); ABTestingServer.setABTest(clientName, abTest); numUpdates++; } catch (ClassNotFoundException e) { logger.error("Can't find class ", e); } catch (IOException e) { logger.error("Failed to deserialize algorithm for client " + clientName, e); } finally { client.getData().usingWatcher(watcher).forPath(AB_ALG_PATH); } } else { logger.error("Unknown path " + path + " changed so reseting watchers"); } } else { //client.getData().usingWatcher(watcher).forPath(DYNAMIC_PARAM_PATH); //client.getData().usingWatcher(watcher).forPath(AB_ALG_PATH); logger.warn("Will try to restart"); restart = true; } } } catch (IOException e) { logger.error("Exception trying to create sk client ", e); error = true; } catch (Exception e) { logger.error("Exception from zookeeper client ", e); error = true; } finally { } if (keepRunning && error) { logger.info("Sleeping " + sleepTime); Thread.sleep(sleepTime); if (sleepTime * 2 < maxSleepTime) sleepTime = sleepTime * 2; error = false; } } } catch (InterruptedException e) { logger.warn("Sleep interuppted ", e); } logger.info("Stopping"); }