Search in sources :

Example 1 with Reporter

use of org.apache.zookeeper.test.system.Instance.Reporter 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;
}
Also used : NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) HashMap(java.util.HashMap) Reporter(org.apache.zookeeper.test.system.Instance.Reporter) Watcher(org.apache.zookeeper.Watcher) ConnectionLossException(org.apache.zookeeper.KeeperException.ConnectionLossException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) NodeExistsException(org.apache.zookeeper.KeeperException.NodeExistsException) Stat(org.apache.zookeeper.data.Stat) HashMap(java.util.HashMap) Map(java.util.Map) KeeperException(org.apache.zookeeper.KeeperException)

Aggregations

IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 KeeperException (org.apache.zookeeper.KeeperException)1 ConnectionLossException (org.apache.zookeeper.KeeperException.ConnectionLossException)1 NoNodeException (org.apache.zookeeper.KeeperException.NoNodeException)1 NodeExistsException (org.apache.zookeeper.KeeperException.NodeExistsException)1 Watcher (org.apache.zookeeper.Watcher)1 Stat (org.apache.zookeeper.data.Stat)1 Reporter (org.apache.zookeeper.test.system.Instance.Reporter)1