Example usage for com.google.common.util.concurrent MoreExecutors directExecutor

List of usage examples for com.google.common.util.concurrent MoreExecutors directExecutor

Introduction

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

Prototype

public static Executor directExecutor() 

Source Link

Document

Returns an Executor that runs each task in the thread that invokes Executor#execute execute , as in CallerRunsPolicy .

Usage

From source file:com.google.cloud.spanner.SessionPool.java

/**
 * Close all the sessions. Once this method is invoked {@link #getReadSession()} and {@link
 * #getReadWriteSession()} will start throwing {@code IllegalStateException}. The returned future
 * blocks till all the sessions created in this pool have been closed.
 *///from  ww w . jav a  2  s .c  o  m
ListenableFuture<Void> closeAsync() {
    ListenableFuture<Void> retFuture = null;
    synchronized (lock) {
        if (closureFuture != null) {
            throw new IllegalStateException("Close has already been invoked");
        }
        // Fail all pending waiters.
        Waiter waiter = readWaiters.poll();
        while (waiter != null) {
            waiter.put(newSpannerException(ErrorCode.INTERNAL, "Client has been closed"));
            waiter = readWaiters.poll();
        }
        waiter = readWriteWaiters.poll();
        while (waiter != null) {
            waiter.put(newSpannerException(ErrorCode.INTERNAL, "Client has been closed"));
            waiter = readWriteWaiters.poll();
        }
        closureFuture = SettableFuture.create();
        retFuture = closureFuture;
        pendingClosure = totalSessions() + numSessionsBeingCreated + 1 /* For pool maintenance thread */;

        poolMaintainer.close();
        readSessions.clear();
        writePreparedSessions.clear();
        for (final PooledSession session : ImmutableList.copyOf(allSessions)) {
            if (session.leakedException != null) {
                logger.log(Level.WARNING, "Leaked session", session.leakedException);
            }
            if (session.state != SessionState.CLOSING) {
                closeSessionAsync(session);
            }
        }
    }
    retFuture.addListener(new Runnable() {
        @Override
        public void run() {
            executorFactory.release(executor);
        }
    }, MoreExecutors.directExecutor());

    return retFuture;
}

From source file:org.bitcoinj.protocols.channels.PaymentChannelClient.java

/**
 * Increments the total value which we pay the server. Note that the amount of money sent may not be the same as the
 * amount of money actually requested. It can be larger if the amount left over in the channel would be too small to
 * be accepted by the Bitcoin network. ValueOutOfRangeException will be thrown, however, if there's not enough money
 * left in the channel to make the payment at all. Only one payment can be in-flight at once. You have to ensure
 * you wait for the previous increase payment future to complete before incrementing the payment again.
 *
 * @param size How many satoshis to increment the payment by (note: not the new total).
 * @param info Information about this update, used to extend this protocol.
 * @param userKey Key derived from a user password, needed for any signing when the wallet is encrypted.
 *                The wallet KeyCrypter is assumed.
 * @return a future that completes when the server acknowledges receipt and acceptance of the payment.
 * @throws ValueOutOfRangeException If the size is negative or would pay more than this channel's total value
 *                                  ({@link PaymentChannelClientConnection#state()}.getTotalValue())
 * @throws IllegalStateException If the channel has been closed or is not yet open
 *                               (see {@link PaymentChannelClientConnection#getChannelOpenFuture()} for the second)
 * @throws ECKey.KeyIsEncryptedException If the keys are encrypted and no AES key has been provided,
 *//*from www. j  a  v a 2s.c  om*/
@Override
public ListenableFuture<PaymentIncrementAck> incrementPayment(Coin size, @Nullable ByteString info,
        @Nullable KeyParameter userKey)
        throws ValueOutOfRangeException, IllegalStateException, ECKey.KeyIsEncryptedException {
    lock.lock();
    try {
        if (state() == null || !connectionOpen || step != InitStep.CHANNEL_OPEN)
            throw new IllegalStateException("Channel is not fully initialized/has already been closed");
        if (increasePaymentFuture != null)
            throw new IllegalStateException(
                    "Already incrementing paying, wait for previous payment to complete.");
        if (wallet.isEncrypted() && userKey == null)
            throw new ECKey.KeyIsEncryptedException();

        PaymentChannelV1ClientState.IncrementedPayment payment = state().incrementPaymentBy(size, userKey);
        Protos.UpdatePayment.Builder updatePaymentBuilder = Protos.UpdatePayment.newBuilder()
                .setSignature(ByteString.copyFrom(payment.signature.encodeToBitcoin()))
                .setClientChangeValue(state.getValueRefunded().value);
        if (info != null)
            updatePaymentBuilder.setInfo(info);

        increasePaymentFuture = SettableFuture.create();
        increasePaymentFuture.addListener(new Runnable() {
            @Override
            public void run() {
                lock.lock();
                increasePaymentFuture = null;
                lock.unlock();
            }
        }, MoreExecutors.directExecutor());

        conn.sendToServer(Protos.TwoWayChannelMessage.newBuilder().setUpdatePayment(updatePaymentBuilder)
                .setType(Protos.TwoWayChannelMessage.MessageType.UPDATE_PAYMENT).build());
        lastPaymentActualAmount = payment.amount;
        return increasePaymentFuture;
    } finally {
        lock.unlock();
    }
}

From source file:com.facebook.buck.core.build.engine.impl.CachingBuildRuleBuilder.java

private ListenableFuture<Optional<BuildResult>> buildLocally(final CacheResult cacheResult,
        final ListeningExecutorService service) {
    @SuppressWarnings("PMD.PrematureDeclaration")
    long start = System.currentTimeMillis();

    onRuleAboutToBeBuilt();//from  w w  w .j a va2s.  c om

    BuildRuleSteps<RulePipelineState> buildRuleSteps = new BuildRuleSteps<>(cacheResult, null);
    BuildStrategyContext strategyContext = new BuildStrategyContext() {
        @Override
        public ListenableFuture<Optional<BuildResult>> runWithDefaultBehavior() {
            if (SupportsPipelining.isSupported(rule) && ((SupportsPipelining<?>) rule).useRulePipelining()) {
                return pipelinesRunner.runPipelineStartingAt(buildRuleBuildContext,
                        (SupportsPipelining<?>) rule, service);
            } else {
                buildRuleSteps.runWithDefaultExecutor();
                return buildRuleSteps.future;
            }
        }

        @Override
        public ListeningExecutorService getExecutorService() {
            return serviceByAdjustingDefaultWeightsTo(CachingBuildEngine.SCHEDULING_MORE_WORK_RESOURCE_AMOUNTS);
        }

        @Override
        public BuildResult createBuildResult(BuildRuleSuccessType successType) {
            return success(successType, cacheResult);
        }

        @Override
        public BuildResult createCancelledResult(Throwable throwable) {
            return canceled(throwable);
        }

        @Override
        public ExecutionContext getExecutionContext() {
            return buildRuleSteps.ruleExecutionContext;
        }

        @Override
        public Scope buildRuleScope() {
            return CachingBuildRuleBuilder.this.buildRuleScope();
        }

        @Override
        public BuildContext getBuildRuleBuildContext() {
            return buildRuleBuildContext;
        }

        @Override
        public BuildableContext getBuildableContext() {
            return buildableContext;
        }
    };

    ListenableFuture<Optional<BuildResult>> future;
    if (customBuildRuleStrategy.isPresent() && customBuildRuleStrategy.get().canBuild(rule)) {
        this.strategyResult = customBuildRuleStrategy.get().build(rule, strategyContext);
        future = strategyResult.getBuildResult();
    } else {
        future = Futures.submitAsync(strategyContext::runWithDefaultBehavior, service);
    }

    return Futures.transform(future, p -> {
        buildTimestampsMillis = new Pair<>(start, System.currentTimeMillis());
        return p;
    }, MoreExecutors.directExecutor());
}

From source file:com.facebook.buck.core.build.engine.impl.CachingBuildRuleBuilder.java

private ListenableFuture<Optional<BuildResult>> checkManifestBasedCaches() throws IOException {
    Optional<RuleKeyAndInputs> manifestKeyAndInputs = manifestBasedKeySupplier.get();
    if (!manifestKeyAndInputs.isPresent()) {
        return Futures.immediateFuture(Optional.empty());
    }/*from   w w  w .  j  a  va  2  s. c o m*/
    getBuildInfoRecorder().addBuildMetadata(BuildInfo.MetadataKey.MANIFEST_KEY,
            manifestKeyAndInputs.get().getRuleKey().toString());
    try (Scope ignored = LeafEvents.scope(eventBus, "checking_cache_depfile_based")) {
        long start = System.currentTimeMillis();
        return Futures.transform(
                manifestRuleKeyManager.performManifestBasedCacheFetch(manifestKeyAndInputs.get()),
                (@Nonnull ManifestFetchResult result) -> {
                    buildRuleScopeManager.setManifestFetchResult(result);
                    manifestRuleKeyCacheCheckTimestampsMillis = new Pair<>(start, System.currentTimeMillis());
                    if (!result.getRuleCacheResult().isPresent()) {
                        return Optional.empty();
                    }
                    if (!result.getRuleCacheResult().get().getType().isSuccess()) {
                        return Optional.empty();
                    }
                    return Optional.of(success(BuildRuleSuccessType.FETCHED_FROM_CACHE_MANIFEST_BASED,
                            result.getRuleCacheResult().get()));
                }, MoreExecutors.directExecutor());
    }
}

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

private ListenableFuture<BuildResult> buildOrFetchFromCache() {
    // If we've already seen a failure, exit early.
    if (!buildContext.isKeepGoing() && buildRuleBuilderDelegate.getFirstFailure() != null) {
        return Futures.immediateFuture(BuildResult.canceled(rule, buildRuleBuilderDelegate.getFirstFailure()));
    }//from w w  w  .j  a v  a2s.  co m

    // 1. Check if it's already built.
    try (Scope scope = BuildRuleEvent.resumeSuspendScope(buildContext.getEventBus(), rule,
            buildRuleDurationTracker, ruleKeyFactories.getDefaultRuleKeyFactory())) {
        Optional<BuildResult> buildResult = checkMatchingLocalKey();
        if (buildResult.isPresent()) {
            return Futures.immediateFuture(buildResult.get());
        }
    }

    AtomicReference<CacheResult> rulekeyCacheResult = new AtomicReference<>();
    ListenableFuture<Optional<BuildResult>> buildResultFuture;

    // 2. Rule key cache lookup.
    buildResultFuture =
            // TODO(cjhopman): This should follow the same, simple pattern as everything else. With a
            // large ui.thread_line_limit, SuperConsole tries to redraw more lines than are available.
            // These cache threads make it more likely to hit that problem when SuperConsole is aware
            // of them.
            cacheActivityService.withDefaultAmounts(CachingBuildEngine.CACHE_CHECK_RESOURCE_AMOUNTS)
                    .submit(() -> {
                        if (!buildRuleBuilderDelegate.shouldKeepGoing(buildContext)) {
                            Preconditions.checkNotNull(buildRuleBuilderDelegate.getFirstFailure());
                            return Optional
                                    .of(BuildResult.canceled(rule, buildRuleBuilderDelegate.getFirstFailure()));
                        }
                        CacheResult cacheResult = performRuleKeyCacheCheck();
                        rulekeyCacheResult.set(cacheResult);
                        return getBuildResultForRuleKeyCacheResult(cacheResult);
                    });

    // 3. Build deps.
    buildResultFuture = transformBuildResultAsyncIfNotPresent(buildResultFuture, () -> {
        if (SupportsPipelining.isSupported(rule)) {
            addToPipelinesRunner((SupportsPipelining<?>) rule,
                    Preconditions.checkNotNull(rulekeyCacheResult.get()));
        }

        return Futures.transformAsync(
                buildRuleBuilderDelegate.getDepResults(rule, buildContext, executionContext),
                (depResults) -> handleDepsResults(depResults),
                serviceByAdjustingDefaultWeightsTo(CachingBuildEngine.SCHEDULING_MORE_WORK_RESOURCE_AMOUNTS));
    });

    // 4. Return to the current rule and check if it was (or is being) built in a pipeline with
    // one of its dependencies
    if (SupportsPipelining.isSupported(rule)) {
        buildResultFuture = transformBuildResultAsyncIfNotPresent(buildResultFuture, () -> {
            SupportsPipelining<?> pipelinedRule = (SupportsPipelining<?>) rule;
            return pipelinesRunner.runningPipelinesContainRule(pipelinedRule)
                    ? pipelinesRunner.getFuture(pipelinedRule)
                    : Futures.immediateFuture(Optional.empty());
        });
    }

    // 5. Return to the current rule and check caches to see if we can avoid building
    if (SupportsInputBasedRuleKey.isSupported(rule)) {
        buildResultFuture = transformBuildResultIfNotPresent(buildResultFuture, this::checkInputBasedCaches,
                serviceByAdjustingDefaultWeightsTo(CachingBuildEngine.CACHE_CHECK_RESOURCE_AMOUNTS));
    }

    // 6. Then check if the depfile matches.
    if (useDependencyFileRuleKey()) {
        buildResultFuture = transformBuildResultIfNotPresent(buildResultFuture, this::checkMatchingDepfile,
                serviceByAdjustingDefaultWeightsTo(CachingBuildEngine.CACHE_CHECK_RESOURCE_AMOUNTS));
    }

    // 7. Check for a manifest-based cache hit.
    if (useManifestCaching()) {
        buildResultFuture = transformBuildResultIfNotPresent(buildResultFuture, this::checkManifestBasedCaches,
                serviceByAdjustingDefaultWeightsTo(CachingBuildEngine.CACHE_CHECK_RESOURCE_AMOUNTS));
    }

    // 8. Fail if populating the cache and cache lookups failed.
    if (buildMode == CachingBuildEngine.BuildMode.POPULATE_FROM_REMOTE_CACHE) {
        buildResultFuture = transformBuildResultIfNotPresent(buildResultFuture, () -> {
            LOG.info("Cannot populate cache for " + rule.getBuildTarget().getFullyQualifiedName());
            return Optional.of(BuildResult.canceled(rule, new HumanReadableException(
                    "Skipping %s: in cache population mode local builds are disabled", rule)));
        }, MoreExecutors.newDirectExecutorService());
    }

    // 9. Build the current rule locally, if we have to.
    buildResultFuture = transformBuildResultAsyncIfNotPresent(buildResultFuture,
            () -> buildLocally(Preconditions.checkNotNull(rulekeyCacheResult.get()), service
                    // This needs to adjust the default amounts even in the non-resource-aware scheduling
                    // case so that RuleScheduleInfo works correctly.
                    .withDefaultAmounts(getRuleResourceAmounts())));

    if (SupportsPipelining.isSupported(rule)) {
        buildResultFuture.addListener(() -> pipelinesRunner.removeRule((SupportsPipelining<?>) rule),
                MoreExecutors.directExecutor());
    }

    // Unwrap the result.
    return Futures.transform(buildResultFuture, Optional::get);
}

From source file:com.facebook.buck.core.build.engine.impl.CachingBuildRuleBuilder.java

private ListenableFuture<Optional<BuildResult>> checkInputBasedCaches() throws IOException {
    long start = System.currentTimeMillis();
    return Futures.transform(inputBasedRuleKeyManager.checkInputBasedCaches(),
            optionalResult -> optionalResult.map(result -> {
                inputRuleKeyCacheCheckTimestampsMillis = new Pair<>(start, System.currentTimeMillis());
                return success(result.getFirst(), result.getSecond());
            }), MoreExecutors.directExecutor());
}

From source file:com.facebook.buck.core.build.engine.impl.CachingBuildRuleBuilder.java

private ListenableFuture<BuildResult> buildOrFetchFromCache() {
    // If we've already seen a failure, exit early.
    if (!shouldKeepGoing()) {
        return Futures.immediateFuture(canceled(firstFailure));
    }//w ww .  jav  a 2  s  .  c om

    // 1. Check if it's already built.
    try (Scope ignored = buildRuleScope()) {
        Optional<BuildResult> buildResult = checkMatchingLocalKey();
        if (buildResult.isPresent()) {
            return Futures.immediateFuture(buildResult.get());
        }
    }

    AtomicReference<CacheResult> rulekeyCacheResult = new AtomicReference<>();
    ListenableFuture<Optional<BuildResult>> buildResultFuture;

    // 2. Rule key cache lookup.
    buildResultFuture =
            // TODO(cjhopman): This should follow the same, simple pattern as everything else. With a
            // large ui.thread_line_limit, SuperConsole tries to redraw more lines than are available.
            // These cache threads make it more likely to hit that problem when SuperConsole is aware
            // of them.
            Futures.transform(performRuleKeyCacheCheck(/* cacheHitExpected */ false), cacheResult -> {
                Objects.requireNonNull(cacheResult);
                cacheResult.getType().verifyValidFinalType();
                rulekeyCacheResult.set(cacheResult);
                return getBuildResultForRuleKeyCacheResult(cacheResult);
            }, MoreExecutors.directExecutor());

    // 3. Before unlocking dependencies, ensure build rule hasn't started remotely.
    buildResultFuture = attemptDistributedBuildSynchronization(buildResultFuture, rulekeyCacheResult);

    // 4. Build deps.
    buildResultFuture = transformBuildResultAsyncIfNotPresent(buildResultFuture, () -> {
        if (SupportsPipelining.isSupported(rule)) {
            addToPipelinesRunner((SupportsPipelining<?>) rule,
                    Objects.requireNonNull(rulekeyCacheResult.get()));
        }

        return Futures.transformAsync(buildRuleBuilderDelegate.getDepResults(rule, executionContext),
                (depResults) -> handleDepsResults(depResults),
                serviceByAdjustingDefaultWeightsTo(CachingBuildEngine.SCHEDULING_MORE_WORK_RESOURCE_AMOUNTS));
    });

    // 5. Return to the current rule and check if it was (or is being) built in a pipeline with
    // one of its dependencies
    if (SupportsPipelining.isSupported(rule)) {
        buildResultFuture = transformBuildResultAsyncIfNotPresent(buildResultFuture, () -> {
            SupportsPipelining<?> pipelinedRule = (SupportsPipelining<?>) rule;
            return pipelinesRunner.runningPipelinesContainRule(pipelinedRule)
                    ? pipelinesRunner.getFuture(pipelinedRule)
                    : Futures.immediateFuture(Optional.empty());
        });
    }

    // 6. Return to the current rule and check caches to see if we can avoid building
    if (SupportsInputBasedRuleKey.isSupported(rule)) {
        buildResultFuture = transformBuildResultAsyncIfNotPresent(buildResultFuture,
                this::checkInputBasedCaches);
    }

    // 7. Then check if the depfile matches.
    if (dependencyFileRuleKeyManager.useDependencyFileRuleKey()) {
        buildResultFuture = transformBuildResultIfNotPresent(buildResultFuture, this::checkMatchingDepfile,
                serviceByAdjustingDefaultWeightsTo(CachingBuildEngine.CACHE_CHECK_RESOURCE_AMOUNTS));
    }

    // 8. Check for a manifest-based cache hit.
    if (manifestRuleKeyManager.useManifestCaching()) {
        buildResultFuture = transformBuildResultAsyncIfNotPresent(buildResultFuture,
                this::checkManifestBasedCaches);
    }

    // 9. Fail if populating the cache and cache lookups failed.
    if (buildMode == BuildType.POPULATE_FROM_REMOTE_CACHE) {
        buildResultFuture = transformBuildResultIfNotPresent(buildResultFuture, () -> {
            LOG.info("Cannot populate cache for " + rule.getBuildTarget().getFullyQualifiedName());
            return Optional.of(canceled(new HumanReadableException(
                    "Skipping %s: in cache population mode local builds are disabled", rule)));
        }, MoreExecutors.newDirectExecutorService());
    }

    // 10. Before building locally, do a final check that rule hasn't started building remotely.
    // (as time has passed due to building of dependencies)
    buildResultFuture = attemptDistributedBuildSynchronization(buildResultFuture, rulekeyCacheResult);

    // 11. Build the current rule locally, if we have to.
    buildResultFuture = transformBuildResultAsyncIfNotPresent(buildResultFuture,
            () -> buildLocally(Objects.requireNonNull(rulekeyCacheResult.get()), service
                    // This needs to adjust the default amounts even in the non-resource-aware
                    // scheduling case so that RuleScheduleInfo works correctly.
                    .withDefaultAmounts(getRuleResourceAmounts())));

    if (SupportsPipelining.isSupported(rule)) {
        buildResultFuture.addListener(() -> pipelinesRunner.removeRule((SupportsPipelining<?>) rule),
                MoreExecutors.directExecutor());
    }

    // Unwrap the result.
    return Futures.transform(buildResultFuture, Optional::get, MoreExecutors.directExecutor());
}

From source file:com.facebook.buck.core.build.engine.impl.CachingBuildRuleBuilder.java

private ListenableFuture<Optional<BuildResult>> attemptDistributedBuildSynchronization(
        ListenableFuture<Optional<BuildResult>> buildResultFuture,
        AtomicReference<CacheResult> rulekeyCacheResult) {
    // Check if rule has started being built remotely (i.e. by Stampede). If it has, or if we are
    // in a 'always wait mode' distributed build, then wait, otherwise proceed immediately.
    return transformBuildResultAsyncIfNotPresent(buildResultFuture, () -> {
        if (!remoteBuildRuleCompletionWaiter
                .shouldWaitForRemoteCompletionOfBuildRule(rule.getFullyQualifiedName())) {
            // Start building locally right away, as remote build hasn't started yet.
            // Note: this code path is also used for regular local Buck builds, these use
            // NoOpRemoteBuildRuleCompletionWaiter that always returns false for above call.
            return Futures.immediateFuture(Optional.empty());
        }// w w  w .j ava  2s  .c o  m

        // Once remote build has finished, download artifact from cache using default key
        return Futures.transform(remoteBuildRuleCompletionWaiter.waitForBuildRuleToAppearInCache(rule,
                () -> performRuleKeyCacheCheck(/* cacheHitExpected */ true)), cacheResult -> {
                    rulekeyCacheResult.set(cacheResult);
                    return getBuildResultForRuleKeyCacheResult(cacheResult);
                }, MoreExecutors.directExecutor());
    });
}

From source file:com.facebook.buck.core.build.engine.impl.CachingBuildRuleBuilder.java

private ListenableFuture<CacheResult> performRuleKeyCacheCheck(boolean cacheHitExpected) {
    long cacheRequestTimestampMillis = System.currentTimeMillis();
    return Futures.transform(buildCacheArtifactFetcher
            .tryToFetchArtifactFromBuildCacheAndOverlayOnTopOfProjectFilesystem(defaultKey, artifactCache,
                    // TODO(simons): This should be a shared between all tests, not one per cell
                    rule.getProjectFilesystem()),
            cacheResult -> {//from  www  . ja  v a  2 s. c  o m
                RuleKeyCacheResult ruleKeyCacheResult = RuleKeyCacheResult.builder()
                        .setBuildTarget(rule.getFullyQualifiedName()).setRuleKey(defaultKey.toString())
                        .setRuleKeyType(RuleKeyType.DEFAULT).setCacheResult(cacheResult.getType())
                        .setRequestTimestampMillis(cacheRequestTimestampMillis)
                        .setTwoLevelContentHashKey(cacheResult.twoLevelContentHashKey()).build();
                ruleKeyCacheCheckTimestampsMillis = new Pair<>(cacheRequestTimestampMillis,
                        System.currentTimeMillis());
                eventBus.post(new RuleKeyCacheResultEvent(ruleKeyCacheResult, cacheHitExpected));
                return cacheResult;
            }, MoreExecutors.directExecutor());
}

From source file:com.facebook.buck.core.build.engine.impl.CachingBuildRuleBuilder.java

private ListenableFuture<Optional<BuildResult>> transformBuildResultAsyncIfNotPresent(
        ListenableFuture<Optional<BuildResult>> future,
        Callable<ListenableFuture<Optional<BuildResult>>> function) {
    // Immediately (i.e. without posting a task), returns the current result if it's already present
    // or a cancelled result if we've already seen a failure.
    return Futures.transformAsync(future, (result) -> {
        if (result.isPresent()) {
            return Futures.immediateFuture(result);
        }/* w w  w .j  av a 2 s .c om*/
        if (!shouldKeepGoing()) {
            return Futures.immediateFuture(Optional.of(canceled(firstFailure)));
        }
        return function.call();
    }, MoreExecutors.directExecutor());
}