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 2019/09/03 05:01:08 UTC
[flink] branch release-1.9 updated (1f0c670 -> d13e537)
This is an automated email from the ASF dual-hosted git repository.
trohrmann pushed a change to branch release-1.9
in repository https://gitbox.apache.org/repos/asf/flink.git.
from 1f0c670 [FLINK-13568][sql-parser] Fix DDL CREATE TABLE statement doesn't allow STRING data type
new 66bfe3f [Flink-12164][runtime] Introduce factory pattern for HeartbeatMonitor
new 96800c9 [Flink-12164][tests] Enrich TestingHeartbeatServices by supporting manually triggering timeout
new e0a95d4 [Flink-12164][tests] Harden JobMasterTest#testJobFailureWhenTaskExecutorHeartbeatTimeout
new d13e537 [hotfix][tests] Fix code style problem of JobMasterTest
The 4 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../runtime/heartbeat/HeartbeatManagerImpl.java | 165 +++++----------------
.../heartbeat/HeartbeatManagerSenderImpl.java | 23 ++-
.../flink/runtime/heartbeat/HeartbeatMonitor.java | 87 +++++++++++
.../runtime/heartbeat/HeartbeatMonitorImpl.java | 160 ++++++++++++++++++++
.../heartbeat/TestingHeartbeatServices.java | 165 ++++++++++++++++++++-
.../flink/runtime/jobmaster/JobMasterTest.java | 22 +--
6 files changed, 478 insertions(+), 144 deletions(-)
create mode 100644 flink-runtime/src/main/java/org/apache/flink/runtime/heartbeat/HeartbeatMonitor.java
create mode 100644 flink-runtime/src/main/java/org/apache/flink/runtime/heartbeat/HeartbeatMonitorImpl.java
[flink] 01/04: [Flink-12164][runtime] Introduce factory pattern for
HeartbeatMonitor
Posted by tr...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
trohrmann pushed a commit to branch release-1.9
in repository https://gitbox.apache.org/repos/asf/flink.git
commit 66bfe3fbb1ee8233cea8c9bbadc22e36bb3bed9b
Author: ifndef-SleePy <mm...@gmail.com>
AuthorDate: Tue Aug 6 17:53:56 2019 +0800
[Flink-12164][runtime] Introduce factory pattern for HeartbeatMonitor
---
.../runtime/heartbeat/HeartbeatManagerImpl.java | 159 ++++----------------
.../flink/runtime/heartbeat/HeartbeatMonitor.java | 87 +++++++++++
.../runtime/heartbeat/HeartbeatMonitorImpl.java | 160 +++++++++++++++++++++
3 files changed, 278 insertions(+), 128 deletions(-)
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/heartbeat/HeartbeatManagerImpl.java b/flink-runtime/src/main/java/org/apache/flink/runtime/heartbeat/HeartbeatManagerImpl.java
index fdb78a2..a0d7f7a 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/heartbeat/HeartbeatManagerImpl.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/heartbeat/HeartbeatManagerImpl.java
@@ -28,9 +28,6 @@ import javax.annotation.concurrent.ThreadSafe;
import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ScheduledFuture;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicReference;
/**
* Heartbeat manager implementation. The heartbeat manager maintains a map of heartbeat monitors
@@ -60,7 +57,9 @@ public class HeartbeatManagerImpl<I, O> implements HeartbeatManager<I, O> {
protected final Logger log;
/** Map containing the heartbeat monitors associated with the respective resource ID. */
- private final ConcurrentHashMap<ResourceID, HeartbeatManagerImpl.HeartbeatMonitor<O>> heartbeatTargets;
+ private final ConcurrentHashMap<ResourceID, HeartbeatMonitor<O>> heartbeatTargets;
+
+ private final HeartbeatMonitor.Factory<O> heartbeatMonitorFactory;
/** Running state of the heartbeat manager. */
protected volatile boolean stopped;
@@ -71,6 +70,23 @@ public class HeartbeatManagerImpl<I, O> implements HeartbeatManager<I, O> {
HeartbeatListener<I, O> heartbeatListener,
ScheduledExecutor mainThreadExecutor,
Logger log) {
+ this(
+ heartbeatTimeoutIntervalMs,
+ ownResourceID,
+ heartbeatListener,
+ mainThreadExecutor,
+ log,
+ new HeartbeatMonitorImpl.Factory<>());
+ }
+
+ public HeartbeatManagerImpl(
+ long heartbeatTimeoutIntervalMs,
+ ResourceID ownResourceID,
+ HeartbeatListener<I, O> heartbeatListener,
+ ScheduledExecutor mainThreadExecutor,
+ Logger log,
+ HeartbeatMonitor.Factory<O> heartbeatMonitorFactory) {
+
Preconditions.checkArgument(heartbeatTimeoutIntervalMs > 0L, "The heartbeat timeout has to be larger than 0.");
this.heartbeatTimeoutIntervalMs = heartbeatTimeoutIntervalMs;
@@ -78,6 +94,7 @@ public class HeartbeatManagerImpl<I, O> implements HeartbeatManager<I, O> {
this.heartbeatListener = Preconditions.checkNotNull(heartbeatListener, "heartbeatListener");
this.mainThreadExecutor = Preconditions.checkNotNull(mainThreadExecutor);
this.log = Preconditions.checkNotNull(log);
+ this.heartbeatMonitorFactory = heartbeatMonitorFactory;
this.heartbeatTargets = new ConcurrentHashMap<>(16);
stopped = false;
@@ -109,12 +126,13 @@ public class HeartbeatManagerImpl<I, O> implements HeartbeatManager<I, O> {
if (heartbeatTargets.containsKey(resourceID)) {
log.debug("The target with resource ID {} is already been monitored.", resourceID);
} else {
- HeartbeatManagerImpl.HeartbeatMonitor<O> heartbeatMonitor = new HeartbeatManagerImpl.HeartbeatMonitor<>(
- resourceID,
- heartbeatTarget,
- mainThreadExecutor,
- heartbeatListener,
- heartbeatTimeoutIntervalMs);
+ HeartbeatMonitor<O> heartbeatMonitor =
+ heartbeatMonitorFactory.createHeartbeatMonitor(
+ resourceID,
+ heartbeatTarget,
+ mainThreadExecutor,
+ heartbeatListener,
+ heartbeatTimeoutIntervalMs);
heartbeatTargets.put(
resourceID,
@@ -133,7 +151,7 @@ public class HeartbeatManagerImpl<I, O> implements HeartbeatManager<I, O> {
@Override
public void unmonitorTarget(ResourceID resourceID) {
if (!stopped) {
- HeartbeatManagerImpl.HeartbeatMonitor<O> heartbeatMonitor = heartbeatTargets.remove(resourceID);
+ HeartbeatMonitor<O> heartbeatMonitor = heartbeatTargets.remove(resourceID);
if (heartbeatMonitor != null) {
heartbeatMonitor.cancel();
@@ -145,7 +163,7 @@ public class HeartbeatManagerImpl<I, O> implements HeartbeatManager<I, O> {
public void stop() {
stopped = true;
- for (HeartbeatManagerImpl.HeartbeatMonitor<O> heartbeatMonitor : heartbeatTargets.values()) {
+ for (HeartbeatMonitor<O> heartbeatMonitor : heartbeatTargets.values()) {
heartbeatMonitor.cancel();
}
@@ -202,7 +220,7 @@ public class HeartbeatManagerImpl<I, O> implements HeartbeatManager<I, O> {
HeartbeatTarget<O> reportHeartbeat(ResourceID resourceID) {
if (heartbeatTargets.containsKey(resourceID)) {
- HeartbeatManagerImpl.HeartbeatMonitor<O> heartbeatMonitor = heartbeatTargets.get(resourceID);
+ HeartbeatMonitor<O> heartbeatMonitor = heartbeatTargets.get(resourceID);
heartbeatMonitor.reportHeartbeat();
return heartbeatMonitor.getHeartbeatTarget();
@@ -210,119 +228,4 @@ public class HeartbeatManagerImpl<I, O> implements HeartbeatManager<I, O> {
return null;
}
}
-
- //----------------------------------------------------------------------------------------------
- // Utility classes
- //----------------------------------------------------------------------------------------------
-
- /**
- * Heartbeat monitor which manages the heartbeat state of the associated heartbeat target. The
- * monitor notifies the {@link HeartbeatListener} whenever it has not seen a heartbeat signal
- * in the specified heartbeat timeout interval. Each heartbeat signal resets this timer.
- *
- * @param <O> Type of the payload being sent to the associated heartbeat target
- */
- static class HeartbeatMonitor<O> implements Runnable {
-
- /** Resource ID of the monitored heartbeat target. */
- private final ResourceID resourceID;
-
- /** Associated heartbeat target. */
- private final HeartbeatTarget<O> heartbeatTarget;
-
- private final ScheduledExecutor scheduledExecutor;
-
- /** Listener which is notified about heartbeat timeouts. */
- private final HeartbeatListener<?, ?> heartbeatListener;
-
- /** Maximum heartbeat timeout interval. */
- private final long heartbeatTimeoutIntervalMs;
-
- private volatile ScheduledFuture<?> futureTimeout;
-
- private final AtomicReference<State> state = new AtomicReference<>(State.RUNNING);
-
- private volatile long lastHeartbeat;
-
- HeartbeatMonitor(
- ResourceID resourceID,
- HeartbeatTarget<O> heartbeatTarget,
- ScheduledExecutor scheduledExecutor,
- HeartbeatListener<?, O> heartbeatListener,
- long heartbeatTimeoutIntervalMs) {
-
- this.resourceID = Preconditions.checkNotNull(resourceID);
- this.heartbeatTarget = Preconditions.checkNotNull(heartbeatTarget);
- this.scheduledExecutor = Preconditions.checkNotNull(scheduledExecutor);
- this.heartbeatListener = Preconditions.checkNotNull(heartbeatListener);
-
- Preconditions.checkArgument(heartbeatTimeoutIntervalMs > 0L, "The heartbeat timeout interval has to be larger than 0.");
- this.heartbeatTimeoutIntervalMs = heartbeatTimeoutIntervalMs;
-
- lastHeartbeat = 0L;
-
- resetHeartbeatTimeout(heartbeatTimeoutIntervalMs);
- }
-
- HeartbeatTarget<O> getHeartbeatTarget() {
- return heartbeatTarget;
- }
-
- ResourceID getHeartbeatTargetId() {
- return resourceID;
- }
-
- public long getLastHeartbeat() {
- return lastHeartbeat;
- }
-
- void reportHeartbeat() {
- lastHeartbeat = System.currentTimeMillis();
- resetHeartbeatTimeout(heartbeatTimeoutIntervalMs);
- }
-
- void resetHeartbeatTimeout(long heartbeatTimeout) {
- if (state.get() == State.RUNNING) {
- cancelTimeout();
-
- futureTimeout = scheduledExecutor.schedule(this, heartbeatTimeout, TimeUnit.MILLISECONDS);
-
- // Double check for concurrent accesses (e.g. a firing of the scheduled future)
- if (state.get() != State.RUNNING) {
- cancelTimeout();
- }
- }
- }
-
- void cancel() {
- // we can only cancel if we are in state running
- if (state.compareAndSet(State.RUNNING, State.CANCELED)) {
- cancelTimeout();
- }
- }
-
- private void cancelTimeout() {
- if (futureTimeout != null) {
- futureTimeout.cancel(true);
- }
- }
-
- public boolean isCanceled() {
- return state.get() == State.CANCELED;
- }
-
- @Override
- public void run() {
- // The heartbeat has timed out if we're in state running
- if (state.compareAndSet(State.RUNNING, State.TIMEOUT)) {
- heartbeatListener.notifyHeartbeatTimeout(resourceID);
- }
- }
-
- private enum State {
- RUNNING,
- TIMEOUT,
- CANCELED
- }
- }
}
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/heartbeat/HeartbeatMonitor.java b/flink-runtime/src/main/java/org/apache/flink/runtime/heartbeat/HeartbeatMonitor.java
new file mode 100644
index 0000000..4441b02
--- /dev/null
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/heartbeat/HeartbeatMonitor.java
@@ -0,0 +1,87 @@
+/*
+ * 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.heartbeat;
+
+import org.apache.flink.runtime.clusterframework.types.ResourceID;
+import org.apache.flink.runtime.concurrent.ScheduledExecutor;
+
+/**
+ * Heartbeat monitor which manages the heartbeat state of the associated heartbeat target. The
+ * monitor notifies the {@link HeartbeatListener} whenever it has not seen a heartbeat signal
+ * in the specified heartbeat timeout interval. Each heartbeat signal resets this timer.
+ *
+ * @param <O> Type of the payload being sent to the associated heartbeat target
+ */
+public interface HeartbeatMonitor<O> {
+
+ /**
+ * Gets heartbeat target.
+ *
+ * @return the heartbeat target
+ */
+ HeartbeatTarget<O> getHeartbeatTarget();
+
+ /**
+ * Gets heartbeat target id.
+ *
+ * @return the heartbeat target id
+ */
+ ResourceID getHeartbeatTargetId();
+
+ /**
+ * Report heartbeat from the monitored target.
+ */
+ void reportHeartbeat();
+
+ /**
+ * Cancel this monitor.
+ */
+ void cancel();
+
+ /**
+ * Gets the last heartbeat.
+ *
+ * @return the last heartbeat
+ */
+ long getLastHeartbeat();
+
+ /**
+ * This factory provides an indirection way to create {@link HeartbeatMonitor}.
+ *
+ * @param <O> Type of the outgoing heartbeat payload
+ */
+ interface Factory<O> {
+ /**
+ * Create heartbeat monitor heartbeat monitor.
+ *
+ * @param resourceID the resource id
+ * @param heartbeatTarget the heartbeat target
+ * @param mainThreadExecutor the main thread executor
+ * @param heartbeatListener the heartbeat listener
+ * @param heartbeatTimeoutIntervalMs the heartbeat timeout interval ms
+ * @return the heartbeat monitor
+ */
+ HeartbeatMonitor<O> createHeartbeatMonitor(
+ ResourceID resourceID,
+ HeartbeatTarget<O> heartbeatTarget,
+ ScheduledExecutor mainThreadExecutor,
+ HeartbeatListener<?, O> heartbeatListener,
+ long heartbeatTimeoutIntervalMs);
+ }
+}
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/heartbeat/HeartbeatMonitorImpl.java b/flink-runtime/src/main/java/org/apache/flink/runtime/heartbeat/HeartbeatMonitorImpl.java
new file mode 100644
index 0000000..f9270e6
--- /dev/null
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/heartbeat/HeartbeatMonitorImpl.java
@@ -0,0 +1,160 @@
+/*
+ * 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.heartbeat;
+
+import org.apache.flink.runtime.clusterframework.types.ResourceID;
+import org.apache.flink.runtime.concurrent.ScheduledExecutor;
+import org.apache.flink.util.Preconditions;
+
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
+
+/**
+ * The default implementation of {@link HeartbeatMonitor}.
+ *
+ * @param <O> Type of the payload being sent to the associated heartbeat target
+ */
+public class HeartbeatMonitorImpl<O> implements HeartbeatMonitor<O>, Runnable {
+
+ /** Resource ID of the monitored heartbeat target. */
+ private final ResourceID resourceID;
+
+ /** Associated heartbeat target. */
+ private final HeartbeatTarget<O> heartbeatTarget;
+
+ private final ScheduledExecutor scheduledExecutor;
+
+ /** Listener which is notified about heartbeat timeouts. */
+ private final HeartbeatListener<?, ?> heartbeatListener;
+
+ /** Maximum heartbeat timeout interval. */
+ private final long heartbeatTimeoutIntervalMs;
+
+ private volatile ScheduledFuture<?> futureTimeout;
+
+ private final AtomicReference<State> state = new AtomicReference<>(State.RUNNING);
+
+ private volatile long lastHeartbeat;
+
+ HeartbeatMonitorImpl(
+ ResourceID resourceID,
+ HeartbeatTarget<O> heartbeatTarget,
+ ScheduledExecutor scheduledExecutor,
+ HeartbeatListener<?, O> heartbeatListener,
+ long heartbeatTimeoutIntervalMs) {
+
+ this.resourceID = Preconditions.checkNotNull(resourceID);
+ this.heartbeatTarget = Preconditions.checkNotNull(heartbeatTarget);
+ this.scheduledExecutor = Preconditions.checkNotNull(scheduledExecutor);
+ this.heartbeatListener = Preconditions.checkNotNull(heartbeatListener);
+
+ Preconditions.checkArgument(heartbeatTimeoutIntervalMs > 0L, "The heartbeat timeout interval has to be larger than 0.");
+ this.heartbeatTimeoutIntervalMs = heartbeatTimeoutIntervalMs;
+
+ lastHeartbeat = 0L;
+
+ resetHeartbeatTimeout(heartbeatTimeoutIntervalMs);
+ }
+
+ @Override
+ public HeartbeatTarget<O> getHeartbeatTarget() {
+ return heartbeatTarget;
+ }
+
+ @Override
+ public ResourceID getHeartbeatTargetId() {
+ return resourceID;
+ }
+
+ @Override
+ public long getLastHeartbeat() {
+ return lastHeartbeat;
+ }
+
+ @Override
+ public void reportHeartbeat() {
+ lastHeartbeat = System.currentTimeMillis();
+ resetHeartbeatTimeout(heartbeatTimeoutIntervalMs);
+ }
+
+ @Override
+ public void cancel() {
+ // we can only cancel if we are in state running
+ if (state.compareAndSet(State.RUNNING, State.CANCELED)) {
+ cancelTimeout();
+ }
+ }
+
+ @Override
+ public void run() {
+ // The heartbeat has timed out if we're in state running
+ if (state.compareAndSet(State.RUNNING, State.TIMEOUT)) {
+ heartbeatListener.notifyHeartbeatTimeout(resourceID);
+ }
+ }
+
+ public boolean isCanceled() {
+ return state.get() == State.CANCELED;
+ }
+
+ void resetHeartbeatTimeout(long heartbeatTimeout) {
+ if (state.get() == State.RUNNING) {
+ cancelTimeout();
+
+ futureTimeout = scheduledExecutor.schedule(this, heartbeatTimeout, TimeUnit.MILLISECONDS);
+
+ // Double check for concurrent accesses (e.g. a firing of the scheduled future)
+ if (state.get() != State.RUNNING) {
+ cancelTimeout();
+ }
+ }
+ }
+
+ private void cancelTimeout() {
+ if (futureTimeout != null) {
+ futureTimeout.cancel(true);
+ }
+ }
+
+ private enum State {
+ RUNNING,
+ TIMEOUT,
+ CANCELED
+ }
+
+ /**
+ * The factory that instantiates {@link HeartbeatMonitorImpl}.
+ *
+ * @param <O> Type of the outgoing heartbeat payload
+ */
+ static class Factory<O> implements HeartbeatMonitor.Factory<O> {
+
+ @Override
+ public HeartbeatMonitor<O> createHeartbeatMonitor(
+ ResourceID resourceID,
+ HeartbeatTarget<O> heartbeatTarget,
+ ScheduledExecutor mainThreadExecutor,
+ HeartbeatListener<?, O> heartbeatListener,
+ long heartbeatTimeoutIntervalMs) {
+
+ return new HeartbeatMonitorImpl<>(resourceID, heartbeatTarget, mainThreadExecutor, heartbeatListener, heartbeatTimeoutIntervalMs);
+ }
+ }
+}
[flink] 04/04: [hotfix][tests] Fix code style problem of
JobMasterTest
Posted by tr...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
trohrmann pushed a commit to branch release-1.9
in repository https://gitbox.apache.org/repos/asf/flink.git
commit d13e5377947d0594523fbd6950b0b1d9789d431c
Author: ifndef-SleePy <mm...@gmail.com>
AuthorDate: Fri Aug 30 20:02:23 2019 +0800
[hotfix][tests] Fix code style problem of JobMasterTest
---
.../test/java/org/apache/flink/runtime/jobmaster/JobMasterTest.java | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)
diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/jobmaster/JobMasterTest.java b/flink-runtime/src/test/java/org/apache/flink/runtime/jobmaster/JobMasterTest.java
index 3e308d0..e3475e7 100644
--- a/flink-runtime/src/test/java/org/apache/flink/runtime/jobmaster/JobMasterTest.java
+++ b/flink-runtime/src/test/java/org/apache/flink/runtime/jobmaster/JobMasterTest.java
@@ -1779,7 +1779,7 @@ public class JobMasterTest extends TestLogger {
}
/**
- * Tests the updateGlobalAggregate functionality
+ * Tests the updateGlobalAggregate functionality.
*/
@Test
public void testJobMasterAggregatesValuesCorrectly() throws Exception {
@@ -1837,9 +1837,7 @@ public class JobMasterTest extends TestLogger {
@Override
public Integer add(Integer value, Integer accumulator) {
- Integer _acc = (Integer) accumulator;
- Integer _value = (Integer) value;
- return _acc + _value;
+ return accumulator + value;
}
@Override
[flink] 02/04: [Flink-12164][tests] Enrich TestingHeartbeatServices
by supporting manually triggering timeout
Posted by tr...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
trohrmann pushed a commit to branch release-1.9
in repository https://gitbox.apache.org/repos/asf/flink.git
commit 96800c94a52c58a86d000d9846c2ff8956732c76
Author: ifndef-SleePy <mm...@gmail.com>
AuthorDate: Tue Aug 6 20:58:54 2019 +0800
[Flink-12164][tests] Enrich TestingHeartbeatServices by supporting manually triggering timeout
---
.../runtime/heartbeat/HeartbeatManagerImpl.java | 6 +-
.../heartbeat/HeartbeatManagerSenderImpl.java | 23 ++-
.../heartbeat/TestingHeartbeatServices.java | 165 ++++++++++++++++++++-
3 files changed, 188 insertions(+), 6 deletions(-)
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/heartbeat/HeartbeatManagerImpl.java b/flink-runtime/src/main/java/org/apache/flink/runtime/heartbeat/HeartbeatManagerImpl.java
index a0d7f7a..67f2cbe 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/heartbeat/HeartbeatManagerImpl.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/heartbeat/HeartbeatManagerImpl.java
@@ -26,7 +26,7 @@ import org.slf4j.Logger;
import javax.annotation.concurrent.ThreadSafe;
-import java.util.Collection;
+import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
@@ -112,8 +112,8 @@ public class HeartbeatManagerImpl<I, O> implements HeartbeatManager<I, O> {
return heartbeatListener;
}
- Collection<HeartbeatMonitor<O>> getHeartbeatTargets() {
- return heartbeatTargets.values();
+ Map<ResourceID, HeartbeatMonitor<O>> getHeartbeatTargets() {
+ return heartbeatTargets;
}
//----------------------------------------------------------------------------------------------
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/heartbeat/HeartbeatManagerSenderImpl.java b/flink-runtime/src/main/java/org/apache/flink/runtime/heartbeat/HeartbeatManagerSenderImpl.java
index a61fe34..4c00f89 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/heartbeat/HeartbeatManagerSenderImpl.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/heartbeat/HeartbeatManagerSenderImpl.java
@@ -43,12 +43,31 @@ public class HeartbeatManagerSenderImpl<I, O> extends HeartbeatManagerImpl<I, O>
HeartbeatListener<I, O> heartbeatListener,
ScheduledExecutor mainThreadExecutor,
Logger log) {
+ this(
+ heartbeatPeriod,
+ heartbeatTimeout,
+ ownResourceID,
+ heartbeatListener,
+ mainThreadExecutor,
+ log,
+ new HeartbeatMonitorImpl.Factory<>());
+ }
+
+ HeartbeatManagerSenderImpl(
+ long heartbeatPeriod,
+ long heartbeatTimeout,
+ ResourceID ownResourceID,
+ HeartbeatListener<I, O> heartbeatListener,
+ ScheduledExecutor mainThreadExecutor,
+ Logger log,
+ HeartbeatMonitor.Factory<O> heartbeatMonitorFactory) {
super(
heartbeatTimeout,
ownResourceID,
heartbeatListener,
mainThreadExecutor,
- log);
+ log,
+ heartbeatMonitorFactory);
this.heartbeatPeriod = heartbeatPeriod;
mainThreadExecutor.schedule(this, 0L, TimeUnit.MILLISECONDS);
@@ -58,7 +77,7 @@ public class HeartbeatManagerSenderImpl<I, O> extends HeartbeatManagerImpl<I, O>
public void run() {
if (!stopped) {
log.debug("Trigger heartbeat request.");
- for (HeartbeatMonitor<O> heartbeatMonitor : getHeartbeatTargets()) {
+ for (HeartbeatMonitor<O> heartbeatMonitor : getHeartbeatTargets().values()) {
requestHeartbeat(heartbeatMonitor);
}
diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/heartbeat/TestingHeartbeatServices.java b/flink-runtime/src/test/java/org/apache/flink/runtime/heartbeat/TestingHeartbeatServices.java
index 9de099a..3fb5c6d 100644
--- a/flink-runtime/src/test/java/org/apache/flink/runtime/heartbeat/TestingHeartbeatServices.java
+++ b/flink-runtime/src/test/java/org/apache/flink/runtime/heartbeat/TestingHeartbeatServices.java
@@ -18,12 +18,175 @@
package org.apache.flink.runtime.heartbeat;
+import org.apache.flink.runtime.clusterframework.types.ResourceID;
+import org.apache.flink.runtime.concurrent.ScheduledExecutor;
+
+import org.slf4j.Logger;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+import static org.apache.flink.util.Preconditions.checkState;
+
/**
* A {@link HeartbeatServices} implementation for testing purposes.
+ * This implementation is able to trigger a timeout of specific component manually.
*/
public class TestingHeartbeatServices extends HeartbeatServices {
+ private static final long DEFAULT_HEARTBEAT_TIMEOUT = 10000L;
+
+ private static final long DEFAULT_HEARTBEAT_INTERVAL = 1000L;
+
+ private final Map<ResourceID, HeartbeatManagerImpl> heartbeatManagers = new ConcurrentHashMap<>();
+
+ private final Map<ResourceID, HeartbeatManagerSenderImpl> heartbeatManagerSenders = new ConcurrentHashMap<>();
+
public TestingHeartbeatServices() {
- super(1000L, 10000L);
+ super(DEFAULT_HEARTBEAT_INTERVAL, DEFAULT_HEARTBEAT_TIMEOUT);
+ }
+
+ public TestingHeartbeatServices(long heartbeatInterval) {
+ super(heartbeatInterval, DEFAULT_HEARTBEAT_TIMEOUT);
+ }
+
+ public TestingHeartbeatServices(long heartbeatInterval, long heartbeatTimeout) {
+ super(heartbeatInterval, heartbeatTimeout);
+ }
+
+ @Override
+ public <I, O> HeartbeatManager<I, O> createHeartbeatManager(
+ ResourceID resourceId,
+ HeartbeatListener<I, O> heartbeatListener,
+ ScheduledExecutor mainThreadExecutor,
+ Logger log) {
+
+ HeartbeatManagerImpl<I, O> heartbeatManager = new HeartbeatManagerImpl<>(
+ heartbeatTimeout,
+ resourceId,
+ heartbeatListener,
+ mainThreadExecutor,
+ log,
+ new TestingHeartbeatMonitorFactory<>());
+
+ heartbeatManagers.put(resourceId, heartbeatManager);
+ return heartbeatManager;
+ }
+
+ @Override
+ public <I, O> HeartbeatManager<I, O> createHeartbeatManagerSender(
+ ResourceID resourceId,
+ HeartbeatListener<I, O> heartbeatListener,
+ ScheduledExecutor mainThreadExecutor,
+ Logger log) {
+
+ HeartbeatManagerSenderImpl<I, O> heartbeatManager = new HeartbeatManagerSenderImpl<>(
+ heartbeatInterval,
+ heartbeatTimeout,
+ resourceId,
+ heartbeatListener,
+ mainThreadExecutor,
+ log,
+ new TestingHeartbeatMonitorFactory<>());
+
+ heartbeatManagerSenders.put(resourceId, heartbeatManager);
+ return heartbeatManager;
+ }
+
+ public void triggerHeartbeatTimeout(ResourceID managerResourceId, ResourceID targetResourceId) {
+
+ boolean triggered = false;
+ HeartbeatManagerImpl heartbeatManager = heartbeatManagers.get(managerResourceId);
+ if (heartbeatManager != null) {
+ final TestingHeartbeatMonitor monitor =
+ (TestingHeartbeatMonitor) heartbeatManager.getHeartbeatTargets().get(targetResourceId);
+ if (monitor != null) {
+ monitor.triggerHeartbeatTimeout();
+ triggered = true;
+ }
+ }
+
+ heartbeatManager = heartbeatManagerSenders.get(managerResourceId);
+ if (heartbeatManager != null) {
+ final TestingHeartbeatMonitor monitor =
+ (TestingHeartbeatMonitor) heartbeatManager.getHeartbeatTargets().get(targetResourceId);
+ if (monitor != null) {
+ monitor.triggerHeartbeatTimeout();
+ triggered = true;
+ }
+ }
+
+ checkState(triggered,
+ "There is no target " + targetResourceId + " monitored under Heartbeat manager " + managerResourceId);
+ }
+
+ /**
+ * Factory instantiates testing monitor instance.
+ *
+ * @param <O> Type of the outgoing heartbeat payload
+ */
+ static class TestingHeartbeatMonitorFactory<O> implements HeartbeatMonitor.Factory<O> {
+
+ @Override
+ public HeartbeatMonitor<O> createHeartbeatMonitor(
+ ResourceID resourceID,
+ HeartbeatTarget<O> heartbeatTarget,
+ ScheduledExecutor mainThreadExecutor,
+ HeartbeatListener<?, O> heartbeatListener,
+ long heartbeatTimeoutIntervalMs) {
+
+ return new TestingHeartbeatMonitor<>(
+ resourceID,
+ heartbeatTarget,
+ mainThreadExecutor,
+ heartbeatListener,
+ heartbeatTimeoutIntervalMs);
+ }
+ }
+
+ /**
+ * A heartbeat monitor for testing which supports triggering timeout manually.
+ *
+ * @param <O> Type of the outgoing heartbeat payload
+ */
+ static class TestingHeartbeatMonitor<O> extends HeartbeatMonitorImpl<O> {
+
+ private volatile boolean timeoutTriggered = false;
+
+ TestingHeartbeatMonitor(
+ ResourceID resourceID,
+ HeartbeatTarget<O> heartbeatTarget,
+ ScheduledExecutor scheduledExecutor,
+ HeartbeatListener<?, O> heartbeatListener,
+ long heartbeatTimeoutIntervalMs) {
+
+ super(resourceID, heartbeatTarget, scheduledExecutor, heartbeatListener, heartbeatTimeoutIntervalMs);
+ }
+
+ @Override
+ public void reportHeartbeat() {
+ if (!timeoutTriggered) {
+ super.reportHeartbeat();
+ }
+ // just swallow the heartbeat report
+ }
+
+ @Override
+ void resetHeartbeatTimeout(long heartbeatTimeout) {
+ synchronized (this) {
+ if (timeoutTriggered) {
+ super.resetHeartbeatTimeout(0);
+ } else {
+ super.resetHeartbeatTimeout(heartbeatTimeout);
+ }
+ }
+ }
+
+ void triggerHeartbeatTimeout() {
+ synchronized (this) {
+ timeoutTriggered = true;
+ resetHeartbeatTimeout(0);
+ }
+ }
}
}
[flink] 03/04: [Flink-12164][tests] Harden
JobMasterTest#testJobFailureWhenTaskExecutorHeartbeatTimeout
Posted by tr...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
trohrmann pushed a commit to branch release-1.9
in repository https://gitbox.apache.org/repos/asf/flink.git
commit e0a95d4a1d536b479c7788c941dc5e8a74d7997c
Author: ifndef-SleePy <mm...@gmail.com>
AuthorDate: Fri Aug 30 17:21:28 2019 +0800
[Flink-12164][tests] Harden JobMasterTest#testJobFailureWhenTaskExecutorHeartbeatTimeout
This closes #9568.
---
.../apache/flink/runtime/jobmaster/JobMasterTest.java | 16 ++++++++++------
1 file changed, 10 insertions(+), 6 deletions(-)
diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/jobmaster/JobMasterTest.java b/flink-runtime/src/test/java/org/apache/flink/runtime/jobmaster/JobMasterTest.java
index 42fadf8..3e308d0 100644
--- a/flink-runtime/src/test/java/org/apache/flink/runtime/jobmaster/JobMasterTest.java
+++ b/flink-runtime/src/test/java/org/apache/flink/runtime/jobmaster/JobMasterTest.java
@@ -62,6 +62,7 @@ import org.apache.flink.runtime.executiongraph.ArchivedExecutionGraph;
import org.apache.flink.runtime.executiongraph.ExecutionAttemptID;
import org.apache.flink.runtime.executiongraph.failover.FailoverStrategyLoader;
import org.apache.flink.runtime.heartbeat.HeartbeatServices;
+import org.apache.flink.runtime.heartbeat.TestingHeartbeatServices;
import org.apache.flink.runtime.highavailability.HighAvailabilityServices;
import org.apache.flink.runtime.highavailability.TestingHighAvailabilityServices;
import org.apache.flink.runtime.instance.SimpleSlotContext;
@@ -220,6 +221,8 @@ public class JobMasterTest extends TestLogger {
private static HeartbeatServices heartbeatServices;
+ private static TestingHeartbeatServices testingHeartbeatService;
+
private Configuration configuration;
private ResourceID jmResourceId;
@@ -238,6 +241,7 @@ public class JobMasterTest extends TestLogger {
fastHeartbeatServices = new HeartbeatServices(fastHeartbeatInterval, fastHeartbeatTimeout);
heartbeatServices = new HeartbeatServices(heartbeatInterval, heartbeatTimeout);
+ testingHeartbeatService = new TestingHeartbeatServices(heartbeatInterval, heartbeatTimeout);
}
@Before
@@ -1969,14 +1973,14 @@ public class JobMasterTest extends TestLogger {
@Test
public void testJobFailureWhenTaskExecutorHeartbeatTimeout() throws Exception {
- final AtomicBoolean respondToHeartbeats = new AtomicBoolean(true);
runJobFailureWhenTaskExecutorTerminatesTest(
- fastHeartbeatServices,
- (localTaskManagerLocation, jobMasterGateway) -> respondToHeartbeats.set(false),
+ testingHeartbeatService,
+ (localTaskManagerLocation, jobMasterGateway) ->
+ testingHeartbeatService.triggerHeartbeatTimeout(
+ jmResourceId,
+ localTaskManagerLocation.getResourceID()),
(jobMasterGateway, taskManagerResourceId) -> (resourceId, ignored) -> {
- if (respondToHeartbeats.get()) {
- jobMasterGateway.heartbeatFromTaskManager(taskManagerResourceId, new AccumulatorReport(Collections.emptyList()));
- }
+ jobMasterGateway.heartbeatFromTaskManager(taskManagerResourceId, new AccumulatorReport(Collections.emptyList()));
}
);
}