Search in sources :

Example 11 with ScheduledExecutor

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
    }
}
Also used : TestingHighAvailabilityServices(org.apache.flink.runtime.highavailability.TestingHighAvailabilityServices) SettableLeaderRetrievalService(org.apache.flink.runtime.leaderretrieval.SettableLeaderRetrievalService) JobMasterId(org.apache.flink.runtime.jobmaster.JobMasterId) Time(org.apache.flink.api.common.time.Time) ExecutionException(java.util.concurrent.ExecutionException) JobID(org.apache.flink.api.common.JobID) ManuallyTriggeredScheduledExecutor(org.apache.flink.util.concurrent.ManuallyTriggeredScheduledExecutor) ScheduledExecutor(org.apache.flink.util.concurrent.ScheduledExecutor) Test(org.junit.Test)

Example 12 with ScheduledExecutor

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));
}
Also used : TestingHighAvailabilityServices(org.apache.flink.runtime.highavailability.TestingHighAvailabilityServices) JobMasterId(org.apache.flink.runtime.jobmaster.JobMasterId) SettableLeaderRetrievalService(org.apache.flink.runtime.leaderretrieval.SettableLeaderRetrievalService) Time(org.apache.flink.api.common.time.Time) JobID(org.apache.flink.api.common.JobID) ManuallyTriggeredScheduledExecutor(org.apache.flink.util.concurrent.ManuallyTriggeredScheduledExecutor) ScheduledExecutor(org.apache.flink.util.concurrent.ScheduledExecutor) Test(org.junit.Test)

Example 13 with ScheduledExecutor

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);
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) ScheduledExecutor(org.apache.flink.util.concurrent.ScheduledExecutor) Test(org.junit.Test)

Example 14 with ScheduledExecutor

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
    }
}
Also used : OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) ScheduledExecutor(org.apache.flink.util.concurrent.ScheduledExecutor) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 15 with ScheduledExecutor

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());
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) ProgramInvocationException(org.apache.flink.client.program.ProgramInvocationException) ScheduledFuture(java.util.concurrent.ScheduledFuture) ExceptionUtils(org.apache.flink.util.ExceptionUtils) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Duration(java.time.Duration) Assertions(org.assertj.core.api.Assertions) FailingJob(org.apache.flink.client.testjar.FailingJob) ScheduledExecutor(org.apache.flink.util.concurrent.ScheduledExecutor) Acknowledge(org.apache.flink.runtime.messages.Acknowledge) Executors(java.util.concurrent.Executors) ExecutorUtils(org.apache.flink.util.ExecutorUtils) Test(org.junit.jupiter.api.Test) FlinkJobNotFoundException(org.apache.flink.runtime.messages.FlinkJobNotFoundException) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) SerializedThrowable(org.apache.flink.util.SerializedThrowable) Optional(java.util.Optional) PackagedProgram(org.apache.flink.client.program.PackagedProgram) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.fail(org.junit.jupiter.api.Assertions.fail) FlinkException(org.apache.flink.util.FlinkException) ScheduledExecutorServiceAdapter(org.apache.flink.util.concurrent.ScheduledExecutorServiceAdapter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) EnumSource(org.junit.jupiter.params.provider.EnumSource) CompletableFuture(java.util.concurrent.CompletableFuture) JobStatus(org.apache.flink.api.common.JobStatus) DispatcherGateway(org.apache.flink.runtime.dispatcher.DispatcherGateway) Supplier(java.util.function.Supplier) EmbeddedExecutor(org.apache.flink.client.deployment.application.executors.EmbeddedExecutor) PipelineOptionsInternal(org.apache.flink.configuration.PipelineOptionsInternal) MultiExecuteJob(org.apache.flink.client.testjar.MultiExecuteJob) JobResult(org.apache.flink.runtime.jobmaster.JobResult) TestLoggerExtension(org.apache.flink.util.TestLoggerExtension) FutureUtils(org.apache.flink.util.concurrent.FutureUtils) FatalErrorHandler(org.apache.flink.runtime.rpc.FatalErrorHandler) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) BiConsumer(java.util.function.BiConsumer) DeploymentOptions(org.apache.flink.configuration.DeploymentOptions) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) JobExecutionException(org.apache.flink.runtime.client.JobExecutionException) HighAvailabilityMode(org.apache.flink.runtime.jobmanager.HighAvailabilityMode) FlinkRuntimeException(org.apache.flink.util.FlinkRuntimeException) ApplicationStatus(org.apache.flink.runtime.clusterframework.ApplicationStatus) Configuration(org.apache.flink.configuration.Configuration) JobCancellationException(org.apache.flink.runtime.client.JobCancellationException) ConcurrentLinkedDeque(java.util.concurrent.ConcurrentLinkedDeque) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) JobID(org.apache.flink.api.common.JobID) TestingDispatcherGateway(org.apache.flink.runtime.webmonitor.TestingDispatcherGateway) Collections(java.util.Collections) HighAvailabilityOptions(org.apache.flink.configuration.HighAvailabilityOptions) DuplicateJobSubmissionException(org.apache.flink.runtime.client.DuplicateJobSubmissionException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestingDispatcherGateway(org.apache.flink.runtime.webmonitor.TestingDispatcherGateway) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

ScheduledExecutor (org.apache.flink.util.concurrent.ScheduledExecutor)20 JobID (org.apache.flink.api.common.JobID)14 Test (org.junit.Test)13 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)9 JobResult (org.apache.flink.runtime.jobmaster.JobResult)9 ScheduledExecutorServiceAdapter (org.apache.flink.util.concurrent.ScheduledExecutorServiceAdapter)9 TimeUnit (java.util.concurrent.TimeUnit)8 CompletableFuture (java.util.concurrent.CompletableFuture)7 ExecutionException (java.util.concurrent.ExecutionException)7 ScheduledFuture (java.util.concurrent.ScheduledFuture)7 Duration (java.time.Duration)6 Collections (java.util.Collections)6 Optional (java.util.Optional)6 ConcurrentLinkedDeque (java.util.concurrent.ConcurrentLinkedDeque)6 Executors (java.util.concurrent.Executors)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 BiConsumer (java.util.function.BiConsumer)6 Supplier (java.util.function.Supplier)6 JobStatus (org.apache.flink.api.common.JobStatus)6 EmbeddedExecutor (org.apache.flink.client.deployment.application.executors.EmbeddedExecutor)6