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

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

Introduction

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

Prototype

public static <I, O> ListenableFuture<O> transformAsync(ListenableFuture<I> input,
        AsyncFunction<? super I, ? extends O> function, Executor executor) 

Source Link

Document

Returns a new ListenableFuture whose result is asynchronously derived from the result of the given Future .

Usage

From source file:com.facebook.buck.core.build.distributed.synchronization.impl.RemoteBuildRuleSynchronizer.java

@Override
public synchronized ListenableFuture<CacheResult> waitForBuildRuleToAppearInCache(BuildRule buildRule,
        Supplier<ListenableFuture<CacheResult>> cacheCheck) {
    String buildTarget = buildRule.getFullyQualifiedName();
    if (!buildRule.isCacheable()) {
        LOG.info(String.format("Doing only immediate cache check for build target [%s]", buildTarget));
        // Stampede transfers artifacts via the cache. If the build rule isn't cacheable, then
        // immediately do only the requested cache check (may contain logging etc.) without any
        // retries.
        // Will allow proceeding with next local steps immediately (i.e. cache fetches for
        // all dependencies).
        return cacheCheck.get();
    }/*from  w  w w.  j  av a  2 s . c om*/

    // If build is already marked as finish, then cannot expect to get completion signal for rule
    // later (possible completion event was missed/misordered). Do not wait for it then.
    ListenableFuture<CacheResult> resultFuture = remoteBuildFinished ? cacheCheck.get()
            : Futures.transformAsync(createCompletionFutureIfNotPresent(buildTarget),
                    (Void v) -> cacheCheck.get(), MoreExecutors.directExecutor());

    // Backoffs are disabled.
    if (cacheSyncMaxTotalBackoffMillis == 0) {
        return resultFuture;
    }

    for (int backOffNumber = 0; backOffNumber < backOffsMillis.length; backOffNumber++) {
        int backOffNumberForLambda = backOffNumber;
        resultFuture = Futures.transformAsync(resultFuture, result -> {
            // If we didn't get a miss (miss -> need more wait time), stop any further retries.
            if (result.getType() != CacheResultType.MISS) {
                return Futures.immediateFuture(result);
            }
            return getNextCacheCheckResult(result, cacheCheck, buildTarget, backOffNumberForLambda);
        }, MoreExecutors.directExecutor());
    }

    LOG.info(String.format("Returning future that waits for build target [%s]", buildTarget));

    return resultFuture;
}

From source file:com.facebook.buck.rules.modern.builders.RemoteExecutionStrategy.java

@Override
public StrategyBuildResult build(BuildRule rule, BuildStrategyContext strategyContext) {
    Preconditions.checkState(rule instanceof ModernBuildRule);
    BuildTarget buildTarget = rule.getBuildTarget();

    RemoteExecutionActionEvent.sendScheduledEvent(eventBus, rule.getBuildTarget());

    ListenableFuture<RemoteExecutionActionInfo> actionInfoFuture = pendingUploadsLimiter.schedule(service,
            () -> computeActionAndUpload(rule, strategyContext));

    // guard should only be set once. The left value indicates that it has been cancelled and holds
    // the reason, a right value indicates that it has passed the point of no return and can no
    // longer be cancelled.
    AtomicReference<Either<Throwable, Object>> guard = new AtomicReference<>();

    ListenableFuture<Optional<BuildResult>> buildResult = Futures.transformAsync(actionInfoFuture,
            actionInfo -> handleActionInfo(rule, strategyContext, buildTarget, actionInfo, () -> {
                if (guard.compareAndSet(null, Either.ofRight(new Object()))) {
                    return null;
                }// w  w w. j a  va  2  s. c o  m
                return Objects.requireNonNull(guard.get()).getLeft();
            }), service);

    return new StrategyBuildResult() {
        @Override
        public void cancel(Throwable cause) {
            guard.compareAndSet(null, Either.ofLeft(cause));
        }

        @Override
        public boolean cancelIfNotStarted(Throwable reason) {
            cancel(reason);
            // cancel() will have set the guard value if it weren't already set.
            return Objects.requireNonNull(guard.get()).isLeft();
        }

        @Override
        public ListenableFuture<Optional<BuildResult>> getBuildResult() {
            return buildResult;
        }
    };
}

From source file:com.orangerhymelabs.helenus.cassandra.document.DocumentService.java

public ListenableFuture<Document> read(String database, String table, Identifier id) {
    ListenableFuture<AbstractDocumentRepository> docs = acquireRepositoryFor(database, table);
    return Futures.transformAsync(docs, new AsyncFunction<AbstractDocumentRepository, Document>() {
        @Override/*from ww  w. jav  a2s.  c  o m*/
        public ListenableFuture<Document> apply(AbstractDocumentRepository input) throws Exception {
            return input.read(new Identifier(id));
        }
    }, MoreExecutors.directExecutor());
}

From source file:com.facebook.buck.core.build.engine.cache.manager.ManifestRuleKeyManager.java

public ListenableFuture<CacheResult> fetchManifest(RuleKey key) {
    Preconditions.checkState(useManifestCaching());

    Path path = getManifestPath(rule);

    // Use a temp path to store the downloaded artifact.  We'll rename it into place on success to
    // make the process more atomic.
    LazyPath tempPath = new LazyPath() {
        @Override/*from  w w w . j a  va 2  s .  c o  m*/
        protected Path create() throws IOException {
            return Files.createTempFile("buck.", ".manifest");
        }
    };

    return Futures.transformAsync(manifestRuleKeyService.fetchManifest(key, tempPath),
            (@Nonnull CacheResult fetchResult) -> {
                if (!fetchResult.getType().isSuccess()) {
                    LOG.verbose("%s: cache miss on manifest %s", rule.getBuildTarget(), key);
                    return Futures.immediateFuture(fetchResult);
                }

                // Download is successful, so move the manifest into place.
                rule.getProjectFilesystem().createParentDirs(path);
                rule.getProjectFilesystem().deleteFileAtPathIfExists(path);

                Path tempManifestPath = Files.createTempFile("buck.", "MANIFEST");
                try {
                    ungzip(tempPath.get(), tempManifestPath);
                } catch (Exception e) {
                    LOG.error("%s: zip error on manifest, key %s, path %s", rule.getBuildTarget(), key,
                            tempManifestPath);
                    throw e;
                }
                rule.getProjectFilesystem().move(tempManifestPath, path);

                LOG.verbose("%s: cache hit on manifest %s", rule.getBuildTarget(), key);

                return Futures.immediateFuture(fetchResult);
            }, MoreExecutors.directExecutor());
}

From source file:com.orangerhymelabs.helenus.cassandra.document.DocumentService.java

public ListenableFuture<Document> read(String database, String table, String view, Identifier id) {
    ListenableFuture<AbstractDocumentRepository> docs = acquireRepositoryFor(
            getTableView(database, table, view));
    return Futures.transformAsync(docs, new AsyncFunction<AbstractDocumentRepository, Document>() {
        @Override// ww  w.ja va  2  s . c  om
        public ListenableFuture<Document> apply(AbstractDocumentRepository input) throws Exception {
            return input.read(new Identifier(id));
        }
    }, MoreExecutors.directExecutor());
}

From source file:com.facebook.buck.distributed.DistBuildArtifactCacheImpl.java

@Override
public synchronized void prewarmRemoteContains(ImmutableSet<BuildRule> rulesToBeChecked) {
    @SuppressWarnings("PMD.PrematureDeclaration")
    Stopwatch stopwatch = Stopwatch.createStarted();
    Set<BuildRule> unseenRules = rulesToBeChecked.stream()
            .filter(rule -> !remoteCacheContainsFutures.containsKey(rule)).collect(Collectors.toSet());

    if (unseenRules.isEmpty()) {
        return;/*from   w  w w  .jav a2  s.  co  m*/
    }

    LOG.info("Checking remote cache for [%d] new rules.", unseenRules.size());
    Map<BuildRule, ListenableFuture<RuleKey>> rulesToKeys = Maps.asMap(unseenRules,
            rule -> ruleKeyCalculator.calculate(eventBus, rule));

    ListenableFuture<Map<RuleKey, CacheResult>> keysToCacheResultFuture = Futures
            .transformAsync(Futures.allAsList(rulesToKeys.values()), ruleKeys -> {
                LOG.info("Computing RuleKeys for %d new rules took %dms.", unseenRules.size(),
                        stopwatch.elapsed(TimeUnit.MILLISECONDS));
                stopwatch.reset();
                stopwatch.start();
                return multiContainsAsync(ruleKeys);
            }, executorService);

    Map<BuildRule, ListenableFuture<Boolean>> containsResultsForUnseenRules = Maps
            .asMap(unseenRules,
                    rule -> Futures.transform(keysToCacheResultFuture, keysToCacheResult -> Objects
                            .requireNonNull(keysToCacheResult.get(Futures.getUnchecked(rulesToKeys.get(rule))))
                            .getType().isSuccess(), MoreExecutors.directExecutor()));

    remoteCacheContainsFutures.putAll(containsResultsForUnseenRules);
    Futures.allAsList(containsResultsForUnseenRules.values())
            .addListener(() -> LOG.info("Checking the remote cache for %d rules took %dms.", unseenRules.size(),
                    stopwatch.elapsed(TimeUnit.MILLISECONDS)), MoreExecutors.directExecutor());
}

From source file:com.facebook.buck.rules.modern.builders.RemoteExecutionStrategy.java

private ListenableFuture<RemoteExecutionActionInfo> computeActionAndUpload(BuildRule rule,
        BuildStrategyContext strategyContext) {
    ListenableFuture<RemoteExecutionActionInfo> actionInfoFuture = computeActionLimiter.schedule(service,
            () -> Futures.immediateFuture(getRemoteExecutionActionInfo(rule, strategyContext)));
    return Futures.transformAsync(actionInfoFuture,
            actionInfo -> uploadInputs(rule.getBuildTarget(), actionInfo), MoreExecutors.directExecutor());
}

From source file:org.glowroot.central.util.Session.java

public ListenableFuture<ResultSet> readAsyncFailIfNoRows(Statement statement, String errorMessage)
        throws Exception {
    return Futures.transformAsync(readAsync(statement), new AsyncFunction<ResultSet, ResultSet>() {
        @Override//from w  w w. j a  v a  2s . c o  m
        public ListenableFuture<ResultSet> apply(ResultSet results) {
            if (results.isExhausted()) {
                return Futures.immediateFailedFuture(new Exception(errorMessage));
            } else {
                return Futures.immediateFuture(results);
            }
        }
    }, MoreExecutors.directExecutor());
}

From source file:com.orangerhymelabs.helenus.cassandra.document.DocumentService.java

public ListenableFuture<List<Document>> readIn(String database, String table, Identifier... ids) {
    ListenableFuture<AbstractDocumentRepository> docs = acquireRepositoryFor(database, table);
    return Futures.transformAsync(docs, new AsyncFunction<AbstractDocumentRepository, List<Document>>() {
        @Override/*from   w  ww.  j a va2  s .  c  om*/
        public ListenableFuture<List<Document>> apply(AbstractDocumentRepository input) throws Exception {
            return input.readIn(ids);
        }
    }, MoreExecutors.directExecutor());
}

From source file:com.google.gerrit.lucene.AbstractLuceneIndex.java

private ListenableFuture<?> submit(Callable<Long> task) {
    ListenableFuture<Long> future = Futures.nonCancellationPropagating(writerThread.submit(task));
    return Futures.transformAsync(future, gen -> {
        // Tell the reopen thread a future is waiting on this
        // generation so it uses the min stale time when refreshing.
        reopenThread.waitForGeneration(gen, 0);
        return new NrtFuture(gen);
    }, directExecutor());//from   ww w  .  j a  v a 2s .  com
}