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;
}
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());
}
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());
}
Aggregations