Search in sources :

Example 1 with ScheduledExecutorServiceAdapter

use of org.apache.flink.runtime.concurrent.ScheduledExecutorServiceAdapter in project flink by apache.

the class HeartbeatManagerTest method testHeartbeatCluster.

/**
	 * Tests the heartbeat interplay between the {@link HeartbeatManagerImpl} and the
	 * {@link HeartbeatManagerSenderImpl}. The sender should regularly trigger heartbeat requests
	 * which are fulfilled by the receiver. Upon stopping the receiver, the sender should notify
	 * the heartbeat listener about the heartbeat timeout.
	 *
	 * @throws Exception
	 */
@Test
public void testHeartbeatCluster() throws Exception {
    long heartbeatTimeout = 100L;
    long heartbeatPeriod = 20L;
    Object object = new Object();
    Object object2 = new Object();
    ResourceID resourceID = new ResourceID("foobar");
    ResourceID resourceID2 = new ResourceID("barfoo");
    HeartbeatListener<Object, Object> heartbeatListener = mock(HeartbeatListener.class);
    when(heartbeatListener.retrievePayload()).thenReturn(FlinkCompletableFuture.completed(object));
    TestingHeartbeatListener heartbeatListener2 = new TestingHeartbeatListener(object2);
    Future<ResourceID> futureTimeout = heartbeatListener2.getTimeoutFuture();
    HeartbeatManagerImpl<Object, Object> heartbeatManager = new HeartbeatManagerImpl<>(heartbeatTimeout, resourceID, heartbeatListener, new DirectExecutorService(), new ScheduledExecutorServiceAdapter(new ScheduledThreadPoolExecutor(1)), LOG);
    HeartbeatManagerSenderImpl<Object, Object> heartbeatManager2 = new HeartbeatManagerSenderImpl<>(heartbeatPeriod, heartbeatTimeout, resourceID2, heartbeatListener2, new DirectExecutorService(), new ScheduledExecutorServiceAdapter(new ScheduledThreadPoolExecutor(1)), LOG);
    ;
    heartbeatManager.monitorTarget(resourceID2, heartbeatManager2);
    heartbeatManager2.monitorTarget(resourceID, heartbeatManager);
    Thread.sleep(2 * heartbeatTimeout);
    assertFalse(futureTimeout.isDone());
    heartbeatManager.stop();
    ResourceID timeoutResourceID = futureTimeout.get(2 * heartbeatTimeout, TimeUnit.MILLISECONDS);
    assertEquals(resourceID, timeoutResourceID);
    int numberHeartbeats = (int) (2 * heartbeatTimeout / heartbeatPeriod);
    verify(heartbeatListener, atLeast(numberHeartbeats / 2)).reportPayload(resourceID2, object2);
    assertTrue(heartbeatListener2.getNumberHeartbeatReports() >= numberHeartbeats / 2);
}
Also used : ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) DirectExecutorService(org.apache.flink.runtime.util.DirectExecutorService) ScheduledExecutorServiceAdapter(org.apache.flink.runtime.concurrent.ScheduledExecutorServiceAdapter) ResourceID(org.apache.flink.runtime.clusterframework.types.ResourceID) Test(org.junit.Test)

Example 2 with ScheduledExecutorServiceAdapter

use of org.apache.flink.runtime.concurrent.ScheduledExecutorServiceAdapter in project flink by apache.

the class HeartbeatManagerTest method testTargetUnmonitoring.

/**
	 * Tests that after unmonitoring a target, there won't be a timeout triggered
	 */
@Test
public void testTargetUnmonitoring() throws InterruptedException, ExecutionException {
    // this might be too aggresive for Travis, let's see...
    long heartbeatTimeout = 100L;
    ResourceID resourceID = new ResourceID("foobar");
    ResourceID targetID = new ResourceID("target");
    Object object = new Object();
    TestingHeartbeatListener heartbeatListener = new TestingHeartbeatListener(object);
    HeartbeatManager<Object, Object> heartbeatManager = new HeartbeatManagerImpl<>(heartbeatTimeout, resourceID, heartbeatListener, new DirectExecutorService(), new ScheduledExecutorServiceAdapter(new ScheduledThreadPoolExecutor(1)), LOG);
    heartbeatManager.monitorTarget(targetID, mock(HeartbeatTarget.class));
    heartbeatManager.unmonitorTarget(targetID);
    Future<ResourceID> timeout = heartbeatListener.getTimeoutFuture();
    try {
        timeout.get(2 * heartbeatTimeout, TimeUnit.MILLISECONDS);
        fail("Timeout should time out.");
    } catch (TimeoutException e) {
    // the timeout should not be completed since we unmonitored the target
    }
}
Also used : ScheduledExecutorServiceAdapter(org.apache.flink.runtime.concurrent.ScheduledExecutorServiceAdapter) ResourceID(org.apache.flink.runtime.clusterframework.types.ResourceID) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) DirectExecutorService(org.apache.flink.runtime.util.DirectExecutorService) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 3 with ScheduledExecutorServiceAdapter

use of org.apache.flink.runtime.concurrent.ScheduledExecutorServiceAdapter in project flink by apache.

the class HeartbeatManagerTest method testHeartbeatTimeout.

/**
	 * Tests that a heartbeat timeout is signaled if the heartbeat is not reported in time.
	 *
	 * @throws Exception
	 */
@Test
public void testHeartbeatTimeout() throws Exception {
    long heartbeatTimeout = 100L;
    int numHeartbeats = 10;
    long heartbeatInterval = 20L;
    Object payload = new Object();
    ResourceID ownResourceID = new ResourceID("foobar");
    ResourceID targetResourceID = new ResourceID("barfoo");
    TestingHeartbeatListener heartbeatListener = new TestingHeartbeatListener(payload);
    ScheduledExecutorService scheduledExecutorService = mock(ScheduledExecutorService.class);
    ScheduledFuture<?> scheduledFuture = mock(ScheduledFuture.class);
    doReturn(scheduledFuture).when(scheduledExecutorService).schedule(any(Runnable.class), anyLong(), any(TimeUnit.class));
    Object expectedObject = new Object();
    HeartbeatManagerImpl<Object, Object> heartbeatManager = new HeartbeatManagerImpl<>(heartbeatTimeout, ownResourceID, heartbeatListener, new DirectExecutorService(), new ScheduledExecutorServiceAdapter(new ScheduledThreadPoolExecutor(1)), LOG);
    HeartbeatTarget<Object> heartbeatTarget = mock(HeartbeatTarget.class);
    Future<ResourceID> timeoutFuture = heartbeatListener.getTimeoutFuture();
    heartbeatManager.monitorTarget(targetResourceID, heartbeatTarget);
    for (int i = 0; i < numHeartbeats; i++) {
        heartbeatManager.receiveHeartbeat(targetResourceID, expectedObject);
        Thread.sleep(heartbeatInterval);
    }
    assertFalse(timeoutFuture.isDone());
    ResourceID timeoutResourceID = timeoutFuture.get(2 * heartbeatTimeout, TimeUnit.MILLISECONDS);
    assertEquals(targetResourceID, timeoutResourceID);
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) DirectExecutorService(org.apache.flink.runtime.util.DirectExecutorService) ScheduledExecutorServiceAdapter(org.apache.flink.runtime.concurrent.ScheduledExecutorServiceAdapter) ResourceID(org.apache.flink.runtime.clusterframework.types.ResourceID) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.Test)

Aggregations

ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)3 ResourceID (org.apache.flink.runtime.clusterframework.types.ResourceID)3 ScheduledExecutorServiceAdapter (org.apache.flink.runtime.concurrent.ScheduledExecutorServiceAdapter)3 DirectExecutorService (org.apache.flink.runtime.util.DirectExecutorService)3 Test (org.junit.Test)3 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 TimeUnit (java.util.concurrent.TimeUnit)1 TimeoutException (java.util.concurrent.TimeoutException)1