[ https://issues.apache.org/jira/browse/FLINK-4449?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15436094#comment-15436094 ]
ASF GitHub Bot commented on FLINK-4449: --------------------------------------- Github user beyond1920 commented on a diff in the pull request: https://github.com/apache/flink/pull/2410#discussion_r76168206 --- Diff: flink-runtime/src/test/java/org/apache/flink/runtime/rpc/heartbeat/HeartbeatSchedulerTest.java --- @@ -0,0 +1,248 @@ +/* + * 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.heartbeat; + +import akka.actor.ActorSystem; +import akka.dispatch.Futures; +import akka.util.Timeout; +import org.apache.flink.runtime.akka.AkkaUtils; +import org.apache.flink.runtime.rpc.RpcGateway; +import org.apache.flink.runtime.rpc.RpcService; +import org.apache.flink.runtime.rpc.RpcTimeout; +import org.apache.flink.runtime.rpc.akka.AkkaRpcService; +import org.apache.flink.util.TestLogger; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.slf4j.Logger; +import scala.concurrent.Future; +import scala.concurrent.duration.FiniteDuration; + +import java.util.UUID; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.atMost; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.timeout; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * test for HeartbeatScheduler, including successful case, timeout case(retry on backoff timeout), failure case(retry) + */ +public class HeartbeatSchedulerTest extends TestLogger { + private static final long INITIAL_INTERVAL = 200; + private static final long INITIAL_TIMEOUT = 20; + private static final long MAX_TIMEOUT = 150; + private static final long DELAY_ON_ERROR = 100; + private static final long MAX_ATTEMPT_TIME = 300; + + + private static ActorSystem actorSystem; + private static AkkaRpcService akkaRpcService; + + private static final Timeout timeout = new Timeout(10000, TimeUnit.MILLISECONDS); + + @BeforeClass + public static void setup() throws Exception { + actorSystem = AkkaUtils.createDefaultActorSystem(); + + akkaRpcService = new AkkaRpcService(actorSystem, timeout); + } + + @AfterClass + public static void teardown() throws Exception { + akkaRpcService.stopService(); + + actorSystem.shutdown(); + actorSystem.awaitTermination(); + } + + + /** + * test for receiving a regular heartbeat response in time, and checking the heartbeatResponse is properly delivered + * @throws Exception + */ + @Test + public void testHeartbeatSuccessful() throws Exception { + HeartbeatSender sender = mock(HeartbeatSender.class); + UUID leaderSessionID = UUID.randomUUID(); + + HeartbeatReceiverGateway targetGateway = mock(HeartbeatReceiverGateway.class); + + String response = "ok"; + when(targetGateway.triggerHeartbeat(any(UUID.class), any(FiniteDuration.class))).thenReturn( + Futures.successful(response) + ); + + TestingHeartbeatScheduler heartbeatScheduler = new TestingHeartbeatScheduler(sender, akkaRpcService, leaderSessionID, + targetGateway, "taskExecutor-test-address", "testTargetGateway", log, INITIAL_INTERVAL, INITIAL_TIMEOUT, MAX_TIMEOUT, DELAY_ON_ERROR, MAX_ATTEMPT_TIME); + heartbeatScheduler.start(); + + // verify heartbeat successful and syncHeartbeatResponse is invoked for sync heartbeat response + verify(sender, timeout(5000)).syncHeartbeatResponse(eq(response)); + // verify heartbeat trigger is still on + Assert.assertFalse(heartbeatScheduler.isClosed()); + heartbeatScheduler.close(); + } + + /** + * test for fail upon first time, but successful after retry, and checking the heartbeatResponse is properly delivered + * @throws Exception + */ + @Test + public void testHeartbeatRetrySuccessOnError() throws Exception { + HeartbeatSender sender = mock(HeartbeatSender.class); + UUID leaderSessionID = UUID.randomUUID(); + + HeartbeatReceiverGateway targetGateway = mock(HeartbeatReceiverGateway.class); + + String response = "ok"; + // triggerHeartbeat fails upon first time, but success on the second + when(targetGateway.triggerHeartbeat(any(UUID.class), any(FiniteDuration.class))).thenReturn( + Futures.<String> failed(new Exception("error happened")), // first attempt fail + Futures.successful(response) // second attempt success + ); + + TestingHeartbeatScheduler heartbeatScheduler = new TestingHeartbeatScheduler(sender, akkaRpcService, leaderSessionID, + targetGateway, "taskExecutor-test-address", "testTargetGateway", log, INITIAL_INTERVAL, INITIAL_TIMEOUT, MAX_TIMEOUT, DELAY_ON_ERROR, MAX_ATTEMPT_TIME); + heartbeatScheduler.start(); + + // verify heartbeat successful and syncHeartbeatResponse is invoked for sync heartbeat response + verify(sender, timeout(5000)).syncHeartbeatResponse(eq(response)); + // verify heartbeat trigger is still on + Assert.assertFalse(heartbeatScheduler.isClosed()); + heartbeatScheduler.close(); + } + + /** + * test loss of heartbeat and check mark the resource failed after retry max attempt times, + * @throws Exception + */ + @Test + public void testLostHeartbeatOnError() throws Exception { + HeartbeatSender sender = mock(HeartbeatSender.class); + UUID leaderSessionID = UUID.randomUUID(); + + HeartbeatReceiverGateway targetGateway = mock(HeartbeatReceiverGateway.class); + + // triggerHeartbeat always fail + when(targetGateway.triggerHeartbeat(any(UUID.class), any(FiniteDuration.class))).thenReturn( + Futures.<String> failed(new Exception("error happened"))); + + TestingHeartbeatScheduler heartbeatScheduler = new TestingHeartbeatScheduler(sender, akkaRpcService, leaderSessionID, + targetGateway, "taskExecutor-test-address", "testTargetGateway", log, INITIAL_INTERVAL, INITIAL_TIMEOUT, MAX_TIMEOUT, DELAY_ON_ERROR, MAX_ATTEMPT_TIME); + heartbeatScheduler.start(); + // verify lost heartbeat after max attempts and notifyLostHeartbeat is invoked --- End diff -- yes, I define max timeout, In this test, i also want to verify how many times sender will try to trigger heartbeat to receiver in the max timeout period. max attempt number = Math.ceil(MAX_ATTEMPT_TIMEOUT/DELAY_ON_ERROR) + 1)) > Heartbeat Manager between ResourceManager and TaskExecutor > ---------------------------------------------------------- > > Key: FLINK-4449 > URL: https://issues.apache.org/jira/browse/FLINK-4449 > Project: Flink > Issue Type: Sub-task > Components: Cluster Management > Reporter: zhangjing > Assignee: zhangjing > > HeartbeatManager is responsible for heartbeat between resourceManager to > TaskExecutor > 1. Register taskExecutors > register heartbeat targets. If the heartbeat response for these targets is > not reported in time, mark target failed and notify resourceManager > 2. trigger heartbeat > trigger heartbeat from resourceManager to TaskExecutor periodically > taskExecutor report slot allocation in the heartbeat response > ResourceManager sync self slot allocation with the heartbeat response -- This message was sent by Atlassian JIRA (v6.3.4#6332)