Search in sources :

Example 51 with StreamRecord

use of org.apache.flink.streaming.runtime.streamrecord.StreamRecord in project flink by apache.

the class ContinuousEventTimeTriggerTest method testWindowSeparationAndFiring.

/**
	 * Verify that state <TimeWindow>of separate windows does not leak into other windows.
	 */
@Test
public void testWindowSeparationAndFiring() throws Exception {
    TriggerTestHarness<Object, TimeWindow> testHarness = new TriggerTestHarness<>(ContinuousEventTimeTrigger.<TimeWindow>of(Time.hours(1)), new TimeWindow.Serializer());
    // inject several elements
    assertEquals(TriggerResult.CONTINUE, testHarness.processElement(new StreamRecord<Object>(1), new TimeWindow(0, 2)));
    assertEquals(TriggerResult.CONTINUE, testHarness.processElement(new StreamRecord<Object>(1), new TimeWindow(0, 2)));
    assertEquals(TriggerResult.CONTINUE, testHarness.processElement(new StreamRecord<Object>(1), new TimeWindow(0, 2)));
    assertEquals(TriggerResult.CONTINUE, testHarness.processElement(new StreamRecord<Object>(1), new TimeWindow(2, 4)));
    assertEquals(TriggerResult.CONTINUE, testHarness.processElement(new StreamRecord<Object>(1), new TimeWindow(2, 4)));
    assertEquals(2, testHarness.numStateEntries());
    assertEquals(0, testHarness.numProcessingTimeTimers());
    assertEquals(4, testHarness.numEventTimeTimers());
    assertEquals(2, testHarness.numEventTimeTimers(new TimeWindow(0, 2)));
    assertEquals(2, testHarness.numEventTimeTimers(new TimeWindow(2, 4)));
    Collection<Tuple2<TimeWindow, TriggerResult>> triggerResults = testHarness.advanceWatermark(2);
    boolean sawFiring = false;
    for (Tuple2<TimeWindow, TriggerResult> r : triggerResults) {
        if (r.f0.equals(new TimeWindow(0, 2))) {
            sawFiring = true;
            assertTrue(r.f1.equals(TriggerResult.FIRE));
        }
    }
    assertTrue(sawFiring);
    assertEquals(2, testHarness.numStateEntries());
    assertEquals(0, testHarness.numProcessingTimeTimers());
    assertEquals(3, testHarness.numEventTimeTimers());
    assertEquals(1, testHarness.numEventTimeTimers(new TimeWindow(0, 2)));
    assertEquals(2, testHarness.numEventTimeTimers(new TimeWindow(2, 4)));
    triggerResults = testHarness.advanceWatermark(4);
    sawFiring = false;
    for (Tuple2<TimeWindow, TriggerResult> r : triggerResults) {
        if (r.f0.equals(new TimeWindow(2, 4))) {
            sawFiring = true;
            assertTrue(r.f1.equals(TriggerResult.FIRE));
        }
    }
    assertTrue(sawFiring);
    assertEquals(2, testHarness.numStateEntries());
    assertEquals(0, testHarness.numProcessingTimeTimers());
    assertEquals(2, testHarness.numEventTimeTimers());
}
Also used : StreamRecord(org.apache.flink.streaming.runtime.streamrecord.StreamRecord) Tuple2(org.apache.flink.api.java.tuple.Tuple2) TriggerResult(org.apache.flink.streaming.api.windowing.triggers.TriggerResult) TimeWindow(org.apache.flink.streaming.api.windowing.windows.TimeWindow) Test(org.junit.Test)

Example 52 with StreamRecord

use of org.apache.flink.streaming.runtime.streamrecord.StreamRecord in project flink by apache.

the class ContinuousEventTimeTriggerTest method testTriggerHandlesAllOnTimerCalls.

/**
	 * Verify that the trigger doesn't fail with an NPE if we insert a timer firing when there is
	 * no trigger state.
	 */
@Test
public void testTriggerHandlesAllOnTimerCalls() throws Exception {
    TriggerTestHarness<Object, TimeWindow> testHarness = new TriggerTestHarness<>(ContinuousEventTimeTrigger.<TimeWindow>of(Time.milliseconds(5)), new TimeWindow.Serializer());
    assertEquals(0, testHarness.numStateEntries());
    assertEquals(0, testHarness.numProcessingTimeTimers());
    assertEquals(0, testHarness.numEventTimeTimers());
    // this will make the elements we now process fall into late windows, i.e. no trigger state
    // will be created
    testHarness.advanceWatermark(10);
    // late fires immediately
    assertEquals(TriggerResult.FIRE, testHarness.processElement(new StreamRecord<Object>(1), new TimeWindow(0, 2)));
    // simulate a GC timer firing
    testHarness.invokeOnEventTime(20, new TimeWindow(0, 2));
}
Also used : StreamRecord(org.apache.flink.streaming.runtime.streamrecord.StreamRecord) TimeWindow(org.apache.flink.streaming.api.windowing.windows.TimeWindow) Test(org.junit.Test)

Example 53 with StreamRecord

use of org.apache.flink.streaming.runtime.streamrecord.StreamRecord in project flink by apache.

the class ContinuousEventTimeTriggerTest method testMergingWindows.

@Test
public void testMergingWindows() throws Exception {
    TriggerTestHarness<Object, TimeWindow> testHarness = new TriggerTestHarness<>(ContinuousEventTimeTrigger.<TimeWindow>of(Time.hours(1)), new TimeWindow.Serializer());
    assertTrue(ContinuousEventTimeTrigger.<TimeWindow>of(Time.hours(1)).canMerge());
    assertEquals(TriggerResult.CONTINUE, testHarness.processElement(new StreamRecord<Object>(1), new TimeWindow(0, 2)));
    assertEquals(TriggerResult.CONTINUE, testHarness.processElement(new StreamRecord<Object>(1), new TimeWindow(2, 4)));
    assertEquals(2, testHarness.numStateEntries());
    assertEquals(0, testHarness.numProcessingTimeTimers());
    assertEquals(4, testHarness.numEventTimeTimers());
    assertEquals(2, testHarness.numEventTimeTimers(new TimeWindow(0, 2)));
    assertEquals(2, testHarness.numEventTimeTimers(new TimeWindow(2, 4)));
    testHarness.mergeWindows(new TimeWindow(0, 4), Lists.newArrayList(new TimeWindow(0, 2), new TimeWindow(2, 4)));
    assertEquals(1, testHarness.numStateEntries());
    assertEquals(0, testHarness.numProcessingTimeTimers());
    // on merging, timers are not cleaned up
    assertEquals(5, testHarness.numEventTimeTimers());
    assertEquals(2, testHarness.numEventTimeTimers(new TimeWindow(0, 2)));
    assertEquals(2, testHarness.numEventTimeTimers(new TimeWindow(2, 4)));
    assertEquals(1, testHarness.numEventTimeTimers(new TimeWindow(0, 4)));
    Collection<Tuple2<TimeWindow, TriggerResult>> triggerResults = testHarness.advanceWatermark(4);
    boolean sawFiring = false;
    for (Tuple2<TimeWindow, TriggerResult> r : triggerResults) {
        if (r.f0.equals(new TimeWindow(0, 4))) {
            sawFiring = true;
            assertTrue(r.f1.equals(TriggerResult.FIRE));
        }
    }
    assertTrue(sawFiring);
    assertEquals(1, testHarness.numStateEntries());
    assertEquals(0, testHarness.numProcessingTimeTimers());
    assertEquals(1, testHarness.numEventTimeTimers());
}
Also used : StreamRecord(org.apache.flink.streaming.runtime.streamrecord.StreamRecord) Tuple2(org.apache.flink.api.java.tuple.Tuple2) TriggerResult(org.apache.flink.streaming.api.windowing.triggers.TriggerResult) TimeWindow(org.apache.flink.streaming.api.windowing.windows.TimeWindow) Test(org.junit.Test)

Example 54 with StreamRecord

use of org.apache.flink.streaming.runtime.streamrecord.StreamRecord in project flink by apache.

the class CountTriggerTest method testMergeSubsumingWindow.

@Test
public void testMergeSubsumingWindow() throws Exception {
    TriggerTestHarness<Object, TimeWindow> testHarness = new TriggerTestHarness<>(CountTrigger.<TimeWindow>of(3), new TimeWindow.Serializer());
    assertEquals(TriggerResult.CONTINUE, testHarness.processElement(new StreamRecord<Object>(1), new TimeWindow(2, 4)));
    assertEquals(TriggerResult.CONTINUE, testHarness.processElement(new StreamRecord<Object>(1), new TimeWindow(4, 6)));
    // shouldn't have any timers
    assertEquals(0, testHarness.numProcessingTimeTimers());
    assertEquals(0, testHarness.numEventTimeTimers());
    assertEquals(2, testHarness.numStateEntries());
    assertEquals(1, testHarness.numStateEntries(new TimeWindow(2, 4)));
    assertEquals(1, testHarness.numStateEntries(new TimeWindow(4, 6)));
    testHarness.mergeWindows(new TimeWindow(0, 8), Lists.newArrayList(new TimeWindow(2, 4), new TimeWindow(4, 6)));
    assertEquals(1, testHarness.numStateEntries());
    assertEquals(0, testHarness.numStateEntries(new TimeWindow(2, 4)));
    assertEquals(0, testHarness.numStateEntries(new TimeWindow(4, 6)));
    assertEquals(1, testHarness.numStateEntries(new TimeWindow(0, 8)));
    assertEquals(TriggerResult.FIRE, testHarness.processElement(new StreamRecord<Object>(1), new TimeWindow(0, 8)));
    assertEquals(0, testHarness.numStateEntries());
}
Also used : StreamRecord(org.apache.flink.streaming.runtime.streamrecord.StreamRecord) TimeWindow(org.apache.flink.streaming.api.windowing.windows.TimeWindow) Test(org.junit.Test)

Example 55 with StreamRecord

use of org.apache.flink.streaming.runtime.streamrecord.StreamRecord in project flink by apache.

the class EventTimeTriggerTest method testClear.

/**
	 * Verify that clear() does not leak across windows.
	 */
@Test
public void testClear() throws Exception {
    TriggerTestHarness<Object, TimeWindow> testHarness = new TriggerTestHarness<>(EventTimeTrigger.create(), new TimeWindow.Serializer());
    assertEquals(TriggerResult.CONTINUE, testHarness.processElement(new StreamRecord<Object>(1), new TimeWindow(0, 2)));
    assertEquals(TriggerResult.CONTINUE, testHarness.processElement(new StreamRecord<Object>(1), new TimeWindow(2, 4)));
    assertEquals(0, testHarness.numStateEntries());
    assertEquals(0, testHarness.numProcessingTimeTimers());
    assertEquals(2, testHarness.numEventTimeTimers());
    assertEquals(1, testHarness.numEventTimeTimers(new TimeWindow(0, 2)));
    assertEquals(1, testHarness.numEventTimeTimers(new TimeWindow(2, 4)));
    testHarness.clearTriggerState(new TimeWindow(2, 4));
    assertEquals(0, testHarness.numStateEntries());
    assertEquals(0, testHarness.numProcessingTimeTimers());
    assertEquals(1, testHarness.numEventTimeTimers());
    assertEquals(1, testHarness.numEventTimeTimers(new TimeWindow(0, 2)));
    assertEquals(0, testHarness.numEventTimeTimers(new TimeWindow(2, 4)));
    testHarness.clearTriggerState(new TimeWindow(0, 2));
    assertEquals(0, testHarness.numStateEntries());
    assertEquals(0, testHarness.numProcessingTimeTimers());
    assertEquals(0, testHarness.numEventTimeTimers());
}
Also used : StreamRecord(org.apache.flink.streaming.runtime.streamrecord.StreamRecord) TimeWindow(org.apache.flink.streaming.api.windowing.windows.TimeWindow) Test(org.junit.Test)

Aggregations

StreamRecord (org.apache.flink.streaming.runtime.streamrecord.StreamRecord)76 Test (org.junit.Test)50 ListStateDescriptor (org.apache.flink.api.common.state.ListStateDescriptor)27 TypeSerializer (org.apache.flink.api.common.typeutils.TypeSerializer)27 StreamElementSerializer (org.apache.flink.streaming.runtime.streamrecord.StreamElementSerializer)27 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)21 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)20 TimeWindow (org.apache.flink.streaming.api.windowing.windows.TimeWindow)19 KeyedOneInputStreamOperatorTestHarness (org.apache.flink.streaming.util.KeyedOneInputStreamOperatorTestHarness)19 Watermark (org.apache.flink.streaming.api.watermark.Watermark)17 RichFunction (org.apache.flink.api.common.functions.RichFunction)16 ArrayList (java.util.ArrayList)14 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)14 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)14 Map (java.util.Map)11 ReducingStateDescriptor (org.apache.flink.api.common.state.ReducingStateDescriptor)11 Event (org.apache.flink.cep.Event)11 HashMap (java.util.HashMap)10 PublicEvolving (org.apache.flink.annotation.PublicEvolving)9 MergingWindowAssigner (org.apache.flink.streaming.api.windowing.assigners.MergingWindowAssigner)9