List of usage examples for org.apache.zookeeper WatchedEvent WatchedEvent
public WatchedEvent(EventType eventType, KeeperState keeperState, String path)
From source file:com.bigdata.zookeeper.HierarchicalZNodeWatcher.java
License:Open Source License
/** * This is used pump mock events into the {@link #queue} during startup. * /*from w w w .jav a 2 s .c o m*/ * @param path * @param flags */ protected void placeMockEventInQueue(final String path, final int flags) { if (!pumpMockEvents) { /* * Note: If we are receiving real events then we don't want to do * this. It is just for producing fake events during startup. */ throw new IllegalStateException(); } if (log.isInfoEnabled()) log.info("path=" + path + ", flags=" + flagString(flags)); if ((flags & EXISTS) != 0) { queue.add(new WatchedEvent(Event.EventType.NodeCreated, Event.KeeperState.Unknown, path)); } if ((flags & DATA) != 0) { queue.add(new WatchedEvent(Event.EventType.NodeDataChanged, Event.KeeperState.Unknown, path)); } if ((flags & CHILDREN) != 0) { queue.add(new MockWatchedEvent(Event.EventType.NodeChildrenChanged, Event.KeeperState.Unknown, path)); } }
From source file:com.continuuity.weave.internal.zookeeper.RewatchOnExpireWatcher.java
License:Open Source License
private void exists() { Futures.addCallback(client.exists(path, this), new FutureCallback<Stat>() { @Override//from w w w. j a v a 2s . c o m public void onSuccess(Stat stat) { // Since we know all callbacks and watcher are triggered from single event thread, there is no race condition. Object oldResult = lastResult.getReference(); lastResult.compareAndSet(oldResult, null, true, false); if (stat != oldResult && (stat == null || !stat.equals(oldResult))) { if (stat == null) { // previous stat is not null, means node deleted process(new WatchedEvent(Event.EventType.NodeDeleted, Event.KeeperState.SyncConnected, path)); } else if (oldResult == null) { // previous stat is null, means node created process(new WatchedEvent(Event.EventType.NodeCreated, Event.KeeperState.SyncConnected, path)); } else { // Otherwise, something changed on the node process(new WatchedEvent(Event.EventType.NodeDataChanged, Event.KeeperState.SyncConnected, path)); } } } @Override public void onFailure(Throwable t) { if (RetryUtils.canRetry(t)) { exists(); } else { lastResult.set(null, false); LOG.error("Fail to re-set watch on exists for path " + path, t); } } }); }
From source file:com.continuuity.weave.internal.zookeeper.RewatchOnExpireWatcher.java
License:Open Source License
private void children() { Futures.addCallback(client.getChildren(path, this), new FutureCallback<NodeChildren>() { @Override// ww w.j av a 2 s .co m public void onSuccess(NodeChildren result) { Object oldResult = lastResult.getReference(); lastResult.compareAndSet(oldResult, null, true, false); if (result.equals(oldResult)) { return; } if (!(oldResult instanceof NodeChildren)) { // Something very wrong LOG.error("The same watcher has been used for different event type."); return; } NodeChildren oldNodeChildren = (NodeChildren) oldResult; if (!result.getChildren().equals(oldNodeChildren.getChildren())) { process(new WatchedEvent(Event.EventType.NodeChildrenChanged, Event.KeeperState.SyncConnected, path)); } else { process(new WatchedEvent(Event.EventType.NodeDataChanged, Event.KeeperState.SyncConnected, path)); } } @Override public void onFailure(Throwable t) { if (RetryUtils.canRetry(t)) { children(); return; } lastResult.set(null, false); if (t instanceof KeeperException) { KeeperException.Code code = ((KeeperException) t).code(); if (code == KeeperException.Code.NONODE) { // Node deleted process(new WatchedEvent(Event.EventType.NodeDeleted, Event.KeeperState.SyncConnected, path)); return; } } LOG.error("Fail to re-set watch on getChildren for path " + path, t); } }); }
From source file:com.continuuity.weave.internal.zookeeper.RewatchOnExpireWatcher.java
License:Open Source License
private void data() { Futures.addCallback(client.getData(path, this), new FutureCallback<NodeData>() { @Override//w w w .j a va 2 s .c o m public void onSuccess(NodeData result) { Object oldResult = lastResult.getReference(); lastResult.compareAndSet(oldResult, null, true, false); if (!result.equals(oldResult)) { // Whenever something changed, treated it as data changed. process(new WatchedEvent(Event.EventType.NodeDataChanged, Event.KeeperState.SyncConnected, path)); } } @Override public void onFailure(Throwable t) { if (RetryUtils.canRetry(t)) { data(); return; } lastResult.set(null, false); if (t instanceof KeeperException) { KeeperException.Code code = ((KeeperException) t).code(); if (code == KeeperException.Code.NONODE) { // Node deleted process(new WatchedEvent(Event.EventType.NodeDeleted, Event.KeeperState.SyncConnected, path)); return; } } LOG.error("Fail to re-set watch on getData for path " + path, t); } }); }
From source file:com.facebook.zookeeper.mock.MockZooKeeper.java
License:Apache License
public synchronized void triggerConnect() { state = ZooKeeper.States.CONNECTED;// w w w . j av a2 s . c o m WatchedEvent watchedEvent = new WatchedEvent(EventType.None, KeeperState.SyncConnected, null); dataStore.signalSessionEvent(sessionId, watchedEvent); defaultWatcher.process(watchedEvent); }
From source file:com.facebook.zookeeper.mock.MockZooKeeper.java
License:Apache License
public synchronized void triggerDisconnect() { state = ZooKeeper.States.CONNECTING; WatchedEvent watchedEvent = new WatchedEvent(EventType.None, KeeperState.Disconnected, null); dataStore.signalSessionEvent(sessionId, watchedEvent); defaultWatcher.process(watchedEvent); }
From source file:com.facebook.zookeeper.mock.MockZooKeeper.java
License:Apache License
public synchronized void triggerSessionExpiration() { state = ZooKeeper.States.CLOSED;/*from ww w . ja v a2 s .co m*/ WatchedEvent watchedEvent = new WatchedEvent(EventType.None, KeeperState.Expired, null); dataStore.signalSessionEvent(sessionId, watchedEvent); defaultWatcher.process(watchedEvent); dataStore.clearSession(sessionId); }
From source file:com.facebook.zookeeper.mock.MockZooKeeperDataStore.java
License:Apache License
public synchronized String create(long sessionId, String path, byte[] data, List<ACL> acl, CreateMode createMode) throws KeeperException { if (isRootPath(path)) { throw new KeeperException.NodeExistsException(path); }//w ww .ja v a2s . co m String relativePath = stripRootFromPath(path); String relativeChildPath = root.createDescendant(sessionId, relativePath, data, acl, createMode); String absChildPath = addRootToPath(relativeChildPath); // Trigger any creation watches that may exist if (creationWatchers.containsKey(absChildPath)) { WatchedEvent watchedEvent = new WatchedEvent(EventType.NodeCreated, KeeperState.SyncConnected, absChildPath); for (Watcher watcher : creationWatchers.get(absChildPath)) { watcher.process(watchedEvent); } creationWatchers.remove(absChildPath); } return absChildPath; }
From source file:com.netflix.curator.x.zkclientbridge.CuratorZKClientBridge.java
License:Apache License
@Override public void connect(final Watcher watcher) { if (watcher != null) { CuratorListener localListener = new CuratorListener() { @Override//from w w w . j a v a2s . c om public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception { if (event.getWatchedEvent() != null) { watcher.process(event.getWatchedEvent()); } } }; curator.getCuratorListenable().addListener(localListener); listener.set(localListener); try { BackgroundCallback callback = new BackgroundCallback() { @Override public void processResult(CuratorFramework client, CuratorEvent event) throws Exception { WatchedEvent fakeEvent = new WatchedEvent(Watcher.Event.EventType.None, curator.getZookeeperClient().isConnected() ? Watcher.Event.KeeperState.SyncConnected : Watcher.Event.KeeperState.Disconnected, null); watcher.process(fakeEvent); } }; curator.checkExists().inBackground(callback).forPath("/foo"); } catch (Exception e) { throw new RuntimeException(e); } } }
From source file:com.talis.platform.sequencing.zookeeper.ZooKeeperProviderTest.java
License:Apache License
@Test public void waitForConnectedEventBeforeReturningInstance() throws Exception { ensureServerStopped(DEFAULT_HOST_PORT); final ZooKeeperProvider provider = new ZooKeeperProvider(); final long waitPeriod = 300; Callable<Long> requester = new Callable<Long>() { @Override//from w w w .j a v a2 s. c o m public Long call() throws ZooKeeperInitialisationException { try { long time1 = System.currentTimeMillis(); provider.get(); long time2 = System.currentTimeMillis(); return (time2 - time1); } catch (ZooKeeperInitialisationException e) { e.printStackTrace(); fail("Caught unexpected exception"); throw e; } } }; ExecutorService executor = Executors.newSingleThreadScheduledExecutor(); Future<Long> future = executor.submit(requester); WatchedEvent connectedEvent = new WatchedEvent(Watcher.Event.EventType.None, Watcher.Event.KeeperState.SyncConnected, null); Thread.sleep(waitPeriod + 10); provider.process(connectedEvent); Long fetchTime = future.get(); assertTrue(fetchTime >= waitPeriod); }