use of org.apache.flink.util.concurrent.ScheduledExecutor in project flink by apache.
the class DefaultJobLeaderIdServiceTest method testRemovingJob.
/**
* Tests that removing a job completes the job leader id future exceptionally.
*/
@Test(timeout = 10000)
public void testRemovingJob() throws Exception {
final JobID jobId = new JobID();
TestingHighAvailabilityServices highAvailabilityServices = new TestingHighAvailabilityServices();
SettableLeaderRetrievalService leaderRetrievalService = new SettableLeaderRetrievalService(null, null);
highAvailabilityServices.setJobMasterLeaderRetriever(jobId, leaderRetrievalService);
ScheduledExecutor scheduledExecutor = mock(ScheduledExecutor.class);
Time timeout = Time.milliseconds(5000L);
JobLeaderIdActions jobLeaderIdActions = mock(JobLeaderIdActions.class);
JobLeaderIdService jobLeaderIdService = new DefaultJobLeaderIdService(highAvailabilityServices, scheduledExecutor, timeout);
jobLeaderIdService.start(jobLeaderIdActions);
jobLeaderIdService.addJob(jobId);
CompletableFuture<JobMasterId> leaderIdFuture = jobLeaderIdService.getLeaderId(jobId);
// remove the job before we could find a leader
jobLeaderIdService.removeJob(jobId);
assertFalse(jobLeaderIdService.containsJob(jobId));
try {
leaderIdFuture.get();
fail("The leader id future should be completed exceptionally.");
} catch (ExecutionException ignored) {
// expected exception
}
}
use of org.apache.flink.util.concurrent.ScheduledExecutor in project flink by apache.
the class DefaultJobLeaderIdServiceTest 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 JobMasterId leaderId = JobMasterId.generate();
TestingHighAvailabilityServices highAvailabilityServices = new TestingHighAvailabilityServices();
SettableLeaderRetrievalService leaderRetrievalService = new SettableLeaderRetrievalService(null, null);
highAvailabilityServices.setJobMasterLeaderRetriever(jobId, leaderRetrievalService);
ScheduledExecutor scheduledExecutor = mock(ScheduledExecutor.class);
Time timeout = Time.milliseconds(5000L);
JobLeaderIdActions jobLeaderIdActions = mock(JobLeaderIdActions.class);
JobLeaderIdService jobLeaderIdService = new DefaultJobLeaderIdService(highAvailabilityServices, scheduledExecutor, timeout);
jobLeaderIdService.start(jobLeaderIdActions);
jobLeaderIdService.addJob(jobId);
CompletableFuture<JobMasterId> leaderIdFuture = jobLeaderIdService.getLeaderId(jobId);
// notify the leader id service about the new leader
leaderRetrievalService.notifyListener(address, leaderId.toUUID());
assertEquals(leaderId, leaderIdFuture.get());
assertTrue(jobLeaderIdService.containsJob(jobId));
}
use of org.apache.flink.util.concurrent.ScheduledExecutor in project flink by apache.
the class AkkaRpcServiceTest method testScheduledExecutorServiceWithFixedDelaySchedule.
/**
* Tests that the RPC service's scheduled executor service can execute runnable with a fixed
* delay.
*/
@Test(timeout = 60000)
public void testScheduledExecutorServiceWithFixedDelaySchedule() throws Exception {
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.scheduleWithFixedDelay(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);
}
use of org.apache.flink.util.concurrent.ScheduledExecutor in project flink by apache.
the class AkkaRpcServiceTest method testScheduledExecutorServiceCancelWithFixedDelay.
/**
* Tests that canceling the returned future will stop the execution of the scheduled runnable.
*/
@Test
public void testScheduledExecutorServiceCancelWithFixedDelay() throws InterruptedException {
ScheduledExecutor scheduledExecutor = akkaRpcService.getScheduledExecutor();
long delay = 10L;
final OneShotLatch futureTask = new OneShotLatch();
final OneShotLatch latch = new OneShotLatch();
final OneShotLatch shouldNotBeTriggeredLatch = new OneShotLatch();
ScheduledFuture<?> future = scheduledExecutor.scheduleWithFixedDelay(() -> {
try {
if (futureTask.isTriggered()) {
shouldNotBeTriggeredLatch.trigger();
} else {
// first run
futureTask.trigger();
latch.await();
}
} catch (InterruptedException ignored) {
// ignore
}
}, delay, delay, TimeUnit.MILLISECONDS);
// wait until we're in the runnable
futureTask.await();
// cancel the scheduled future
future.cancel(false);
latch.trigger();
try {
shouldNotBeTriggeredLatch.await(5 * delay, TimeUnit.MILLISECONDS);
fail("The shouldNotBeTriggeredLatch should never be triggered.");
} catch (TimeoutException e) {
// expected
}
}
use of org.apache.flink.util.concurrent.ScheduledExecutor in project flink by apache.
the class ApplicationDispatcherBootstrapTest method testErrorHandlerIsCalledWhenApplicationStatusIsUnknown.
@Test
public void testErrorHandlerIsCalledWhenApplicationStatusIsUnknown() throws Exception {
// we're "listening" on this to be completed to verify that the cluster
// is being shut down from the ApplicationDispatcherBootstrap
final AtomicBoolean shutdownCalled = new AtomicBoolean(false);
final TestingDispatcherGateway.Builder dispatcherBuilder = canceledJobGatewayBuilder().setRequestJobResultFunction(jobID -> CompletableFuture.completedFuture(createUnknownJobResult(jobID))).setClusterShutdownFunction(status -> {
shutdownCalled.set(true);
return CompletableFuture.completedFuture(Acknowledge.get());
});
final TestingDispatcherGateway dispatcherGateway = dispatcherBuilder.build();
final CompletableFuture<Void> errorHandlerFuture = new CompletableFuture<>();
final ApplicationDispatcherBootstrap bootstrap = createApplicationDispatcherBootstrap(3, dispatcherGateway, scheduledExecutor, errorHandlerFuture::completeExceptionally);
// check that bootstrap shutdown completes exceptionally
assertException(bootstrap.getApplicationCompletionFuture(), UnsuccessfulExecutionException.class);
// and exception gets propagated to error handler
assertException(bootstrap.getApplicationCompletionFuture(), UnsuccessfulExecutionException.class);
// and cluster didn't shut down
assertFalse(shutdownCalled.get());
}
Aggregations