Refine search
private ZooKeeper getZooKeeper(String zooKeeperConnectString, AuthMode authMode, byte[] authData) throws IOException { CountDownLatch connectionLatch = new CountDownLatch(1); ZooKeeper zooKeeper = new ZooKeeper(zooKeeperConnectString, 3000, watchedEvent -> { if (LOGGER.isDebugEnabled()) { LOGGER.debug("ZooKeeper server state changed to {} in {}", watchedEvent.getState(), zooKeeperConnectString); } if (watchedEvent.getType().equals(Watcher.Event.EventType.None) && watchedEvent.getState().equals(Watcher.Event.KeeperState.SyncConnected)) { connectionLatch.countDown(); } }); final boolean connected; try { connected = connectionLatch.await(5, TimeUnit.SECONDS); } catch (InterruptedException e) { closeZooKeeper(zooKeeper); Thread.currentThread().interrupt(); throw new IOException(String.format("interrupted while waiting for ZooKeeper connection to %s", zooKeeperConnectString), e); } if (!connected) { closeZooKeeper(zooKeeper); throw new IOException(String.format("unable to connect to %s", zooKeeperConnectString)); } if (authMode.equals(AuthMode.DIGEST)) { zooKeeper.addAuthInfo(SCHEME_DIGEST, authData); } return zooKeeper; }
@Override public void process(WatchedEvent event) { LOG.debug("Received event {} from lock {} at {} : watcher epoch {}, lock epoch {}.", new Object[] { event, lockPath, System.currentTimeMillis(), epoch, ZKSessionLock.this.epoch.get() }); if (event.getType() == Watcher.Event.EventType.None) { switch (event.getState()) { case SyncConnected: break; case Expired: LOG.info("Session {} is expired for lock {} at {} : watcher epoch {}, lock epoch {}.", new Object[] { lockId.getRight(), lockPath, System.currentTimeMillis(), epoch, ZKSessionLock.this.epoch.get() }); handleSessionExpired(epoch); break; default: break; } } else if (event.getType() == Event.EventType.NodeDeleted) { // this handles the case where we have aborted a lock and deleted ourselves but still have a // watch on the nextLowestNode. This is a workaround since ZK doesn't support unsub. if (!event.getPath().equals(watchedNode)) { LOG.warn("{} (watching {}) ignored watched event from {} ", new Object[] { lockId, watchedNode, event.getPath() }); return; } handleNodeDelete(epoch, event); } else { LOG.warn("Unexpected ZK event: {}", event.getType().name()); } }
@Test public void testCreatingWatchedEventFromWrapper() { // Make sure we can handle any type of correct wrapper EnumSet<EventType> allTypes = EnumSet.allOf(EventType.class); EnumSet<KeeperState> allStates = EnumSet.allOf(KeeperState.class); WatchedEvent we; WatcherEvent wep; for(EventType et : allTypes) { for(KeeperState ks : allStates) { wep = new WatcherEvent(et.getIntValue(), ks.getIntValue(), "blah"); we = new WatchedEvent(wep); Assert.assertEquals(et, we.getType()); Assert.assertEquals(ks, we.getState()); Assert.assertEquals("blah", we.getPath()); } } }
public void process(WatchedEvent event) throws Exception { if (EventType.NodeChildrenChanged.equals(event.getType())) { this.processNodeChildrenChanged(event); } else if (EventType.NodeDeleted.equals(event.getType())) { String application = CommonUtils.getApplication(this.endpoint); String parent = String.format("%s/%s/instances", CONSTANTS_ROOT_PATH, application); String current = event.getPath(); String path = String.format("%s/%s", parent, this.endpoint); if (StringUtils.equalsIgnoreCase(path, current)) { this.initializeCurrentClusterInstanceConfigIfNecessary(false); } // end-if (StringUtils.equalsIgnoreCase(path, current)) } }
public void process(WatchedEvent event) throws Exception { if (EventType.NodeChildrenChanged.equals(event.getType())) { String parent = String.format("%s/%s/rollback", CONSTANTS_ROOT_PATH, CommonUtils.getApplication(this.endpoint)); this.curatorFramework.getChildren().usingWatcher(this).inBackground(this).forPath(parent); } }
/** * */ @Override public void process(WatchedEvent event) { client.registerListener(event.getPath(), ZKSyncEventFactory.getValueChangeEvent(), this); // if(LOG.isDebugEnabled()){ LOG.debug(event.toString()); // } if (event.getType().equals(EventType.NodeChildrenChanged)) { LOG.debug("Node children changed - " + event.getPath()); onChildKeySetChange(); } else if (event.getType().equals(EventType.NodeDeleted)) { LOG.debug("Node children deleted - " + event.getPath()); onDelete(); } else if (event.getType().equals(EventType.NodeDataChanged)) { LOG.debug("Node children changed - " + event.getPath()); onChange(); } }
@Test public void testConvertingToEventWrapper() { WatchedEvent we = new WatchedEvent(EventType.NodeCreated, KeeperState.Expired, "blah"); WatcherEvent wew = we.getWrapper(); Assert.assertEquals(EventType.NodeCreated.getIntValue(), wew.getType()); Assert.assertEquals(KeeperState.Expired.getIntValue(), wew.getState()); Assert.assertEquals("blah", wew.getPath()); } }
@Test public void testIntConversion() { // Ensure that we can convert all valid integers to EventTypes EnumSet<EventType> allTypes = EnumSet.allOf(EventType.class); for(EventType et : allTypes) { Assert.assertEquals(et, EventType.fromInt( et.getIntValue() ) ); } }
/** * Convert a WatcherEvent sent over the wire into a full-fledged WatcherEvent */ public WatchedEvent(WatcherEvent eventMessage) { keeperState = KeeperState.fromInt(eventMessage.getState()); eventType = EventType.fromInt(eventMessage.getType()); path = eventMessage.getPath(); }
@Test public void testInvalidIntConversion() { try { EventType.fromInt(324242); Assert.fail("Was able to create an invalid EventType via an integer"); } catch(RuntimeException re) { // we're good. } } }
/** * Convert WatchedEvent to type that can be sent over network */ public WatcherEvent getWrapper() { return new WatcherEvent(eventType.getIntValue(), keeperState.getIntValue(), path); } }
@Override public void execute(KeeperState state, EventType type, String path) { if (active.get()) { if (!(state.equals(KeeperState.SyncConnected))) { LOG.warn("Received event " + state + ":" + type + ":" + path + " with disconnected Zookeeper."); } else { LOG.info("Received event " + state + ":" + type + ":" + path); } if (!type.equals(EventType.None)) { for (Entry<UUID, ClusterStateCallback> e : callbacks.entrySet()) { ClusterStateCallback fn = e.getValue(); fn.execute(type, path); } } } } };
/** * Convert a WatcherEvent sent over the wire into a full-fledged WatcherEvent */ public WatchedEvent(WatcherEvent eventMessage) { keeperState = KeeperState.fromInt(eventMessage.getState()); eventType = EventType.fromInt(eventMessage.getType()); path = eventMessage.getPath(); }
/** * Convert WatchedEvent to type that can be sent over network */ public WatcherEvent getWrapper() { return new WatcherEvent(eventType.getIntValue(), keeperState.getIntValue(), path); } }
@SuppressWarnings("unchecked") @Override public void execute(KeeperState state, EventType type, String path) { if (active.get()) { if (!(state.equals(KeeperState.SyncConnected))) { LOG.warn("Received event " + state + ":" + type + ":" + path + " with disconnected Zookeeper."); } if (!type.equals(EventType.None)) { for (Entry<UUID, ClusterStateCallback> e : callbacks .entrySet()) { ClusterStateCallback fn = e.getValue(); fn.execute(type, path); } } } } };
/** * Convert a WatcherEvent sent over the wire into a full-fledged WatcherEvent */ public WatchedEvent(WatcherEvent eventMessage) { keeperState = KeeperState.fromInt(eventMessage.getState()); eventType = EventType.fromInt(eventMessage.getType()); path = eventMessage.getPath(); }
@Override public void callBack(String target, EventType etype) throws Exception { System.out.println(target + " got event " + etype.name() + ", num=" + etype.getIntValue()); if (etype.getIntValue() == ZkConnect.DATACHANGED) System.out.println(zk.getObject(target)); zk.addWatch(target, this); }
/** * Convert WatchedEvent to type that can be sent over network */ public WatcherEvent getWrapper() { return new WatcherEvent(eventType.getIntValue(), keeperState.getIntValue(), path); } }
private Counter getEventCounter(EventType type) { Counter c = eventCounters.get(type); if (null == c) { Counter newCounter = statsLogger.scope("events").getCounter(type.name()); Counter oldCounter = eventCounters.putIfAbsent(type, newCounter); if (null != oldCounter) { c = oldCounter; } else { c = newCounter; } } return c; }