use of org.apache.curator.framework.api.CuratorEvent in project Singularity by HubSpot.
the class CuratorAsyncManager method getAsyncThrows.
private <T> Map<String, T> getAsyncThrows(final String pathNameForLogs, final Collection<String> paths, final Transcoder<T> transcoder, final Optional<ZkCache<T>> cache) throws Exception {
final Map<String, T> objects = new HashMap<>(paths.size());
if (cache.isPresent()) {
for (Iterator<String> itr = paths.iterator(); itr.hasNext(); ) {
final String path = itr.next();
final Optional<T> fromCache = cache.get().get(path);
if (fromCache.isPresent()) {
objects.put(path, fromCache.get());
itr.remove();
}
}
}
if (paths.isEmpty()) {
return objects;
}
final Map<String, T> synchronizedObjects = Collections.synchronizedMap(objects);
final CountDownLatch latch = new CountDownLatch(paths.size());
final AtomicInteger bytes = new AtomicInteger();
final BackgroundCallback callback = new BackgroundCallback() {
@Override
public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
try {
if (event.getData() == null || event.getData().length == 0) {
LOG.trace("Expected active node {} but it wasn't there", event.getPath());
return;
}
bytes.getAndAdd(event.getData().length);
final T object = transcoder.fromBytes(event.getData());
synchronizedObjects.put(event.getPath(), object);
if (cache.isPresent()) {
cache.get().set(event.getPath(), object);
}
} finally {
latch.countDown();
}
}
};
return queryAndReturnResultsThrows(objects, paths, callback, latch, pathNameForLogs, bytes, CuratorQueryMethod.GET_DATA);
}
Aggregations