Java Code Examples for org.apache.flink.streaming.api.windowing.assigners.EventTimeSessionWindows#withGap()
The following examples show how to use
org.apache.flink.streaming.api.windowing.assigners.EventTimeSessionWindows#withGap() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MergingWindowSetTest.java From Flink-CEPplus with Apache License 2.0 | 6 votes |
@Test public void testPersistOnlyIfHaveUpdates() throws Exception { @SuppressWarnings("unchecked") ListState<Tuple2<TimeWindow, TimeWindow>> mockState = mock(ListState.class); when(mockState.get()).thenReturn(Lists.newArrayList( new Tuple2<>(new TimeWindow(17, 42), new TimeWindow(42, 17)), new Tuple2<>(new TimeWindow(1, 2), new TimeWindow(3, 4)) )); MergingWindowSet<TimeWindow> windowSet = new MergingWindowSet<>(EventTimeSessionWindows.withGap(Time.milliseconds(3)), mockState); assertEquals(new TimeWindow(42, 17), windowSet.getStateWindow(new TimeWindow(17, 42))); assertEquals(new TimeWindow(3, 4), windowSet.getStateWindow(new TimeWindow(1, 2))); windowSet.persist(); verify(mockState, times(0)).add(Matchers.<Tuple2<TimeWindow, TimeWindow>>anyObject()); }
Example 2
Source File: EventTimeSessionWindowsTest.java From flink with Apache License 2.0 | 6 votes |
@Test public void testMergeCoveringWindow() { MergingWindowAssigner.MergeCallback callback = mock(MergingWindowAssigner.MergeCallback.class); EventTimeSessionWindows assigner = EventTimeSessionWindows.withGap(Time.milliseconds(5000)); assigner.mergeWindows( Lists.newArrayList( new TimeWindow(1, 1), new TimeWindow(0, 2), new TimeWindow(4, 7), new TimeWindow(5, 6)), callback); verify(callback, times(1)).merge( (Collection<TimeWindow>) argThat(containsInAnyOrder(new TimeWindow(1, 1), new TimeWindow(0, 2))), eq(new TimeWindow(0, 2))); verify(callback, times(1)).merge( (Collection<TimeWindow>) argThat(containsInAnyOrder(new TimeWindow(5, 6), new TimeWindow(4, 7))), eq(new TimeWindow(4, 7))); verify(callback, times(2)).merge(anyCollection(), Matchers.anyObject()); }
Example 3
Source File: MergingWindowSetTest.java From flink with Apache License 2.0 | 6 votes |
/** * Test adding a new window that is identical to an existing window. This should not cause * a merge. */ @Test public void testAddingIdenticalWindows() throws Exception { @SuppressWarnings("unchecked") ListState<Tuple2<TimeWindow, TimeWindow>> mockState = mock(ListState.class); MergingWindowSet<TimeWindow> windowSet = new MergingWindowSet<>(EventTimeSessionWindows.withGap(Time.milliseconds(3)), mockState); TestingMergeFunction mergeFunction = new TestingMergeFunction(); mergeFunction.reset(); assertEquals(new TimeWindow(1, 2), windowSet.addWindow(new TimeWindow(1, 2), mergeFunction)); assertFalse(mergeFunction.hasMerged()); assertEquals(new TimeWindow(1, 2), windowSet.getStateWindow(new TimeWindow(1, 2))); mergeFunction.reset(); assertEquals(new TimeWindow(1, 2), windowSet.addWindow(new TimeWindow(1, 2), mergeFunction)); assertFalse(mergeFunction.hasMerged()); assertEquals(new TimeWindow(1, 2), windowSet.getStateWindow(new TimeWindow(1, 2))); }
Example 4
Source File: MergingWindowSetTest.java From flink with Apache License 2.0 | 5 votes |
/** * Test merging of a large new window that covers multiple existing windows. */ @Test public void testMergeLargeWindowCoveringMultipleWindows() throws Exception { @SuppressWarnings("unchecked") ListState<Tuple2<TimeWindow, TimeWindow>> mockState = mock(ListState.class); MergingWindowSet<TimeWindow> windowSet = new MergingWindowSet<>(EventTimeSessionWindows.withGap(Time.milliseconds(3)), mockState); TestingMergeFunction mergeFunction = new TestingMergeFunction(); // add several non-overlapping initial windows mergeFunction.reset(); assertEquals(new TimeWindow(1, 3), windowSet.addWindow(new TimeWindow(1, 3), mergeFunction)); assertFalse(mergeFunction.hasMerged()); assertEquals(new TimeWindow(1, 3), windowSet.getStateWindow(new TimeWindow(1, 3))); mergeFunction.reset(); assertEquals(new TimeWindow(5, 8), windowSet.addWindow(new TimeWindow(5, 8), mergeFunction)); assertFalse(mergeFunction.hasMerged()); assertEquals(new TimeWindow(5, 8), windowSet.getStateWindow(new TimeWindow(5, 8))); mergeFunction.reset(); assertEquals(new TimeWindow(10, 13), windowSet.addWindow(new TimeWindow(10, 13), mergeFunction)); assertFalse(mergeFunction.hasMerged()); assertEquals(new TimeWindow(10, 13), windowSet.getStateWindow(new TimeWindow(10, 13))); // add a new window that completely covers the existing windows mergeFunction.reset(); assertEquals(new TimeWindow(0, 13), windowSet.addWindow(new TimeWindow(0, 13), mergeFunction)); assertTrue(mergeFunction.hasMerged()); assertThat(mergeFunction.mergedStateWindows(), anyOf( containsInAnyOrder(new TimeWindow(0, 3), new TimeWindow(5, 8)), containsInAnyOrder(new TimeWindow(0, 3), new TimeWindow(10, 13)), containsInAnyOrder(new TimeWindow(5, 8), new TimeWindow(10, 13)))); assertThat(windowSet.getStateWindow(new TimeWindow(0, 13)), anyOf(is(new TimeWindow(1, 3)), is(new TimeWindow(5, 8)), is(new TimeWindow(10, 13)))); }
Example 5
Source File: MergingWindowSetTest.java From flink with Apache License 2.0 | 5 votes |
/** * Test merging of a large new window that covers multiple existing windows. */ @Test public void testMergeLargeWindowCoveringMultipleWindows() throws Exception { @SuppressWarnings("unchecked") ListState<Tuple2<TimeWindow, TimeWindow>> mockState = mock(ListState.class); MergingWindowSet<TimeWindow> windowSet = new MergingWindowSet<>(EventTimeSessionWindows.withGap(Time.milliseconds(3)), mockState); TestingMergeFunction mergeFunction = new TestingMergeFunction(); // add several non-overlapping initial windows mergeFunction.reset(); assertEquals(new TimeWindow(1, 3), windowSet.addWindow(new TimeWindow(1, 3), mergeFunction)); assertFalse(mergeFunction.hasMerged()); assertEquals(new TimeWindow(1, 3), windowSet.getStateWindow(new TimeWindow(1, 3))); mergeFunction.reset(); assertEquals(new TimeWindow(5, 8), windowSet.addWindow(new TimeWindow(5, 8), mergeFunction)); assertFalse(mergeFunction.hasMerged()); assertEquals(new TimeWindow(5, 8), windowSet.getStateWindow(new TimeWindow(5, 8))); mergeFunction.reset(); assertEquals(new TimeWindow(10, 13), windowSet.addWindow(new TimeWindow(10, 13), mergeFunction)); assertFalse(mergeFunction.hasMerged()); assertEquals(new TimeWindow(10, 13), windowSet.getStateWindow(new TimeWindow(10, 13))); // add a new window that completely covers the existing windows mergeFunction.reset(); assertEquals(new TimeWindow(0, 13), windowSet.addWindow(new TimeWindow(0, 13), mergeFunction)); assertTrue(mergeFunction.hasMerged()); assertThat(mergeFunction.mergedStateWindows(), anyOf( containsInAnyOrder(new TimeWindow(0, 3), new TimeWindow(5, 8)), containsInAnyOrder(new TimeWindow(0, 3), new TimeWindow(10, 13)), containsInAnyOrder(new TimeWindow(5, 8), new TimeWindow(10, 13)))); assertThat(windowSet.getStateWindow(new TimeWindow(0, 13)), anyOf(is(new TimeWindow(1, 3)), is(new TimeWindow(5, 8)), is(new TimeWindow(10, 13)))); }
Example 6
Source File: MergingWindowSetTest.java From flink with Apache License 2.0 | 5 votes |
@Test public void testRestoreFromState() throws Exception { @SuppressWarnings("unchecked") ListState<Tuple2<TimeWindow, TimeWindow>> mockState = mock(ListState.class); when(mockState.get()).thenReturn(Lists.newArrayList( new Tuple2<>(new TimeWindow(17, 42), new TimeWindow(42, 17)), new Tuple2<>(new TimeWindow(1, 2), new TimeWindow(3, 4)) )); MergingWindowSet<TimeWindow> windowSet = new MergingWindowSet<>(EventTimeSessionWindows.withGap(Time.milliseconds(3)), mockState); assertEquals(new TimeWindow(42, 17), windowSet.getStateWindow(new TimeWindow(17, 42))); assertEquals(new TimeWindow(3, 4), windowSet.getStateWindow(new TimeWindow(1, 2))); }
Example 7
Source File: WindowOperatorMigrationTest.java From flink with Apache License 2.0 | 5 votes |
/** * Manually run this to write binary snapshot data. */ @Ignore @Test public void writeSessionWindowsWithCountTriggerInMintConditionSnapshot() throws Exception { final int sessionSize = 3; ListStateDescriptor<Tuple2<String, Integer>> stateDesc = new ListStateDescriptor<>("window-contents", STRING_INT_TUPLE.createSerializer(new ExecutionConfig())); WindowOperator<String, Tuple2<String, Integer>, Iterable<Tuple2<String, Integer>>, Tuple3<String, Long, Long>, TimeWindow> operator = new WindowOperator<>( EventTimeSessionWindows.withGap(Time.seconds(sessionSize)), new TimeWindow.Serializer(), new TupleKeySelector<String>(), BasicTypeInfo.STRING_TYPE_INFO.createSerializer(new ExecutionConfig()), stateDesc, new InternalIterableWindowFunction<>(new SessionWindowFunction()), PurgingTrigger.of(CountTrigger.of(4)), 0, null /* late data output tag */); OneInputStreamOperatorTestHarness<Tuple2<String, Integer>, Tuple3<String, Long, Long>> testHarness = new KeyedOneInputStreamOperatorTestHarness<>(operator, new TupleKeySelector<>(), BasicTypeInfo.STRING_TYPE_INFO); testHarness.setup(); testHarness.open(); // do snapshot and save to file OperatorSubtaskState snapshot = testHarness.snapshot(0, 0); OperatorSnapshotUtil.writeStateHandle( snapshot, "src/test/resources/win-op-migration-test-session-with-stateful-trigger-mint-flink" + flinkGenerateSavepointVersion + "-snapshot"); testHarness.close(); }
Example 8
Source File: WindowOperatorTest.java From Flink-CEPplus with Apache License 2.0 | 5 votes |
@Test public void testCleanupTimerWithEmptyListStateForSessionWindows() throws Exception { final int gapSize = 3; final long lateness = 10; ListStateDescriptor<Tuple2<String, Integer>> windowStateDesc = new ListStateDescriptor<>("window-contents", STRING_INT_TUPLE.createSerializer(new ExecutionConfig())); WindowOperator<String, Tuple2<String, Integer>, Iterable<Tuple2<String, Integer>>, Tuple2<String, Integer>, TimeWindow> operator = new WindowOperator<>( EventTimeSessionWindows.withGap(Time.seconds(gapSize)), new TimeWindow.Serializer(), new TupleKeySelector(), BasicTypeInfo.STRING_TYPE_INFO.createSerializer(new ExecutionConfig()), windowStateDesc, new InternalIterableWindowFunction<>(new PassThroughFunction()), EventTimeTrigger.create(), lateness, null /* late data output tag */); OneInputStreamOperatorTestHarness<Tuple2<String, Integer>, Tuple2<String, Integer>> testHarness = createTestHarness(operator); testHarness.open(); ConcurrentLinkedQueue<Object> expected = new ConcurrentLinkedQueue<>(); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 1000)); testHarness.processWatermark(new Watermark(4998)); expected.add(new StreamRecord<>(new Tuple2<>("key2", 1), 3999)); expected.add(new Watermark(4998)); testHarness.processWatermark(new Watermark(14600)); expected.add(new Watermark(14600)); ConcurrentLinkedQueue<Object> actual = testHarness.getOutput(); TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expected, actual, new Tuple2ResultSortComparator()); testHarness.close(); }
Example 9
Source File: MergingWindowSetTest.java From flink with Apache License 2.0 | 5 votes |
@Test public void testRestoreFromState() throws Exception { @SuppressWarnings("unchecked") ListState<Tuple2<TimeWindow, TimeWindow>> mockState = mock(ListState.class); when(mockState.get()).thenReturn(Lists.newArrayList( new Tuple2<>(new TimeWindow(17, 42), new TimeWindow(42, 17)), new Tuple2<>(new TimeWindow(1, 2), new TimeWindow(3, 4)) )); MergingWindowSet<TimeWindow> windowSet = new MergingWindowSet<>(EventTimeSessionWindows.withGap(Time.milliseconds(3)), mockState); assertEquals(new TimeWindow(42, 17), windowSet.getStateWindow(new TimeWindow(17, 42))); assertEquals(new TimeWindow(3, 4), windowSet.getStateWindow(new TimeWindow(1, 2))); }
Example 10
Source File: EventTimeSessionWindowsTest.java From flink with Apache License 2.0 | 5 votes |
@Test public void testProperties() { EventTimeSessionWindows assigner = EventTimeSessionWindows.withGap(Time.seconds(5)); assertTrue(assigner.isEventTime()); assertEquals(new TimeWindow.Serializer(), assigner.getWindowSerializer(new ExecutionConfig())); assertThat(assigner.getDefaultTrigger(mock(StreamExecutionEnvironment.class)), instanceOf(EventTimeTrigger.class)); }
Example 11
Source File: WindowOperatorTest.java From flink with Apache License 2.0 | 4 votes |
@Test public void testNotSideOutputDueToLatenessSessionWithLateness() throws Exception { // same as testSideOutputDueToLatenessSessionWithLateness() but with an accumulating trigger, i.e. // one that does not return FIRE_AND_PURGE when firing but just FIRE. The expected // results are therefore slightly different. final int gapSize = 3; final long lateness = 10; ReducingStateDescriptor<Tuple2<String, Integer>> stateDesc = new ReducingStateDescriptor<>("window-contents", new SumReducer(), STRING_INT_TUPLE.createSerializer(new ExecutionConfig())); WindowOperator<String, Tuple2<String, Integer>, Tuple2<String, Integer>, Tuple3<String, Long, Long>, TimeWindow> operator = new WindowOperator<>( EventTimeSessionWindows.withGap(Time.seconds(gapSize)), new TimeWindow.Serializer(), new TupleKeySelector(), BasicTypeInfo.STRING_TYPE_INFO.createSerializer(new ExecutionConfig()), stateDesc, new InternalSingleValueWindowFunction<>(new ReducedSessionWindowFunction()), EventTimeTrigger.create(), lateness, lateOutputTag /* late data output tag */); OneInputStreamOperatorTestHarness<Tuple2<String, Integer>, Tuple3<String, Long, Long>> testHarness = createTestHarness(operator); testHarness.open(); ConcurrentLinkedQueue<Object> expected = new ConcurrentLinkedQueue<>(); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 1000)); testHarness.processWatermark(new Watermark(1999)); expected.add(new Watermark(1999)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 2000)); testHarness.processWatermark(new Watermark(4998)); expected.add(new Watermark(4998)); // this will not be sideoutput because the session we're adding two has maxTimestamp // after the current watermark testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 4500)); // new session testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 8500)); testHarness.processWatermark(new Watermark(7400)); expected.add(new Watermark(7400)); // this will merge the two sessions into one testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 7000)); testHarness.processWatermark(new Watermark(11501)); expected.add(new StreamRecord<>(new Tuple3<>("key2-5", 1000L, 11500L), 11499)); expected.add(new Watermark(11501)); // new session testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 11600)); testHarness.processWatermark(new Watermark(14600)); expected.add(new StreamRecord<>(new Tuple3<>("key2-1", 11600L, 14600L), 14599)); expected.add(new Watermark(14600)); // because of the small allowed lateness and because the trigger is accumulating // this will be merged into the session (11600-14600) and therefore will not // be sideoutput as late testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 10000)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 14500)); // adding ("key2", 1) extended the session to (10000-146000) for which // maxTimestamp <= currentWatermark. Therefore, we immediately get a firing // with the current version of EventTimeTrigger/EventTimeTriggerAccum expected.add(new StreamRecord<>(new Tuple3<>("key2-2", 10000L, 14600L), 14599)); ConcurrentLinkedQueue<Object> actual = testHarness.getOutput(); ConcurrentLinkedQueue<StreamRecord<Tuple2<String, Integer>>> sideActual = testHarness.getSideOutput( lateOutputTag); TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expected, actual, new Tuple3ResultSortComparator()); assertEquals(null, sideActual); testHarness.processWatermark(new Watermark(20000)); expected.add(new StreamRecord<>(new Tuple3<>("key2-3", 10000L, 17500L), 17499)); expected.add(new Watermark(20000)); testHarness.processWatermark(new Watermark(100000)); expected.add(new Watermark(100000)); actual = testHarness.getOutput(); sideActual = testHarness.getSideOutput(lateOutputTag); TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expected, actual, new Tuple3ResultSortComparator()); assertEquals(null, sideActual); testHarness.close(); }
Example 12
Source File: WindowOperatorMigrationTest.java From flink with Apache License 2.0 | 4 votes |
/** * Manually run this to write binary snapshot data. */ @Ignore @Test public void writeSessionWindowsWithCountTriggerSnapshot() throws Exception { final int sessionSize = 3; ListStateDescriptor<Tuple2<String, Integer>> stateDesc = new ListStateDescriptor<>("window-contents", STRING_INT_TUPLE.createSerializer(new ExecutionConfig())); WindowOperator<String, Tuple2<String, Integer>, Iterable<Tuple2<String, Integer>>, Tuple3<String, Long, Long>, TimeWindow> operator = new WindowOperator<>( EventTimeSessionWindows.withGap(Time.seconds(sessionSize)), new TimeWindow.Serializer(), new TupleKeySelector<String>(), BasicTypeInfo.STRING_TYPE_INFO.createSerializer(new ExecutionConfig()), stateDesc, new InternalIterableWindowFunction<>(new SessionWindowFunction()), PurgingTrigger.of(CountTrigger.of(4)), 0, null /* late data output tag */); OneInputStreamOperatorTestHarness<Tuple2<String, Integer>, Tuple3<String, Long, Long>> testHarness = new KeyedOneInputStreamOperatorTestHarness<>(operator, new TupleKeySelector<>(), BasicTypeInfo.STRING_TYPE_INFO); testHarness.setup(); testHarness.open(); // add elements out-of-order testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 0)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 2), 1000)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 3), 2500)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 4), 3500)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key1", 1), 10)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key1", 2), 1000)); // do snapshot and save to file OperatorSubtaskState snapshot = testHarness.snapshot(0L, 0L); OperatorSnapshotUtil.writeStateHandle( snapshot, "src/test/resources/win-op-migration-test-session-with-stateful-trigger-flink" + flinkGenerateSavepointVersion + "-snapshot"); testHarness.close(); }
Example 13
Source File: WindowOperatorMigrationTest.java From flink with Apache License 2.0 | 4 votes |
/** * This checks that we can restore from a virgin {@code WindowOperator} that has never seen * any elements. */ @Test public void testRestoreSessionWindowsWithCountTriggerInMintCondition() throws Exception { final int sessionSize = 3; ListStateDescriptor<Tuple2<String, Integer>> stateDesc = new ListStateDescriptor<>("window-contents", STRING_INT_TUPLE.createSerializer(new ExecutionConfig())); WindowOperator<String, Tuple2<String, Integer>, Iterable<Tuple2<String, Integer>>, Tuple3<String, Long, Long>, TimeWindow> operator = new WindowOperator<>( EventTimeSessionWindows.withGap(Time.seconds(sessionSize)), new TimeWindow.Serializer(), new TupleKeySelector<String>(), BasicTypeInfo.STRING_TYPE_INFO.createSerializer(new ExecutionConfig()), stateDesc, new InternalIterableWindowFunction<>(new SessionWindowFunction()), PurgingTrigger.of(CountTrigger.of(4)), 0, null /* late data output tag */); ConcurrentLinkedQueue<Object> expectedOutput = new ConcurrentLinkedQueue<>(); OneInputStreamOperatorTestHarness<Tuple2<String, Integer>, Tuple3<String, Long, Long>> testHarness = new KeyedOneInputStreamOperatorTestHarness<>(operator, new TupleKeySelector<>(), BasicTypeInfo.STRING_TYPE_INFO); testHarness.setup(); testHarness.initializeState( OperatorSnapshotUtil.getResourceFilename( "win-op-migration-test-session-with-stateful-trigger-mint-flink" + testMigrateVersion + "-snapshot")); testHarness.open(); // add elements out-of-order testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 0)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 2), 1000)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 3), 2500)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 4), 3500)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key1", 1), 10)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key1", 2), 1000)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key1", 3), 2500)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key1", 1), 6000)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key1", 2), 6500)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key1", 3), 7000)); expectedOutput.add(new StreamRecord<>(new Tuple3<>("key2-10", 0L, 6500L), 6499)); TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expectedOutput, testHarness.getOutput(), new Tuple3ResultSortComparator()); // add an element that merges the two "key1" sessions, they should now have count 6, and therefore fire testHarness.processElement(new StreamRecord<>(new Tuple2<>("key1", 10), 4500)); expectedOutput.add(new StreamRecord<>(new Tuple3<>("key1-22", 10L, 10000L), 9999L)); TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expectedOutput, testHarness.getOutput(), new Tuple3ResultSortComparator()); testHarness.close(); }
Example 14
Source File: WindowOperatorTest.java From flink with Apache License 2.0 | 4 votes |
@Test public void testNotSideOutputDueToLatenessSessionWithHugeLateness() throws Exception { final int gapSize = 3; final long lateness = 10000; ReducingStateDescriptor<Tuple2<String, Integer>> stateDesc = new ReducingStateDescriptor<>("window-contents", new SumReducer(), STRING_INT_TUPLE.createSerializer(new ExecutionConfig())); WindowOperator<String, Tuple2<String, Integer>, Tuple2<String, Integer>, Tuple3<String, Long, Long>, TimeWindow> operator = new WindowOperator<>( EventTimeSessionWindows.withGap(Time.seconds(gapSize)), new TimeWindow.Serializer(), new TupleKeySelector(), BasicTypeInfo.STRING_TYPE_INFO.createSerializer(new ExecutionConfig()), stateDesc, new InternalSingleValueWindowFunction<>(new ReducedSessionWindowFunction()), EventTimeTrigger.create(), lateness, lateOutputTag /* late data output tag */); OneInputStreamOperatorTestHarness<Tuple2<String, Integer>, Tuple3<String, Long, Long>> testHarness = createTestHarness(operator); testHarness.open(); ConcurrentLinkedQueue<Object> expected = new ConcurrentLinkedQueue<>(); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 1000)); testHarness.processWatermark(new Watermark(1999)); expected.add(new Watermark(1999)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 2000)); testHarness.processWatermark(new Watermark(4998)); expected.add(new Watermark(4998)); // this will not be sideoutput because the session we're adding two has maxTimestamp // after the current watermark testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 4500)); // new session testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 8500)); testHarness.processWatermark(new Watermark(7400)); expected.add(new Watermark(7400)); // this will merge the two sessions into one testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 7000)); testHarness.processWatermark(new Watermark(11501)); expected.add(new StreamRecord<>(new Tuple3<>("key2-5", 1000L, 11500L), 11499)); expected.add(new Watermark(11501)); // new session testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 11600)); testHarness.processWatermark(new Watermark(14600)); expected.add(new StreamRecord<>(new Tuple3<>("key2-1", 11600L, 14600L), 14599)); expected.add(new Watermark(14600)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 10000)); // the maxTimestamp of the merged session is already late, // so we get an immediate firing expected.add(new StreamRecord<>(new Tuple3<>("key2-7", 1000L, 14600L), 14599)); ConcurrentLinkedQueue<Object> actual = testHarness.getOutput(); ConcurrentLinkedQueue<StreamRecord<Tuple2<String, Integer>>> sideActual = testHarness.getSideOutput(lateOutputTag); TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expected, actual, new Tuple3ResultSortComparator()); assertEquals(null, sideActual); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 14500)); testHarness.processWatermark(new Watermark(20000)); expected.add(new StreamRecord<>(new Tuple3<>("key2-8", 1000L, 17500L), 17499)); expected.add(new Watermark(20000)); testHarness.processWatermark(new Watermark(100000)); expected.add(new Watermark(100000)); actual = testHarness.getOutput(); sideActual = testHarness.getSideOutput(lateOutputTag); TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expected, actual, new Tuple3ResultSortComparator()); assertEquals(null, sideActual); testHarness.close(); }
Example 15
Source File: WindowOperatorTest.java From Flink-CEPplus with Apache License 2.0 | 4 votes |
@Test public void testSideOutputDueToLatenessSessionZeroLateness() throws Exception { final int gapSize = 3; final long lateness = 0; ReducingStateDescriptor<Tuple2<String, Integer>> stateDesc = new ReducingStateDescriptor<>("window-contents", new SumReducer(), STRING_INT_TUPLE.createSerializer(new ExecutionConfig())); WindowOperator<String, Tuple2<String, Integer>, Tuple2<String, Integer>, Tuple3<String, Long, Long>, TimeWindow> operator = new WindowOperator<>( EventTimeSessionWindows.withGap(Time.seconds(gapSize)), new TimeWindow.Serializer(), new TupleKeySelector(), BasicTypeInfo.STRING_TYPE_INFO.createSerializer(new ExecutionConfig()), stateDesc, new InternalSingleValueWindowFunction<>(new ReducedSessionWindowFunction()), EventTimeTrigger.create(), lateness, lateOutputTag); OneInputStreamOperatorTestHarness<Tuple2<String, Integer>, Tuple3<String, Long, Long>> testHarness = createTestHarness(operator); testHarness.open(); ConcurrentLinkedQueue<Object> expected = new ConcurrentLinkedQueue<>(); ConcurrentLinkedQueue<Object> sideExpected = new ConcurrentLinkedQueue<>(); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 1000)); testHarness.processWatermark(new Watermark(1999)); expected.add(new Watermark(1999)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 2000)); testHarness.processWatermark(new Watermark(4998)); expected.add(new Watermark(4998)); // this will not be dropped because the session we're adding two has maxTimestamp // after the current watermark testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 4500)); // new session testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 8500)); testHarness.processWatermark(new Watermark(7400)); expected.add(new Watermark(7400)); // this will merge the two sessions into one testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 7000)); testHarness.processWatermark(new Watermark(11501)); expected.add(new StreamRecord<>(new Tuple3<>("key2-5", 1000L, 11500L), 11499)); expected.add(new Watermark(11501)); // new session testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 11600)); testHarness.processWatermark(new Watermark(14600)); expected.add(new StreamRecord<>(new Tuple3<>("key2-1", 11600L, 14600L), 14599)); expected.add(new Watermark(14600)); // this is sideoutput as late, reuse last timestamp testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 10000)); sideExpected.add(new StreamRecord<>(new Tuple2<>("key2", 1), 10000)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 14500)); testHarness.processWatermark(new Watermark(20000)); expected.add(new StreamRecord<>(new Tuple3<>("key2-1", 14500L, 17500L), 17499)); expected.add(new Watermark(20000)); testHarness.processWatermark(new Watermark(100000)); expected.add(new Watermark(100000)); ConcurrentLinkedQueue<Object> actual = testHarness.getOutput(); ConcurrentLinkedQueue<StreamRecord<Tuple2<String, Integer>>> sideActual = testHarness.getSideOutput( lateOutputTag); TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expected, actual, new Tuple2ResultSortComparator()); TestHarnessUtil.assertOutputEqualsSorted("SideOutput was not correct.", sideExpected, (Iterable) sideActual, new Tuple2ResultSortComparator()); testHarness.close(); }
Example 16
Source File: WindowOperatorTest.java From flink with Apache License 2.0 | 4 votes |
@Test @SuppressWarnings("unchecked") public void testSessionWindowsWithProcessFunction() throws Exception { closeCalled.set(0); final int sessionSize = 3; ListStateDescriptor<Tuple2<String, Integer>> stateDesc = new ListStateDescriptor<>("window-contents", STRING_INT_TUPLE.createSerializer(new ExecutionConfig())); WindowOperator<String, Tuple2<String, Integer>, Iterable<Tuple2<String, Integer>>, Tuple3<String, Long, Long>, TimeWindow> operator = new WindowOperator<>( EventTimeSessionWindows.withGap(Time.seconds(sessionSize)), new TimeWindow.Serializer(), new TupleKeySelector(), BasicTypeInfo.STRING_TYPE_INFO.createSerializer(new ExecutionConfig()), stateDesc, new InternalIterableProcessWindowFunction<>(new SessionProcessWindowFunction()), EventTimeTrigger.create(), 0, null /* late data output tag */); OneInputStreamOperatorTestHarness<Tuple2<String, Integer>, Tuple3<String, Long, Long>> testHarness = createTestHarness(operator); ConcurrentLinkedQueue<Object> expectedOutput = new ConcurrentLinkedQueue<>(); testHarness.open(); // add elements out-of-order testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 0)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 2), 1000)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 3), 2500)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key1", 1), 10)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key1", 2), 1000)); // do a snapshot, close and restore again OperatorSubtaskState snapshot = testHarness.snapshot(0L, 0L); TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expectedOutput, testHarness.getOutput(), new Tuple3ResultSortComparator()); testHarness.close(); testHarness = createTestHarness(operator); testHarness.setup(); testHarness.initializeState(snapshot); testHarness.open(); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key1", 3), 2500)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 4), 5501)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 5), 6000)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 5), 6000)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 6), 6050)); testHarness.processWatermark(new Watermark(12000)); expectedOutput.add(new StreamRecord<>(new Tuple3<>("key1-6", 10L, 5500L), 5499)); expectedOutput.add(new StreamRecord<>(new Tuple3<>("key2-6", 0L, 5500L), 5499)); expectedOutput.add(new StreamRecord<>(new Tuple3<>("key2-20", 5501L, 9050L), 9049)); expectedOutput.add(new Watermark(12000)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 10), 15000)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 20), 15000)); testHarness.processWatermark(new Watermark(17999)); expectedOutput.add(new StreamRecord<>(new Tuple3<>("key2-30", 15000L, 18000L), 17999)); expectedOutput.add(new Watermark(17999)); TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expectedOutput, testHarness.getOutput(), new Tuple3ResultSortComparator()); testHarness.close(); }
Example 17
Source File: WindowOperatorTest.java From flink with Apache License 2.0 | 4 votes |
@Test @SuppressWarnings("unchecked") public void testSessionWindowsWithProcessFunction() throws Exception { closeCalled.set(0); final int sessionSize = 3; ListStateDescriptor<Tuple2<String, Integer>> stateDesc = new ListStateDescriptor<>("window-contents", STRING_INT_TUPLE.createSerializer(new ExecutionConfig())); WindowOperator<String, Tuple2<String, Integer>, Iterable<Tuple2<String, Integer>>, Tuple3<String, Long, Long>, TimeWindow> operator = new WindowOperator<>( EventTimeSessionWindows.withGap(Time.seconds(sessionSize)), new TimeWindow.Serializer(), new TupleKeySelector(), BasicTypeInfo.STRING_TYPE_INFO.createSerializer(new ExecutionConfig()), stateDesc, new InternalIterableProcessWindowFunction<>(new SessionProcessWindowFunction()), EventTimeTrigger.create(), 0, null /* late data output tag */); OneInputStreamOperatorTestHarness<Tuple2<String, Integer>, Tuple3<String, Long, Long>> testHarness = createTestHarness(operator); ConcurrentLinkedQueue<Object> expectedOutput = new ConcurrentLinkedQueue<>(); testHarness.open(); // add elements out-of-order testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 0)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 2), 1000)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 3), 2500)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key1", 1), 10)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key1", 2), 1000)); // do a snapshot, close and restore again OperatorSubtaskState snapshot = testHarness.snapshot(0L, 0L); TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expectedOutput, testHarness.getOutput(), new Tuple3ResultSortComparator()); testHarness.close(); testHarness = createTestHarness(operator); testHarness.setup(); testHarness.initializeState(snapshot); testHarness.open(); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key1", 3), 2500)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 4), 5501)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 5), 6000)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 5), 6000)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 6), 6050)); testHarness.processWatermark(new Watermark(12000)); expectedOutput.add(new StreamRecord<>(new Tuple3<>("key1-6", 10L, 5500L), 5499)); expectedOutput.add(new StreamRecord<>(new Tuple3<>("key2-6", 0L, 5500L), 5499)); expectedOutput.add(new StreamRecord<>(new Tuple3<>("key2-20", 5501L, 9050L), 9049)); expectedOutput.add(new Watermark(12000)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 10), 15000)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 20), 15000)); testHarness.processWatermark(new Watermark(17999)); expectedOutput.add(new StreamRecord<>(new Tuple3<>("key2-30", 15000L, 18000L), 17999)); expectedOutput.add(new Watermark(17999)); TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expectedOutput, testHarness.getOutput(), new Tuple3ResultSortComparator()); testHarness.close(); }
Example 18
Source File: WindowOperatorTest.java From flink with Apache License 2.0 | 4 votes |
@Test public void testSideOutputDueToLatenessSessionZeroLatenessPurgingTrigger() throws Exception { final int gapSize = 3; final long lateness = 0; ReducingStateDescriptor<Tuple2<String, Integer>> stateDesc = new ReducingStateDescriptor<>("window-contents", new SumReducer(), STRING_INT_TUPLE.createSerializer(new ExecutionConfig())); WindowOperator<String, Tuple2<String, Integer>, Tuple2<String, Integer>, Tuple3<String, Long, Long>, TimeWindow> operator = new WindowOperator<>( EventTimeSessionWindows.withGap(Time.seconds(gapSize)), new TimeWindow.Serializer(), new TupleKeySelector(), BasicTypeInfo.STRING_TYPE_INFO.createSerializer(new ExecutionConfig()), stateDesc, new InternalSingleValueWindowFunction<>(new ReducedSessionWindowFunction()), PurgingTrigger.of(EventTimeTrigger.create()), lateness, lateOutputTag); OneInputStreamOperatorTestHarness<Tuple2<String, Integer>, Tuple3<String, Long, Long>> testHarness = createTestHarness(operator); testHarness.open(); ConcurrentLinkedQueue<Object> expected = new ConcurrentLinkedQueue<>(); ConcurrentLinkedQueue<Object> sideExpected = new ConcurrentLinkedQueue<>(); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 1000)); testHarness.processWatermark(new Watermark(1999)); expected.add(new Watermark(1999)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 2000)); testHarness.processWatermark(new Watermark(4998)); expected.add(new Watermark(4998)); // this will not be dropped because the session we're adding two has maxTimestamp // after the current watermark testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 4500)); // new session testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 8500)); testHarness.processWatermark(new Watermark(7400)); expected.add(new Watermark(7400)); // this will merge the two sessions into one testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 7000)); testHarness.processWatermark(new Watermark(11501)); expected.add(new StreamRecord<>(new Tuple3<>("key2-5", 1000L, 11500L), 11499)); expected.add(new Watermark(11501)); // new session testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 11600)); testHarness.processWatermark(new Watermark(14600)); expected.add(new StreamRecord<>(new Tuple3<>("key2-1", 11600L, 14600L), 14599)); expected.add(new Watermark(14600)); // this is side output as late testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 10000)); sideExpected.add(new StreamRecord<>(new Tuple2<>("key2", 1), 10000)); // this is also side output as late (we test that they are not accidentally merged) testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 10100)); sideExpected.add(new StreamRecord<>(new Tuple2<>("key2", 1), 10100)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 14500)); testHarness.processWatermark(new Watermark(20000)); expected.add(new StreamRecord<>(new Tuple3<>("key2-1", 14500L, 17500L), 17499)); expected.add(new Watermark(20000)); testHarness.processWatermark(new Watermark(100000)); expected.add(new Watermark(100000)); ConcurrentLinkedQueue<Object> actual = testHarness.getOutput(); ConcurrentLinkedQueue<StreamRecord<Tuple2<String, Integer>>> sideActual = testHarness.getSideOutput(lateOutputTag); TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expected, actual, new Tuple2ResultSortComparator()); TestHarnessUtil.assertOutputEqualsSorted("SideOutput was not correct.", sideExpected, (Iterable) sideActual, new Tuple2ResultSortComparator()); testHarness.close(); }
Example 19
Source File: WindowOperatorTest.java From flink with Apache License 2.0 | 4 votes |
@Test public void testNotSideOutputDueToLatenessSessionWithHugeLatenessPurgingTrigger() throws Exception { final int gapSize = 3; final long lateness = 10000; ReducingStateDescriptor<Tuple2<String, Integer>> stateDesc = new ReducingStateDescriptor<>("window-contents", new SumReducer(), STRING_INT_TUPLE.createSerializer(new ExecutionConfig())); WindowOperator<String, Tuple2<String, Integer>, Tuple2<String, Integer>, Tuple3<String, Long, Long>, TimeWindow> operator = new WindowOperator<>( EventTimeSessionWindows.withGap(Time.seconds(gapSize)), new TimeWindow.Serializer(), new TupleKeySelector(), BasicTypeInfo.STRING_TYPE_INFO.createSerializer(new ExecutionConfig()), stateDesc, new InternalSingleValueWindowFunction<>(new ReducedSessionWindowFunction()), PurgingTrigger.of(EventTimeTrigger.create()), lateness, lateOutputTag /* late data output tag */); OneInputStreamOperatorTestHarness<Tuple2<String, Integer>, Tuple3<String, Long, Long>> testHarness = createTestHarness(operator); testHarness.open(); ConcurrentLinkedQueue<Object> expected = new ConcurrentLinkedQueue<>(); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 1000)); testHarness.processWatermark(new Watermark(1999)); expected.add(new Watermark(1999)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 2000)); testHarness.processWatermark(new Watermark(4998)); expected.add(new Watermark(4998)); // this will not be sideoutput because the session we're adding two has maxTimestamp // after the current watermark testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 4500)); // new session testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 8500)); testHarness.processWatermark(new Watermark(7400)); expected.add(new Watermark(7400)); // this will merge the two sessions into one testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 7000)); testHarness.processWatermark(new Watermark(11501)); expected.add(new StreamRecord<>(new Tuple3<>("key2-5", 1000L, 11500L), 11499)); expected.add(new Watermark(11501)); // new session testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 11600)); testHarness.processWatermark(new Watermark(14600)); expected.add(new StreamRecord<>(new Tuple3<>("key2-1", 11600L, 14600L), 14599)); expected.add(new Watermark(14600)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 10000)); expected.add(new StreamRecord<>(new Tuple3<>("key2-1", 1000L, 14600L), 14599)); ConcurrentLinkedQueue<Object> actual = testHarness.getOutput(); ConcurrentLinkedQueue<StreamRecord<Tuple2<String, Integer>>> sideActual = testHarness.getSideOutput(lateOutputTag); TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expected, actual, new Tuple3ResultSortComparator()); assertEquals(null, sideActual); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 14500)); testHarness.processWatermark(new Watermark(20000)); expected.add(new StreamRecord<>(new Tuple3<>("key2-1", 1000L, 17500L), 17499)); expected.add(new Watermark(20000)); testHarness.processWatermark(new Watermark(100000)); expected.add(new Watermark(100000)); actual = testHarness.getOutput(); sideActual = testHarness.getSideOutput(lateOutputTag); TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expected, actual, new Tuple3ResultSortComparator()); assertEquals(null, sideActual); testHarness.close(); }
Example 20
Source File: EventTimeSessionWindowsTest.java From Flink-CEPplus with Apache License 2.0 | 3 votes |
@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()); }