Example usage for com.google.common.util.concurrent ListenableFuture addListener

List of usage examples for com.google.common.util.concurrent ListenableFuture addListener

Introduction

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

Prototype

void addListener(Runnable listener, Executor executor);

Source Link

Document

Registers a listener to be Executor#execute(Runnable) run on the given executor.

Usage

From source file:io.prestosql.server.TaskResource.java

@GET
@Path("{taskId}/results/{bufferId}/{token}")
@Produces(PRESTO_PAGES)//w  ww.  j  a  v  a2 s .c om
public void getResults(@PathParam("taskId") TaskId taskId, @PathParam("bufferId") OutputBufferId bufferId,
        @PathParam("token") final long token, @HeaderParam(PRESTO_MAX_SIZE) DataSize maxSize,
        @Suspended AsyncResponse asyncResponse) {
    requireNonNull(taskId, "taskId is null");
    requireNonNull(bufferId, "bufferId is null");

    long start = System.nanoTime();
    ListenableFuture<BufferResult> bufferResultFuture = taskManager.getTaskResults(taskId, bufferId, token,
            maxSize);
    Duration waitTime = randomizeWaitTime(DEFAULT_MAX_WAIT_TIME);
    bufferResultFuture = addTimeout(bufferResultFuture,
            () -> BufferResult.emptyResults(taskManager.getTaskInstanceId(taskId), token, false), waitTime,
            timeoutExecutor);

    ListenableFuture<Response> responseFuture = Futures.transform(bufferResultFuture, result -> {
        List<SerializedPage> serializedPages = result.getSerializedPages();

        GenericEntity<?> entity = null;
        Status status;
        if (serializedPages.isEmpty()) {
            status = Status.NO_CONTENT;
        } else {
            entity = new GenericEntity<>(serializedPages, new TypeToken<List<Page>>() {
            }.getType());
            status = Status.OK;
        }

        return Response.status(status).entity(entity)
                .header(PRESTO_TASK_INSTANCE_ID, result.getTaskInstanceId())
                .header(PRESTO_PAGE_TOKEN, result.getToken())
                .header(PRESTO_PAGE_NEXT_TOKEN, result.getNextToken())
                .header(PRESTO_BUFFER_COMPLETE, result.isBufferComplete()).build();
    }, directExecutor());

    // For hard timeout, add an additional time to max wait for thread scheduling contention and GC
    Duration timeout = new Duration(waitTime.toMillis() + ADDITIONAL_WAIT_TIME.toMillis(), MILLISECONDS);
    bindAsyncResponse(asyncResponse, responseFuture, responseExecutor).withTimeout(timeout,
            Response.status(Status.NO_CONTENT)
                    .header(PRESTO_TASK_INSTANCE_ID, taskManager.getTaskInstanceId(taskId))
                    .header(PRESTO_PAGE_TOKEN, token).header(PRESTO_PAGE_NEXT_TOKEN, token)
                    .header(PRESTO_BUFFER_COMPLETE, false).build());

    responseFuture.addListener(() -> readFromOutputBufferTime.add(Duration.nanosSince(start)),
            directExecutor());
    asyncResponse
            .register((CompletionCallback) throwable -> resultsRequestTime.add(Duration.nanosSince(start)));
}

From source file:org.apache.qpid.server.virtualhost.AbstractVirtualHost.java

private void postCreateDefaultExchangeTasks() {
    if (getContextValue(Boolean.class, USE_ASYNC_RECOVERY)) {
        _messageStoreRecoverer = new AsynchronousMessageStoreRecoverer();
    } else {/*from ww w .  j  a v a 2  s  . c  o m*/
        _messageStoreRecoverer = new SynchronousMessageStoreRecoverer();
    }

    // propagate any exception thrown during recovery into HouseKeepingTaskExecutor to handle them accordingly
    // TODO if message recovery fails we ought to be transitioning the VH into ERROR and releasing the thread-pools etc.
    final ListenableFuture<Void> recoveryResult = _messageStoreRecoverer.recover(this);
    recoveryResult.addListener(new Runnable() {
        @Override
        public void run() {
            Futures.getUnchecked(recoveryResult);
        }
    }, _houseKeepingTaskExecutor);

    State finalState = State.ERRORED;
    try {
        initialiseHouseKeeping(getHousekeepingCheckPeriod());
        finalState = State.ACTIVE;
        _acceptsConnections.set(true);
    } finally {
        setState(finalState);
        reportIfError(getState());
    }
}

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.
 *//* ww  w  .j  ava2  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: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));
    }/*from  w  w  w  .j a  v a2s .  co  m*/

    // 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.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()));
    }//  w  ww. j  a  va 2  s . c  o 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);
}