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/09/06 07:54:13 UTC
[flink] 03/04: [refactor][streaming] Busy waiting in
checkScheduledTimestamps replaced by CompletableFuture
This is an automated email from the ASF dual-hosted git repository.
pnowojski pushed a commit to branch release-1.14
in repository https://gitbox.apache.org/repos/asf/flink.git
commit 08481d95c0cd014ee48746352fad19d29e2eecce
Author: Anton Kalashnikov <ka...@yandex.ru>
AuthorDate: Wed Sep 1 15:32:35 2021 +0200
[refactor][streaming] Busy waiting in checkScheduledTimestamps replaced by CompletableFuture
---
.../runtime/operators/StreamTaskTimerTest.java | 55 ++++++++++------------
1 file changed, 24 insertions(+), 31 deletions(-)
diff --git a/flink-streaming-java/src/test/java/org/apache/flink/streaming/runtime/operators/StreamTaskTimerTest.java b/flink-streaming-java/src/test/java/org/apache/flink/streaming/runtime/operators/StreamTaskTimerTest.java
index cab02b2..765075d 100644
--- a/flink-streaming-java/src/test/java/org/apache/flink/streaming/runtime/operators/StreamTaskTimerTest.java
+++ b/flink-streaming-java/src/test/java/org/apache/flink/streaming/runtime/operators/StreamTaskTimerTest.java
@@ -38,15 +38,17 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;
-import javax.annotation.Nullable;
-
import java.time.Duration;
+import java.util.ArrayList;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicReference;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
-import static org.junit.Assert.fail;
/** Tests for the timer service of {@link org.apache.flink.streaming.runtime.tasks.StreamTask}. */
@SuppressWarnings("serial")
@@ -107,26 +109,22 @@ public class StreamTaskTimerTest extends TestLogger {
@Test
public void checkScheduledTimestamps() throws Exception {
- final AtomicReference<Throwable> errorRef = new AtomicReference<>();
-
- final long t1 = System.currentTimeMillis();
- final long t2 = System.currentTimeMillis() - 200;
- final long t3 = System.currentTimeMillis() + 100;
- final long t4 = System.currentTimeMillis() + 200;
+ ValidatingProcessingTimeCallback.numInSequence = 0;
+ long currentTimeMillis = System.currentTimeMillis();
+ ArrayList<ValidatingProcessingTimeCallback> timeCallbacks = new ArrayList<>();
- timeService.registerTimer(t1, new ValidatingProcessingTimeCallback(errorRef, t1, 0));
- timeService.registerTimer(t2, new ValidatingProcessingTimeCallback(errorRef, t2, 1));
- timeService.registerTimer(t3, new ValidatingProcessingTimeCallback(errorRef, t3, 2));
- timeService.registerTimer(t4, new ValidatingProcessingTimeCallback(errorRef, t4, 3));
+ timeCallbacks.add(new ValidatingProcessingTimeCallback(currentTimeMillis, 0));
+ timeCallbacks.add(new ValidatingProcessingTimeCallback(currentTimeMillis - 200, 1));
+ timeCallbacks.add(new ValidatingProcessingTimeCallback(currentTimeMillis + 100, 2));
+ timeCallbacks.add(new ValidatingProcessingTimeCallback(currentTimeMillis + 200, 3));
- long deadline = System.currentTimeMillis() + 20000;
- while (errorRef.get() == null
- && ValidatingProcessingTimeCallback.numInSequence < 4
- && System.currentTimeMillis() < deadline) {
- Thread.sleep(100);
+ for (ValidatingProcessingTimeCallback timeCallback : timeCallbacks) {
+ timeService.registerTimer(timeCallback.expectedTimestamp, timeCallback);
}
- verifyNoException(errorRef.get());
+ for (ValidatingProcessingTimeCallback timeCallback : timeCallbacks) {
+ timeCallback.assertExpectedValues();
+ }
assertEquals(4, ValidatingProcessingTimeCallback.numInSequence);
}
@@ -134,16 +132,12 @@ public class StreamTaskTimerTest extends TestLogger {
static int numInSequence;
- private final AtomicReference<Throwable> errorRef;
+ private final CompletableFuture<Void> finished = new CompletableFuture<>();
private final long expectedTimestamp;
private final int expectedInSequence;
- private ValidatingProcessingTimeCallback(
- AtomicReference<Throwable> errorRef,
- long expectedTimestamp,
- int expectedInSequence) {
- this.errorRef = errorRef;
+ private ValidatingProcessingTimeCallback(long expectedTimestamp, int expectedInSequence) {
this.expectedTimestamp = expectedTimestamp;
this.expectedInSequence = expectedInSequence;
}
@@ -154,16 +148,15 @@ public class StreamTaskTimerTest extends TestLogger {
assertEquals(expectedTimestamp, timestamp);
assertEquals(expectedInSequence, numInSequence);
numInSequence++;
+ finished.complete(null);
} catch (Throwable t) {
- errorRef.compareAndSet(null, t);
+ finished.completeExceptionally(t);
}
}
- }
- private static void verifyNoException(@Nullable Throwable exception) {
- if (exception != null) {
- exception.printStackTrace();
- fail(exception.getMessage());
+ private void assertExpectedValues()
+ throws ExecutionException, InterruptedException, TimeoutException {
+ finished.get(20, TimeUnit.SECONDS);
}
}