List of usage examples for com.google.common.util.concurrent Futures allAsList
@Beta @CheckReturnValue public static <V> ListenableFuture<List<V>> allAsList( Iterable<? extends ListenableFuture<? extends V>> futures)
From source file:com.spectralogic.ds3client.helpers.ChunkTransferrer.java
private static void executeWithExceptionHandling(final List<ListenableFuture<?>> tasks) throws IOException, XmlProcessingException { try {//from w w w . j a va 2s . co m // Block on the asynchronous result. Futures.allAsList(tasks).get(); } catch (final InterruptedException e) { // This is a checked exception, but we don't want to expose that this is implemented with futures. throw new RuntimeException(e); } catch (final ExecutionException e) { // The future throws a wrapper exception, but we want don't want to expose that this was implemented with futures. final Throwable cause = e.getCause(); // Throw each of the advertised thrown exceptions. if (cause instanceof IOException) { throw (IOException) cause; } else if (cause instanceof XmlProcessingException) { throw (XmlProcessingException) cause; } // The rest we don't know about, so we'll just forward them. if (cause instanceof RuntimeException) { throw (RuntimeException) cause; } else { throw new RuntimeException(cause); } } }
From source file:org.apache.druid.query.GroupByMergedQueryRunner.java
@Override public Sequence<T> run(final QueryPlus<T> queryPlus, final Map<String, Object> responseContext) { final GroupByQuery query = (GroupByQuery) queryPlus.getQuery(); final GroupByQueryConfig querySpecificConfig = configSupplier.get().withOverrides(query); final boolean isSingleThreaded = querySpecificConfig.isSingleThreaded(); final Pair<IncrementalIndex, Accumulator<IncrementalIndex, T>> indexAccumulatorPair = GroupByQueryHelper .createIndexAccumulatorPair(query, querySpecificConfig, bufferPool, true); final Pair<Queue, Accumulator<Queue, T>> bySegmentAccumulatorPair = GroupByQueryHelper .createBySegmentAccumulatorPair(); final boolean bySegment = QueryContexts.isBySegment(query); final int priority = QueryContexts.getPriority(query); final QueryPlus<T> threadSafeQueryPlus = queryPlus.withoutThreadUnsafeState(); final ListenableFuture<List<Void>> futures = Futures.allAsList(Lists.newArrayList( Iterables.transform(queryables, new Function<QueryRunner<T>, ListenableFuture<Void>>() { @Override/*from w w w . j a v a2 s . c o m*/ public ListenableFuture<Void> apply(final QueryRunner<T> input) { if (input == null) { throw new ISE("Null queryRunner! Looks to be some segment unmapping action happening"); } ListenableFuture<Void> future = exec .submit(new AbstractPrioritizedCallable<Void>(priority) { @Override public Void call() { try { if (bySegment) { input.run(threadSafeQueryPlus, responseContext).accumulate( bySegmentAccumulatorPair.lhs, bySegmentAccumulatorPair.rhs); } else { input.run(threadSafeQueryPlus, responseContext).accumulate( indexAccumulatorPair.lhs, indexAccumulatorPair.rhs); } return null; } catch (QueryInterruptedException e) { throw Throwables.propagate(e); } catch (Exception e) { log.error(e, "Exception with one of the sequences!"); throw Throwables.propagate(e); } } }); if (isSingleThreaded) { waitForFutureCompletion(query, future, indexAccumulatorPair.lhs); } return future; } }))); if (!isSingleThreaded) { waitForFutureCompletion(query, futures, indexAccumulatorPair.lhs); } if (bySegment) { return Sequences.simple(bySegmentAccumulatorPair.lhs); } return Sequences.withBaggage(Sequences.simple(Iterables.transform( indexAccumulatorPair.lhs.iterableWithPostAggregations(null, query.isDescending()), new Function<Row, T>() { @Override public T apply(Row input) { return (T) input; } })), indexAccumulatorPair.lhs); }
From source file:co.cask.tigon.internal.app.runtime.distributed.AbstractServiceTwillRunnable.java
@Override public void run() { runThread = Thread.currentThread(); LOG.info("Starting runnable {}", name); List<ListenableFuture<Service.State>> completions = Lists.newArrayList(); for (Service service : services) { SettableFuture<Service.State> completion = SettableFuture.create(); service.addListener(createServiceListener(completion), Threads.SAME_THREAD_EXECUTOR); completions.add(completion);/* w w w .j a v a2 s. c om*/ } Services.chainStart(services.get(0), services.subList(1, services.size()).toArray(new Service[0])); LOG.info("Runnable started {}", name); try { Futures.allAsList(completions).get(); } catch (InterruptedException e) { LOG.debug("Waiting on latch interrupted {}", name); Thread.currentThread().interrupt(); } catch (ExecutionException e) { LOG.error("Exception in service.", e); throw Throwables.propagate(e); } List<Service> reverse = Lists.reverse(services); Services.chainStop(reverse.get(0), reverse.subList(1, reverse.size()).toArray(new Service[0])); LOG.info("Runnable stopped {}", name); }
From source file:com.android.camera.async.Futures2.java
/** * Create a new joined future from three existing futures and a joining function * that combines the resulting outputs of the previous functions into a single * result. The resulting future will fail if any of the dependent futures also * fail.//from w w w. java 2 s . com */ public static <T1, T2, T3, TResult> ListenableFuture<TResult> joinAll(final ListenableFuture<T1> f1, final ListenableFuture<T2> f2, final ListenableFuture<T3> f3, final AsyncFunction3<T1, T2, T3, TResult> fn) { ListenableFuture<?>[] futures = new ListenableFuture<?>[3]; futures[0] = f1; futures[1] = f2; futures[2] = f3; // Futures.allAsList is used instead of Futures.successfulAsList because // allAsList will propagate the failures instead of null values to the // parameters of the supplied function. ListenableFuture<List<Object>> result = Futures.allAsList(futures); return Futures.transform(result, new AsyncFunction<List<Object>, TResult>() { @Override public ListenableFuture<TResult> apply(@Nullable List<Object> list) throws Exception { T1 value1 = (T1) list.get(0); T2 value2 = (T2) list.get(1); T3 value3 = (T3) list.get(2); return fn.apply(value1, value2, value3); } }); }
From source file:zipkin.cassandra.CassandraSpanConsumer.java
@Override public ListenableFuture<Void> accept(List<Span> spans) { List<ListenableFuture<?>> futures = new LinkedList<>(); for (Span span : spans) { span = ApplyTimestampAndDuration.apply(span); futures.add(repository.storeSpan(span.traceId, span.timestamp != null ? span.timestamp : 0L, String.format("%d_%d_%d", span.id, span.annotations.hashCode(), span.binaryAnnotations.hashCode()), ByteBuffer.wrap(Codec.THRIFT.writeSpan(span)), spanTtl)); for (String serviceName : span.serviceNames()) { // SpanStore.getServiceNames futures.add(repository.storeServiceName(serviceName, indexTtl)); if (!span.name.isEmpty()) { // SpanStore.getSpanNames futures.add(repository.storeSpanName(serviceName, span.name, indexTtl)); }/* w w w .j a v a 2 s . co m*/ if (span.timestamp != null) { // QueryRequest.serviceName futures.add(repository.storeTraceIdByServiceName(serviceName, span.timestamp, span.traceId, indexTtl)); // QueryRequest.spanName if (!span.name.isEmpty()) { futures.add(repository.storeTraceIdBySpanName(serviceName, span.name, span.timestamp, span.traceId, indexTtl)); } // QueryRequest.min/maxDuration if (span.duration != null) { // Contract for Repository.storeTraceIdByDuration is to store the span twice, once with // the span name and another with empty string. futures.add(repository.storeTraceIdByDuration(serviceName, span.name, span.timestamp, span.duration, span.traceId, indexTtl)); if (!span.name.isEmpty()) { // If span.name == "", this would be redundant repository.storeTraceIdByDuration(serviceName, "", span.timestamp, span.duration, span.traceId, indexTtl); } } } } // QueryRequest.annotations/binaryAnnotations if (span.timestamp != null) { for (ByteBuffer annotation : annotationKeys(span)) { futures.add(repository.storeTraceIdByAnnotation(annotation, span.timestamp, span.traceId, indexTtl)); } } } return transform(Futures.allAsList(futures), TO_VOID); }
From source file:org.apache.druid.query.ChainedExecutionQueryRunner.java
@Override public Sequence<T> run(final QueryPlus<T> queryPlus, final Map<String, Object> responseContext) { Query<T> query = queryPlus.getQuery(); final int priority = QueryContexts.getPriority(query); final Ordering ordering = query.getResultOrdering(); final QueryPlus<T> threadSafeQueryPlus = queryPlus.withoutThreadUnsafeState(); return new BaseSequence<T, Iterator<T>>(new BaseSequence.IteratorMaker<T, Iterator<T>>() { @Override/*from w w w . jav a 2s .com*/ public Iterator<T> make() { // Make it a List<> to materialize all of the values (so that it will submit everything to the executor) ListenableFuture<List<Iterable<T>>> futures = Futures .allAsList(Lists.newArrayList(Iterables.transform(queryables, input -> { if (input == null) { throw new ISE( "Null queryRunner! Looks to be some segment unmapping action happening"); } return exec.submit(new AbstractPrioritizedCallable<Iterable<T>>(priority) { @Override public Iterable<T> call() { try { Sequence<T> result = input.run(threadSafeQueryPlus, responseContext); if (result == null) { throw new ISE("Got a null result! Segments are missing!"); } List<T> retVal = result.toList(); if (retVal == null) { throw new ISE("Got a null list of results! WTF?!"); } return retVal; } catch (QueryInterruptedException e) { throw Throwables.propagate(e); } catch (Exception e) { log.error(e, "Exception with one of the sequences!"); throw Throwables.propagate(e); } } }); }))); queryWatcher.registerQuery(query, futures); try { return new MergeIterable<>(ordering.nullsFirst(), QueryContexts.hasTimeout(query) ? futures.get(QueryContexts.getTimeout(query), TimeUnit.MILLISECONDS) : futures.get()).iterator(); } catch (InterruptedException e) { log.warn(e, "Query interrupted, cancelling pending results, query id [%s]", query.getId()); futures.cancel(true); throw new QueryInterruptedException(e); } catch (CancellationException e) { throw new QueryInterruptedException(e); } catch (TimeoutException e) { log.info("Query timeout, cancelling pending results for query id [%s]", query.getId()); futures.cancel(true); throw new QueryInterruptedException(e); } catch (ExecutionException e) { throw Throwables.propagate(e.getCause()); } } @Override public void cleanup(Iterator<T> tIterator) { } }); }
From source file:com.dangdang.ddframe.rdb.sharding.executor.ExecutorEngine.java
private <I, O> ListenableFuture<List<O>> submitFutures(final Collection<I> inputs, final ExecuteUnit<I, O> executeUnit) { Set<ListenableFuture<O>> result = new HashSet<>(inputs.size()); for (final I each : inputs) { result.add(executorService.submit(new Callable<O>() { @Override// w ww. j a v a2 s .c o m public O call() throws Exception { return executeUnit.execute(each); } })); } return Futures.allAsList(result); }
From source file:io.crate.operation.ThreadPools.java
/** * Similar to {@link #runWithAvailableThreads(ThreadPoolExecutor, Collection)} * but this function will return a Future that wraps the futures of each callable * * @param executor executor that is used to execute the callableList * @param poolSize the corePoolSize of the given executor * @param callableCollection a collection of callable that should be executed * @param mergeFunction function that will be applied to merge the results of multiple callable in case that they are * executed together if the threadPool is exhausted * @param <T> type of the final result * @return a future that will return a list of the results of the callableList * @throws RejectedExecutionException/* w w w . j av a 2 s. c om*/ */ public static <T> ListenableFuture<List<T>> runWithAvailableThreads(ThreadPoolExecutor executor, int poolSize, Collection<Callable<T>> callableCollection, final Function<List<T>, T> mergeFunction) throws RejectedExecutionException { ListeningExecutorService listeningExecutorService = MoreExecutors.listeningDecorator(executor); List<ListenableFuture<T>> futures; int availableThreads = Math.max(poolSize - executor.getActiveCount(), 1); if (availableThreads < callableCollection.size()) { Iterable<List<Callable<T>>> partition = Iterables.partition(callableCollection, callableCollection.size() / availableThreads); futures = new ArrayList<>(availableThreads + 1); for (final List<Callable<T>> callableList : partition) { futures.add(listeningExecutorService.submit(new Callable<T>() { @Override public T call() throws Exception { List<T> results = new ArrayList<T>(callableList.size()); for (Callable<T> tCallable : callableList) { results.add(tCallable.call()); } return mergeFunction.apply(results); } })); } } else { futures = new ArrayList<>(callableCollection.size()); for (Callable<T> callable : callableCollection) { futures.add(listeningExecutorService.submit(callable)); } } return Futures.allAsList(futures); }
From source file:com.skcraft.launcher.install.HttpDownloader.java
/** * Prevent further downloads from being queued and download queued files. * * @throws InterruptedException thrown on interruption * @throws IOException thrown on I/O error *///from ww w .j av a 2s. c om public void execute() throws InterruptedException, IOException { synchronized (this) { queue = Collections.unmodifiableList(queue); } ListeningExecutorService executor = MoreExecutors .listeningDecorator(Executors.newFixedThreadPool(threadCount)); try { List<ListenableFuture<?>> futures = new ArrayList<ListenableFuture<?>>(); synchronized (this) { for (HttpDownloadJob job : queue) { futures.add(executor.submit(job)); } } try { Futures.allAsList(futures).get(); } catch (ExecutionException e) { throw new IOException("Something went wrong", e); } synchronized (this) { if (failed.size() > 0) { throw new IOException(failed.size() + " file(s) could not be downloaded"); } } } finally { executor.shutdownNow(); } }
From source file:org.stem.client.ClusterDescriber.java
private void refreshNodeList(StemCluster.Manager cluster, REST.TopologySnapshot state) { List<InetSocketAddress> foundHosts = new ArrayList<>(); REST.Topology topology = state.getTopology(); for (REST.StorageNode nodeInfo : topology.nodes()) { InetSocketAddress addr = nodeInfo.getSocketAddress(); foundHosts.add(addr);//w w w. j ava 2 s . com } List<ListenableFuture<?>> futures = new ArrayList<>(foundHosts.size()); for (InetSocketAddress addr : foundHosts) { Host host = cluster.metadata.getHost(addr); boolean isNew = false; if (null == host) { host = cluster.metadata.add(addr); isNew = true; } if (isNew) futures.add(cluster.triggerOnAdd(host)); } try { ListenableFuture<List<Object>> f = Futures.allAsList(futures); Uninterruptibles.getUninterruptibly(f); } catch (ExecutionException e) { logger.error("Some error while handling addition of new nodes. We continue anyway"); } cluster.metadata.updateRouting(state.getTopology(), state.getMapping()); // TODO: merge both parameters into single instance (REST.TopologySnapshot) }