use of org.apache.flink.streaming.api.watermark.Watermark in project flink by apache.
the class WindowOperatorTest method testSlidingEventTimeWindows.
private void testSlidingEventTimeWindows(OneInputStreamOperatorTestHarness<Tuple2<String, Integer>, Tuple2<String, Integer>> testHarness) throws Exception {
ConcurrentLinkedQueue<Object> expectedOutput = new ConcurrentLinkedQueue<>();
// add elements out-of-order
testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 3999));
testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 3000));
testHarness.processElement(new StreamRecord<>(new Tuple2<>("key1", 1), 20));
testHarness.processElement(new StreamRecord<>(new Tuple2<>("key1", 1), 0));
testHarness.processElement(new StreamRecord<>(new Tuple2<>("key1", 1), 999));
testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 1998));
testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 1999));
testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 1000));
testHarness.processWatermark(new Watermark(999));
expectedOutput.add(new StreamRecord<>(new Tuple2<>("key1", 3), 999));
expectedOutput.add(new Watermark(999));
TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expectedOutput, testHarness.getOutput(), new Tuple2ResultSortComparator());
testHarness.processWatermark(new Watermark(1999));
expectedOutput.add(new StreamRecord<>(new Tuple2<>("key1", 3), 1999));
expectedOutput.add(new StreamRecord<>(new Tuple2<>("key2", 3), 1999));
expectedOutput.add(new Watermark(1999));
TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expectedOutput, testHarness.getOutput(), new Tuple2ResultSortComparator());
testHarness.processWatermark(new Watermark(2999));
expectedOutput.add(new StreamRecord<>(new Tuple2<>("key1", 3), 2999));
expectedOutput.add(new StreamRecord<>(new Tuple2<>("key2", 3), 2999));
expectedOutput.add(new Watermark(2999));
TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expectedOutput, testHarness.getOutput(), new Tuple2ResultSortComparator());
// do a snapshot, close and restore again
OperatorStateHandles snapshot = testHarness.snapshot(0L, 0L);
testHarness.close();
testHarness.setup();
testHarness.initializeState(snapshot);
testHarness.open();
testHarness.processWatermark(new Watermark(3999));
expectedOutput.add(new StreamRecord<>(new Tuple2<>("key2", 5), 3999));
expectedOutput.add(new Watermark(3999));
TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expectedOutput, testHarness.getOutput(), new Tuple2ResultSortComparator());
testHarness.processWatermark(new Watermark(4999));
expectedOutput.add(new StreamRecord<>(new Tuple2<>("key2", 2), 4999));
expectedOutput.add(new Watermark(4999));
TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expectedOutput, testHarness.getOutput(), new Tuple2ResultSortComparator());
testHarness.processWatermark(new Watermark(5999));
expectedOutput.add(new StreamRecord<>(new Tuple2<>("key2", 2), 5999));
expectedOutput.add(new Watermark(5999));
TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expectedOutput, testHarness.getOutput(), new Tuple2ResultSortComparator());
// those don't have any effect...
testHarness.processWatermark(new Watermark(6999));
testHarness.processWatermark(new Watermark(7999));
expectedOutput.add(new Watermark(6999));
expectedOutput.add(new Watermark(7999));
TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expectedOutput, testHarness.getOutput(), new Tuple2ResultSortComparator());
}
use of org.apache.flink.streaming.api.watermark.Watermark in project flink by apache.
the class WindowOperatorTest method testCleanupTimerWithEmptyFoldingStateForSessionWindows.
// TODO this test seems invalid, as it uses the unsupported combination of merging windows and folding window state
@Test
public void testCleanupTimerWithEmptyFoldingStateForSessionWindows() throws Exception {
final int GAP_SIZE = 3;
final long LATENESS = 10;
TypeInformation<Tuple2<String, Integer>> inputType = TypeInfoParser.parse("Tuple2<String, Integer>");
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);
}
}, inputType);
windowStateDesc.initializeSerializerUnlessSet(new ExecutionConfig());
WindowOperator<String, Tuple2<String, Integer>, Tuple2<String, Integer>, Tuple2<String, Integer>, TimeWindow> operator = new WindowOperator<>(EventTimeSessionWindows.withGap(Time.seconds(GAP_SIZE)), new TimeWindow.Serializer(), new TupleKeySelector(), BasicTypeInfo.STRING_TYPE_INFO.createSerializer(new ExecutionConfig()), windowStateDesc, new InternalSingleValueWindowFunction<>(new PassThroughFunction()), EventTimeTrigger.create(), LATENESS, null);
OneInputStreamOperatorTestHarness<Tuple2<String, Integer>, Tuple2<String, Integer>> testHarness = new KeyedOneInputStreamOperatorTestHarness<>(operator, new TupleKeySelector(), BasicTypeInfo.STRING_TYPE_INFO);
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();
}
use of org.apache.flink.streaming.api.watermark.Watermark in project flink by apache.
the class StatusWatermarkValveTest method testMultipleInputValve.
/**
* Tests that valves work as expected when they handle multiple input channels (tested with 3).
* Tested behaviours are explained as inline comments.
*/
@Test
public void testMultipleInputValve() {
BufferedValveOutputHandler valveOutput = new BufferedValveOutputHandler();
StatusWatermarkValve valve = new StatusWatermarkValve(3, valveOutput);
// ------------------------------------------------------------------------
// Ensure that watermarks are output only when all
// channels have been input some watermark.
// ------------------------------------------------------------------------
valve.inputWatermark(new Watermark(0), 0);
assertTrue(valveOutput.hasNoOutputWatermarks());
assertTrue(valveOutput.hasNoOutputStreamStatuses());
valve.inputWatermark(new Watermark(0), 1);
assertTrue(valveOutput.hasNoOutputWatermarks());
assertTrue(valveOutput.hasNoOutputStreamStatuses());
valve.inputWatermark(new Watermark(0), 2);
assertEquals(new Watermark(0), valveOutput.popLastOutputWatermark());
assertTrue(valveOutput.hasNoOutputWatermarks());
assertTrue(valveOutput.hasNoOutputStreamStatuses());
// ------------------------------------------------------------------------
// Ensure that watermarks are output as soon as the overall min
// watermark across all channels have advanced.
// ------------------------------------------------------------------------
valve.inputWatermark(new Watermark(12), 0);
assertTrue(valveOutput.hasNoOutputWatermarks());
assertTrue(valveOutput.hasNoOutputStreamStatuses());
valve.inputWatermark(new Watermark(8), 2);
assertTrue(valveOutput.hasNoOutputWatermarks());
assertTrue(valveOutput.hasNoOutputStreamStatuses());
valve.inputWatermark(new Watermark(10), 2);
assertTrue(valveOutput.hasNoOutputWatermarks());
assertTrue(valveOutput.hasNoOutputStreamStatuses());
valve.inputWatermark(new Watermark(15), 1);
// lowest watermark across all channels is now channel 2, with watermark @ 10
assertEquals(new Watermark(10), valveOutput.popLastOutputWatermark());
assertTrue(valveOutput.hasNoOutputWatermarks());
assertTrue(valveOutput.hasNoOutputStreamStatuses());
// ------------------------------------------------------------------------
// Ensure that decreasing watermarks are ignored
// ------------------------------------------------------------------------
valve.inputWatermark(new Watermark(6), 0);
assertTrue(valveOutput.hasNoOutputWatermarks());
assertTrue(valveOutput.hasNoOutputStreamStatuses());
// ------------------------------------------------------------------------
// Ensure that when some input channel becomes idle, that channel will
// no longer be accounted for when advancing the watermark.
// ------------------------------------------------------------------------
// marking channel 2 as IDLE shouldn't result in overall status toggle for the valve,
// because there are still other active channels (0 and 1), so there should not be any
// stream status outputs;
// also, now that channel 2 is IDLE, the overall min watermark is 12 (from channel 0),
// so the valve should output that
valve.inputStreamStatus(StreamStatus.IDLE, 2);
assertEquals(new Watermark(12), valveOutput.popLastOutputWatermark());
assertTrue(valveOutput.hasNoOutputWatermarks());
assertTrue(valveOutput.hasNoOutputStreamStatuses());
// from now on, since channel 2 is IDLE, the valve should use watermarks only from
// channel 0 and 1 to find the min watermark, even if channel 2 has the lowest watermark (10)
valve.inputWatermark(new Watermark(17), 0);
assertEquals(new Watermark(15), valveOutput.popLastOutputWatermark());
assertTrue(valveOutput.hasNoOutputWatermarks());
assertTrue(valveOutput.hasNoOutputStreamStatuses());
valve.inputWatermark(new Watermark(25), 0);
assertTrue(valveOutput.hasNoOutputWatermarks());
assertTrue(valveOutput.hasNoOutputStreamStatuses());
valve.inputWatermark(new Watermark(20), 1);
assertEquals(new Watermark(20), valveOutput.popLastOutputWatermark());
assertTrue(valveOutput.hasNoOutputWatermarks());
assertTrue(valveOutput.hasNoOutputStreamStatuses());
// ------------------------------------------------------------------------
// Ensure that after some channel resumes to be ACTIVE, it needs to
// catch up" with the current overall min watermark before it can be
// accounted for again when finding the min watermark across channels.
// Also tests that before the resumed channel catches up, the overall
// min watermark can still advance with watermarks of other channels.
// ------------------------------------------------------------------------
// resuming channel 2 to be ACTIVE shouldn't result in overall status toggle for the valve,
// because the valve wasn't overall IDLE, so there should not be any stream status outputs;
valve.inputStreamStatus(StreamStatus.ACTIVE, 2);
assertTrue(valveOutput.hasNoOutputWatermarks());
assertTrue(valveOutput.hasNoOutputStreamStatuses());
// although watermarks for channel 2 will now be accepted, it still
// hasn't caught up with the overall min watermark (20)
valve.inputWatermark(new Watermark(18), 2);
assertTrue(valveOutput.hasNoOutputWatermarks());
assertTrue(valveOutput.hasNoOutputStreamStatuses());
// since channel 2 hasn't caught up yet, it is still ignored when advancing new min watermarks
valve.inputWatermark(new Watermark(22), 1);
assertEquals(new Watermark(22), valveOutput.popLastOutputWatermark());
assertTrue(valveOutput.hasNoOutputWatermarks());
assertTrue(valveOutput.hasNoOutputStreamStatuses());
valve.inputWatermark(new Watermark(28), 0);
assertTrue(valveOutput.hasNoOutputWatermarks());
assertTrue(valveOutput.hasNoOutputStreamStatuses());
valve.inputWatermark(new Watermark(33), 1);
assertEquals(new Watermark(28), valveOutput.popLastOutputWatermark());
assertTrue(valveOutput.hasNoOutputWatermarks());
assertTrue(valveOutput.hasNoOutputStreamStatuses());
// now, channel 2 has caught up with the overall min watermark
valve.inputWatermark(new Watermark(30), 2);
assertTrue(valveOutput.hasNoOutputWatermarks());
assertTrue(valveOutput.hasNoOutputStreamStatuses());
valve.inputWatermark(new Watermark(31), 0);
// this acknowledges that channel 2's watermark is being accounted for again
assertEquals(new Watermark(30), valveOutput.popLastOutputWatermark());
assertTrue(valveOutput.hasNoOutputWatermarks());
assertTrue(valveOutput.hasNoOutputStreamStatuses());
valve.inputWatermark(new Watermark(34), 2);
assertEquals(new Watermark(31), valveOutput.popLastOutputWatermark());
assertTrue(valveOutput.hasNoOutputWatermarks());
assertTrue(valveOutput.hasNoOutputStreamStatuses());
// ------------------------------------------------------------------------
// Ensure that once all channels are IDLE, the valve should also
// determine itself to be IDLE output a IDLE stream status
// ------------------------------------------------------------------------
valve.inputStreamStatus(StreamStatus.IDLE, 0);
// this is because once channel 0 becomes IDLE,
// the new min watermark will be 33 (channel 1)
assertEquals(new Watermark(33), valveOutput.popLastOutputWatermark());
assertTrue(valveOutput.hasNoOutputWatermarks());
assertTrue(valveOutput.hasNoOutputStreamStatuses());
valve.inputStreamStatus(StreamStatus.IDLE, 2);
assertTrue(valveOutput.hasNoOutputWatermarks());
assertTrue(valveOutput.hasNoOutputStreamStatuses());
valve.inputStreamStatus(StreamStatus.IDLE, 1);
assertEquals(StreamStatus.IDLE, valveOutput.popLastOutputStreamStatus());
assertTrue(valveOutput.hasNoOutputWatermarks());
assertTrue(valveOutput.hasNoOutputStreamStatuses());
// ------------------------------------------------------------------------
// Ensure that channels gradually become ACTIVE again, the above behaviours
// still hold. Also ensure that as soon as one of the input channels
// become ACTIVE, the valve is ACTIVE again and outputs an ACTIVE stream status.
// ------------------------------------------------------------------------
// let channel 0 resume to be ACTIVE
valve.inputStreamStatus(StreamStatus.ACTIVE, 0);
assertEquals(StreamStatus.ACTIVE, valveOutput.popLastOutputStreamStatus());
assertTrue(valveOutput.hasNoOutputWatermarks());
assertTrue(valveOutput.hasNoOutputStreamStatuses());
// channel 0 is the only ACTIVE channel now, and is the only channel
// accounted for when advancing min watermark
valve.inputWatermark(new Watermark(36), 0);
assertEquals(new Watermark(36), valveOutput.popLastOutputWatermark());
assertTrue(valveOutput.hasNoOutputWatermarks());
assertTrue(valveOutput.hasNoOutputStreamStatuses());
// new also let channel 1 become ACTIVE
valve.inputStreamStatus(StreamStatus.ACTIVE, 1);
assertTrue(valveOutput.hasNoOutputWatermarks());
assertTrue(valveOutput.hasNoOutputStreamStatuses());
// channel 1 is still behind overall min watermark
valve.inputWatermark(new Watermark(35), 1);
assertTrue(valveOutput.hasNoOutputWatermarks());
assertTrue(valveOutput.hasNoOutputStreamStatuses());
// since channel 1 is still behind, channel 0 remains to be the only
// channel used to advance min watermark
valve.inputWatermark(new Watermark(37), 0);
assertEquals(new Watermark(37), valveOutput.popLastOutputWatermark());
assertTrue(valveOutput.hasNoOutputWatermarks());
assertTrue(valveOutput.hasNoOutputStreamStatuses());
// now, channel 1 has caught up with the overall min watermark
valve.inputWatermark(new Watermark(38), 1);
assertTrue(valveOutput.hasNoOutputWatermarks());
assertTrue(valveOutput.hasNoOutputStreamStatuses());
valve.inputWatermark(new Watermark(40), 0);
// this acknowledges that channel 1's watermark is being accounted for again
assertEquals(new Watermark(38), valveOutput.popLastOutputWatermark());
assertTrue(valveOutput.hasNoOutputWatermarks());
assertTrue(valveOutput.hasNoOutputStreamStatuses());
}
use of org.apache.flink.streaming.api.watermark.Watermark in project flink by apache.
the class WindowOperatorMigrationTest method testRestoreApplyEventTimeWindowsFromFlink11.
@Test
@SuppressWarnings("unchecked")
public void testRestoreApplyEventTimeWindowsFromFlink11() throws Exception {
final int WINDOW_SIZE = 3;
TypeInformation<Tuple2<String, Integer>> inputType = TypeInfoParser.parse("Tuple2<String, Integer>");
ListStateDescriptor<Tuple2<String, Integer>> stateDesc = new ListStateDescriptor<>("window-contents", inputType.createSerializer(new ExecutionConfig()));
WindowOperator<String, Tuple2<String, Integer>, Iterable<Tuple2<String, Integer>>, Tuple2<String, Integer>, TimeWindow> operator = new WindowOperator<>(TumblingEventTimeWindows.of(Time.of(WINDOW_SIZE, TimeUnit.SECONDS)), new TimeWindow.Serializer(), new TupleKeySelector(), BasicTypeInfo.STRING_TYPE_INFO.createSerializer(new ExecutionConfig()), stateDesc, new InternalIterableWindowFunction<>(new RichSumReducer<TimeWindow>()), EventTimeTrigger.create(), 0, null);
ConcurrentLinkedQueue<Object> expectedOutput = new ConcurrentLinkedQueue<>();
/*
operator.setInputType(TypeInfoParser.<Tuple2<String, Integer>>parse("Tuple2<String, Integer>"), new ExecutionConfig());
OneInputStreamOperatorTestHarness<Tuple2<String, Integer>, Tuple2<String, Integer>> testHarness =
new OneInputStreamOperatorTestHarness<>(operator);
testHarness.configureForKeyedStream(new TupleKeySelector(), BasicTypeInfo.STRING_TYPE_INFO);
testHarness.setup();
testHarness.open();
// add elements out-of-order
testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 3999));
testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 3000));
testHarness.processElement(new StreamRecord<>(new Tuple2<>("key1", 1), 20));
testHarness.processElement(new StreamRecord<>(new Tuple2<>("key1", 1), 0));
testHarness.processElement(new StreamRecord<>(new Tuple2<>("key1", 1), 999));
testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 1998));
testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 1999));
testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), 1000));
testHarness.processWatermark(new Watermark(999));
expectedOutput.add(new Watermark(999));
TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expectedOutput, testHarness.getOutput(), new Tuple2ResultSortComparator());
testHarness.processWatermark(new Watermark(1999));
expectedOutput.add(new Watermark(1999));
TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expectedOutput, testHarness.getOutput(), new Tuple2ResultSortComparator());
// do snapshot and save to file
StreamTaskState snapshot = testHarness.snapshot(0L, 0L);
testHarness.snaphotToFile(snapshot, "src/test/resources/win-op-migration-test-apply-event-time-flink1.1-snapshot");
testHarness.close();
*/
OneInputStreamOperatorTestHarness<Tuple2<String, Integer>, Tuple2<String, Integer>> testHarness = new KeyedOneInputStreamOperatorTestHarness<>(operator, new TupleKeySelector(), BasicTypeInfo.STRING_TYPE_INFO);
testHarness.setup();
testHarness.initializeStateFromLegacyCheckpoint(getResourceFilename("win-op-migration-test-apply-event-time-flink1.1-snapshot"));
testHarness.open();
testHarness.processWatermark(new Watermark(2999));
expectedOutput.add(new StreamRecord<>(new Tuple2<>("key1", 3), 2999));
expectedOutput.add(new StreamRecord<>(new Tuple2<>("key2", 3), 2999));
expectedOutput.add(new Watermark(2999));
testHarness.processWatermark(new Watermark(3999));
expectedOutput.add(new Watermark(3999));
testHarness.processWatermark(new Watermark(4999));
expectedOutput.add(new Watermark(4999));
testHarness.processWatermark(new Watermark(5999));
expectedOutput.add(new StreamRecord<>(new Tuple2<>("key2", 2), 5999));
expectedOutput.add(new Watermark(5999));
TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expectedOutput, testHarness.getOutput(), new Tuple2ResultSortComparator());
testHarness.close();
}
use of org.apache.flink.streaming.api.watermark.Watermark in project flink by apache.
the class CEPMigration11to13Test method testNonKeyedCEPFunctionMigration.
@Test
public void testNonKeyedCEPFunctionMigration() throws Exception {
final Event startEvent = new Event(42, "start", 1.0);
final SubEvent middleEvent = new SubEvent(42, "foo", 1.0, 10.0);
final Event endEvent = new Event(42, "end", 1.0);
// uncomment these lines for regenerating the snapshot on Flink 1.1
/*
OneInputStreamOperatorTestHarness<Event, Map<String, Event>> harness = new OneInputStreamOperatorTestHarness<>(
new CEPPatternOperator<>(
Event.createTypeSerializer(),
false,
new NFAFactory()));
harness.open();
harness.processElement(new StreamRecord<Event>(startEvent, 1));
harness.processElement(new StreamRecord<Event>(new Event(42, "foobar", 1.0), 2));
harness.processElement(new StreamRecord<Event>(new SubEvent(42, "barfoo", 1.0, 5.0), 3));
harness.processWatermark(new Watermark(2));
// simulate snapshot/restore with empty element queue but NFA state
StreamTaskState snapshot = harness.snapshot(1, 1);
FileOutputStream out = new FileOutputStream(
"src/test/resources/cep-non-keyed-snapshot-1.1");
ObjectOutputStream oos = new ObjectOutputStream(out);
oos.writeObject(snapshot);
out.close();
harness.close();
*/
NullByteKeySelector keySelector = new NullByteKeySelector();
OneInputStreamOperatorTestHarness<Event, Map<String, Event>> harness = new KeyedOneInputStreamOperatorTestHarness<>(new KeyedCEPPatternOperator<>(Event.createTypeSerializer(), false, keySelector, ByteSerializer.INSTANCE, new NFAFactory(), false), keySelector, BasicTypeInfo.BYTE_TYPE_INFO);
harness.setup();
harness.initializeStateFromLegacyCheckpoint(getResourceFilename("cep-non-keyed-snapshot-1.1"));
harness.open();
harness.processElement(new StreamRecord<Event>(middleEvent, 3));
harness.processElement(new StreamRecord<>(new Event(42, "start", 1.0), 4));
harness.processElement(new StreamRecord<>(endEvent, 5));
harness.processWatermark(new Watermark(Long.MAX_VALUE));
ConcurrentLinkedQueue<Object> result = harness.getOutput();
// watermark and the result
assertEquals(2, result.size());
Object resultObject = result.poll();
assertTrue(resultObject instanceof StreamRecord);
StreamRecord<?> resultRecord = (StreamRecord<?>) resultObject;
assertTrue(resultRecord.getValue() instanceof Map);
@SuppressWarnings("unchecked") Map<String, Event> patternMap = (Map<String, Event>) resultRecord.getValue();
assertEquals(startEvent, patternMap.get("start"));
assertEquals(middleEvent, patternMap.get("middle"));
assertEquals(endEvent, patternMap.get("end"));
harness.close();
}
Aggregations