Search in sources :

Example 1 with EventType

use of org.apache.zookeeper.Watcher.Event.EventType in project pinpoint by naver.

the class ZookeeperUtils method isConnectedEvent.

public static boolean isConnectedEvent(WatchedEvent event) {
    KeeperState state = event.getState();
    EventType eventType = event.getType();
    return isConnectedEvent(state, eventType);
}
Also used : EventType(org.apache.zookeeper.Watcher.Event.EventType) KeeperState(org.apache.zookeeper.Watcher.Event.KeeperState)

Example 2 with EventType

use of org.apache.zookeeper.Watcher.Event.EventType in project pinpoint by naver.

the class ZookeeperUtils method isDisconnectedEvent.

public static boolean isDisconnectedEvent(WatchedEvent event) {
    KeeperState state = event.getState();
    EventType eventType = event.getType();
    return isDisconnectedEvent(state, eventType);
}
Also used : EventType(org.apache.zookeeper.Watcher.Event.EventType) KeeperState(org.apache.zookeeper.Watcher.Event.KeeperState)

Example 3 with EventType

use of org.apache.zookeeper.Watcher.Event.EventType in project pinpoint by naver.

the class ZookeeperClusterDataManager method process.

@SuppressWarnings("deprecation")
@Override
public void process(WatchedEvent event) {
    logger.info("Handle Zookeeper Event({}) started.", event);
    KeeperState state = event.getState();
    EventType eventType = event.getType();
    String path = event.getPath();
    // when this happens, ephemeral node disappears
    // reconnects automatically, and process gets notified for all events
    boolean result = false;
    if (state == KeeperState.SyncConnected || state == KeeperState.NoSyncConnected) {
        if (eventType == EventType.NodeChildrenChanged) {
            result = handleNodeChildrenChanged(path);
        } else if (eventType == EventType.NodeDeleted) {
            result = handleNodeDeleted(path);
        } else if (eventType == EventType.NodeDataChanged) {
            result = handleNodeDataChanged(path);
        }
    }
    if (result) {
        logger.info("Handle Zookeeper Event({}) completed.", event);
    } else {
        logger.info("Handle Zookeeper Event({}) failed.", event);
    }
}
Also used : EventType(org.apache.zookeeper.Watcher.Event.EventType) KeeperState(org.apache.zookeeper.Watcher.Event.KeeperState)

Example 4 with EventType

use of org.apache.zookeeper.Watcher.Event.EventType in project zookeeper by apache.

the class WatchedEventTest method testCreatingWatchedEvent.

@Test
public void testCreatingWatchedEvent() {
    // EventWatch is a simple, immutable type, so all we need to do
    // is make sure we can create all possible combinations of values.
    EnumSet<EventType> allTypes = EnumSet.allOf(EventType.class);
    EnumSet<KeeperState> allStates = EnumSet.allOf(KeeperState.class);
    WatchedEvent we;
    for (EventType et : allTypes) {
        for (KeeperState ks : allStates) {
            we = new WatchedEvent(et, ks, "blah");
            assertEquals(et, we.getType());
            assertEquals(ks, we.getState());
            assertEquals("blah", we.getPath());
        }
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) EventType(org.apache.zookeeper.Watcher.Event.EventType) KeeperState(org.apache.zookeeper.Watcher.Event.KeeperState) Test(org.junit.jupiter.api.Test)

Example 5 with EventType

use of org.apache.zookeeper.Watcher.Event.EventType in project zookeeper by apache.

the class RemoveWatchesCmdTest method verifyRemoveAnyWatches.

private void verifyRemoveAnyWatches(boolean local) throws Exception {
    final Map<String, List<EventType>> pathVsEvent = new HashMap<>();
    LOG.info("Adding NodeChildrenChanged, NodeDataChanged watchers");
    final CountDownLatch watcherLatch = new CountDownLatch(2);
    Watcher watcher = new Watcher() {

        @Override
        public void process(WatchedEvent event) {
            switch(event.getType()) {
                case ChildWatchRemoved:
                case DataWatchRemoved:
                    addWatchNotifications(pathVsEvent, event);
                    watcherLatch.countDown();
                    break;
                case NodeChildrenChanged:
                case NodeDataChanged:
                    addWatchNotifications(pathVsEvent, event);
                    break;
            }
        }

        private void addWatchNotifications(Map<String, List<EventType>> pathVsEvent, WatchedEvent event) {
            pathVsEvent.computeIfAbsent(event.getPath(), k -> new ArrayList<>()).add(event.getType());
        }
    };
    zk.create("/testnode1", "data".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    zk.getChildren("/testnode1", watcher);
    zk.getData("/testnode1", watcher, null);
    String cmdstring = "removewatches /testnode1 -a";
    if (local) {
        LOG.info("Stopping ZK server to verify deletion of watches locally");
        stopServer();
        cmdstring = "removewatches /testnode1 -a -l";
    }
    LOG.info("Remove watchers using shell command : {}", cmdstring);
    zkMain.cl.parseCommand(cmdstring);
    assertTrue(zkMain.processZKCmd(zkMain.cl), "Removewatches cmd fails to remove child/data watches");
    LOG.info("Waiting for the WatchRemoved events");
    watcherLatch.await(10, TimeUnit.SECONDS);
    assertEquals(1, pathVsEvent.size(), "Didn't receives WatchRemoved events!");
    assertTrue(pathVsEvent.get("/testnode1").contains(EventType.DataWatchRemoved), "Didn't receives DataWatchRemoved!");
    assertTrue(pathVsEvent.get("/testnode1").contains(EventType.ChildWatchRemoved), "Didn't receives ChildWatchRemoved!");
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Logger(org.slf4j.Logger) Ids(org.apache.zookeeper.ZooDefs.Ids) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Map(java.util.Map) EventType(org.apache.zookeeper.Watcher.Event.EventType) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ClientBase(org.apache.zookeeper.test.ClientBase) Timeout(org.junit.jupiter.api.Timeout) HashMap(java.util.HashMap) EventType(org.apache.zookeeper.Watcher.Event.EventType) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) CountDownLatch(java.util.concurrent.CountDownLatch) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

EventType (org.apache.zookeeper.Watcher.Event.EventType)13 KeeperState (org.apache.zookeeper.Watcher.Event.KeeperState)9 Test (org.junit.jupiter.api.Test)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 WatchedEvent (org.apache.zookeeper.WatchedEvent)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 TimeUnit (java.util.concurrent.TimeUnit)1 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)1 Ids (org.apache.zookeeper.ZooDefs.Ids)1 WatcherEvent (org.apache.zookeeper.proto.WatcherEvent)1 ClientBase (org.apache.zookeeper.test.ClientBase)1 Test (org.junit.Test)1 AfterEach (org.junit.jupiter.api.AfterEach)1 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)1 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)1 Timeout (org.junit.jupiter.api.Timeout)1