Search in sources :

Example 1 with KeeperState

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

the class ReadOnlyModeTest method testConnectionEvents.

/**
     * Ensures that upon connection to a read-only server client receives
     * ConnectedReadOnly state notification.
     */
@Test(timeout = 90000)
public void testConnectionEvents() throws Exception {
    final List<KeeperState> states = new ArrayList<KeeperState>();
    ZooKeeper zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT, new Watcher() {

        public void process(WatchedEvent event) {
            states.add(event.getState());
        }
    }, true);
    boolean success = false;
    for (int i = 0; i < 30; i++) {
        try {
            zk.create("/test", "test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            success = true;
            break;
        } catch (KeeperException.ConnectionLossException e) {
            Thread.sleep(1000);
        }
    }
    Assert.assertTrue("Did not succeed in connecting in 30s", success);
    // kill peer and wait no more than 5 seconds for read-only server
    // to be started (which should take one tickTime (2 seconds))
    qu.shutdown(2);
    // Re-connect the client (in case we were connected to the shut down
    // server and the local session was not persisted).
    zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT, new Watcher() {

        public void process(WatchedEvent event) {
            states.add(event.getState());
        }
    }, true);
    long start = Time.currentElapsedTime();
    while (!(zk.getState() == States.CONNECTEDREADONLY)) {
        Thread.sleep(200);
        // FIXME this was originally 5 seconds, but realistically, on random/slow/virt hosts, there is no way to guarantee this
        Assert.assertTrue("Can't connect to the server", Time.currentElapsedTime() - start < 30000);
    }
    // At this point states list should contain, in the given order,
    // SyncConnected, Disconnected, and ConnectedReadOnly states
    Assert.assertTrue("ConnectedReadOnly event wasn't received", states.get(2) == KeeperState.ConnectedReadOnly);
    zk.close();
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeper(org.apache.zookeeper.ZooKeeper) ArrayList(java.util.ArrayList) Watcher(org.apache.zookeeper.Watcher) CountdownWatcher(org.apache.zookeeper.test.ClientBase.CountdownWatcher) KeeperState(org.apache.zookeeper.Watcher.Event.KeeperState) KeeperException(org.apache.zookeeper.KeeperException) Test(org.junit.Test)

Example 2 with KeeperState

use of org.apache.zookeeper.Watcher.Event.KeeperState 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");
            Assert.assertEquals(et, we.getType());
            Assert.assertEquals(ks, we.getState());
            Assert.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.Test)

Example 3 with KeeperState

use of org.apache.zookeeper.Watcher.Event.KeeperState 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 4 with KeeperState

use of org.apache.zookeeper.Watcher.Event.KeeperState 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 (ZookeeperUtils.isDisconnectedEvent(event)) {
        result = handleDisconnected();
        if (state == KeeperState.Expired) {
            client.reconnectWhenSessionExpired();
        }
    } else if (state == KeeperState.SyncConnected || state == KeeperState.NoSyncConnected) {
        if (eventType == EventType.None) {
            result = handleConnected();
        } else 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 5 with KeeperState

use of org.apache.zookeeper.Watcher.Event.KeeperState 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)

Aggregations

KeeperState (org.apache.zookeeper.Watcher.Event.KeeperState)11 EventType (org.apache.zookeeper.Watcher.Event.EventType)7 WatchedEvent (org.apache.zookeeper.WatchedEvent)3 Test (org.junit.Test)3 CanalException (com.alibaba.otter.canal.common.CanalException)1 ServerRunningMonitor (com.alibaba.otter.canal.common.zookeeper.running.ServerRunningMonitor)1 InstanceConfigMonitor (com.alibaba.otter.canal.deployer.monitor.InstanceConfigMonitor)1 ManagerInstanceConfigMonitor (com.alibaba.otter.canal.deployer.monitor.ManagerInstanceConfigMonitor)1 SpringInstanceConfigMonitor (com.alibaba.otter.canal.deployer.monitor.SpringInstanceConfigMonitor)1 CanalServerException (com.alibaba.otter.canal.server.exception.CanalServerException)1 MigrateMap (com.google.common.collect.MigrateMap)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 TimeoutException (java.util.concurrent.TimeoutException)1 IZkStateListener (org.I0Itec.zkclient.IZkStateListener)1 ZkNoNodeException (org.I0Itec.zkclient.exception.ZkNoNodeException)1 ZkNodeExistsException (org.I0Itec.zkclient.exception.ZkNodeExistsException)1 SolrException (org.apache.solr.common.SolrException)1 KeeperException (org.apache.zookeeper.KeeperException)1 Watcher (org.apache.zookeeper.Watcher)1