You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by kk...@apache.org on 2020/10/26 10:04:59 UTC

[flink] branch application-1.11 created (now a4ae9d8)

This is an automated email from the ASF dual-hosted git repository.

kkloudas pushed a change to branch application-1.11
in repository https://gitbox.apache.org/repos/asf/flink.git.


      at a4ae9d8  [hotfix] minor refactoring in ApplicationDispatcherBootstrap

This branch includes the following new commits:

     new 0057edf  [FLINK-19154] ApplicationDispatcherBootstrap cleans up HA data only on FAILED, CANCELLED, SUCCEEDED
     new e24f70b  [FLINK-19154] Pass DispatcherGateway to DispatcherBootstrap
     new 54e8d11  [hotfix] Merge ApplicationDispatcherBootstrap#initialize() with constructor.
     new a4ae9d8  [hotfix] minor refactoring in ApplicationDispatcherBootstrap

The 4 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[flink] 03/04: [hotfix] Merge ApplicationDispatcherBootstrap#initialize() with constructor.

Posted by kk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

kkloudas pushed a commit to branch application-1.11
in repository https://gitbox.apache.org/repos/asf/flink.git

commit 54e8d11d67d8a6a3ebd3da6c2503a2d26dba6924
Author: Kostas Kloudas <kk...@gmail.com>
AuthorDate: Wed Oct 21 20:55:44 2020 +0200

    [hotfix] Merge ApplicationDispatcherBootstrap#initialize() with constructor.
---
 .../ApplicationDispatcherBootstrap.java            | 38 +++++-----
 ...ApplicationDispatcherGatewayServiceFactory.java |  3 +-
 .../ApplicationDispatcherBootstrapTest.java        | 84 ++++++++++++----------
 .../flink/runtime/dispatcher/Dispatcher.java       | 10 +--
 .../runtime/dispatcher/DispatcherBootstrap.java    | 13 +---
 ...tstrap.java => DispatcherBootstrapFactory.java} | 23 ++----
 .../runtime/dispatcher/DispatcherFactory.java      |  4 +-
 .../runtime/dispatcher/JobDispatcherFactory.java   |  4 +-
 .../flink/runtime/dispatcher/MiniDispatcher.java   |  4 +-
 .../dispatcher/NoOpDispatcherBootstrap.java        |  6 --
 .../dispatcher/SessionDispatcherFactory.java       |  4 +-
 .../runtime/dispatcher/StandaloneDispatcher.java   |  4 +-
 .../DefaultDispatcherGatewayServiceFactory.java    |  2 +-
 .../dispatcher/DispatcherResourceCleanupTest.java  |  2 +-
 .../flink/runtime/dispatcher/DispatcherTest.java   |  7 +-
 .../runtime/dispatcher/MiniDispatcherTest.java     |  2 +-
 .../runtime/dispatcher/TestingDispatcher.java      |  3 +-
 .../runner/DefaultDispatcherRunnerITCase.java      |  6 +-
 18 files changed, 97 insertions(+), 122 deletions(-)

diff --git a/flink-clients/src/main/java/org/apache/flink/client/deployment/application/ApplicationDispatcherBootstrap.java b/flink-clients/src/main/java/org/apache/flink/client/deployment/application/ApplicationDispatcherBootstrap.java
index a817048..1443804 100644
--- a/flink-clients/src/main/java/org/apache/flink/client/deployment/application/ApplicationDispatcherBootstrap.java
+++ b/flink-clients/src/main/java/org/apache/flink/client/deployment/application/ApplicationDispatcherBootstrap.java
@@ -84,7 +84,9 @@ public class ApplicationDispatcherBootstrap implements DispatcherBootstrap {
 
 	private final FatalErrorHandler errorHandler;
 
-	private CompletableFuture<Void> applicationCompletionFuture;
+	private final CompletableFuture<Void> applicationCompletionFuture;
+
+	private final CompletableFuture<Acknowledge> clusterShutdownFuture;
 
 	private ScheduledFuture<?> applicationExecutionTask;
 
@@ -92,20 +94,19 @@ public class ApplicationDispatcherBootstrap implements DispatcherBootstrap {
 			final PackagedProgram application,
 			final Collection<JobID> recoveredJobIds,
 			final Configuration configuration,
+			final DispatcherGateway dispatcherGateway,
+			final ScheduledExecutor scheduledExecutor,
 			final FatalErrorHandler errorHandler) {
 		this.configuration = checkNotNull(configuration);
 		this.recoveredJobIds = checkNotNull(recoveredJobIds);
 		this.application = checkNotNull(application);
 		this.errorHandler = checkNotNull(errorHandler);
-	}
 
-	@Override
-	public void initialize(final DispatcherGateway dispatcherGateway, ScheduledExecutor scheduledExecutor) {
-		checkNotNull(dispatcherGateway);
+		this.applicationCompletionFuture =
+				fixJobIdAndRunApplicationAsync(dispatcherGateway, scheduledExecutor);
 
-		runApplicationAndShutdownClusterAsync(
-				dispatcherGateway,
-				scheduledExecutor);
+		this.clusterShutdownFuture =
+				runApplicationAndShutdownClusterAsync(dispatcherGateway);
 	}
 
 	@Override
@@ -124,17 +125,21 @@ public class ApplicationDispatcherBootstrap implements DispatcherBootstrap {
 		return applicationExecutionTask;
 	}
 
+	@VisibleForTesting
+	CompletableFuture<Void> getApplicationCompletionFuture() {
+		return applicationCompletionFuture;
+	}
+
+	@VisibleForTesting
+	CompletableFuture<Acknowledge> getClusterShutdownFuture() {
+		return clusterShutdownFuture;
+	}
+
 	/**
 	 * Runs the user program entrypoint and shuts down the given dispatcherGateway when
 	 * the application completes (either successfully or in case of failure).
 	 */
-	@VisibleForTesting
-	CompletableFuture<Acknowledge> runApplicationAndShutdownClusterAsync(
-			final DispatcherGateway dispatcherGateway,
-			final ScheduledExecutor scheduledExecutor) {
-
-		applicationCompletionFuture = fixJobIdAndRunApplicationAsync(dispatcherGateway, scheduledExecutor);
-
+	private CompletableFuture<Acknowledge> runApplicationAndShutdownClusterAsync(final DispatcherGateway dispatcherGateway) {
 		return applicationCompletionFuture
 				.handle((r, t) -> {
 					if (t != null) {
@@ -163,8 +168,7 @@ public class ApplicationDispatcherBootstrap implements DispatcherBootstrap {
 				.thenCompose(Function.identity());
 	}
 
-	@VisibleForTesting
-	CompletableFuture<Void> fixJobIdAndRunApplicationAsync(
+	private CompletableFuture<Void> fixJobIdAndRunApplicationAsync(
 			final DispatcherGateway dispatcherGateway,
 			final ScheduledExecutor scheduledExecutor) {
 
diff --git a/flink-clients/src/main/java/org/apache/flink/client/deployment/application/ApplicationDispatcherGatewayServiceFactory.java b/flink-clients/src/main/java/org/apache/flink/client/deployment/application/ApplicationDispatcherGatewayServiceFactory.java
index a749fad..a492cf8 100644
--- a/flink-clients/src/main/java/org/apache/flink/client/deployment/application/ApplicationDispatcherGatewayServiceFactory.java
+++ b/flink-clients/src/main/java/org/apache/flink/client/deployment/application/ApplicationDispatcherGatewayServiceFactory.java
@@ -88,7 +88,8 @@ public class ApplicationDispatcherGatewayServiceFactory implements AbstractDispa
 					rpcService,
 					fencingToken,
 					recoveredJobs,
-					errorHandler -> new ApplicationDispatcherBootstrap(application, recoveredJobIds, configuration, errorHandler),
+					(dispatcherGateway, scheduledExecutor, errorHandler) -> new ApplicationDispatcherBootstrap(
+							application, recoveredJobIds, configuration, dispatcherGateway, scheduledExecutor, errorHandler),
 					PartialDispatcherServicesWithJobGraphStore.from(partialDispatcherServices, jobGraphWriter));
 		} catch (Exception e) {
 			throw new FlinkRuntimeException("Could not create the Dispatcher rpc endpoint.", e);
diff --git a/flink-clients/src/test/java/org/apache/flink/client/deployment/application/ApplicationDispatcherBootstrapTest.java b/flink-clients/src/test/java/org/apache/flink/client/deployment/application/ApplicationDispatcherBootstrapTest.java
index df1c786..09c8dbe 100644
--- a/flink-clients/src/test/java/org/apache/flink/client/deployment/application/ApplicationDispatcherBootstrapTest.java
+++ b/flink-clients/src/test/java/org/apache/flink/client/deployment/application/ApplicationDispatcherBootstrapTest.java
@@ -33,6 +33,7 @@ import org.apache.flink.runtime.client.JobExecutionException;
 import org.apache.flink.runtime.clusterframework.ApplicationStatus;
 import org.apache.flink.runtime.concurrent.ScheduledExecutor;
 import org.apache.flink.runtime.concurrent.ScheduledExecutorServiceAdapter;
+import org.apache.flink.runtime.dispatcher.DispatcherGateway;
 import org.apache.flink.runtime.jobmanager.HighAvailabilityMode;
 import org.apache.flink.runtime.jobmaster.JobResult;
 import org.apache.flink.runtime.messages.Acknowledge;
@@ -263,10 +264,11 @@ public class ApplicationDispatcherBootstrapTest {
 				})
 				.setRequestJobResultFunction(jobId -> CompletableFuture.completedFuture(createCancelledJobResult(jobId)));
 
-		ApplicationDispatcherBootstrap bootstrap = createApplicationDispatcherBootstrap(3);
+		ApplicationDispatcherBootstrap bootstrap = createApplicationDispatcherBootstrap(
+				3, dispatcherBuilder.build(), scheduledExecutor);
 
 		final CompletableFuture<Acknowledge> shutdownFuture =
-				bootstrap.runApplicationAndShutdownClusterAsync(dispatcherBuilder.build(), scheduledExecutor);
+				bootstrap.getClusterShutdownFuture();
 
 		// wait until the bootstrap "thinks" it's done, also makes sure that we don't
 		// fail the future exceptionally with a JobCancelledException
@@ -282,10 +284,10 @@ public class ApplicationDispatcherBootstrapTest {
 				.setRequestJobStatusFunction(jobId -> CompletableFuture.completedFuture(JobStatus.FINISHED))
 				.setRequestJobResultFunction(jobId -> CompletableFuture.completedFuture(createSuccessfulJobResult(jobId)));
 
-		ApplicationDispatcherBootstrap bootstrap = createApplicationDispatcherBootstrap(3);
+		ApplicationDispatcherBootstrap bootstrap = createApplicationDispatcherBootstrap(
+				3, dispatcherBuilder.build(), scheduledExecutor);
 
-		final CompletableFuture<Acknowledge> shutdownFuture =
-				bootstrap.runApplicationAndShutdownClusterAsync(dispatcherBuilder.build(), scheduledExecutor);
+		final CompletableFuture<Acknowledge> shutdownFuture = bootstrap.getClusterShutdownFuture();
 
 		ScheduledFuture<?> applicationExecutionFuture = bootstrap.getApplicationExecutionFuture();
 
@@ -306,10 +308,9 @@ public class ApplicationDispatcherBootstrapTest {
 		// In production, this will shut down the cluster with an exception.
 		final CompletableFuture<Void> errorHandlerFuture = new CompletableFuture<>();
 		final ApplicationDispatcherBootstrap bootstrap = createApplicationDispatcherBootstrap(
-				3, errorHandlerFuture::completeExceptionally);
+				3, dispatcherBuilder.build(), scheduledExecutor, errorHandlerFuture::completeExceptionally);
 
-		final CompletableFuture<Acknowledge> shutdownFuture =
-				bootstrap.runApplicationAndShutdownClusterAsync(dispatcherBuilder.build(), scheduledExecutor);
+		final CompletableFuture<Acknowledge> shutdownFuture = bootstrap.getClusterShutdownFuture();
 
 		ScheduledFuture<?> applicationExecutionFuture = bootstrap.getApplicationExecutionFuture();
 
@@ -335,10 +336,9 @@ public class ApplicationDispatcherBootstrapTest {
 		// In production, this will shut down the cluster with an exception.
 		final CompletableFuture<Void> errorHandlerFuture = new CompletableFuture<>();
 		final ApplicationDispatcherBootstrap bootstrap = createApplicationDispatcherBootstrap(
-				2, errorHandlerFuture::completeExceptionally);
+				2, dispatcherBuilder.build(), scheduledExecutor, errorHandlerFuture::completeExceptionally);
 
-		final CompletableFuture<Acknowledge> shutdownFuture =
-				bootstrap.runApplicationAndShutdownClusterAsync(dispatcherBuilder.build(), scheduledExecutor);
+		final CompletableFuture<Acknowledge> shutdownFuture = bootstrap.getClusterShutdownFuture();
 
 		bootstrap.stop();
 
@@ -363,12 +363,11 @@ public class ApplicationDispatcherBootstrapTest {
 		// we're "listening" on this to be completed to verify that the error handler is called.
 		// In production, this will shut down the cluster with an exception.
 		final CompletableFuture<Void> errorHandlerFuture = new CompletableFuture<>();
+		final TestingDispatcherGateway dispatcherGateway = dispatcherBuilder.build();
 		final ApplicationDispatcherBootstrap bootstrap = createApplicationDispatcherBootstrap(
-				3, errorHandlerFuture::completeExceptionally);
+				3, dispatcherGateway, scheduledExecutor, errorHandlerFuture::completeExceptionally);
 
-		final TestingDispatcherGateway dispatcherGateway = dispatcherBuilder.build();
-		final CompletableFuture<Acknowledge> shutdownFuture =
-				bootstrap.runApplicationAndShutdownClusterAsync(dispatcherGateway, scheduledExecutor);
+		final CompletableFuture<Acknowledge> shutdownFuture = bootstrap.getClusterShutdownFuture();
 
 		// we call the error handler
 		assertException(errorHandlerFuture, ApplicationExecutionException.class);
@@ -392,10 +391,10 @@ public class ApplicationDispatcherBootstrapTest {
 					return CompletableFuture.completedFuture(Acknowledge.get());
 				});
 
-		ApplicationDispatcherBootstrap bootstrap = createApplicationDispatcherBootstrap(3);
+		ApplicationDispatcherBootstrap bootstrap = createApplicationDispatcherBootstrap(
+				3, dispatcherBuilder.build(), scheduledExecutor);
 
-		final CompletableFuture<Acknowledge> shutdownFuture =
-				bootstrap.runApplicationAndShutdownClusterAsync(dispatcherBuilder.build(), scheduledExecutor);
+		final CompletableFuture<Acknowledge> shutdownFuture = bootstrap.getClusterShutdownFuture();
 
 		// wait until the bootstrap "thinks" it's done
 		shutdownFuture.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
@@ -419,10 +418,10 @@ public class ApplicationDispatcherBootstrapTest {
 					return CompletableFuture.completedFuture(Acknowledge.get());
 				});
 
-		ApplicationDispatcherBootstrap bootstrap = createApplicationDispatcherBootstrap(3);
+		ApplicationDispatcherBootstrap bootstrap = createApplicationDispatcherBootstrap(
+				3, dispatcherBuilder.build(), scheduledExecutor);
 
-		final CompletableFuture<Acknowledge> shutdownFuture =
-				bootstrap.runApplicationAndShutdownClusterAsync(dispatcherBuilder.build(), scheduledExecutor);
+		final CompletableFuture<Acknowledge> shutdownFuture = bootstrap.getClusterShutdownFuture();
 
 		// wait until the bootstrap "thinks" it's done
 		shutdownFuture.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
@@ -446,10 +445,10 @@ public class ApplicationDispatcherBootstrapTest {
 					return CompletableFuture.completedFuture(Acknowledge.get());
 				});
 
-		ApplicationDispatcherBootstrap bootstrap = createApplicationDispatcherBootstrap(3);
+		ApplicationDispatcherBootstrap bootstrap =
+				createApplicationDispatcherBootstrap(3, dispatcherBuilder.build(), scheduledExecutor);
 
-		final CompletableFuture<Acknowledge> shutdownFuture =
-				bootstrap.runApplicationAndShutdownClusterAsync(dispatcherBuilder.build(), scheduledExecutor);
+		final CompletableFuture<Acknowledge> shutdownFuture = bootstrap.getClusterShutdownFuture();
 
 		// wait until the bootstrap "thinks" it's done
 		shutdownFuture.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
@@ -471,11 +470,11 @@ public class ApplicationDispatcherBootstrapTest {
 					return CompletableFuture.completedFuture(Acknowledge.get());
 				});
 
-		final ApplicationDispatcherBootstrap bootstrap = createApplicationDispatcherBootstrap(3);
-
 		final TestingDispatcherGateway dispatcherGateway = dispatcherBuilder.build();
-		final CompletableFuture<Acknowledge> applicationFuture =
-				bootstrap.runApplicationAndShutdownClusterAsync(dispatcherGateway, scheduledExecutor);
+		final ApplicationDispatcherBootstrap bootstrap =
+				createApplicationDispatcherBootstrap(3, dispatcherGateway, scheduledExecutor);
+
+		final CompletableFuture<Acknowledge> applicationFuture = bootstrap.getClusterShutdownFuture();
 
 		final ApplicationFailureException exception = assertException(applicationFuture, ApplicationFailureException.class);
 		assertEquals(exception.getStatus(), ApplicationStatus.UNKNOWN);
@@ -509,22 +508,31 @@ public class ApplicationDispatcherBootstrapTest {
 
 		final ApplicationDispatcherBootstrap bootstrap =
 				new ApplicationDispatcherBootstrap(
-						program, Collections.emptyList(), configuration, exception -> {});
-
-		return bootstrap.fixJobIdAndRunApplicationAsync(
-				dispatcherBuilder.build(),
-				scheduledExecutor);
+						program,
+						Collections.emptyList(),
+						configuration,
+						dispatcherBuilder.build(),
+						scheduledExecutor,
+						exception -> {});
+
+		return bootstrap.getApplicationCompletionFuture();
 	}
 
-	private ApplicationDispatcherBootstrap createApplicationDispatcherBootstrap(int noOfJobs) throws FlinkException {
-		return createApplicationDispatcherBootstrap(noOfJobs, exception -> {});
+	private ApplicationDispatcherBootstrap createApplicationDispatcherBootstrap(
+			final int noOfJobs,
+			final DispatcherGateway dispatcherGateway,
+			final ScheduledExecutor scheduledExecutor) throws FlinkException {
+		return createApplicationDispatcherBootstrap(noOfJobs, dispatcherGateway, scheduledExecutor, exception -> {});
 	}
 
 	private ApplicationDispatcherBootstrap createApplicationDispatcherBootstrap(
-			int noOfJobs,
-			FatalErrorHandler errorHandler) throws FlinkException {
+			final int noOfJobs,
+			final DispatcherGateway dispatcherGateway,
+			final ScheduledExecutor scheduledExecutor,
+			final FatalErrorHandler errorHandler) throws FlinkException {
 		final PackagedProgram program = getProgram(noOfJobs);
-		return new ApplicationDispatcherBootstrap(program, Collections.emptyList(), getConfiguration(), errorHandler);
+		return new ApplicationDispatcherBootstrap(
+				program, Collections.emptyList(), getConfiguration(), dispatcherGateway, scheduledExecutor, errorHandler);
 	}
 
 	private PackagedProgram getProgram(int noOfJobs) throws FlinkException {
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/Dispatcher.java b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/Dispatcher.java
index 4aeba30..55e2c3d 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/Dispatcher.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/Dispatcher.java
@@ -122,7 +122,7 @@ public abstract class Dispatcher extends PermanentlyFencedRpcEndpoint<Dispatcher
 
 	private final Collection<JobGraph> recoveredJobs;
 
-	private final Function<FatalErrorHandler, DispatcherBootstrap> dispatcherBootstrapFactory;
+	private final DispatcherBootstrapFactory dispatcherBootstrapFactory;
 
 	private final ArchivedExecutionGraphStore archivedExecutionGraphStore;
 
@@ -145,7 +145,7 @@ public abstract class Dispatcher extends PermanentlyFencedRpcEndpoint<Dispatcher
 			RpcService rpcService,
 			DispatcherId fencingToken,
 			Collection<JobGraph> recoveredJobs,
-			Function<FatalErrorHandler, DispatcherBootstrap> dispatcherBootstrapFactory,
+			DispatcherBootstrapFactory dispatcherBootstrapFactory,
 			DispatcherServices dispatcherServices) throws Exception {
 		super(rpcService, AkkaRpcServiceUtils.createRandomName(DISPATCHER_NAME), fencingToken);
 		checkNotNull(dispatcherServices);
@@ -206,9 +206,11 @@ public abstract class Dispatcher extends PermanentlyFencedRpcEndpoint<Dispatcher
 			throw exception;
 		}
 
-		this.dispatcherBootstrap = this.dispatcherBootstrapFactory.apply(this::onFatalError);
 		startRecoveredJobs();
-		this.dispatcherBootstrap.initialize(getSelfGateway(DispatcherGateway.class), this.getRpcService().getScheduledExecutor());
+		this.dispatcherBootstrap = this.dispatcherBootstrapFactory.create(
+				getSelfGateway(DispatcherGateway.class),
+				this.getRpcService().getScheduledExecutor() ,
+				this::onFatalError);
 	}
 
 	private void startDispatcherServices() throws Exception {
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/DispatcherBootstrap.java b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/DispatcherBootstrap.java
index 5cf9336..d8b7b80 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/DispatcherBootstrap.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/DispatcherBootstrap.java
@@ -19,7 +19,6 @@
 package org.apache.flink.runtime.dispatcher;
 
 import org.apache.flink.annotation.Internal;
-import org.apache.flink.runtime.concurrent.ScheduledExecutor;
 
 /**
  * An interface containing the logic of bootstrapping the {@link Dispatcher} of a cluster.
@@ -28,18 +27,8 @@ import org.apache.flink.runtime.concurrent.ScheduledExecutor;
 public interface DispatcherBootstrap {
 
 	/**
-	 * Initializes the {@link Dispatcher} provided as an argument.
-	 *
-	 * <p>IMPORTANT: In HA settings, this method will run during
-	 * the initialization of the **leader** dispatcher.
-	 *
-	 * @param dispatcher the dispatcher to be initialized.
-	 */
-	void initialize(final DispatcherGateway dispatcher, ScheduledExecutor scheduledExecutor) throws Exception;
-
-	/**
 	 * Stops and frees any resources (e.g. threads) acquired
-	 * by the {@link #initialize(DispatcherGateway, ScheduledExecutor)}.
+	 * during the execution of the bootstrap.
 	 */
 	void stop() throws Exception;
 }
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/NoOpDispatcherBootstrap.java b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/DispatcherBootstrapFactory.java
similarity index 67%
copy from flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/NoOpDispatcherBootstrap.java
copy to flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/DispatcherBootstrapFactory.java
index 9d3934d..2cbba75 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/NoOpDispatcherBootstrap.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/DispatcherBootstrapFactory.java
@@ -20,25 +20,16 @@ package org.apache.flink.runtime.dispatcher;
 
 import org.apache.flink.annotation.Internal;
 import org.apache.flink.runtime.concurrent.ScheduledExecutor;
-import org.apache.flink.runtime.jobgraph.JobGraph;
+import org.apache.flink.runtime.rpc.FatalErrorHandler;
 
 /**
- * A {@link DispatcherBootstrap} which submits the provided {@link JobGraph job graphs}
- * for execution upon dispatcher initialization.
+ * A factory to create a {@link DispatcherBootstrap}.
  */
 @Internal
-public class NoOpDispatcherBootstrap implements DispatcherBootstrap {
+public interface DispatcherBootstrapFactory {
 
-	public NoOpDispatcherBootstrap() {
-	}
-
-	@Override
-	public void initialize(final DispatcherGateway dispatcher, ScheduledExecutor scheduledExecutor) {
-
-	}
-
-	@Override
-	public void stop() throws Exception {
-		// do nothing
-	}
+	DispatcherBootstrap create(
+			final DispatcherGateway dispatcher,
+			final ScheduledExecutor scheduledExecutor,
+			final FatalErrorHandler errorHandler) throws Exception;
 }
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/DispatcherFactory.java b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/DispatcherFactory.java
index 54a8338..2ad968d 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/DispatcherFactory.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/DispatcherFactory.java
@@ -19,11 +19,9 @@
 package org.apache.flink.runtime.dispatcher;
 
 import org.apache.flink.runtime.jobgraph.JobGraph;
-import org.apache.flink.runtime.rpc.FatalErrorHandler;
 import org.apache.flink.runtime.rpc.RpcService;
 
 import java.util.Collection;
-import java.util.function.Function;
 
 /**
  * {@link Dispatcher} factory interface.
@@ -37,6 +35,6 @@ public interface DispatcherFactory {
 			RpcService rpcService,
 			DispatcherId fencingToken,
 			Collection<JobGraph> recoveredJobs,
-			Function<FatalErrorHandler, DispatcherBootstrap> dispatcherBootstrapFactory,
+			DispatcherBootstrapFactory dispatcherBootstrapFactory,
 			PartialDispatcherServicesWithJobGraphStore partialDispatcherServicesWithJobGraphStore) throws Exception;
 }
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/JobDispatcherFactory.java b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/JobDispatcherFactory.java
index 7b08e10..c779c19 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/JobDispatcherFactory.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/JobDispatcherFactory.java
@@ -21,13 +21,11 @@ package org.apache.flink.runtime.dispatcher;
 import org.apache.flink.configuration.Configuration;
 import org.apache.flink.runtime.entrypoint.ClusterEntrypoint;
 import org.apache.flink.runtime.jobgraph.JobGraph;
-import org.apache.flink.runtime.rpc.FatalErrorHandler;
 import org.apache.flink.runtime.rpc.RpcService;
 
 import org.apache.flink.shaded.guava18.com.google.common.collect.Iterables;
 
 import java.util.Collection;
-import java.util.function.Function;
 
 import static org.apache.flink.runtime.entrypoint.ClusterEntrypoint.EXECUTION_MODE;
 
@@ -42,7 +40,7 @@ public enum JobDispatcherFactory implements DispatcherFactory {
 			RpcService rpcService,
 			DispatcherId fencingToken,
 			Collection<JobGraph> recoveredJobs,
-			Function<FatalErrorHandler, DispatcherBootstrap> dispatcherBootstrapFactory,
+			DispatcherBootstrapFactory dispatcherBootstrapFactory,
 			PartialDispatcherServicesWithJobGraphStore partialDispatcherServicesWithJobGraphStore) throws Exception {
 		final JobGraph jobGraph = Iterables.getOnlyElement(recoveredJobs);
 
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/MiniDispatcher.java b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/MiniDispatcher.java
index 568e456..b06453d 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/MiniDispatcher.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/MiniDispatcher.java
@@ -27,7 +27,6 @@ import org.apache.flink.runtime.executiongraph.ArchivedExecutionGraph;
 import org.apache.flink.runtime.jobgraph.JobGraph;
 import org.apache.flink.runtime.jobmaster.JobResult;
 import org.apache.flink.runtime.messages.Acknowledge;
-import org.apache.flink.runtime.rpc.FatalErrorHandler;
 import org.apache.flink.runtime.rpc.RpcService;
 import org.apache.flink.util.FlinkException;
 
@@ -36,7 +35,6 @@ import org.slf4j.LoggerFactory;
 
 import java.util.Collections;
 import java.util.concurrent.CompletableFuture;
-import java.util.function.Function;
 
 import static org.apache.flink.util.Preconditions.checkNotNull;
 
@@ -59,7 +57,7 @@ public class MiniDispatcher extends Dispatcher {
 			DispatcherId fencingToken,
 			DispatcherServices dispatcherServices,
 			JobGraph jobGraph,
-			Function<FatalErrorHandler, DispatcherBootstrap> dispatcherBootstrapFactory,
+			DispatcherBootstrapFactory dispatcherBootstrapFactory,
 			JobClusterEntrypoint.ExecutionMode executionMode) throws Exception {
 		super(
 			rpcService,
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/NoOpDispatcherBootstrap.java b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/NoOpDispatcherBootstrap.java
index 9d3934d..b4a3b36 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/NoOpDispatcherBootstrap.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/NoOpDispatcherBootstrap.java
@@ -19,7 +19,6 @@
 package org.apache.flink.runtime.dispatcher;
 
 import org.apache.flink.annotation.Internal;
-import org.apache.flink.runtime.concurrent.ScheduledExecutor;
 import org.apache.flink.runtime.jobgraph.JobGraph;
 
 /**
@@ -33,11 +32,6 @@ public class NoOpDispatcherBootstrap implements DispatcherBootstrap {
 	}
 
 	@Override
-	public void initialize(final DispatcherGateway dispatcher, ScheduledExecutor scheduledExecutor) {
-
-	}
-
-	@Override
 	public void stop() throws Exception {
 		// do nothing
 	}
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/SessionDispatcherFactory.java b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/SessionDispatcherFactory.java
index 05fcc14..3aa7b4b 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/SessionDispatcherFactory.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/SessionDispatcherFactory.java
@@ -19,11 +19,9 @@
 package org.apache.flink.runtime.dispatcher;
 
 import org.apache.flink.runtime.jobgraph.JobGraph;
-import org.apache.flink.runtime.rpc.FatalErrorHandler;
 import org.apache.flink.runtime.rpc.RpcService;
 
 import java.util.Collection;
-import java.util.function.Function;
 
 /**
  * {@link DispatcherFactory} which creates a {@link StandaloneDispatcher}.
@@ -36,7 +34,7 @@ public enum SessionDispatcherFactory implements DispatcherFactory {
 			RpcService rpcService,
 			DispatcherId fencingToken,
 			Collection<JobGraph> recoveredJobs,
-			Function<FatalErrorHandler, DispatcherBootstrap> dispatcherBootstrapFactory,
+			DispatcherBootstrapFactory dispatcherBootstrapFactory,
 			PartialDispatcherServicesWithJobGraphStore partialDispatcherServicesWithJobGraphStore) throws Exception {
 		// create the default dispatcher
 		return new StandaloneDispatcher(
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/StandaloneDispatcher.java b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/StandaloneDispatcher.java
index 6b7b942..8d88d6d 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/StandaloneDispatcher.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/StandaloneDispatcher.java
@@ -20,11 +20,9 @@ package org.apache.flink.runtime.dispatcher;
 
 import org.apache.flink.runtime.jobgraph.JobGraph;
 import org.apache.flink.runtime.jobmaster.JobMaster;
-import org.apache.flink.runtime.rpc.FatalErrorHandler;
 import org.apache.flink.runtime.rpc.RpcService;
 
 import java.util.Collection;
-import java.util.function.Function;
 
 /**
  * Dispatcher implementation which spawns a {@link JobMaster} for each
@@ -36,7 +34,7 @@ public class StandaloneDispatcher extends Dispatcher {
 			RpcService rpcService,
 			DispatcherId fencingToken,
 			Collection<JobGraph> recoveredJobs,
-			Function<FatalErrorHandler, DispatcherBootstrap> dispatcherBootstrapFactory,
+			DispatcherBootstrapFactory dispatcherBootstrapFactory,
 			DispatcherServices dispatcherServices) throws Exception {
 		super(
 			rpcService,
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/runner/DefaultDispatcherGatewayServiceFactory.java b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/runner/DefaultDispatcherGatewayServiceFactory.java
index adc1440..67a3231 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/runner/DefaultDispatcherGatewayServiceFactory.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/runner/DefaultDispatcherGatewayServiceFactory.java
@@ -63,7 +63,7 @@ class DefaultDispatcherGatewayServiceFactory implements AbstractDispatcherLeader
 				rpcService,
 				fencingToken,
 				recoveredJobs,
-				errorHandler -> new NoOpDispatcherBootstrap(),
+				(dispatcherGateway, scheduledExecutor, errorHandler) -> new NoOpDispatcherBootstrap(),
 				PartialDispatcherServicesWithJobGraphStore.from(partialDispatcherServices, jobGraphWriter));
 		} catch (Exception e) {
 			throw new FlinkRuntimeException("Could not create the Dispatcher rpc endpoint.", e);
diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/DispatcherResourceCleanupTest.java b/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/DispatcherResourceCleanupTest.java
index 06c9cc8b..fdbf165 100644
--- a/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/DispatcherResourceCleanupTest.java
+++ b/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/DispatcherResourceCleanupTest.java
@@ -194,7 +194,7 @@ public class DispatcherResourceCleanupTest extends TestLogger {
 			rpcService,
 			DispatcherId.generate(),
 			Collections.emptyList(),
-			errorHandler -> new NoOpDispatcherBootstrap(),
+			(dispatcher, scheduledExecutor, errorHandler) -> new NoOpDispatcherBootstrap(),
 			new DispatcherServices(
 				configuration,
 				highAvailabilityServices,
diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/DispatcherTest.java b/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/DispatcherTest.java
index 3ccc6b8..1ffaf86 100755
--- a/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/DispatcherTest.java
+++ b/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/DispatcherTest.java
@@ -99,7 +99,6 @@ import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
-import java.util.function.Function;
 
 import static org.hamcrest.Matchers.empty;
 import static org.hamcrest.Matchers.equalTo;
@@ -206,8 +205,8 @@ public class DispatcherTest extends TestLogger {
 
 		private Collection<JobGraph> initialJobGraphs = Collections.emptyList();
 
-		private Function<FatalErrorHandler, DispatcherBootstrap> dispatcherBootstrapFactory =
-				errorHandler -> new NoOpDispatcherBootstrap();
+		private DispatcherBootstrapFactory dispatcherBootstrapFactory =
+				(dispatcher, scheduledExecutor, errorHandler) -> new NoOpDispatcherBootstrap();
 
 		private HeartbeatServices heartbeatServices = DispatcherTest.this.heartbeatServices;
 
@@ -233,7 +232,7 @@ public class DispatcherTest extends TestLogger {
 		}
 
 		TestingDispatcherBuilder setDispatcherBootstrapFactory(
-				Function<FatalErrorHandler, DispatcherBootstrap> dispatcherBootstrapFactory) {
+				DispatcherBootstrapFactory dispatcherBootstrapFactory) {
 			this.dispatcherBootstrapFactory = dispatcherBootstrapFactory;
 			return this;
 		}
diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/MiniDispatcherTest.java b/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/MiniDispatcherTest.java
index 4794797..bcded74 100644
--- a/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/MiniDispatcherTest.java
+++ b/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/MiniDispatcherTest.java
@@ -251,7 +251,7 @@ public class MiniDispatcherTest extends TestLogger {
 				highAvailabilityServices.getJobGraphStore(),
 				testingJobManagerRunnerFactory),
 			jobGraph,
-			errorHandler -> new NoOpDispatcherBootstrap(),
+			(dispatcher, scheduledExecutor, errorHandler) -> new NoOpDispatcherBootstrap(),
 			executionMode);
 	}
 
diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/TestingDispatcher.java b/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/TestingDispatcher.java
index 7b2bbcb..646fcda 100644
--- a/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/TestingDispatcher.java
+++ b/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/TestingDispatcher.java
@@ -22,7 +22,6 @@ import org.apache.flink.api.common.JobID;
 import org.apache.flink.api.common.time.Time;
 import org.apache.flink.runtime.executiongraph.ArchivedExecutionGraph;
 import org.apache.flink.runtime.jobgraph.JobGraph;
-import org.apache.flink.runtime.rpc.FatalErrorHandler;
 import org.apache.flink.runtime.rpc.RpcService;
 
 import javax.annotation.Nonnull;
@@ -42,7 +41,7 @@ class TestingDispatcher extends Dispatcher {
 			RpcService rpcService,
 			DispatcherId fencingToken,
 			Collection<JobGraph> recoveredJobs,
-			Function<FatalErrorHandler, DispatcherBootstrap> dispatcherBootstrapFactory,
+			DispatcherBootstrapFactory dispatcherBootstrapFactory,
 			DispatcherServices dispatcherServices) throws Exception {
 		super(
 			rpcService,
diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/runner/DefaultDispatcherRunnerITCase.java b/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/runner/DefaultDispatcherRunnerITCase.java
index 51325bf..2d5613e 100644
--- a/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/runner/DefaultDispatcherRunnerITCase.java
+++ b/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/runner/DefaultDispatcherRunnerITCase.java
@@ -22,7 +22,7 @@ import org.apache.flink.api.common.JobID;
 import org.apache.flink.api.common.time.Time;
 import org.apache.flink.configuration.Configuration;
 import org.apache.flink.runtime.dispatcher.Dispatcher;
-import org.apache.flink.runtime.dispatcher.DispatcherBootstrap;
+import org.apache.flink.runtime.dispatcher.DispatcherBootstrapFactory;
 import org.apache.flink.runtime.dispatcher.DispatcherFactory;
 import org.apache.flink.runtime.dispatcher.DispatcherGateway;
 import org.apache.flink.runtime.dispatcher.DispatcherId;
@@ -44,7 +44,6 @@ import org.apache.flink.runtime.jobmanager.JobGraphStore;
 import org.apache.flink.runtime.jobmaster.TestingJobManagerRunner;
 import org.apache.flink.runtime.leaderelection.TestingLeaderElectionService;
 import org.apache.flink.runtime.metrics.groups.UnregisteredMetricGroups;
-import org.apache.flink.runtime.rpc.FatalErrorHandler;
 import org.apache.flink.runtime.rpc.RpcService;
 import org.apache.flink.runtime.rpc.TestingRpcServiceResource;
 import org.apache.flink.runtime.testingUtils.TestingUtils;
@@ -66,7 +65,6 @@ import java.util.Collection;
 import java.util.UUID;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.TimeUnit;
-import java.util.function.Function;
 
 import static org.hamcrest.Matchers.contains;
 import static org.hamcrest.Matchers.equalTo;
@@ -209,7 +207,7 @@ public class DefaultDispatcherRunnerITCase extends TestLogger {
 			RpcService rpcService,
 			DispatcherId fencingToken,
 			Collection<JobGraph> recoveredJobs,
-			Function<FatalErrorHandler, DispatcherBootstrap> dispatcherBootstrapFactory,
+			DispatcherBootstrapFactory dispatcherBootstrapFactory,
 			PartialDispatcherServicesWithJobGraphStore partialDispatcherServicesWithJobGraphStore) throws Exception {
 			return new StandaloneDispatcher(
 				rpcService,


[flink] 02/04: [FLINK-19154] Pass DispatcherGateway to DispatcherBootstrap

Posted by kk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

kkloudas pushed a commit to branch application-1.11
in repository https://gitbox.apache.org/repos/asf/flink.git

commit e24f70bbc0a280a6c16c6ee976d3b3232f47c48f
Author: Kostas Kloudas <kk...@gmail.com>
AuthorDate: Mon Oct 19 12:42:16 2020 +0200

    [FLINK-19154] Pass DispatcherGateway to DispatcherBootstrap
    
    In order to make sure that all calls from the
    ApplicationDispatcherBootstrap are executed from the main
    thread, we are passing the DispatcherGateway
    to the Bootstrap and not the Dispatcher itself.
---
 .../ApplicationDispatcherBootstrap.java            | 59 ++++++-------
 ...ApplicationDispatcherGatewayServiceFactory.java | 15 +++-
 .../ApplicationDispatcherBootstrapTest.java        | 97 ++++++++++++++++------
 .../dispatcher/AbstractDispatcherBootstrap.java    | 46 ----------
 .../flink/runtime/dispatcher/Dispatcher.java       | 19 ++++-
 .../runtime/dispatcher/DispatcherBootstrap.java    |  4 +-
 .../runtime/dispatcher/DispatcherFactory.java      |  3 +
 .../runtime/dispatcher/JobDispatcherFactory.java   |  8 ++
 .../flink/runtime/dispatcher/MiniDispatcher.java   |  3 +
 ...Bootstrap.java => NoOpDispatcherBootstrap.java} | 17 +---
 .../dispatcher/SessionDispatcherFactory.java       |  4 +
 .../runtime/dispatcher/StandaloneDispatcher.java   |  3 +
 .../DefaultDispatcherGatewayServiceFactory.java    |  9 +-
 .../dispatcher/DispatcherResourceCleanupTest.java  |  3 +-
 .../flink/runtime/dispatcher/DispatcherTest.java   | 14 +++-
 .../runtime/dispatcher/MiniDispatcherTest.java     |  4 +-
 .../runtime/dispatcher/TestingDispatcher.java      |  4 +
 .../runner/DefaultDispatcherRunnerITCase.java      |  2 +
 18 files changed, 176 insertions(+), 138 deletions(-)

diff --git a/flink-clients/src/main/java/org/apache/flink/client/deployment/application/ApplicationDispatcherBootstrap.java b/flink-clients/src/main/java/org/apache/flink/client/deployment/application/ApplicationDispatcherBootstrap.java
index e581e29..a817048 100644
--- a/flink-clients/src/main/java/org/apache/flink/client/deployment/application/ApplicationDispatcherBootstrap.java
+++ b/flink-clients/src/main/java/org/apache/flink/client/deployment/application/ApplicationDispatcherBootstrap.java
@@ -33,8 +33,6 @@ import org.apache.flink.core.execution.PipelineExecutorServiceLoader;
 import org.apache.flink.runtime.clusterframework.ApplicationStatus;
 import org.apache.flink.runtime.concurrent.FutureUtils;
 import org.apache.flink.runtime.concurrent.ScheduledExecutor;
-import org.apache.flink.runtime.dispatcher.AbstractDispatcherBootstrap;
-import org.apache.flink.runtime.dispatcher.Dispatcher;
 import org.apache.flink.runtime.dispatcher.DispatcherBootstrap;
 import org.apache.flink.runtime.dispatcher.DispatcherGateway;
 import org.apache.flink.runtime.jobgraph.JobGraph;
@@ -43,6 +41,7 @@ import org.apache.flink.runtime.jobmaster.JobResult;
 import org.apache.flink.runtime.messages.Acknowledge;
 import org.apache.flink.runtime.rpc.FatalErrorHandler;
 import org.apache.flink.util.ExceptionUtils;
+import org.apache.flink.util.FlinkException;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -71,7 +70,7 @@ import static org.apache.flink.util.Preconditions.checkNotNull;
  * if it should submit a job for execution (in case of a new job) or the job was already recovered and is running.
  */
 @Internal
-public class ApplicationDispatcherBootstrap extends AbstractDispatcherBootstrap {
+public class ApplicationDispatcherBootstrap implements DispatcherBootstrap {
 
 	private static final Logger LOG = LoggerFactory.getLogger(ApplicationDispatcherBootstrap.class);
 
@@ -79,7 +78,7 @@ public class ApplicationDispatcherBootstrap extends AbstractDispatcherBootstrap
 
 	private final PackagedProgram application;
 
-	private final Collection<JobGraph> recoveredJobs;
+	private final Collection<JobID> recoveredJobIds;
 
 	private final Configuration configuration;
 
@@ -91,22 +90,21 @@ public class ApplicationDispatcherBootstrap extends AbstractDispatcherBootstrap
 
 	public ApplicationDispatcherBootstrap(
 			final PackagedProgram application,
-			final Collection<JobGraph> recoveredJobs,
+			final Collection<JobID> recoveredJobIds,
 			final Configuration configuration,
 			final FatalErrorHandler errorHandler) {
 		this.configuration = checkNotNull(configuration);
-		this.recoveredJobs = checkNotNull(recoveredJobs);
+		this.recoveredJobIds = checkNotNull(recoveredJobIds);
 		this.application = checkNotNull(application);
 		this.errorHandler = checkNotNull(errorHandler);
 	}
 
 	@Override
-	public void initialize(final Dispatcher dispatcher, ScheduledExecutor scheduledExecutor) {
-		checkNotNull(dispatcher);
-		launchRecoveredJobGraphs(dispatcher, recoveredJobs);
+	public void initialize(final DispatcherGateway dispatcherGateway, ScheduledExecutor scheduledExecutor) {
+		checkNotNull(dispatcherGateway);
 
 		runApplicationAndShutdownClusterAsync(
-				dispatcher,
+				dispatcherGateway,
 				scheduledExecutor);
 	}
 
@@ -127,16 +125,15 @@ public class ApplicationDispatcherBootstrap extends AbstractDispatcherBootstrap
 	}
 
 	/**
-	 * Runs the user program entrypoint using {@link #runApplicationAsync(DispatcherGateway,
-	 * ScheduledExecutor, boolean)} and shuts down the given dispatcher when the application
-	 * completes (either successfully or in case of failure).
+	 * Runs the user program entrypoint and shuts down the given dispatcherGateway when
+	 * the application completes (either successfully or in case of failure).
 	 */
 	@VisibleForTesting
 	CompletableFuture<Acknowledge> runApplicationAndShutdownClusterAsync(
-			final DispatcherGateway dispatcher,
+			final DispatcherGateway dispatcherGateway,
 			final ScheduledExecutor scheduledExecutor) {
 
-		applicationCompletionFuture = fixJobIdAndRunApplicationAsync(dispatcher, scheduledExecutor);
+		applicationCompletionFuture = fixJobIdAndRunApplicationAsync(dispatcherGateway, scheduledExecutor);
 
 		return applicationCompletionFuture
 				.handle((r, t) -> {
@@ -150,38 +147,38 @@ public class ApplicationDispatcherBootstrap extends AbstractDispatcherBootstrap
 
 							if (applicationStatus == ApplicationStatus.CANCELED || applicationStatus == ApplicationStatus.FAILED) {
 								LOG.info("Application {}: ", applicationStatus, t);
-								return dispatcher.shutDownCluster(applicationStatus);
+								return dispatcherGateway.shutDownCluster(applicationStatus);
 							}
 						}
 
 						LOG.warn("Exiting with Application Status UNKNOWN: ", t);
-						this.errorHandler.onFatalError(t);
+						this.errorHandler.onFatalError(new FlinkException("Application failed unexpectedly.", t));
 
 						return FutureUtils.<Acknowledge>completedExceptionally(t);
 					}
 
 					LOG.info("Application completed SUCCESSFULLY");
-					return dispatcher.shutDownCluster(ApplicationStatus.SUCCEEDED);
+					return dispatcherGateway.shutDownCluster(ApplicationStatus.SUCCEEDED);
 				})
 				.thenCompose(Function.identity());
 	}
 
 	@VisibleForTesting
 	CompletableFuture<Void> fixJobIdAndRunApplicationAsync(
-			final DispatcherGateway dispatcher,
+			final DispatcherGateway dispatcherGateway,
 			final ScheduledExecutor scheduledExecutor) {
 
 		final Optional<String> configuredJobId =
 				configuration.getOptional(PipelineOptionsInternal.PIPELINE_FIXED_JOB_ID);
 
 		if (!HighAvailabilityMode.isHighAvailabilityModeActivated(configuration) && !configuredJobId.isPresent()) {
-			return runApplicationAsync(dispatcher, scheduledExecutor, false);
+			return runApplicationAsync(dispatcherGateway, scheduledExecutor, false);
 		}
 
 		if (!configuredJobId.isPresent()) {
 			configuration.set(PipelineOptionsInternal.PIPELINE_FIXED_JOB_ID, ZERO_JOB_ID.toHexString());
 		}
-		return runApplicationAsync(dispatcher, scheduledExecutor, true);
+		return runApplicationAsync(dispatcherGateway, scheduledExecutor, true);
 	}
 
 	/**
@@ -190,7 +187,7 @@ public class ApplicationDispatcherBootstrap extends AbstractDispatcherBootstrap
 	 * succeeded. if any of them fails, or if job submission fails.
 	 */
 	private CompletableFuture<Void> runApplicationAsync(
-			final DispatcherGateway dispatcher,
+			final DispatcherGateway dispatcherGateway,
 			final ScheduledExecutor scheduledExecutor,
 			final boolean enforceSingleJobExecution) {
 		final CompletableFuture<List<JobID>> applicationExecutionFuture = new CompletableFuture<>();
@@ -200,14 +197,14 @@ public class ApplicationDispatcherBootstrap extends AbstractDispatcherBootstrap
 		applicationExecutionTask = scheduledExecutor.schedule(
 				() -> runApplicationEntryPoint(
 						applicationExecutionFuture,
-						dispatcher,
+						dispatcherGateway,
 						scheduledExecutor,
 						enforceSingleJobExecution),
 				0L,
 				TimeUnit.MILLISECONDS);
 
 		return applicationExecutionFuture.thenCompose(
-				jobIds -> getApplicationResult(dispatcher, jobIds, scheduledExecutor));
+				jobIds -> getApplicationResult(dispatcherGateway, jobIds, scheduledExecutor));
 	}
 
 	/**
@@ -218,16 +215,15 @@ public class ApplicationDispatcherBootstrap extends AbstractDispatcherBootstrap
 	 */
 	private void runApplicationEntryPoint(
 			final CompletableFuture<List<JobID>> jobIdsFuture,
-			final DispatcherGateway dispatcher,
+			final DispatcherGateway dispatcherGateway,
 			final ScheduledExecutor scheduledExecutor,
 			final boolean enforceSingleJobExecution) {
 		try {
-			final List<JobID> applicationJobIds =
-					new ArrayList<>(getRecoveredJobIds(recoveredJobs));
+			final List<JobID> applicationJobIds = new ArrayList<>(recoveredJobIds);
 
 			final PipelineExecutorServiceLoader executorServiceLoader =
 					new EmbeddedExecutorServiceLoader(
-							applicationJobIds, dispatcher, scheduledExecutor);
+							applicationJobIds, dispatcherGateway, scheduledExecutor);
 
 			ClientUtils.executeProgram(
 					executorServiceLoader,
@@ -288,11 +284,4 @@ public class ApplicationDispatcherBootstrap extends AbstractDispatcherBootstrap
 							result, application.getUserCodeClassLoader()));
 		});
 	}
-
-	private List<JobID> getRecoveredJobIds(final Collection<JobGraph> recoveredJobs) {
-		return recoveredJobs
-				.stream()
-				.map(JobGraph::getJobID)
-				.collect(Collectors.toList());
-	}
 }
diff --git a/flink-clients/src/main/java/org/apache/flink/client/deployment/application/ApplicationDispatcherGatewayServiceFactory.java b/flink-clients/src/main/java/org/apache/flink/client/deployment/application/ApplicationDispatcherGatewayServiceFactory.java
index 1389b34..a749fad 100644
--- a/flink-clients/src/main/java/org/apache/flink/client/deployment/application/ApplicationDispatcherGatewayServiceFactory.java
+++ b/flink-clients/src/main/java/org/apache/flink/client/deployment/application/ApplicationDispatcherGatewayServiceFactory.java
@@ -19,6 +19,7 @@
 package org.apache.flink.client.deployment.application;
 
 import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.JobID;
 import org.apache.flink.client.program.PackagedProgram;
 import org.apache.flink.configuration.Configuration;
 import org.apache.flink.runtime.dispatcher.Dispatcher;
@@ -34,6 +35,8 @@ import org.apache.flink.runtime.rpc.RpcService;
 import org.apache.flink.util.FlinkRuntimeException;
 
 import java.util.Collection;
+import java.util.List;
+import java.util.stream.Collectors;
 
 import static org.apache.flink.util.Preconditions.checkNotNull;
 
@@ -77,12 +80,15 @@ public class ApplicationDispatcherGatewayServiceFactory implements AbstractDispa
 			Collection<JobGraph> recoveredJobs,
 			JobGraphWriter jobGraphWriter) {
 
+		final List<JobID> recoveredJobIds = getRecoveredJobIds(recoveredJobs);
+
 		final Dispatcher dispatcher;
 		try {
 			dispatcher = dispatcherFactory.createDispatcher(
 					rpcService,
 					fencingToken,
-					errorHandler -> new ApplicationDispatcherBootstrap(application, recoveredJobs, configuration, errorHandler),
+					recoveredJobs,
+					errorHandler -> new ApplicationDispatcherBootstrap(application, recoveredJobIds, configuration, errorHandler),
 					PartialDispatcherServicesWithJobGraphStore.from(partialDispatcherServices, jobGraphWriter));
 		} catch (Exception e) {
 			throw new FlinkRuntimeException("Could not create the Dispatcher rpc endpoint.", e);
@@ -92,4 +98,11 @@ public class ApplicationDispatcherGatewayServiceFactory implements AbstractDispa
 
 		return DefaultDispatcherGatewayService.from(dispatcher);
 	}
+
+	private List<JobID> getRecoveredJobIds(final Collection<JobGraph> recoveredJobs) {
+		return recoveredJobs
+				.stream()
+				.map(JobGraph::getJobID)
+				.collect(Collectors.toList());
+	}
 }
diff --git a/flink-clients/src/test/java/org/apache/flink/client/deployment/application/ApplicationDispatcherBootstrapTest.java b/flink-clients/src/test/java/org/apache/flink/client/deployment/application/ApplicationDispatcherBootstrapTest.java
index 0d0fc03..df1c786 100644
--- a/flink-clients/src/test/java/org/apache/flink/client/deployment/application/ApplicationDispatcherBootstrapTest.java
+++ b/flink-clients/src/test/java/org/apache/flink/client/deployment/application/ApplicationDispatcherBootstrapTest.java
@@ -33,7 +33,6 @@ import org.apache.flink.runtime.client.JobExecutionException;
 import org.apache.flink.runtime.clusterframework.ApplicationStatus;
 import org.apache.flink.runtime.concurrent.ScheduledExecutor;
 import org.apache.flink.runtime.concurrent.ScheduledExecutorServiceAdapter;
-import org.apache.flink.runtime.jobgraph.JobGraph;
 import org.apache.flink.runtime.jobmanager.HighAvailabilityMode;
 import org.apache.flink.runtime.jobmaster.JobResult;
 import org.apache.flink.runtime.messages.Acknowledge;
@@ -51,7 +50,6 @@ import org.junit.Test;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.net.MalformedURLException;
-import java.util.Collection;
 import java.util.Collections;
 import java.util.Optional;
 import java.util.concurrent.CancellationException;
@@ -235,18 +233,8 @@ public class ApplicationDispatcherBootstrapTest {
 				});
 
 		final CompletableFuture<Void> applicationFuture = runApplication(dispatcherBuilder, 2);
-
-		try {
-			applicationFuture.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
-			fail("Expected exception but the execution went smoothly.");
-		} catch (Throwable e) {
-			Optional<ApplicationFailureException> expectedException =
-					ExceptionUtils.findThrowable(e, ApplicationFailureException.class);
-			if (!expectedException.isPresent()) {
-				throw e;
-			}
-			assertEquals(expectedException.get().getStatus(), ApplicationStatus.FAILED);
-		}
+		final ApplicationFailureException exception = assertException(applicationFuture, ApplicationFailureException.class);
+		assertEquals(exception.getStatus(), ApplicationStatus.FAILED);
 	}
 
 	@Test
@@ -366,6 +354,10 @@ public class ApplicationDispatcherBootstrapTest {
 		final TestingDispatcherGateway.Builder dispatcherBuilder = new TestingDispatcherGateway.Builder()
 				.setSubmitFunction(jobGraph -> {
 					throw new FlinkRuntimeException("Nope!");
+				})
+				.setClusterShutdownFunction(status -> {
+					fail("We should not call shutdownCluster()");
+					return CompletableFuture.completedFuture(Acknowledge.get());
 				});
 
 		// we're "listening" on this to be completed to verify that the error handler is called.
@@ -374,8 +366,9 @@ public class ApplicationDispatcherBootstrapTest {
 		final ApplicationDispatcherBootstrap bootstrap = createApplicationDispatcherBootstrap(
 				3, errorHandlerFuture::completeExceptionally);
 
+		final TestingDispatcherGateway dispatcherGateway = dispatcherBuilder.build();
 		final CompletableFuture<Acknowledge> shutdownFuture =
-				bootstrap.runApplicationAndShutdownClusterAsync(dispatcherBuilder.build(), scheduledExecutor);
+				bootstrap.runApplicationAndShutdownClusterAsync(dispatcherGateway, scheduledExecutor);
 
 		// we call the error handler
 		assertException(errorHandlerFuture, ApplicationExecutionException.class);
@@ -438,6 +431,56 @@ public class ApplicationDispatcherBootstrapTest {
 		assertThat(externalShutdownFuture.get(TIMEOUT_SECONDS, TimeUnit.SECONDS), is(ApplicationStatus.FAILED));
 	}
 
+	@Test
+	public void testClusterShutdownWhenApplicationGetsCancelled() throws Exception {
+		// we're "listening" on this to be completed to verify that the cluster
+		// is being shut down from the ApplicationDispatcherBootstrap
+		final CompletableFuture<ApplicationStatus> externalShutdownFuture = new CompletableFuture<>();
+
+		final TestingDispatcherGateway.Builder dispatcherBuilder = new TestingDispatcherGateway.Builder()
+				.setSubmitFunction(jobGraph -> CompletableFuture.completedFuture(Acknowledge.get()))
+				.setRequestJobStatusFunction(jobId -> CompletableFuture.completedFuture(JobStatus.CANCELED))
+				.setRequestJobResultFunction(jobId -> CompletableFuture.completedFuture(createCancelledJobResult(jobId)))
+				.setClusterShutdownFunction((status) -> {
+					externalShutdownFuture.complete(status);
+					return CompletableFuture.completedFuture(Acknowledge.get());
+				});
+
+		ApplicationDispatcherBootstrap bootstrap = createApplicationDispatcherBootstrap(3);
+
+		final CompletableFuture<Acknowledge> shutdownFuture =
+				bootstrap.runApplicationAndShutdownClusterAsync(dispatcherBuilder.build(), scheduledExecutor);
+
+		// wait until the bootstrap "thinks" it's done
+		shutdownFuture.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
+
+		// verify that the dispatcher is actually being shut down
+		assertThat(externalShutdownFuture.get(TIMEOUT_SECONDS, TimeUnit.SECONDS), is(ApplicationStatus.CANCELED));
+	}
+
+	@Test
+	public void testClusterDoesNOTShutdownWhenApplicationStatusUknown() throws Exception {
+		// we're "listening" on this to be completed to verify that the cluster
+		// is being shut down from the ApplicationDispatcherBootstrap
+		final TestingDispatcherGateway.Builder dispatcherBuilder = new TestingDispatcherGateway.Builder()
+				.setSubmitFunction(jobGraph -> CompletableFuture.completedFuture(Acknowledge.get()))
+				.setRequestJobStatusFunction(jobId -> CompletableFuture.completedFuture(JobStatus.FAILED))
+				.setRequestJobResultFunction(jobId -> CompletableFuture.completedFuture(createUnknownJobResult(jobId)))
+				.setClusterShutdownFunction(status -> {
+					fail("We should not call shutdownCluster()");
+					return CompletableFuture.completedFuture(Acknowledge.get());
+				});
+
+		final ApplicationDispatcherBootstrap bootstrap = createApplicationDispatcherBootstrap(3);
+
+		final TestingDispatcherGateway dispatcherGateway = dispatcherBuilder.build();
+		final CompletableFuture<Acknowledge> applicationFuture =
+				bootstrap.runApplicationAndShutdownClusterAsync(dispatcherGateway, scheduledExecutor);
+
+		final ApplicationFailureException exception = assertException(applicationFuture, ApplicationFailureException.class);
+		assertEquals(exception.getStatus(), ApplicationStatus.UNKNOWN);
+	}
+
 	private CompletableFuture<Void> runApplication(
 			TestingDispatcherGateway.Builder dispatcherBuilder,
 			int noOfJobs) throws FlinkException {
@@ -480,15 +523,8 @@ public class ApplicationDispatcherBootstrapTest {
 	private ApplicationDispatcherBootstrap createApplicationDispatcherBootstrap(
 			int noOfJobs,
 			FatalErrorHandler errorHandler) throws FlinkException {
-		return createApplicationDispatcherBootstrap(noOfJobs, Collections.emptyList(), errorHandler);
-	}
-
-	private ApplicationDispatcherBootstrap createApplicationDispatcherBootstrap(
-			int noOfJobs,
-			Collection<JobGraph> recoveredJobGraphs,
-			FatalErrorHandler errorHandler) throws FlinkException {
 		final PackagedProgram program = getProgram(noOfJobs);
-		return new ApplicationDispatcherBootstrap(program, recoveredJobGraphs, getConfiguration(), errorHandler);
+		return new ApplicationDispatcherBootstrap(program, Collections.emptyList(), getConfiguration(), errorHandler);
 	}
 
 	private PackagedProgram getProgram(int noOfJobs) throws FlinkException {
@@ -503,6 +539,15 @@ public class ApplicationDispatcherBootstrapTest {
 		}
 	}
 
+	private static JobResult createUnknownJobResult(final JobID jobId) {
+		return new JobResult.Builder()
+				.jobId(jobId)
+				.netRuntime(2L)
+				.applicationStatus(ApplicationStatus.UNKNOWN)
+				.serializedThrowable(new SerializedThrowable(new JobExecutionException(jobId, "unknown bla bla bla")))
+				.build();
+	}
+
 	private static JobResult createFailedJobResult(final JobID jobId) {
 		return new JobResult.Builder()
 				.jobId(jobId)
@@ -531,7 +576,7 @@ public class ApplicationDispatcherBootstrapTest {
 				.build();
 	}
 
-	private static <T, E extends Throwable> void assertException(
+	private static <T, E extends Throwable> E assertException(
 			CompletableFuture<T> future,
 			Class<E> exceptionClass) throws Exception {
 
@@ -543,9 +588,9 @@ public class ApplicationDispatcherBootstrapTest {
 			if (!expectionException.isPresent()) {
 				throw e;
 			}
-			return;
+			return expectionException.get();
 		}
-		fail("Future should have completed exceptionally with " + exceptionClass.getCanonicalName() + ".");
+		throw new Exception("Future should have completed exceptionally with " + exceptionClass.getCanonicalName() + ".");
 	}
 
 	private Configuration getConfiguration() {
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/AbstractDispatcherBootstrap.java b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/AbstractDispatcherBootstrap.java
deleted file mode 100644
index 9dde71e..0000000
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/AbstractDispatcherBootstrap.java
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.flink.runtime.dispatcher;
-
-import org.apache.flink.annotation.Internal;
-import org.apache.flink.runtime.jobgraph.JobGraph;
-
-import java.util.Collection;
-
-import static org.apache.flink.util.Preconditions.checkNotNull;
-
-/**
- * A base class for a {@link DispatcherBootstrap}.
- *
- * <p>NOTE TO IMPLEMENTERS: This is meant as a bridge between the package-private
- * {@link Dispatcher#runRecoveredJob(JobGraph)} method, and dispatcher bootstrap
- * implementations in other packages.
- */
-@Internal
-public abstract class AbstractDispatcherBootstrap implements DispatcherBootstrap {
-
-	protected void launchRecoveredJobGraphs(final Dispatcher dispatcher, final Collection<JobGraph> recoveredJobGraphs) {
-		checkNotNull(dispatcher);
-		checkNotNull(recoveredJobGraphs);
-
-		for (JobGraph recoveredJob : recoveredJobGraphs) {
-			dispatcher.runRecoveredJob(recoveredJob);
-		}
-	}
-}
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/Dispatcher.java b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/Dispatcher.java
index 013d22c..4aeba30 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/Dispatcher.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/Dispatcher.java
@@ -120,6 +120,8 @@ public abstract class Dispatcher extends PermanentlyFencedRpcEndpoint<Dispatcher
 
 	private final Map<JobID, CompletableFuture<JobManagerRunner>> jobManagerRunnerFutures;
 
+	private final Collection<JobGraph> recoveredJobs;
+
 	private final Function<FatalErrorHandler, DispatcherBootstrap> dispatcherBootstrapFactory;
 
 	private final ArchivedExecutionGraphStore archivedExecutionGraphStore;
@@ -142,6 +144,7 @@ public abstract class Dispatcher extends PermanentlyFencedRpcEndpoint<Dispatcher
 	public Dispatcher(
 			RpcService rpcService,
 			DispatcherId fencingToken,
+			Collection<JobGraph> recoveredJobs,
 			Function<FatalErrorHandler, DispatcherBootstrap> dispatcherBootstrapFactory,
 			DispatcherServices dispatcherServices) throws Exception {
 		super(rpcService, AkkaRpcServiceUtils.createRandomName(DISPATCHER_NAME), fencingToken);
@@ -177,6 +180,8 @@ public abstract class Dispatcher extends PermanentlyFencedRpcEndpoint<Dispatcher
 		this.shutDownFuture = new CompletableFuture<>();
 
 		this.dispatcherBootstrapFactory = checkNotNull(dispatcherBootstrapFactory);
+
+		this.recoveredJobs = new HashSet<>(recoveredJobs);
 	}
 
 	//------------------------------------------------------
@@ -201,8 +206,9 @@ public abstract class Dispatcher extends PermanentlyFencedRpcEndpoint<Dispatcher
 			throw exception;
 		}
 
-		this.dispatcherBootstrap = this.dispatcherBootstrapFactory.apply(shutDownFuture::completeExceptionally);
-		this.dispatcherBootstrap.initialize(this, this.getRpcService().getScheduledExecutor());
+		this.dispatcherBootstrap = this.dispatcherBootstrapFactory.apply(this::onFatalError);
+		startRecoveredJobs();
+		this.dispatcherBootstrap.initialize(getSelfGateway(DispatcherGateway.class), this.getRpcService().getScheduledExecutor());
 	}
 
 	private void startDispatcherServices() throws Exception {
@@ -213,7 +219,14 @@ public abstract class Dispatcher extends PermanentlyFencedRpcEndpoint<Dispatcher
 		}
 	}
 
-	void runRecoveredJob(final JobGraph recoveredJob) {
+	private void startRecoveredJobs() {
+		for (JobGraph recoveredJob : recoveredJobs) {
+			runRecoveredJob(recoveredJob);
+		}
+		recoveredJobs.clear();
+	}
+
+	private void runRecoveredJob(final JobGraph recoveredJob) {
 		checkNotNull(recoveredJob);
 		FutureUtils.assertNoException(runJob(recoveredJob)
 			.handle(handleRecoveredJobStartError(recoveredJob.getJobID())));
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/DispatcherBootstrap.java b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/DispatcherBootstrap.java
index e0a327b..5cf9336 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/DispatcherBootstrap.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/DispatcherBootstrap.java
@@ -35,11 +35,11 @@ public interface DispatcherBootstrap {
 	 *
 	 * @param dispatcher the dispatcher to be initialized.
 	 */
-	void initialize(final Dispatcher dispatcher, ScheduledExecutor scheduledExecutor) throws Exception;
+	void initialize(final DispatcherGateway dispatcher, ScheduledExecutor scheduledExecutor) throws Exception;
 
 	/**
 	 * Stops and frees any resources (e.g. threads) acquired
-	 * by the {@link #initialize(Dispatcher, ScheduledExecutor)}.
+	 * by the {@link #initialize(DispatcherGateway, ScheduledExecutor)}.
 	 */
 	void stop() throws Exception;
 }
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/DispatcherFactory.java b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/DispatcherFactory.java
index e1f3827..54a8338 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/DispatcherFactory.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/DispatcherFactory.java
@@ -18,9 +18,11 @@
 
 package org.apache.flink.runtime.dispatcher;
 
+import org.apache.flink.runtime.jobgraph.JobGraph;
 import org.apache.flink.runtime.rpc.FatalErrorHandler;
 import org.apache.flink.runtime.rpc.RpcService;
 
+import java.util.Collection;
 import java.util.function.Function;
 
 /**
@@ -34,6 +36,7 @@ public interface DispatcherFactory {
 	Dispatcher createDispatcher(
 			RpcService rpcService,
 			DispatcherId fencingToken,
+			Collection<JobGraph> recoveredJobs,
 			Function<FatalErrorHandler, DispatcherBootstrap> dispatcherBootstrapFactory,
 			PartialDispatcherServicesWithJobGraphStore partialDispatcherServicesWithJobGraphStore) throws Exception;
 }
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/JobDispatcherFactory.java b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/JobDispatcherFactory.java
index a68da40..7b08e10 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/JobDispatcherFactory.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/JobDispatcherFactory.java
@@ -20,9 +20,13 @@ package org.apache.flink.runtime.dispatcher;
 
 import org.apache.flink.configuration.Configuration;
 import org.apache.flink.runtime.entrypoint.ClusterEntrypoint;
+import org.apache.flink.runtime.jobgraph.JobGraph;
 import org.apache.flink.runtime.rpc.FatalErrorHandler;
 import org.apache.flink.runtime.rpc.RpcService;
 
+import org.apache.flink.shaded.guava18.com.google.common.collect.Iterables;
+
+import java.util.Collection;
 import java.util.function.Function;
 
 import static org.apache.flink.runtime.entrypoint.ClusterEntrypoint.EXECUTION_MODE;
@@ -37,8 +41,11 @@ public enum JobDispatcherFactory implements DispatcherFactory {
 	public MiniDispatcher createDispatcher(
 			RpcService rpcService,
 			DispatcherId fencingToken,
+			Collection<JobGraph> recoveredJobs,
 			Function<FatalErrorHandler, DispatcherBootstrap> dispatcherBootstrapFactory,
 			PartialDispatcherServicesWithJobGraphStore partialDispatcherServicesWithJobGraphStore) throws Exception {
+		final JobGraph jobGraph = Iterables.getOnlyElement(recoveredJobs);
+
 		final Configuration configuration = partialDispatcherServicesWithJobGraphStore.getConfiguration();
 		final String executionModeValue = configuration.getString(EXECUTION_MODE);
 		final ClusterEntrypoint.ExecutionMode executionMode = ClusterEntrypoint.ExecutionMode.valueOf(executionModeValue);
@@ -47,6 +54,7 @@ public enum JobDispatcherFactory implements DispatcherFactory {
 			rpcService,
 			fencingToken,
 			DispatcherServices.from(partialDispatcherServicesWithJobGraphStore, DefaultJobManagerRunnerFactory.INSTANCE),
+			jobGraph,
 			dispatcherBootstrapFactory,
 			executionMode);
 	}
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/MiniDispatcher.java b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/MiniDispatcher.java
index 79872ff..568e456 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/MiniDispatcher.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/MiniDispatcher.java
@@ -34,6 +34,7 @@ import org.apache.flink.util.FlinkException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.util.Collections;
 import java.util.concurrent.CompletableFuture;
 import java.util.function.Function;
 
@@ -57,11 +58,13 @@ public class MiniDispatcher extends Dispatcher {
 			RpcService rpcService,
 			DispatcherId fencingToken,
 			DispatcherServices dispatcherServices,
+			JobGraph jobGraph,
 			Function<FatalErrorHandler, DispatcherBootstrap> dispatcherBootstrapFactory,
 			JobClusterEntrypoint.ExecutionMode executionMode) throws Exception {
 		super(
 			rpcService,
 			fencingToken,
+			Collections.singleton(jobGraph),
 			dispatcherBootstrapFactory,
 			dispatcherServices);
 
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/DefaultDispatcherBootstrap.java b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/NoOpDispatcherBootstrap.java
similarity index 68%
rename from flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/DefaultDispatcherBootstrap.java
rename to flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/NoOpDispatcherBootstrap.java
index 871b5a4..9d3934d 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/DefaultDispatcherBootstrap.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/NoOpDispatcherBootstrap.java
@@ -22,28 +22,19 @@ import org.apache.flink.annotation.Internal;
 import org.apache.flink.runtime.concurrent.ScheduledExecutor;
 import org.apache.flink.runtime.jobgraph.JobGraph;
 
-import java.util.Collection;
-import java.util.HashSet;
-
-import static org.apache.flink.util.Preconditions.checkNotNull;
-
 /**
  * A {@link DispatcherBootstrap} which submits the provided {@link JobGraph job graphs}
  * for execution upon dispatcher initialization.
  */
 @Internal
-public class DefaultDispatcherBootstrap extends AbstractDispatcherBootstrap {
+public class NoOpDispatcherBootstrap implements DispatcherBootstrap {
 
-	private final Collection<JobGraph> recoveredJobs;
-
-	public DefaultDispatcherBootstrap(final Collection<JobGraph> recoveredJobsGraphs) {
-		this.recoveredJobs = new HashSet<>(checkNotNull(recoveredJobsGraphs));
+	public NoOpDispatcherBootstrap() {
 	}
 
 	@Override
-	public void initialize(final Dispatcher dispatcher, ScheduledExecutor scheduledExecutor) {
-		launchRecoveredJobGraphs(dispatcher, recoveredJobs);
-		recoveredJobs.clear();
+	public void initialize(final DispatcherGateway dispatcher, ScheduledExecutor scheduledExecutor) {
+
 	}
 
 	@Override
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/SessionDispatcherFactory.java b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/SessionDispatcherFactory.java
index 35f8d32..05fcc14 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/SessionDispatcherFactory.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/SessionDispatcherFactory.java
@@ -18,9 +18,11 @@
 
 package org.apache.flink.runtime.dispatcher;
 
+import org.apache.flink.runtime.jobgraph.JobGraph;
 import org.apache.flink.runtime.rpc.FatalErrorHandler;
 import org.apache.flink.runtime.rpc.RpcService;
 
+import java.util.Collection;
 import java.util.function.Function;
 
 /**
@@ -33,12 +35,14 @@ public enum SessionDispatcherFactory implements DispatcherFactory {
 	public StandaloneDispatcher createDispatcher(
 			RpcService rpcService,
 			DispatcherId fencingToken,
+			Collection<JobGraph> recoveredJobs,
 			Function<FatalErrorHandler, DispatcherBootstrap> dispatcherBootstrapFactory,
 			PartialDispatcherServicesWithJobGraphStore partialDispatcherServicesWithJobGraphStore) throws Exception {
 		// create the default dispatcher
 		return new StandaloneDispatcher(
 			rpcService,
 			fencingToken,
+			recoveredJobs,
 			dispatcherBootstrapFactory,
 			DispatcherServices.from(partialDispatcherServicesWithJobGraphStore, DefaultJobManagerRunnerFactory.INSTANCE));
 	}
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/StandaloneDispatcher.java b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/StandaloneDispatcher.java
index 65bd4a3..6b7b942 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/StandaloneDispatcher.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/StandaloneDispatcher.java
@@ -23,6 +23,7 @@ import org.apache.flink.runtime.jobmaster.JobMaster;
 import org.apache.flink.runtime.rpc.FatalErrorHandler;
 import org.apache.flink.runtime.rpc.RpcService;
 
+import java.util.Collection;
 import java.util.function.Function;
 
 /**
@@ -34,11 +35,13 @@ public class StandaloneDispatcher extends Dispatcher {
 	public StandaloneDispatcher(
 			RpcService rpcService,
 			DispatcherId fencingToken,
+			Collection<JobGraph> recoveredJobs,
 			Function<FatalErrorHandler, DispatcherBootstrap> dispatcherBootstrapFactory,
 			DispatcherServices dispatcherServices) throws Exception {
 		super(
 			rpcService,
 			fencingToken,
+			recoveredJobs,
 			dispatcherBootstrapFactory,
 			dispatcherServices);
 	}
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/runner/DefaultDispatcherGatewayServiceFactory.java b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/runner/DefaultDispatcherGatewayServiceFactory.java
index e5312d7..adc1440 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/runner/DefaultDispatcherGatewayServiceFactory.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/runner/DefaultDispatcherGatewayServiceFactory.java
@@ -18,11 +18,10 @@
 
 package org.apache.flink.runtime.dispatcher.runner;
 
-import org.apache.flink.runtime.dispatcher.DefaultDispatcherBootstrap;
 import org.apache.flink.runtime.dispatcher.Dispatcher;
-import org.apache.flink.runtime.dispatcher.DispatcherBootstrap;
 import org.apache.flink.runtime.dispatcher.DispatcherFactory;
 import org.apache.flink.runtime.dispatcher.DispatcherId;
+import org.apache.flink.runtime.dispatcher.NoOpDispatcherBootstrap;
 import org.apache.flink.runtime.dispatcher.PartialDispatcherServices;
 import org.apache.flink.runtime.dispatcher.PartialDispatcherServicesWithJobGraphStore;
 import org.apache.flink.runtime.jobgraph.JobGraph;
@@ -58,15 +57,13 @@ class DefaultDispatcherGatewayServiceFactory implements AbstractDispatcherLeader
 			Collection<JobGraph> recoveredJobs,
 			JobGraphWriter jobGraphWriter) {
 
-		final DispatcherBootstrap bootstrap =
-				new DefaultDispatcherBootstrap(recoveredJobs);
-
 		final Dispatcher dispatcher;
 		try {
 			dispatcher = dispatcherFactory.createDispatcher(
 				rpcService,
 				fencingToken,
-				errorHandler -> bootstrap,
+				recoveredJobs,
+				errorHandler -> new NoOpDispatcherBootstrap(),
 				PartialDispatcherServicesWithJobGraphStore.from(partialDispatcherServices, jobGraphWriter));
 		} catch (Exception e) {
 			throw new FlinkRuntimeException("Could not create the Dispatcher rpc endpoint.", e);
diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/DispatcherResourceCleanupTest.java b/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/DispatcherResourceCleanupTest.java
index bc0cad5..06c9cc8b 100644
--- a/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/DispatcherResourceCleanupTest.java
+++ b/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/DispatcherResourceCleanupTest.java
@@ -193,7 +193,8 @@ public class DispatcherResourceCleanupTest extends TestLogger {
 		dispatcher = new TestingDispatcher(
 			rpcService,
 			DispatcherId.generate(),
-			errorHandler -> new DefaultDispatcherBootstrap(Collections.emptyList()),
+			Collections.emptyList(),
+			errorHandler -> new NoOpDispatcherBootstrap(),
 			new DispatcherServices(
 				configuration,
 				highAvailabilityServices,
diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/DispatcherTest.java b/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/DispatcherTest.java
index 8215bc4..3ccc6b8 100755
--- a/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/DispatcherTest.java
+++ b/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/DispatcherTest.java
@@ -204,8 +204,10 @@ public class DispatcherTest extends TestLogger {
 
 	private class TestingDispatcherBuilder {
 
+		private Collection<JobGraph> initialJobGraphs = Collections.emptyList();
+
 		private Function<FatalErrorHandler, DispatcherBootstrap> dispatcherBootstrapFactory =
-				errorHandler -> new DefaultDispatcherBootstrap(Collections.emptyList());
+				errorHandler -> new NoOpDispatcherBootstrap();
 
 		private HeartbeatServices heartbeatServices = DispatcherTest.this.heartbeatServices;
 
@@ -225,6 +227,11 @@ public class DispatcherTest extends TestLogger {
 			return this;
 		}
 
+		TestingDispatcherBuilder setInitialJobGraphs(Collection<JobGraph> initialJobGraphs) {
+			this.initialJobGraphs = initialJobGraphs;
+			return this;
+		}
+
 		TestingDispatcherBuilder setDispatcherBootstrapFactory(
 				Function<FatalErrorHandler, DispatcherBootstrap> dispatcherBootstrapFactory) {
 			this.dispatcherBootstrapFactory = dispatcherBootstrapFactory;
@@ -249,6 +256,7 @@ public class DispatcherTest extends TestLogger {
 			return new TestingDispatcher(
 				rpcService,
 				DispatcherId.generate(),
+				initialJobGraphs,
 				dispatcherBootstrapFactory,
 				new DispatcherServices(
 					configuration,
@@ -449,7 +457,7 @@ public class DispatcherTest extends TestLogger {
 		final JobGraph failingJobGraph = createFailingJobGraph(testException);
 
 		dispatcher = new TestingDispatcherBuilder()
-			.setDispatcherBootstrapFactory(errorHandler -> new DefaultDispatcherBootstrap(Collections.singleton(failingJobGraph)))
+			.setInitialJobGraphs(Collections.singleton(failingJobGraph))
 			.build();
 
 		dispatcher.start();
@@ -601,7 +609,7 @@ public class DispatcherTest extends TestLogger {
 			.build();
 
 		dispatcher = new TestingDispatcherBuilder()
-			.setDispatcherBootstrapFactory(errorHandler -> new DefaultDispatcherBootstrap(Collections.singleton(jobGraph)))
+			.setInitialJobGraphs(Collections.singleton(jobGraph))
 			.setJobGraphWriter(testingJobGraphStore)
 			.build();
 		dispatcher.start();
diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/MiniDispatcherTest.java b/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/MiniDispatcherTest.java
index 02dedab..4794797 100644
--- a/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/MiniDispatcherTest.java
+++ b/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/MiniDispatcherTest.java
@@ -52,7 +52,6 @@ import org.junit.rules.TemporaryFolder;
 import javax.annotation.Nonnull;
 
 import java.io.IOException;
-import java.util.Collections;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.TimeoutException;
@@ -251,7 +250,8 @@ public class MiniDispatcherTest extends TestLogger {
 				UnregisteredMetricGroups.createUnregisteredJobManagerMetricGroup(),
 				highAvailabilityServices.getJobGraphStore(),
 				testingJobManagerRunnerFactory),
-			errorHandler -> new DefaultDispatcherBootstrap(Collections.singletonList(jobGraph)),
+			jobGraph,
+			errorHandler -> new NoOpDispatcherBootstrap(),
 			executionMode);
 	}
 
diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/TestingDispatcher.java b/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/TestingDispatcher.java
index fe6472a..7b2bbcb 100644
--- a/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/TestingDispatcher.java
+++ b/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/TestingDispatcher.java
@@ -21,11 +21,13 @@ package org.apache.flink.runtime.dispatcher;
 import org.apache.flink.api.common.JobID;
 import org.apache.flink.api.common.time.Time;
 import org.apache.flink.runtime.executiongraph.ArchivedExecutionGraph;
+import org.apache.flink.runtime.jobgraph.JobGraph;
 import org.apache.flink.runtime.rpc.FatalErrorHandler;
 import org.apache.flink.runtime.rpc.RpcService;
 
 import javax.annotation.Nonnull;
 
+import java.util.Collection;
 import java.util.concurrent.CompletableFuture;
 import java.util.function.Function;
 
@@ -39,11 +41,13 @@ class TestingDispatcher extends Dispatcher {
 	TestingDispatcher(
 			RpcService rpcService,
 			DispatcherId fencingToken,
+			Collection<JobGraph> recoveredJobs,
 			Function<FatalErrorHandler, DispatcherBootstrap> dispatcherBootstrapFactory,
 			DispatcherServices dispatcherServices) throws Exception {
 		super(
 			rpcService,
 			fencingToken,
+			recoveredJobs,
 			dispatcherBootstrapFactory,
 			dispatcherServices);
 
diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/runner/DefaultDispatcherRunnerITCase.java b/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/runner/DefaultDispatcherRunnerITCase.java
index d31a7f8..51325bf 100644
--- a/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/runner/DefaultDispatcherRunnerITCase.java
+++ b/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/runner/DefaultDispatcherRunnerITCase.java
@@ -208,11 +208,13 @@ public class DefaultDispatcherRunnerITCase extends TestLogger {
 		public Dispatcher createDispatcher(
 			RpcService rpcService,
 			DispatcherId fencingToken,
+			Collection<JobGraph> recoveredJobs,
 			Function<FatalErrorHandler, DispatcherBootstrap> dispatcherBootstrapFactory,
 			PartialDispatcherServicesWithJobGraphStore partialDispatcherServicesWithJobGraphStore) throws Exception {
 			return new StandaloneDispatcher(
 				rpcService,
 				fencingToken,
+				recoveredJobs,
 				dispatcherBootstrapFactory,
 				DispatcherServices.from(partialDispatcherServicesWithJobGraphStore, jobManagerRunnerFactory));
 		}


[flink] 04/04: [hotfix] minor refactoring in ApplicationDispatcherBootstrap

Posted by kk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

kkloudas pushed a commit to branch application-1.11
in repository https://gitbox.apache.org/repos/asf/flink.git

commit a4ae9d8386e74d17edd0b549de537080e1a676d9
Author: Kostas Kloudas <kk...@gmail.com>
AuthorDate: Wed Oct 21 21:00:27 2020 +0200

    [hotfix] minor refactoring in ApplicationDispatcherBootstrap
---
 .../ApplicationDispatcherBootstrap.java            | 32 +++++++++++-----------
 1 file changed, 16 insertions(+), 16 deletions(-)

diff --git a/flink-clients/src/main/java/org/apache/flink/client/deployment/application/ApplicationDispatcherBootstrap.java b/flink-clients/src/main/java/org/apache/flink/client/deployment/application/ApplicationDispatcherBootstrap.java
index 1443804..48dd704 100644
--- a/flink-clients/src/main/java/org/apache/flink/client/deployment/application/ApplicationDispatcherBootstrap.java
+++ b/flink-clients/src/main/java/org/apache/flink/client/deployment/application/ApplicationDispatcherBootstrap.java
@@ -142,28 +142,28 @@ public class ApplicationDispatcherBootstrap implements DispatcherBootstrap {
 	private CompletableFuture<Acknowledge> runApplicationAndShutdownClusterAsync(final DispatcherGateway dispatcherGateway) {
 		return applicationCompletionFuture
 				.handle((r, t) -> {
-					if (t != null) {
 
-						final Optional<ApplicationFailureException> exception =
-								ExceptionUtils.findThrowable(t, ApplicationFailureException.class);
-
-						if (exception.isPresent()) {
-							final ApplicationStatus applicationStatus = exception.get().getStatus();
+					if (t == null) {
+						LOG.info("Application completed SUCCESSFULLY");
+						return dispatcherGateway.shutDownCluster(ApplicationStatus.SUCCEEDED);
+					}
 
-							if (applicationStatus == ApplicationStatus.CANCELED || applicationStatus == ApplicationStatus.FAILED) {
-								LOG.info("Application {}: ", applicationStatus, t);
-								return dispatcherGateway.shutDownCluster(applicationStatus);
-							}
-						}
+					final Optional<ApplicationFailureException> exception =
+							ExceptionUtils.findThrowable(t, ApplicationFailureException.class);
 
-						LOG.warn("Exiting with Application Status UNKNOWN: ", t);
-						this.errorHandler.onFatalError(new FlinkException("Application failed unexpectedly.", t));
+					if (exception.isPresent()) {
+						final ApplicationStatus applicationStatus = exception.get().getStatus();
 
-						return FutureUtils.<Acknowledge>completedExceptionally(t);
+						if (applicationStatus == ApplicationStatus.CANCELED || applicationStatus == ApplicationStatus.FAILED) {
+							LOG.info("Application {}: ", applicationStatus, t);
+							return dispatcherGateway.shutDownCluster(applicationStatus);
+						}
 					}
 
-					LOG.info("Application completed SUCCESSFULLY");
-					return dispatcherGateway.shutDownCluster(ApplicationStatus.SUCCEEDED);
+					LOG.warn("Application failed unexpectedly: ", t);
+					this.errorHandler.onFatalError(new FlinkException("Application failed unexpectedly.", t));
+
+					return FutureUtils.<Acknowledge>completedExceptionally(t);
 				})
 				.thenCompose(Function.identity());
 	}


[flink] 01/04: [FLINK-19154] ApplicationDispatcherBootstrap cleans up HA data only on FAILED, CANCELLED, SUCCEEDED

Posted by kk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

kkloudas pushed a commit to branch application-1.11
in repository https://gitbox.apache.org/repos/asf/flink.git

commit 0057edf19168bd415f5c9b34e6b642c6ba460d96
Author: Kostas Kloudas <kk...@gmail.com>
AuthorDate: Mon Oct 19 09:49:14 2020 +0200

    [FLINK-19154] ApplicationDispatcherBootstrap cleans up HA data only on FAILED, CANCELLED, SUCCEEDED
    
    Depending on the status with which a job got terminated, we may
    want to shutdown the cluster and clean up all HA data, or not. To
    be able to differentiate between the different termination reasons
    we add the ApplicationFailureException.
    
    In addition, to be able to shutdown the cluster without cleaning up
    the HA data, we need to be able to terminate the dispatcher's shutdown future
    with an exception. This is what the new error handler pass in the
    ApplicationDispatcherBootstrap does. We chose to pass the FatalErrorHandler
    as a constructor argument because this allows for more robust code.
---
 .../ApplicationDispatcherBootstrap.java            | 68 +++++++---------
 ...ApplicationDispatcherGatewayServiceFactory.java |  6 +-
 .../application/ApplicationFailureException.java   | 82 +++++++++++++++++++
 .../ApplicationDispatcherBootstrapTest.java        | 93 +++++++++++++---------
 .../flink/runtime/dispatcher/Dispatcher.java       | 11 ++-
 .../runtime/dispatcher/DispatcherFactory.java      | 11 ++-
 .../runtime/dispatcher/JobDispatcherFactory.java   |  7 +-
 .../flink/runtime/dispatcher/MiniDispatcher.java   |  6 +-
 .../dispatcher/SessionDispatcherFactory.java       |  7 +-
 .../runtime/dispatcher/StandaloneDispatcher.java   |  7 +-
 .../DefaultDispatcherGatewayServiceFactory.java    |  2 +-
 .../dispatcher/DispatcherResourceCleanupTest.java  |  2 +-
 .../flink/runtime/dispatcher/DispatcherTest.java   | 15 ++--
 .../runtime/dispatcher/MiniDispatcherTest.java     |  2 +-
 .../runtime/dispatcher/TestingDispatcher.java      |  5 +-
 .../runner/DefaultDispatcherRunnerITCase.java      |  6 +-
 16 files changed, 218 insertions(+), 112 deletions(-)

diff --git a/flink-clients/src/main/java/org/apache/flink/client/deployment/application/ApplicationDispatcherBootstrap.java b/flink-clients/src/main/java/org/apache/flink/client/deployment/application/ApplicationDispatcherBootstrap.java
index 2b4ddf3..e581e29 100644
--- a/flink-clients/src/main/java/org/apache/flink/client/deployment/application/ApplicationDispatcherBootstrap.java
+++ b/flink-clients/src/main/java/org/apache/flink/client/deployment/application/ApplicationDispatcherBootstrap.java
@@ -30,7 +30,6 @@ import org.apache.flink.client.program.PackagedProgram;
 import org.apache.flink.configuration.Configuration;
 import org.apache.flink.configuration.PipelineOptionsInternal;
 import org.apache.flink.core.execution.PipelineExecutorServiceLoader;
-import org.apache.flink.runtime.client.JobCancellationException;
 import org.apache.flink.runtime.clusterframework.ApplicationStatus;
 import org.apache.flink.runtime.concurrent.FutureUtils;
 import org.apache.flink.runtime.concurrent.ScheduledExecutor;
@@ -42,8 +41,8 @@ import org.apache.flink.runtime.jobgraph.JobGraph;
 import org.apache.flink.runtime.jobmanager.HighAvailabilityMode;
 import org.apache.flink.runtime.jobmaster.JobResult;
 import org.apache.flink.runtime.messages.Acknowledge;
+import org.apache.flink.runtime.rpc.FatalErrorHandler;
 import org.apache.flink.util.ExceptionUtils;
-import org.apache.flink.util.SerializedThrowable;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -52,7 +51,6 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
 import java.util.Optional;
-import java.util.concurrent.CancellationException;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.CompletionException;
 import java.util.concurrent.ScheduledFuture;
@@ -85,6 +83,8 @@ public class ApplicationDispatcherBootstrap extends AbstractDispatcherBootstrap
 
 	private final Configuration configuration;
 
+	private final FatalErrorHandler errorHandler;
+
 	private CompletableFuture<Void> applicationCompletionFuture;
 
 	private ScheduledFuture<?> applicationExecutionTask;
@@ -92,10 +92,12 @@ public class ApplicationDispatcherBootstrap extends AbstractDispatcherBootstrap
 	public ApplicationDispatcherBootstrap(
 			final PackagedProgram application,
 			final Collection<JobGraph> recoveredJobs,
-			final Configuration configuration) {
+			final Configuration configuration,
+			final FatalErrorHandler errorHandler) {
 		this.configuration = checkNotNull(configuration);
 		this.recoveredJobs = checkNotNull(recoveredJobs);
 		this.application = checkNotNull(application);
+		this.errorHandler = checkNotNull(errorHandler);
 	}
 
 	@Override
@@ -138,28 +140,28 @@ public class ApplicationDispatcherBootstrap extends AbstractDispatcherBootstrap
 
 		return applicationCompletionFuture
 				.handle((r, t) -> {
-					final ApplicationStatus applicationStatus;
 					if (t != null) {
 
-						final Optional<JobCancellationException> cancellationException =
-								ExceptionUtils.findThrowable(t, JobCancellationException.class);
-
-						if (cancellationException.isPresent()) {
-							// this means the Flink Job was cancelled
-							applicationStatus = ApplicationStatus.CANCELED;
-						} else if (t instanceof CancellationException) {
-							// this means that the future was cancelled
-							applicationStatus = ApplicationStatus.UNKNOWN;
-						} else {
-							applicationStatus = ApplicationStatus.FAILED;
+						final Optional<ApplicationFailureException> exception =
+								ExceptionUtils.findThrowable(t, ApplicationFailureException.class);
+
+						if (exception.isPresent()) {
+							final ApplicationStatus applicationStatus = exception.get().getStatus();
+
+							if (applicationStatus == ApplicationStatus.CANCELED || applicationStatus == ApplicationStatus.FAILED) {
+								LOG.info("Application {}: ", applicationStatus, t);
+								return dispatcher.shutDownCluster(applicationStatus);
+							}
 						}
 
-						LOG.warn("Application {}: ", applicationStatus, t);
-					} else {
-						applicationStatus = ApplicationStatus.SUCCEEDED;
-						LOG.info("Application completed SUCCESSFULLY");
+						LOG.warn("Exiting with Application Status UNKNOWN: ", t);
+						this.errorHandler.onFatalError(t);
+
+						return FutureUtils.<Acknowledge>completedExceptionally(t);
 					}
-					return dispatcher.shutDownCluster(applicationStatus);
+
+					LOG.info("Application completed SUCCESSFULLY");
+					return dispatcher.shutDownCluster(ApplicationStatus.SUCCEEDED);
 				})
 				.thenCompose(Function.identity());
 	}
@@ -275,31 +277,15 @@ public class ApplicationDispatcherBootstrap extends AbstractDispatcherBootstrap
 	 * Otherwise, this returns a future that is finished exceptionally (potentially with an
 	 * exception from the {@link JobResult}.
 	 */
-	private CompletableFuture<JobResult> unwrapJobResultException(
-			CompletableFuture<JobResult> jobResult) {
+	private CompletableFuture<JobResult> unwrapJobResultException(final CompletableFuture<JobResult> jobResult) {
 		return jobResult.thenApply(result -> {
 			if (result.isSuccess()) {
 				return result;
 			}
 
-			Optional<SerializedThrowable> serializedThrowable = result.getSerializedThrowable();
-
-			if (result.getApplicationStatus() == ApplicationStatus.CANCELED) {
-				throw new CompletionException(
-						new JobCancellationException(
-								result.getJobId(),
-								"Job was cancelled.",
-								serializedThrowable.orElse(null)));
-			}
-
-			if (serializedThrowable.isPresent()) {
-				Throwable throwable =
-						serializedThrowable
-								.get()
-								.deserializeError(application.getUserCodeClassLoader());
-				throw new CompletionException(throwable);
-			}
-			throw new RuntimeException("Job execution failed for unknown reason.");
+			throw new CompletionException(
+					ApplicationFailureException.fromJobResult(
+							result, application.getUserCodeClassLoader()));
 		});
 	}
 
diff --git a/flink-clients/src/main/java/org/apache/flink/client/deployment/application/ApplicationDispatcherGatewayServiceFactory.java b/flink-clients/src/main/java/org/apache/flink/client/deployment/application/ApplicationDispatcherGatewayServiceFactory.java
index fdac2ec..1389b34 100644
--- a/flink-clients/src/main/java/org/apache/flink/client/deployment/application/ApplicationDispatcherGatewayServiceFactory.java
+++ b/flink-clients/src/main/java/org/apache/flink/client/deployment/application/ApplicationDispatcherGatewayServiceFactory.java
@@ -22,7 +22,6 @@ import org.apache.flink.annotation.Internal;
 import org.apache.flink.client.program.PackagedProgram;
 import org.apache.flink.configuration.Configuration;
 import org.apache.flink.runtime.dispatcher.Dispatcher;
-import org.apache.flink.runtime.dispatcher.DispatcherBootstrap;
 import org.apache.flink.runtime.dispatcher.DispatcherFactory;
 import org.apache.flink.runtime.dispatcher.DispatcherId;
 import org.apache.flink.runtime.dispatcher.PartialDispatcherServices;
@@ -78,15 +77,12 @@ public class ApplicationDispatcherGatewayServiceFactory implements AbstractDispa
 			Collection<JobGraph> recoveredJobs,
 			JobGraphWriter jobGraphWriter) {
 
-		final DispatcherBootstrap bootstrap =
-				new ApplicationDispatcherBootstrap(application, recoveredJobs, configuration);
-
 		final Dispatcher dispatcher;
 		try {
 			dispatcher = dispatcherFactory.createDispatcher(
 					rpcService,
 					fencingToken,
-					bootstrap,
+					errorHandler -> new ApplicationDispatcherBootstrap(application, recoveredJobs, configuration, errorHandler),
 					PartialDispatcherServicesWithJobGraphStore.from(partialDispatcherServices, jobGraphWriter));
 		} catch (Exception e) {
 			throw new FlinkRuntimeException("Could not create the Dispatcher rpc endpoint.", e);
diff --git a/flink-clients/src/main/java/org/apache/flink/client/deployment/application/ApplicationFailureException.java b/flink-clients/src/main/java/org/apache/flink/client/deployment/application/ApplicationFailureException.java
new file mode 100644
index 0000000..7bb8719
--- /dev/null
+++ b/flink-clients/src/main/java/org/apache/flink/client/deployment/application/ApplicationFailureException.java
@@ -0,0 +1,82 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.client.deployment.application;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.JobID;
+import org.apache.flink.runtime.clusterframework.ApplicationStatus;
+import org.apache.flink.runtime.jobmaster.JobResult;
+import org.apache.flink.util.FlinkException;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * Exception that signals the failure of an
+ * application with a given {@link ApplicationStatus}.
+ */
+@Internal
+public class ApplicationFailureException extends FlinkException {
+
+	private final JobID jobID;
+
+	private final ApplicationStatus status;
+
+	public ApplicationFailureException(
+			final JobID jobID,
+			final ApplicationStatus status,
+			final String message,
+			final Throwable cause) {
+		super(message, cause);
+		this.jobID = jobID;
+		this.status = checkNotNull(status);
+	}
+
+	public JobID getJobID() {
+		return jobID;
+	}
+
+	public ApplicationStatus getStatus() {
+		return status;
+	}
+
+	public static ApplicationFailureException fromJobResult(
+			final JobResult result,
+			final ClassLoader userClassLoader) {
+
+		checkState(result != null && !result.isSuccess());
+		checkNotNull(userClassLoader);
+
+		final JobID jobID = result.getJobId();
+		final ApplicationStatus status = result.getApplicationStatus();
+
+		final Throwable throwable = result
+				.getSerializedThrowable()
+				.map(ser -> ser.deserializeError(userClassLoader))
+				.orElse(new FlinkException("Unknown reason."));
+
+		if (status == ApplicationStatus.CANCELED || status == ApplicationStatus.FAILED) {
+			return new ApplicationFailureException(
+					jobID, status, "Application Status: " + status.name(), throwable);
+		}
+
+		return new ApplicationFailureException(
+				jobID, ApplicationStatus.UNKNOWN, "Job failed for unknown reason.", throwable);
+	}
+}
diff --git a/flink-clients/src/test/java/org/apache/flink/client/deployment/application/ApplicationDispatcherBootstrapTest.java b/flink-clients/src/test/java/org/apache/flink/client/deployment/application/ApplicationDispatcherBootstrapTest.java
index 3b7aa2e..0d0fc03 100644
--- a/flink-clients/src/test/java/org/apache/flink/client/deployment/application/ApplicationDispatcherBootstrapTest.java
+++ b/flink-clients/src/test/java/org/apache/flink/client/deployment/application/ApplicationDispatcherBootstrapTest.java
@@ -37,6 +37,7 @@ import org.apache.flink.runtime.jobgraph.JobGraph;
 import org.apache.flink.runtime.jobmanager.HighAvailabilityMode;
 import org.apache.flink.runtime.jobmaster.JobResult;
 import org.apache.flink.runtime.messages.Acknowledge;
+import org.apache.flink.runtime.rpc.FatalErrorHandler;
 import org.apache.flink.runtime.webmonitor.TestingDispatcherGateway;
 import org.apache.flink.util.ExceptionUtils;
 import org.apache.flink.util.ExecutorUtils;
@@ -53,6 +54,7 @@ import java.net.MalformedURLException;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Optional;
+import java.util.concurrent.CancellationException;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ConcurrentLinkedDeque;
 import java.util.concurrent.Executors;
@@ -61,6 +63,7 @@ import java.util.concurrent.ScheduledFuture;
 import java.util.concurrent.TimeUnit;
 
 import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertThat;
 import static org.junit.Assert.fail;
 
@@ -233,7 +236,17 @@ public class ApplicationDispatcherBootstrapTest {
 
 		final CompletableFuture<Void> applicationFuture = runApplication(dispatcherBuilder, 2);
 
-		assertException(applicationFuture, JobExecutionException.class);
+		try {
+			applicationFuture.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
+			fail("Expected exception but the execution went smoothly.");
+		} catch (Throwable e) {
+			Optional<ApplicationFailureException> expectedException =
+					ExceptionUtils.findThrowable(e, ApplicationFailureException.class);
+			if (!expectedException.isPresent()) {
+				throw e;
+			}
+			assertEquals(expectedException.get().getStatus(), ApplicationStatus.FAILED);
+		}
 	}
 
 	@Test
@@ -301,7 +314,11 @@ public class ApplicationDispatcherBootstrapTest {
 				.setSubmitFunction(jobGraph -> CompletableFuture.completedFuture(Acknowledge.get()))
 				.setRequestJobStatusFunction(jobId -> CompletableFuture.completedFuture(JobStatus.RUNNING));
 
-		ApplicationDispatcherBootstrap bootstrap = createApplicationDispatcherBootstrap(3);
+		// we're "listening" on this to be completed to verify that the error handler is called.
+		// In production, this will shut down the cluster with an exception.
+		final CompletableFuture<Void> errorHandlerFuture = new CompletableFuture<>();
+		final ApplicationDispatcherBootstrap bootstrap = createApplicationDispatcherBootstrap(
+				3, errorHandlerFuture::completeExceptionally);
 
 		final CompletableFuture<Acknowledge> shutdownFuture =
 				bootstrap.runApplicationAndShutdownClusterAsync(dispatcherBuilder.build(), scheduledExecutor);
@@ -310,66 +327,61 @@ public class ApplicationDispatcherBootstrapTest {
 
 		bootstrap.stop();
 
-		// wait until the bootstrap "thinks" it's done
-		shutdownFuture.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
+		// we call the error handler
+		assertException(errorHandlerFuture, CancellationException.class);
+
+		// we return a future that is completed exceptionally
+		assertException(shutdownFuture, CancellationException.class);
 
 		// verify that the application task is being cancelled
 		assertThat(applicationExecutionFuture.isCancelled(), is(true));
 	}
 
 	@Test
-	public void testClusterShutdownWhenStoppingBootstrap() throws Exception {
-		// we're "listening" on this to be completed to verify that the cluster
-		// is being shut down from the ApplicationDispatcherBootstrap
-		final CompletableFuture<ApplicationStatus> externalShutdownFuture = new CompletableFuture<>();
-
+	public void testErrorHandlerIsCalledWhenStoppingBootstrap() throws Exception {
 		final TestingDispatcherGateway.Builder dispatcherBuilder = new TestingDispatcherGateway.Builder()
 				.setSubmitFunction(jobGraph -> CompletableFuture.completedFuture(Acknowledge.get()))
-				.setRequestJobStatusFunction(jobId -> CompletableFuture.completedFuture(JobStatus.RUNNING))
-				.setClusterShutdownFunction((status) -> {
-					externalShutdownFuture.complete(status);
-					return CompletableFuture.completedFuture(Acknowledge.get());
-				});
+				.setRequestJobStatusFunction(jobId -> CompletableFuture.completedFuture(JobStatus.RUNNING));
 
-		ApplicationDispatcherBootstrap bootstrap = createApplicationDispatcherBootstrap(2);
+		// we're "listening" on this to be completed to verify that the error handler is called.
+		// In production, this will shut down the cluster with an exception.
+		final CompletableFuture<Void> errorHandlerFuture = new CompletableFuture<>();
+		final ApplicationDispatcherBootstrap bootstrap = createApplicationDispatcherBootstrap(
+				2, errorHandlerFuture::completeExceptionally);
 
 		final CompletableFuture<Acknowledge> shutdownFuture =
 				bootstrap.runApplicationAndShutdownClusterAsync(dispatcherBuilder.build(), scheduledExecutor);
 
 		bootstrap.stop();
 
-		// wait until the bootstrap "thinks" it's done
-		shutdownFuture.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
+		// we call the error handler
+		assertException(errorHandlerFuture, CancellationException.class);
 
-		// verify that the dispatcher is actually being shut down
-		assertThat(externalShutdownFuture.get(TIMEOUT_SECONDS, TimeUnit.SECONDS), is(ApplicationStatus.UNKNOWN));
+		// we return a future that is completed exceptionally
+		assertException(shutdownFuture, CancellationException.class);
 	}
 
 	@Test
-	public void testClusterShutdownWhenSubmissionFails() throws Exception {
-		// we're "listening" on this to be completed to verify that the cluster
-		// is being shut down from the ApplicationDispatcherBootstrap
-		final CompletableFuture<ApplicationStatus> externalShutdownFuture = new CompletableFuture<>();
-
+	public void testErrorHandlerIsCalledWhenSubmissionFails() throws Exception {
 		final TestingDispatcherGateway.Builder dispatcherBuilder = new TestingDispatcherGateway.Builder()
 				.setSubmitFunction(jobGraph -> {
 					throw new FlinkRuntimeException("Nope!");
-				})
-				.setClusterShutdownFunction((status) -> {
-					externalShutdownFuture.complete(status);
-					return CompletableFuture.completedFuture(Acknowledge.get());
 				});
 
-		ApplicationDispatcherBootstrap bootstrap = createApplicationDispatcherBootstrap(3);
+		// we're "listening" on this to be completed to verify that the error handler is called.
+		// In production, this will shut down the cluster with an exception.
+		final CompletableFuture<Void> errorHandlerFuture = new CompletableFuture<>();
+		final ApplicationDispatcherBootstrap bootstrap = createApplicationDispatcherBootstrap(
+				3, errorHandlerFuture::completeExceptionally);
 
 		final CompletableFuture<Acknowledge> shutdownFuture =
 				bootstrap.runApplicationAndShutdownClusterAsync(dispatcherBuilder.build(), scheduledExecutor);
 
-		// wait until the bootstrap "thinks" it's done
-		shutdownFuture.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
+		// we call the error handler
+		assertException(errorHandlerFuture, ApplicationExecutionException.class);
 
-		// verify that the dispatcher is actually being shut down
-		assertThat(externalShutdownFuture.get(TIMEOUT_SECONDS, TimeUnit.SECONDS), is(ApplicationStatus.FAILED));
+		// we return a future that is completed exceptionally
+		assertException(shutdownFuture, ApplicationExecutionException.class);
 	}
 
 	@Test
@@ -454,7 +466,7 @@ public class ApplicationDispatcherBootstrapTest {
 
 		final ApplicationDispatcherBootstrap bootstrap =
 				new ApplicationDispatcherBootstrap(
-						program, Collections.emptyList(), configuration);
+						program, Collections.emptyList(), configuration, exception -> {});
 
 		return bootstrap.fixJobIdAndRunApplicationAsync(
 				dispatcherBuilder.build(),
@@ -462,14 +474,21 @@ public class ApplicationDispatcherBootstrapTest {
 	}
 
 	private ApplicationDispatcherBootstrap createApplicationDispatcherBootstrap(int noOfJobs) throws FlinkException {
-		return createApplicationDispatcherBootstrap(noOfJobs, Collections.emptyList());
+		return createApplicationDispatcherBootstrap(noOfJobs, exception -> {});
+	}
+
+	private ApplicationDispatcherBootstrap createApplicationDispatcherBootstrap(
+			int noOfJobs,
+			FatalErrorHandler errorHandler) throws FlinkException {
+		return createApplicationDispatcherBootstrap(noOfJobs, Collections.emptyList(), errorHandler);
 	}
 
 	private ApplicationDispatcherBootstrap createApplicationDispatcherBootstrap(
 			int noOfJobs,
-			Collection<JobGraph> recoveredJobGraphs) throws FlinkException {
+			Collection<JobGraph> recoveredJobGraphs,
+			FatalErrorHandler errorHandler) throws FlinkException {
 		final PackagedProgram program = getProgram(noOfJobs);
-		return new ApplicationDispatcherBootstrap(program, recoveredJobGraphs, getConfiguration());
+		return new ApplicationDispatcherBootstrap(program, recoveredJobGraphs, getConfiguration(), errorHandler);
 	}
 
 	private PackagedProgram getProgram(int noOfJobs) throws FlinkException {
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/Dispatcher.java b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/Dispatcher.java
index c4f1670..013d22c 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/Dispatcher.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/Dispatcher.java
@@ -120,7 +120,7 @@ public abstract class Dispatcher extends PermanentlyFencedRpcEndpoint<Dispatcher
 
 	private final Map<JobID, CompletableFuture<JobManagerRunner>> jobManagerRunnerFutures;
 
-	private final DispatcherBootstrap dispatcherBootstrap;
+	private final Function<FatalErrorHandler, DispatcherBootstrap> dispatcherBootstrapFactory;
 
 	private final ArchivedExecutionGraphStore archivedExecutionGraphStore;
 
@@ -137,10 +137,12 @@ public abstract class Dispatcher extends PermanentlyFencedRpcEndpoint<Dispatcher
 
 	protected final CompletableFuture<ApplicationStatus> shutDownFuture;
 
+	private DispatcherBootstrap dispatcherBootstrap;
+
 	public Dispatcher(
 			RpcService rpcService,
 			DispatcherId fencingToken,
-			DispatcherBootstrap dispatcherBootstrap,
+			Function<FatalErrorHandler, DispatcherBootstrap> dispatcherBootstrapFactory,
 			DispatcherServices dispatcherServices) throws Exception {
 		super(rpcService, AkkaRpcServiceUtils.createRandomName(DISPATCHER_NAME), fencingToken);
 		checkNotNull(dispatcherServices);
@@ -174,7 +176,7 @@ public abstract class Dispatcher extends PermanentlyFencedRpcEndpoint<Dispatcher
 
 		this.shutDownFuture = new CompletableFuture<>();
 
-		this.dispatcherBootstrap = checkNotNull(dispatcherBootstrap);
+		this.dispatcherBootstrapFactory = checkNotNull(dispatcherBootstrapFactory);
 	}
 
 	//------------------------------------------------------
@@ -199,7 +201,8 @@ public abstract class Dispatcher extends PermanentlyFencedRpcEndpoint<Dispatcher
 			throw exception;
 		}
 
-		dispatcherBootstrap.initialize(this, this.getRpcService().getScheduledExecutor());
+		this.dispatcherBootstrap = this.dispatcherBootstrapFactory.apply(shutDownFuture::completeExceptionally);
+		this.dispatcherBootstrap.initialize(this, this.getRpcService().getScheduledExecutor());
 	}
 
 	private void startDispatcherServices() throws Exception {
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/DispatcherFactory.java b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/DispatcherFactory.java
index 71376c2..e1f3827 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/DispatcherFactory.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/DispatcherFactory.java
@@ -18,8 +18,11 @@
 
 package org.apache.flink.runtime.dispatcher;
 
+import org.apache.flink.runtime.rpc.FatalErrorHandler;
 import org.apache.flink.runtime.rpc.RpcService;
 
+import java.util.function.Function;
+
 /**
  * {@link Dispatcher} factory interface.
  */
@@ -29,8 +32,8 @@ public interface DispatcherFactory {
 	 * Create a {@link Dispatcher}.
 	 */
 	Dispatcher createDispatcher(
-		RpcService rpcService,
-		DispatcherId fencingToken,
-		DispatcherBootstrap dispatcherBootstrap,
-		PartialDispatcherServicesWithJobGraphStore partialDispatcherServicesWithJobGraphStore) throws Exception;
+			RpcService rpcService,
+			DispatcherId fencingToken,
+			Function<FatalErrorHandler, DispatcherBootstrap> dispatcherBootstrapFactory,
+			PartialDispatcherServicesWithJobGraphStore partialDispatcherServicesWithJobGraphStore) throws Exception;
 }
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/JobDispatcherFactory.java b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/JobDispatcherFactory.java
index cc17177..a68da40 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/JobDispatcherFactory.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/JobDispatcherFactory.java
@@ -20,8 +20,11 @@ package org.apache.flink.runtime.dispatcher;
 
 import org.apache.flink.configuration.Configuration;
 import org.apache.flink.runtime.entrypoint.ClusterEntrypoint;
+import org.apache.flink.runtime.rpc.FatalErrorHandler;
 import org.apache.flink.runtime.rpc.RpcService;
 
+import java.util.function.Function;
+
 import static org.apache.flink.runtime.entrypoint.ClusterEntrypoint.EXECUTION_MODE;
 
 /**
@@ -34,7 +37,7 @@ public enum JobDispatcherFactory implements DispatcherFactory {
 	public MiniDispatcher createDispatcher(
 			RpcService rpcService,
 			DispatcherId fencingToken,
-			DispatcherBootstrap dispatcherBootstrap,
+			Function<FatalErrorHandler, DispatcherBootstrap> dispatcherBootstrapFactory,
 			PartialDispatcherServicesWithJobGraphStore partialDispatcherServicesWithJobGraphStore) throws Exception {
 		final Configuration configuration = partialDispatcherServicesWithJobGraphStore.getConfiguration();
 		final String executionModeValue = configuration.getString(EXECUTION_MODE);
@@ -44,7 +47,7 @@ public enum JobDispatcherFactory implements DispatcherFactory {
 			rpcService,
 			fencingToken,
 			DispatcherServices.from(partialDispatcherServicesWithJobGraphStore, DefaultJobManagerRunnerFactory.INSTANCE),
-			dispatcherBootstrap,
+			dispatcherBootstrapFactory,
 			executionMode);
 	}
 }
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/MiniDispatcher.java b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/MiniDispatcher.java
index 1464b3c..79872ff 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/MiniDispatcher.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/MiniDispatcher.java
@@ -27,6 +27,7 @@ import org.apache.flink.runtime.executiongraph.ArchivedExecutionGraph;
 import org.apache.flink.runtime.jobgraph.JobGraph;
 import org.apache.flink.runtime.jobmaster.JobResult;
 import org.apache.flink.runtime.messages.Acknowledge;
+import org.apache.flink.runtime.rpc.FatalErrorHandler;
 import org.apache.flink.runtime.rpc.RpcService;
 import org.apache.flink.util.FlinkException;
 
@@ -34,6 +35,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.util.concurrent.CompletableFuture;
+import java.util.function.Function;
 
 import static org.apache.flink.util.Preconditions.checkNotNull;
 
@@ -55,12 +57,12 @@ public class MiniDispatcher extends Dispatcher {
 			RpcService rpcService,
 			DispatcherId fencingToken,
 			DispatcherServices dispatcherServices,
-			DispatcherBootstrap dispatcherBootstrap,
+			Function<FatalErrorHandler, DispatcherBootstrap> dispatcherBootstrapFactory,
 			JobClusterEntrypoint.ExecutionMode executionMode) throws Exception {
 		super(
 			rpcService,
 			fencingToken,
-			dispatcherBootstrap,
+			dispatcherBootstrapFactory,
 			dispatcherServices);
 
 		this.executionMode = checkNotNull(executionMode);
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/SessionDispatcherFactory.java b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/SessionDispatcherFactory.java
index cc867ca..35f8d32 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/SessionDispatcherFactory.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/SessionDispatcherFactory.java
@@ -18,8 +18,11 @@
 
 package org.apache.flink.runtime.dispatcher;
 
+import org.apache.flink.runtime.rpc.FatalErrorHandler;
 import org.apache.flink.runtime.rpc.RpcService;
 
+import java.util.function.Function;
+
 /**
  * {@link DispatcherFactory} which creates a {@link StandaloneDispatcher}.
  */
@@ -30,13 +33,13 @@ public enum SessionDispatcherFactory implements DispatcherFactory {
 	public StandaloneDispatcher createDispatcher(
 			RpcService rpcService,
 			DispatcherId fencingToken,
-			DispatcherBootstrap dispatcherBootstrap,
+			Function<FatalErrorHandler, DispatcherBootstrap> dispatcherBootstrapFactory,
 			PartialDispatcherServicesWithJobGraphStore partialDispatcherServicesWithJobGraphStore) throws Exception {
 		// create the default dispatcher
 		return new StandaloneDispatcher(
 			rpcService,
 			fencingToken,
-			dispatcherBootstrap,
+			dispatcherBootstrapFactory,
 			DispatcherServices.from(partialDispatcherServicesWithJobGraphStore, DefaultJobManagerRunnerFactory.INSTANCE));
 	}
 }
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/StandaloneDispatcher.java b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/StandaloneDispatcher.java
index 4ba50c5..65bd4a3 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/StandaloneDispatcher.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/StandaloneDispatcher.java
@@ -20,8 +20,11 @@ package org.apache.flink.runtime.dispatcher;
 
 import org.apache.flink.runtime.jobgraph.JobGraph;
 import org.apache.flink.runtime.jobmaster.JobMaster;
+import org.apache.flink.runtime.rpc.FatalErrorHandler;
 import org.apache.flink.runtime.rpc.RpcService;
 
+import java.util.function.Function;
+
 /**
  * Dispatcher implementation which spawns a {@link JobMaster} for each
  * submitted {@link JobGraph} within in the same process. This dispatcher
@@ -31,12 +34,12 @@ public class StandaloneDispatcher extends Dispatcher {
 	public StandaloneDispatcher(
 			RpcService rpcService,
 			DispatcherId fencingToken,
-			DispatcherBootstrap dispatcherBootstrap,
+			Function<FatalErrorHandler, DispatcherBootstrap> dispatcherBootstrapFactory,
 			DispatcherServices dispatcherServices) throws Exception {
 		super(
 			rpcService,
 			fencingToken,
-			dispatcherBootstrap,
+			dispatcherBootstrapFactory,
 			dispatcherServices);
 	}
 }
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/runner/DefaultDispatcherGatewayServiceFactory.java b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/runner/DefaultDispatcherGatewayServiceFactory.java
index 072d048..e5312d7 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/runner/DefaultDispatcherGatewayServiceFactory.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/runner/DefaultDispatcherGatewayServiceFactory.java
@@ -66,7 +66,7 @@ class DefaultDispatcherGatewayServiceFactory implements AbstractDispatcherLeader
 			dispatcher = dispatcherFactory.createDispatcher(
 				rpcService,
 				fencingToken,
-				bootstrap,
+				errorHandler -> bootstrap,
 				PartialDispatcherServicesWithJobGraphStore.from(partialDispatcherServices, jobGraphWriter));
 		} catch (Exception e) {
 			throw new FlinkRuntimeException("Could not create the Dispatcher rpc endpoint.", e);
diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/DispatcherResourceCleanupTest.java b/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/DispatcherResourceCleanupTest.java
index d2ad995..bc0cad5 100644
--- a/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/DispatcherResourceCleanupTest.java
+++ b/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/DispatcherResourceCleanupTest.java
@@ -193,7 +193,7 @@ public class DispatcherResourceCleanupTest extends TestLogger {
 		dispatcher = new TestingDispatcher(
 			rpcService,
 			DispatcherId.generate(),
-			new DefaultDispatcherBootstrap(Collections.emptyList()),
+			errorHandler -> new DefaultDispatcherBootstrap(Collections.emptyList()),
 			new DispatcherServices(
 				configuration,
 				highAvailabilityServices,
diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/DispatcherTest.java b/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/DispatcherTest.java
index c04e765..8215bc4 100755
--- a/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/DispatcherTest.java
+++ b/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/DispatcherTest.java
@@ -99,6 +99,7 @@ import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
+import java.util.function.Function;
 
 import static org.hamcrest.Matchers.empty;
 import static org.hamcrest.Matchers.equalTo;
@@ -203,7 +204,8 @@ public class DispatcherTest extends TestLogger {
 
 	private class TestingDispatcherBuilder {
 
-		private DispatcherBootstrap dispatcherBootstrap = new DefaultDispatcherBootstrap(Collections.emptyList());
+		private Function<FatalErrorHandler, DispatcherBootstrap> dispatcherBootstrapFactory =
+				errorHandler -> new DefaultDispatcherBootstrap(Collections.emptyList());
 
 		private HeartbeatServices heartbeatServices = DispatcherTest.this.heartbeatServices;
 
@@ -223,8 +225,9 @@ public class DispatcherTest extends TestLogger {
 			return this;
 		}
 
-		TestingDispatcherBuilder setDispatcherBootstrap(DispatcherBootstrap dispatcherBootstrap) {
-			this.dispatcherBootstrap = dispatcherBootstrap;
+		TestingDispatcherBuilder setDispatcherBootstrapFactory(
+				Function<FatalErrorHandler, DispatcherBootstrap> dispatcherBootstrapFactory) {
+			this.dispatcherBootstrapFactory = dispatcherBootstrapFactory;
 			return this;
 		}
 
@@ -246,7 +249,7 @@ public class DispatcherTest extends TestLogger {
 			return new TestingDispatcher(
 				rpcService,
 				DispatcherId.generate(),
-				dispatcherBootstrap,
+				dispatcherBootstrapFactory,
 				new DispatcherServices(
 					configuration,
 					haServices,
@@ -446,7 +449,7 @@ public class DispatcherTest extends TestLogger {
 		final JobGraph failingJobGraph = createFailingJobGraph(testException);
 
 		dispatcher = new TestingDispatcherBuilder()
-			.setDispatcherBootstrap(new DefaultDispatcherBootstrap(Collections.singleton(failingJobGraph)))
+			.setDispatcherBootstrapFactory(errorHandler -> new DefaultDispatcherBootstrap(Collections.singleton(failingJobGraph)))
 			.build();
 
 		dispatcher.start();
@@ -598,7 +601,7 @@ public class DispatcherTest extends TestLogger {
 			.build();
 
 		dispatcher = new TestingDispatcherBuilder()
-			.setDispatcherBootstrap(new DefaultDispatcherBootstrap(Collections.singleton(jobGraph)))
+			.setDispatcherBootstrapFactory(errorHandler -> new DefaultDispatcherBootstrap(Collections.singleton(jobGraph)))
 			.setJobGraphWriter(testingJobGraphStore)
 			.build();
 		dispatcher.start();
diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/MiniDispatcherTest.java b/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/MiniDispatcherTest.java
index 2362d65..02dedab 100644
--- a/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/MiniDispatcherTest.java
+++ b/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/MiniDispatcherTest.java
@@ -251,7 +251,7 @@ public class MiniDispatcherTest extends TestLogger {
 				UnregisteredMetricGroups.createUnregisteredJobManagerMetricGroup(),
 				highAvailabilityServices.getJobGraphStore(),
 				testingJobManagerRunnerFactory),
-			new DefaultDispatcherBootstrap(Collections.singletonList(jobGraph)),
+			errorHandler -> new DefaultDispatcherBootstrap(Collections.singletonList(jobGraph)),
 			executionMode);
 	}
 
diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/TestingDispatcher.java b/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/TestingDispatcher.java
index 22e5d74..fe6472a 100644
--- a/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/TestingDispatcher.java
+++ b/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/TestingDispatcher.java
@@ -21,6 +21,7 @@ package org.apache.flink.runtime.dispatcher;
 import org.apache.flink.api.common.JobID;
 import org.apache.flink.api.common.time.Time;
 import org.apache.flink.runtime.executiongraph.ArchivedExecutionGraph;
+import org.apache.flink.runtime.rpc.FatalErrorHandler;
 import org.apache.flink.runtime.rpc.RpcService;
 
 import javax.annotation.Nonnull;
@@ -38,12 +39,12 @@ class TestingDispatcher extends Dispatcher {
 	TestingDispatcher(
 			RpcService rpcService,
 			DispatcherId fencingToken,
-			DispatcherBootstrap dispatcherBootstrap,
+			Function<FatalErrorHandler, DispatcherBootstrap> dispatcherBootstrapFactory,
 			DispatcherServices dispatcherServices) throws Exception {
 		super(
 			rpcService,
 			fencingToken,
-			dispatcherBootstrap,
+			dispatcherBootstrapFactory,
 			dispatcherServices);
 
 		this.startFuture = new CompletableFuture<>();
diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/runner/DefaultDispatcherRunnerITCase.java b/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/runner/DefaultDispatcherRunnerITCase.java
index bcd1eb5..d31a7f8 100644
--- a/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/runner/DefaultDispatcherRunnerITCase.java
+++ b/flink-runtime/src/test/java/org/apache/flink/runtime/dispatcher/runner/DefaultDispatcherRunnerITCase.java
@@ -44,6 +44,7 @@ import org.apache.flink.runtime.jobmanager.JobGraphStore;
 import org.apache.flink.runtime.jobmaster.TestingJobManagerRunner;
 import org.apache.flink.runtime.leaderelection.TestingLeaderElectionService;
 import org.apache.flink.runtime.metrics.groups.UnregisteredMetricGroups;
+import org.apache.flink.runtime.rpc.FatalErrorHandler;
 import org.apache.flink.runtime.rpc.RpcService;
 import org.apache.flink.runtime.rpc.TestingRpcServiceResource;
 import org.apache.flink.runtime.testingUtils.TestingUtils;
@@ -65,6 +66,7 @@ import java.util.Collection;
 import java.util.UUID;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.TimeUnit;
+import java.util.function.Function;
 
 import static org.hamcrest.Matchers.contains;
 import static org.hamcrest.Matchers.equalTo;
@@ -206,12 +208,12 @@ public class DefaultDispatcherRunnerITCase extends TestLogger {
 		public Dispatcher createDispatcher(
 			RpcService rpcService,
 			DispatcherId fencingToken,
-			DispatcherBootstrap dispatcherBootstrap,
+			Function<FatalErrorHandler, DispatcherBootstrap> dispatcherBootstrapFactory,
 			PartialDispatcherServicesWithJobGraphStore partialDispatcherServicesWithJobGraphStore) throws Exception {
 			return new StandaloneDispatcher(
 				rpcService,
 				fencingToken,
-				dispatcherBootstrap,
+				dispatcherBootstrapFactory,
 				DispatcherServices.from(partialDispatcherServicesWithJobGraphStore, jobManagerRunnerFactory));
 		}
 	}