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();
}
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());
}
}
}
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);
}
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);
}
}
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);
}
Aggregations