Search in sources :

Example 11 with TimeWindow

use of org.apache.flink.streaming.api.windowing.windows.TimeWindow in project flink by apache.

the class TimeWindowTranslationTest method testApplyAlignedTimeWindows.

/**
	 * These tests ensure that the fast aligned time windows operator is used if the
	 * conditions are right.
	 */
@Test
public void testApplyAlignedTimeWindows() throws Exception {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    env.setStreamTimeCharacteristic(TimeCharacteristic.ProcessingTime);
    DataStream<Tuple2<String, Integer>> source = env.fromElements(Tuple2.of("hello", 1), Tuple2.of("hello", 2));
    DataStream<Tuple2<String, Integer>> window1 = source.keyBy(0).window(TumblingAlignedProcessingTimeWindows.of(Time.of(1000, TimeUnit.MILLISECONDS))).apply(new WindowFunction<Tuple2<String, Integer>, Tuple2<String, Integer>, Tuple, TimeWindow>() {

        private static final long serialVersionUID = 1L;

        @Override
        public void apply(Tuple tuple, TimeWindow window, Iterable<Tuple2<String, Integer>> values, Collector<Tuple2<String, Integer>> out) throws Exception {
        }
    });
    OneInputTransformation<Tuple2<String, Integer>, Tuple2<String, Integer>> transform1 = (OneInputTransformation<Tuple2<String, Integer>, Tuple2<String, Integer>>) window1.getTransformation();
    OneInputStreamOperator<Tuple2<String, Integer>, Tuple2<String, Integer>> operator1 = transform1.getOperator();
    Assert.assertTrue(operator1 instanceof AccumulatingProcessingTimeWindowOperator);
}
Also used : TimeWindow(org.apache.flink.streaming.api.windowing.windows.TimeWindow) Tuple2(org.apache.flink.api.java.tuple.Tuple2) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) OneInputTransformation(org.apache.flink.streaming.api.transformations.OneInputTransformation) Tuple(org.apache.flink.api.java.tuple.Tuple) Test(org.junit.Test)

Example 12 with TimeWindow

use of org.apache.flink.streaming.api.windowing.windows.TimeWindow in project flink by apache.

the class WindowOperatorContractTest method testWindowsAreMergedEagerly.

/**
	 * Verify that windows are merged eagerly, if possible.
	 */
public void testWindowsAreMergedEagerly(final TimeDomainAdaptor timeAdaptor) throws Exception {
    // in this test we only have one state window and windows are eagerly
    // merged into the first window
    MergingWindowAssigner<Integer, TimeWindow> mockAssigner = mockMergingAssigner();
    timeAdaptor.setIsEventTime(mockAssigner);
    Trigger<Integer, TimeWindow> mockTrigger = mockTrigger();
    InternalWindowFunction<Iterable<Integer>, Void, Integer, TimeWindow> mockWindowFunction = mockWindowFunction();
    KeyedOneInputStreamOperatorTestHarness<Integer, Integer, Void> testHarness = createWindowOperator(mockAssigner, mockTrigger, 0L, intListDescriptor, mockWindowFunction);
    testHarness.open();
    timeAdaptor.advanceTime(testHarness, Long.MIN_VALUE);
    assertEquals(0, testHarness.extractOutputStreamRecords().size());
    assertEquals(0, testHarness.numKeyedStateEntries());
    doAnswer(new Answer<TriggerResult>() {

        @Override
        public TriggerResult answer(InvocationOnMock invocation) throws Exception {
            Trigger.TriggerContext context = (Trigger.TriggerContext) invocation.getArguments()[3];
            // don't intefere with cleanup timers
            timeAdaptor.registerTimer(context, 0L);
            context.getPartitionedState(valueStateDescriptor).update("hello");
            return TriggerResult.CONTINUE;
        }
    }).when(mockTrigger).onElement(Matchers.<Integer>anyObject(), anyLong(), anyTimeWindow(), anyTriggerContext());
    doAnswer(new Answer<TriggerResult>() {

        @Override
        public TriggerResult answer(InvocationOnMock invocation) throws Exception {
            Trigger.OnMergeContext context = (Trigger.OnMergeContext) invocation.getArguments()[1];
            // don't intefere with cleanup timers
            timeAdaptor.registerTimer(context, 0L);
            context.getPartitionedState(valueStateDescriptor).update("hello");
            return TriggerResult.CONTINUE;
        }
    }).when(mockTrigger).onMerge(anyTimeWindow(), anyOnMergeContext());
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Exception {
            Trigger.TriggerContext context = (Trigger.TriggerContext) invocation.getArguments()[1];
            timeAdaptor.deleteTimer(context, 0L);
            context.getPartitionedState(valueStateDescriptor).clear();
            return null;
        }
    }).when(mockTrigger).clear(anyTimeWindow(), anyTriggerContext());
    when(mockAssigner.assignWindows(anyInt(), anyLong(), anyAssignerContext())).thenReturn(Arrays.asList(new TimeWindow(0, 2)));
    testHarness.processElement(new StreamRecord<>(0, 0L));
    // window state plus trigger state plus merging window set
    assertEquals(3, testHarness.numKeyedStateEntries());
    // timer and GC timer
    assertEquals(2, timeAdaptor.numTimers(testHarness));
    when(mockAssigner.assignWindows(anyInt(), anyLong(), anyAssignerContext())).thenReturn(Arrays.asList(new TimeWindow(2, 4)));
    shouldMergeWindows(mockAssigner, Lists.newArrayList(new TimeWindow(0, 2), new TimeWindow(2, 4)), Lists.newArrayList(new TimeWindow(0, 2), new TimeWindow(2, 4)), new TimeWindow(0, 4));
    // don't register a timer or update state in onElement, this checks
    // whether onMerge does correctly set those things
    doAnswer(new Answer<TriggerResult>() {

        @Override
        public TriggerResult answer(InvocationOnMock invocation) throws Exception {
            return TriggerResult.CONTINUE;
        }
    }).when(mockTrigger).onElement(Matchers.<Integer>anyObject(), anyLong(), anyTimeWindow(), anyTriggerContext());
    testHarness.processElement(new StreamRecord<>(0, 0L));
    verify(mockTrigger).onMerge(eq(new TimeWindow(0, 4)), anyOnMergeContext());
    assertEquals(3, testHarness.numKeyedStateEntries());
    assertEquals(2, timeAdaptor.numTimers(testHarness));
}
Also used : TimeWindow(org.apache.flink.streaming.api.windowing.windows.TimeWindow) Trigger(org.apache.flink.streaming.api.windowing.triggers.Trigger) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TriggerResult(org.apache.flink.streaming.api.windowing.triggers.TriggerResult)

Example 13 with TimeWindow

use of org.apache.flink.streaming.api.windowing.windows.TimeWindow in project flink by apache.

the class WindowOperatorContractTest method testProcessingElementsWithinAllowedLateness.

@Test
public void testProcessingElementsWithinAllowedLateness() throws Exception {
    WindowAssigner<Integer, TimeWindow> mockAssigner = mockTimeWindowAssigner();
    Trigger<Integer, TimeWindow> mockTrigger = mockTrigger();
    InternalWindowFunction<Iterable<Integer>, Void, Integer, TimeWindow> mockWindowFunction = mockWindowFunction();
    KeyedOneInputStreamOperatorTestHarness<Integer, Integer, Void> testHarness = createWindowOperator(mockAssigner, mockTrigger, 20L, intListDescriptor, mockWindowFunction);
    testHarness.open();
    when(mockAssigner.assignWindows(anyInt(), anyLong(), anyAssignerContext())).thenReturn(Arrays.asList(new TimeWindow(0, 2)));
    assertEquals(0, testHarness.getOutput().size());
    assertEquals(0, testHarness.numKeyedStateEntries());
    doAnswer(new Answer<TriggerResult>() {

        @Override
        public TriggerResult answer(InvocationOnMock invocation) throws Exception {
            return TriggerResult.FIRE;
        }
    }).when(mockTrigger).onElement(Matchers.<Integer>anyObject(), anyLong(), anyTimeWindow(), anyTriggerContext());
    // 20 is just at the limit, window.maxTime() is 1 and allowed lateness is 20
    testHarness.processWatermark(new Watermark(20));
    testHarness.processElement(new StreamRecord<>(0, 0L));
    verify(mockWindowFunction, times(1)).apply(eq(0), eq(new TimeWindow(0, 2)), intIterable(0), WindowOperatorContractTest.<Void>anyCollector());
    // clear is only called at cleanup time/GC time
    verify(mockTrigger, never()).clear(anyTimeWindow(), anyTriggerContext());
    // FIRE should not purge contents
    // window contents plus trigger state
    assertEquals(1, testHarness.numKeyedStateEntries());
    // just the GC timer
    assertEquals(1, testHarness.numEventTimeTimers());
}
Also used : TimeWindow(org.apache.flink.streaming.api.windowing.windows.TimeWindow) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TriggerResult(org.apache.flink.streaming.api.windowing.triggers.TriggerResult) Watermark(org.apache.flink.streaming.api.watermark.Watermark) Test(org.junit.Test)

Example 14 with TimeWindow

use of org.apache.flink.streaming.api.windowing.windows.TimeWindow in project flink by apache.

the class WindowTranslationTest method testFoldWithProcessWindowFunctionEventTime.

@Test
@SuppressWarnings("rawtypes")
public void testFoldWithProcessWindowFunctionEventTime() throws Exception {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    env.setStreamTimeCharacteristic(TimeCharacteristic.IngestionTime);
    DataStream<Tuple2<String, Integer>> source = env.fromElements(Tuple2.of("hello", 1), Tuple2.of("hello", 2));
    DataStream<Tuple2<String, Integer>> window = source.keyBy(new TupleKeySelector()).window(TumblingEventTimeWindows.of(Time.of(1, TimeUnit.SECONDS))).fold(new Tuple3<>("", "", 0), new DummyFolder(), new ProcessWindowFunction<Tuple3<String, String, Integer>, Tuple2<String, Integer>, String, TimeWindow>() {

        private static final long serialVersionUID = 1L;

        @Override
        public void process(String key, Context ctx, Iterable<Tuple3<String, String, Integer>> values, Collector<Tuple2<String, Integer>> out) throws Exception {
            for (Tuple3<String, String, Integer> in : values) {
                out.collect(new Tuple2<>(in.f0, in.f2));
            }
        }
    });
    OneInputTransformation<Tuple2<String, Integer>, Tuple2<String, Integer>> transform = (OneInputTransformation<Tuple2<String, Integer>, Tuple2<String, Integer>>) window.getTransformation();
    OneInputStreamOperator<Tuple2<String, Integer>, Tuple2<String, Integer>> operator = transform.getOperator();
    Assert.assertTrue(operator instanceof WindowOperator);
    WindowOperator<String, Tuple2<String, Integer>, ?, ?, ?> winOperator = (WindowOperator<String, Tuple2<String, Integer>, ?, ?, ?>) operator;
    Assert.assertTrue(winOperator.getTrigger() instanceof EventTimeTrigger);
    Assert.assertTrue(winOperator.getWindowAssigner() instanceof TumblingEventTimeWindows);
    Assert.assertTrue(winOperator.getStateDescriptor() instanceof FoldingStateDescriptor);
    processElementAndEnsureOutput(winOperator, winOperator.getKeySelector(), BasicTypeInfo.STRING_TYPE_INFO, new Tuple2<>("hello", 1));
}
Also used : FoldingStateDescriptor(org.apache.flink.api.common.state.FoldingStateDescriptor) TumblingEventTimeWindows(org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows) TimeWindow(org.apache.flink.streaming.api.windowing.windows.TimeWindow) Tuple2(org.apache.flink.api.java.tuple.Tuple2) Tuple3(org.apache.flink.api.java.tuple.Tuple3) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) OneInputTransformation(org.apache.flink.streaming.api.transformations.OneInputTransformation) EventTimeTrigger(org.apache.flink.streaming.api.windowing.triggers.EventTimeTrigger) Test(org.junit.Test)

Example 15 with TimeWindow

use of org.apache.flink.streaming.api.windowing.windows.TimeWindow in project flink by apache.

the class WindowTranslationTest method testReduceWithWindowFunctionEventTime.

@Test
@SuppressWarnings("rawtypes")
public void testReduceWithWindowFunctionEventTime() throws Exception {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    env.setStreamTimeCharacteristic(TimeCharacteristic.IngestionTime);
    DataStream<Tuple2<String, Integer>> source = env.fromElements(Tuple2.of("hello", 1), Tuple2.of("hello", 2));
    DummyReducer reducer = new DummyReducer();
    DataStream<Tuple3<String, String, Integer>> window = source.keyBy(new TupleKeySelector()).window(TumblingEventTimeWindows.of(Time.of(1, TimeUnit.SECONDS))).reduce(reducer, new WindowFunction<Tuple2<String, Integer>, Tuple3<String, String, Integer>, String, TimeWindow>() {

        private static final long serialVersionUID = 1L;

        @Override
        public void apply(String key, TimeWindow window, Iterable<Tuple2<String, Integer>> values, Collector<Tuple3<String, String, Integer>> out) throws Exception {
            for (Tuple2<String, Integer> in : values) {
                out.collect(new Tuple3<>(in.f0, in.f0, in.f1));
            }
        }
    });
    OneInputTransformation<Tuple2<String, Integer>, Tuple3<String, String, Integer>> transform = (OneInputTransformation<Tuple2<String, Integer>, Tuple3<String, String, Integer>>) window.getTransformation();
    OneInputStreamOperator<Tuple2<String, Integer>, Tuple3<String, String, Integer>> operator = transform.getOperator();
    Assert.assertTrue(operator instanceof WindowOperator);
    WindowOperator<String, Tuple2<String, Integer>, ?, ?, ?> winOperator = (WindowOperator<String, Tuple2<String, Integer>, ?, ?, ?>) operator;
    Assert.assertTrue(winOperator.getTrigger() instanceof EventTimeTrigger);
    Assert.assertTrue(winOperator.getWindowAssigner() instanceof TumblingEventTimeWindows);
    Assert.assertTrue(winOperator.getStateDescriptor() instanceof ReducingStateDescriptor);
    processElementAndEnsureOutput(operator, winOperator.getKeySelector(), BasicTypeInfo.STRING_TYPE_INFO, new Tuple2<>("hello", 1));
}
Also used : TumblingEventTimeWindows(org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows) ReducingStateDescriptor(org.apache.flink.api.common.state.ReducingStateDescriptor) TimeWindow(org.apache.flink.streaming.api.windowing.windows.TimeWindow) Tuple2(org.apache.flink.api.java.tuple.Tuple2) Tuple3(org.apache.flink.api.java.tuple.Tuple3) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) OneInputTransformation(org.apache.flink.streaming.api.transformations.OneInputTransformation) EventTimeTrigger(org.apache.flink.streaming.api.windowing.triggers.EventTimeTrigger) Test(org.junit.Test)

Aggregations

TimeWindow (org.apache.flink.streaming.api.windowing.windows.TimeWindow)158 Test (org.junit.Test)147 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)99 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)75 OneInputTransformation (org.apache.flink.streaming.api.transformations.OneInputTransformation)48 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)43 Tuple3 (org.apache.flink.api.java.tuple.Tuple3)38 ListStateDescriptor (org.apache.flink.api.common.state.ListStateDescriptor)37 Watermark (org.apache.flink.streaming.api.watermark.Watermark)29 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)28 TumblingEventTimeWindows (org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows)28 KeyedOneInputStreamOperatorTestHarness (org.apache.flink.streaming.util.KeyedOneInputStreamOperatorTestHarness)28 ReducingStateDescriptor (org.apache.flink.api.common.state.ReducingStateDescriptor)26 EventTimeTrigger (org.apache.flink.streaming.api.windowing.triggers.EventTimeTrigger)25 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)22 Configuration (org.apache.flink.configuration.Configuration)22 StreamRecord (org.apache.flink.streaming.runtime.streamrecord.StreamRecord)19 Tuple (org.apache.flink.api.java.tuple.Tuple)13 FoldingStateDescriptor (org.apache.flink.api.common.state.FoldingStateDescriptor)12 TumblingProcessingTimeWindows (org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows)12