Search in sources :

Example 36 with CuratorEvent

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);
}
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 37 with CuratorEvent

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);
}
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 38 with CuratorEvent

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

Example 39 with CuratorEvent

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);
}
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 40 with CuratorEvent

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

Aggregations

CuratorEvent (org.apache.curator.framework.api.CuratorEvent)60 CuratorFramework (org.apache.curator.framework.CuratorFramework)57 BackgroundCallback (org.apache.curator.framework.api.BackgroundCallback)36 CountDownLatch (java.util.concurrent.CountDownLatch)34 CuratorListener (org.apache.curator.framework.api.CuratorListener)23 Test (org.testng.annotations.Test)21 RetryOneTime (org.apache.curator.retry.RetryOneTime)18 KeeperException (org.apache.zookeeper.KeeperException)14 Test (org.junit.Test)11 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 Watcher (org.apache.zookeeper.Watcher)8 ArrayList (java.util.ArrayList)4 Timing (org.apache.curator.test.Timing)4 WatchedEvent (org.apache.zookeeper.WatchedEvent)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 Message (com.google.protobuf.Message)3 IOException (java.io.IOException)3 Map (java.util.Map)3 ExecutionException (java.util.concurrent.ExecutionException)3 TimeUnit (java.util.concurrent.TimeUnit)3