use of org.apache.flink.streaming.api.windowing.triggers.TriggerResult in project flink by apache.
the class TriggerTestHarness method advanceWatermark.
/**
* Advanced the watermark and processes any timers that fire because of this. The
* window and {@link TriggerResult} for each firing are returned.
*/
public Collection<Tuple2<W, TriggerResult>> advanceWatermark(long time) throws Exception {
Collection<TestInternalTimerService.Timer<Integer, W>> firedTimers = internalTimerService.advanceWatermark(time);
Collection<Tuple2<W, TriggerResult>> result = new ArrayList<>();
for (TestInternalTimerService.Timer<Integer, W> timer : firedTimers) {
TriggerResult triggerResult = invokeOnEventTime(timer);
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 testWindowsAreMergedEagerly.
/**
* Verify that windows are merged eagerly, if possible.
*/
public void testWindowsAreMergedEagerly(final TimeDomainAdaptor timeAdaptor) throws Exception {
// in this test we only have one state window and windows are eagerly
// merged into the first window
MergingWindowAssigner<Integer, TimeWindow> mockAssigner = mockMergingAssigner();
timeAdaptor.setIsEventTime(mockAssigner);
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();
timeAdaptor.advanceTime(testHarness, Long.MIN_VALUE);
assertEquals(0, testHarness.extractOutputStreamRecords().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];
// don't intefere with cleanup timers
timeAdaptor.registerTimer(context, 0L);
context.getPartitionedState(valueStateDescriptor).update("hello");
return TriggerResult.CONTINUE;
}
}).when(mockTrigger).onElement(Matchers.<Integer>anyObject(), anyLong(), anyTimeWindow(), anyTriggerContext());
doAnswer(new Answer<TriggerResult>() {
@Override
public TriggerResult answer(InvocationOnMock invocation) throws Exception {
Trigger.OnMergeContext context = (Trigger.OnMergeContext) invocation.getArguments()[1];
// don't intefere with cleanup timers
timeAdaptor.registerTimer(context, 0L);
context.getPartitionedState(valueStateDescriptor).update("hello");
return TriggerResult.CONTINUE;
}
}).when(mockTrigger).onMerge(anyTimeWindow(), anyOnMergeContext());
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Exception {
Trigger.TriggerContext context = (Trigger.TriggerContext) invocation.getArguments()[1];
timeAdaptor.deleteTimer(context, 0L);
context.getPartitionedState(valueStateDescriptor).clear();
return null;
}
}).when(mockTrigger).clear(anyTimeWindow(), anyTriggerContext());
when(mockAssigner.assignWindows(anyInt(), anyLong(), anyAssignerContext())).thenReturn(Arrays.asList(new TimeWindow(0, 2)));
testHarness.processElement(new StreamRecord<>(0, 0L));
// window state plus trigger state plus merging window set
assertEquals(3, testHarness.numKeyedStateEntries());
// timer and GC timer
assertEquals(2, timeAdaptor.numTimers(testHarness));
when(mockAssigner.assignWindows(anyInt(), anyLong(), anyAssignerContext())).thenReturn(Arrays.asList(new TimeWindow(2, 4)));
shouldMergeWindows(mockAssigner, Lists.newArrayList(new TimeWindow(0, 2), new TimeWindow(2, 4)), Lists.newArrayList(new TimeWindow(0, 2), new TimeWindow(2, 4)), new TimeWindow(0, 4));
// don't register a timer or update state in onElement, this checks
// whether onMerge does correctly set those things
doAnswer(new Answer<TriggerResult>() {
@Override
public TriggerResult answer(InvocationOnMock invocation) throws Exception {
return TriggerResult.CONTINUE;
}
}).when(mockTrigger).onElement(Matchers.<Integer>anyObject(), anyLong(), anyTimeWindow(), anyTriggerContext());
testHarness.processElement(new StreamRecord<>(0, 0L));
verify(mockTrigger).onMerge(eq(new TimeWindow(0, 4)), anyOnMergeContext());
assertEquals(3, testHarness.numKeyedStateEntries());
assertEquals(2, timeAdaptor.numTimers(testHarness));
}
use of org.apache.flink.streaming.api.windowing.triggers.TriggerResult in project flink by apache.
the class WindowOperatorContractTest method testProcessingElementsWithinAllowedLateness.
@Test
public void testProcessingElementsWithinAllowedLateness() 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());
// 20 is just at the limit, window.maxTime() is 1 and allowed lateness is 20
testHarness.processWatermark(new Watermark(20));
testHarness.processElement(new StreamRecord<>(0, 0L));
verify(mockWindowFunction, times(1)).apply(eq(0), eq(new TimeWindow(0, 2)), intIterable(0), WindowOperatorContractTest.<Void>anyCollector());
// clear is only called at cleanup time/GC time
verify(mockTrigger, never()).clear(anyTimeWindow(), anyTriggerContext());
// FIRE should not purge contents
// window contents plus trigger state
assertEquals(1, testHarness.numKeyedStateEntries());
// just the GC timer
assertEquals(1, testHarness.numEventTimeTimers());
}
use of org.apache.flink.streaming.api.windowing.triggers.TriggerResult in project flink by apache.
the class WindowTranslationTest method testMergingAssignerWithNonMergingTriggerFails.
@Test
public void testMergingAssignerWithNonMergingTriggerFails() throws Exception {
// verify that we check for trigger compatibility
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
WindowedStream<String, String, TimeWindow> windowedStream = env.fromElements("Hello", "Ciao").keyBy(new KeySelector<String, String>() {
private static final long serialVersionUID = 598309916882894293L;
@Override
public String getKey(String value) throws Exception {
return value;
}
}).window(EventTimeSessionWindows.withGap(Time.seconds(5)));
try {
windowedStream.trigger(new Trigger<String, TimeWindow>() {
private static final long serialVersionUID = 6558046711583024443L;
@Override
public TriggerResult onElement(String element, long timestamp, TimeWindow window, TriggerContext ctx) throws Exception {
return null;
}
@Override
public TriggerResult onProcessingTime(long time, TimeWindow window, TriggerContext ctx) throws Exception {
return null;
}
@Override
public TriggerResult onEventTime(long time, TimeWindow window, TriggerContext ctx) throws Exception {
return null;
}
@Override
public boolean canMerge() {
return false;
}
@Override
public void clear(TimeWindow window, TriggerContext ctx) throws Exception {
}
});
} catch (UnsupportedOperationException e) {
// use a catch to ensure that the exception is thrown by the fold
return;
}
fail("The trigger call should fail.");
}
use of org.apache.flink.streaming.api.windowing.triggers.TriggerResult in project flink by apache.
the class WindowOperator method onEventTime.
@Override
public void onEventTime(InternalTimer<K, W> timer) throws Exception {
context.key = timer.getKey();
context.window = timer.getNamespace();
MergingWindowSet<W> mergingWindows;
if (windowAssigner instanceof MergingWindowAssigner) {
mergingWindows = getMergingWindowSet();
W stateWindow = mergingWindows.getStateWindow(context.window);
if (stateWindow == null) {
// window and therefore the Trigger state, however, so nothing to do.
return;
} else {
windowState.setCurrentNamespace(stateWindow);
}
} else {
windowState.setCurrentNamespace(context.window);
mergingWindows = null;
}
ACC contents = null;
if (windowState != null) {
contents = windowState.get();
}
if (contents != null) {
TriggerResult triggerResult = context.onEventTime(timer.getTimestamp());
if (triggerResult.isFire()) {
emitWindowContents(context.window, contents);
}
if (triggerResult.isPurge()) {
windowState.clear();
}
}
if (windowAssigner.isEventTime() && isCleanupTime(context.window, timer.getTimestamp())) {
clearAllState(context.window, windowState, mergingWindows);
}
if (mergingWindows != null) {
// need to make sure to update the merging state in state
mergingWindows.persist();
}
}
Aggregations