org.apache.flink.streaming.runtime.operators.windowing.functions.InternalSingleValueWindowFunction Java Examples
The following examples show how to use
org.apache.flink.streaming.runtime.operators.windowing.functions.InternalSingleValueWindowFunction.
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: WindowOperatorTest.java From flink with Apache License 2.0 | 6 votes |
@Test @SuppressWarnings("unchecked") public void testSlidingEventTimeWindowsReduce() throws Exception { closeCalled.set(0); final int windowSize = 3; final int windowSlide = 1; 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>, Tuple2<String, Integer>, TimeWindow> operator = new WindowOperator<>( SlidingEventTimeWindows.of(Time.of(windowSize, TimeUnit.SECONDS), Time.of(windowSlide, TimeUnit.SECONDS)), new TimeWindow.Serializer(), new TupleKeySelector(), BasicTypeInfo.STRING_TYPE_INFO.createSerializer(new ExecutionConfig()), stateDesc, new InternalSingleValueWindowFunction<>(new PassThroughWindowFunction<String, TimeWindow, Tuple2<String, Integer>>()), EventTimeTrigger.create(), 0, null /* late data output tag */); testSlidingEventTimeWindows(operator); }
Example #2
Source File: WindowOperatorTest.java From flink with Apache License 2.0 | 6 votes |
@Test @SuppressWarnings("unchecked") public void testTumblingEventTimeWindowsReduce() throws Exception { closeCalled.set(0); final int windowSize = 3; 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>, Tuple2<String, Integer>, TimeWindow> operator = new WindowOperator<>( TumblingEventTimeWindows.of(Time.of(windowSize, TimeUnit.SECONDS)), new TimeWindow.Serializer(), new TupleKeySelector(), BasicTypeInfo.STRING_TYPE_INFO.createSerializer(new ExecutionConfig()), stateDesc, new InternalSingleValueWindowFunction<>(new PassThroughWindowFunction<String, TimeWindow, Tuple2<String, Integer>>()), EventTimeTrigger.create(), 0, null /* late data output tag */); testTumblingEventTimeWindows(operator); }
Example #3
Source File: WindowOperatorTest.java From flink with Apache License 2.0 | 6 votes |
@Test @SuppressWarnings("unchecked") public void testSlidingEventTimeWindowsReduce() throws Exception { closeCalled.set(0); final int windowSize = 3; final int windowSlide = 1; 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>, Tuple2<String, Integer>, TimeWindow> operator = new WindowOperator<>( SlidingEventTimeWindows.of(Time.of(windowSize, TimeUnit.SECONDS), Time.of(windowSlide, TimeUnit.SECONDS)), new TimeWindow.Serializer(), new TupleKeySelector(), BasicTypeInfo.STRING_TYPE_INFO.createSerializer(new ExecutionConfig()), stateDesc, new InternalSingleValueWindowFunction<>(new PassThroughWindowFunction<String, TimeWindow, Tuple2<String, Integer>>()), EventTimeTrigger.create(), 0, null /* late data output tag */); testSlidingEventTimeWindows(operator); }
Example #4
Source File: WindowOperatorTest.java From Flink-CEPplus with Apache License 2.0 | 6 votes |
@Test @SuppressWarnings("unchecked") public void testSlidingEventTimeWindowsReduce() throws Exception { closeCalled.set(0); final int windowSize = 3; final int windowSlide = 1; 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>, Tuple2<String, Integer>, TimeWindow> operator = new WindowOperator<>( SlidingEventTimeWindows.of(Time.of(windowSize, TimeUnit.SECONDS), Time.of(windowSlide, TimeUnit.SECONDS)), new TimeWindow.Serializer(), new TupleKeySelector(), BasicTypeInfo.STRING_TYPE_INFO.createSerializer(new ExecutionConfig()), stateDesc, new InternalSingleValueWindowFunction<>(new PassThroughWindowFunction<String, TimeWindow, Tuple2<String, Integer>>()), EventTimeTrigger.create(), 0, null /* late data output tag */); testSlidingEventTimeWindows(operator); }
Example #5
Source File: WindowOperatorTest.java From Flink-CEPplus with Apache License 2.0 | 6 votes |
@Test @SuppressWarnings("unchecked") public void testTumblingEventTimeWindowsReduce() throws Exception { closeCalled.set(0); final int windowSize = 3; 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>, Tuple2<String, Integer>, TimeWindow> operator = new WindowOperator<>( TumblingEventTimeWindows.of(Time.of(windowSize, TimeUnit.SECONDS)), new TimeWindow.Serializer(), new TupleKeySelector(), BasicTypeInfo.STRING_TYPE_INFO.createSerializer(new ExecutionConfig()), stateDesc, new InternalSingleValueWindowFunction<>(new PassThroughWindowFunction<String, TimeWindow, Tuple2<String, Integer>>()), EventTimeTrigger.create(), 0, null /* late data output tag */); testTumblingEventTimeWindows(operator); }
Example #6
Source File: WindowOperatorTest.java From flink with Apache License 2.0 | 4 votes |
@Test public void testProcessingTimeSlidingWindows() throws Throwable { final int windowSize = 3; final int windowSlide = 1; 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>, Tuple2<String, Integer>, TimeWindow> operator = new WindowOperator<>( SlidingProcessingTimeWindows.of(Time.of(windowSize, TimeUnit.SECONDS), Time.of(windowSlide, TimeUnit.SECONDS)), new TimeWindow.Serializer(), new TupleKeySelector(), BasicTypeInfo.STRING_TYPE_INFO.createSerializer(new ExecutionConfig()), stateDesc, new InternalSingleValueWindowFunction<>(new PassThroughWindowFunction<String, TimeWindow, Tuple2<String, Integer>>()), ProcessingTimeTrigger.create(), 0, null /* late data output tag */); OneInputStreamOperatorTestHarness<Tuple2<String, Integer>, Tuple2<String, Integer>> testHarness = createTestHarness(operator); ConcurrentLinkedQueue<Object> expectedOutput = new ConcurrentLinkedQueue<>(); testHarness.open(); // timestamp is ignored in processing time testHarness.setProcessingTime(3); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), Long.MAX_VALUE)); testHarness.setProcessingTime(1000); expectedOutput.add(new StreamRecord<>(new Tuple2<>("key2", 1), 999)); TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expectedOutput, testHarness.getOutput(), new Tuple2ResultSortComparator()); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), Long.MAX_VALUE)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), Long.MAX_VALUE)); testHarness.setProcessingTime(2000); expectedOutput.add(new StreamRecord<>(new Tuple2<>("key2", 3), 1999)); TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expectedOutput, testHarness.getOutput(), new Tuple2ResultSortComparator()); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key1", 1), Long.MAX_VALUE)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key1", 1), Long.MAX_VALUE)); testHarness.setProcessingTime(3000); expectedOutput.add(new StreamRecord<>(new Tuple2<>("key2", 3), 2999)); expectedOutput.add(new StreamRecord<>(new Tuple2<>("key1", 2), 2999)); TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expectedOutput, testHarness.getOutput(), new Tuple2ResultSortComparator()); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key1", 1), Long.MAX_VALUE)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key1", 1), Long.MAX_VALUE)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key1", 1), Long.MAX_VALUE)); testHarness.setProcessingTime(7000); expectedOutput.add(new StreamRecord<>(new Tuple2<>("key2", 2), 3999)); expectedOutput.add(new StreamRecord<>(new Tuple2<>("key1", 5), 3999)); expectedOutput.add(new StreamRecord<>(new Tuple2<>("key1", 5), 4999)); expectedOutput.add(new StreamRecord<>(new Tuple2<>("key1", 3), 5999)); TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expectedOutput, testHarness.getOutput(), new Tuple2ResultSortComparator()); testHarness.close(); }
Example #7
Source File: WindowOperatorTest.java From flink with Apache License 2.0 | 4 votes |
@Test public void testCleanupTimerWithEmptyReduceStateForTumblingWindows() throws Exception { final int windowSize = 2; final long lateness = 1; 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>, Tuple2<String, Integer>, TimeWindow> operator = new WindowOperator<>( TumblingEventTimeWindows.of(Time.of(windowSize, TimeUnit.SECONDS)), new TimeWindow.Serializer(), new TupleKeySelector(), BasicTypeInfo.STRING_TYPE_INFO.createSerializer(new ExecutionConfig()), stateDesc, new InternalSingleValueWindowFunction<>(new PassThroughWindowFunction<String, TimeWindow, Tuple2<String, Integer>>()), 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<>(); // normal element testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 1000)); testHarness.processWatermark(new Watermark(1599)); testHarness.processWatermark(new Watermark(1999)); testHarness.processWatermark(new Watermark(2000)); testHarness.processWatermark(new Watermark(5000)); expected.add(new Watermark(1599)); expected.add(new StreamRecord<>(new Tuple2<>("key2", 1), 1999)); expected.add(new Watermark(1999)); // here it fires and purges expected.add(new Watermark(2000)); // here is the cleanup timer expected.add(new Watermark(5000)); TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expected, testHarness.getOutput(), new Tuple2ResultSortComparator()); testHarness.close(); }
Example #8
Source File: WindowOperatorTest.java From flink with Apache License 2.0 | 4 votes |
@Test public void testCleanupTimerWithEmptyFoldingStateForTumblingWindows() throws Exception { final int windowSize = 2; final long lateness = 1; FoldingStateDescriptor<Tuple2<String, Integer>, Tuple2<String, Integer>> windowStateDesc = new FoldingStateDescriptor<>( "window-contents", new Tuple2<>((String) null, 0), new FoldFunction<Tuple2<String, Integer>, Tuple2<String, Integer>>() { private static final long serialVersionUID = 1L; @Override public Tuple2<String, Integer> fold(Tuple2<String, Integer> accumulator, Tuple2<String, Integer> value) throws Exception { return new Tuple2<>(value.f0, accumulator.f1 + value.f1); } }, STRING_INT_TUPLE); windowStateDesc.initializeSerializerUnlessSet(new ExecutionConfig()); WindowOperator<String, Tuple2<String, Integer>, Tuple2<String, Integer>, Tuple2<String, Integer>, TimeWindow> operator = new WindowOperator<>( TumblingEventTimeWindows.of(Time.of(windowSize, TimeUnit.SECONDS)), new TimeWindow.Serializer(), new TupleKeySelector(), BasicTypeInfo.STRING_TYPE_INFO.createSerializer(new ExecutionConfig()), windowStateDesc, new InternalSingleValueWindowFunction<>(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<>(); // normal element testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 1000)); testHarness.processWatermark(new Watermark(1599)); testHarness.processWatermark(new Watermark(1999)); testHarness.processWatermark(new Watermark(2000)); testHarness.processWatermark(new Watermark(5000)); expected.add(new Watermark(1599)); expected.add(new StreamRecord<>(new Tuple2<>("key2", 1), 1999)); expected.add(new Watermark(1999)); // here it fires and purges expected.add(new Watermark(2000)); // here is the cleanup timer expected.add(new Watermark(5000)); TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expected, testHarness.getOutput(), new Tuple2ResultSortComparator()); testHarness.close(); }
Example #9
Source File: WindowOperatorTest.java From flink with Apache License 2.0 | 4 votes |
@Test public void testCleanupTimerWithEmptyReduceStateForSessionWindows() throws Exception { 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, null /* 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(4998)); expected.add(new StreamRecord<>(new Tuple3<>("key2-1", 1000L, 4000L), 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 #10
Source File: WindowOperatorTest.java From flink with Apache License 2.0 | 4 votes |
@Test public void testCleanupTimerWithEmptyFoldingStateForSessionWindows() throws Exception { final int gapSize = 3; final long lateness = 10; FoldingStateDescriptor<Tuple2<String, Integer>, Tuple2<String, Integer>> windowStateDesc = new FoldingStateDescriptor<>( "window-contents", new Tuple2<>((String) null, 0), new FoldFunction<Tuple2<String, Integer>, Tuple2<String, Integer>>() { private static final long serialVersionUID = 1L; @Override public Tuple2<String, Integer> fold(Tuple2<String, Integer> accumulator, Tuple2<String, Integer> value) throws Exception { return new Tuple2<>(value.f0, accumulator.f1 + value.f1); } }, STRING_INT_TUPLE); windowStateDesc.initializeSerializerUnlessSet(new ExecutionConfig()); WindowOperator<String, Tuple2<String, Integer>, 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 InternalSingleValueWindowFunction<>(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 #11
Source File: InternalWindowFunctionTest.java From flink with Apache License 2.0 | 4 votes |
@SuppressWarnings("unchecked") @Test public void testInternalSingleValueWindowFunction() throws Exception { WindowFunctionMock mock = mock(WindowFunctionMock.class); InternalSingleValueWindowFunction<Long, String, Long, TimeWindow> windowFunction = new InternalSingleValueWindowFunction<>(mock); // check setOutputType TypeInformation<String> stringType = BasicTypeInfo.STRING_TYPE_INFO; ExecutionConfig execConf = new ExecutionConfig(); execConf.setParallelism(42); StreamingFunctionUtils.setOutputType(windowFunction, stringType, execConf); verify(mock).setOutputType(stringType, execConf); // check open Configuration config = new Configuration(); windowFunction.open(config); verify(mock).open(config); // check setRuntimeContext RuntimeContext rCtx = mock(RuntimeContext.class); windowFunction.setRuntimeContext(rCtx); verify(mock).setRuntimeContext(rCtx); // check apply TimeWindow w = mock(TimeWindow.class); Collector<String> c = (Collector<String>) mock(Collector.class); InternalWindowFunction.InternalWindowContext ctx = mock(InternalWindowFunction.InternalWindowContext.class); windowFunction.process(42L, w, ctx, 23L, c); verify(mock).apply(eq(42L), eq(w), (Iterable<Long>) argThat(IsIterableContainingInOrder.contains(23L)), eq(c)); // check close windowFunction.close(); verify(mock).close(); }
Example #12
Source File: WindowOperatorTest.java From flink with Apache License 2.0 | 4 votes |
@Test public void testSideOutputDueToLatenessTumbling() throws Exception { final int windowSize = 2; 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>, Tuple2<String, Integer>, TimeWindow> operator = new WindowOperator<>( TumblingEventTimeWindows.of(Time.of(windowSize, TimeUnit.SECONDS)), new TimeWindow.Serializer(), new TupleKeySelector(), BasicTypeInfo.STRING_TYPE_INFO.createSerializer(new ExecutionConfig()), stateDesc, new InternalSingleValueWindowFunction<>(new PassThroughWindowFunction<String, TimeWindow, Tuple2<String, Integer>>()), EventTimeTrigger.create(), lateness, lateOutputTag); OneInputStreamOperatorTestHarness<Tuple2<String, Integer>, Tuple2<String, Integer>> testHarness = createTestHarness(operator); testHarness.open(); ConcurrentLinkedQueue<Object> expected = new ConcurrentLinkedQueue<>(); ConcurrentLinkedQueue<Object> sideExpected = new ConcurrentLinkedQueue<>(); // normal element testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 1000)); testHarness.processWatermark(new Watermark(1985)); expected.add(new Watermark(1985)); // this will not be dropped because window.maxTimestamp() + allowedLateness > currentWatermark testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 1980)); testHarness.processWatermark(new Watermark(1999)); expected.add(new StreamRecord<>(new Tuple2<>("key2", 2), 1999)); expected.add(new Watermark(1999)); // sideoutput as late, will reuse previous timestamp since only input tuple is sideoutputed testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 1998)); sideExpected.add(new StreamRecord<>(new Tuple2<>("key2", 1), 1998)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 2001)); testHarness.processWatermark(new Watermark(2999)); expected.add(new Watermark(2999)); testHarness.processWatermark(new Watermark(3999)); expected.add(new StreamRecord<>(new Tuple2<>("key2", 1), 3999)); expected.add(new Watermark(3999)); TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expected, testHarness.getOutput(), new Tuple2ResultSortComparator()); TestHarnessUtil.assertOutputEqualsSorted("SideOutput was not correct.", sideExpected, (Iterable) testHarness.getSideOutput( lateOutputTag), new Tuple2ResultSortComparator()); testHarness.close(); }
Example #13
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 #14
Source File: WindowOperatorTest.java From flink with Apache License 2.0 | 4 votes |
@Test public void testProcessingTimeSessionWindows() throws Throwable { final int windowGap = 3; 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>, Tuple2<String, Integer>, TimeWindow> operator = new WindowOperator<>( ProcessingTimeSessionWindows.withGap(Time.of(windowGap, TimeUnit.SECONDS)), new TimeWindow.Serializer(), new TupleKeySelector(), BasicTypeInfo.STRING_TYPE_INFO.createSerializer(new ExecutionConfig()), stateDesc, new InternalSingleValueWindowFunction<>(new PassThroughWindowFunction<String, TimeWindow, Tuple2<String, Integer>>()), ProcessingTimeTrigger.create(), 0, null /* late data output tag */); OneInputStreamOperatorTestHarness<Tuple2<String, Integer>, Tuple2<String, Integer>> testHarness = createTestHarness(operator); ConcurrentLinkedQueue<Object> expectedOutput = new ConcurrentLinkedQueue<>(); testHarness.open(); // timestamp is ignored in processing time testHarness.setProcessingTime(3); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 1)); //Long.MAX_VALUE)); testHarness.setProcessingTime(1000); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 1002)); //Long.MAX_VALUE)); testHarness.setProcessingTime(5000); expectedOutput.add(new StreamRecord<>(new Tuple2<>("key2", 2), 3999)); TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expectedOutput, testHarness.getOutput(), new Tuple2ResultSortComparator()); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 5000)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 5000)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key1", 1), 5000)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key1", 1), 5000)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key1", 1), 5000)); testHarness.setProcessingTime(10000); expectedOutput.add(new StreamRecord<>(new Tuple2<>("key2", 2), 7999)); expectedOutput.add(new StreamRecord<>(new Tuple2<>("key1", 3), 7999)); TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expectedOutput, testHarness.getOutput(), new Tuple2ResultSortComparator()); assertEquals(expectedOutput.size(), testHarness.getOutput().size()); for (Object elem : testHarness.getOutput()) { if (elem instanceof StreamRecord) { StreamRecord<Tuple2<String, Integer>> el = (StreamRecord<Tuple2<String, Integer>>) elem; assertTrue(expectedOutput.contains(el)); } } testHarness.close(); }
Example #15
Source File: WindowOperatorTest.java From flink with Apache License 2.0 | 4 votes |
@Test public void testLateness() throws Exception { final int windowSize = 2; final long lateness = 500; 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>, Tuple2<String, Integer>, TimeWindow> operator = new WindowOperator<>( TumblingEventTimeWindows.of(Time.of(windowSize, TimeUnit.SECONDS)), new TimeWindow.Serializer(), new TupleKeySelector(), BasicTypeInfo.STRING_TYPE_INFO.createSerializer(new ExecutionConfig()), stateDesc, new InternalSingleValueWindowFunction<>(new PassThroughWindowFunction<String, TimeWindow, Tuple2<String, Integer>>()), PurgingTrigger.of(EventTimeTrigger.create()), lateness, lateOutputTag); OneInputStreamOperatorTestHarness<Tuple2<String, Integer>, Tuple2<String, Integer>> testHarness = createTestHarness(operator); testHarness.open(); ConcurrentLinkedQueue<Object> expected = new ConcurrentLinkedQueue<>(); ConcurrentLinkedQueue<Object> lateExpected = new ConcurrentLinkedQueue<>(); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 500)); testHarness.processWatermark(new Watermark(1500)); expected.add(new Watermark(1500)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 1300)); testHarness.processWatermark(new Watermark(2300)); expected.add(new StreamRecord<>(new Tuple2<>("key2", 2), 1999)); expected.add(new Watermark(2300)); // this will not be sideoutput because window.maxTimestamp() + allowedLateness > currentWatermark testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 1997)); testHarness.processWatermark(new Watermark(6000)); // this is 1 and not 3 because the trigger fires and purges expected.add(new StreamRecord<>(new Tuple2<>("key2", 1), 1999)); expected.add(new Watermark(6000)); // this will be side output because window.maxTimestamp() + allowedLateness < currentWatermark testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 1998)); testHarness.processWatermark(new Watermark(7000)); lateExpected.add(new StreamRecord<>(new Tuple2<>("key2", 1), 1998)); expected.add(new Watermark(7000)); TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expected, testHarness.getOutput(), new Tuple2ResultSortComparator()); TestHarnessUtil.assertOutputEqualsSorted( "SideOutput was not correct.", lateExpected, (Iterable) testHarness.getSideOutput(lateOutputTag), new Tuple2ResultSortComparator()); testHarness.close(); }
Example #16
Source File: WindowOperatorTest.java From flink with Apache License 2.0 | 4 votes |
@Test public void testCleanupTimeOverflow() throws Exception { final int windowSize = 1000; final long lateness = 2000; ReducingStateDescriptor<Tuple2<String, Integer>> stateDesc = new ReducingStateDescriptor<>("window-contents", new SumReducer(), STRING_INT_TUPLE.createSerializer(new ExecutionConfig())); TumblingEventTimeWindows windowAssigner = TumblingEventTimeWindows.of(Time.milliseconds(windowSize)); final WindowOperator<String, Tuple2<String, Integer>, Tuple2<String, Integer>, Tuple2<String, Integer>, TimeWindow> operator = new WindowOperator<>( windowAssigner, new TimeWindow.Serializer(), new TupleKeySelector(), BasicTypeInfo.STRING_TYPE_INFO.createSerializer(new ExecutionConfig()), stateDesc, new InternalSingleValueWindowFunction<>(new PassThroughWindowFunction<String, TimeWindow, Tuple2<String, Integer>>()), 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<>(); long timestamp = Long.MAX_VALUE - 1750; Collection<TimeWindow> windows = windowAssigner.assignWindows(new Tuple2<>("key2", 1), timestamp, new WindowAssigner.WindowAssignerContext() { @Override public long getCurrentProcessingTime() { return operator.windowAssignerContext.getCurrentProcessingTime(); } }); TimeWindow window = Iterables.getOnlyElement(windows); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), timestamp)); // the garbage collection timer would wrap-around Assert.assertTrue(window.maxTimestamp() + lateness < window.maxTimestamp()); // and it would prematurely fire with watermark (Long.MAX_VALUE - 1500) Assert.assertTrue(window.maxTimestamp() + lateness < Long.MAX_VALUE - 1500); // if we don't correctly prevent wrap-around in the garbage collection // timers this watermark will clean our window state for the just-added // element/window testHarness.processWatermark(new Watermark(Long.MAX_VALUE - 1500)); // this watermark is before the end timestamp of our only window Assert.assertTrue(Long.MAX_VALUE - 1500 < window.maxTimestamp()); Assert.assertTrue(window.maxTimestamp() < Long.MAX_VALUE); // push in a watermark that will trigger computation of our window testHarness.processWatermark(new Watermark(window.maxTimestamp())); expected.add(new Watermark(Long.MAX_VALUE - 1500)); expected.add(new StreamRecord<>(new Tuple2<>("key2", 1), window.maxTimestamp())); expected.add(new Watermark(window.maxTimestamp())); TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expected, testHarness.getOutput(), new Tuple2ResultSortComparator()); testHarness.close(); }
Example #17
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 #18
Source File: WindowOperatorTest.java From flink with Apache License 2.0 | 4 votes |
@Test public void testDropDueToLatenessSessionWithLatenessPurgingTrigger() throws Exception { // this has the same output as testSideOutputDueToLatenessSessionZeroLateness() because // the allowed lateness is too small to make a difference 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()), PurgingTrigger.of(EventTimeTrigger.create()), lateness, lateOutputTag); 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 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)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 10000)); expected.add(new StreamRecord<>(new Tuple3<>("key2-1", 10000L, 14600L), 14599)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 14500)); testHarness.processWatermark(new Watermark(20000)); expected.add(new StreamRecord<>(new Tuple3<>("key2-1", 10000L, 17500L), 17499)); expected.add(new Watermark(20000)); testHarness.processWatermark(new Watermark(100000)); expected.add(new Watermark(100000)); ConcurrentLinkedQueue<Object> actual = testHarness.getOutput(); TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expected, actual, new Tuple3ResultSortComparator()); testHarness.close(); }
Example #19
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 writeReducingProcessingTimeWindowsSnapshot() throws Exception { final int windowSize = 3; 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>, Tuple2<String, Integer>, TimeWindow> operator = new WindowOperator<>( TumblingProcessingTimeWindows.of(Time.of(windowSize, TimeUnit.SECONDS)), new TimeWindow.Serializer(), new TupleKeySelector<>(), BasicTypeInfo.STRING_TYPE_INFO.createSerializer(new ExecutionConfig()), stateDesc, new InternalSingleValueWindowFunction<>(new PassThroughWindowFunction<String, TimeWindow, Tuple2<String, Integer>>()), ProcessingTimeTrigger.create(), 0, null /* late data output tag */); ConcurrentLinkedQueue<Object> expectedOutput = new ConcurrentLinkedQueue<>(); OneInputStreamOperatorTestHarness<Tuple2<String, Integer>, Tuple2<String, Integer>> testHarness = new KeyedOneInputStreamOperatorTestHarness<>(operator, new TupleKeySelector<>(), BasicTypeInfo.STRING_TYPE_INFO); testHarness.setup(); testHarness.open(); testHarness.setProcessingTime(10); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1))); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key1", 1))); testHarness.setProcessingTime(3010); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1))); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key3", 1))); expectedOutput.add(new StreamRecord<>(new Tuple2<>("key1", 1), 2999)); expectedOutput.add(new StreamRecord<>(new Tuple2<>("key2", 1), 2999)); TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expectedOutput, testHarness.getOutput(), new Tuple2ResultSortComparator<>()); // do snapshot and save to file OperatorSubtaskState snapshot = testHarness.snapshot(0, 0); OperatorSnapshotUtil.writeStateHandle( snapshot, "src/test/resources/win-op-migration-test-reduce-processing-time-flink" + flinkGenerateSavepointVersion + "-snapshot"); testHarness.close(); }
Example #20
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 #21
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 #22
Source File: WindowOperatorTest.java From flink with Apache License 2.0 | 4 votes |
@Test public void testDropDueToLatenessSessionWithLatenessPurgingTrigger() throws Exception { // this has the same output as testSideOutputDueToLatenessSessionZeroLateness() because // the allowed lateness is too small to make a difference 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()), PurgingTrigger.of(EventTimeTrigger.create()), lateness, lateOutputTag); 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 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)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 10000)); expected.add(new StreamRecord<>(new Tuple3<>("key2-1", 10000L, 14600L), 14599)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 14500)); testHarness.processWatermark(new Watermark(20000)); expected.add(new StreamRecord<>(new Tuple3<>("key2-1", 10000L, 17500L), 17499)); expected.add(new Watermark(20000)); testHarness.processWatermark(new Watermark(100000)); expected.add(new Watermark(100000)); ConcurrentLinkedQueue<Object> actual = testHarness.getOutput(); TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expected, actual, new Tuple3ResultSortComparator()); testHarness.close(); }
Example #23
Source File: WindowOperatorTest.java From flink 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 #24
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 #25
Source File: WindowOperatorTest.java From flink with Apache License 2.0 | 4 votes |
@Test public void testSideOutputDueToLatenessSliding() throws Exception { final int windowSize = 3; final int windowSlide = 1; 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>, Tuple2<String, Integer>, TimeWindow> operator = new WindowOperator<>( SlidingEventTimeWindows.of(Time.of(windowSize, TimeUnit.SECONDS), Time.of(windowSlide, TimeUnit.SECONDS)), new TimeWindow.Serializer(), new TupleKeySelector(), BasicTypeInfo.STRING_TYPE_INFO.createSerializer(new ExecutionConfig()), stateDesc, new InternalSingleValueWindowFunction<>(new PassThroughWindowFunction<String, TimeWindow, Tuple2<String, Integer>>()), EventTimeTrigger.create(), lateness, lateOutputTag /* late data output tag */); OneInputStreamOperatorTestHarness<Tuple2<String, Integer>, Tuple2<String, Integer>> 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 StreamRecord<>(new Tuple2<>("key2", 1), 1999)); expected.add(new Watermark(1999)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 2000)); testHarness.processWatermark(new Watermark(3000)); expected.add(new StreamRecord<>(new Tuple2<>("key2", 2), 2999)); expected.add(new Watermark(3000)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key1", 1), 3001)); // lateness is set to 0 and window size = 3 sec and slide 1, the following 2 elements (2400) // are assigned to windows ending at 2999, 3999, 4999. // The 2999 is dropped because it is already late (WM = 2999) but the rest are kept. testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 2400)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 2400)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key1", 1), 3001)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 3900)); testHarness.processWatermark(new Watermark(6000)); expected.add(new StreamRecord<>(new Tuple2<>("key2", 5), 3999)); expected.add(new StreamRecord<>(new Tuple2<>("key1", 2), 3999)); expected.add(new StreamRecord<>(new Tuple2<>("key2", 4), 4999)); expected.add(new StreamRecord<>(new Tuple2<>("key1", 2), 4999)); expected.add(new StreamRecord<>(new Tuple2<>("key2", 1), 5999)); expected.add(new StreamRecord<>(new Tuple2<>("key1", 2), 5999)); expected.add(new Watermark(6000)); // sideoutput element due to lateness testHarness.processElement(new StreamRecord<>(new Tuple2<>("key1", 1), 3001)); sideExpected.add(new StreamRecord<>(new Tuple2<>("key1", 1), 3001)); testHarness.processWatermark(new Watermark(25000)); expected.add(new Watermark(25000)); TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expected, testHarness.getOutput(), new Tuple2ResultSortComparator()); TestHarnessUtil.assertOutputEqualsSorted("SideOutput was not correct.", sideExpected, (Iterable) testHarness.getSideOutput(lateOutputTag), new Tuple2ResultSortComparator()); testHarness.close(); }
Example #26
Source File: WindowOperatorTest.java From flink with Apache License 2.0 | 4 votes |
@Test public void testSideOutputDueToLatenessTumbling() throws Exception { final int windowSize = 2; 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>, Tuple2<String, Integer>, TimeWindow> operator = new WindowOperator<>( TumblingEventTimeWindows.of(Time.of(windowSize, TimeUnit.SECONDS)), new TimeWindow.Serializer(), new TupleKeySelector(), BasicTypeInfo.STRING_TYPE_INFO.createSerializer(new ExecutionConfig()), stateDesc, new InternalSingleValueWindowFunction<>(new PassThroughWindowFunction<String, TimeWindow, Tuple2<String, Integer>>()), EventTimeTrigger.create(), lateness, lateOutputTag); OneInputStreamOperatorTestHarness<Tuple2<String, Integer>, Tuple2<String, Integer>> testHarness = createTestHarness(operator); testHarness.open(); ConcurrentLinkedQueue<Object> expected = new ConcurrentLinkedQueue<>(); ConcurrentLinkedQueue<Object> sideExpected = new ConcurrentLinkedQueue<>(); // normal element testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 1000)); testHarness.processWatermark(new Watermark(1985)); expected.add(new Watermark(1985)); // this will not be dropped because window.maxTimestamp() + allowedLateness > currentWatermark testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 1980)); testHarness.processWatermark(new Watermark(1999)); expected.add(new StreamRecord<>(new Tuple2<>("key2", 2), 1999)); expected.add(new Watermark(1999)); // sideoutput as late, will reuse previous timestamp since only input tuple is sideoutputed testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 1998)); sideExpected.add(new StreamRecord<>(new Tuple2<>("key2", 1), 1998)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 2001)); testHarness.processWatermark(new Watermark(2999)); expected.add(new Watermark(2999)); testHarness.processWatermark(new Watermark(3999)); expected.add(new StreamRecord<>(new Tuple2<>("key2", 1), 3999)); expected.add(new Watermark(3999)); TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expected, testHarness.getOutput(), new Tuple2ResultSortComparator()); TestHarnessUtil.assertOutputEqualsSorted("SideOutput was not correct.", sideExpected, (Iterable) testHarness.getSideOutput( lateOutputTag), new Tuple2ResultSortComparator()); testHarness.close(); }
Example #27
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 #28
Source File: WindowOperatorTest.java From flink with Apache License 2.0 | 4 votes |
@Test public void testCleanupTimerWithEmptyFoldingStateForSessionWindows() throws Exception { final int gapSize = 3; final long lateness = 10; FoldingStateDescriptor<Tuple2<String, Integer>, Tuple2<String, Integer>> windowStateDesc = new FoldingStateDescriptor<>( "window-contents", new Tuple2<>((String) null, 0), new FoldFunction<Tuple2<String, Integer>, Tuple2<String, Integer>>() { private static final long serialVersionUID = 1L; @Override public Tuple2<String, Integer> fold(Tuple2<String, Integer> accumulator, Tuple2<String, Integer> value) throws Exception { return new Tuple2<>(value.f0, accumulator.f1 + value.f1); } }, STRING_INT_TUPLE); windowStateDesc.initializeSerializerUnlessSet(new ExecutionConfig()); WindowOperator<String, Tuple2<String, Integer>, 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 InternalSingleValueWindowFunction<>(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 #29
Source File: WindowOperatorTest.java From flink with Apache License 2.0 | 4 votes |
@Test public void testProcessingTimeSessionWindows() throws Throwable { final int windowGap = 3; 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>, Tuple2<String, Integer>, TimeWindow> operator = new WindowOperator<>( ProcessingTimeSessionWindows.withGap(Time.of(windowGap, TimeUnit.SECONDS)), new TimeWindow.Serializer(), new TupleKeySelector(), BasicTypeInfo.STRING_TYPE_INFO.createSerializer(new ExecutionConfig()), stateDesc, new InternalSingleValueWindowFunction<>(new PassThroughWindowFunction<String, TimeWindow, Tuple2<String, Integer>>()), ProcessingTimeTrigger.create(), 0, null /* late data output tag */); OneInputStreamOperatorTestHarness<Tuple2<String, Integer>, Tuple2<String, Integer>> testHarness = createTestHarness(operator); ConcurrentLinkedQueue<Object> expectedOutput = new ConcurrentLinkedQueue<>(); testHarness.open(); // timestamp is ignored in processing time testHarness.setProcessingTime(3); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 1)); //Long.MAX_VALUE)); testHarness.setProcessingTime(1000); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 1002)); //Long.MAX_VALUE)); testHarness.setProcessingTime(5000); expectedOutput.add(new StreamRecord<>(new Tuple2<>("key2", 2), 3999)); TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expectedOutput, testHarness.getOutput(), new Tuple2ResultSortComparator()); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 5000)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 5000)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key1", 1), 5000)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key1", 1), 5000)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key1", 1), 5000)); testHarness.setProcessingTime(10000); expectedOutput.add(new StreamRecord<>(new Tuple2<>("key2", 2), 7999)); expectedOutput.add(new StreamRecord<>(new Tuple2<>("key1", 3), 7999)); TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expectedOutput, testHarness.getOutput(), new Tuple2ResultSortComparator()); assertEquals(expectedOutput.size(), testHarness.getOutput().size()); for (Object elem : testHarness.getOutput()) { if (elem instanceof StreamRecord) { StreamRecord<Tuple2<String, Integer>> el = (StreamRecord<Tuple2<String, Integer>>) elem; assertTrue(expectedOutput.contains(el)); } } testHarness.close(); }
Example #30
Source File: WindowOperatorTest.java From flink with Apache License 2.0 | 4 votes |
@Test public void testProcessingTimeSlidingWindows() throws Throwable { final int windowSize = 3; final int windowSlide = 1; 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>, Tuple2<String, Integer>, TimeWindow> operator = new WindowOperator<>( SlidingProcessingTimeWindows.of(Time.of(windowSize, TimeUnit.SECONDS), Time.of(windowSlide, TimeUnit.SECONDS)), new TimeWindow.Serializer(), new TupleKeySelector(), BasicTypeInfo.STRING_TYPE_INFO.createSerializer(new ExecutionConfig()), stateDesc, new InternalSingleValueWindowFunction<>(new PassThroughWindowFunction<String, TimeWindow, Tuple2<String, Integer>>()), ProcessingTimeTrigger.create(), 0, null /* late data output tag */); OneInputStreamOperatorTestHarness<Tuple2<String, Integer>, Tuple2<String, Integer>> testHarness = createTestHarness(operator); ConcurrentLinkedQueue<Object> expectedOutput = new ConcurrentLinkedQueue<>(); testHarness.open(); // timestamp is ignored in processing time testHarness.setProcessingTime(3); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), Long.MAX_VALUE)); testHarness.setProcessingTime(1000); expectedOutput.add(new StreamRecord<>(new Tuple2<>("key2", 1), 999)); TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expectedOutput, testHarness.getOutput(), new Tuple2ResultSortComparator()); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), Long.MAX_VALUE)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), Long.MAX_VALUE)); testHarness.setProcessingTime(2000); expectedOutput.add(new StreamRecord<>(new Tuple2<>("key2", 3), 1999)); TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expectedOutput, testHarness.getOutput(), new Tuple2ResultSortComparator()); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key1", 1), Long.MAX_VALUE)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key1", 1), Long.MAX_VALUE)); testHarness.setProcessingTime(3000); expectedOutput.add(new StreamRecord<>(new Tuple2<>("key2", 3), 2999)); expectedOutput.add(new StreamRecord<>(new Tuple2<>("key1", 2), 2999)); TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expectedOutput, testHarness.getOutput(), new Tuple2ResultSortComparator()); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key1", 1), Long.MAX_VALUE)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key1", 1), Long.MAX_VALUE)); testHarness.processElement(new StreamRecord<>(new Tuple2<>("key1", 1), Long.MAX_VALUE)); testHarness.setProcessingTime(7000); expectedOutput.add(new StreamRecord<>(new Tuple2<>("key2", 2), 3999)); expectedOutput.add(new StreamRecord<>(new Tuple2<>("key1", 5), 3999)); expectedOutput.add(new StreamRecord<>(new Tuple2<>("key1", 5), 4999)); expectedOutput.add(new StreamRecord<>(new Tuple2<>("key1", 3), 5999)); TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expectedOutput, testHarness.getOutput(), new Tuple2ResultSortComparator()); testHarness.close(); }