Search in sources :

Example 1 with GetDataCallbackHandler

use of org.apache.helix.manager.zk.ZkAsyncCallbacks.GetDataCallbackHandler in project helix by apache.

the class ZkBaseDataAccessor method get.

/**
 * async get
 */
List<T> get(List<String> paths, List<Stat> stats, boolean[] needRead, boolean throwException) throws HelixException {
    if (paths == null || paths.size() == 0) {
        return Collections.emptyList();
    }
    // init stats
    if (stats != null) {
        stats.clear();
        stats.addAll(Collections.<Stat>nCopies(paths.size(), null));
    }
    long startT = System.nanoTime();
    try {
        // issue asyn get requests
        GetDataCallbackHandler[] cbList = new GetDataCallbackHandler[paths.size()];
        for (int i = 0; i < paths.size(); i++) {
            if (!needRead[i])
                continue;
            String path = paths.get(i);
            cbList[i] = new GetDataCallbackHandler();
            _zkClient.asyncGetData(path, cbList[i]);
        }
        // wait for completion
        for (int i = 0; i < cbList.length; i++) {
            if (!needRead[i])
                continue;
            GetDataCallbackHandler cb = cbList[i];
            cb.waitForSuccess();
        }
        // construct return results
        List<T> records = new ArrayList<T>(Collections.<T>nCopies(paths.size(), null));
        Map<String, Integer> pathFailToRead = new HashMap<>();
        for (int i = 0; i < paths.size(); i++) {
            if (!needRead[i])
                continue;
            GetDataCallbackHandler cb = cbList[i];
            if (Code.get(cb.getRc()) == Code.OK) {
                @SuppressWarnings("unchecked") T record = (T) _zkClient.deserialize(cb._data, paths.get(i));
                records.set(i, record);
                if (stats != null) {
                    stats.set(i, cb._stat);
                }
            } else if (Code.get(cb.getRc()) != Code.NONODE && throwException) {
                throw new HelixMetaDataAccessException(String.format("Failed to read node %s", paths.get(i)));
            } else {
                pathFailToRead.put(paths.get(i), cb.getRc());
            }
        }
        if (pathFailToRead.size() > 0) {
            LOG.warn("Fail to read record for paths: " + pathFailToRead);
        }
        return records;
    } catch (Exception e) {
        throw new HelixMetaDataAccessException(String.format("Fail to read nodes for %s", paths));
    } finally {
        long endT = System.nanoTime();
        if (LOG.isTraceEnabled()) {
            LOG.trace("getData_async, size: " + paths.size() + ", paths: " + paths.get(0) + ",... time: " + (endT - startT) + " ns");
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ZkException(org.I0Itec.zkclient.exception.ZkException) HelixMetaDataAccessException(org.apache.helix.api.exceptions.HelixMetaDataAccessException) HelixException(org.apache.helix.HelixException) ZkNoNodeException(org.I0Itec.zkclient.exception.ZkNoNodeException) ZkNodeExistsException(org.I0Itec.zkclient.exception.ZkNodeExistsException) ZkBadVersionException(org.I0Itec.zkclient.exception.ZkBadVersionException) HelixMetaDataAccessException(org.apache.helix.api.exceptions.HelixMetaDataAccessException) GetDataCallbackHandler(org.apache.helix.manager.zk.ZkAsyncCallbacks.GetDataCallbackHandler)

Aggregations

ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ZkBadVersionException (org.I0Itec.zkclient.exception.ZkBadVersionException)1 ZkException (org.I0Itec.zkclient.exception.ZkException)1 ZkNoNodeException (org.I0Itec.zkclient.exception.ZkNoNodeException)1 ZkNodeExistsException (org.I0Itec.zkclient.exception.ZkNodeExistsException)1 HelixException (org.apache.helix.HelixException)1 HelixMetaDataAccessException (org.apache.helix.api.exceptions.HelixMetaDataAccessException)1 GetDataCallbackHandler (org.apache.helix.manager.zk.ZkAsyncCallbacks.GetDataCallbackHandler)1