Search in sources :

Example 1 with ProcessingTimeCallback

use of org.apache.flink.streaming.runtime.tasks.ProcessingTimeCallback in project flink by apache.

the class AsyncWaitOperator method processElement.

@Override
public void processElement(StreamRecord<IN> element) throws Exception {
    final StreamRecordQueueEntry<OUT> streamRecordBufferEntry = new StreamRecordQueueEntry<>(element);
    if (timeout > 0L) {
        // register a timeout for this AsyncStreamRecordBufferEntry
        long timeoutTimestamp = timeout + getProcessingTimeService().getCurrentProcessingTime();
        final ScheduledFuture<?> timerFuture = getProcessingTimeService().registerTimer(timeoutTimestamp, new ProcessingTimeCallback() {

            @Override
            public void onProcessingTime(long timestamp) throws Exception {
                streamRecordBufferEntry.collect(new TimeoutException("Async function call has timed out."));
            }
        });
        // Cancel the timer once we've completed the stream record buffer entry. This will remove
        // the register trigger task
        streamRecordBufferEntry.onComplete(new AcceptFunction<StreamElementQueueEntry<Collection<OUT>>>() {

            @Override
            public void accept(StreamElementQueueEntry<Collection<OUT>> value) {
                timerFuture.cancel(true);
            }
        }, executor);
    }
    addAsyncBufferEntry(streamRecordBufferEntry);
    userFunction.asyncInvoke(element.getValue(), streamRecordBufferEntry);
}
Also used : StreamElementQueueEntry(org.apache.flink.streaming.api.operators.async.queue.StreamElementQueueEntry) TimeoutException(java.util.concurrent.TimeoutException) ProcessingTimeCallback(org.apache.flink.streaming.runtime.tasks.ProcessingTimeCallback) Collection(java.util.Collection) StreamRecordQueueEntry(org.apache.flink.streaming.api.operators.async.queue.StreamRecordQueueEntry) TimeoutException(java.util.concurrent.TimeoutException)

Example 2 with ProcessingTimeCallback

use of org.apache.flink.streaming.runtime.tasks.ProcessingTimeCallback in project flink by apache.

the class StreamTaskTimerTest method testOpenCloseAndTimestamps.

@Test
public void testOpenCloseAndTimestamps() throws Exception {
    final OneInputStreamTask<String, String> mapTask = new OneInputStreamTask<>();
    final OneInputStreamTaskTestHarness<String, String> testHarness = new OneInputStreamTaskTestHarness<>(mapTask, BasicTypeInfo.STRING_TYPE_INFO, BasicTypeInfo.STRING_TYPE_INFO);
    testHarness.setupOutputForSingletonOperatorChain();
    StreamConfig streamConfig = testHarness.getStreamConfig();
    StreamMap<String, String> mapOperator = new StreamMap<>(new DummyMapFunction<String>());
    streamConfig.setStreamOperator(mapOperator);
    testHarness.invoke();
    testHarness.waitForTaskRunning();
    // first one spawns thread
    mapTask.getProcessingTimeService().registerTimer(System.currentTimeMillis(), new ProcessingTimeCallback() {

        @Override
        public void onProcessingTime(long timestamp) {
        }
    });
    assertEquals(1, StreamTask.TRIGGER_THREAD_GROUP.activeCount());
    testHarness.endInput();
    testHarness.waitForTaskCompletion();
    // thread needs to die in time
    long deadline = System.currentTimeMillis() + 4000;
    while (StreamTask.TRIGGER_THREAD_GROUP.activeCount() > 0 && System.currentTimeMillis() < deadline) {
        Thread.sleep(10);
    }
    assertEquals("Trigger timer thread did not properly shut down", 0, StreamTask.TRIGGER_THREAD_GROUP.activeCount());
}
Also used : OneInputStreamTask(org.apache.flink.streaming.runtime.tasks.OneInputStreamTask) ProcessingTimeCallback(org.apache.flink.streaming.runtime.tasks.ProcessingTimeCallback) OneInputStreamTaskTestHarness(org.apache.flink.streaming.runtime.tasks.OneInputStreamTaskTestHarness) StreamConfig(org.apache.flink.streaming.api.graph.StreamConfig) StreamMap(org.apache.flink.streaming.api.operators.StreamMap) Test(org.junit.Test)

Aggregations

ProcessingTimeCallback (org.apache.flink.streaming.runtime.tasks.ProcessingTimeCallback)2 Collection (java.util.Collection)1 TimeoutException (java.util.concurrent.TimeoutException)1 StreamConfig (org.apache.flink.streaming.api.graph.StreamConfig)1 StreamMap (org.apache.flink.streaming.api.operators.StreamMap)1 StreamElementQueueEntry (org.apache.flink.streaming.api.operators.async.queue.StreamElementQueueEntry)1 StreamRecordQueueEntry (org.apache.flink.streaming.api.operators.async.queue.StreamRecordQueueEntry)1 OneInputStreamTask (org.apache.flink.streaming.runtime.tasks.OneInputStreamTask)1 OneInputStreamTaskTestHarness (org.apache.flink.streaming.runtime.tasks.OneInputStreamTaskTestHarness)1 Test (org.junit.Test)1