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:com.facebook.buck.rules.AbstractCachingBuildRule.java

@Override
public final ListenableFuture<BuildRuleSuccess> build(final BuildContext context) {
    // We use hasBuildStarted as a lock so that we can minimize how much we need to synchronize.
    synchronized (hasBuildStarted) {
        if (hasBuildStarted.get()) {
            return buildRuleResult;
        } else {/*from   ww  w  . j  a  va2s  .  co m*/
            hasBuildStarted.set(true);
        }
    }

    // Build all of the deps first and then schedule a callback for this rule to build itself once
    // all of those rules are done building.
    try {
        // Invoke every dep's build() method and create an uber-ListenableFuture that represents the
        // successful completion of all deps.
        List<ListenableFuture<BuildRuleSuccess>> builtDeps = Lists.newArrayListWithCapacity(getDeps().size());
        for (BuildRule dep : getDeps()) {
            builtDeps.add(dep.build(context));
        }
        ListenableFuture<List<BuildRuleSuccess>> allBuiltDeps = Futures.allAsList(builtDeps);

        // Schedule this rule to build itself once all of the deps are built.
        Futures.addCallback(allBuiltDeps, new FutureCallback<List<BuildRuleSuccess>>() {

            private final BuckEventBus eventBus = context.getEventBus();

            private final OnDiskBuildInfo onDiskBuildInfo = context.createOnDiskBuildInfoFor(getBuildTarget());

            /**
             * It is imperative that:
             * <ol>
             *   <li>The {@link BuildInfoRecorder} is not constructed until all of the
             *       {@link Buildable}'s {@code deps} are guaranteed to be built. This ensures that
             *       the {@link RuleKey} will be available before the {@link BuildInfoRecorder} is
             *       constructed.
             *       <p>
             *       This is why a {@link Supplier} is used.
             *   <li>Only one {@link BuildInfoRecorder} is created per {@link Buildable}. This
             *       ensures that all build-related information for a {@link Buildable} goes though
             *       a single recorder, whose data will be persisted in {@link #onSuccess(List)}.
             *       <p>
             *       This is why {@link Suppliers#memoize(Supplier)} is used.
             * </ol>
             */
            private final Supplier<BuildInfoRecorder> buildInfoRecorder = Suppliers
                    .memoize(new Supplier<BuildInfoRecorder>() {
                        @Override
                        public BuildInfoRecorder get() {
                            AbstractBuildRule buildRule = AbstractCachingBuildRule.this;
                            RuleKey ruleKey;
                            RuleKey ruleKeyWithoutDeps;
                            try {
                                ruleKey = buildRule.getRuleKey();
                                ruleKeyWithoutDeps = buildRule.getRuleKeyWithoutDeps();
                            } catch (IOException e) {
                                throw new RuntimeException(e);
                            }

                            return context.createBuildInfoRecorder(buildRule.getBuildTarget(), ruleKey,
                                    ruleKeyWithoutDeps);
                        }
                    });

            private boolean startOfBuildWasRecordedOnTheEventBus = false;

            @Override
            public void onSuccess(List<BuildRuleSuccess> deps) {
                // Record the start of the build.
                eventBus.post(BuildRuleEvent.started(AbstractCachingBuildRule.this));
                startOfBuildWasRecordedOnTheEventBus = true;

                try {
                    BuildResult result = buildOnceDepsAreBuilt(context, onDiskBuildInfo,
                            buildInfoRecorder.get());
                    if (result.isSuccess()) {
                        recordBuildRuleSuccess(result);
                    } else {
                        recordBuildRuleFailure(result);
                    }
                } catch (IOException e) {
                    onFailure(e);
                }
            }

            private void recordBuildRuleSuccess(BuildResult result) {
                // Make sure that all of the local files have the same values they would as if the
                // rule had been built locally.
                if (result.success.shouldWriteRecordedMetadataToDiskAfterBuilding()) {
                    try {
                        buildInfoRecorder.get().writeMetadataToDisk();
                    } catch (IOException e) {
                        onFailure(e);
                    }
                }

                // Give the rule a chance to populate its internal data structures now that all of the
                // files should be in a valid state.
                if (result.success.shouldInitializeFromDiskAfterBuilding()) {
                    initializeFromDisk(onDiskBuildInfo);
                }

                // Only now that the rule should be in a completely valid state, resolve the future.
                BuildRuleSuccess buildRuleSuccess = new BuildRuleSuccess(AbstractCachingBuildRule.this,
                        result.success);
                buildRuleResult.set(buildRuleSuccess);

                // Do the post to the event bus immediately after the future is set so that the
                // build time measurement is as accurate as possible.
                eventBus.post(BuildRuleEvent.finished(AbstractCachingBuildRule.this, result.status,
                        result.cacheResult, Optional.of(result.success)));

                // Finally, upload to the artifact cache.
                if (result.success.shouldUploadResultingArtifact()) {
                    buildInfoRecorder.get().performUploadToArtifactCache(context.getArtifactCache(), eventBus);
                }
            }

            @Override
            public void onFailure(Throwable failure) {
                recordBuildRuleFailure(new BuildResult(failure));
            }

            private void recordBuildRuleFailure(BuildResult result) {
                // TODO(mbolin): Delete all genfiles and metadata, as they are not guaranteed to be
                // valid at this point?

                // Note that startOfBuildWasRecordedOnTheEventBus will be false if onSuccess() was
                // never invoked.
                if (startOfBuildWasRecordedOnTheEventBus) {
                    eventBus.post(BuildRuleEvent.finished(AbstractCachingBuildRule.this, result.status,
                            result.cacheResult, Optional.<BuildRuleSuccess.Type>absent()));
                }

                // It seems possible (albeit unlikely) that something could go wrong in
                // recordBuildRuleSuccess() after buildRuleResult has been resolved such that Buck
                // would attempt to resolve the future again, which would fail.
                buildRuleResult.setException(result.failure);
            }
        }, context.getExecutor());
    } catch (Throwable failure) {
        // This is a defensive catch block: if buildRuleResult is never satisfied, then Buck will
        // hang because a callback that is waiting for this rule's future to complete will never be
        // executed.
        buildRuleResult.setException(failure);
    }

    return buildRuleResult;
}

From source file:com.cloudera.science.ml.client.cmd.KMeansCommand.java

private List<Centers> getClusters(ListeningExecutorService exec, List<Weighted<Vector>> sketch, KMeans kmeans) {
    List<ListenableFuture<Centers>> futures = Lists.newArrayList();
    for (Integer nc : clusters) {
        int loops = nc == 1 ? 1 : bestOf;
        for (int i = 0; i < loops; i++) {
            Random r = randomParams.getRandom(nc + i);
            futures.add(exec.submit(new Clustering(kmeans, sketch, nc, r)));
        }/*from   w  ww  .j  a va  2s  . co m*/
    }
    try {
        return Futures.allAsList(futures).get();
    } catch (Exception e) {
        throw new CommandException("Error in clustering", e);
    }
}

From source file:org.opendaylight.mdsal.singleton.dom.impl.ClusterSingletonServiceGroupImpl.java

@SuppressWarnings("checkstyle:IllegalCatch")
@Override/*from w  w  w.j  av  a 2s  . c  o m*/
public ListenableFuture<List<Void>> closeClusterSingletonGroup() {
    LOG.debug("Close method for service Provider {}", clusterSingletonGroupIdentifier);
    boolean needReleaseLock = false;
    final ListenableFuture<List<Void>> destroyFuture;
    try {
        needReleaseLock = clusterLock.tryAcquire(1, TimeUnit.SECONDS);
    } catch (final Exception e) {
        LOG.warn("Unexpected Exception for service Provider {} in closing phase.",
                clusterSingletonGroupIdentifier, e);
    } finally {
        if (serviceEntityCandidateReg != null) {
            serviceEntityCandidateReg.close();
            serviceEntityCandidateReg = null;
        }
        final List<ListenableFuture<Void>> serviceCloseFutureList = new ArrayList<>();
        if (hasOwnership) {
            for (final ClusterSingletonServiceRegistrationDelegator service : serviceGroup) {
                try {
                    serviceCloseFutureList.add(service.closeServiceInstance());
                } catch (final RuntimeException e) {
                    LOG.warn("Unexpected exception while closing service: {}, resuming with next..",
                            service.getIdentifier());
                }
            }
            hasOwnership = false;
        }
        destroyFuture = Futures.allAsList(serviceCloseFutureList);
        final Semaphore finalRelease = needReleaseLock ? clusterLock : null;
        Futures.addCallback(destroyFuture, newAsyncCloseCallback(finalRelease, true));
    }
    return destroyFuture;
}

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

@Override
public ListenableFuture<List<Void>> save(TenantId tenantId, EntityId entityId, List<TsKvEntry> tsKvEntries,
        long ttl) {
    List<ListenableFuture<Void>> futures = Lists
            .newArrayListWithExpectedSize(tsKvEntries.size() * INSERTS_PER_ENTRY);
    for (TsKvEntry tsKvEntry : tsKvEntries) {
        if (tsKvEntry == null) {
            throw new IncorrectParameterException("Key value entry can't be null");
        }/*  ww  w.j  av a2  s .  c  o m*/
        saveAndRegisterFutures(tenantId, futures, entityId, tsKvEntry, ttl);
    }
    return Futures.allAsList(futures);
}

From source file:org.apache.beam.runners.dataflow.util.PackageUtil.java

/**
 * Utility function that computes sizes and hashes of packages so that we can validate whether
 * they have already been correctly staged.
 *//*from  w ww  .j a  va 2 s  .com*/
private static List<PackageAttributes> computePackageAttributes(Collection<String> classpathElements,
        final String stagingPath, ListeningExecutorService executorService) {
    List<ListenableFuture<PackageAttributes>> futures = new LinkedList<>();
    for (String classpathElement : classpathElements) {
        @Nullable
        String userPackageName = null;
        if (classpathElement.contains("=")) {
            String[] components = classpathElement.split("=", 2);
            userPackageName = components[0];
            classpathElement = components[1];
        }
        @Nullable
        final String packageName = userPackageName;

        final File file = new File(classpathElement);
        if (!file.exists()) {
            LOG.warn("Skipping non-existent classpath element {} that was specified.", classpathElement);
            continue;
        }

        ListenableFuture<PackageAttributes> future = executorService.submit(new Callable<PackageAttributes>() {
            @Override
            public PackageAttributes call() throws Exception {
                return createPackageAttributes(file, stagingPath, packageName);
            }
        });
        futures.add(future);
    }

    try {
        return Futures.allAsList(futures).get();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new RuntimeException("Interrupted while staging packages", e);
    } catch (ExecutionException e) {
        throw new RuntimeException("Error while staging packages", e.getCause());
    }
}

From source file:com.facebook.buck.rules.UnskippedRulesTracker.java

private ListenableFuture<Void> releaseReferences(ImmutableSortedSet<BuildRule> rules) {
    ImmutableList.Builder<ListenableFuture<Void>> futures = ImmutableList.builder();
    for (BuildRule rule : rules) {
        futures.add(releaseReference(rule));
    }//from   w w w.  j av a 2  s.c  o  m
    return Futures.transform(Futures.allAsList(futures.build()), NULL_FUNCTION);
}

From source file:org.opendaylight.controller.cluster.sharding.ShardProxyTransaction.java

@Override
public ListenableFuture<Void> prepare() {
    LOG.debug("Preparing transaction for shard {}", shardRoot);

    checkTransactionReadied();//from   ww w  .j  a va  2s . c o m
    final List<ListenableFuture<Void>> futures = cohorts.stream().map(DOMStoreThreePhaseCommitCohort::preCommit)
            .collect(Collectors.toList());
    final SettableFuture<Void> ret = SettableFuture.create();

    Futures.addCallback(Futures.allAsList(futures), new FutureCallback<List<Void>>() {
        @Override
        public void onSuccess(final List<Void> result) {
            ret.set(null);
        }

        @Override
        public void onFailure(final Throwable throwable) {
            ret.setException(throwable);
        }
    }, MoreExecutors.directExecutor());

    return ret;
}

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

public static <ROW, SEQUENCE extends Iterable<ROW>, KEY> ListenableFuture<List<ROW>> invokeAsyncScatter(
        final Executor executor, final AsyncFunction<KEY, SEQUENCE> source, List<KEY> keys, Tracer tracer,
        Timeout timeout, TimeoutHandler handler) throws Exception {
    List<ListenableFuture<SEQUENCE>> results = Lists.newArrayList();
    int idx = -1;
    for (KEY key : keys) {
        if (key != null) {
            final Tracer childTracer = tracer.start(tracer.getGroup(), tracer.getName() + "." + (++idx));
            ListenableFuture<SEQUENCE> result = source.apply(key);
            results.add(result);/*from  w ww  .  j ava 2  s .  c  om*/
            result.addListener(new Runnable() {
                @Override
                public void run() {
                    childTracer.end();
                }
            }, MoreExecutors.sameThreadExecutor());
        }
    }
    ListenableFuture<List<SEQUENCE>> gather = Futures.allAsList(results);
    final int estimatedResultSize = results.size();
    return handler.withTimeout(gatherResults(executor, gather, estimatedResultSize), timeout.verify(),
            timeout.getTickUnits());
}

From source file:org.jclouds.compute.util.ConcurrentOpenSocketFinder.java

private static void blockOn(Iterable<ListenableFuture<?>> immutableList) {
    try {//from www .jav a  2  s  . c o  m
        Futures.allAsList(immutableList).get();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw propagate(e);
    } catch (ExecutionException e) {
        throw propagate(e);
    }
}

From source file:com.b2international.snowowl.core.events.util.Promise.java

/**
 * @param promises//  ww  w. j  a  v a2s . c  o  m
 * @return
 * @since 4.6
 */
@Beta
public static Promise<List<Object>> all(Iterable<? extends Promise<?>> promises) {
    return Promise.wrap(Futures.allAsList(promises));
}