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:com.facebook.buck.distributed.build_slave.MultiSlaveBuildModeRunnerFactory.java
/** * Create a {@link CoordinatorModeRunner}. * * @param isLocalMinionAlsoRunning - are we going to have a local minion working on this build? * @return a new instance of the {@link CoordinatorModeRunner}. *///from w ww . j a v a 2 s .co m public static CoordinatorModeRunner createCoordinator( ListenableFuture<DelegateAndGraphs> delegateAndGraphsFuture, List<BuildTarget> topLevelTargetsToBuild, DistBuildConfig distBuildConfig, DistBuildService distBuildService, StampedeId stampedeId, Optional<BuildId> clientBuildId, boolean isLocalMinionAlsoRunning, Path logDirectoryPath, CoordinatorBuildRuleEventsPublisher coordinatorBuildRuleEventsPublisher, BuckEventBus eventBus, ListeningExecutorService executorService, ArtifactCache remoteCache, ListenableFuture<ParallelRuleKeyCalculator<RuleKey>> asyncRuleKeyCalculator, HealthCheckStatsTracker healthCheckStatsTracker, Optional<BuildSlaveTimingStatsTracker> timingStatsTracker, Optional<String> coordinatorMinionId) { ListenableFuture<BuildTargetsQueue> queueFuture = Futures.transformAsync(asyncRuleKeyCalculator, ruleKeyCalculator -> Futures.transform(delegateAndGraphsFuture, graphs -> { timingStatsTracker .ifPresent(tracker -> tracker.startTimer(REVERSE_DEPENDENCY_QUEUE_CREATION_TIME)); BuildTargetsQueue queue; try (ArtifactCacheByBuildRule artifactCache = new DistBuildArtifactCacheImpl( graphs.getActionGraphAndBuilder().getActionGraphBuilder(), executorService, remoteCache, eventBus, ruleKeyCalculator, Optional.empty())) { queue = new CacheOptimizedBuildTargetsQueueFactory( graphs.getActionGraphAndBuilder().getActionGraphBuilder(), artifactCache, distBuildConfig.isDeepRemoteBuildEnabled(), ruleKeyCalculator.getRuleDepsCache(), distBuildConfig.shouldBuildSelectedTargetsLocally()).createBuildTargetsQueue( topLevelTargetsToBuild, coordinatorBuildRuleEventsPublisher, distBuildConfig.getMostBuildRulesFinishedPercentageThreshold()); } catch (Exception e) { LOG.error(e, "Failed to create BuildTargetsQueue."); throw new RuntimeException(e); } timingStatsTracker .ifPresent(tracker -> tracker.stopTimer(REVERSE_DEPENDENCY_QUEUE_CREATION_TIME)); return queue; }, executorService), executorService); Optional<String> minionQueue = distBuildConfig.getMinionQueue(); Optional<String> minionRegion = distBuildConfig.getMinionRegion(); Preconditions.checkArgument(minionQueue.isPresent(), "Minion queue name is missing to be able to run in Coordinator mode."); MinionQueueProvider minionQueueProvider = createMinionQueueProvider(distBuildConfig.getLowSpecMinionQueue(), minionQueue.get()); CoordinatorEventListener listenerAndMinionCountProvider = new CoordinatorEventListener(distBuildService, stampedeId, distBuildConfig.getBuildLabel(), minionQueueProvider, isLocalMinionAlsoRunning, minionRegion.orElse(null)); MinionHealthTracker minionHealthTracker = new MinionHealthTracker(new DefaultClock(), distBuildConfig.getMaxMinionSilenceMillis(), distBuildConfig.getMaxConsecutiveSlowDeadMinionChecks(), distBuildConfig.getHeartbeatServiceRateMillis(), distBuildConfig.getSlowHeartbeatWarningThresholdMillis(), healthCheckStatsTracker); ChromeTraceBuckConfig chromeTraceBuckConfig = distBuildConfig.getBuckConfig() .getView(ChromeTraceBuckConfig.class); Optional<URI> traceUploadUri = chromeTraceBuckConfig.getTraceUploadUriIfEnabled(); return new CoordinatorModeRunner(queueFuture, stampedeId, listenerAndMinionCountProvider, logDirectoryPath, coordinatorBuildRuleEventsPublisher, distBuildService, clientBuildId, traceUploadUri, minionHealthTracker, listenerAndMinionCountProvider, coordinatorMinionId, distBuildConfig.isReleasingMinionsEarlyEnabled()); }
From source file:org.thingsboard.rule.engine.action.TbClearAlarmNode.java
@Override protected ListenableFuture<AlarmResult> processAlarm(TbContext ctx, TbMsg msg) { ListenableFuture<Alarm> latest = ctx.getAlarmService().findLatestByOriginatorAndType(ctx.getTenantId(), msg.getOriginator(), config.getAlarmType()); return Futures.transformAsync(latest, a -> { if (a != null && !a.getStatus().isCleared()) { return clearAlarm(ctx, msg, a); }//from w w w .j a va2 s . c om return Futures.immediateFuture(new AlarmResult(false, false, false, null)); }, ctx.getDbCallbackExecutor()); }
From source file:com.orangerhymelabs.helenus.cassandra.document.DocumentService.java
public ListenableFuture<Document> create(String database, String table, Document document) { ListenableFuture<AbstractDocumentRepository> docs = acquireRepositoryFor(database, table); return Futures.transformAsync(docs, new AsyncFunction<AbstractDocumentRepository, Document>() { @Override// w w w.j a va2 s . c om public ListenableFuture<Document> apply(AbstractDocumentRepository docRepo) throws Exception { try { ValidationEngine.validateAndThrow(document); Document newDoc = docRepo.create(document).get(); updateViews(newDoc); return Futures.immediateFuture(newDoc); } catch (ValidationException e) { return Futures.immediateFailedFuture(e); } } private void updateViews(Document document) throws InterruptedException, ExecutionException { List<View> tableViews = getTableViews(database, table).get(); tableViews.forEach(new Consumer<View>() { @Override public void accept(View v) { try { Identifier id = v.identifierFrom(document); if (id != null) { Document viewDoc = new Document(document.object()); viewDoc.identifier(id); Document newView = createViewDocument(v, viewDoc).get(); System.out.println(newView.createdAt()); } } catch (KeyDefinitionException e) { e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ExecutionException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); } // private ListenableFuture<List<Document>> updateViews(Document document) // { // ListenableFuture<List<View>> tableViews = getTableViews(database, table); // return Futures.transformAsync(tableViews, new AsyncFunction<List<View>, List<Document>>() // { // @Override // public ListenableFuture<List<Document>> apply(List<View> input) // throws Exception // { // List<ListenableFuture<Document>> newDocs = new ArrayList<>(input.size()); // input.parallelStream().forEach(new Consumer<View>() // { // @Override // public void accept(View v) // { // try // { // Identifier id = v.identifierFrom(document); // // if (id != null) // { // Document viewDoc = new Document(document.object()); // viewDoc.identifier(id); // newDocs.add(createViewDocument(v, document)); // } // } // catch (KeyDefinitionException e) // { // e.printStackTrace(); // } // } // }); // // return null; // } // }); // } }, MoreExecutors.directExecutor()); }
From source file:org.thingsboard.rule.engine.action.TbCreateAlarmNode.java
@Override protected ListenableFuture<AlarmResult> processAlarm(TbContext ctx, TbMsg msg) { ListenableFuture<Alarm> latest = ctx.getAlarmService().findLatestByOriginatorAndType(ctx.getTenantId(), msg.getOriginator(), config.getAlarmType()); return Futures.transformAsync(latest, a -> { if (a == null || a.getStatus().isCleared()) { return createNewAlarm(ctx, msg); } else {//from w w w. j av a2 s . c om return updateAlarm(ctx, msg, a); } }, ctx.getDbCallbackExecutor()); }
From source file:com.orangerhymelabs.helenus.cassandra.AbstractCassandraRepository.java
public ListenableFuture<T> create(T entity) { ListenableFuture<ResultSet> future = submitCreate(entity); return Futures.transformAsync(future, new AsyncFunction<ResultSet, T>() { @Override// w w w . j a v a 2s .co m public ListenableFuture<T> apply(ResultSet result) throws Exception { if (result.wasApplied()) { return Futures.immediateFuture(entity); } return Futures.immediateFailedFuture(new DuplicateItemException(entity.toString())); } }, MoreExecutors.directExecutor()); }
From source file:com.facebook.buck.parser.ParserLeaseVendor.java
/** * @param cell the cell in which we're parsing * @param withParser the function that performs the interaction with the parser * @param executorService where to apply the async function * @param <T> type of result//from w ww .j a va 2s .c om * @return a {@link ListenableFuture} that will run the supplied AsyncFunction when a parser * is available. */ public <T> ListenableFuture<T> leaseParser(final Cell cell, final AsyncFunction<P, T> withParser, ListeningExecutorService executorService) { Preconditions.checkState(!closed.get()); final ListenableFuture<P> obtainedParser = obtainParser(cell); ListenableFuture<T> futureWork = Futures.transformAsync(obtainedParser, new AsyncFunction<P, T>() { @Override public ListenableFuture<T> apply(P input) throws Exception { try { return withParser.apply(input); } finally { returnParser(cell, input); } } }, executorService); return futureWork; }
From source file:org.thingsboard.rule.engine.action.TbClearAlarmNode.java
private ListenableFuture<AlarmResult> clearAlarm(TbContext ctx, TbMsg msg, Alarm alarm) { ListenableFuture<JsonNode> asyncDetails = buildAlarmDetails(ctx, msg, alarm.getDetails()); return Futures.transformAsync(asyncDetails, details -> { ListenableFuture<Boolean> clearFuture = ctx.getAlarmService().clearAlarm(ctx.getTenantId(), alarm.getId(), details, System.currentTimeMillis()); return Futures.transformAsync(clearFuture, cleared -> { if (cleared && details != null) { alarm.setDetails(details); }/*w w w.j ava 2 s . c o m*/ alarm.setStatus(alarm.getStatus().isAck() ? AlarmStatus.CLEARED_ACK : AlarmStatus.CLEARED_UNACK); return Futures.immediateFuture(new AlarmResult(false, false, true, alarm)); }); }, ctx.getDbCallbackExecutor()); }
From source file:com.orangerhymelabs.helenus.cassandra.view.ViewService.java
public ListenableFuture<View> update(View view) { ListenableFuture<Boolean> tableFuture = tables.exists(view.databaseName(), view.tableName()); return Futures.transformAsync(tableFuture, new AsyncFunction<Boolean, View>() { @Override//from w w w .j a va 2 s.c o m public ListenableFuture<View> apply(Boolean exists) throws Exception { if (exists) { try { ValidationEngine.validateAndThrow(view); return views.update(view); } catch (ValidationException e) { return Futures.immediateFailedFuture(e); } } else { return Futures.immediateFailedFuture( new ItemNotFoundException("Database not found: " + view.databaseName())); } } }, MoreExecutors.directExecutor()); }
From source file:com.orangerhymelabs.helenus.cassandra.table.TableService.java
public ListenableFuture<List<Table>> readAll(String database, Object... parms) { ListenableFuture<Boolean> dbFuture = databases.exists(database); return Futures.transformAsync(dbFuture, new AsyncFunction<Boolean, List<Table>>() { @Override//from www .j a va2s .c o m public ListenableFuture<List<Table>> apply(Boolean exists) throws Exception { if (exists) { return tables.readAll(parms); } else { return Futures .immediateFailedFuture(new ItemNotFoundException("Database not found: " + database)); } } }, MoreExecutors.directExecutor()); }
From source file:com.facebook.buck.core.rulekey.calculator.ParallelRuleKeyCalculator.java
/** * @return a {@link ListenableFuture} wrapping the result of calculating the {@link RuleKey} of * the given {@link BuildRule}.// ww w .j a v a 2 s . c o m */ public synchronized ListenableFuture<T> calculate(BuckEventBus buckEventBus, BuildRule rule) { ListenableFuture<T> fromOurCache = ruleKeys.get(rule.getBuildTarget()); if (fromOurCache != null) { return fromOurCache; } T fromInternalCache = ruleKeyFactory.getFromCache(rule); if (fromInternalCache != null) { ListenableFuture<T> future = Futures.immediateFuture(fromInternalCache); // Record the rule key future. ruleKeys.put(rule.getBuildTarget(), future); // Because a rule key will be invalidated from the internal cache any time one of its // dependents is invalidated, we know that all of our transitive deps are also in cache. return future; } // Grab all the dependency rule key futures. Since our rule key calculation depends on this // one, we need to wait for them to complete. ListenableFuture<List<T>> depKeys = Futures.transformAsync(Futures.immediateFuture(ruleDepsCache.get(rule)), (@Nonnull SortedSet<BuildRule> deps) -> { List<ListenableFuture<T>> depKeys1 = new ArrayList<>( SortedSets.sizeEstimate(rule.getBuildDeps())); for (BuildRule dep : deps) { depKeys1.add(calculate(buckEventBus, dep)); } return Futures.allAsList(depKeys1); }, service); // Setup a future to calculate this rule key once the dependencies have been calculated. ListenableFuture<T> calculated = Futures.transform(depKeys, (List<T> input) -> { try (Scope scope = ruleKeyCalculationScope.apply(buckEventBus, rule)) { return ruleKeyFactory.build(rule); } catch (Exception e) { throw new BuckUncheckedExecutionException(e, String.format("When computing rulekey for %s.", rule)); } }, service); // Record the rule key future. ruleKeys.put(rule.getBuildTarget(), calculated); return calculated; }