use of org.apache.curator.framework.api.BackgroundCallback 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.BackgroundCallback 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.BackgroundCallback in project Singularity by HubSpot.
the class CuratorAsyncManager method existsThrows.
private <T extends SingularityId> List<T> existsThrows(final String pathNameforLogs, final Collection<String> paths, final IdTranscoder<T> idTranscoder) throws Exception {
if (paths.isEmpty()) {
return Collections.emptyList();
}
final List<T> objects = Lists.newArrayListWithCapacity(paths.size());
final CountDownLatch latch = new CountDownLatch(paths.size());
final BackgroundCallback callback = new BackgroundCallback() {
@Override
public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
try {
if (event.getStat() != null) {
objects.add(Transcoders.getFromStringFunction(idTranscoder).apply(ZKPaths.getNodeFromPath(event.getPath())));
}
} finally {
latch.countDown();
}
}
};
return queryAndReturnResultsThrows(objects, paths, callback, latch, pathNameforLogs, new AtomicInteger(), CuratorQueryMethod.GET_DATA);
}
use of org.apache.curator.framework.api.BackgroundCallback 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.BackgroundCallback in project xian by happyyangyuan.
the class TestFramework method testNamespaceInBackground.
@Test
public void testNamespaceInBackground() throws Exception {
CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder();
CuratorFramework client = builder.connectString(server.getConnectString()).namespace("aisa").retryPolicy(new RetryOneTime(1)).build();
client.start();
try {
final BlockingQueue<String> queue = new LinkedBlockingQueue<String>();
CuratorListener listener = new CuratorListener() {
@Override
public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
if (event.getType() == CuratorEventType.EXISTS) {
queue.put(event.getPath());
}
}
};
client.getCuratorListenable().addListener(listener);
client.create().forPath("/base");
client.checkExists().inBackground().forPath("/base");
String path = queue.poll(10, TimeUnit.SECONDS);
Assert.assertEquals(path, "/base");
client.getCuratorListenable().removeListener(listener);
BackgroundCallback callback = new BackgroundCallback() {
@Override
public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
queue.put(event.getPath());
}
};
client.getChildren().inBackground(callback).forPath("/base");
path = queue.poll(10, TimeUnit.SECONDS);
Assert.assertEquals(path, "/base");
} finally {
CloseableUtils.closeQuietly(client);
}
}
Aggregations