Java Code Examples for org.apache.flink.runtime.concurrent.FutureUtils#composeAfterwards()

The following examples show how to use org.apache.flink.runtime.concurrent.FutureUtils#composeAfterwards() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TaskManagerRunner.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<Void> closeAsync() {
	synchronized (lock) {
		if (!shutdown) {
			shutdown = true;

			final CompletableFuture<Void> taskManagerTerminationFuture = taskManager.closeAsync();

			final CompletableFuture<Void> serviceTerminationFuture = FutureUtils.composeAfterwards(
				taskManagerTerminationFuture,
				this::shutDownServices);

			serviceTerminationFuture.whenComplete(
				(Void ignored, Throwable throwable) -> {
					if (throwable != null) {
						terminationFuture.completeExceptionally(throwable);
					} else {
						terminationFuture.complete(null);
					}
				});
		}
	}

	return terminationFuture;
}
 
Example 2
Source File: TaskManagerRunner.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<Void> closeAsync() {
	synchronized (lock) {
		if (!shutdown) {
			shutdown = true;

			final CompletableFuture<Void> taskManagerTerminationFuture = taskManager.closeAsync();

			final CompletableFuture<Void> serviceTerminationFuture = FutureUtils.composeAfterwards(
				taskManagerTerminationFuture,
				this::shutDownServices);

			serviceTerminationFuture.whenComplete(
				(Void ignored, Throwable throwable) -> {
					if (throwable != null) {
						terminationFuture.completeExceptionally(throwable);
					} else {
						terminationFuture.complete(null);
					}
				});
		}
	}

	return terminationFuture;
}
 
Example 3
Source File: TaskManagerRunner.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<Void> closeAsync() {
	synchronized (lock) {
		if (!shutdown) {
			shutdown = true;

			final CompletableFuture<Void> taskManagerTerminationFuture = taskManager.closeAsync();

			final CompletableFuture<Void> serviceTerminationFuture = FutureUtils.composeAfterwards(
				taskManagerTerminationFuture,
				this::shutDownServices);

			serviceTerminationFuture.whenComplete(
				(Void ignored, Throwable throwable) -> {
					if (throwable != null) {
						terminationFuture.completeExceptionally(throwable);
					} else {
						terminationFuture.complete(null);
					}
				});
		}
	}

	return terminationFuture;
}
 
Example 4
Source File: DispatcherResourceManagerComponent.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Deregister the Flink application from the resource management system by signalling
 * the {@link ResourceManager}.
 *
 * @param applicationStatus to terminate the application with
 * @param diagnostics additional information about the shut down, can be {@code null}
 * @return Future which is completed once the shut down
 */
public CompletableFuture<Void> deregisterApplicationAndClose(
		final ApplicationStatus applicationStatus,
		final @Nullable String diagnostics) {

	if (isRunning.compareAndSet(true, false)) {
		final CompletableFuture<Void> closeWebMonitorAndDeregisterAppFuture =
			FutureUtils.composeAfterwards(webMonitorEndpoint.closeAsync(), () -> deregisterApplication(applicationStatus, diagnostics));

		return FutureUtils.composeAfterwards(closeWebMonitorAndDeregisterAppFuture, this::closeAsyncInternal);
	} else {
		return terminationFuture;
	}
}
 
Example 5
Source File: AkkaRpcService.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Void> stopService() {
	final CompletableFuture<Void> akkaRpcActorsTerminationFuture;

	synchronized (lock) {
		if (stopped) {
			return terminationFuture;
		}

		LOG.info("Stopping Akka RPC service.");

		stopped = true;

		akkaRpcActorsTerminationFuture = terminateAkkaRpcActors();
	}

	final CompletableFuture<Void> actorSystemTerminationFuture = FutureUtils.composeAfterwards(
		akkaRpcActorsTerminationFuture,
		() -> FutureUtils.toJava(actorSystem.terminate()));

	actorSystemTerminationFuture.whenComplete(
		(Void ignored, Throwable throwable) -> {
			if (throwable != null) {
				terminationFuture.completeExceptionally(throwable);
			} else {
				terminationFuture.complete(null);
			}

			LOG.info("Stopped Akka RPC service.");
		});

	return terminationFuture;
}
 
Example 6
Source File: ClusterEntrypoint.java    From flink with Apache License 2.0 5 votes vote down vote up
private CompletableFuture<ApplicationStatus> shutDownAsync(
		ApplicationStatus applicationStatus,
		@Nullable String diagnostics,
		boolean cleanupHaData) {
	if (isShutDown.compareAndSet(false, true)) {
		LOG.info("Shutting {} down with application status {}. Diagnostics {}.",
			getClass().getSimpleName(),
			applicationStatus,
			diagnostics);

		final CompletableFuture<Void> shutDownApplicationFuture = closeClusterComponent(applicationStatus, diagnostics);

		final CompletableFuture<Void> serviceShutdownFuture = FutureUtils.composeAfterwards(
			shutDownApplicationFuture,
			() -> 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(applicationStatus);
				}
			});
	}

	return terminationFuture;
}
 
Example 7
Source File: AbstractHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public final CompletableFuture<Void> closeAsync() {
	synchronized (lock) {
		if (terminationFuture == null) {
			this.terminationFuture = FutureUtils.composeAfterwards(closeHandlerAsync(), inFlightRequestTracker::awaitAsync);
		} else {
			log.warn("The handler instance for {} had already been closed, but another attempt at closing it was made.", untypedResponseMessageHeaders.getTargetRestEndpointURL());
		}
		return this.terminationFuture;
	}
}
 
Example 8
Source File: RestServerEndpoint.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Void> closeAsync() {
	synchronized (lock) {
		log.info("Shutting down rest endpoint.");

		if (state == State.RUNNING) {
			final CompletableFuture<Void> shutDownFuture = FutureUtils.composeAfterwards(
				closeHandlersAsync(),
				this::shutDownInternal);

			shutDownFuture.whenComplete(
				(Void ignored, Throwable throwable) -> {
					log.info("Shut down complete.");
					if (throwable != null) {
						terminationFuture.completeExceptionally(throwable);
					} else {
						terminationFuture.complete(null);
					}
				});
			state = State.SHUTDOWN;
		} else if (state == State.CREATED) {
			terminationFuture.complete(null);
			state = State.SHUTDOWN;
		}

		return terminationFuture;
	}
}
 
Example 9
Source File: AkkaRpcService.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Void> stopService() {
	final CompletableFuture<Void> akkaRpcActorsTerminationFuture;

	synchronized (lock) {
		if (stopped) {
			return terminationFuture;
		}

		LOG.info("Stopping Akka RPC service.");

		stopped = true;

		akkaRpcActorsTerminationFuture = terminateAkkaRpcActors();
	}

	final CompletableFuture<Void> supervisorTerminationFuture = FutureUtils.composeAfterwards(
		akkaRpcActorsTerminationFuture,
		supervisor::closeAsync);

	final CompletableFuture<Void> actorSystemTerminationFuture = FutureUtils.composeAfterwards(
		supervisorTerminationFuture,
		() -> FutureUtils.toJava(actorSystem.terminate()));

	actorSystemTerminationFuture.whenComplete(
		(Void ignored, Throwable throwable) -> {
			if (throwable != null) {
				terminationFuture.completeExceptionally(throwable);
			} else {
				terminationFuture.complete(null);
			}

			LOG.info("Stopped Akka RPC service.");
		});

	return terminationFuture;
}
 
Example 10
Source File: AbstractDispatcherLeaderProcess.java    From flink with Apache License 2.0 5 votes vote down vote up
private void closeInternal() {
	log.info("Stopping {}.", getClass().getSimpleName());

	final CompletableFuture<Void> dispatcherServiceTerminationFuture = closeDispatcherService();

	final CompletableFuture<Void> onCloseTerminationFuture = FutureUtils.composeAfterwards(
		dispatcherServiceTerminationFuture,
		this::onClose);

	FutureUtils.forward(
		onCloseTerminationFuture,
		this.terminationFuture);

	state = State.STOPPED;
}
 
Example 11
Source File: DispatcherResourceManagerComponent.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Deregister the Flink application from the resource management system by signalling
 * the {@link ResourceManager}.
 *
 * @param applicationStatus to terminate the application with
 * @param diagnostics additional information about the shut down, can be {@code null}
 * @return Future which is completed once the shut down
 */
public CompletableFuture<Void> deregisterApplicationAndClose(
		final ApplicationStatus applicationStatus,
		final @Nullable String diagnostics) {

	if (isRunning.compareAndSet(true, false)) {
		final CompletableFuture<Void> closeWebMonitorAndDeregisterAppFuture =
			FutureUtils.composeAfterwards(webMonitorEndpoint.closeAsync(), () -> deregisterApplication(applicationStatus, diagnostics));

		return FutureUtils.composeAfterwards(closeWebMonitorAndDeregisterAppFuture, this::closeAsyncInternal);
	} else {
		return terminationFuture;
	}
}
 
Example 12
Source File: ClusterEntrypoint.java    From flink with Apache License 2.0 5 votes vote down vote up
private CompletableFuture<ApplicationStatus> shutDownAsync(
		ApplicationStatus applicationStatus,
		@Nullable String diagnostics,
		boolean cleanupHaData) {
	if (isShutDown.compareAndSet(false, true)) {
		LOG.info("Shutting {} down with application status {}. Diagnostics {}.",
			getClass().getSimpleName(),
			applicationStatus,
			diagnostics);

		final CompletableFuture<Void> shutDownApplicationFuture = closeClusterComponent(applicationStatus, diagnostics);

		final CompletableFuture<Void> serviceShutdownFuture = FutureUtils.composeAfterwards(
			shutDownApplicationFuture,
			() -> 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(applicationStatus);
				}
			});
	}

	return terminationFuture;
}
 
Example 13
Source File: RestServerEndpoint.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Void> closeAsync() {
	synchronized (lock) {
		log.info("Shutting down rest endpoint.");

		if (state == State.RUNNING) {
			final CompletableFuture<Void> shutDownFuture = FutureUtils.composeAfterwards(
				closeHandlersAsync(),
				this::shutDownInternal);

			shutDownFuture.whenComplete(
				(Void ignored, Throwable throwable) -> {
					log.info("Shut down complete.");
					if (throwable != null) {
						terminationFuture.completeExceptionally(throwable);
					} else {
						terminationFuture.complete(null);
					}
				});
			state = State.SHUTDOWN;
		} else if (state == State.CREATED) {
			terminationFuture.complete(null);
			state = State.SHUTDOWN;
		}

		return terminationFuture;
	}
}
 
Example 14
Source File: AkkaRpcService.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Void> stopService() {
	final CompletableFuture<Void> akkaRpcActorsTerminationFuture;

	synchronized (lock) {
		if (stopped) {
			return terminationFuture;
		}

		LOG.info("Stopping Akka RPC service.");

		stopped = true;

		akkaRpcActorsTerminationFuture = terminateAkkaRpcActors();
	}

	final CompletableFuture<Void> actorSystemTerminationFuture = FutureUtils.composeAfterwards(
		akkaRpcActorsTerminationFuture,
		() -> FutureUtils.toJava(actorSystem.terminate()));

	actorSystemTerminationFuture.whenComplete(
		(Void ignored, Throwable throwable) -> {
			if (throwable != null) {
				terminationFuture.completeExceptionally(throwable);
			} else {
				terminationFuture.complete(null);
			}

			LOG.info("Stopped Akka RPC service.");
		});

	return terminationFuture;
}
 
Example 15
Source File: DispatcherResourceManagerComponent.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Deregister the Flink application from the resource management system by signalling
 * the {@link ResourceManager}.
 *
 * @param applicationStatus to terminate the application with
 * @param diagnostics additional information about the shut down, can be {@code null}
 * @return Future which is completed once the shut down
 */
public CompletableFuture<Void> deregisterApplicationAndClose(
		final ApplicationStatus applicationStatus,
		final @Nullable String diagnostics) {

	if (isRunning.compareAndSet(true, false)) {
		final CompletableFuture<Void> closeWebMonitorAndDeregisterAppFuture =
			FutureUtils.composeAfterwards(webMonitorEndpoint.closeAsync(), () -> deregisterApplication(applicationStatus, diagnostics));

		return FutureUtils.composeAfterwards(closeWebMonitorAndDeregisterAppFuture, this::closeAsyncInternal);
	} else {
		return terminationFuture;
	}
}
 
Example 16
Source File: ClusterEntrypoint.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private CompletableFuture<ApplicationStatus> shutDownAsync(
		ApplicationStatus applicationStatus,
		@Nullable String diagnostics,
		boolean cleanupHaData) {
	if (isShutDown.compareAndSet(false, true)) {
		LOG.info("Shutting {} down with application status {}. Diagnostics {}.",
			getClass().getSimpleName(),
			applicationStatus,
			diagnostics);

		final CompletableFuture<Void> shutDownApplicationFuture = closeClusterComponent(applicationStatus, diagnostics);

		final CompletableFuture<Void> serviceShutdownFuture = FutureUtils.composeAfterwards(
			shutDownApplicationFuture,
			() -> 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(applicationStatus);
				}
			});
	}

	return terminationFuture;
}
 
Example 17
Source File: RestServerEndpoint.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Void> closeAsync() {
	synchronized (lock) {
		log.info("Shutting down rest endpoint.");

		if (state == State.RUNNING) {
			final CompletableFuture<Void> shutDownFuture = FutureUtils.composeAfterwards(
				closeHandlersAsync(),
				this::shutDownInternal);

			shutDownFuture.whenComplete(
				(Void ignored, Throwable throwable) -> {
					log.info("Shut down complete.");
					if (throwable != null) {
						terminationFuture.completeExceptionally(throwable);
					} else {
						terminationFuture.complete(null);
					}
				});
			state = State.SHUTDOWN;
		} else if (state == State.CREATED) {
			terminationFuture.complete(null);
			state = State.SHUTDOWN;
		}

		return terminationFuture;
	}
}
 
Example 18
Source File: MiniCluster.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Shuts down the mini cluster, failing all currently executing jobs.
 * The mini cluster can be started again by calling the {@link #start()} method again.
 *
 * <p>This method shuts down all started services and components,
 * even if an exception occurs in the process of shutting down some component.
 *
 * @return Future which is completed once the MiniCluster has been completely shut down
 */
@Override
public CompletableFuture<Void> closeAsync() {
	synchronized (lock) {
		if (running) {
			LOG.info("Shutting down Flink Mini Cluster");
			try {
				final long shutdownTimeoutMillis = miniClusterConfiguration.getConfiguration().getLong(ClusterOptions.CLUSTER_SERVICES_SHUTDOWN_TIMEOUT);
				final int numComponents = 2 + miniClusterConfiguration.getNumTaskManagers();
				final Collection<CompletableFuture<Void>> componentTerminationFutures = new ArrayList<>(numComponents);

				componentTerminationFutures.addAll(terminateTaskExecutors());

				componentTerminationFutures.add(shutDownResourceManagerComponents());

				final FutureUtils.ConjunctFuture<Void> componentsTerminationFuture = FutureUtils.completeAll(componentTerminationFutures);

				final CompletableFuture<Void> metricSystemTerminationFuture = FutureUtils.composeAfterwards(
					componentsTerminationFuture,
					this::closeMetricSystem);

				final CompletableFuture<Void> rpcServicesTerminationFuture = FutureUtils.composeAfterwards(
					metricSystemTerminationFuture,
					this::terminateRpcServices);

				final CompletableFuture<Void> remainingServicesTerminationFuture = FutureUtils.runAfterwards(
					rpcServicesTerminationFuture,
					this::terminateMiniClusterServices);

				final CompletableFuture<Void> executorsTerminationFuture = FutureUtils.composeAfterwards(
					remainingServicesTerminationFuture,
					() -> terminateExecutors(shutdownTimeoutMillis));

				executorsTerminationFuture.whenComplete(
						(Void ignored, Throwable throwable) -> {
							if (throwable != null) {
								terminationFuture.completeExceptionally(ExceptionUtils.stripCompletionException(throwable));
							} else {
								terminationFuture.complete(null);
							}
						});
			} finally {
				running = false;
			}
		}

		return terminationFuture;
	}
}
 
Example 19
Source File: AbstractHandler.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public final CompletableFuture<Void> closeAsync() {
	return FutureUtils.composeAfterwards(closeHandlerAsync(), inFlightRequestTracker::awaitAsync);
}
 
Example 20
Source File: AbstractHandler.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Override
public final CompletableFuture<Void> closeAsync() {
	return FutureUtils.composeAfterwards(closeHandlerAsync(), inFlightRequestTracker::awaitAsync);
}