return FutureUtils.completedExceptionally( new UnknownLocationException( "Could not retrieve location of state=" + queryableStateName + " of job=" + jobId +
CompletableFuture<String> savepointPathFuture = FutureUtils.retryWithDelay( () -> { try { return client.triggerSavepoint(jobID, null); } catch (FlinkException e) { return FutureUtils.completedExceptionally(e);
/** * Tries to shut down the given actor gracefully. * * @param actorRef specifying the actor to shut down * @param timeout for the graceful shut down * @return A future that finishes with {@code true} iff. the actor could be stopped gracefully * or {@code actorRef} was {@code null}. */ private CompletableFuture<Boolean> stopActor(@Nullable final ActorRef actorRef, FiniteDuration timeout) { if (actorRef == null) { return CompletableFuture.completedFuture(true); } return FutureUtils.toJava(Patterns.gracefulStop(actorRef, timeout)) .exceptionally( (Throwable throwable) -> { // The actor did not stop gracefully in time, try to directly stop it actorSystem.stop(actorRef); log.warn("Could not stop actor {} gracefully.", actorRef.path(), throwable); return false; } ); }
NotifyingMapper.notifyLatch.await(); FutureUtils.retrySuccessfulWithDelay( () -> { try { return CompletableFuture.completedFuture(client.getAccumulators(jobGraph.getJobID())); } catch (Exception e) { return FutureUtils.completedExceptionally(e);
@Override public CompletableFuture<MetricQueryServiceGateway> retrieveService(String queryServicePath) { ActorSelection selection = actorSystem.actorSelection(queryServicePath); return FutureUtils.toJava(selection.resolveOne(FutureUtils.toFiniteDuration(lookupTimeout))).thenApply(AkkaQueryServiceGateway::new); } }
@Override protected CompletableFuture<JobManagerGateway> createGateway(CompletableFuture<Tuple2<String, UUID>> leaderFuture) { return FutureUtils.retryWithDelay( () -> leaderFuture.thenCompose( (Tuple2<String, UUID> addressLeaderId) -> FutureUtils.toJava( AkkaUtils.getActorRefFuture( addressLeaderId.f0, actorSystem, FutureUtils.toFiniteDuration(timeout))) .thenApply( (ActorRef jobManagerRef) -> { ActorGateway leaderGateway = new AkkaActorGateway( jobManagerRef, addressLeaderId.f1); return new AkkaJobManagerGateway(leaderGateway); } )), retries, retryDelay, scheduledExecutor); } }
@Override public CompletableFuture<LogicalSlot> allocateSlot( SlotRequestId slotRequestId, ScheduledUnit task, boolean allowQueued, SlotProfile slotProfile, Time allocationTimeout) { try { final Object ret = scheduleTask(task, allowQueued, slotProfile.getPreferredLocations()); if (ret instanceof SimpleSlot) { return CompletableFuture.completedFuture((SimpleSlot) ret); } else if (ret instanceof CompletableFuture) { @SuppressWarnings("unchecked") CompletableFuture<LogicalSlot> typed = (CompletableFuture<LogicalSlot>) ret; return FutureUtils.orTimeout(typed, allocationTimeout.toMilliseconds(), TimeUnit.MILLISECONDS); } else { // this should never happen, simply guard this case with an exception throw new RuntimeException(); } } catch (NoResourceAvailableException e) { return FutureUtils.completedExceptionally(e); } }
terminationFutures.add(FutureUtils.completedExceptionally(exception)); final CompletableFuture<Void> componentTerminationFuture = FutureUtils.completeAll(terminationFutures); final CompletableFuture<Void> metricGroupTerminationFuture = FutureUtils.runAfterwards( componentTerminationFuture, jobManagerMetricGroup::close);
FutureUtils.waitForAll(futures.values()).get();
private <C> CompletableFuture<C> retry( CheckedSupplier<CompletableFuture<C>> operation, Predicate<Throwable> retryPredicate) { return FutureUtils.retryWithDelay( CheckedSupplier.unchecked(operation), restClusterClientConfiguration.getRetryMaxAttempts(), Time.milliseconds(restClusterClientConfiguration.getRetryDelay()), retryPredicate, new ScheduledExecutorServiceAdapter(retryExecutorService)); }
@VisibleForTesting CompletableFuture<URL> getWebMonitorBaseUrl() { return FutureUtils.orTimeout( webMonitorLeaderRetriever.getLeaderFuture(), restClusterClientConfiguration.getAwaitLeaderTimeout(), TimeUnit.MILLISECONDS) .thenApplyAsync(leaderAddressSessionId -> { final String url = leaderAddressSessionId.f0; try { return new URL(url); } catch (MalformedURLException e) { throw new IllegalArgumentException("Could not parse URL from " + url, e); } }, executorService); } }
private CompletableFuture<Void> shutDownAsync( boolean cleanupHaData, ApplicationStatus applicationStatus, @Nullable String diagnostics) { if (isShutDown.compareAndSet(false, true)) { LOG.info("Stopping {}.", getClass().getSimpleName()); final CompletableFuture<Void> shutDownApplicationFuture = deregisterApplication(applicationStatus, diagnostics); final CompletableFuture<Void> componentShutdownFuture = FutureUtils.composeAfterwards( shutDownApplicationFuture, this::stopClusterComponents); final CompletableFuture<Void> serviceShutdownFuture = FutureUtils.composeAfterwards( componentShutdownFuture, () -> stopClusterServices(cleanupHaData)); final CompletableFuture<Void> cleanupDirectoriesFuture = FutureUtils.runAfterwards( serviceShutdownFuture, this::cleanupDirectories); cleanupDirectoriesFuture.whenComplete( (Void ignored2, Throwable serviceThrowable) -> { if (serviceThrowable != null) { terminationFuture.completeExceptionally(serviceThrowable); } else { terminationFuture.complete(null); } }); } return terminationFuture; }
/** * Sends stop RPC call. */ public void stop() { final LogicalSlot slot = assignedResource; if (slot != null) { final TaskManagerGateway taskManagerGateway = slot.getTaskManagerGateway(); CompletableFuture<Acknowledge> stopResultFuture = FutureUtils.retry( () -> taskManagerGateway.stopTask(attemptId, rpcTimeout), NUM_STOP_CALL_TRIES, executor); stopResultFuture.exceptionally( failure -> { LOG.info("Stopping task was not successful.", failure); return null; }); } }
@Override protected CompletableFuture<Void> shutDownInternal() { executionGraphCache.close(); final CompletableFuture<Void> shutdownFuture = FutureUtils.runAfterwards( super.shutDownInternal(), () -> ExecutorUtils.gracefulShutdown(10, TimeUnit.SECONDS, executor)); final File webUiDir = restConfiguration.getWebUiDir(); return FutureUtils.runAfterwardsAsync( shutdownFuture, () -> { Exception exception = null; try { log.info("Removing cache directory {}", webUiDir); FileUtils.deleteDirectory(webUiDir); } catch (Exception e) { exception = e; } try { leaderElectionService.stop(); } catch (Exception e) { exception = ExceptionUtils.firstOrSuppressed(e, exception); } if (exception != null) { throw exception; } }); }
@Override protected CompletableFuture<Void> stopClusterServices(boolean cleanupHaData) { final CompletableFuture<Void> serviceShutDownFuture = super.stopClusterServices(cleanupHaData); return FutureUtils.runAfterwards( serviceShutDownFuture, () -> { if (mesosServices != null) { mesosServices.close(cleanupHaData); } }); }
final CompletableFuture<Void> allJobManagerRunnersTerminationFuture = FutureUtils.completeAll(Arrays.asList( jobManagerRunnersTerminationFuture, orphanedJobManagerRunnersTerminationFuture)); return FutureUtils.runAfterwards( allJobManagerRunnersTerminationFuture, () -> {
httpClient.sendGetRequest(restPath, FutureUtils.toFiniteDuration(timeout)); HttpTestClient.SimpleHttpResponse response = httpClient.getNextResponse(FutureUtils.toFiniteDuration(timeout)); httpClient.sendGetRequest(restPath, FutureUtils.toFiniteDuration(timeout)); response = httpClient.getNextResponse(FutureUtils.toFiniteDuration(timeout));
@Override public CompletableFuture<MetricQueryServiceGateway> retrieveService(String queryServicePath) { ActorSelection selection = actorSystem.actorSelection(queryServicePath); return FutureUtils.toJava(selection.resolveOne(FutureUtils.toFiniteDuration(lookupTimeout))).thenApply(AkkaQueryServiceGateway::new); } }
@Override protected CompletableFuture<JobManagerGateway> createGateway(CompletableFuture<Tuple2<String, UUID>> leaderFuture) { return FutureUtils.retryWithDelay( () -> leaderFuture.thenCompose( (Tuple2<String, UUID> addressLeaderId) -> FutureUtils.toJava( AkkaUtils.getActorRefFuture( addressLeaderId.f0, actorSystem, FutureUtils.toFiniteDuration(timeout))) .thenApply( (ActorRef jobManagerRef) -> { ActorGateway leaderGateway = new AkkaActorGateway( jobManagerRef, addressLeaderId.f1); return new AkkaJobManagerGateway(leaderGateway); } )), retries, retryDelay, scheduledExecutor); } }
@Override public CompletableFuture<LogicalSlot> allocateSlot( SlotRequestId slotRequestId, ScheduledUnit task, boolean allowQueued, SlotProfile slotProfile, Time allocationTimeout) { try { final Object ret = scheduleTask(task, allowQueued, slotProfile.getPreferredLocations()); if (ret instanceof SimpleSlot) { return CompletableFuture.completedFuture((SimpleSlot) ret); } else if (ret instanceof CompletableFuture) { @SuppressWarnings("unchecked") CompletableFuture<LogicalSlot> typed = (CompletableFuture<LogicalSlot>) ret; return FutureUtils.orTimeout(typed, allocationTimeout.toMilliseconds(), TimeUnit.MILLISECONDS); } else { // this should never happen, simply guard this case with an exception throw new RuntimeException(); } } catch (NoResourceAvailableException e) { return FutureUtils.completedExceptionally(e); } }