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