use of org.apache.zookeeper.Watcher in project whirr by apache.
the class ZooKeeperServiceTest method test.
@Test
public void test() throws Exception {
class ConnectionWatcher implements Watcher {
private ZooKeeper zk;
private CountDownLatch latch = new CountDownLatch(1);
public void connect(String hosts) throws IOException, InterruptedException {
zk = new ZooKeeper(hosts, 5000, this);
latch.await();
}
public ZooKeeper getZooKeeper() {
return zk;
}
@Override
public void process(WatchedEvent event) {
if (event.getState() == KeeperState.SyncConnected) {
latch.countDown();
}
}
public void close() throws InterruptedException {
if (zk != null) {
zk.close();
}
}
}
String path = "/data";
String data = "Hello";
ConnectionWatcher watcher = new ConnectionWatcher();
watcher.connect(cluster.getHosts());
watcher.getZooKeeper().create(path, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
watcher.close();
watcher = new ConnectionWatcher();
watcher.connect(cluster.getHosts());
byte[] actualData = watcher.getZooKeeper().getData(path, false, null);
assertEquals(data, new String(actualData));
watcher.close();
}
use of org.apache.zookeeper.Watcher in project zookeeper by apache.
the class WatchManager method getWatchesByPath.
/**
* Returns a watch report by path.
*
* @return watch report
* @see WatchesPathReport
*/
synchronized WatchesPathReport getWatchesByPath() {
Map<String, Set<Long>> path2ids = new HashMap<String, Set<Long>>();
for (Entry<String, HashSet<Watcher>> e : watchTable.entrySet()) {
Set<Long> ids = new HashSet<Long>(e.getValue().size());
path2ids.put(e.getKey(), ids);
for (Watcher watcher : e.getValue()) {
ids.add(((ServerCnxn) watcher).getSessionId());
}
}
return new WatchesPathReport(path2ids);
}
use of org.apache.zookeeper.Watcher in project zookeeper by apache.
the class BaseSysTest method setUp.
@Before
public void setUp() throws Exception {
if (!fakeMachines) {
zk = new ZooKeeper(zkHostPort, 15000, new Watcher() {
public void process(WatchedEvent e) {
}
});
im = new InstanceManager(zk, prefix);
}
}
use of org.apache.zookeeper.Watcher in project zookeeper by apache.
the class InstanceContainer method processResult.
@Override
public void processResult(int rc, String path, Object ctx, List<String> children) {
if (rc != KeeperException.Code.OK.intValue()) {
// try it again
zk.getChildren(assignmentsNode, true, this, null);
return;
}
HashMap<String, Instance> newList = new HashMap<String, Instance>();
// check for differences
Stat stat = new Stat();
for (String child : children) {
Instance i = instances.remove(child);
if (i == null) {
// Start up a new instance
byte[] data = null;
String myNode = assignmentsNode + '/' + child;
while (true) {
try {
data = zk.getData(myNode, true, stat);
break;
} catch (NoNodeException e) {
// The node doesn't exist anymore, so skip it
break;
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
return;
}
}
if (data != null) {
String instanceSpec = new String(data);
int spaceIndex = instanceSpec.indexOf(' ');
String clazz;
String conf;
if (spaceIndex == -1) {
clazz = instanceSpec;
conf = null;
} else {
clazz = instanceSpec.substring(0, spaceIndex);
conf = instanceSpec.substring(spaceIndex + 1);
}
try {
Class<?> c = Class.forName(clazz);
i = (Instance) c.newInstance();
Reporter reporter = new MyReporter(child);
i.setReporter(reporter);
i.configure(conf);
i.start();
newList.put(child, i);
int ver = stat.getVersion();
Instance myInstance = i;
DataCallback dc = new MyDataCallback(myNode, myInstance, ver);
Watcher watcher = new MyWatcher(myNode, dc);
zk.getData(myNode, watcher, dc, watcher);
} catch (Exception e) {
LOG.warn("Skipping " + child, e);
if (e.getCause() != null) {
LOG.warn("Caused by", e.getCause());
}
}
}
} else {
// just move it to the new list
newList.put(child, i);
}
}
// kill anything that was removed for the children
for (Map.Entry<String, Instance> i : instances.entrySet()) {
i.getValue().stop();
try {
rmnod(reportsNode + '/' + i.getKey());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (KeeperException e) {
e.printStackTrace();
}
}
instances = newList;
}
use of org.apache.zookeeper.Watcher in project zookeeper by apache.
the class DataTreeTest method testRootWatchTriggered.
@Test(timeout = 60000)
public void testRootWatchTriggered() throws Exception {
class MyWatcher implements Watcher {
boolean fired = false;
public void process(WatchedEvent event) {
if (event.getPath().equals("/"))
fired = true;
}
}
MyWatcher watcher = new MyWatcher();
// set a watch on the root node
dt.getChildren("/", new Stat(), watcher);
// add a new node, should trigger a watch
dt.createNode("/xyz", new byte[0], null, 0, dt.getNode("/").stat.getCversion() + 1, 1, 1);
Assert.assertFalse("Root node watch not triggered", !watcher.fired);
}
Aggregations