Search in sources :

Example 91 with OneShotLatch

use of org.apache.flink.core.testutils.OneShotLatch in project flink by apache.

the class SystemProcessingTimeServiceTest method testQuiescing.

@Test
public void testQuiescing() throws Exception {
    final AtomicReference<Throwable> errorRef = new AtomicReference<>();
    final SystemProcessingTimeService timer = createSystemProcessingTimeService(errorRef);
    try {
        final OneShotLatch latch = new OneShotLatch();
        final ReentrantLock scopeLock = new ReentrantLock();
        timer.registerTimer(timer.getCurrentProcessingTime() + 20L, new ProcessingTimeCallback() {

            @Override
            public void onProcessingTime(long timestamp) throws Exception {
                scopeLock.lock();
                try {
                    latch.trigger();
                    // delay a bit before leaving the method
                    Thread.sleep(5);
                } finally {
                    scopeLock.unlock();
                }
            }
        });
        // after the task triggered, shut the timer down cleanly, waiting for the task to finish
        latch.await();
        timer.quiesce().get();
        // should be able to immediately acquire the lock, since the task must have exited by
        // now
        assertTrue(scopeLock.tryLock());
        // should be able to schedule more tasks (that never get executed)
        ScheduledFuture<?> future = timer.registerTimer(timer.getCurrentProcessingTime() - 5L, new ProcessingTimeCallback() {

            @Override
            public void onProcessingTime(long timestamp) throws Exception {
                throw new Exception("test");
            }
        });
        assertNotNull(future);
        // nothing should be scheduled right now
        assertEquals(0L, timer.getNumTasksScheduled());
        // triggerable did, in fact, not trigger
        if (errorRef.get() != null) {
            throw new Exception(errorRef.get());
        }
    } finally {
        timer.shutdownService();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) ProcessingTimeCallback(org.apache.flink.api.common.operators.ProcessingTimeService.ProcessingTimeCallback) OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) AtomicReference(java.util.concurrent.atomic.AtomicReference) CancellationException(java.util.concurrent.CancellationException) Test(org.junit.Test)

Example 92 with OneShotLatch

use of org.apache.flink.core.testutils.OneShotLatch in project flink by apache.

the class SystemProcessingTimeServiceTest method testShutdownServiceUninterruptible.

@Test
public void testShutdownServiceUninterruptible() {
    final OneShotLatch blockUntilTriggered = new OneShotLatch();
    final AtomicBoolean timerFinished = new AtomicBoolean(false);
    final SystemProcessingTimeService timeService = createBlockingSystemProcessingTimeService(blockUntilTriggered, timerFinished);
    Assert.assertFalse(timeService.isTerminated());
    final Thread interruptTarget = Thread.currentThread();
    final AtomicBoolean runInterrupts = new AtomicBoolean(true);
    final Thread interruptCallerThread = new Thread(() -> {
        while (runInterrupts.get()) {
            interruptTarget.interrupt();
            try {
                Thread.sleep(1);
            } catch (InterruptedException ignore) {
            }
        }
    });
    interruptCallerThread.start();
    final long timeoutMs = 50L;
    final long startTime = System.nanoTime();
    Assert.assertFalse(timeService.isTerminated());
    // check that termination did not succeed (because of blocking timer execution)
    Assert.assertFalse(timeService.shutdownServiceUninterruptible(timeoutMs));
    // check that termination flag was set.
    Assert.assertTrue(timeService.isTerminated());
    // check that the blocked timer is still in flight.
    Assert.assertFalse(timerFinished.get());
    // check that we waited until timeout
    Assert.assertTrue((System.nanoTime() - startTime) >= (1_000_000L * timeoutMs));
    runInterrupts.set(false);
    do {
        try {
            interruptCallerThread.join();
        } catch (InterruptedException ignore) {
        }
    } while (interruptCallerThread.isAlive());
    // clear the interrupted flag in case join didn't do it
    final boolean ignored = Thread.interrupted();
    blockUntilTriggered.trigger();
    Assert.assertTrue(timeService.shutdownServiceUninterruptible(timeoutMs));
    Assert.assertTrue(timerFinished.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) Test(org.junit.Test)

Example 93 with OneShotLatch

use of org.apache.flink.core.testutils.OneShotLatch in project flink by apache.

the class TaskMailboxProcessorTest method testSuspendRunningMailboxLoop.

@Test
public void testSuspendRunningMailboxLoop() throws Exception {
    // given: Thread for suspending the suspendable loop.
    OneShotLatch doSomeWork = new OneShotLatch();
    AtomicBoolean stop = new AtomicBoolean(false);
    MailboxProcessor mailboxProcessor = new MailboxProcessor(controller -> {
        doSomeWork.trigger();
        if (stop.get()) {
            controller.allActionsCompleted();
        }
    });
    Thread suspendThread = new Thread(() -> {
        try {
            // Ensure that loop was started.
            doSomeWork.await();
            // when: Suspend the suspendable loop.
            mailboxProcessor.suspend();
            // and: Execute the command for stopping the loop.
            mailboxProcessor.getMailboxExecutor(DEFAULT_PRIORITY).execute(() -> stop.set(true), "stop");
        } catch (Exception ignore) {
        }
    });
    suspendThread.start();
    // when: Start the suspendable loop.
    mailboxProcessor.runMailboxLoop();
    suspendThread.join();
    // then: Mailbox is not stopped because it was suspended before the stop command.
    assertFalse(stop.get());
    // when: Resume the suspendable loop.
    mailboxProcessor.runMailboxLoop();
    // then: Stop command successfully executed because it was in the queue.
    assertFalse(mailboxProcessor.isMailboxLoopRunning());
    assertTrue(stop.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) FlinkException(org.apache.flink.util.FlinkException) RunnableWithException(org.apache.flink.util.function.RunnableWithException) FutureTaskWithException(org.apache.flink.util.function.FutureTaskWithException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ExpectedException(org.junit.rules.ExpectedException) Test(org.junit.Test)

Example 94 with OneShotLatch

use of org.apache.flink.core.testutils.OneShotLatch in project flink by apache.

the class TaskMailboxProcessorTest method testSignalUnAvailable.

@Test
public void testSignalUnAvailable() throws Exception {
    final AtomicInteger counter = new AtomicInteger(0);
    final AtomicReference<MailboxDefaultAction.Suspension> suspendedActionRef = new AtomicReference<>();
    final OneShotLatch actionSuspendedLatch = new OneShotLatch();
    final int blockAfterInvocations = 3;
    final int totalInvocations = blockAfterInvocations * 2;
    MailboxThread mailboxThread = new MailboxThread() {

        @Override
        public void runDefaultAction(Controller controller) {
            if (counter.incrementAndGet() == blockAfterInvocations) {
                suspendedActionRef.set(controller.suspendDefaultAction());
                actionSuspendedLatch.trigger();
            } else if (counter.get() == totalInvocations) {
                controller.allActionsCompleted();
            }
        }
    };
    MailboxProcessor mailboxProcessor = start(mailboxThread);
    actionSuspendedLatch.await();
    Assert.assertEquals(blockAfterInvocations, counter.get());
    MailboxDefaultAction.Suspension suspension = suspendedActionRef.get();
    mailboxProcessor.getMailboxExecutor(DEFAULT_PRIORITY).execute(suspension::resume, "resume");
    mailboxThread.join();
    Assert.assertEquals(totalInvocations, counter.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 95 with OneShotLatch

use of org.apache.flink.core.testutils.OneShotLatch in project flink by apache.

the class DispatcherTest method testJobCleanupWithoutRecoveredJobGraph.

@Test
public void testJobCleanupWithoutRecoveredJobGraph() throws Exception {
    final JobID jobIdOfRecoveredDirtyJobs = new JobID();
    final TestingJobMasterServiceLeadershipRunnerFactory jobManagerRunnerFactory = new TestingJobMasterServiceLeadershipRunnerFactory();
    final TestingCleanupRunnerFactory cleanupRunnerFactory = new TestingCleanupRunnerFactory();
    final OneShotLatch dispatcherBootstrapLatch = new OneShotLatch();
    dispatcher = createTestingDispatcherBuilder().setJobManagerRunnerFactory(jobManagerRunnerFactory).setCleanupRunnerFactory(cleanupRunnerFactory).setRecoveredDirtyJobs(Collections.singleton(new JobResult.Builder().jobId(jobIdOfRecoveredDirtyJobs).applicationStatus(ApplicationStatus.SUCCEEDED).netRuntime(1).build())).setDispatcherBootstrapFactory((ignoredDispatcherGateway, ignoredScheduledExecutor, ignoredFatalErrorHandler) -> {
        dispatcherBootstrapLatch.trigger();
        return new NoOpDispatcherBootstrap();
    }).build();
    dispatcher.start();
    dispatcherBootstrapLatch.await();
    final TestingJobManagerRunner cleanupRunner = cleanupRunnerFactory.takeCreatedJobManagerRunner();
    assertThat("The CleanupJobManagerRunner has the wrong job ID attached.", cleanupRunner.getJobID(), is(jobIdOfRecoveredDirtyJobs));
    assertThat("No JobMaster should have been started.", jobManagerRunnerFactory.getQueueSize(), is(0));
}
Also used : JobResult(org.apache.flink.runtime.jobmaster.JobResult) TestingCleanupRunnerFactory(org.apache.flink.runtime.dispatcher.cleanup.TestingCleanupRunnerFactory) OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) TestingJobManagerRunner(org.apache.flink.runtime.jobmaster.TestingJobManagerRunner) JobID(org.apache.flink.api.common.JobID) Test(org.junit.Test)

Aggregations

OneShotLatch (org.apache.flink.core.testutils.OneShotLatch)138 Test (org.junit.Test)118 JobID (org.apache.flink.api.common.JobID)41 CompletableFuture (java.util.concurrent.CompletableFuture)38 ExecutionException (java.util.concurrent.ExecutionException)27 Configuration (org.apache.flink.configuration.Configuration)26 IOException (java.io.IOException)24 Before (org.junit.Before)24 FlinkException (org.apache.flink.util.FlinkException)23 TestLogger (org.apache.flink.util.TestLogger)21 File (java.io.File)20 UUID (java.util.UUID)18 TimeoutException (java.util.concurrent.TimeoutException)18 TestingResourceManagerGateway (org.apache.flink.runtime.resourcemanager.utils.TestingResourceManagerGateway)18 Time (org.apache.flink.api.common.time.Time)17 TestingJobMasterGateway (org.apache.flink.runtime.jobmaster.utils.TestingJobMasterGateway)17 Rule (org.junit.Rule)17 Collections (java.util.Collections)16 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)16 RpcUtils (org.apache.flink.runtime.rpc.RpcUtils)16