List of usage examples for com.google.common.util.concurrent Futures transformAsync
public static <I, O> ListenableFuture<O> transformAsync(ListenableFuture<I> input, AsyncFunction<? super I, ? extends O> function, Executor executor)
From source file:org.opendaylight.controller.cluster.sharding.ShardProxyTransaction.java
@Override public ListenableFuture<Void> submit() { LOG.debug("Submitting transaction for shard {}", shardRoot); checkTransactionReadied();// w w w. jav a 2s . c o m final AsyncFunction<Boolean, Void> validateFunction = input -> prepare(); final AsyncFunction<Void, Void> prepareFunction = input -> commit(); // transform validate into prepare final ListenableFuture<Void> prepareFuture = Futures.transformAsync(validate(), validateFunction, MoreExecutors.directExecutor()); // transform prepare into commit and return as submit result return Futures.transformAsync(prepareFuture, prepareFunction, MoreExecutors.directExecutor()); }
From source file:com.orangerhymelabs.helenus.cassandra.AbstractCassandraRepository.java
public ListenableFuture<T> read(Identifier id) { ListenableFuture<ResultSet> rs = submitRead(id); return Futures.transformAsync(rs, new AsyncFunction<ResultSet, T>() { @Override/*from w w w .j av a2s .com*/ public ListenableFuture<T> apply(ResultSet result) { if (result.isExhausted()) { return Futures.immediateFailedFuture(new ItemNotFoundException(id.toString())); } return Futures.immediateFuture(marshalRow(result.one())); } }, MoreExecutors.directExecutor()); }
From source file:com.facebook.buck.rules.UnskippedRulesTracker.java
private ListenableFuture<Void> acquireReference(BuildRule rule) { AtomicInteger referenceCount = ruleReferenceCounts.getUnchecked(rule.getBuildTarget()); int newValue = referenceCount.incrementAndGet(); if (newValue == 1) { // 0 -> 1 transition means that the rule might be used in the future (not skipped for now). unskippedRules.incrementAndGet(); stateChanged.set(true);// w w w .j a v a 2 s. c o m // Add references to all dependencies of the rule. return Futures.transformAsync(ruleDepsCache.get(rule), acquireReferences, executor); } return Futures.immediateFuture(null); }
From source file:com.facebook.buck.distributed.MultiSourceContentsProvider.java
@Override public ListenableFuture<Boolean> materializeFileContentsAsync(BuildJobStateFileHashEntry entry, Path targetAbsPath) {//from w ww .ja v a 2 s. co m return Futures.transformAsync(inlineProvider.materializeFileContentsAsync(entry, targetAbsPath), (inlineSuccess) -> postInlineMaterializationHelper(inlineSuccess, entry, targetAbsPath), executorService); }
From source file:com.orangerhymelabs.helenus.cassandra.AbstractCassandraRepository.java
public ListenableFuture<List<T>> readAll(PreparedStatement statement, Object... parms) { ListenableFuture<ResultSet> future = submitStatement(statement, parms); return Futures.transformAsync(future, new AsyncFunction<ResultSet, List<T>>() { @Override// ww w . j a v a2 s . co m public ListenableFuture<List<T>> apply(ResultSet input) { return Futures.immediateFuture(marshalAll(input)); } }, MoreExecutors.directExecutor()); }
From source file:com.facebook.buck.distributed.build_client.PreBuildPhase.java
/** Run all steps required before the build. */ public Pair<StampedeId, ListenableFuture<Void>> runPreDistBuildLocalStepsAsync( ListeningExecutorService networkExecutorService, ProjectFilesystem projectFilesystem, FileHashCache fileHashCache, BuckEventBus eventBus, BuildId buildId, BuildMode buildMode, MinionRequirements minionRequirements, String repository, String tenantId, ListenableFuture<ParallelRuleKeyCalculator<RuleKey>> localRuleKeyCalculatorFuture) throws IOException, DistBuildRejectedException { ConsoleEventsDispatcher consoleEventsDispatcher = new ConsoleEventsDispatcher(eventBus); distBuildClientStats.startTimer(CREATE_DISTRIBUTED_BUILD); List<String> buildTargets = topLevelTargets.stream().map(BuildTarget::getFullyQualifiedName).sorted() .collect(Collectors.toList()); BuildJob job = distBuildService.createBuild(buildId, buildMode, minionRequirements, repository, tenantId, buildTargets, buildLabel);/*from ww w . j a v a 2 s . c om*/ distBuildClientStats.stopTimer(CREATE_DISTRIBUTED_BUILD); if (job.getBuildLabel() != null) { // Override the build label with the server-side inferred label. this.buildLabel = job.getBuildLabel(); distBuildClientStats.setUserOrInferredBuildLabel(buildLabel); } StampedeId stampedeId = job.getStampedeId(); eventBus.post(new DistBuildCreatedEvent(stampedeId)); LOG.info("Created job. StampedeId = " + stampedeId.getId()); consoleEventsDispatcher.postDistBuildStatusEvent(job, ImmutableList.of(), "SERIALIZING AND UPLOADING DATA"); List<ListenableFuture<?>> asyncJobs = new LinkedList<>(); asyncJobs.add(Futures.transformAsync(asyncJobState, jobState -> { LOG.info("Uploading local changes."); return distBuildService.uploadMissingFilesAsync(distBuildCellIndexer.getLocalFilesystemsByCellIndex(), jobState.fileHashes, distBuildClientStats, networkExecutorService); }, networkExecutorService)); asyncJobs.add(Futures.transform(asyncJobState, jobState -> { LOG.info("Uploading target graph."); try { distBuildService.uploadTargetGraph(jobState, stampedeId, distBuildClientStats); } catch (IOException e) { throw new RuntimeException("Failed to upload target graph with exception.", e); } return null; }, networkExecutorService)); LOG.info("Uploading buck dot-files."); asyncJobs.add(distBuildService.uploadBuckDotFilesAsync(stampedeId, projectFilesystem, fileHashCache, distBuildClientStats, networkExecutorService)); asyncJobs.add(networkExecutorService.submit(() -> { LOG.info("Setting buck version."); try { distBuildService.setBuckVersion(stampedeId, buckVersion, distBuildClientStats); } catch (IOException e) { throw new RuntimeException("Failed to set buck-version with exception.", e); } })); DistBuildConfig distBuildConfig = new DistBuildConfig(buildExecutorArgs.getBuckConfig()); if (distBuildConfig.isUploadFromLocalCacheEnabled()) { asyncJobs.add(Futures.transformAsync(localRuleKeyCalculatorFuture, localRuleKeyCalculator -> { try (ArtifactCacheByBuildRule artifactCache = new DistBuildArtifactCacheImpl( actionAndTargetGraphs.getActionGraphAndBuilder().getActionGraphBuilder(), networkExecutorService, buildExecutorArgs.getArtifactCacheFactory().remoteOnlyInstance(true, false), eventBus, localRuleKeyCalculator, Optional.of(buildExecutorArgs.getArtifactCacheFactory().localOnlyInstance(true, false)))) { return new CacheOptimizedBuildTargetsQueueFactory( actionAndTargetGraphs.getActionGraphAndBuilder().getActionGraphBuilder(), artifactCache, /* isDeepRemoteBuild */ false, localRuleKeyCalculator.getRuleDepsCache(), /* shouldBuildSelectedTargetsLocally */ false) .uploadCriticalNodesFromLocalCache(topLevelTargets, distBuildClientStats); } catch (Exception e) { LOG.error(e, "Failed to create BuildTargetsQueue."); throw new RuntimeException(e); } }, networkExecutorService)); } ListenableFuture<Void> asyncPrep = Futures.transform(Futures.allAsList(asyncJobs), results -> { LOG.info("Finished async preparation of stampede job."); consoleEventsDispatcher.postDistBuildStatusEvent(job, ImmutableList.of(), "STARTING REMOTE BUILD"); // Everything is now setup remotely to run the distributed build. No more local prep. this.distBuildClientStats.stopTimer(LOCAL_PREPARATION); return null; }, MoreExecutors.directExecutor()); return new Pair<StampedeId, ListenableFuture<Void>>(stampedeId, asyncPrep); }
From source file:com.facebook.buck.rules.UnskippedRulesTracker.java
private ListenableFuture<Void> releaseReference(BuildRule rule) { AtomicInteger referenceCount = ruleReferenceCounts.getUnchecked(rule.getBuildTarget()); int newValue = referenceCount.decrementAndGet(); if (newValue == 0) { // 1 -> 0 transition means that the rule can be marked as skipped. unskippedRules.decrementAndGet(); stateChanged.set(true);/*from w w w .j av a 2 s . co m*/ // Remove references from all dependencies of the rule. return Futures.transformAsync(ruleDepsCache.get(rule), releaseReferences, executor); } return Futures.immediateFuture(null); }
From source file:com.facebook.buck.distributed.DistBuildArtifactCacheImpl.java
@Override public synchronized ListenableFuture<BuildRule> uploadFromLocal(BuildRule rule) throws IOException { Preconditions.checkState(localCache.isPresent()); if (localUploadFutures.containsKey(rule)) { return localUploadFutures.get(rule); }//from w ww . j a va 2 s . co m ListenableFuture<RuleKey> asyncRuleKey = ruleKeyCalculator.calculate(eventBus, rule); NamedTemporaryFile releasedFile; ListenableFuture<CacheResult> asyncfetchResult; try (CloseableHolder<NamedTemporaryFile> tempFile = new CloseableHolder<>( new NamedTemporaryFile(MostFiles.sanitize(rule.getBuildTarget().getShortName()), ""))) { asyncfetchResult = Futures.transformAsync(asyncRuleKey, ruleKey -> localCache.get().fetchAsync(rule.getBuildTarget(), ruleKey, LazyPath.ofInstance(tempFile.get().get())), // We should already have computed the rulekey before calling this method, so using // DirectExecutor here is fine. MoreExecutors.directExecutor()); releasedFile = tempFile.release(); } ListenableFuture<Void> uploadFuture = Futures.transformAsync(asyncfetchResult, fetchResult -> { if (!fetchResult.getType().isSuccess()) { LOG.error("Could not upload missing target [%s] from local cache.", rule.getBuildTarget().getFullyQualifiedName()); return Futures.immediateFuture(null); } return remoteCache.store( ArtifactInfo.builder().setRuleKeys(ImmutableSet.of(Futures.getUnchecked(asyncRuleKey))) .setMetadata(fetchResult.getMetadata()).build(), BorrowablePath.notBorrowablePath(releasedFile.get())); }, executorService); ListenableFuture<BuildRule> finalFuture = Futures.transform(uploadFuture, result -> { try { releasedFile.close(); } catch (IOException e) { throw new RuntimeException(e); } return rule; }, MoreExecutors.directExecutor()); localUploadFutures.put(rule, finalFuture); return finalFuture; }
From source file:com.orangerhymelabs.helenus.cassandra.AbstractCassandraRepository.java
/** * Read all given identifiers./*from www . j a v a 2 s. co m*/ * * Leverages the token-awareness of the driver to optimally query each node directly instead of invoking a * coordinator node. Sends an individual query for each partition key, so reaches the appropriate replica * directly and collates the results client-side. * * @param ids the partition keys (identifiers) to select. */ public ListenableFuture<List<T>> readIn(Identifier... ids) { List<ListenableFuture<ResultSet>> futures = submitReadIn(ids); List<ListenableFuture<T>> results = new ArrayList<>(ids.length); for (ListenableFuture<ResultSet> future : futures) { results.add(Futures.transformAsync(future, new AsyncFunction<ResultSet, T>() { @Override public ListenableFuture<T> apply(ResultSet input) { if (!input.isExhausted()) { return Futures.immediateFuture(marshalRow(input.one())); } return Futures.immediateFuture(null); } }, MoreExecutors.directExecutor())); } return Futures.allAsList(results); }
From source file:com.google.devtools.build.lib.query2.engine.AbstractQueryEnvironment.java
@Override public <T1, T2> QueryTaskFuture<T2> transformAsync(QueryTaskFuture<T1> future, final Function<T1, QueryTaskFuture<T2>> function) { return QueryTaskFutureImpl .ofDelegate(Futures.transformAsync((QueryTaskFutureImpl<T1>) future, new AsyncFunction<T1, T2>() { @Override//from www .ja v a 2 s . c om public ListenableFuture<T2> apply(T1 input) throws Exception { return (QueryTaskFutureImpl<T2>) function.apply(input); } }, directExecutor())); }