You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by pn...@apache.org on 2021/10/08 08:01:20 UTC
[flink] 06/10: [FLINK-24182][task] Extend test coverage of
interruptions in SourceStreamTask
This is an automated email from the ASF dual-hosted git repository.
pnowojski pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git
commit 281de7277468bf546592710fe8ef206d871d00b6
Author: Piotr Nowojski <pi...@gmail.com>
AuthorDate: Mon Sep 13 16:39:31 2021 +0200
[FLINK-24182][task] Extend test coverage of interruptions in SourceStreamTask
If we have a source that takes long time to cancel, SourceStreamTask should
always wait for the source thread to finish. Regardless if SourceStreamTask is
cleanly cancelled or interrupted.
This was working properly on the master before this commit. Here we are just
adding test coverage for that.
---
.../flink/streaming/runtime/tasks/StreamTask.java | 6 ++--
.../runtime/tasks/SourceStreamTaskTest.java | 11 ++++++-
.../runtime/tasks/StreamTaskTestHarness.java | 36 ++++++++++++++++------
3 files changed, 41 insertions(+), 12 deletions(-)
diff --git a/flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/tasks/StreamTask.java b/flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/tasks/StreamTask.java
index 093a03a..5b8bc95 100644
--- a/flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/tasks/StreamTask.java
+++ b/flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/tasks/StreamTask.java
@@ -900,12 +900,14 @@ public abstract class StreamTask<OUT, OP extends StreamOperator<OUT>>
Exception suppressedException =
throwable == null ? null : runAndSuppressThrowable(this::cancelTask, null);
+ disableInterruptOnCancel();
+
+ // note: This `.join()` is already uninterruptible, so it doesn't matter if we have already
+ // disabled the interruptions or not.
getCompletionFuture().exceptionally(unused -> null).join();
// clean up everything we initialized
isRunning = false;
- disableInterruptOnCancel();
-
// clear any previously issued interrupt for a more graceful shutdown
Thread.interrupted();
diff --git a/flink-streaming-java/src/test/java/org/apache/flink/streaming/runtime/tasks/SourceStreamTaskTest.java b/flink-streaming-java/src/test/java/org/apache/flink/streaming/runtime/tasks/SourceStreamTaskTest.java
index 2057c15..5fbc65c 100644
--- a/flink-streaming-java/src/test/java/org/apache/flink/streaming/runtime/tasks/SourceStreamTaskTest.java
+++ b/flink-streaming-java/src/test/java/org/apache/flink/streaming/runtime/tasks/SourceStreamTaskTest.java
@@ -560,10 +560,19 @@ public class SourceStreamTaskTest extends SourceStreamTaskTestBase {
harness.invoke();
NonStoppingSource.waitForStart();
+ // SourceStreamTask should be still waiting for NonStoppingSource after cancellation
harness.getTask().cancel();
- harness.waitForTaskCompletion(500, true); // allow task to exit prematurely
+ harness.waitForTaskCompletion(50, true); // allow task to exit prematurely
assertTrue(harness.taskThread.isAlive());
+ // SourceStreamTask should be still waiting for NonStoppingSource after interruptions
+ for (int i = 0; i < 10; i++) {
+ harness.getTask().maybeInterruptOnCancel(harness.getTaskThread(), null, null);
+ harness.waitForTaskCompletion(50, true); // allow task to exit prematurely
+ assertTrue(harness.taskThread.isAlive());
+ }
+
+ // It should only exit once NonStoppingSource allows for it
NonStoppingSource.forceCancel();
harness.waitForTaskCompletion(Long.MAX_VALUE, true);
}
diff --git a/flink-streaming-java/src/test/java/org/apache/flink/streaming/runtime/tasks/StreamTaskTestHarness.java b/flink-streaming-java/src/test/java/org/apache/flink/streaming/runtime/tasks/StreamTaskTestHarness.java
index 45b841c8..0b0390f 100644
--- a/flink-streaming-java/src/test/java/org/apache/flink/streaming/runtime/tasks/StreamTaskTestHarness.java
+++ b/flink-streaming-java/src/test/java/org/apache/flink/streaming/runtime/tasks/StreamTaskTestHarness.java
@@ -324,18 +324,22 @@ public class StreamTaskTestHarness<OUT> {
*
* @param timeout Timeout for the task completion
*/
- public void waitForTaskCompletion(long timeout, boolean ignoreCancellationException)
- throws Exception {
+ public void waitForTaskCompletion(
+ long timeout, boolean ignoreCancellationOrInterruptedException) throws Exception {
Preconditions.checkState(taskThread != null, "Task thread was not started.");
taskThread.join(timeout);
if (taskThread.getError() != null) {
- if (!ignoreCancellationException
- || !ExceptionUtils.findThrowable(
- taskThread.getError(), CancelTaskException.class)
- .isPresent()) {
- throw new Exception("error in task", taskThread.getError());
+ boolean errorIsCancellationOrInterrupted =
+ ExceptionUtils.findThrowable(taskThread.getError(), CancelTaskException.class)
+ .isPresent()
+ || ExceptionUtils.findThrowable(
+ taskThread.getError(), InterruptedException.class)
+ .isPresent();
+ if (ignoreCancellationOrInterruptedException && errorIsCancellationOrInterrupted) {
+ return;
}
+ throw new Exception("error in task", taskThread.getError());
}
}
@@ -360,6 +364,10 @@ public class StreamTaskTestHarness<OUT> {
return taskThread.task;
}
+ public Thread getTaskThread() {
+ return taskThread;
+ }
+
/**
* Get all the output from the task. This contains StreamRecords and Events interleaved. Use
* {@link
@@ -524,8 +532,18 @@ public class StreamTaskTestHarness<OUT> {
task.invoke();
shutdownIOManager();
shutdownMemoryManager();
- } catch (Throwable t) {
- this.error = t;
+ } catch (Throwable throwable) {
+ this.error = throwable;
+ } finally {
+ try {
+ task.cleanUp(this.error);
+ } catch (Exception cleanUpException) {
+ if (this.error == null) {
+ this.error = cleanUpException;
+ } else {
+ this.error.addSuppressed(cleanUpException);
+ }
+ }
}
}