use of org.apache.curator.framework.api.CuratorEvent in project Singularity by HubSpot.
the class CuratorAsyncManager method getAsyncNestedChildDataAsMapThrows.
protected <T, Q> Map<T, List<Q>> getAsyncNestedChildDataAsMapThrows(final String pathNameForLogs, final Map<String, T> parentPathsMap, final String subpath, final Transcoder<Q> transcoder) throws Exception {
final Map<String, T> allPathsMap = Maps.newHashMap();
for (Map.Entry<String, T> entry : parentPathsMap.entrySet()) {
for (String child : getChildren(ZKPaths.makePath(entry.getKey(), subpath))) {
allPathsMap.put(ZKPaths.makePath(entry.getKey(), subpath, child), entry.getValue());
}
}
final ConcurrentHashMap<T, List<Q>> resultsMap = new ConcurrentHashMap<>();
final CountDownLatch latch = new CountDownLatch(allPathsMap.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 Q object = transcoder.fromBytes(event.getData());
if (allPathsMap.get(event.getPath()) != null) {
resultsMap.putIfAbsent(allPathsMap.get(event.getPath()), new ArrayList<Q>());
resultsMap.get(allPathsMap.get(event.getPath())).add(object);
}
} finally {
latch.countDown();
}
}
};
return queryAndReturnResultsThrows(resultsMap, allPathsMap.keySet(), callback, latch, pathNameForLogs, bytes, CuratorQueryMethod.GET_DATA);
}
use of org.apache.curator.framework.api.CuratorEvent in project Singularity by HubSpot.
the class CuratorAsyncManager method getChildrenAsIdsForParentsThrows.
private <T extends SingularityId> List<T> getChildrenAsIdsForParentsThrows(final String pathNameforLogs, final Collection<String> parents, final IdTranscoder<T> idTranscoder) throws Exception {
if (parents.isEmpty()) {
return Collections.emptyList();
}
final List<T> objects = Lists.newArrayListWithExpectedSize(parents.size());
final List<T> synchronizedObjects = Collections.synchronizedList(objects);
final CountDownLatch latch = new CountDownLatch(parents.size());
final BackgroundCallback callback = new BackgroundCallback() {
@Override
public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
try {
if (event.getChildren() == null || event.getChildren().size() == 0) {
LOG.trace("Expected children for node {} - but found none", event.getPath());
return;
}
synchronizedObjects.addAll(Lists.transform(event.getChildren(), Transcoders.getFromStringFunction(idTranscoder)));
} finally {
latch.countDown();
}
}
};
return queryAndReturnResultsThrows(synchronizedObjects, parents, callback, latch, pathNameforLogs, new AtomicInteger(), CuratorQueryMethod.GET_CHILDREN);
}
use of org.apache.curator.framework.api.CuratorEvent in project Singularity by HubSpot.
the class CuratorAsyncManager method getAsyncNestedChildrenAsListThrows.
protected <T> List<T> getAsyncNestedChildrenAsListThrows(final String pathNameForLogs, final List<String> parentPaths, final Transcoder<T> transcoder) throws Exception {
final List<String> allPaths = new ArrayList<>();
for (String parent : parentPaths) {
for (String child : getChildren(parent)) {
allPaths.add(ZKPaths.makePath(parent, child));
}
}
final List<T> results = new ArrayList<>();
final CountDownLatch latch = new CountDownLatch(allPaths.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());
results.add(object);
} finally {
latch.countDown();
}
}
};
return queryAndReturnResultsThrows(results, allPaths, callback, latch, pathNameForLogs, bytes, CuratorQueryMethod.GET_DATA);
}
use of org.apache.curator.framework.api.CuratorEvent in project Singularity by HubSpot.
the class CuratorAsyncManager method notExistsThrows.
private <T extends SingularityId> List<T> notExistsThrows(final String pathNameforLogs, final Map<String, T> pathsMap) throws Exception {
if (pathsMap.isEmpty()) {
return Collections.emptyList();
}
final List<T> objects = Lists.newArrayListWithCapacity(pathsMap.size());
final CountDownLatch latch = new CountDownLatch(pathsMap.size());
final BackgroundCallback callback = new BackgroundCallback() {
@Override
public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
try {
if (event.getStat() == null) {
objects.add(pathsMap.get(event.getPath()));
}
} finally {
latch.countDown();
}
}
};
return queryAndReturnResultsThrows(objects, pathsMap.keySet(), callback, latch, pathNameforLogs, new AtomicInteger(), CuratorQueryMethod.CHECK_EXISTS);
}
use of org.apache.curator.framework.api.CuratorEvent in project Singularity by HubSpot.
the class CuratorAsyncManager method getAsyncNestedChildIdsAsListThrows.
protected <T extends SingularityId> List<T> getAsyncNestedChildIdsAsListThrows(final String pathNameForLogs, final String parentPath, final IdTranscoder<T> transcoder) throws Exception {
final List<String> allPaths = new ArrayList<>();
for (String child : getChildren(parentPath)) {
allPaths.add(ZKPaths.makePath(parentPath, child));
}
final List<T> results = new ArrayList<>();
final CountDownLatch latch = new CountDownLatch(allPaths.size());
final AtomicInteger bytes = new AtomicInteger();
final BackgroundCallback callback = new BackgroundCallback() {
@Override
public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
try {
event.getChildren().forEach(child -> {
final T object = transcoder.fromString(child);
bytes.getAndAdd(child.getBytes().length);
results.add(object);
});
} finally {
latch.countDown();
}
}
};
return queryAndReturnResultsThrows(results, allPaths, callback, latch, pathNameForLogs, bytes, CuratorQueryMethod.GET_CHILDREN);
}
Aggregations