Search in sources :

Example 1 with ByteArrayCallback

use of org.voltcore.zk.ZKUtil.ByteArrayCallback in project voltdb by VoltDB.

the class MapCache method processParentEvent.

/**
     * Rebuild the point-in-time snapshot of the children objects
     * and set watches on new children.
     *
     * @Param event may be null on the first initialization.
     */
private void processParentEvent(WatchedEvent event) throws Exception {
    // get current children snapshot and reset this watch.
    Set<String> children = new TreeSet<String>(m_zk.getChildren(m_rootNode, m_parentWatch));
    // intersect to get newChildren and update m_lastChildren to the current set.
    Set<String> newChildren = new HashSet<String>(children);
    newChildren.removeAll(m_lastChildren);
    m_lastChildren = children;
    List<ByteArrayCallback> callbacks = new ArrayList<ByteArrayCallback>();
    for (String child : children) {
        ByteArrayCallback cb = new ByteArrayCallback();
        // set watches on new children.
        if (newChildren.contains(child)) {
            m_zk.getData(ZKUtil.joinZKPath(m_rootNode, child), m_childWatch, cb, null);
        } else {
            m_zk.getData(ZKUtil.joinZKPath(m_rootNode, child), false, cb, null);
        }
        callbacks.add(cb);
    }
    HashMap<String, JSONObject> cache = new HashMap<String, JSONObject>();
    for (ByteArrayCallback callback : callbacks) {
        try {
            byte[] payload = callback.getData();
            JSONObject jsObj = new JSONObject(new String(payload, "UTF-8"));
            cache.put(callback.getPath(), jsObj);
        } catch (KeeperException.NoNodeException e) {
        // child may have been deleted between the parent trigger and getData.
        }
    }
    m_publicCache.set(ImmutableMap.copyOf(cache));
    if (m_cb != null) {
        m_cb.run(m_publicCache.get());
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ByteArrayCallback(org.voltcore.zk.ZKUtil.ByteArrayCallback) JSONObject(org.json_voltpatches.JSONObject) TreeSet(java.util.TreeSet) KeeperException(org.apache.zookeeper_voltpatches.KeeperException) HashSet(java.util.HashSet)

Example 2 with ByteArrayCallback

use of org.voltcore.zk.ZKUtil.ByteArrayCallback in project voltdb by VoltDB.

the class LeaderCache method processChildEvent.

/**
     * Update a modified child and republish a new snapshot. This may indicate
     * a deleted child or a child with modified data.
     */
private void processChildEvent(WatchedEvent event) throws Exception {
    HashMap<Integer, Long> cacheCopy = new HashMap<Integer, Long>(m_publicCache);
    ByteArrayCallback cb = new ByteArrayCallback();
    m_zk.getData(event.getPath(), m_childWatch, cb, null);
    try {
        // cb.getData() and cb.getPath() throw KeeperException
        byte[] payload = cb.getData();
        long HSId = Long.valueOf(new String(payload, "UTF-8"));
        cacheCopy.put(getPartitionIdFromZKPath(cb.getPath()), HSId);
    } catch (KeeperException.NoNodeException e) {
        // rtb: I think result's path is the same as cb.getPath()?
        cacheCopy.remove(getPartitionIdFromZKPath(event.getPath()));
    }
    m_publicCache = ImmutableMap.copyOf(cacheCopy);
    if (m_cb != null) {
        m_cb.run(m_publicCache);
    }
}
Also used : ByteArrayCallback(org.voltcore.zk.ZKUtil.ByteArrayCallback) HashMap(java.util.HashMap) KeeperException(org.apache.zookeeper_voltpatches.KeeperException)

Example 3 with ByteArrayCallback

use of org.voltcore.zk.ZKUtil.ByteArrayCallback in project voltdb by VoltDB.

the class LeaderCache method processParentEvent.

/**
     * Rebuild the point-in-time snapshot of the children objects
     * and set watches on new children.
     *
     * @Param event may be null on the first initialization.
     */
private void processParentEvent(WatchedEvent event) throws Exception {
    // get current children snapshot and reset this watch.
    Set<String> children = new TreeSet<String>(m_zk.getChildren(m_rootNode, m_parentWatch));
    // intersect to get newChildren and update m_lastChildren to the current set.
    Set<String> newChildren = new HashSet<String>(children);
    newChildren.removeAll(m_lastChildren);
    m_lastChildren = children;
    List<ByteArrayCallback> callbacks = new ArrayList<ByteArrayCallback>();
    for (String child : children) {
        ByteArrayCallback cb = new ByteArrayCallback();
        // set watches on new children.
        if (newChildren.contains(child)) {
            m_zk.getData(ZKUtil.joinZKPath(m_rootNode, child), m_childWatch, cb, null);
        } else {
            m_zk.getData(ZKUtil.joinZKPath(m_rootNode, child), false, cb, null);
        }
        callbacks.add(cb);
    }
    HashMap<Integer, Long> cache = new HashMap<Integer, Long>();
    for (ByteArrayCallback callback : callbacks) {
        try {
            byte[] payload = callback.getData();
            long HSId = Long.valueOf(new String(payload, "UTF-8"));
            cache.put(getPartitionIdFromZKPath(callback.getPath()), HSId);
        } catch (KeeperException.NoNodeException e) {
        // child may have been deleted between the parent trigger and getData.
        }
    }
    m_publicCache = ImmutableMap.copyOf(cache);
    if (m_cb != null) {
        m_cb.run(m_publicCache);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ByteArrayCallback(org.voltcore.zk.ZKUtil.ByteArrayCallback) TreeSet(java.util.TreeSet) KeeperException(org.apache.zookeeper_voltpatches.KeeperException) HashSet(java.util.HashSet)

Example 4 with ByteArrayCallback

use of org.voltcore.zk.ZKUtil.ByteArrayCallback in project voltdb by VoltDB.

the class MapCache method processChildEvent.

/**
     * Update a modified child and republish a new snapshot. This may indicate
     * a deleted child or a child with modified data.
     */
private void processChildEvent(WatchedEvent event) throws Exception {
    HashMap<String, JSONObject> cacheCopy = new HashMap<String, JSONObject>(m_publicCache.get());
    ByteArrayCallback cb = new ByteArrayCallback();
    m_zk.getData(event.getPath(), m_childWatch, cb, null);
    try {
        byte[] payload = cb.getData();
        JSONObject jsObj = new JSONObject(new String(payload, "UTF-8"));
        cacheCopy.put(cb.getPath(), jsObj);
    } catch (KeeperException.NoNodeException e) {
        cacheCopy.remove(event.getPath());
    }
    m_publicCache.set(ImmutableMap.copyOf(cacheCopy));
    if (m_cb != null) {
        m_cb.run(m_publicCache.get());
    }
}
Also used : ByteArrayCallback(org.voltcore.zk.ZKUtil.ByteArrayCallback) JSONObject(org.json_voltpatches.JSONObject) HashMap(java.util.HashMap) KeeperException(org.apache.zookeeper_voltpatches.KeeperException)

Aggregations

HashMap (java.util.HashMap)4 KeeperException (org.apache.zookeeper_voltpatches.KeeperException)4 ByteArrayCallback (org.voltcore.zk.ZKUtil.ByteArrayCallback)4 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 TreeSet (java.util.TreeSet)2 JSONObject (org.json_voltpatches.JSONObject)2