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.opendaylight.openflowplugin.applications.frsync.impl.strategy.SyncPlanPushStrategyIncrementalImpl.java

ListenableFuture<RpcResult<Void>> removeRedundantFlows(final NodeId nodeId,
        final InstanceIdentifier<FlowCapableNode> nodeIdent, final Map<TableKey, ItemSyncBox<Flow>> removalPlan,
        final SyncCrudCounters counters) {
    if (removalPlan.isEmpty()) {
        LOG.trace("no tables in operational for node: {} -> SKIPPING", nodeId.getValue());
        return RpcResultBuilder.<Void>success().buildFuture();
    }/*from w w w  .j  av a 2s  .  co  m*/

    final List<ListenableFuture<RpcResult<RemoveFlowOutput>>> allResults = new ArrayList<>();
    final CrudCounts flowCrudCounts = counters.getFlowCrudCounts();

    for (final Map.Entry<TableKey, ItemSyncBox<Flow>> flowsPerTable : removalPlan.entrySet()) {
        final KeyedInstanceIdentifier<Table, TableKey> tableIdent = nodeIdent.child(Table.class,
                flowsPerTable.getKey());

        // loop flows on device and check if the are configured
        for (final Flow flow : flowsPerTable.getValue().getItemsToPush()) {
            final KeyedInstanceIdentifier<Flow, FlowKey> flowIdent = tableIdent.child(Flow.class,
                    flow.getKey());
            allResults.add(
                    JdkFutureAdapters.listenInPoolThread(flowForwarder.remove(flowIdent, flow, nodeIdent)));
            flowCrudCounts.incRemoved();
        }
    }

    final ListenableFuture<RpcResult<Void>> singleVoidResult = Futures.transform(Futures.allAsList(allResults),
            ReconcileUtil.<RemoveFlowOutput>createRpcResultCondenser("flow remove"));
    return Futures.transform(singleVoidResult,
            ReconcileUtil.chainBarrierFlush(PathUtil.digNodePath(nodeIdent), transactionService));

}

From source file:com.netflix.metacat.main.services.impl.PartitionServiceImpl.java

@Override
public Map<String, List<QualifiedName>> getQualifiedNames(final List<String> uris, final boolean prefixSearch) {
    final Map<String, List<QualifiedName>> result = Maps.newConcurrentMap();
    final List<ListenableFuture<Void>> futures = Lists.newArrayList();
    catalogService.getCatalogNames().forEach(catalog -> {
        final Session session = sessionProvider.getSession(QualifiedName.ofCatalog(catalog.getCatalogName()));
        futures.add(threadServiceManager.getExecutor().submit(() -> {
            final Map<String, List<SchemaTablePartitionName>> schemaTablePartitionNames = splitManager
                    .getPartitionNames(session, uris, prefixSearch);
            schemaTablePartitionNames.forEach((uri, schemaTablePartitionNames1) -> {
                final List<QualifiedName> partitionNames = schemaTablePartitionNames1.stream()
                        .map(schemaTablePartitionName -> QualifiedName.ofPartition(catalog.getConnectorName(),
                                schemaTablePartitionName.getTableName().getSchemaName(),
                                schemaTablePartitionName.getTableName().getTableName(),
                                schemaTablePartitionName.getPartitionId()))
                        .collect(Collectors.toList());
                final List<QualifiedName> existingPartitionNames = result.get(uri);
                if (existingPartitionNames == null) {
                    result.put(uri, partitionNames);
                } else {
                    existingPartitionNames.addAll(partitionNames);
                }//  w ww .  j  a  va 2  s. c  o m
            });
            return null;
        }));
    });
    try {
        Futures.allAsList(futures).get(1, TimeUnit.HOURS);
    } catch (Exception e) {
        Throwables.propagate(e);
    }
    return result;
}

From source file:com.google.cloud.dataflow.sdk.io.FileBasedSource.java

@Override
public final List<? extends FileBasedSource<T>> splitIntoBundles(long desiredBundleSizeBytes,
        PipelineOptions options) throws Exception {
    // This implementation of method splitIntoBundles is provided to simplify subclasses. Here we
    // split a FileBasedSource based on a file pattern to FileBasedSources based on full single
    // files. For files that can be efficiently seeked, we further split FileBasedSources based on
    // those files to FileBasedSources based on sub ranges of single files.

    if (mode == Mode.FILEPATTERN) {
        long startTime = System.currentTimeMillis();
        List<ListenableFuture<List<? extends FileBasedSource<T>>>> futures = new ArrayList<>();

        ListeningExecutorService service = MoreExecutors
                .listeningDecorator(Executors.newFixedThreadPool(THREAD_POOL_SIZE));
        try {/* w w w .j  a  v a  2s. co  m*/
            for (final String file : FileBasedSource.expandFilePattern(fileOrPatternSpec)) {
                futures.add(createFutureForFileSplit(file, desiredBundleSizeBytes, options, service));
            }
            List<? extends FileBasedSource<T>> splitResults = ImmutableList
                    .copyOf(Iterables.concat(Futures.allAsList(futures).get()));
            LOG.debug("Splitting the source based on file pattern " + fileOrPatternSpec + " took "
                    + (System.currentTimeMillis() - startTime) + " ms");
            return splitResults;
        } finally {
            service.shutdown();
        }
    } else {
        if (isSplittable()) {
            List<FileBasedSource<T>> splitResults = new ArrayList<>();
            for (OffsetBasedSource<T> split : super.splitIntoBundles(desiredBundleSizeBytes, options)) {
                splitResults.add((FileBasedSource<T>) split);
            }
            return splitResults;
        } else {
            LOG.debug("The source for file " + fileOrPatternSpec
                    + " is not split into sub-range based sources since the file is not seekable");
            return ImmutableList.of(this);
        }
    }
}

From source file:com.yahoo.yqlplus.engine.internal.java.sequences.Sequences.java

public static <ROW, SEQUENCE extends Iterable<ROW>, SET> ListenableFuture<List<ROW>> invokeBatchSet(
        final ListeningExecutorService workExecutor, final Function<List<SET>, SEQUENCE> source,
        final Tracer tracer, final Timeout timeout, final TimeoutHandler handler, final List<ROW>... inputs)
        throws Exception {
    final List<ListenableFuture<SEQUENCE>> results = Lists.newArrayList();
    for (List<ROW> input : inputs) {
        for (int i = 0; i < input.size(); i++) {
            Record record = (Record) input.get(i);
            List methodArgs = Lists.newArrayList();
            Iterable<String> fieldNames = record.getFieldNames();
            for (String fieldName : fieldNames) {
                methodArgs.add(record.get(fieldName));
            }/*from   w w  w . j a v a2  s .  c om*/
            results.add(workExecutor.submit(createJob(tracer, source, methodArgs)));
        }
    }
    final ListenableFuture<List<SEQUENCE>> gather = Futures.allAsList(results);
    return handler.withTimeout(gatherResults(workExecutor, gather, 1), timeout.verify(),
            timeout.getTickUnits());
}

From source file:org.thingsboard.server.actors.device.DeviceActorMessageProcessor.java

private void handleGetAttributesRequest(ActorContext context, SessionInfoProto sessionInfo,
        GetAttributeRequestMsg request) {
    ListenableFuture<List<AttributeKvEntry>> clientAttributesFuture = getAttributeKvEntries(deviceId,
            DataConstants.CLIENT_SCOPE, toOptionalSet(request.getClientAttributeNamesList()));
    ListenableFuture<List<AttributeKvEntry>> sharedAttributesFuture = getAttributeKvEntries(deviceId,
            DataConstants.SHARED_SCOPE, toOptionalSet(request.getSharedAttributeNamesList()));
    int requestId = request.getRequestId();
    Futures.addCallback(Futures.allAsList(Arrays.asList(clientAttributesFuture, sharedAttributesFuture)),
            new FutureCallback<List<List<AttributeKvEntry>>>() {
                @Override/*w w  w  .j  a  va 2 s  . co  m*/
                public void onSuccess(@Nullable List<List<AttributeKvEntry>> result) {
                    GetAttributeResponseMsg responseMsg = GetAttributeResponseMsg.newBuilder()
                            .setRequestId(requestId).addAllClientAttributeList(toTsKvProtos(result.get(0)))
                            .addAllSharedAttributeList(toTsKvProtos(result.get(1))).build();
                    sendToTransport(responseMsg, sessionInfo);
                }

                @Override
                public void onFailure(Throwable t) {
                    GetAttributeResponseMsg responseMsg = GetAttributeResponseMsg.newBuilder()
                            .setError(t.getMessage()).build();
                    sendToTransport(responseMsg, sessionInfo);
                }
            });
}

From source file:org.apache.druid.server.coordinator.CostBalancerStrategy.java

/**
 * For assignment, we want to move to the lowest cost server that isn't already serving the segment.
 *
 * @param proposalSegment A DataSegment that we are proposing to move.
 * @param serverHolders   An iterable of ServerHolders for a particular tier.
 *
 * @return A ServerHolder with the new home for a segment.
 *///from   w  w  w  .  jav  a 2s . c  om

protected Pair<Double, ServerHolder> chooseBestServer(final DataSegment proposalSegment,
        final Iterable<ServerHolder> serverHolders, final boolean includeCurrentServer) {
    Pair<Double, ServerHolder> bestServer = Pair.of(Double.POSITIVE_INFINITY, null);

    List<ListenableFuture<Pair<Double, ServerHolder>>> futures = Lists.newArrayList();

    for (final ServerHolder server : serverHolders) {
        futures.add(
                exec.submit(() -> Pair.of(computeCost(proposalSegment, server, includeCurrentServer), server)));
    }

    final ListenableFuture<List<Pair<Double, ServerHolder>>> resultsFuture = Futures.allAsList(futures);
    final List<Pair<Double, ServerHolder>> bestServers = new ArrayList<>();
    bestServers.add(bestServer);
    try {
        for (Pair<Double, ServerHolder> server : resultsFuture.get()) {
            if (server.lhs <= bestServers.get(0).lhs) {
                if (server.lhs < bestServers.get(0).lhs) {
                    bestServers.clear();
                }
                bestServers.add(server);
            }
        }

        // Randomly choose a server from the best servers
        bestServer = bestServers.get(ThreadLocalRandom.current().nextInt(bestServers.size()));
    } catch (Exception e) {
        log.makeAlert(e, "Cost Balancer Multithread strategy wasn't able to complete cost computation.").emit();
    }
    return bestServer;
}

From source file:org.opendaylight.vbd.impl.VbdBridgeDomain.java

private ListenableFuture<Void> handleModifiedTopology(final DataObjectModification<Topology> modification) {
    Preconditions.checkNotNull(modification.getDataAfter());
    final DataObjectModification<TopologyVbridgeAugment> topologyModification = modification
            .getModifiedAugmentation(TopologyVbridgeAugment.class);
    // Handle VBridge augmentation
    if (topologyModification != null && !DataObjectModification.ModificationType.DELETE
            .equals(topologyModification.getModificationType())) {
        // Update configuration
        updateConfiguration(topologyModification);
    }//from w w  w .  j a v  a2 s  .  c  om
    // Handle new/modified nodes
    final Collection<DataObjectModification<? extends DataObject>> modifiedChildren = modification
            .getModifiedChildren();
    final List<ListenableFuture<Void>> updatedCumulativeTopologyTask = new ArrayList<>();
    for (final DataObjectModification<? extends DataObject> childNode : modifiedChildren) {
        LOG.debug("Processing modified child {} from topology {}", childNode, PPrint.topology(topology));
        if (Node.class.isAssignableFrom(childNode.getDataType())) {
            updatedCumulativeTopologyTask.add(handleModifiedNode(childNode));
        }
    }
    final ListenableFuture<List<Void>> updatedTopologyResult = Futures.allAsList(updatedCumulativeTopologyTask);
    return transform(updatedTopologyResult);
}

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

private AsyncFunction<List<Long>, List<ResultSet>> getFetchChunksAsyncFunction(TenantId tenantId,
        EntityId entityId, String key, Aggregation aggregation, long startTs, long endTs) {
    return partitions -> {
        try {// w  ww  . ja  v a  2  s  . c  o  m
            PreparedStatement proto = getFetchStmt(aggregation, DESC_ORDER);
            List<ResultSetFuture> futures = new ArrayList<>(partitions.size());
            for (Long partition : partitions) {
                log.trace("Fetching data for partition [{}] for entityType {} and entityId {}", partition,
                        entityId.getEntityType(), entityId.getId());
                BoundStatement stmt = proto.bind();
                stmt.setString(0, entityId.getEntityType().name());
                stmt.setUUID(1, entityId.getId());
                stmt.setString(2, key);
                stmt.setLong(3, partition);
                stmt.setLong(4, startTs);
                stmt.setLong(5, endTs);
                log.debug(GENERATED_QUERY_FOR_ENTITY_TYPE_AND_ENTITY_ID, stmt, entityId.getEntityType(),
                        entityId.getId());
                futures.add(executeAsyncRead(tenantId, stmt));
            }
            return Futures.allAsList(futures);
        } catch (Throwable e) {
            log.error("Failed to fetch data", e);
            throw e;
        }
    };
}

From source file:org.thingsboard.server.dao.audit.AuditLogServiceImpl.java

private ListenableFuture<List<Void>> logAction(TenantId tenantId, EntityId entityId, String entityName,
        CustomerId customerId, UserId userId, String userName, ActionType actionType, JsonNode actionData,
        ActionStatus actionStatus, String actionFailureDetails) {
    AuditLog auditLogEntry = createAuditLogEntry(tenantId, entityId, entityName, customerId, userId, userName,
            actionType, actionData, actionStatus, actionFailureDetails);
    log.trace("Executing logAction [{}]", auditLogEntry);
    auditLogValidator.validate(auditLogEntry, AuditLog::getTenantId);
    List<ListenableFuture<Void>> futures = Lists.newArrayListWithExpectedSize(INSERTS_PER_ENTRY);
    futures.add(auditLogDao.savePartitionsByTenantId(auditLogEntry));
    futures.add(auditLogDao.saveByTenantId(auditLogEntry));
    futures.add(auditLogDao.saveByTenantIdAndEntityId(auditLogEntry));
    futures.add(auditLogDao.saveByTenantIdAndCustomerId(auditLogEntry));
    futures.add(auditLogDao.saveByTenantIdAndUserId(auditLogEntry));

    auditLogSink.logAction(auditLogEntry);

    return Futures.allAsList(futures);
}

From source file:alluxio.underfs.s3a.S3ALowLevelOutputStream.java

/**
 * Waits for the submitted upload tasks to finish.
 *///from   www  . jav  a  2 s. c  o m
private void waitForAllPartsUpload() throws IOException {
    int beforeSize = mTags.size();
    try {
        for (ListenableFuture<PartETag> future : mTagFutures) {
            mTags.add(future.get());
        }
    } catch (ExecutionException e) {
        // No recover ways so that we need to cancel all the upload tasks
        // and abort the multipart upload
        Futures.allAsList(mTagFutures).cancel(true);
        abortMultiPartUpload();
        throw new IOException(
                "Part upload failed in multipart upload with " + "id '" + mUploadId + "' to " + mKey, e);
    } catch (InterruptedException e) {
        LOG.warn("Interrupted object upload.", e);
        Futures.allAsList(mTagFutures).cancel(true);
        abortMultiPartUpload();
        Thread.currentThread().interrupt();
    }
    mTagFutures = new ArrayList<>();
    if (mTags.size() != beforeSize) {
        LOG.debug("Uploaded {} partitions of id '{}' to {}.", mTags.size(), mUploadId, mKey);
    }
}