Search in sources :

Example 1 with ProcessingTimeCallback

use of org.apache.flink.api.common.operators.ProcessingTimeService.ProcessingTimeCallback in project flink by apache.

the class SystemProcessingTimeServiceTest method testQuiesceAndAwaitingCancelsScheduledAtFixRateFuture.

/**
 * Tests that shutting down the SystemProcessingTimeService will also cancel the scheduled at
 * fix rate future.
 */
@Test
public void testQuiesceAndAwaitingCancelsScheduledAtFixRateFuture() throws Exception {
    final AtomicReference<Throwable> errorRef = new AtomicReference<>();
    final long period = 10L;
    final SystemProcessingTimeService timer = createSystemProcessingTimeService(errorRef);
    try {
        ScheduledFuture<?> scheduledFuture = timer.scheduleAtFixedRate(new ProcessingTimeCallback() {

            @Override
            public void onProcessingTime(long timestamp) throws Exception {
            }
        }, 0L, period);
        assertFalse(scheduledFuture.isDone());
        // this should cancel our future
        timer.quiesce().get();
        // exception to be on the safe side
        try {
            scheduledFuture.get();
            fail("scheduled future is not cancelled");
        } catch (CancellationException ignored) {
        // expected
        }
        scheduledFuture = timer.scheduleAtFixedRate(new ProcessingTimeCallback() {

            @Override
            public void onProcessingTime(long timestamp) throws Exception {
                throw new Exception("Test exception.");
            }
        }, 0L, 100L);
        assertNotNull(scheduledFuture);
        assertEquals(0, timer.getNumTasksScheduled());
        if (errorRef.get() != null) {
            throw new Exception(errorRef.get());
        }
    } finally {
        timer.shutdownService();
    }
}
Also used : ProcessingTimeCallback(org.apache.flink.api.common.operators.ProcessingTimeService.ProcessingTimeCallback) CancellationException(java.util.concurrent.CancellationException) AtomicReference(java.util.concurrent.atomic.AtomicReference) CancellationException(java.util.concurrent.CancellationException) Test(org.junit.Test)

Example 2 with ProcessingTimeCallback

use of org.apache.flink.api.common.operators.ProcessingTimeService.ProcessingTimeCallback 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 3 with ProcessingTimeCallback

use of org.apache.flink.api.common.operators.ProcessingTimeService.ProcessingTimeCallback in project flink by apache.

the class SystemProcessingTimeServiceTest method testScheduleAtFixedRate.

/**
 * Tests that SystemProcessingTimeService#scheduleAtFixedRate is actually triggered multiple
 * times.
 */
@Test(timeout = 10000)
public void testScheduleAtFixedRate() throws Exception {
    final AtomicReference<Throwable> errorRef = new AtomicReference<>();
    final long period = 10L;
    final int countDown = 3;
    final SystemProcessingTimeService timer = createSystemProcessingTimeService(errorRef);
    final CountDownLatch countDownLatch = new CountDownLatch(countDown);
    try {
        timer.scheduleAtFixedRate(new ProcessingTimeCallback() {

            @Override
            public void onProcessingTime(long timestamp) throws Exception {
                countDownLatch.countDown();
            }
        }, 0L, period);
        countDownLatch.await();
        if (errorRef.get() != null) {
            throw new Exception(errorRef.get());
        }
    } finally {
        timer.shutdownService();
    }
}
Also used : ProcessingTimeCallback(org.apache.flink.api.common.operators.ProcessingTimeService.ProcessingTimeCallback) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) CancellationException(java.util.concurrent.CancellationException) Test(org.junit.Test)

Example 4 with ProcessingTimeCallback

use of org.apache.flink.api.common.operators.ProcessingTimeService.ProcessingTimeCallback in project flink by apache.

the class StreamTaskTimerTest method testErrorReporting.

@Test
public void testErrorReporting() throws Exception {
    AtomicReference<Throwable> errorRef = new AtomicReference<>();
    OneShotLatch latch = new OneShotLatch();
    testHarness.getEnvironment().setExternalExceptionHandler(ex -> {
        errorRef.set(ex);
        latch.trigger();
    });
    ProcessingTimeCallback callback = timestamp -> {
        throw new Exception("Exception in Timer");
    };
    timeService.registerTimer(System.currentTimeMillis(), callback);
    latch.await();
    assertThat(errorRef.get(), instanceOf(Exception.class));
}
Also used : OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) StreamConfig(org.apache.flink.streaming.api.graph.StreamConfig) TimeoutException(java.util.concurrent.TimeoutException) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) MapFunction(org.apache.flink.api.common.functions.MapFunction) ArrayList(java.util.ArrayList) BasicTypeInfo(org.apache.flink.api.common.typeinfo.BasicTypeInfo) Assert.assertThat(org.junit.Assert.assertThat) TaskManagerOptions(org.apache.flink.configuration.TaskManagerOptions) StreamTaskTestHarness(org.apache.flink.streaming.runtime.tasks.StreamTaskTestHarness) After(org.junit.After) Duration(java.time.Duration) TestLogger(org.apache.flink.util.TestLogger) Timeout(org.junit.rules.Timeout) StreamMap(org.apache.flink.streaming.api.operators.StreamMap) Before(org.junit.Before) StreamTask(org.apache.flink.streaming.runtime.tasks.StreamTask) ProcessingTimeService(org.apache.flink.streaming.runtime.tasks.ProcessingTimeService) Configuration(org.apache.flink.configuration.Configuration) OneInputStreamTask(org.apache.flink.streaming.runtime.tasks.OneInputStreamTask) Test(org.junit.Test) OneInputStreamTaskTestHarness(org.apache.flink.streaming.runtime.tasks.OneInputStreamTaskTestHarness) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Rule(org.junit.Rule) ProcessingTimeCallback(org.apache.flink.api.common.operators.ProcessingTimeService.ProcessingTimeCallback) Assert.assertEquals(org.junit.Assert.assertEquals) ProcessingTimeCallback(org.apache.flink.api.common.operators.ProcessingTimeService.ProcessingTimeCallback) OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) AtomicReference(java.util.concurrent.atomic.AtomicReference) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Aggregations

AtomicReference (java.util.concurrent.atomic.AtomicReference)4 ProcessingTimeCallback (org.apache.flink.api.common.operators.ProcessingTimeService.ProcessingTimeCallback)4 Test (org.junit.Test)4 CancellationException (java.util.concurrent.CancellationException)3 OneShotLatch (org.apache.flink.core.testutils.OneShotLatch)2 Duration (java.time.Duration)1 ArrayList (java.util.ArrayList)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeUnit (java.util.concurrent.TimeUnit)1 TimeoutException (java.util.concurrent.TimeoutException)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 MapFunction (org.apache.flink.api.common.functions.MapFunction)1 BasicTypeInfo (org.apache.flink.api.common.typeinfo.BasicTypeInfo)1 Configuration (org.apache.flink.configuration.Configuration)1 TaskManagerOptions (org.apache.flink.configuration.TaskManagerOptions)1 StreamConfig (org.apache.flink.streaming.api.graph.StreamConfig)1 StreamMap (org.apache.flink.streaming.api.operators.StreamMap)1 OneInputStreamTask (org.apache.flink.streaming.runtime.tasks.OneInputStreamTask)1