Example usage for com.google.common.util.concurrent Futures allAsList

List of usage examples for com.google.common.util.concurrent Futures allAsList

Introduction

In this page you can find the example usage for com.google.common.util.concurrent Futures allAsList.

Prototype

@Beta
@CheckReturnValue
public static <V> ListenableFuture<List<V>> allAsList(
        Iterable<? extends ListenableFuture<? extends V>> futures) 

Source Link

Document

Creates a new ListenableFuture whose value is a list containing the values of all its input futures, if all succeed.

Usage

From source file:org.thingsboard.server.dao.timeseries.BaseTimeseriesDao.java

private ListenableFuture<List<TsKvEntry>> findAllAsync(String entityType, UUID entityId, TsKvQuery query) {
    if (query.getAggregation() == Aggregation.NONE) {
        return findAllAsyncWithLimit(entityType, entityId, query);
    } else {/*ww w.j  a v  a  2s . c o m*/
        long step = Math.max(query.getInterval(), minAggregationStepMs);
        long stepTs = query.getStartTs();
        List<ListenableFuture<Optional<TsKvEntry>>> futures = new ArrayList<>();
        while (stepTs < query.getEndTs()) {
            long startTs = stepTs;
            long endTs = stepTs + step;
            TsKvQuery subQuery = new BaseTsKvQuery(query.getKey(), startTs, endTs, step, 1,
                    query.getAggregation());
            futures.add(findAndAggregateAsync(entityType, entityId, subQuery, toPartitionTs(startTs),
                    toPartitionTs(endTs)));
            stepTs = endTs;
        }
        ListenableFuture<List<Optional<TsKvEntry>>> future = Futures.allAsList(futures);
        return Futures.transform(future, new Function<List<Optional<TsKvEntry>>, List<TsKvEntry>>() {
            @Nullable
            @Override
            public List<TsKvEntry> apply(@Nullable List<Optional<TsKvEntry>> input) {
                return input.stream().filter(v -> v.isPresent()).map(v -> v.get()).collect(Collectors.toList());
            }
        }, readResultsProcessingExecutor);
    }
}

From source file:de.softwareforge.kafka.LoadCommand.java

@Override
public void execute() throws Exception {
    Logging logging = Logging.initialize();
    logging.configure(new LoggingConfiguration());
    new LoggingMBean().setLevel("kafka", "ERROR");

    String tableNames = loaderOptions.tables;
    final Map<String, TpchTable<?>> allTables = ImmutableMap
            .copyOf(Maps.uniqueIndex(TpchTable.getTables(), new Function<TpchTable<?>, String>() {
                @Override//w ww. j  ava2  s  . c  o  m
                public String apply(@Nonnull TpchTable<?> input) {
                    return input.getTableName();
                }
            }));

    List<String> tables;
    if (tableNames == null) {
        tables = ImmutableList.copyOf(allTables.keySet());
    } else {
        ImmutableList.Builder<String> builder = ImmutableList.builder();
        for (String tableName : Splitter.on(",").omitEmptyStrings().trimResults().split(tableNames)) {
            checkState(allTables.keySet().contains(tableName), "Table %s is unknown", tableName);
            builder.add(tableName);
        }
        tables = builder.build();
    }

    LOG.info("Processing tables: %s", tables);

    Properties props = new Properties();
    props.put("metadata.broker.list", loaderOptions.brokers);
    props.put("serializer.class", StringEncoder.class.getName());
    props.put("key.serializer.class", LongEncoder.class.getName());
    props.put("partitioner.class", LongPartitioner.class.getName());
    props.put("serializer.encoding", "UTF8");
    props.put("request.required.acks", "1");
    ProducerConfig producerConfig = new ProducerConfig(props);

    final ObjectMapper mapper = objectMapperProvider.get();
    mapper.enable(MapperFeature.AUTO_DETECT_GETTERS);

    final Producer<Long, String> producer = new Producer<>(producerConfig);

    ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());

    ImmutableList.Builder<ListenableFuture<Long>> futureBuilder = ImmutableList.builder();

    for (final String table : tables) {
        ListenableFuture<Long> future = executor.submit(new Callable<Long>() {
            @Override
            public Long call() throws Exception {
                TpchTable<?> tpchTable = allTables.get(table);
                LOG.info("Loading table '%s' into topic '%s%s'...", table, loaderOptions.prefix, table);
                long count = 0;

                for (List<? extends TpchEntity> partition : Iterables.partition(
                        tpchTable.createGenerator(loaderOptions.tpchType.getScaleFactor(), 1, 1), 100)) {
                    ImmutableList.Builder<KeyedMessage<Long, String>> builder = ImmutableList.builder();
                    for (TpchEntity o : partition) {
                        builder.add(new KeyedMessage<>(loaderOptions.prefix + table, count++,
                                mapper.writeValueAsString(o)));
                    }
                    producer.send(builder.build());
                }
                LOG.info("Generated %d rows for table '%s'.", count, table);
                return count;
            }
        });
        futureBuilder.add(future);
    }

    Futures.allAsList(futureBuilder.build()).get();
    executor.shutdown();
    executor.awaitTermination(1, TimeUnit.DAYS);
    producer.close();
}

From source file:org.thingsboard.server.dao.attributes.BaseAttributesDao.java

@Override
public ListenableFuture<List<ResultSet>> removeAll(EntityId entityId, String attributeType, List<String> keys) {
    List<ResultSetFuture> futures = keys.stream().map(key -> delete(entityId, attributeType, key))
            .collect(Collectors.toList());
    return Futures.allAsList(futures);
}

From source file:zipkin.storage.cassandra.CassandraSpanConsumer.java

/**
 * This fans out into many requests, last count was 8 * spans.size. If any of these fail, the
 * returned future will fail. Most callers drop or log the result.
 *//* w  w  w  .  jav  a 2 s  . c om*/
@Override
public ListenableFuture<Void> accept(List<Span> rawSpans) {
    ImmutableSet.Builder<ListenableFuture<?>> futures = ImmutableSet.builder();

    ImmutableList.Builder<Span> spans = ImmutableList.builder();
    for (Span span : rawSpans) {
        // indexing occurs by timestamp, so derive one if not present.
        Long timestamp = guessTimestamp(span);
        spans.add(span);

        futures.add(storeSpan(span.traceId, timestamp != null ? timestamp : 0L,
                String.format("%s%d_%d_%d", span.traceIdHigh == 0 ? "" : span.traceIdHigh + "_", span.id,
                        span.annotations.hashCode(), span.binaryAnnotations.hashCode()),
                // store the raw span without any adjustments
                ByteBuffer.wrap(Codec.THRIFT.writeSpan(span))));

        for (String serviceName : span.serviceNames()) {
            // SpanStore.getServiceNames
            futures.add(storeServiceName(serviceName));
            if (!span.name.isEmpty()) {
                // SpanStore.getSpanNames
                futures.add(storeSpanName(serviceName, span.name));
            }
        }
    }
    futures.addAll(indexer.index(spans.build()));
    return transform(Futures.allAsList(futures.build()), TO_VOID);
}

From source file:zipkin.storage.cassandra3.CassandraSpanConsumer.java

/**
 * This fans out into many requests, last count was 2 * spans.size. If any of these fail, the
 * returned future will fail. Most callers drop or log the result.
 *//*  w w w .j ava  2  s.  c o m*/
@Override
public ListenableFuture<Void> accept(List<Span> rawSpans) {
    ImmutableSet.Builder<ListenableFuture<?>> futures = ImmutableSet.builder();

    for (Span span : rawSpans) {
        // indexing occurs by timestamp, so derive one if not present.
        Long timestamp = guessTimestamp(span);
        TraceIdUDT traceId = new TraceIdUDT(span.traceIdHigh, span.traceId);
        futures.add(storeSpan(span, traceId, timestamp));

        for (String serviceName : span.serviceNames()) {
            // QueryRequest.min/maxDuration
            if (timestamp != null) {
                // Contract for Repository.storeTraceServiceSpanName is to store the span twice, once with
                // the span name and another with empty string.
                futures.add(
                        storeTraceServiceSpanName(serviceName, span.name, timestamp, span.duration, traceId));
                if (!span.name.isEmpty()) { // If span.name == "", this would be redundant
                    futures.add(storeTraceServiceSpanName(serviceName, "", timestamp, span.duration, traceId));
                }
                futures.add(storeServiceSpanName(serviceName, span.name));
            }
        }
    }
    return transform(Futures.allAsList(futures.build()), TO_VOID);
}

From source file:io.crate.operation.collect.collectors.MultiShardScoreDocCollector.java

private void runThreaded() {
    List<ListenableFuture<KeyIterable<ShardId, Row>>> futures = new ArrayList<>(orderedDocCollectors.size());
    for (OrderedDocCollector orderedDocCollector : orderedDocCollectors.subList(1,
            this.orderedDocCollectors.size())) {
        try {/*ww w  . j a  va2  s.  c  om*/
            futures.add(executor.submit(orderedDocCollector));
        } catch (EsRejectedExecutionException | RejectedExecutionException e) {
            futures.add(directExecutor.submit(orderedDocCollector));
        }
    }
    // since the whole MultiShardScoreDocCollector already runs in the search threadPool we can
    // run one orderedCollector in the same thread to avoid some context switching
    futures.add(directExecutor.submit(orderedDocCollectors.get(0)));
    Futures.addCallback(Futures.allAsList(futures), futureCallback);
}

From source file:com.continuuity.loom.common.zookeeper.lib.ZKCollection.java

public void clear() {
    currentView.set(Collections.<T>emptyList());
    // Hint: again, we can try to make removal more efficient by cleaning only when in-mem collection cleaned smth,
    //       but then we may face races...
    NodeChildren nodeChildren = Futures.getUnchecked(zkClient.getChildren(""));
    List<ListenableFuture<String>> deleteFutures = Lists.newArrayList();
    for (String node : nodeChildren.getChildren()) {
        deleteFutures.add(ZKClientExt.delete(zkClient, getNodePath(node), true));
    }//w  w w  .j  a  va2  s .c  o  m
    Futures.getUnchecked(Futures.allAsList(deleteFutures));
}

From source file:org.opendaylight.openflowplugin.impl.OpenFlowPluginProviderImpl.java

private void startSwitchConnections() {
    final List<ListenableFuture<Boolean>> starterChain = new ArrayList<>(switchConnectionProviders.size());
    for (final SwitchConnectionProvider switchConnectionPrv : switchConnectionProviders) {
        switchConnectionPrv.setSwitchConnectionHandler(connectionManager);
        final ListenableFuture<Boolean> isOnlineFuture = switchConnectionPrv.startup();
        starterChain.add(isOnlineFuture);
    }//  www  .j av  a  2 s . c  o m

    final ListenableFuture<List<Boolean>> srvStarted = Futures.allAsList(starterChain);
    Futures.addCallback(srvStarted, new FutureCallback<List<Boolean>>() {
        @Override
        public void onSuccess(final List<Boolean> result) {
            LOG.info("All switchConnectionProviders are up and running ({}).", result.size());
        }

        @Override
        public void onFailure(final Throwable t) {
            LOG.warn("Some switchConnectionProviders failed to start.", t);
        }
    });
}

From source file:org.opendaylight.mdsal.dom.broker.ShardedDOMDataTreeWriteTransaction.java

void doSubmit(final Consumer<ShardedDOMDataTreeWriteTransaction> success,
        final BiConsumer<ShardedDOMDataTreeWriteTransaction, Throwable> failure) {

    final ListenableFuture<List<Void>> listListenableFuture = Futures
            .allAsList(transactions.values().stream().map(tx -> {
                LOG.debug("Readying tx {}", identifier);
                tx.ready();/*from  w  ww  .  ja  v  a 2s  .  c  om*/
                return tx.submit();
            }).collect(Collectors.toList()));

    Futures.addCallback(listListenableFuture, new FutureCallback<List<Void>>() {
        @Override
        public void onSuccess(final List<Void> result) {
            success.accept(ShardedDOMDataTreeWriteTransaction.this);
        }

        @Override
        public void onFailure(final Throwable exp) {
            failure.accept(ShardedDOMDataTreeWriteTransaction.this, exp);
        }
    });
}

From source file:io.crate.operation.projectors.UpdateProjector.java

private void collectUpdateResultsAndPassOverRowCount() {
    Futures.addCallback(Futures.allAsList(updateResults), new FutureCallback<List<Long>>() {
        @Override/*from w w w.ja  v  a2s .  c o  m*/
        public void onSuccess(@Nullable List<Long> result) {
            long rowCount = 0;
            for (Long val : result) {
                rowCount += val;
            }
            downstream.setNextRow(rowCount);
            Throwable throwable = upstreamFailure.get();
            if (throwable == null) {
                downstream.upstreamFinished();
            } else {
                downstream.upstreamFailed(throwable);
            }
        }

        @Override
        public void onFailure(Throwable t) {
            downstream.setNextRow(0L);
            downstream.upstreamFailed(t);
        }
    });
}