You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by tr...@apache.org on 2020/04/20 16:22:23 UTC

[flink] branch master updated (b4618ae -> 422aa89)

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

trohrmann pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git.


    from b4618ae  [FLINK-16874] Respect the dynamic options when calculating memory options in taskmanager.sh
     new 7d65fe2  [hotfix] Add RpcUtils#terminateRpcEndpoints
     new eb56ab4  [hotfix] Add RpcUtils#INF_DURATION
     new 03b5115  [hotfix] Add RpcRuntimeException
     new 067b9c0  [hotfix] Add AkkRpcRuntimeException
     new ef0ae0b  [hotfix] Let AkkaUnknownMessageException extend AkkaRpcRuntimeException
     new db375bb  [FLINK-15347] Bump AkkRpcVersion to 2
     new 4c63f21  [FLINK-15347][tests] Add ActorSystemResource
     new f0529de  [FLINK-15347] Add SupervisorActor which monitors the proper termination of AkkaRpcActors
     new 2cf7955  [FLINK-15347] Introduce separate dispatcher for SupervisorActor
     new df30edd  [hotfix] Add FutureUtils.forwardAsync
     new 422aa89  [FLINK-15347] Add dedicated termination future executor to SupervisorActor

The 11 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.


Summary of changes:
 ...tegy.java => EscalatingSupervisorStrategy.java} |  21 +-
 .../flink/runtime/concurrent/FutureUtils.java      |  22 +-
 .../org/apache/flink/runtime/rpc/RpcUtils.java     |  37 ++-
 .../flink/runtime/rpc/akka/AkkaRpcService.java     | 122 +++++--
 .../runtime/rpc/akka/AkkaRpcServiceUtils.java      |   4 +-
 .../flink/runtime/rpc/akka/SupervisorActor.java    | 366 +++++++++++++++++++++
 ...Exception.java => AkkaRpcRuntimeException.java} |  21 +-
 .../exceptions/AkkaUnknownMessageException.java    |   2 +-
 ...{RpcException.java => RpcRuntimeException.java} |  18 +-
 .../org/apache/flink/runtime/akka/AkkaUtils.scala  |  11 +-
 .../flink/runtime/concurrent/FutureUtilsTest.java  |  18 +
 .../flink/runtime/rpc/FencedRpcEndpointTest.java   |   4 +-
 .../runtime/rpc/akka/ActorSystemResource.java      |  64 ++++
 .../runtime/rpc/akka/AkkaActorSystemTest.java      |  89 +++++
 .../flink/runtime/rpc/akka/AkkaRpcActorTest.java   |  38 +++
 .../runtime/rpc/akka/SupervisorActorTest.java      | 249 ++++++++++++++
 .../retriever/impl/RpcGatewayRetrieverTest.java    |   3 +-
 17 files changed, 1007 insertions(+), 82 deletions(-)
 rename flink-runtime/src/main/java/org/apache/flink/runtime/akka/{StoppingSupervisorWithoutLoggingActorKilledExceptionStrategy.java => EscalatingSupervisorStrategy.java} (61%)
 create mode 100644 flink-runtime/src/main/java/org/apache/flink/runtime/rpc/akka/SupervisorActor.java
 copy flink-runtime/src/main/java/org/apache/flink/runtime/rpc/akka/exceptions/{AkkaRpcException.java => AkkaRpcRuntimeException.java} (67%)
 copy flink-runtime/src/main/java/org/apache/flink/runtime/rpc/exceptions/{RpcException.java => RpcRuntimeException.java} (72%)
 create mode 100644 flink-runtime/src/test/java/org/apache/flink/runtime/rpc/akka/ActorSystemResource.java
 create mode 100644 flink-runtime/src/test/java/org/apache/flink/runtime/rpc/akka/AkkaActorSystemTest.java
 create mode 100644 flink-runtime/src/test/java/org/apache/flink/runtime/rpc/akka/SupervisorActorTest.java


[flink] 05/11: [hotfix] Let AkkaUnknownMessageException extend AkkaRpcRuntimeException

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

trohrmann pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git

commit ef0ae0be1cb9fae78bc1b60cacb412124e477185
Author: Till Rohrmann <tr...@apache.org>
AuthorDate: Thu Apr 9 10:18:06 2020 +0200

    [hotfix] Let AkkaUnknownMessageException extend AkkaRpcRuntimeException
    
    AkkaUnknownMessageException represents a programming error which is not
    recoverable. Consequently, this exception becomes an unchecked exception
    with this commit.
---
 .../runtime/rpc/akka/exceptions/AkkaUnknownMessageException.java      | 2 +-
 .../test/java/org/apache/flink/runtime/rpc/FencedRpcEndpointTest.java | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/akka/exceptions/AkkaUnknownMessageException.java b/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/akka/exceptions/AkkaUnknownMessageException.java
index 7504761..1251b6d 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/akka/exceptions/AkkaUnknownMessageException.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/akka/exceptions/AkkaUnknownMessageException.java
@@ -22,7 +22,7 @@ package org.apache.flink.runtime.rpc.akka.exceptions;
  * Exception which indicates that the AkkaRpcActor has received an
  * unknown message type.
  */
-public class AkkaUnknownMessageException extends AkkaRpcException {
+public class AkkaUnknownMessageException extends AkkaRpcRuntimeException {
 
 	private static final long serialVersionUID = 1691338049911020814L;
 
diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/rpc/FencedRpcEndpointTest.java b/flink-runtime/src/test/java/org/apache/flink/runtime/rpc/FencedRpcEndpointTest.java
index 43e5798..f86eb31 100644
--- a/flink-runtime/src/test/java/org/apache/flink/runtime/rpc/FencedRpcEndpointTest.java
+++ b/flink-runtime/src/test/java/org/apache/flink/runtime/rpc/FencedRpcEndpointTest.java
@@ -22,7 +22,7 @@ import org.apache.flink.api.common.time.Time;
 import org.apache.flink.core.testutils.OneShotLatch;
 import org.apache.flink.runtime.messages.Acknowledge;
 import org.apache.flink.runtime.rpc.exceptions.FencingTokenException;
-import org.apache.flink.runtime.rpc.exceptions.RpcException;
+import org.apache.flink.runtime.rpc.exceptions.RpcRuntimeException;
 import org.apache.flink.util.ExceptionUtils;
 import org.apache.flink.util.FlinkException;
 import org.apache.flink.util.TestLogger;
@@ -263,7 +263,7 @@ public class FencedRpcEndpointTest extends TestLogger {
 				unfencedGateway.foobar(timeout).get(timeout.toMilliseconds(), TimeUnit.MILLISECONDS);
 				fail("This should have failed because we have an unfenced gateway.");
 			} catch (ExecutionException e) {
-				assertTrue(ExceptionUtils.stripExecutionException(e) instanceof RpcException);
+				assertTrue(ExceptionUtils.stripExecutionException(e) instanceof RpcRuntimeException);
 			}
 
 			try {


[flink] 01/11: [hotfix] Add RpcUtils#terminateRpcEndpoints

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

trohrmann pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git

commit 7d65fe2b480f32f6d79a5a97b89b3abb22bb987a
Author: Till Rohrmann <tr...@apache.org>
AuthorDate: Tue Apr 7 18:28:10 2020 +0200

    [hotfix] Add RpcUtils#terminateRpcEndpoints
---
 .../org/apache/flink/runtime/rpc/RpcUtils.java     | 34 +++++++++++++++++++---
 .../retriever/impl/RpcGatewayRetrieverTest.java    |  3 +-
 2 files changed, 31 insertions(+), 6 deletions(-)

diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/RpcUtils.java b/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/RpcUtils.java
index 1378d6f..8b07b17 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/RpcUtils.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/RpcUtils.java
@@ -20,8 +20,10 @@ package org.apache.flink.runtime.rpc;
 
 import org.apache.flink.api.common.time.Time;
 import org.apache.flink.runtime.concurrent.FutureUtils;
+import org.apache.flink.util.AutoCloseableAsync;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.HashSet;
 import java.util.Set;
@@ -29,6 +31,7 @@ import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
+import java.util.stream.Collectors;
 
 /**
  * Utility functions for Flink's RPC implementation.
@@ -78,6 +81,21 @@ public class RpcUtils {
 	}
 
 	/**
+	 * Shuts the given {@link RpcEndpoint RpcEndpoints} down and waits for their termination.
+	 *
+	 * @param rpcEndpoints to shut down
+	 * @param timeout for this operation
+	 * @throws InterruptedException if the operation has been interrupted
+	 * @throws ExecutionException if a problem occurred
+	 * @throws TimeoutException if a timeout occurred
+	 */
+	public static void terminateRpcEndpoints(
+			Time timeout,
+			RpcEndpoint... rpcEndpoints) throws InterruptedException, ExecutionException, TimeoutException {
+		terminateAsyncCloseables(Arrays.asList(rpcEndpoints), timeout);
+	}
+
+	/**
 	 * Shuts the given rpc service down and waits for its termination.
 	 *
 	 * @param rpcService to shut down
@@ -102,11 +120,19 @@ public class RpcUtils {
 	public static void terminateRpcServices(
 			Time timeout,
 			RpcService... rpcServices) throws InterruptedException, ExecutionException, TimeoutException {
-		final Collection<CompletableFuture<?>> terminationFutures = new ArrayList<>(rpcServices.length);
+		terminateAsyncCloseables(
+			Arrays.stream(rpcServices)
+				.map(rpcService -> (AutoCloseableAsync) rpcService::stopService)
+				.collect(Collectors.toList()),
+			timeout);
+	}
+
+	private static void terminateAsyncCloseables(Collection<? extends AutoCloseableAsync> closeables, Time timeout) throws InterruptedException, ExecutionException, TimeoutException {
+		final Collection<CompletableFuture<?>> terminationFutures = new ArrayList<>(closeables.size());
 
-		for (RpcService service : rpcServices) {
-			if (service != null) {
-				terminationFutures.add(service.stopService());
+		for (AutoCloseableAsync closeableAsync : closeables) {
+			if (closeableAsync != null) {
+				terminationFutures.add(closeableAsync.closeAsync());
 			}
 		}
 
diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/webmonitor/retriever/impl/RpcGatewayRetrieverTest.java b/flink-runtime/src/test/java/org/apache/flink/runtime/webmonitor/retriever/impl/RpcGatewayRetrieverTest.java
index 1ccf503..d7b3075 100644
--- a/flink-runtime/src/test/java/org/apache/flink/runtime/webmonitor/retriever/impl/RpcGatewayRetrieverTest.java
+++ b/flink-runtime/src/test/java/org/apache/flink/runtime/webmonitor/retriever/impl/RpcGatewayRetrieverTest.java
@@ -106,8 +106,7 @@ public class RpcGatewayRetrieverTest extends TestLogger {
 			assertEquals(dummyRpcEndpoint2.getAddress(), dummyGateway2.getAddress());
 			assertEquals(expectedValue2, dummyGateway2.foobar(TIMEOUT).get(TIMEOUT.toMilliseconds(), TimeUnit.MILLISECONDS));
 		} finally {
-			RpcUtils.terminateRpcEndpoint(dummyRpcEndpoint, TIMEOUT);
-			RpcUtils.terminateRpcEndpoint(dummyRpcEndpoint2, TIMEOUT);
+			RpcUtils.terminateRpcEndpoints(TIMEOUT, dummyRpcEndpoint, dummyRpcEndpoint2);
 		}
 	}
 


[flink] 07/11: [FLINK-15347][tests] Add ActorSystemResource

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

trohrmann pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git

commit 4c63f212e2d98bb8f6a5b813ed594e78a6b5b420
Author: Till Rohrmann <tr...@apache.org>
AuthorDate: Wed Apr 8 17:00:21 2020 +0200

    [FLINK-15347][tests] Add ActorSystemResource
---
 .../runtime/rpc/akka/ActorSystemResource.java      | 64 ++++++++++++++++++++++
 1 file changed, 64 insertions(+)

diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/rpc/akka/ActorSystemResource.java b/flink-runtime/src/test/java/org/apache/flink/runtime/rpc/akka/ActorSystemResource.java
new file mode 100644
index 0000000..0f734bf
--- /dev/null
+++ b/flink-runtime/src/test/java/org/apache/flink/runtime/rpc/akka/ActorSystemResource.java
@@ -0,0 +1,64 @@
+/*
+ * 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.rpc.akka;
+
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.runtime.akka.AkkaUtils;
+import org.apache.flink.util.Preconditions;
+
+import akka.actor.ActorSystem;
+import org.junit.rules.ExternalResource;
+
+/**
+ * External resource which starts an {@link akka.actor.ActorSystem}.
+ */
+public class ActorSystemResource extends ExternalResource {
+
+	private final Configuration configuration;
+
+	private ActorSystem actorSystem;
+
+	private ActorSystemResource(Configuration configuration) {
+		this.configuration = configuration;
+	}
+
+	@Override
+	protected void before() throws Throwable {
+		Preconditions.checkState(actorSystem == null, "ActorSystem must not be initialized when calling before.");
+		actorSystem = AkkaUtils.createLocalActorSystem(configuration);
+	}
+
+	@Override
+	protected void after() {
+		Preconditions.checkState(actorSystem != null, "ActorSystem must be initialized when calling after.");
+		AkkaUtils.terminateActorSystem(actorSystem).join();
+	}
+
+	public ActorSystem getActorSystem() {
+		return actorSystem;
+	}
+
+	public static ActorSystemResource defaultConfiguration() {
+		return new ActorSystemResource(new Configuration());
+	}
+
+	public static ActorSystemResource withConfiguration(Configuration configuration) {
+		return new ActorSystemResource(configuration);
+	}
+}


[flink] 11/11: [FLINK-15347] Add dedicated termination future executor to SupervisorActor

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

trohrmann pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git

commit 422aa8956c246cad379f1e72a3aeb8b9b858abba
Author: Till Rohrmann <tr...@apache.org>
AuthorDate: Wed Apr 15 15:33:44 2020 +0200

    [FLINK-15347] Add dedicated termination future executor to SupervisorActor
    
    The SupervisorActor must never block its own main thread. Due to this constraint,
    we must not complete the external termination futures from the main thread, because
    one does not know what the user does with this future. Hence, this commit introduces
    a dedicated termination future executor which is responsible for completing the
    termination futures.
    
    This closes #11683.
---
 .../flink/runtime/rpc/akka/AkkaRpcService.java     | 51 +++++++++++++++++++---
 .../flink/runtime/rpc/akka/SupervisorActor.java    | 26 ++++++-----
 .../flink/runtime/rpc/akka/AkkaRpcActorTest.java   | 16 +++++++
 .../runtime/rpc/akka/SupervisorActorTest.java      | 10 ++---
 4 files changed, 83 insertions(+), 20 deletions(-)

diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/akka/AkkaRpcService.java b/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/akka/AkkaRpcService.java
index f3604de..7862b35 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/akka/AkkaRpcService.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/akka/AkkaRpcService.java
@@ -36,6 +36,9 @@ import org.apache.flink.runtime.rpc.akka.exceptions.AkkaRpcRuntimeException;
 import org.apache.flink.runtime.rpc.exceptions.RpcConnectionException;
 import org.apache.flink.runtime.rpc.messages.HandshakeSuccessMessage;
 import org.apache.flink.runtime.rpc.messages.RemoteHandshakeMessage;
+import org.apache.flink.runtime.util.ExecutorThreadFactory;
+import org.apache.flink.util.AutoCloseableAsync;
+import org.apache.flink.util.ExecutorUtils;
 
 import akka.actor.AbstractActor;
 import akka.actor.ActorIdentity;
@@ -67,6 +70,8 @@ import java.util.concurrent.Callable;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.CompletionException;
 import java.util.concurrent.Executor;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledFuture;
 import java.util.concurrent.TimeUnit;
 import java.util.function.Function;
@@ -107,7 +112,7 @@ public class AkkaRpcService implements RpcService {
 
 	private final CompletableFuture<Void> terminationFuture;
 
-	private final ActorRef supervisor;
+	private final Supervisor supervisor;
 
 	private volatile boolean stopped;
 
@@ -141,8 +146,11 @@ public class AkkaRpcService implements RpcService {
 		supervisor = startSupervisorActor();
 	}
 
-	private ActorRef startSupervisorActor() {
-		return SupervisorActor.startSupervisorActor(actorSystem);
+	private Supervisor startSupervisorActor() {
+		final ExecutorService terminationFutureExecutor = Executors.newSingleThreadExecutor(new ExecutorThreadFactory("AkkaRpcService-Supervisor-Termination-Future-Executor"));
+		final ActorRef actorRef = SupervisorActor.startSupervisorActor(actorSystem, terminationFutureExecutor);
+
+		return Supervisor.create(actorRef, terminationFutureExecutor);
 	}
 
 	public ActorSystem getActorSystem() {
@@ -284,7 +292,7 @@ public class AkkaRpcService implements RpcService {
 			checkState(!stopped, "RpcService is stopped");
 
 			final SupervisorActor.StartAkkaRpcActorResponse startAkkaRpcActorResponse = SupervisorActor.startAkkaRpcActor(
-				supervisor,
+				supervisor.getActor(),
 				actorTerminationFuture -> Props.create(
 					akkaRpcActorType,
 					rpcEndpoint,
@@ -371,8 +379,12 @@ public class AkkaRpcService implements RpcService {
 			akkaRpcActorsTerminationFuture = terminateAkkaRpcActors();
 		}
 
-		final CompletableFuture<Void> actorSystemTerminationFuture = FutureUtils.composeAfterwards(
+		final CompletableFuture<Void> supervisorTerminationFuture = FutureUtils.composeAfterwards(
 			akkaRpcActorsTerminationFuture,
+			supervisor::closeAsync);
+
+		final CompletableFuture<Void> actorSystemTerminationFuture = FutureUtils.composeAfterwards(
+			supervisorTerminationFuture,
 			() -> FutureUtils.toJava(actorSystem.terminate()));
 
 		actorSystemTerminationFuture.whenComplete(
@@ -513,4 +525,33 @@ public class AkkaRpcService implements RpcService {
 			},
 			actorSystem.dispatcher());
 	}
+
+	// ---------------------------------------------------------------------------------------
+	// Private inner classes
+	// ---------------------------------------------------------------------------------------
+
+	private static final class Supervisor implements AutoCloseableAsync {
+
+		private final ActorRef actor;
+
+		private final ExecutorService terminationFutureExecutor;
+
+		private Supervisor(ActorRef actor, ExecutorService terminationFutureExecutor) {
+			this.actor = actor;
+			this.terminationFutureExecutor = terminationFutureExecutor;
+		}
+
+		private static Supervisor create(ActorRef actorRef, ExecutorService terminationFutureExecutor) {
+			return new Supervisor(actorRef, terminationFutureExecutor);
+		}
+
+		public ActorRef getActor() {
+			return actor;
+		}
+
+		@Override
+		public CompletableFuture<Void> closeAsync() {
+			return ExecutorUtils.nonBlockingShutdown(30L, TimeUnit.SECONDS, terminationFutureExecutor);
+		}
+	}
 }
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/akka/SupervisorActor.java b/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/akka/SupervisorActor.java
index 3758fb2..1a2be62 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/akka/SupervisorActor.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/akka/SupervisorActor.java
@@ -43,6 +43,7 @@ import java.util.Map;
 import java.util.Optional;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.CompletionException;
+import java.util.concurrent.Executor;
 import java.util.function.Function;
 
 import scala.PartialFunction;
@@ -56,9 +57,12 @@ class SupervisorActor extends AbstractActor {
 
 	private static final Logger LOG = LoggerFactory.getLogger(SupervisorActor.class);
 
+	private final Executor terminationFutureExecutor;
+
 	private final Map<ActorRef, AkkaRpcActorRegistration> registeredAkkaRpcActors;
 
-	SupervisorActor() {
+	SupervisorActor(Executor terminationFutureExecutor) {
+		this.terminationFutureExecutor = terminationFutureExecutor;
 		this.registeredAkkaRpcActors = new HashMap<>();
 	}
 
@@ -89,8 +93,10 @@ class SupervisorActor extends AbstractActor {
 	}
 
 	private void terminateAkkaRpcActorOnStop(AkkaRpcActorRegistration akkaRpcActorRegistration) {
-		akkaRpcActorRegistration.terminateExceptionally(new AkkaRpcException(
-			String.format("Unexpected closing of %s with name %s.", getClass().getSimpleName(), akkaRpcActorRegistration.getEndpointId())));
+		akkaRpcActorRegistration.terminateExceptionally(
+			new AkkaRpcException(
+				String.format("Unexpected closing of %s with name %s.", getClass().getSimpleName(), akkaRpcActorRegistration.getEndpointId())),
+			terminationFutureExecutor);
 	}
 
 	private void createStartAkkaRpcActorMessage(StartAkkaRpcActor startAkkaRpcActor) {
@@ -120,7 +126,7 @@ class SupervisorActor extends AbstractActor {
 		final AkkaRpcActorRegistration actorRegistration = removeAkkaRpcActor(actorRef);
 
 		LOG.debug("AkkaRpcActor {} has terminated.", actorRef.path());
-		actorRegistration.terminate();
+		actorRegistration.terminate(terminationFutureExecutor);
 	}
 
 	private void akkaRpcActorFailed(ActorRef actorRef, Throwable cause) {
@@ -155,8 +161,8 @@ class SupervisorActor extends AbstractActor {
 		return AkkaRpcServiceUtils.SUPERVISOR_NAME;
 	}
 
-	public static ActorRef startSupervisorActor(ActorSystem actorSystem) {
-		final Props supervisorProps = Props.create(SupervisorActor.class).withDispatcher("akka.actor.supervisor-dispatcher");
+	public static ActorRef startSupervisorActor(ActorSystem actorSystem, Executor terminationFutureExecutor) {
+		final Props supervisorProps = Props.create(SupervisorActor.class, terminationFutureExecutor).withDispatcher("akka.actor.supervisor-dispatcher");
 		return actorSystem.actorOf(supervisorProps, getActorName());
 	}
 
@@ -240,7 +246,7 @@ class SupervisorActor extends AbstractActor {
 			return endpointId;
 		}
 
-		private void terminate() {
+		private void terminate(Executor terminationFutureExecutor) {
 			CompletableFuture<Void> terminationFuture = internalTerminationFuture;
 
 			if (errorCause != null) {
@@ -261,11 +267,11 @@ class SupervisorActor extends AbstractActor {
 						String.format("RpcEndpoint %s did not complete the internal termination future.", endpointId)));
 			}
 
-			FutureUtils.forward(terminationFuture, externalTerminationFuture);
+			FutureUtils.forwardAsync(terminationFuture, externalTerminationFuture, terminationFutureExecutor);
 		}
 
-		private void terminateExceptionally(Throwable cause) {
-			externalTerminationFuture.completeExceptionally(cause);
+		private void terminateExceptionally(Throwable cause, Executor terminationFutureExecutor) {
+			terminationFutureExecutor.execute(() -> externalTerminationFuture.completeExceptionally(cause));
 		}
 
 		public void markFailed(Throwable cause) {
diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/rpc/akka/AkkaRpcActorTest.java b/flink-runtime/src/test/java/org/apache/flink/runtime/rpc/akka/AkkaRpcActorTest.java
index 404f5c1..5c104f4 100644
--- a/flink-runtime/src/test/java/org/apache/flink/runtime/rpc/akka/AkkaRpcActorTest.java
+++ b/flink-runtime/src/test/java/org/apache/flink/runtime/rpc/akka/AkkaRpcActorTest.java
@@ -450,6 +450,22 @@ public class AkkaRpcActorTest extends TestLogger {
 		}
 	}
 
+	@Test
+	public void terminationFutureDoesNotBlockRpcEndpointCreation() throws Exception {
+		try (final SimpleRpcEndpoint simpleRpcEndpoint = new SimpleRpcEndpoint(akkaRpcService, "foobar")) {
+			final CompletableFuture<Void> terminationFuture = simpleRpcEndpoint.getTerminationFuture();
+
+			// Creating a new RpcEndpoint within the termination future ensures that
+			// completing the termination future won't block the RpcService
+			final CompletableFuture<SimpleRpcEndpoint> foobar2 = terminationFuture.thenApply(ignored -> new SimpleRpcEndpoint(akkaRpcService, "foobar2"));
+
+			simpleRpcEndpoint.closeAsync();
+
+			final SimpleRpcEndpoint simpleRpcEndpoint2 = foobar2.join();
+			simpleRpcEndpoint2.close();
+		}
+	}
+
 	// ------------------------------------------------------------------------
 	//  Test Actors and Interfaces
 	// ------------------------------------------------------------------------
diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/rpc/akka/SupervisorActorTest.java b/flink-runtime/src/test/java/org/apache/flink/runtime/rpc/akka/SupervisorActorTest.java
index ecf68ee..b1aacbd 100644
--- a/flink-runtime/src/test/java/org/apache/flink/runtime/rpc/akka/SupervisorActorTest.java
+++ b/flink-runtime/src/test/java/org/apache/flink/runtime/rpc/akka/SupervisorActorTest.java
@@ -52,7 +52,7 @@ public class SupervisorActorTest extends TestLogger {
 	public void completesTerminationFutureIfActorStops() {
 		final ActorSystem actorSystem = actorSystemResource.getActorSystem();
 
-		final ActorRef supervisor = SupervisorActor.startSupervisorActor(actorSystem);
+		final ActorRef supervisor = SupervisorActor.startSupervisorActor(actorSystem, actorSystem.getDispatcher());
 
 		final SupervisorActor.ActorRegistration actorRegistration = startAkkaRpcActor(supervisor, "foobar");
 
@@ -68,7 +68,7 @@ public class SupervisorActorTest extends TestLogger {
 	public void completesTerminationFutureExceptionallyIfActorStopsExceptionally() throws Exception {
 		final ActorSystem actorSystem = actorSystemResource.getActorSystem();
 
-		final ActorRef supervisor = SupervisorActor.startSupervisorActor(actorSystem);
+		final ActorRef supervisor = SupervisorActor.startSupervisorActor(actorSystem, actorSystem.getDispatcher());
 
 		final SupervisorActor.ActorRegistration actorRegistration = startAkkaRpcActor(supervisor, "foobar");
 
@@ -91,7 +91,7 @@ public class SupervisorActorTest extends TestLogger {
 	public void completesTerminationFutureExceptionallyIfActorStopsWithoutReason() throws InterruptedException {
 		final ActorSystem actorSystem = actorSystemResource.getActorSystem();
 
-		final ActorRef supervisor = SupervisorActor.startSupervisorActor(actorSystem);
+		final ActorRef supervisor = SupervisorActor.startSupervisorActor(actorSystem, actorSystem.getDispatcher());
 
 		final SupervisorActor.ActorRegistration actorRegistration = startAkkaRpcActor(supervisor, "foobar");
 
@@ -110,7 +110,7 @@ public class SupervisorActorTest extends TestLogger {
 	public void completesTerminationFutureExceptionallyIfActorFails() throws Exception {
 		final ActorSystem actorSystem = actorSystemResource.getActorSystem();
 
-		final ActorRef supervisor = SupervisorActor.startSupervisorActor(actorSystem);
+		final ActorRef supervisor = SupervisorActor.startSupervisorActor(actorSystem, actorSystem.getDispatcher());
 
 		final SupervisorActor.ActorRegistration actorRegistration = startAkkaRpcActor(supervisor, "foobar");
 
@@ -138,7 +138,7 @@ public class SupervisorActorTest extends TestLogger {
 	public void completesTerminationFutureOfSiblingsIfActorFails() throws Exception {
 		final ActorSystem actorSystem = actorSystemResource.getActorSystem();
 
-		final ActorRef supervisor = SupervisorActor.startSupervisorActor(actorSystem);
+		final ActorRef supervisor = SupervisorActor.startSupervisorActor(actorSystem, actorSystem.getDispatcher());
 
 		final SupervisorActor.ActorRegistration actorRegistration1 = startAkkaRpcActor(supervisor, "foobar1");
 		final SupervisorActor.ActorRegistration actorRegistration2 = startAkkaRpcActor(supervisor, "foobar2");


[flink] 04/11: [hotfix] Add AkkRpcRuntimeException

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

trohrmann pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git

commit 067b9c0f17f3f6b037ceaf462d69973e9205d41c
Author: Till Rohrmann <tr...@apache.org>
AuthorDate: Wed Apr 8 12:15:36 2020 +0200

    [hotfix] Add AkkRpcRuntimeException
---
 .../akka/exceptions/AkkaRpcRuntimeException.java   | 38 ++++++++++++++++++++++
 1 file changed, 38 insertions(+)

diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/akka/exceptions/AkkaRpcRuntimeException.java b/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/akka/exceptions/AkkaRpcRuntimeException.java
new file mode 100644
index 0000000..2a36c41
--- /dev/null
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/akka/exceptions/AkkaRpcRuntimeException.java
@@ -0,0 +1,38 @@
+/*
+ * 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.rpc.akka.exceptions;
+
+import org.apache.flink.runtime.rpc.exceptions.RpcRuntimeException;
+
+/**
+ *  Base class for Akka RPC related runtime exceptions.
+ */
+public class AkkaRpcRuntimeException extends RpcRuntimeException {
+	public AkkaRpcRuntimeException(String message) {
+		super(message);
+	}
+
+	public AkkaRpcRuntimeException(Throwable cause) {
+		super(cause);
+	}
+
+	public AkkaRpcRuntimeException(String message, Throwable cause) {
+		super(message, cause);
+	}
+}


[flink] 10/11: [hotfix] Add FutureUtils.forwardAsync

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

trohrmann pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git

commit df30eddb4a5c97c22ef048dafbd8c46d5603b06c
Author: Till Rohrmann <tr...@apache.org>
AuthorDate: Wed Apr 15 15:11:59 2020 +0200

    [hotfix] Add FutureUtils.forwardAsync
    
    FutureUtils.forwardAsync forwards the source value to the target future using
    the provided executor.
---
 .../flink/runtime/concurrent/FutureUtils.java      | 22 ++++++++++++++++++++--
 .../flink/runtime/concurrent/FutureUtilsTest.java  | 18 ++++++++++++++++++
 2 files changed, 38 insertions(+), 2 deletions(-)

diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/concurrent/FutureUtils.java b/flink-runtime/src/main/java/org/apache/flink/runtime/concurrent/FutureUtils.java
index 175f6ed..7ea36e0 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/concurrent/FutureUtils.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/concurrent/FutureUtils.java
@@ -1094,12 +1094,30 @@ public class FutureUtils {
 	 * @param <T> type of the value
 	 */
 	public static <T> void forward(CompletableFuture<T> source, CompletableFuture<T> target) {
-		source.whenComplete((value, throwable) -> {
+		source.whenComplete(forwardTo(target));
+	}
+
+	/**
+	 * Forwards the value from the source future to the target future using the provided executor.
+	 *
+	 * @param source future to forward the value from
+	 * @param target future to forward the value to
+	 * @param executor executor to forward the source value to the target future
+	 * @param <T> type of the value
+	 */
+	public static <T> void forwardAsync(CompletableFuture<T> source, CompletableFuture<T> target, Executor executor) {
+		source.whenCompleteAsync(
+			forwardTo(target),
+			executor);
+	}
+
+	private static <T> BiConsumer<T, Throwable> forwardTo(CompletableFuture<T> target) {
+		return (value, throwable) -> {
 			if (throwable != null) {
 				target.completeExceptionally(throwable);
 			} else {
 				target.complete(value);
 			}
-		});
+		};
 	}
 }
diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/concurrent/FutureUtilsTest.java b/flink-runtime/src/test/java/org/apache/flink/runtime/concurrent/FutureUtilsTest.java
index bb328b4..397ccd6 100644
--- a/flink-runtime/src/test/java/org/apache/flink/runtime/concurrent/FutureUtilsTest.java
+++ b/flink-runtime/src/test/java/org/apache/flink/runtime/concurrent/FutureUtilsTest.java
@@ -803,6 +803,24 @@ public class FutureUtilsTest extends TestLogger {
 	}
 
 	@Test
+	public void testForwardAsync() throws Exception {
+		final CompletableFuture<String> source = new CompletableFuture<>();
+		final CompletableFuture<String> target = new CompletableFuture<>();
+		final ManuallyTriggeredScheduledExecutor executor = new ManuallyTriggeredScheduledExecutor();
+
+		FutureUtils.forwardAsync(source, target, executor);
+
+		source.complete("foobar");
+
+		assertThat(target.isDone(), is(false));
+
+		// execute the forward action
+		executor.triggerAll();
+
+		assertThat(target.get(), is(equalTo(source.get())));
+	}
+
+	@Test
 	public void testGetWithoutException() {
 		final CompletableFuture<Integer> completableFuture = new CompletableFuture<>();
 		completableFuture.complete(1);


[flink] 08/11: [FLINK-15347] Add SupervisorActor which monitors the proper termination of AkkaRpcActors

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

trohrmann pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git

commit f0529de9a35009c190863060c8bda1328b3bfd83
Author: Till Rohrmann <tr...@apache.org>
AuthorDate: Thu Apr 9 10:26:27 2020 +0200

    [FLINK-15347] Add SupervisorActor which monitors the proper termination of AkkaRpcActors
    
    In order to properly complete the termination future of an RpcEndpoint, we need to monitor
    when the underlying AkkaRpcActor has been removed from the ActorSystem. If this is not done,
    then it can happen that another RpcEndpoint using the same name cannot be started because
    the old RpcEndpoint is still registered.
    
    The way we achieve this monitoring is to introduce a helper actor which is responsible for
    starting the AkkaRpcActors for all RpcEndpoints. Since the SupervisorActor is the parent
    of all RpcEndpoints, it can tell when they are being removed from the ActorSystem through
    the SupervisorStrategy.
    
    A consequence of the new actor is that the akka urls change from akka://flink@actorsystem:port/user/xyz
    to akka://flink@actorsystem:port/user/rpc/xyz. The respective method AkkaRpcServiceUtils.getRpcUrl
    has been updated to reflect this change. This hierarchy change also warrants the bump of the
    AkkaRpcService.VERSION.
    
    The failure behaviour of the underlying ActorSystem has been changed so that it terminates
    all running actors if an exception is thrown from the SupervisorActor. The assumption is that
    such an exception always indicates a programming error and is unrecoverable.
    
    The same applies to failure originating from an AkkaRpcActor (children of the SupervisorActor).
    If such an exception is thrown, then we assume that the system is in an illegal state and shut it
    down. The way it works is by recording the failure cause for the respective AkkaRpcActor and
    then terminating the ActorSystem.
---
 ...tegy.java => EscalatingSupervisorStrategy.java} |  21 +-
 .../flink/runtime/rpc/akka/AkkaRpcService.java     |  77 +++--
 .../runtime/rpc/akka/AkkaRpcServiceUtils.java      |   4 +-
 .../flink/runtime/rpc/akka/SupervisorActor.java    | 360 +++++++++++++++++++++
 .../org/apache/flink/runtime/akka/AkkaUtils.scala  |   2 +-
 .../runtime/rpc/akka/AkkaActorSystemTest.java      |  89 +++++
 .../flink/runtime/rpc/akka/AkkaRpcActorTest.java   |  22 ++
 .../runtime/rpc/akka/SupervisorActorTest.java      | 249 ++++++++++++++
 8 files changed, 777 insertions(+), 47 deletions(-)

diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/akka/StoppingSupervisorWithoutLoggingActorKilledExceptionStrategy.java b/flink-runtime/src/main/java/org/apache/flink/runtime/akka/EscalatingSupervisorStrategy.java
similarity index 61%
rename from flink-runtime/src/main/java/org/apache/flink/runtime/akka/StoppingSupervisorWithoutLoggingActorKilledExceptionStrategy.java
rename to flink-runtime/src/main/java/org/apache/flink/runtime/akka/EscalatingSupervisorStrategy.java
index e2d8fd6..dc0832f 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/akka/StoppingSupervisorWithoutLoggingActorKilledExceptionStrategy.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/akka/EscalatingSupervisorStrategy.java
@@ -18,36 +18,23 @@
 
 package org.apache.flink.runtime.akka;
 
-import akka.actor.ActorKilledException;
 import akka.actor.OneForOneStrategy;
 import akka.actor.SupervisorStrategy;
 import akka.actor.SupervisorStrategyConfigurator;
 import akka.japi.pf.PFBuilder;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /**
- * Stopping supervisor strategy which logs {@link ActorKilledException} only on debug log level.
+ * Escalating supervisor strategy.
  */
-public class StoppingSupervisorWithoutLoggingActorKilledExceptionStrategy implements SupervisorStrategyConfigurator {
-
-	private static final Logger LOG = LoggerFactory.getLogger(StoppingSupervisorWithoutLoggingActorKilledExceptionStrategy.class);
+public class EscalatingSupervisorStrategy implements SupervisorStrategyConfigurator {
 
 	@Override
 	public SupervisorStrategy create() {
 		return new OneForOneStrategy(
 			false,
 			new PFBuilder<Throwable, SupervisorStrategy.Directive>()
-				.match(
-					Exception.class,
-					(Exception e) -> {
-						if (e instanceof ActorKilledException) {
-							LOG.debug("Actor was killed. Stopping it now.", e);
-						} else {
-							LOG.error("Actor failed with exception. Stopping it now.", e);
-						}
-						return SupervisorStrategy.Stop$.MODULE$;
-					})
+				.matchAny(
+					(ignored) -> SupervisorStrategy.escalate())
 				.build());
 	}
 }
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/akka/AkkaRpcService.java b/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/akka/AkkaRpcService.java
index 87a7682..f3604de 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/akka/AkkaRpcService.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/akka/AkkaRpcService.java
@@ -32,10 +32,12 @@ import org.apache.flink.runtime.rpc.RpcGateway;
 import org.apache.flink.runtime.rpc.RpcServer;
 import org.apache.flink.runtime.rpc.RpcService;
 import org.apache.flink.runtime.rpc.RpcUtils;
+import org.apache.flink.runtime.rpc.akka.exceptions.AkkaRpcRuntimeException;
 import org.apache.flink.runtime.rpc.exceptions.RpcConnectionException;
 import org.apache.flink.runtime.rpc.messages.HandshakeSuccessMessage;
 import org.apache.flink.runtime.rpc.messages.RemoteHandshakeMessage;
 
+import akka.actor.AbstractActor;
 import akka.actor.ActorIdentity;
 import akka.actor.ActorRef;
 import akka.actor.ActorSelection;
@@ -105,6 +107,8 @@ public class AkkaRpcService implements RpcService {
 
 	private final CompletableFuture<Void> terminationFuture;
 
+	private final ActorRef supervisor;
+
 	private volatile boolean stopped;
 
 	@VisibleForTesting
@@ -133,6 +137,12 @@ public class AkkaRpcService implements RpcService {
 		terminationFuture = new CompletableFuture<>();
 
 		stopped = false;
+
+		supervisor = startSupervisorActor();
+	}
+
+	private ActorRef startSupervisorActor() {
+		return SupervisorActor.startSupervisorActor(actorSystem);
 	}
 
 	public ActorSystem getActorSystem() {
@@ -201,32 +211,9 @@ public class AkkaRpcService implements RpcService {
 	public <C extends RpcEndpoint & RpcGateway> RpcServer startServer(C rpcEndpoint) {
 		checkNotNull(rpcEndpoint, "rpc endpoint");
 
-		CompletableFuture<Void> terminationFuture = new CompletableFuture<>();
-		final Props akkaRpcActorProps;
-
-		if (rpcEndpoint instanceof FencedRpcEndpoint) {
-			akkaRpcActorProps = Props.create(
-				FencedAkkaRpcActor.class,
-				rpcEndpoint,
-				terminationFuture,
-				getVersion(),
-				configuration.getMaximumFramesize());
-		} else {
-			akkaRpcActorProps = Props.create(
-				AkkaRpcActor.class,
-				rpcEndpoint,
-				terminationFuture,
-				getVersion(),
-				configuration.getMaximumFramesize());
-		}
-
-		ActorRef actorRef;
-
-		synchronized (lock) {
-			checkState(!stopped, "RpcService is stopped");
-			actorRef = actorSystem.actorOf(akkaRpcActorProps, rpcEndpoint.getEndpointId());
-			actors.put(actorRef, rpcEndpoint);
-		}
+		final SupervisorActor.ActorRegistration actorRegistration = registerAkkaRpcActor(rpcEndpoint);
+		final ActorRef actorRef = actorRegistration.getActorRef();
+		final CompletableFuture<Void> actorTerminationFuture = actorRegistration.getTerminationFuture();
 
 		LOG.info("Starting RPC endpoint for {} at {} .", rpcEndpoint.getClass().getName(), actorRef.path());
 
@@ -254,7 +241,7 @@ public class AkkaRpcService implements RpcService {
 				actorRef,
 				configuration.getTimeout(),
 				configuration.getMaximumFramesize(),
-				terminationFuture,
+				actorTerminationFuture,
 				((FencedRpcEndpoint<?>) rpcEndpoint)::getFencingToken,
 				captureAskCallstacks);
 
@@ -266,7 +253,7 @@ public class AkkaRpcService implements RpcService {
 				actorRef,
 				configuration.getTimeout(),
 				configuration.getMaximumFramesize(),
-				terminationFuture,
+				actorTerminationFuture,
 				captureAskCallstacks);
 		}
 
@@ -284,6 +271,40 @@ public class AkkaRpcService implements RpcService {
 		return server;
 	}
 
+	private <C extends RpcEndpoint & RpcGateway> SupervisorActor.ActorRegistration registerAkkaRpcActor(C rpcEndpoint) {
+		final Class<? extends AbstractActor> akkaRpcActorType;
+
+		if (rpcEndpoint instanceof FencedRpcEndpoint) {
+			akkaRpcActorType = FencedAkkaRpcActor.class;
+		} else {
+			akkaRpcActorType = AkkaRpcActor.class;
+		}
+
+		synchronized (lock) {
+			checkState(!stopped, "RpcService is stopped");
+
+			final SupervisorActor.StartAkkaRpcActorResponse startAkkaRpcActorResponse = SupervisorActor.startAkkaRpcActor(
+				supervisor,
+				actorTerminationFuture -> Props.create(
+					akkaRpcActorType,
+					rpcEndpoint,
+					actorTerminationFuture,
+					getVersion(),
+					configuration.getMaximumFramesize()),
+				rpcEndpoint.getEndpointId());
+
+			final SupervisorActor.ActorRegistration actorRegistration = startAkkaRpcActorResponse.orElseThrow(cause -> new AkkaRpcRuntimeException(
+				String.format("Could not create the %s for %s.",
+					AkkaRpcActor.class.getSimpleName(),
+					rpcEndpoint.getEndpointId()),
+				cause));
+
+			actors.put(actorRegistration.getActorRef(), rpcEndpoint);
+
+			return actorRegistration;
+		}
+	}
+
 	@Override
 	public <F extends Serializable> RpcServer fenceRpcServer(RpcServer rpcServer, F fencingToken) {
 		if (rpcServer instanceof AkkaBasedEndpoint) {
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/akka/AkkaRpcServiceUtils.java b/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/akka/AkkaRpcServiceUtils.java
index 1b50e45..5b464f4 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/akka/AkkaRpcServiceUtils.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/akka/AkkaRpcServiceUtils.java
@@ -57,6 +57,8 @@ public class AkkaRpcServiceUtils {
 	private static final String AKKA_TCP = "akka.tcp";
 	private static final String AKKA_SSL_TCP = "akka.ssl.tcp";
 
+	static final String SUPERVISOR_NAME = "rpc";
+
 	private static final String SIMPLE_AKKA_CONFIG_TEMPLATE =
 		"akka {remote {netty.tcp {maximum-frame-size = %s}}}";
 
@@ -166,7 +168,7 @@ public class AkkaRpcServiceUtils {
 
 		final String hostPort = NetUtils.unresolvedHostAndPortToNormalizedString(hostname, port);
 
-		return String.format("%s://flink@%s/user/%s", protocolPrefix, hostPort, endpointName);
+		return String.format("%s://flink@%s/user/%s/%s", protocolPrefix, hostPort, SUPERVISOR_NAME, endpointName);
 	}
 
 	/**
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/akka/SupervisorActor.java b/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/akka/SupervisorActor.java
new file mode 100644
index 0000000..e74ad67
--- /dev/null
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/akka/SupervisorActor.java
@@ -0,0 +1,360 @@
+/*
+ * 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.rpc.akka;
+
+import org.apache.flink.runtime.concurrent.FutureUtils;
+import org.apache.flink.runtime.rpc.RpcUtils;
+import org.apache.flink.runtime.rpc.akka.exceptions.AkkaRpcException;
+import org.apache.flink.runtime.rpc.akka.exceptions.AkkaUnknownMessageException;
+import org.apache.flink.util.Preconditions;
+
+import akka.AkkaException;
+import akka.actor.AbstractActor;
+import akka.actor.ActorRef;
+import akka.actor.ActorSystem;
+import akka.actor.ChildRestartStats;
+import akka.actor.Props;
+import akka.actor.SupervisorStrategy;
+import akka.japi.pf.DeciderBuilder;
+import akka.pattern.Patterns;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nullable;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CompletionException;
+import java.util.function.Function;
+
+import scala.PartialFunction;
+import scala.collection.Iterable;
+
+/**
+ * Supervisor actor which is responsible for starting {@link AkkaRpcActor} instances and monitoring
+ * when the actors have terminated.
+ */
+class SupervisorActor extends AbstractActor {
+
+	private static final Logger LOG = LoggerFactory.getLogger(SupervisorActor.class);
+
+	private final Map<ActorRef, AkkaRpcActorRegistration> registeredAkkaRpcActors;
+
+	SupervisorActor() {
+		this.registeredAkkaRpcActors = new HashMap<>();
+	}
+
+	@Override
+	public Receive createReceive() {
+		return receiveBuilder()
+			.match(StartAkkaRpcActor.class, this::createStartAkkaRpcActorMessage)
+			.matchAny(this::handleUnknownMessage)
+			.build();
+	}
+
+	@Override
+	public void postStop() throws Exception {
+		LOG.debug("Stopping supervisor actor.");
+
+		super.postStop();
+
+		for (AkkaRpcActorRegistration actorRegistration : registeredAkkaRpcActors.values()) {
+			terminateAkkaRpcActorOnStop(actorRegistration);
+		}
+
+		registeredAkkaRpcActors.clear();
+	}
+
+	@Override
+	public SupervisorActorSupervisorStrategy supervisorStrategy() {
+		return new SupervisorActorSupervisorStrategy();
+	}
+
+	private void terminateAkkaRpcActorOnStop(AkkaRpcActorRegistration akkaRpcActorRegistration) {
+		akkaRpcActorRegistration.terminateExceptionally(new AkkaRpcException(
+			String.format("Unexpected closing of %s with name %s.", getClass().getSimpleName(), akkaRpcActorRegistration.getEndpointId())));
+	}
+
+	private void createStartAkkaRpcActorMessage(StartAkkaRpcActor startAkkaRpcActor) {
+		final String endpointId = startAkkaRpcActor.getEndpointId();
+		final AkkaRpcActorRegistration akkaRpcActorRegistration = new AkkaRpcActorRegistration(endpointId);
+
+		final Props akkaRpcActorProps = startAkkaRpcActor.getPropsFactory().create(akkaRpcActorRegistration.getInternalTerminationFuture());
+
+		LOG.debug("Starting {} with name {}.", akkaRpcActorProps.actorClass().getSimpleName(), endpointId);
+
+		try {
+			final ActorRef actorRef = getContext().actorOf(akkaRpcActorProps, endpointId);
+
+			registeredAkkaRpcActors.put(actorRef, akkaRpcActorRegistration);
+
+			getSender().tell(StartAkkaRpcActorResponse.success(
+				ActorRegistration.create(
+					actorRef,
+					akkaRpcActorRegistration.getExternalTerminationFuture())),
+				getSelf());
+		} catch (AkkaException akkaException) {
+			getSender().tell(StartAkkaRpcActorResponse.failure(akkaException), getSelf());
+		}
+	}
+
+	private void akkaRpcActorTerminated(ActorRef actorRef) {
+		final AkkaRpcActorRegistration actorRegistration = removeAkkaRpcActor(actorRef);
+
+		LOG.debug("AkkaRpcActor {} has terminated.", actorRef.path());
+		actorRegistration.terminate();
+	}
+
+	private void akkaRpcActorFailed(ActorRef actorRef, Throwable cause) {
+		LOG.warn("AkkaRpcActor {} has failed. Shutting it down now.", actorRef.path(), cause);
+
+		for (Map.Entry<ActorRef, AkkaRpcActorRegistration> registeredAkkaRpcActor : registeredAkkaRpcActors.entrySet()) {
+			final ActorRef otherActorRef = registeredAkkaRpcActor.getKey();
+			if (otherActorRef.equals(actorRef)) {
+				final AkkaRpcException error = new AkkaRpcException(String.format("Stopping actor %s because it failed.", actorRef.path()), cause);
+				registeredAkkaRpcActor.getValue().markFailed(error);
+			} else {
+				final AkkaRpcException siblingException = new AkkaRpcException(String.format("Stopping actor %s because its sibling %s has failed.", otherActorRef.path(), actorRef.path()));
+				registeredAkkaRpcActor.getValue().markFailed(siblingException);
+			}
+		}
+
+		getContext().getSystem().terminate();
+	}
+
+	private AkkaRpcActorRegistration removeAkkaRpcActor(ActorRef actorRef) {
+		return Optional.ofNullable(registeredAkkaRpcActors.remove(actorRef))
+			.orElseThrow(() -> new IllegalStateException(String.format("Could not find actor %s.", actorRef.path())));
+	}
+
+	private void handleUnknownMessage(Object msg) {
+		final AkkaUnknownMessageException cause = new AkkaUnknownMessageException(String.format("Cannot handle unknown message %s.", msg));
+		getSender().tell(new akka.actor.Status.Failure(cause), getSelf());
+		throw cause;
+	}
+
+	public static String getActorName() {
+		return AkkaRpcServiceUtils.SUPERVISOR_NAME;
+	}
+
+	public static ActorRef startSupervisorActor(ActorSystem actorSystem) {
+		final Props supervisorProps = Props.create(SupervisorActor.class);
+		return actorSystem.actorOf(supervisorProps, getActorName());
+	}
+
+	public static StartAkkaRpcActorResponse startAkkaRpcActor(
+			ActorRef supervisor,
+			StartAkkaRpcActor.PropsFactory propsFactory,
+			String endpointId) {
+		return Patterns.ask(
+				supervisor,
+				createStartAkkaRpcActorMessage(
+					propsFactory,
+					endpointId),
+				RpcUtils.INF_DURATION).toCompletableFuture()
+			.thenApply(SupervisorActor.StartAkkaRpcActorResponse.class::cast)
+			.join();
+	}
+
+	public static StartAkkaRpcActor createStartAkkaRpcActorMessage(
+			StartAkkaRpcActor.PropsFactory propsFactory,
+			String endpointId) {
+		return StartAkkaRpcActor.create(propsFactory, endpointId);
+	}
+
+	// -----------------------------------------------------------------------------
+	// Internal classes
+	// -----------------------------------------------------------------------------
+
+	private final class SupervisorActorSupervisorStrategy extends SupervisorStrategy {
+
+		@Override
+		public PartialFunction<Throwable, Directive> decider() {
+			return DeciderBuilder.match(
+				Exception.class, e -> SupervisorStrategy.stop()
+			).build();
+		}
+
+		@Override
+		public boolean loggingEnabled() {
+			return false;
+		}
+
+		@Override
+		public void handleChildTerminated(akka.actor.ActorContext context, ActorRef child, Iterable<ActorRef> children) {
+			akkaRpcActorTerminated(child);
+		}
+
+		@Override
+		public void processFailure(akka.actor.ActorContext context, boolean restart, ActorRef child, Throwable cause, ChildRestartStats stats, Iterable<ChildRestartStats> children) {
+			Preconditions.checkArgument(!restart, "The supervisor strategy should never restart an actor.");
+
+			akkaRpcActorFailed(child, cause);
+		}
+	}
+
+	private static final class AkkaRpcActorRegistration {
+		private final String endpointId;
+
+		private final CompletableFuture<Void> internalTerminationFuture;
+
+		private final CompletableFuture<Void> externalTerminationFuture;
+
+		@Nullable
+		private Throwable errorCause;
+
+		private AkkaRpcActorRegistration(String endpointId) {
+			this.endpointId = endpointId;
+			internalTerminationFuture = new CompletableFuture<>();
+			externalTerminationFuture = new CompletableFuture<>();
+			errorCause = null;
+		}
+
+		private CompletableFuture<Void> getInternalTerminationFuture() {
+			return internalTerminationFuture;
+		}
+
+		private CompletableFuture<Void> getExternalTerminationFuture() {
+			return externalTerminationFuture;
+		}
+
+		private String getEndpointId() {
+			return endpointId;
+		}
+
+		private void terminate() {
+			CompletableFuture<Void> terminationFuture = internalTerminationFuture;
+
+			if (errorCause != null) {
+				if (!internalTerminationFuture.completeExceptionally(errorCause)) {
+					// we have another failure reason -> let's add it
+					terminationFuture = internalTerminationFuture.handle(
+						(ignored, throwable) -> {
+							if (throwable != null) {
+								errorCause.addSuppressed(throwable);
+							}
+
+							throw new CompletionException(errorCause);
+						});
+				}
+			} else {
+				internalTerminationFuture.completeExceptionally(
+					new AkkaRpcException(
+						String.format("RpcEndpoint %s did not complete the internal termination future.", endpointId)));
+			}
+
+			FutureUtils.forward(terminationFuture, externalTerminationFuture);
+		}
+
+		private void terminateExceptionally(Throwable cause) {
+			externalTerminationFuture.completeExceptionally(cause);
+		}
+
+		public void markFailed(Throwable cause) {
+			if (errorCause == null) {
+				errorCause = cause;
+			} else {
+				errorCause.addSuppressed(cause);
+			}
+		}
+	}
+
+	// -----------------------------------------------------------------------------
+	// Messages
+	// -----------------------------------------------------------------------------
+
+	static final class StartAkkaRpcActor {
+		private final PropsFactory propsFactory;
+		private final String endpointId;
+
+		private StartAkkaRpcActor(PropsFactory propsFactory, String endpointId) {
+			this.propsFactory = propsFactory;
+			this.endpointId = endpointId;
+		}
+
+		public String getEndpointId() {
+			return endpointId;
+		}
+
+		public PropsFactory getPropsFactory() {
+			return propsFactory;
+		}
+
+		private static StartAkkaRpcActor create(PropsFactory propsFactory, String endpointId) {
+			return new StartAkkaRpcActor(propsFactory, endpointId);
+		}
+
+		interface PropsFactory {
+			Props create(CompletableFuture<Void> terminationFuture);
+		}
+	}
+
+	static final class ActorRegistration {
+		private final ActorRef actorRef;
+		private final CompletableFuture<Void> terminationFuture;
+
+		private ActorRegistration(ActorRef actorRef, CompletableFuture<Void> terminationFuture) {
+			this.actorRef = actorRef;
+			this.terminationFuture = terminationFuture;
+		}
+
+		public ActorRef getActorRef() {
+			return actorRef;
+		}
+
+		public CompletableFuture<Void> getTerminationFuture() {
+			return terminationFuture;
+		}
+
+		public static ActorRegistration create(ActorRef actorRef, CompletableFuture<Void> terminationFuture) {
+			return new ActorRegistration(actorRef, terminationFuture);
+		}
+	}
+
+	static final class StartAkkaRpcActorResponse {
+		@Nullable
+		private final ActorRegistration actorRegistration;
+
+		@Nullable
+		private final Throwable error;
+
+		private StartAkkaRpcActorResponse(@Nullable ActorRegistration actorRegistration, @Nullable Throwable error) {
+			this.actorRegistration = actorRegistration;
+			this.error = error;
+		}
+
+		public <X extends Throwable> ActorRegistration orElseThrow(Function<? super Throwable, ? extends X> throwableFunction) throws X {
+			if (actorRegistration != null) {
+				return actorRegistration;
+			} else {
+				throw throwableFunction.apply(error);
+			}
+		}
+
+		public static StartAkkaRpcActorResponse success(ActorRegistration actorRegistration) {
+			return new StartAkkaRpcActorResponse(actorRegistration, null);
+		}
+
+		public static StartAkkaRpcActorResponse failure(Throwable error) {
+			return new StartAkkaRpcActorResponse(null, error);
+		}
+	}
+}
diff --git a/flink-runtime/src/main/scala/org/apache/flink/runtime/akka/AkkaUtils.scala b/flink-runtime/src/main/scala/org/apache/flink/runtime/akka/AkkaUtils.scala
index efa15c2..adf0ebd 100755
--- a/flink-runtime/src/main/scala/org/apache/flink/runtime/akka/AkkaUtils.scala
+++ b/flink-runtime/src/main/scala/org/apache/flink/runtime/akka/AkkaUtils.scala
@@ -273,7 +273,7 @@ object AkkaUtils {
 
     val logLevel = getLogLevel
 
-    val supervisorStrategy = classOf[StoppingSupervisorWithoutLoggingActorKilledExceptionStrategy]
+    val supervisorStrategy = classOf[EscalatingSupervisorStrategy]
       .getCanonicalName
 
     val config =
diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/rpc/akka/AkkaActorSystemTest.java b/flink-runtime/src/test/java/org/apache/flink/runtime/rpc/akka/AkkaActorSystemTest.java
new file mode 100644
index 0000000..6d3830a
--- /dev/null
+++ b/flink-runtime/src/test/java/org/apache/flink/runtime/rpc/akka/AkkaActorSystemTest.java
@@ -0,0 +1,89 @@
+/*
+ * 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.rpc.akka;
+
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.runtime.akka.AkkaUtils;
+import org.apache.flink.util.FlinkException;
+import org.apache.flink.util.TestLogger;
+
+import akka.actor.AbstractActor;
+import akka.actor.ActorRef;
+import akka.actor.ActorSystem;
+import akka.actor.Props;
+import akka.actor.Terminated;
+import akka.japi.pf.ReceiveBuilder;
+import org.junit.Test;
+
+import java.util.concurrent.CompletableFuture;
+
+/**
+ * Tests for the {@link akka.actor.ActorSystem} instantiated through {@link AkkaUtils}.
+ */
+public class AkkaActorSystemTest extends TestLogger {
+
+	@Test
+	public void shutsDownOnActorFailure() {
+		final ActorSystem actorSystem = AkkaUtils.createLocalActorSystem(new Configuration());
+
+		try {
+			final CompletableFuture<Terminated> terminationFuture = actorSystem.getWhenTerminated().toCompletableFuture();
+			final ActorRef actorRef = actorSystem.actorOf(Props.create(SimpleActor.class));
+
+			final FlinkException cause = new FlinkException("Flink test exception");
+
+			actorRef.tell(Fail.exceptionally(cause), ActorRef.noSender());
+
+			// make sure that the ActorSystem shuts down
+			terminationFuture.join();
+		} finally {
+			AkkaUtils.terminateActorSystem(actorSystem).join();
+		}
+	}
+
+	private static final class SimpleActor extends AbstractActor {
+
+		@Override
+		public Receive createReceive() {
+			return ReceiveBuilder.create()
+				.match(Fail.class, this::handleFail)
+				.build();
+		}
+
+		private void handleFail(Fail fail) {
+			throw new RuntimeException(fail.getErrorCause());
+		}
+	}
+
+	private static final class Fail {
+		private final Throwable errorCause;
+
+		private Fail(Throwable errorCause) {
+			this.errorCause = errorCause;
+		}
+
+		private Throwable getErrorCause() {
+			return errorCause;
+		}
+
+		private static Fail exceptionally(Throwable errorCause) {
+			return new Fail(errorCause);
+		}
+	}
+}
diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/rpc/akka/AkkaRpcActorTest.java b/flink-runtime/src/test/java/org/apache/flink/runtime/rpc/akka/AkkaRpcActorTest.java
index 64270c4..404f5c1 100644
--- a/flink-runtime/src/test/java/org/apache/flink/runtime/rpc/akka/AkkaRpcActorTest.java
+++ b/flink-runtime/src/test/java/org/apache/flink/runtime/rpc/akka/AkkaRpcActorTest.java
@@ -42,6 +42,8 @@ import org.hamcrest.core.Is;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
 import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import javax.annotation.Nullable;
 
@@ -52,6 +54,7 @@ import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 import java.util.concurrent.atomic.AtomicInteger;
 
+import static org.hamcrest.Matchers.equalTo;
 import static org.hamcrest.Matchers.is;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
@@ -64,6 +67,8 @@ import static org.junit.Assert.fail;
  */
 public class AkkaRpcActorTest extends TestLogger {
 
+	private static final Logger LOG = LoggerFactory.getLogger(AkkaRpcActorTest.class);
+
 	// ------------------------------------------------------------------------
 	//  shared test members
 	// ------------------------------------------------------------------------
@@ -428,6 +433,23 @@ public class AkkaRpcActorTest extends TestLogger {
 		}
 	}
 
+	@Test
+	public void canReuseEndpointNameAfterTermination() throws Exception {
+		final String endpointName = "not_unique";
+		try (SimpleRpcEndpoint simpleRpcEndpoint1 = new SimpleRpcEndpoint(akkaRpcService, endpointName)) {
+
+			simpleRpcEndpoint1.start();
+
+			simpleRpcEndpoint1.closeAsync().join();
+
+			try (SimpleRpcEndpoint simpleRpcEndpoint2 = new SimpleRpcEndpoint(akkaRpcService, endpointName)) {
+				simpleRpcEndpoint2.start();
+
+				assertThat(simpleRpcEndpoint2.getAddress(), is(equalTo(simpleRpcEndpoint1.getAddress())));
+			}
+		}
+	}
+
 	// ------------------------------------------------------------------------
 	//  Test Actors and Interfaces
 	// ------------------------------------------------------------------------
diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/rpc/akka/SupervisorActorTest.java b/flink-runtime/src/test/java/org/apache/flink/runtime/rpc/akka/SupervisorActorTest.java
new file mode 100644
index 0000000..ecf68ee
--- /dev/null
+++ b/flink-runtime/src/test/java/org/apache/flink/runtime/rpc/akka/SupervisorActorTest.java
@@ -0,0 +1,249 @@
+/*
+ * 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.rpc.akka;
+
+import org.apache.flink.util.ExceptionUtils;
+import org.apache.flink.util.FlinkException;
+import org.apache.flink.util.TestLogger;
+
+import akka.actor.AbstractActor;
+import akka.actor.ActorRef;
+import akka.actor.ActorSystem;
+import akka.actor.Props;
+import akka.actor.Terminated;
+import akka.japi.pf.ReceiveBuilder;
+import org.junit.Rule;
+import org.junit.Test;
+
+import javax.annotation.Nullable;
+
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.fail;
+
+/**
+ * Tests for the {@link SupervisorActor}.
+ */
+public class SupervisorActorTest extends TestLogger {
+
+	@Rule
+	public final ActorSystemResource actorSystemResource = ActorSystemResource.defaultConfiguration();
+
+	@Test
+	public void completesTerminationFutureIfActorStops() {
+		final ActorSystem actorSystem = actorSystemResource.getActorSystem();
+
+		final ActorRef supervisor = SupervisorActor.startSupervisorActor(actorSystem);
+
+		final SupervisorActor.ActorRegistration actorRegistration = startAkkaRpcActor(supervisor, "foobar");
+
+		final CompletableFuture<Void> terminationFuture = actorRegistration.getTerminationFuture();
+		assertThat(terminationFuture.isDone(), is(false));
+
+		actorRegistration.getActorRef().tell(TerminateWithFutureCompletion.normal(), ActorRef.noSender());
+
+		terminationFuture.join();
+	}
+
+	@Test
+	public void completesTerminationFutureExceptionallyIfActorStopsExceptionally() throws Exception {
+		final ActorSystem actorSystem = actorSystemResource.getActorSystem();
+
+		final ActorRef supervisor = SupervisorActor.startSupervisorActor(actorSystem);
+
+		final SupervisorActor.ActorRegistration actorRegistration = startAkkaRpcActor(supervisor, "foobar");
+
+		final CompletableFuture<Void> terminationFuture = actorRegistration.getTerminationFuture();
+		assertThat(terminationFuture.isDone(), is(false));
+
+		final FlinkException cause = new FlinkException("Test cause.");
+		actorRegistration.getActorRef().tell(TerminateWithFutureCompletion.exceptionally(cause), ActorRef.noSender());
+
+		try {
+			terminationFuture.get();
+			fail("Expected the termination future being completed exceptionally");
+		} catch (ExecutionException expected) {
+			ExceptionUtils.findThrowable(expected, e -> e.equals(cause))
+				.orElseThrow(() -> new FlinkException("Unexpected exception", expected));
+		}
+	}
+
+	@Test
+	public void completesTerminationFutureExceptionallyIfActorStopsWithoutReason() throws InterruptedException {
+		final ActorSystem actorSystem = actorSystemResource.getActorSystem();
+
+		final ActorRef supervisor = SupervisorActor.startSupervisorActor(actorSystem);
+
+		final SupervisorActor.ActorRegistration actorRegistration = startAkkaRpcActor(supervisor, "foobar");
+
+		final CompletableFuture<Void> terminationFuture = actorRegistration.getTerminationFuture();
+		assertThat(terminationFuture.isDone(), is(false));
+
+		actorRegistration.getActorRef().tell(Terminate.INSTANCE, ActorRef.noSender());
+
+		try {
+			terminationFuture.get();
+			fail("Expected the termination future being completed exceptionally");
+		} catch (ExecutionException expected) {}
+	}
+
+	@Test
+	public void completesTerminationFutureExceptionallyIfActorFails() throws Exception {
+		final ActorSystem actorSystem = actorSystemResource.getActorSystem();
+
+		final ActorRef supervisor = SupervisorActor.startSupervisorActor(actorSystem);
+
+		final SupervisorActor.ActorRegistration actorRegistration = startAkkaRpcActor(supervisor, "foobar");
+
+		final CompletableFuture<Void> terminationFuture = actorRegistration.getTerminationFuture();
+		assertThat(terminationFuture.isDone(), is(false));
+
+		final CompletableFuture<Terminated> actorSystemTerminationFuture = actorSystem.getWhenTerminated().toCompletableFuture();
+
+		final FlinkException cause = new FlinkException("Test cause.");
+		actorRegistration.getActorRef().tell(Fail.exceptionally(cause), ActorRef.noSender());
+
+		try {
+			terminationFuture.get();
+			fail("Expected the termination future being completed exceptionally");
+		} catch (ExecutionException expected) {
+			ExceptionUtils.findThrowable(expected, e -> e.equals(cause))
+				.orElseThrow(() -> new FlinkException("Unexpected exception", expected));
+		}
+
+		// make sure that the supervisor actor has stopped --> terminating the actor system
+		actorSystemTerminationFuture.join();
+	}
+
+	@Test
+	public void completesTerminationFutureOfSiblingsIfActorFails() throws Exception {
+		final ActorSystem actorSystem = actorSystemResource.getActorSystem();
+
+		final ActorRef supervisor = SupervisorActor.startSupervisorActor(actorSystem);
+
+		final SupervisorActor.ActorRegistration actorRegistration1 = startAkkaRpcActor(supervisor, "foobar1");
+		final SupervisorActor.ActorRegistration actorRegistration2 = startAkkaRpcActor(supervisor, "foobar2");
+
+		final CompletableFuture<Void> terminationFuture = actorRegistration2.getTerminationFuture();
+		assertThat(terminationFuture.isDone(), is(false));
+
+		final FlinkException cause = new FlinkException("Test cause.");
+		actorRegistration1.getActorRef().tell(Fail.exceptionally(cause), ActorRef.noSender());
+
+		try {
+			terminationFuture.get();
+			fail("Expected the termination future being completed exceptionally");
+		} catch (ExecutionException expected) {}
+	}
+
+	private SupervisorActor.ActorRegistration startAkkaRpcActor(ActorRef supervisor, String endpointId) {
+		final SupervisorActor.StartAkkaRpcActorResponse startResponse = SupervisorActor.startAkkaRpcActor(
+			supervisor,
+			terminationFuture -> Props.create(SimpleActor.class, terminationFuture),
+			endpointId);
+
+		return startResponse.orElseThrow(cause -> new AssertionError("Expected the start to succeed.", cause));
+	}
+
+	private static final class SimpleActor extends AbstractActor {
+
+		private final CompletableFuture<Void> terminationFuture;
+
+		private SimpleActor(CompletableFuture<Void> terminationFuture) {
+			this.terminationFuture = terminationFuture;
+		}
+
+		@Override
+		public Receive createReceive() {
+			return ReceiveBuilder.create()
+				.match(Terminate.class, this::terminate)
+				.match(TerminateWithFutureCompletion.class, this::terminateActorWithFutureCompletion)
+				.match(Fail.class, this::fail)
+				.build();
+		}
+
+		private void fail(Fail fail) {
+			throw new RuntimeException(fail.getCause());
+		}
+
+		private void terminate(Terminate terminate) {
+			terminateActor();
+		}
+
+		private void terminateActor() {
+			getContext().stop(getSelf());
+		}
+
+		private void terminateActorWithFutureCompletion(TerminateWithFutureCompletion terminateWithFutureCompletion) {
+			final Throwable terminationError = terminateWithFutureCompletion.getTerminationError();
+			if (terminationError == null) {
+				terminationFuture.complete(null);
+			} else {
+				terminationFuture.completeExceptionally(terminationError);
+			}
+
+			terminateActor();
+		}
+	}
+
+	private static final class Terminate {
+		private static final Terminate INSTANCE = new Terminate();
+	}
+
+	private static final class TerminateWithFutureCompletion {
+		@Nullable
+		private final Throwable terminationError;
+
+		private TerminateWithFutureCompletion(@Nullable Throwable terminationError) {
+			this.terminationError = terminationError;
+		}
+
+		@Nullable
+		private Throwable getTerminationError() {
+			return terminationError;
+		}
+
+		private static TerminateWithFutureCompletion normal() {
+			return new TerminateWithFutureCompletion(null);
+		}
+
+		private static TerminateWithFutureCompletion exceptionally(Throwable cause) {
+			return new TerminateWithFutureCompletion(cause);
+		}
+	}
+
+	private static final class Fail {
+		private final Throwable cause;
+
+		private Fail(Throwable cause) {
+			this.cause = cause;
+		}
+
+		private Throwable getCause() {
+			return cause;
+		}
+
+		private static Fail exceptionally(Throwable cause) {
+			return new Fail(cause);
+		}
+	}
+}


[flink] 02/11: [hotfix] Add RpcUtils#INF_DURATION

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

trohrmann pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git

commit eb56ab4c04e168ab4ddf9f3c7d89525f4d3a0be3
Author: Till Rohrmann <tr...@apache.org>
AuthorDate: Wed Apr 8 11:48:48 2020 +0200

    [hotfix] Add RpcUtils#INF_DURATION
---
 flink-runtime/src/main/java/org/apache/flink/runtime/rpc/RpcUtils.java | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/RpcUtils.java b/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/RpcUtils.java
index 8b07b17..5f86e8f 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/RpcUtils.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/RpcUtils.java
@@ -22,6 +22,7 @@ import org.apache.flink.api.common.time.Time;
 import org.apache.flink.runtime.concurrent.FutureUtils;
 import org.apache.flink.util.AutoCloseableAsync;
 
+import java.time.Duration;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -45,6 +46,8 @@ public class RpcUtils {
 	 */
 	public static final Time INF_TIMEOUT = Time.seconds(21474835);
 
+	public static final Duration INF_DURATION = Duration.ofSeconds(21474835);
+
 	/**
 	 * Extracts all {@link RpcGateway} interfaces implemented by the given clazz.
 	 *


[flink] 09/11: [FLINK-15347] Introduce separate dispatcher for SupervisorActor

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

trohrmann pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git

commit 2cf79554971e6d0f9b8a4dcb2508f3968ce75b8e
Author: Till Rohrmann <tr...@apache.org>
AuthorDate: Thu Apr 9 15:08:48 2020 +0200

    [FLINK-15347] Introduce separate dispatcher for SupervisorActor
    
    The separate dispatcher ensures that the SupervisorActor can always make progress.
    This again ensures that the other RpcEndpoints won't deadlock when starting a new
    RpcEndpoint. The reason for this is because a newly started RpcEndpoint needs to
    register at the SupervisorActor.
---
 .../java/org/apache/flink/runtime/rpc/akka/SupervisorActor.java  | 2 +-
 .../src/main/scala/org/apache/flink/runtime/akka/AkkaUtils.scala | 9 +++++++++
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/akka/SupervisorActor.java b/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/akka/SupervisorActor.java
index e74ad67..3758fb2 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/akka/SupervisorActor.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/akka/SupervisorActor.java
@@ -156,7 +156,7 @@ class SupervisorActor extends AbstractActor {
 	}
 
 	public static ActorRef startSupervisorActor(ActorSystem actorSystem) {
-		final Props supervisorProps = Props.create(SupervisorActor.class);
+		final Props supervisorProps = Props.create(SupervisorActor.class).withDispatcher("akka.actor.supervisor-dispatcher");
 		return actorSystem.actorOf(supervisorProps, getActorName());
 	}
 
diff --git a/flink-runtime/src/main/scala/org/apache/flink/runtime/akka/AkkaUtils.scala b/flink-runtime/src/main/scala/org/apache/flink/runtime/akka/AkkaUtils.scala
index adf0ebd..f046a19 100755
--- a/flink-runtime/src/main/scala/org/apache/flink/runtime/akka/AkkaUtils.scala
+++ b/flink-runtime/src/main/scala/org/apache/flink/runtime/akka/AkkaUtils.scala
@@ -303,6 +303,15 @@ object AkkaUtils {
         |   default-dispatcher {
         |     throughput = $akkaThroughput
         |   }
+        |
+        |   supervisor-dispatcher {
+        |     type = Dispatcher
+        |     executor = "thread-pool-executor"
+        |     thread-pool-executor {
+        |       core-pool-size-min = 1
+        |       core-pool-size-max = 1
+        |     }
+        |   }
         | }
         |}
       """.stripMargin


[flink] 06/11: [FLINK-15347] Bump AkkRpcVersion to 2

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

trohrmann pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git

commit db375bb2b4e8ff5de0960558c250e511c1bbc05c
Author: Till Rohrmann <tr...@apache.org>
AuthorDate: Wed Apr 8 15:55:49 2020 +0200

    [FLINK-15347] Bump AkkRpcVersion to 2
---
 .../src/main/java/org/apache/flink/runtime/rpc/akka/AkkaRpcService.java | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/akka/AkkaRpcService.java b/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/akka/AkkaRpcService.java
index f629ffd..87a7682 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/akka/AkkaRpcService.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/akka/AkkaRpcService.java
@@ -86,7 +86,7 @@ public class AkkaRpcService implements RpcService {
 
 	private static final Logger LOG = LoggerFactory.getLogger(AkkaRpcService.class);
 
-	static final int VERSION = 1;
+	static final int VERSION = 2;
 
 	private final Object lock = new Object();
 


[flink] 03/11: [hotfix] Add RpcRuntimeException

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

trohrmann pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git

commit 03b5115f8d35514904c40ac1bcffb40a4b4f3728
Author: Till Rohrmann <tr...@apache.org>
AuthorDate: Wed Apr 8 12:15:17 2020 +0200

    [hotfix] Add RpcRuntimeException
---
 .../rpc/exceptions/RpcRuntimeException.java        | 37 ++++++++++++++++++++++
 1 file changed, 37 insertions(+)

diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/exceptions/RpcRuntimeException.java b/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/exceptions/RpcRuntimeException.java
new file mode 100644
index 0000000..666e83f
--- /dev/null
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/rpc/exceptions/RpcRuntimeException.java
@@ -0,0 +1,37 @@
+/*
+ * 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.rpc.exceptions;
+
+/**
+ * Base class for RPC related runtime exceptions.
+ */
+public class RpcRuntimeException extends RuntimeException {
+
+	public RpcRuntimeException(String message) {
+		super(message);
+	}
+
+	public RpcRuntimeException(Throwable cause) {
+		super(cause);
+	}
+
+	public RpcRuntimeException(String message, Throwable cause) {
+		super(message, cause);
+	}
+}