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

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

Introduction

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

Prototype

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

Source Link

Document

Returns a new ListenableFuture whose result is the product of applying the given Function to the result of the given Future .

Usage

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

@GET
@Path("{taskId}/results/{bufferId}/{token}")
@Produces(PRESTO_PAGES)/*from  w w  w.  j av  a 2 s  .  c o m*/
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:com.facebook.buck.artifact_cache.SQLiteArtifactCache.java

@Override
public ListenableFuture<Void> store(ArtifactInfo info, BorrowablePath content) {
    if (!getCacheReadMode().isWritable()) {
        return Futures.immediateFuture(null);
    }// w w  w  .  ja va 2s .c om

    ListenableFuture<Void> metadataResult = Futures.immediateFuture(null);
    if (!info.getMetadata().isEmpty()) {
        metadataResult = storeMetadata(info);
    }

    ListenableFuture<Void> contentResult = Futures.immediateFuture(null);
    if (!info.getMetadata().containsKey(TwoLevelArtifactCacheDecorator.METADATA_KEY)) {
        contentResult = storeContent(info.getRuleKeys(), content);
    }

    return Futures.transform(Futures.allAsList(metadataResult, contentResult), Functions.constant(null),
            MoreExecutors.directExecutor());
}

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

@Override
@SuppressWarnings("CheckReturnValue")
public void close() {
    Futures.transform(Futures.allAsList(remoteCacheContainsFutures.values()), remoteContainsResults -> {
        LOG.info(/*from  w w w  . j  ava2  s.c om*/
                "Hit [%d out of %d] targets checked in the remote cache. "
                        + "[%d] targets were uploaded from the local cache.",
                remoteContainsResults.stream().filter(r -> r).count(), remoteContainsResults.size(),
                localUploadFutures.size());
        return null;
    }, MoreExecutors.directExecutor());
}

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

private ListenableFuture<RemoteExecutionActionInfo> uploadInputs(BuildTarget buildTarget,
        RemoteExecutionActionInfo actionInfo) throws Exception {
    Objects.requireNonNull(actionInfo);
    ImmutableMap<Digest, UploadDataSupplier> requiredData = actionInfo.getRequiredData();
    long totalInputSizeBytes = 0;
    for (Digest digest : requiredData.keySet()) {
        totalInputSizeBytes += digest.getSize();
    }//from   www.  jav  a2s . c  om
    if (maxInputSizeBytes.isPresent() && maxInputSizeBytes.getAsInt() < totalInputSizeBytes) {
        throw new RuntimeException("Max file size exceeded for Remote Execution, action contains: "
                + totalInputSizeBytes + " bytes, max allowed: " + maxInputSizeBytes.getAsInt());
    }
    Digest actionDigest = actionInfo.getActionDigest();
    Scope uploadingInputsScope = RemoteExecutionActionEvent.sendEvent(eventBus, State.UPLOADING_INPUTS,
            buildTarget, Optional.of(actionDigest));
    ListenableFuture<Void> inputsUploadedFuture = executionClients.getContentAddressedStorage()
            .addMissing(requiredData);
    return Futures.transform(inputsUploadedFuture, ignored -> {
        uploadingInputsScope.close();
        // The actionInfo may be very large, so explicitly clear out the unneeded parts.
        // actionInfo.getRequiredData() in particular may be very, very large and is unneeded once
        // uploading has completed.
        return actionInfo.withRequiredData(ImmutableMap.of());
    }, MoreExecutors.directExecutor());
}

From source file:org.jclouds.atmos.internal.StubAtmosAsyncClient.java

@Override
public ListenableFuture<AtmosObject> readFile(String path, GetOptions... options) {
    String container = path.substring(0, path.indexOf('/'));
    String blobName = path.substring(path.indexOf('/') + 1);
    org.jclouds.blobstore.options.GetOptions getOptions = httpGetOptionsConverter.apply(options);
    return Futures.transform(blobStore.getBlob(container, blobName, getOptions), blob2Object, userExecutor);
}

From source file:org.opendaylight.groupbasedpolicy.endpoint.AbstractEndpointRegistry.java

@Override
public Future<RpcResult<Void>> unsetEndpointGroupConditions(UnsetEndpointGroupConditionsInput input) {
    WriteTransaction t = dataProvider.newWriteOnlyTransaction();

    ConditionMappingKey key = new ConditionMappingKey(input.getEndpointGroup());

    for (EndpointGroupCondition condition : input.getEndpointGroupCondition()) {
        EndpointGroupConditionKey ckey = new EndpointGroupConditionKey(condition.getCondition());
        InstanceIdentifier<EndpointGroupCondition> iid = InstanceIdentifier.builder(Endpoints.class)
                .child(ConditionMapping.class, key).child(EndpointGroupCondition.class, ckey).build();

        t.delete(LogicalDatastoreType.OPERATIONAL, iid);
    }//from w  w w  .j a  va  2  s  .  c o  m

    ListenableFuture<Void> r = t.submit();
    return Futures.transform(r, futureTrans, executor);
}

From source file:com.facebook.presto.server.protocol.Query.java

public synchronized ListenableFuture<QueryResults> waitForResults(OptionalLong token, UriInfo uriInfo,
        String scheme, Duration wait, DataSize targetResultSize) {
    // before waiting, check if this request has already been processed and cached
    if (token.isPresent()) {
        Optional<QueryResults> cachedResult = getCachedResult(token.getAsLong(), uriInfo);
        if (cachedResult.isPresent()) {
            return immediateFuture(cachedResult.get());
        }/* w w  w .ja v a2 s.c o m*/
    }

    // wait for a results data or query to finish, up to the wait timeout
    ListenableFuture<?> futureStateChange = addTimeout(getFutureStateChange(), () -> null, wait,
            timeoutExecutor);

    // when state changes, fetch the next result
    return Futures.transform(futureStateChange,
            ignored -> getNextResult(token, uriInfo, scheme, targetResultSize), resultsProcessorExecutor);
}

From source file:com.facebook.buck.distributed.build_slave.DistBuildSlaveExecutor.java

private ListenableFuture<BuildExecutor> createBuilder(BuildExecutorArgs builderArgs,
        ExecutionContext executionContext) {
    return Futures.transform(initializer.getDelegateAndGraphs(),
            delegateAndGraphs -> new LocalBuildExecutor(builderArgs, executionContext,
                    delegateAndGraphs.getActionGraphAndBuilder(),
                    delegateAndGraphs.getCachingBuildEngineDelegate(), args.getExecutorService(), KEEP_GOING,
                    true, false, args.getRuleKeyCacheScope(), Optional.empty(), Optional.empty(),
                    // Only the client side build needs to synchronize, not the slave.
                    // (as the co-ordinator synchronizes artifacts between slaves).
                    new NoOpRemoteBuildRuleCompletionWaiter(), args.getMetadataProvider(),
                    args.getUnconfiguredBuildTargetFactory(), EmptyTargetConfiguration.INSTANCE,
                    args.getTargetConfigurationSerializer()),
            args.getExecutorService());//from   w  w w.j a v  a 2 s  .com
}

From source file:com.facebook.buck.parser.TargetSpecResolver.java

private <T extends HasBuildTarget> void handleTargetNodeSpec(FlavorEnhancer<T> flavorEnhancer,
        TargetNodeProviderForSpecResolver<T> targetNodeProvider,
        TargetNodeFilterForSpecResolver<T> targetNodeFilter,
        List<ListenableFuture<Map.Entry<Integer, ImmutableSet<BuildTarget>>>> targetFutures, Cell cell,
        Path buildFile, TargetConfiguration targetConfiguration, int index, TargetNodeSpec spec) {
    if (spec instanceof BuildTargetSpec) {
        BuildTargetSpec buildTargetSpec = (BuildTargetSpec) spec;
        targetFutures.add(Futures.transform(targetNodeProvider.getTargetNodeJob(
                buildTargetSpec.getUnconfiguredBuildTarget().configure(EmptyTargetConfiguration.INSTANCE)),
                node -> {//  w  ww  .  ja  v a 2  s .c  om
                    ImmutableSet<BuildTarget> buildTargets = applySpecFilter(spec, ImmutableList.of(node),
                            flavorEnhancer, targetNodeFilter);
                    Preconditions.checkState(buildTargets.size() == 1,
                            "BuildTargetSpec %s filter discarded target %s, but was not supposed to.", spec,
                            node.getBuildTarget());
                    return new AbstractMap.SimpleEntry<>(index, buildTargets);
                }, MoreExecutors.directExecutor()));
    } else {
        // Build up a list of all target nodes from the build file.
        targetFutures.add(
                Futures.transform(targetNodeProvider.getAllTargetNodesJob(cell, buildFile, targetConfiguration),
                        nodes -> new AbstractMap.SimpleEntry<>(index,
                                applySpecFilter(spec, nodes, flavorEnhancer, targetNodeFilter)),
                        MoreExecutors.directExecutor()));
    }
}

From source file:org.hawkular.metrics.core.service.MetricsServiceImpl.java

void loadDataRetentions() {
    List<String> tenantIds = loadTenantIds();
    CountDownLatch latch = new CountDownLatch(tenantIds.size() * 2);
    for (String tenantId : tenantIds) {
        DataRetentionsMapper gaugeMapper = new DataRetentionsMapper(tenantId, GAUGE);
        DataRetentionsMapper availMapper = new DataRetentionsMapper(tenantId, AVAILABILITY);
        ResultSetFuture gaugeFuture = dataAccess.findDataRetentions(tenantId, GAUGE);
        ResultSetFuture availabilityFuture = dataAccess.findDataRetentions(tenantId, AVAILABILITY);
        ListenableFuture<Set<Retention>> gaugeRetentions = Futures.transform(gaugeFuture, gaugeMapper,
                metricsTasks);/*from   w  ww .  ja v a 2s  .  c o  m*/
        ListenableFuture<Set<Retention>> availabilityRetentions = Futures.transform(availabilityFuture,
                availMapper, metricsTasks);
        Futures.addCallback(gaugeRetentions, new DataRetentionsLoadedCallback(tenantId, GAUGE, latch));
        Futures.addCallback(availabilityRetentions,
                new DataRetentionsLoadedCallback(tenantId, AVAILABILITY, latch));
    }
    try {
        latch.await();
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}