Search in sources :

Example 16 with TriggerResult

use of org.apache.flink.streaming.api.windowing.triggers.TriggerResult in project flink by apache.

the class TriggerTestHarness method advanceProcessingTime.

/**
	 * Advanced processing time and processes any timers that fire because of this. The
	 * window and {@link TriggerResult} for each firing are returned.
	 */
public Collection<Tuple2<W, TriggerResult>> advanceProcessingTime(long time) throws Exception {
    Collection<TestInternalTimerService.Timer<Integer, W>> firedTimers = internalTimerService.advanceProcessingTime(time);
    Collection<Tuple2<W, TriggerResult>> result = new ArrayList<>();
    for (TestInternalTimerService.Timer<Integer, W> timer : firedTimers) {
        TestTriggerContext<Integer, W> triggerContext = new TestTriggerContext<>(KEY, timer.getNamespace(), internalTimerService, stateBackend, windowSerializer);
        TriggerResult triggerResult = trigger.onProcessingTime(timer.getTimestamp(), timer.getNamespace(), triggerContext);
        result.add(new Tuple2<>(timer.getNamespace(), triggerResult));
    }
    return result;
}
Also used : TestInternalTimerService(org.apache.flink.streaming.api.operators.TestInternalTimerService) ArrayList(java.util.ArrayList) Tuple2(org.apache.flink.api.java.tuple.Tuple2) TriggerResult(org.apache.flink.streaming.api.windowing.triggers.TriggerResult)

Example 17 with TriggerResult

use of org.apache.flink.streaming.api.windowing.triggers.TriggerResult in project flink by apache.

the class WindowOperatorContractTest method testStateSnapshotAndRestore.

@Test
public void testStateSnapshotAndRestore() throws Exception {
    MergingWindowAssigner<Integer, TimeWindow> mockAssigner = mockMergingAssigner();
    Trigger<Integer, TimeWindow> mockTrigger = mockTrigger();
    InternalWindowFunction<Iterable<Integer>, Void, Integer, TimeWindow> mockWindowFunction = mockWindowFunction();
    KeyedOneInputStreamOperatorTestHarness<Integer, Integer, Void> testHarness = createWindowOperator(mockAssigner, mockTrigger, 0L, intListDescriptor, mockWindowFunction);
    testHarness.open();
    when(mockAssigner.assignWindows(anyInt(), anyLong(), anyAssignerContext())).thenReturn(Arrays.asList(new TimeWindow(2, 4), new TimeWindow(0, 2)));
    assertEquals(0, testHarness.getOutput().size());
    assertEquals(0, testHarness.numKeyedStateEntries());
    doAnswer(new Answer<TriggerResult>() {

        @Override
        public TriggerResult answer(InvocationOnMock invocation) throws Exception {
            Trigger.TriggerContext context = (Trigger.TriggerContext) invocation.getArguments()[3];
            context.registerEventTimeTimer(0L);
            context.getPartitionedState(valueStateDescriptor).update("hello");
            return TriggerResult.CONTINUE;
        }
    }).when(mockTrigger).onElement(Matchers.<Integer>anyObject(), anyLong(), anyTimeWindow(), anyTriggerContext());
    shouldFireAndPurgeOnEventTime(mockTrigger);
    testHarness.processElement(new StreamRecord<>(0, 0L));
    // window-contents and trigger state for two windows plus merging window set
    assertEquals(5, testHarness.numKeyedStateEntries());
    // timers/gc timers for two windows
    assertEquals(4, testHarness.numEventTimeTimers());
    OperatorStateHandles snapshot = testHarness.snapshot(0, 0);
    // restore
    mockAssigner = mockMergingAssigner();
    mockTrigger = mockTrigger();
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Exception {
            Trigger.TriggerContext context = (Trigger.TriggerContext) invocation.getArguments()[1];
            context.deleteEventTimeTimer(0L);
            context.getPartitionedState(valueStateDescriptor).clear();
            return null;
        }
    }).when(mockTrigger).clear(anyTimeWindow(), anyTriggerContext());
    // only fire on the timestamp==0L timers, not the gc timers
    when(mockTrigger.onEventTime(eq(0L), Matchers.<TimeWindow>any(), anyTriggerContext())).thenReturn(TriggerResult.FIRE);
    mockWindowFunction = mockWindowFunction();
    testHarness = createWindowOperator(mockAssigner, mockTrigger, 0L, intListDescriptor, mockWindowFunction);
    testHarness.setup();
    testHarness.initializeState(snapshot);
    testHarness.open();
    assertEquals(0, testHarness.extractOutputStreamRecords().size());
    // verify that we still have all the state/timers/merging window set
    assertEquals(5, testHarness.numKeyedStateEntries());
    // timers/gc timers for two windows
    assertEquals(4, testHarness.numEventTimeTimers());
    verify(mockTrigger, never()).clear(anyTimeWindow(), anyTriggerContext());
    testHarness.processWatermark(new Watermark(20L));
    verify(mockTrigger, times(2)).clear(anyTimeWindow(), anyTriggerContext());
    verify(mockWindowFunction, times(2)).apply(eq(0), anyTimeWindow(), anyIntIterable(), WindowOperatorContractTest.<Void>anyCollector());
    verify(mockWindowFunction, times(1)).apply(eq(0), eq(new TimeWindow(0, 2)), intIterable(0), WindowOperatorContractTest.<Void>anyCollector());
    verify(mockWindowFunction, times(1)).apply(eq(0), eq(new TimeWindow(2, 4)), intIterable(0), WindowOperatorContractTest.<Void>anyCollector());
    // it's also called for the cleanup timers
    verify(mockTrigger, times(4)).onEventTime(anyLong(), anyTimeWindow(), anyTriggerContext());
    verify(mockTrigger, times(1)).onEventTime(eq(0L), eq(new TimeWindow(0, 2)), anyTriggerContext());
    verify(mockTrigger, times(1)).onEventTime(eq(0L), eq(new TimeWindow(2, 4)), anyTriggerContext());
    assertEquals(0, testHarness.numKeyedStateEntries());
    assertEquals(0, testHarness.numEventTimeTimers());
}
Also used : TimeWindow(org.apache.flink.streaming.api.windowing.windows.TimeWindow) Trigger(org.apache.flink.streaming.api.windowing.triggers.Trigger) OperatorStateHandles(org.apache.flink.streaming.runtime.tasks.OperatorStateHandles) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TriggerResult(org.apache.flink.streaming.api.windowing.triggers.TriggerResult) Watermark(org.apache.flink.streaming.api.watermark.Watermark) Test(org.junit.Test)

Example 18 with TriggerResult

use of org.apache.flink.streaming.api.windowing.triggers.TriggerResult in project flink by apache.

the class WindowOperatorContractTest method testLateWindowDropping.

@Test
public void testLateWindowDropping() throws Exception {
    WindowAssigner<Integer, TimeWindow> mockAssigner = mockTimeWindowAssigner();
    Trigger<Integer, TimeWindow> mockTrigger = mockTrigger();
    InternalWindowFunction<Iterable<Integer>, Void, Integer, TimeWindow> mockWindowFunction = mockWindowFunction();
    KeyedOneInputStreamOperatorTestHarness<Integer, Integer, Void> testHarness = createWindowOperator(mockAssigner, mockTrigger, 20L, intListDescriptor, mockWindowFunction);
    testHarness.open();
    when(mockAssigner.assignWindows(anyInt(), anyLong(), anyAssignerContext())).thenReturn(Arrays.asList(new TimeWindow(0, 2)));
    assertEquals(0, testHarness.getOutput().size());
    assertEquals(0, testHarness.numKeyedStateEntries());
    doAnswer(new Answer<TriggerResult>() {

        @Override
        public TriggerResult answer(InvocationOnMock invocation) throws Exception {
            return TriggerResult.FIRE;
        }
    }).when(mockTrigger).onElement(Matchers.<Integer>anyObject(), anyLong(), anyTimeWindow(), anyTriggerContext());
    // window.maxTime() == 1 plus 20L of allowed lateness
    testHarness.processWatermark(new Watermark(21));
    testHarness.processElement(new StreamRecord<>(0, 0L));
    // there should be nothing
    assertEquals(0, testHarness.numKeyedStateEntries());
    assertEquals(0, testHarness.numEventTimeTimers());
    assertEquals(0, testHarness.numProcessingTimeTimers());
    // there should be two elements now
    assertEquals(0, testHarness.extractOutputStreamRecords().size());
}
Also used : TimeWindow(org.apache.flink.streaming.api.windowing.windows.TimeWindow) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TriggerResult(org.apache.flink.streaming.api.windowing.triggers.TriggerResult) Watermark(org.apache.flink.streaming.api.watermark.Watermark) Test(org.junit.Test)

Aggregations

TriggerResult (org.apache.flink.streaming.api.windowing.triggers.TriggerResult)18 TimeWindow (org.apache.flink.streaming.api.windowing.windows.TimeWindow)10 Test (org.junit.Test)8 MergingWindowAssigner (org.apache.flink.streaming.api.windowing.assigners.MergingWindowAssigner)6 StreamRecord (org.apache.flink.streaming.runtime.streamrecord.StreamRecord)6 InvocationOnMock (org.mockito.invocation.InvocationOnMock)6 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)4 Trigger (org.apache.flink.streaming.api.windowing.triggers.Trigger)4 Watermark (org.apache.flink.streaming.api.watermark.Watermark)3 ArrayList (java.util.ArrayList)2 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)2 TestInternalTimerService (org.apache.flink.streaming.api.operators.TestInternalTimerService)2 IOException (java.io.IOException)1 KeySelector (org.apache.flink.api.java.functions.KeySelector)1 PurgingTrigger (org.apache.flink.streaming.api.windowing.triggers.PurgingTrigger)1 WindowOperatorContractTest.anyTimeWindow (org.apache.flink.streaming.runtime.operators.windowing.WindowOperatorContractTest.anyTimeWindow)1 WindowOperatorContractTest.anyTriggerContext (org.apache.flink.streaming.runtime.operators.windowing.WindowOperatorContractTest.anyTriggerContext)1 OperatorStateHandles (org.apache.flink.streaming.runtime.tasks.OperatorStateHandles)1