Search in sources :

Example 1 with ScheduledExecutor

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

the class AkkaRpcServiceTest method testScheduledExecutorServicePeriodicSchedule.

/**
	 * Tests that the RPC service's scheduled executor service can execute runnables at a fixed
	 * rate.
	 */
@Test(timeout = 60000)
public void testScheduledExecutorServicePeriodicSchedule() throws ExecutionException, InterruptedException {
    ScheduledExecutor scheduledExecutor = akkaRpcService.getScheduledExecutor();
    final int tries = 4;
    final long delay = 10L;
    final CountDownLatch countDownLatch = new CountDownLatch(tries);
    long currentTime = System.nanoTime();
    ScheduledFuture<?> future = scheduledExecutor.scheduleAtFixedRate(new Runnable() {

        @Override
        public void run() {
            countDownLatch.countDown();
        }
    }, delay, delay, TimeUnit.MILLISECONDS);
    assertTrue(!future.isDone());
    countDownLatch.await();
    // the future should not complete since we have a periodic task
    assertTrue(!future.isDone());
    long finalTime = System.nanoTime() - currentTime;
    // the processing should have taken at least delay times the number of count downs.
    assertTrue(finalTime >= tries * delay);
    future.cancel(true);
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) ScheduledExecutor(org.apache.flink.runtime.concurrent.ScheduledExecutor) Test(org.junit.Test)

Example 2 with ScheduledExecutor

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

the class JobLeaderIdServiceTest method testAddingJob.

/**
	 * Tests adding a job and finding out its leader id
	 */
@Test(timeout = 10000)
public void testAddingJob() throws Exception {
    final JobID jobId = new JobID();
    final String address = "foobar";
    final UUID leaderId = UUID.randomUUID();
    TestingHighAvailabilityServices highAvailabilityServices = new TestingHighAvailabilityServices();
    TestingLeaderRetrievalService leaderRetrievalService = new TestingLeaderRetrievalService();
    highAvailabilityServices.setJobMasterLeaderRetriever(jobId, leaderRetrievalService);
    ScheduledExecutor scheduledExecutor = mock(ScheduledExecutor.class);
    Time timeout = Time.milliseconds(5000L);
    JobLeaderIdActions jobLeaderIdActions = mock(JobLeaderIdActions.class);
    JobLeaderIdService jobLeaderIdService = new JobLeaderIdService(highAvailabilityServices, scheduledExecutor, timeout);
    jobLeaderIdService.start(jobLeaderIdActions);
    jobLeaderIdService.addJob(jobId);
    Future<UUID> leaderIdFuture = jobLeaderIdService.getLeaderId(jobId);
    // notify the leader id service about the new leader
    leaderRetrievalService.notifyListener(address, leaderId);
    assertEquals(leaderId, leaderIdFuture.get());
    assertTrue(jobLeaderIdService.containsJob(jobId));
}
Also used : TestingHighAvailabilityServices(org.apache.flink.runtime.highavailability.TestingHighAvailabilityServices) TestingLeaderRetrievalService(org.apache.flink.runtime.leaderelection.TestingLeaderRetrievalService) Time(org.apache.flink.api.common.time.Time) UUID(java.util.UUID) JobID(org.apache.flink.api.common.JobID) ScheduledExecutor(org.apache.flink.runtime.concurrent.ScheduledExecutor) Test(org.junit.Test)

Example 3 with ScheduledExecutor

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

the class AkkaRpcServiceTest method testScheduledExecutorServiceSimpleSchedule.

/**
	 * Tests a simple scheduled runnable being executed by the RPC services scheduled executor
	 * service.
	 */
@Test(timeout = 60000)
public void testScheduledExecutorServiceSimpleSchedule() throws ExecutionException, InterruptedException {
    ScheduledExecutor scheduledExecutor = akkaRpcService.getScheduledExecutor();
    final OneShotLatch latch = new OneShotLatch();
    ScheduledFuture<?> future = scheduledExecutor.schedule(new Runnable() {

        @Override
        public void run() {
            latch.trigger();
        }
    }, 10L, TimeUnit.MILLISECONDS);
    future.get();
    // once the future is completed, then the latch should have been triggered
    assertTrue(latch.isTriggered());
}
Also used : OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) ScheduledExecutor(org.apache.flink.runtime.concurrent.ScheduledExecutor) Test(org.junit.Test)

Example 4 with ScheduledExecutor

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

the class HeartbeatManagerTest method testHeartbeatMonitorUpdate.

/**
	 * Tests that the heartbeat monitors are updated when receiving a new heartbeat signal.
	 */
@Test
public void testHeartbeatMonitorUpdate() {
    long heartbeatTimeout = 1000L;
    ResourceID ownResourceID = new ResourceID("foobar");
    ResourceID targetResourceID = new ResourceID("barfoo");
    HeartbeatListener<Object, Object> heartbeatListener = mock(HeartbeatListener.class);
    ScheduledExecutor scheduledExecutor = mock(ScheduledExecutor.class);
    ScheduledFuture<?> scheduledFuture = mock(ScheduledFuture.class);
    doReturn(scheduledFuture).when(scheduledExecutor).schedule(any(Runnable.class), anyLong(), any(TimeUnit.class));
    Object expectedObject = new Object();
    when(heartbeatListener.retrievePayload()).thenReturn(FlinkCompletableFuture.completed(expectedObject));
    HeartbeatManagerImpl<Object, Object> heartbeatManager = new HeartbeatManagerImpl<>(heartbeatTimeout, ownResourceID, heartbeatListener, new DirectExecutorService(), scheduledExecutor, LOG);
    HeartbeatTarget<Object> heartbeatTarget = mock(HeartbeatTarget.class);
    heartbeatManager.monitorTarget(targetResourceID, heartbeatTarget);
    heartbeatManager.receiveHeartbeat(targetResourceID, expectedObject);
    verify(scheduledFuture, times(1)).cancel(true);
    verify(scheduledExecutor, times(2)).schedule(any(Runnable.class), eq(heartbeatTimeout), eq(TimeUnit.MILLISECONDS));
}
Also used : ResourceID(org.apache.flink.runtime.clusterframework.types.ResourceID) TimeUnit(java.util.concurrent.TimeUnit) DirectExecutorService(org.apache.flink.runtime.util.DirectExecutorService) ScheduledExecutor(org.apache.flink.runtime.concurrent.ScheduledExecutor) Test(org.junit.Test)

Example 5 with ScheduledExecutor

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

the class JobLeaderIdServiceTest method jobTimeoutAfterLostLeadership.

/**
	 * Tests that a timeout get cancelled once a job leader has been found. Furthermore, it tests
	 * that a new timeout is registered after the jobmanager has lost leadership.
	 */
@Test(timeout = 10000)
public void jobTimeoutAfterLostLeadership() throws Exception {
    final JobID jobId = new JobID();
    final String address = "foobar";
    final UUID leaderId = UUID.randomUUID();
    TestingHighAvailabilityServices highAvailabilityServices = new TestingHighAvailabilityServices();
    TestingLeaderRetrievalService leaderRetrievalService = new TestingLeaderRetrievalService();
    highAvailabilityServices.setJobMasterLeaderRetriever(jobId, leaderRetrievalService);
    ScheduledFuture<?> timeout1 = mock(ScheduledFuture.class);
    ScheduledFuture<?> timeout2 = mock(ScheduledFuture.class);
    final Queue<ScheduledFuture<?>> timeoutQueue = new ArrayDeque<>(Arrays.asList(timeout1, timeout2));
    ScheduledExecutor scheduledExecutor = mock(ScheduledExecutor.class);
    final AtomicReference<Runnable> lastRunnable = new AtomicReference<>();
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            lastRunnable.set((Runnable) invocation.getArguments()[0]);
            return timeoutQueue.poll();
        }
    }).when(scheduledExecutor).schedule(any(Runnable.class), anyLong(), any(TimeUnit.class));
    Time timeout = Time.milliseconds(5000L);
    JobLeaderIdActions jobLeaderIdActions = mock(JobLeaderIdActions.class);
    final AtomicReference<UUID> lastTimeoutId = new AtomicReference<>();
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            lastTimeoutId.set((UUID) invocation.getArguments()[1]);
            return null;
        }
    }).when(jobLeaderIdActions).notifyJobTimeout(eq(jobId), any(UUID.class));
    JobLeaderIdService jobLeaderIdService = new JobLeaderIdService(highAvailabilityServices, scheduledExecutor, timeout);
    jobLeaderIdService.start(jobLeaderIdActions);
    jobLeaderIdService.addJob(jobId);
    Future<UUID> leaderIdFuture = jobLeaderIdService.getLeaderId(jobId);
    // notify the leader id service about the new leader
    leaderRetrievalService.notifyListener(address, leaderId);
    assertEquals(leaderId, leaderIdFuture.get());
    assertTrue(jobLeaderIdService.containsJob(jobId));
    // check that the first timeout got cancelled
    verify(timeout1, times(1)).cancel(anyBoolean());
    verify(scheduledExecutor, times(1)).schedule(any(Runnable.class), anyLong(), any(TimeUnit.class));
    // initial timeout runnable which should no longer have an effect
    Runnable runnable = lastRunnable.get();
    assertNotNull(runnable);
    runnable.run();
    verify(jobLeaderIdActions, times(1)).notifyJobTimeout(eq(jobId), any(UUID.class));
    // the timeout should no longer be valid
    assertFalse(jobLeaderIdService.isValidTimeout(jobId, lastTimeoutId.get()));
    // lose leadership
    leaderRetrievalService.notifyListener("", null);
    verify(scheduledExecutor, times(2)).schedule(any(Runnable.class), anyLong(), any(TimeUnit.class));
    // the second runnable should be the new timeout
    runnable = lastRunnable.get();
    assertNotNull(runnable);
    runnable.run();
    verify(jobLeaderIdActions, times(2)).notifyJobTimeout(eq(jobId), any(UUID.class));
    // the new timeout should be valid
    assertTrue(jobLeaderIdService.isValidTimeout(jobId, lastTimeoutId.get()));
}
Also used : TestingLeaderRetrievalService(org.apache.flink.runtime.leaderelection.TestingLeaderRetrievalService) AtomicReference(java.util.concurrent.atomic.AtomicReference) Time(org.apache.flink.api.common.time.Time) ArrayDeque(java.util.ArrayDeque) ScheduledFuture(java.util.concurrent.ScheduledFuture) ScheduledExecutor(org.apache.flink.runtime.concurrent.ScheduledExecutor) TestingHighAvailabilityServices(org.apache.flink.runtime.highavailability.TestingHighAvailabilityServices) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TimeUnit(java.util.concurrent.TimeUnit) UUID(java.util.UUID) JobID(org.apache.flink.api.common.JobID) Test(org.junit.Test)

Aggregations

ScheduledExecutor (org.apache.flink.runtime.concurrent.ScheduledExecutor)12 Test (org.junit.Test)12 UUID (java.util.UUID)6 TestingHighAvailabilityServices (org.apache.flink.runtime.highavailability.TestingHighAvailabilityServices)6 TestingLeaderRetrievalService (org.apache.flink.runtime.leaderelection.TestingLeaderRetrievalService)6 JobID (org.apache.flink.api.common.JobID)5 Time (org.apache.flink.api.common.time.Time)5 ResourceID (org.apache.flink.runtime.clusterframework.types.ResourceID)4 TimeUnit (java.util.concurrent.TimeUnit)3 TimeoutException (java.util.concurrent.TimeoutException)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 Configuration (org.apache.flink.configuration.Configuration)2 OneShotLatch (org.apache.flink.core.testutils.OneShotLatch)2 HeartbeatServices (org.apache.flink.runtime.heartbeat.HeartbeatServices)2 TestingSerialRpcService (org.apache.flink.runtime.rpc.TestingSerialRpcService)2 TaskManagerLocation (org.apache.flink.runtime.taskmanager.TaskManagerLocation)2 TestingFatalErrorHandler (org.apache.flink.runtime.util.TestingFatalErrorHandler)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 URL (java.net.URL)1 ArrayDeque (java.util.ArrayDeque)1