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

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

Introduction

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

Prototype

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

Source Link

Document

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

Usage

From source file:com.google.caliper.runner.ExperimentingCaliperRun.java

/**
 * Schedule all the trials./*from   w  w  w. ja v a 2s . co m*/
 *
 * <p>This method arranges all the {@link ScheduledTrial trials} to run according to their
 * scheduling criteria.  The executor instance is responsible for enforcing max parallelism.
 */
private List<ListenableFuture<TrialResult>> scheduleTrials(List<ScheduledTrial> trials,
        final ListeningExecutorService executor) {
    List<ListenableFuture<TrialResult>> pendingTrials = Lists.newArrayList();
    List<ScheduledTrial> serialTrials = Lists.newArrayList();
    for (final ScheduledTrial scheduledTrial : trials) {
        if (scheduledTrial.policy() == TrialSchedulingPolicy.PARALLEL) {
            pendingTrials.add(executor.submit(scheduledTrial.trialTask()));
        } else {
            serialTrials.add(scheduledTrial);
        }
    }
    // A future representing the completion of all prior tasks. Futures.successfulAsList allows us
    // to ignore failure.
    ListenableFuture<?> previous = Futures.successfulAsList(pendingTrials);
    for (final ScheduledTrial scheduledTrial : serialTrials) {
        // each of these trials can only start after all prior trials have finished, so we use
        // Futures.transform to force the sequencing.
        ListenableFuture<TrialResult> current = Futures.transform(previous,
                new AsyncFunction<Object, TrialResult>() {
                    @Override
                    public ListenableFuture<TrialResult> apply(Object ignored) {
                        return executor.submit(scheduledTrial.trialTask());
                    }
                });
        pendingTrials.add(current);
        // ignore failure of the prior task.
        previous = Futures.withFallback(current, FALLBACK_TO_NULL);
    }
    return pendingTrials;
}

From source file:org.glassfish.jersey.examples.rx.agent.ListenableFutureAgentResource.java

private ListenableFuture<List<Recommendation>> calculations(
        final ListenableFuture<List<Recommendation>> recommendations) {
    return Futures.transform(recommendations, new AsyncFunction<List<Recommendation>, List<Recommendation>>() {
        @Override//from   ww  w.jav  a 2 s  . c  o m
        public ListenableFuture<List<Recommendation>> apply(final List<Recommendation> input) throws Exception {
            final RxWebTarget<RxListenableFutureInvoker> rxCalculations = RxListenableFuture.from(calculation);
            return Futures.successfulAsList(
                    Lists.transform(input, new Function<Recommendation, ListenableFuture<Recommendation>>() {
                        @Override
                        public ListenableFuture<Recommendation> apply(final Recommendation r) {
                            return Futures.transform(
                                    rxCalculations.resolveTemplate("from", "Moon")
                                            .resolveTemplate("to", r.getDestination()).request().rx()
                                            .get(Calculation.class),
                                    new AsyncFunction<Calculation, Recommendation>() {
                                        @Override
                                        public ListenableFuture<Recommendation> apply(final Calculation c)
                                                throws Exception {
                                            r.setPrice(c.getPrice());
                                            return Futures.immediateFuture(r);
                                        }
                                    });
                        }
                    }));
        }
    });
}

From source file:org.apache.twill.discovery.ZKDiscoveryService.java

@Override
public void close() {
    if (!closed.compareAndSet(false, true)) {
        return;/*from  ww w  .j a  va 2 s .  com*/
    }

    // Stop the registration retry executor
    retryExecutor.shutdownNow();

    // Cancel the connection watcher
    watcherCancellable.cancel();

    // Cancel all registered services
    List<ListenableFuture<?>> futures = new ArrayList<>();
    lock.lock();
    try {
        for (Map.Entry<Discoverable, DiscoveryCancellable> entry : discoverables.entries()) {
            LOG.debug("Un-registering service {} - {}", entry.getKey().getName(),
                    entry.getKey().getSocketAddress());
            futures.add(entry.getValue().asyncCancel());
        }
    } finally {
        lock.unlock();
    }
    try {
        Futures.successfulAsList(futures).get();
        LOG.debug("All services unregistered");
    } catch (Exception e) {
        // This is not expected to happen
        LOG.warn("Unexpected exception when waiting for all services to get unregistered", e);
    }

    // Cancel all services being watched
    services.invalidateAll();
}

From source file:flipkart.lego.engine.Lego.java

private long waitUntilAvailableOrTimeout(Map<String, ListenableFuture> requiredFutureHashMap,
        Map<String, ListenableFuture> optionalFutureHashMap, Request request, long elementTimeout)
        throws TimeoutException {
    //requiredFuture is only realized if all the futures are realized
    List<ListenableFuture<Object>> requireFutureList = new ArrayList<>();
    for (Map.Entry<String, ListenableFuture> listenableFutureEntry : requiredFutureHashMap.entrySet()) {
        requireFutureList.add(listenableFutureEntry.getValue());
    }//from w w  w .  j  a  v a2  s  .c  o  m
    ListenableFuture<List<Object>> requiredFuture = Futures.allAsList(requireFutureList);

    //requiredFuture is only realized if all the futures are realized
    List<ListenableFuture<Object>> optionalFutureList = new ArrayList<>();
    for (Map.Entry<String, ListenableFuture> listenableFutureEntry : optionalFutureHashMap.entrySet()) {
        optionalFutureList.add(listenableFutureEntry.getValue());
    }
    ListenableFuture<List<Object>> optionalFuture = Futures.successfulAsList(optionalFutureList);

    //used to calculate remaining time for timeout
    Stopwatch requiredDSStopWatch = Stopwatch.createStarted();

    //Wait until timeout to see if required data is realized, if it times out throw internalErrorException
    try {
        requiredFuture.get(elementTimeout, TimeUnit.MILLISECONDS);
    } catch (TimeoutException timeoutException) {
        exceptionLogger.error("TimeOutException: required data sources timed out {}, Timeout:{}, Exception:{}",
                request, elementTimeout, timeoutException);
        requiredFuture.cancel(true);
        cancelFutures((Collection) requireFutureList);
        throw timeoutException;
    } catch (InterruptedException interruptedException) {
        exceptionLogger.error(
                "InterruptedException: required data sources were interrupted{}, Message:{}, Exception:{}",
                request, interruptedException.getMessage(), interruptedException);
        requiredFuture.cancel(true);
        cancelFutures((Collection) requireFutureList);
        throwTimeoutException(interruptedException);
    } catch (ExecutionException executionException) {
        exceptionLogger.error("ExcecutionException: {}", executionException);
        requiredFuture.cancel(true);
        cancelFutures((Collection) requireFutureList);
        throwTimeoutException(executionException);
    }

    //if time is still remaining before timeout wait until timeout for optional data to realize itself
    requiredDSStopWatch.stop();
    long remainingTimeForTimeout = elementTimeout - requiredDSStopWatch.elapsed(TimeUnit.MILLISECONDS); //calculates milliseconds remaining before elementTimeout
    Stopwatch optionalDsStopWatch = Stopwatch.createStarted();
    if (remainingTimeForTimeout > 0) {
        try {
            optionalFuture.get(1, TimeUnit.MILLISECONDS);
        } catch (Exception exception) {
            optionalFuture.cancel(true);
            cancelFutures((Collection) optionalFutureList);
            exceptionLogger.warn("Optional Data Sources Were Not Realized {}, Exception: {}", request,
                    exception);
        }
    }

    optionalDsStopWatch.stop();
    remainingTimeForTimeout = remainingTimeForTimeout - optionalDsStopWatch.elapsed(TimeUnit.MILLISECONDS); //calculate time remaining for execution of response filters
    return remainingTimeForTimeout > 0 ? remainingTimeForTimeout : 0;
}

From source file:com.continuuity.weave.internal.appmaster.RunningContainers.java

/**
 * Stops all running services. Only called when the AppMaster stops.
 *///from ww  w  .  ja v a  2  s .co m
void stopAll() {
    containerLock.lock();
    try {
        // Stop it one by one in reverse order of start sequence
        Iterator<String> itor = startSequence.descendingIterator();
        List<ListenableFuture<ServiceController.State>> futures = Lists.newLinkedList();
        while (itor.hasNext()) {
            String runnableName = itor.next();
            LOG.info("Stopping all instances of " + runnableName);

            futures.clear();
            // Parallel stops all running containers of the current runnable.
            for (WeaveContainerController controller : containers.row(runnableName).values()) {
                futures.add(controller.stop());
            }
            // Wait for containers to stop. Assumes the future returned by Futures.successfulAsList won't throw exception.
            Futures.getUnchecked(Futures.successfulAsList(futures));

            LOG.info("Terminated all instances of " + runnableName);
        }
        containers.clear();
        runnableInstances.clear();
    } finally {
        containerLock.unlock();
    }
}

From source file:org.thingsboard.server.dao.entityview.EntityViewServiceImpl.java

@Override
public ListenableFuture<List<EntityView>> findEntityViewsByQuery(TenantId tenantId,
        EntityViewSearchQuery query) {//from   w  w w  .  j ava  2s .c om
    ListenableFuture<List<EntityRelation>> relations = relationService.findByQuery(tenantId,
            query.toEntitySearchQuery());
    ListenableFuture<List<EntityView>> entityViews = Futures.transformAsync(relations, r -> {
        EntitySearchDirection direction = query.toEntitySearchQuery().getParameters().getDirection();
        List<ListenableFuture<EntityView>> futures = new ArrayList<>();
        for (EntityRelation relation : r) {
            EntityId entityId = direction == EntitySearchDirection.FROM ? relation.getTo() : relation.getFrom();
            if (entityId.getEntityType() == EntityType.ENTITY_VIEW) {
                futures.add(findEntityViewByIdAsync(tenantId, new EntityViewId(entityId.getId())));
            }
        }
        return Futures.successfulAsList(futures);
    });

    entityViews = Futures.transform(entityViews, new Function<List<EntityView>, List<EntityView>>() {
        @Nullable
        @Override
        public List<EntityView> apply(@Nullable List<EntityView> entityViewList) {
            return entityViewList == null ? Collections.emptyList()
                    : entityViewList.stream()
                            .filter(entityView -> query.getEntityViewTypes().contains(entityView.getType()))
                            .collect(Collectors.toList());
        }
    });

    return entityViews;
}

From source file:com.facebook.buck.util.concurrent.ResourcePool.java

@Override
public synchronized void close() {
    Preconditions.checkState(!closing.get());
    closing.set(true);/* w  w w .jav  a2 s .  c o m*/

    // Unblock all waiting requests.
    for (SettableFuture<Void> request : resourceRequests) {
        request.set(null);
    }
    resourceRequests.clear();

    // Any processing that is currently taking place will be allowed to complete (as it won't notice
    // `closing` is true.
    // Any scheduled (but not executing) resource requests should notice `closing` is true and
    // mark themselves as cancelled.
    // Therefore `closeFuture` should allow us to wait for any resources that are in use.
    ListenableFuture<List<Object>> closeFuture = Futures.successfulAsList(pendingWork);

    // As silly as it seems this is the only reliable way to make sure we run the shutdown code.
    // Reusing an external executor means we run the risk of it being shut down before the cleanup
    // future is ready to run (which causes it to never run).
    // Using a direct executor means we run the chance of executing shutdown synchronously (which
    // we try to avoid).
    final ExecutorService executorService = MostExecutors.newSingleThreadExecutor("resource shutdown");

    // It is possible that more requests for work are scheduled at this point, however they should
    // all early-out due to `closing` being set to true, so we don't really care about those.
    shutdownFuture = Futures.transformAsync(closeFuture, new AsyncFunction<List<Object>, Void>() {
        @Override
        public ListenableFuture<Void> apply(List<Object> input) throws Exception {
            synchronized (ResourcePool.this) {
                if (parkedResources.size() != createdResources.size()) {
                    LOG.error("Whoops! Some resource are still in use during shutdown.");
                }
                // Now that pending work is done we can close all resources.
                for (R resource : createdResources) {
                    resource.close();
                }
                if (!resourceRequests.isEmpty()) {
                    LOG.error("Error shutting down ResourcePool: "
                            + "there should be no enqueued resource requests.");
                }
            }
            executorService.shutdown();
            return Futures.immediateFuture(null);
        }
    }, executorService);
}

From source file:org.apache.cassandra.repair.RepairRunnable.java

protected void runMayThrow() throws Exception {
    final TraceState traceState;

    final String tag = "repair:" + cmd;

    final AtomicInteger progress = new AtomicInteger();
    final int totalProgress = 3 + options.getRanges().size(); // calculate neighbors, validation, prepare for repair + number of ranges to repair

    String[] columnFamilies = options.getColumnFamilies()
            .toArray(new String[options.getColumnFamilies().size()]);
    Iterable<ColumnFamilyStore> validColumnFamilies = storageService.getValidColumnFamilies(false, false,
            keyspace, columnFamilies);//from  w  w  w. j  ava  2 s.c o m

    final long startTime = System.currentTimeMillis();
    String message = String.format("Starting repair command #%d, repairing keyspace %s with %s", cmd, keyspace,
            options);
    logger.info(message);
    fireProgressEvent(tag, new ProgressEvent(ProgressEventType.START, 0, 100, message));
    if (options.isTraced()) {
        StringBuilder cfsb = new StringBuilder();
        for (ColumnFamilyStore cfs : validColumnFamilies)
            cfsb.append(", ").append(cfs.keyspace.getName()).append(".").append(cfs.name);

        UUID sessionId = Tracing.instance.newSession(Tracing.TraceType.REPAIR);
        traceState = Tracing.instance.begin("repair",
                ImmutableMap.of("keyspace", keyspace, "columnFamilies", cfsb.substring(2)));
        Tracing.traceRepair(message);
        traceState.enableActivityNotification(tag);
        for (ProgressListener listener : listeners)
            traceState.addProgressListener(listener);
        Thread queryThread = createQueryThread(cmd, sessionId);
        queryThread.setName("RepairTracePolling");
        queryThread.start();
    } else {
        traceState = null;
    }

    final Set<InetAddress> allNeighbors = new HashSet<>();
    Map<Range, Set<InetAddress>> rangeToNeighbors = new HashMap<>();
    try {
        for (Range<Token> range : options.getRanges()) {
            Set<InetAddress> neighbors = ActiveRepairService.getNeighbors(keyspace, range,
                    options.getDataCenters(), options.getHosts());
            rangeToNeighbors.put(range, neighbors);
            allNeighbors.addAll(neighbors);
        }
        progress.incrementAndGet();
    } catch (IllegalArgumentException e) {
        logger.error("Repair failed:", e);
        fireErrorAndComplete(tag, progress.get(), totalProgress, e.getMessage());
        return;
    }

    // Validate columnfamilies
    List<ColumnFamilyStore> columnFamilyStores = new ArrayList<>();
    try {
        Iterables.addAll(columnFamilyStores, validColumnFamilies);
        progress.incrementAndGet();
    } catch (IllegalArgumentException e) {
        fireErrorAndComplete(tag, progress.get(), totalProgress, e.getMessage());
        return;
    }

    String[] cfnames = new String[columnFamilyStores.size()];
    for (int i = 0; i < columnFamilyStores.size(); i++) {
        cfnames[i] = columnFamilyStores.get(i).name;
    }

    final UUID parentSession = UUIDGen.getTimeUUID();
    SystemDistributedKeyspace.startParentRepair(parentSession, keyspace, cfnames, options.getRanges());
    long repairedAt;
    try {
        ActiveRepairService.instance.prepareForRepair(parentSession, allNeighbors, options, columnFamilyStores);
        repairedAt = ActiveRepairService.instance.getParentRepairSession(parentSession).getRepairedAt();
        progress.incrementAndGet();
    } catch (Throwable t) {
        SystemDistributedKeyspace.failParentRepair(parentSession, t);
        fireErrorAndComplete(tag, progress.get(), totalProgress, t.getMessage());
        return;
    }

    // Set up RepairJob executor for this repair command.
    final ListeningExecutorService executor = MoreExecutors.listeningDecorator(
            new JMXConfigurableThreadPoolExecutor(options.getJobThreads(), Integer.MAX_VALUE, TimeUnit.SECONDS,
                    new LinkedBlockingQueue<Runnable>(), new NamedThreadFactory("Repair#" + cmd), "internal"));

    List<ListenableFuture<RepairSessionResult>> futures = new ArrayList<>(options.getRanges().size());
    for (Range<Token> range : options.getRanges()) {
        final RepairSession session = ActiveRepairService.instance.submitRepairSession(parentSession, range,
                keyspace, options.getParallelism(), rangeToNeighbors.get(range), repairedAt, executor, cfnames);
        if (session == null)
            continue;
        // After repair session completes, notify client its result
        Futures.addCallback(session, new FutureCallback<RepairSessionResult>() {
            public void onSuccess(RepairSessionResult result) {
                String message = String.format("Repair session %s for range %s finished", session.getId(),
                        session.getRange().toString());
                logger.info(message);
                fireProgressEvent(tag, new ProgressEvent(ProgressEventType.PROGRESS, progress.incrementAndGet(),
                        totalProgress, message));
            }

            public void onFailure(Throwable t) {
                String message = String.format("Repair session %s for range %s failed with error %s",
                        session.getId(), session.getRange().toString(), t.getMessage());
                logger.error(message, t);
                fireProgressEvent(tag, new ProgressEvent(ProgressEventType.PROGRESS, progress.incrementAndGet(),
                        totalProgress, message));
            }
        });
        futures.add(session);
    }

    // After all repair sessions completes(successful or not),
    // run anticompaction if necessary and send finish notice back to client
    final Collection<Range<Token>> successfulRanges = new ArrayList<>();
    final AtomicBoolean hasFailure = new AtomicBoolean();
    final ListenableFuture<List<RepairSessionResult>> allSessions = Futures.successfulAsList(futures);
    ListenableFuture anticompactionResult = Futures.transform(allSessions,
            new AsyncFunction<List<RepairSessionResult>, Object>() {
                @SuppressWarnings("unchecked")
                public ListenableFuture apply(List<RepairSessionResult> results) throws Exception {
                    // filter out null(=failed) results and get successful ranges
                    for (RepairSessionResult sessionResult : results) {
                        if (sessionResult != null) {
                            successfulRanges.add(sessionResult.range);
                        } else {
                            hasFailure.compareAndSet(false, true);
                        }
                    }
                    return ActiveRepairService.instance.finishParentSession(parentSession, allNeighbors,
                            successfulRanges);
                }
            });
    Futures.addCallback(anticompactionResult, new FutureCallback<Object>() {
        public void onSuccess(Object result) {
            SystemDistributedKeyspace.successfulParentRepair(parentSession, successfulRanges);
            if (hasFailure.get()) {
                fireProgressEvent(tag, new ProgressEvent(ProgressEventType.ERROR, progress.get(), totalProgress,
                        "Some repair failed"));
            } else {
                fireProgressEvent(tag, new ProgressEvent(ProgressEventType.SUCCESS, progress.get(),
                        totalProgress, "Repair completed successfully"));
            }
            repairComplete();
        }

        public void onFailure(Throwable t) {
            fireProgressEvent(tag,
                    new ProgressEvent(ProgressEventType.ERROR, progress.get(), totalProgress, t.getMessage()));
            SystemDistributedKeyspace.failParentRepair(parentSession, t);
            repairComplete();
        }

        private void repairComplete() {
            String duration = DurationFormatUtils.formatDurationWords(System.currentTimeMillis() - startTime,
                    true, true);
            String message = String.format("Repair command #%d finished in %s", cmd, duration);
            fireProgressEvent(tag,
                    new ProgressEvent(ProgressEventType.COMPLETE, progress.get(), totalProgress, message));
            logger.info(message);
            if (options.isTraced() && traceState != null) {
                for (ProgressListener listener : listeners)
                    traceState.removeProgressListener(listener);
                // Because DebuggableThreadPoolExecutor#afterExecute and this callback
                // run in a nondeterministic order (within the same thread), the
                // TraceState may have been nulled out at this point. The TraceState
                // should be traceState, so just set it without bothering to check if it
                // actually was nulled out.
                Tracing.instance.set(traceState);
                Tracing.traceRepair(message);
                Tracing.instance.stopSession();
            }
            executor.shutdownNow();
        }
    });
}

From source file:org.thingsboard.server.service.state.DefaultDeviceStateService.java

private void initStateFromDB() {
    List<Tenant> tenants = tenantService.findTenants(new TextPageLink(Integer.MAX_VALUE)).getData();
    for (Tenant tenant : tenants) {
        List<ListenableFuture<DeviceStateData>> fetchFutures = new ArrayList<>();
        List<Device> devices = deviceService
                .findDevicesByTenantId(tenant.getId(), new TextPageLink(Integer.MAX_VALUE)).getData();
        for (Device device : devices) {
            if (!routingService.resolveById(device.getId()).isPresent()) {
                fetchFutures.add(fetchDeviceState(device));
            }// w  ww  . j a  v  a2 s . co m
        }
        try {
            Futures.successfulAsList(fetchFutures).get().forEach(this::addDeviceUsingState);
        } catch (InterruptedException | ExecutionException e) {
            log.warn("Failed to init device state service from DB", e);
        }
    }
}

From source file:org.apache.brooklyn.core.management.internal.LocalUsageManager.java

public void terminate() {
    // Wait for the listeners to finish + close the listeners
    Duration timeout = managementContext.getBrooklynProperties()
            .getConfig(UsageManager.USAGE_LISTENER_TERMINATION_TIMEOUT);
    if (listenerQueueSize.get() > 0) {
        log.info("Usage manager waiting for " + listenerQueueSize + " listener events for up to " + timeout);
    }// w  w w  . j  a v  a2s .  c o m
    List<ListenableFuture<?>> futures = Lists.newArrayList();
    for (final org.apache.brooklyn.core.management.internal.UsageListener listener : listeners) {
        ListenableFuture<?> future = listenerExecutor.submit(new Runnable() {
            public void run() {
                if (listener instanceof Closeable) {
                    try {
                        ((Closeable) listener).close();
                    } catch (IOException e) {
                        log.warn("Problem closing usage listener " + listener + " (continuing)", e);
                    }
                }
            }
        });
        futures.add(future);
    }
    try {
        Futures.successfulAsList(futures).get(timeout.toMilliseconds(), TimeUnit.MILLISECONDS);
    } catch (Exception e) {
        Exceptions.propagateIfFatal(e);
        log.warn("Problem terminiating usage listeners (continuing)", e);
    } finally {
        listenerExecutor.shutdownNow();
    }
}