Search in sources :

Example 6 with BackgroundCallback

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);
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BackgroundCallback(org.apache.curator.framework.api.BackgroundCallback) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 7 with BackgroundCallback

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);
}
Also used : BackgroundCallback(org.apache.curator.framework.api.BackgroundCallback) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) CountDownLatch(java.util.concurrent.CountDownLatch) CuratorFramework(org.apache.curator.framework.CuratorFramework) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) List(java.util.List) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 8 with BackgroundCallback

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);
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BackgroundCallback(org.apache.curator.framework.api.BackgroundCallback) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 9 with BackgroundCallback

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);
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BackgroundCallback(org.apache.curator.framework.api.BackgroundCallback) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 10 with BackgroundCallback

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);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) CuratorFrameworkFactory(org.apache.curator.framework.CuratorFrameworkFactory) CuratorListener(org.apache.curator.framework.api.CuratorListener) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) BackgroundCallback(org.apache.curator.framework.api.BackgroundCallback) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Test(org.testng.annotations.Test)

Aggregations

BackgroundCallback (org.apache.curator.framework.api.BackgroundCallback)37 CuratorFramework (org.apache.curator.framework.CuratorFramework)33 CuratorEvent (org.apache.curator.framework.api.CuratorEvent)33 CountDownLatch (java.util.concurrent.CountDownLatch)18 Test (org.testng.annotations.Test)14 RetryOneTime (org.apache.curator.retry.RetryOneTime)11 KeeperException (org.apache.zookeeper.KeeperException)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 Watcher (org.apache.zookeeper.Watcher)6 Timing (org.apache.curator.test.Timing)4 Test (org.junit.Test)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 CompletableFuture (java.util.concurrent.CompletableFuture)3 CreateBuilder (org.apache.curator.framework.api.CreateBuilder)3 RetryNTimes (org.apache.curator.retry.RetryNTimes)3 WatchedEvent (org.apache.zookeeper.WatchedEvent)3 Message (com.google.protobuf.Message)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2