You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2020/09/22 09:06:40 UTC
[GitHub] [flink] zentol commented on a change in pull request #13424: [FLINK-19219] Run JobManager initialization in separate thread to make it cancellable
zentol commented on a change in pull request #13424:
URL: https://github.com/apache/flink/pull/13424#discussion_r492527287
##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/DispatcherJob.java
##########
@@ -119,11 +119,17 @@ private DispatcherJob(
}
});
} else { // failure during initialization
- final Throwable strippedThrowable = ExceptionUtils.stripCompletionException(throwable);
+ JobStatus finalStatus = JobStatus.FAILED;
+ Throwable strippedThrowable = ExceptionUtils.stripCompletionException(throwable);
+ if (throwable instanceof JobInitializationCancelledException) {
Review comment:
We could just query the job manager runner for whether it was canceled or not; relying on exception types is quite indirect and can be easily broken by accident.
##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/InitializingJobManagerRunner.java
##########
@@ -0,0 +1,105 @@
+/*
+ * 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.api.common.JobID;
+import org.apache.flink.runtime.client.JobInitializationException;
+import org.apache.flink.runtime.concurrent.FutureUtils;
+import org.apache.flink.runtime.jobmaster.JobManagerRunner;
+import org.apache.flink.runtime.messages.Acknowledge;
+import org.apache.flink.runtime.rpc.RpcUtils;
+import org.apache.flink.util.function.SupplierWithException;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.concurrent.GuardedBy;
+
+import java.util.concurrent.CompletableFuture;
+
+/**
+ * Wrapper for an initializing JobManagerRunner.
+ */
+public class InitializingJobManagerRunner {
+ private final Logger log = LoggerFactory.getLogger(InitializingJobManagerRunner.class);
Review comment:
should be static
##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/DispatcherJob.java
##########
@@ -165,17 +171,10 @@ private DispatcherJob(
if (isInitialized()) {
return getJobMasterGateway().thenCompose(jobMasterGateway -> jobMasterGateway.cancel(timeout));
} else {
- log.info("Cancellation during initialization requested for job {}. Job will be cancelled once JobManager has been initialized.", jobId);
+ log.info("Cancellation during initialization for job {}.", jobId);
Review comment:
Is the fact that it is still initializing relevant enough for users to mention it in an info message?
##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/InitializingJobManagerRunner.java
##########
@@ -0,0 +1,105 @@
+/*
+ * 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.api.common.JobID;
+import org.apache.flink.runtime.client.JobInitializationException;
+import org.apache.flink.runtime.concurrent.FutureUtils;
+import org.apache.flink.runtime.jobmaster.JobManagerRunner;
+import org.apache.flink.runtime.messages.Acknowledge;
+import org.apache.flink.runtime.rpc.RpcUtils;
+import org.apache.flink.util.function.SupplierWithException;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.concurrent.GuardedBy;
+
+import java.util.concurrent.CompletableFuture;
+
+/**
+ * Wrapper for an initializing JobManagerRunner.
+ */
+public class InitializingJobManagerRunner {
+ private final Logger log = LoggerFactory.getLogger(InitializingJobManagerRunner.class);
+
+ private final CompletableFuture<JobManagerRunner> jobManagerRunnerFuture = new CompletableFuture<>();
+
+ private final Thread initializationRunner;
+
+ private final Object lock = new Object();
+
+ @GuardedBy("lock")
+ private CompletableFuture<Acknowledge> cancellationFuture;
+
+ public InitializingJobManagerRunner(SupplierWithException<JobManagerRunner, JobInitializationException> initializationRunnable, JobID jobID) {
+ String threadName = "jobmanager-initialization-" + jobID;
Review comment:
```suggestion
String threadName = "flink-jobmanager-initialization-" + jobID;
```
It would be good if all threads that we create have a "flink" prefix, just so we can see at a glance where it comes from.
##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/InitializingJobManagerRunner.java
##########
@@ -0,0 +1,105 @@
+/*
+ * 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.api.common.JobID;
+import org.apache.flink.runtime.client.JobInitializationException;
+import org.apache.flink.runtime.concurrent.FutureUtils;
+import org.apache.flink.runtime.jobmaster.JobManagerRunner;
+import org.apache.flink.runtime.messages.Acknowledge;
+import org.apache.flink.runtime.rpc.RpcUtils;
+import org.apache.flink.util.function.SupplierWithException;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.concurrent.GuardedBy;
+
+import java.util.concurrent.CompletableFuture;
+
+/**
+ * Wrapper for an initializing JobManagerRunner.
+ */
+public class InitializingJobManagerRunner {
+ private final Logger log = LoggerFactory.getLogger(InitializingJobManagerRunner.class);
+
+ private final CompletableFuture<JobManagerRunner> jobManagerRunnerFuture = new CompletableFuture<>();
+
+ private final Thread initializationRunner;
+
+ private final Object lock = new Object();
+
+ @GuardedBy("lock")
+ private CompletableFuture<Acknowledge> cancellationFuture;
+
+ public InitializingJobManagerRunner(SupplierWithException<JobManagerRunner, JobInitializationException> initializationRunnable, JobID jobID) {
+ String threadName = "jobmanager-initialization-" + jobID;
+ this.initializationRunner = new Thread(() -> {
+ log.debug("Starting JobManager initialization thread: {}", threadName);
+ try {
+ runInitialization(initializationRunnable);
+ } catch (Throwable threadError) {
+ log.warn("Unexpected error in thread '{}'", threadName, threadError);
+ } finally {
+ log.debug("Stopping JobManager initialization thread: {}", threadName);
+ }
+ }, threadName);
+ this.initializationRunner.setDaemon(true); // this should not block the JVM from shutting down
+ this.initializationRunner.start();
+ }
+
+ private void runInitialization(SupplierWithException<JobManagerRunner, JobInitializationException> initializationRunnable) {
+ try {
+ JobManagerRunner runner = initializationRunnable.get();
+ synchronized (lock) {
+ if (cancellationFuture == null) {
+ jobManagerRunnerFuture.complete(runner);
+ } else {
+ // jobMaster has finished despite the interruption. Cancel job.
+ FutureUtils.forward(runner.getJobMasterGateway().thenCompose(gateway -> gateway.cancel(RpcUtils.INF_TIMEOUT)),
+ cancellationFuture);
+ jobManagerRunnerFuture.complete(runner);
+ }
+ }
+ } catch (JobInitializationException throwable) {
+ synchronized (lock) {
+ if (cancellationFuture == null) {
+ jobManagerRunnerFuture.completeExceptionally(throwable);
+ } else {
+ // an exception related to an interruption is expected here.
+ log.debug("Cancellation finished with (expected) exception", throwable);
+ jobManagerRunnerFuture.completeExceptionally(new JobInitializationCancelledException("Job initialization has been cancelled", throwable));
+ cancellationFuture.complete(Acknowledge.get());
+ }
+ }
+ }
+ }
+
+ public CompletableFuture<Acknowledge> cancelInitialization() {
+ synchronized (lock) {
+ initializationRunner.interrupt();
Review comment:
So...this only has any effect if the initializing thread is stuck in a blocking operation. What cases are we targeting here?
##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/InitializingJobManagerRunner.java
##########
@@ -0,0 +1,105 @@
+/*
+ * 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.api.common.JobID;
+import org.apache.flink.runtime.client.JobInitializationException;
+import org.apache.flink.runtime.concurrent.FutureUtils;
+import org.apache.flink.runtime.jobmaster.JobManagerRunner;
+import org.apache.flink.runtime.messages.Acknowledge;
+import org.apache.flink.runtime.rpc.RpcUtils;
+import org.apache.flink.util.function.SupplierWithException;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.concurrent.GuardedBy;
+
+import java.util.concurrent.CompletableFuture;
+
+/**
+ * Wrapper for an initializing JobManagerRunner.
+ */
+public class InitializingJobManagerRunner {
+ private final Logger log = LoggerFactory.getLogger(InitializingJobManagerRunner.class);
+
+ private final CompletableFuture<JobManagerRunner> jobManagerRunnerFuture = new CompletableFuture<>();
+
+ private final Thread initializationRunner;
+
+ private final Object lock = new Object();
+
+ @GuardedBy("lock")
+ private CompletableFuture<Acknowledge> cancellationFuture;
+
+ public InitializingJobManagerRunner(SupplierWithException<JobManagerRunner, JobInitializationException> initializationRunnable, JobID jobID) {
+ String threadName = "jobmanager-initialization-" + jobID;
+ this.initializationRunner = new Thread(() -> {
Review comment:
Why isn't this using a thread pool?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org