use of org.apache.flink.streaming.api.windowing.windows.TimeWindow in project flink by apache.
the class EventTimeSessionWindowsTest method testMergeSinglePointWindow.
@Test
public void testMergeSinglePointWindow() {
MergingWindowAssigner.MergeCallback callback = mock(MergingWindowAssigner.MergeCallback.class);
EventTimeSessionWindows assigner = EventTimeSessionWindows.withGap(Time.milliseconds(5000));
assigner.mergeWindows(Lists.newArrayList(new TimeWindow(0, 0)), callback);
verify(callback, never()).merge(anyCollection(), Matchers.anyObject());
}
use of org.apache.flink.streaming.api.windowing.windows.TimeWindow in project flink by apache.
the class EventTimeTriggerTest method testClear.
/**
* Verify that clear() does not leak across windows.
*/
@Test
public void testClear() throws Exception {
TriggerTestHarness<Object, TimeWindow> testHarness = new TriggerTestHarness<>(EventTimeTrigger.create(), new TimeWindow.Serializer());
assertEquals(TriggerResult.CONTINUE, testHarness.processElement(new StreamRecord<Object>(1), new TimeWindow(0, 2)));
assertEquals(TriggerResult.CONTINUE, testHarness.processElement(new StreamRecord<Object>(1), new TimeWindow(2, 4)));
assertEquals(0, testHarness.numStateEntries());
assertEquals(0, testHarness.numProcessingTimeTimers());
assertEquals(2, testHarness.numEventTimeTimers());
assertEquals(1, testHarness.numEventTimeTimers(new TimeWindow(0, 2)));
assertEquals(1, testHarness.numEventTimeTimers(new TimeWindow(2, 4)));
testHarness.clearTriggerState(new TimeWindow(2, 4));
assertEquals(0, testHarness.numStateEntries());
assertEquals(0, testHarness.numProcessingTimeTimers());
assertEquals(1, testHarness.numEventTimeTimers());
assertEquals(1, testHarness.numEventTimeTimers(new TimeWindow(0, 2)));
assertEquals(0, testHarness.numEventTimeTimers(new TimeWindow(2, 4)));
testHarness.clearTriggerState(new TimeWindow(0, 2));
assertEquals(0, testHarness.numStateEntries());
assertEquals(0, testHarness.numProcessingTimeTimers());
assertEquals(0, testHarness.numEventTimeTimers());
}
use of org.apache.flink.streaming.api.windowing.windows.TimeWindow in project flink by apache.
the class EventTimeTriggerTest method testMergingWindows.
@Test
public void testMergingWindows() throws Exception {
TriggerTestHarness<Object, TimeWindow> testHarness = new TriggerTestHarness<>(EventTimeTrigger.create(), new TimeWindow.Serializer());
assertTrue(EventTimeTrigger.create().canMerge());
assertEquals(TriggerResult.CONTINUE, testHarness.processElement(new StreamRecord<Object>(1), new TimeWindow(0, 2)));
assertEquals(TriggerResult.CONTINUE, testHarness.processElement(new StreamRecord<Object>(1), new TimeWindow(2, 4)));
assertEquals(0, testHarness.numStateEntries());
assertEquals(0, testHarness.numProcessingTimeTimers());
assertEquals(2, testHarness.numEventTimeTimers());
assertEquals(1, testHarness.numEventTimeTimers(new TimeWindow(0, 2)));
assertEquals(1, testHarness.numEventTimeTimers(new TimeWindow(2, 4)));
testHarness.mergeWindows(new TimeWindow(0, 4), Lists.newArrayList(new TimeWindow(0, 2), new TimeWindow(2, 4)));
assertEquals(0, testHarness.numStateEntries());
assertEquals(0, testHarness.numProcessingTimeTimers());
assertEquals(1, testHarness.numEventTimeTimers());
assertEquals(0, testHarness.numEventTimeTimers(new TimeWindow(0, 2)));
assertEquals(0, testHarness.numEventTimeTimers(new TimeWindow(2, 4)));
assertEquals(1, testHarness.numEventTimeTimers(new TimeWindow(0, 4)));
assertEquals(TriggerResult.FIRE, testHarness.advanceWatermark(4, new TimeWindow(0, 4)));
assertEquals(0, testHarness.numStateEntries());
assertEquals(0, testHarness.numProcessingTimeTimers());
assertEquals(0, testHarness.numEventTimeTimers());
}
use of org.apache.flink.streaming.api.windowing.windows.TimeWindow in project flink by apache.
the class WindowCheckpointingITCase method testSlidingProcessingTimeWindow.
@Test
public void testSlidingProcessingTimeWindow() {
final int NUM_ELEMENTS = 3000;
FailingSource.reset();
try {
StreamExecutionEnvironment env = StreamExecutionEnvironment.createRemoteEnvironment("localhost", cluster.getLeaderRPCPort());
env.setParallelism(PARALLELISM);
env.setStreamTimeCharacteristic(timeCharacteristic);
env.getConfig().setAutoWatermarkInterval(10);
env.enableCheckpointing(100);
env.setRestartStrategy(RestartStrategies.fixedDelayRestart(3, 0));
env.getConfig().disableSysoutLogging();
env.addSource(new FailingSource(NUM_ELEMENTS, NUM_ELEMENTS / 3)).rebalance().keyBy(0).timeWindow(Time.of(150, MILLISECONDS), Time.of(50, MILLISECONDS)).apply(new RichWindowFunction<Tuple2<Long, IntType>, Tuple2<Long, IntType>, Tuple, TimeWindow>() {
private boolean open = false;
@Override
public void open(Configuration parameters) {
assertEquals(PARALLELISM, getRuntimeContext().getNumberOfParallelSubtasks());
open = true;
}
@Override
public void apply(Tuple tuple, TimeWindow window, Iterable<Tuple2<Long, IntType>> values, Collector<Tuple2<Long, IntType>> out) {
// validate that the function has been opened properly
assertTrue(open);
for (Tuple2<Long, IntType> value : values) {
assertEquals(value.f0.intValue(), value.f1.value);
out.collect(new Tuple2<Long, IntType>(value.f0, new IntType(1)));
}
}
}).addSink(new ValidatingSink(NUM_ELEMENTS, 3)).setParallelism(1);
tryExecute(env, "Tumbling Window Test");
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of org.apache.flink.streaming.api.windowing.windows.TimeWindow in project flink by apache.
the class SessionWindowITCase method runTest.
private void runTest(SourceFunction<SessionEvent<Integer, TestEventPayload>> dataSource, WindowFunction<SessionEvent<Integer, TestEventPayload>, String, Tuple, TimeWindow> windowFunction) throws Exception {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);
WindowedStream<SessionEvent<Integer, TestEventPayload>, Tuple, TimeWindow> windowedStream = env.addSource(dataSource).keyBy("sessionKey").window(EventTimeSessionWindows.withGap(Time.milliseconds(MAX_SESSION_EVENT_GAP_MS)));
if (ALLOWED_LATENESS_MS != Long.MAX_VALUE) {
windowedStream = windowedStream.allowedLateness(Time.milliseconds(ALLOWED_LATENESS_MS));
}
if (PURGE_WINDOW_ON_FIRE) {
windowedStream = windowedStream.trigger(PurgingTrigger.of(EventTimeTrigger.create()));
}
windowedStream.apply(windowFunction).print();
JobExecutionResult result = env.execute();
// check that overall event counts match with our expectations. remember that late events within lateness will
// each trigger a window!
Assert.assertEquals((LATE_EVENTS_PER_SESSION + 1) * NUMBER_OF_SESSIONS * EVENTS_PER_SESSION, (long) result.getAccumulatorResult(SESSION_COUNTER_ON_TIME_KEY));
Assert.assertEquals(NUMBER_OF_SESSIONS * (LATE_EVENTS_PER_SESSION * (LATE_EVENTS_PER_SESSION + 1) / 2), (long) result.getAccumulatorResult(SESSION_COUNTER_LATE_KEY));
}
Aggregations