Search in sources :

Example 31 with Watermark

use of org.apache.flink.streaming.api.watermark.Watermark in project flink by apache.

the class IngestionTimeExtractorTest method testMonotonousTimestamps.

@Test
public void testMonotonousTimestamps() {
    AssignerWithPeriodicWatermarks<String> assigner = new IngestionTimeExtractor<>();
    long maxRecordSoFar = 0L;
    long maxWatermarkSoFar = 0L;
    for (int i = 0; i < 1343; i++) {
        if (i % 7 == 1) {
            Watermark mark = assigner.getCurrentWatermark();
            assertNotNull(mark);
            // increasing watermarks
            assertTrue(mark.getTimestamp() >= maxWatermarkSoFar);
            maxWatermarkSoFar = mark.getTimestamp();
            // tight watermarks
            assertTrue(mark.getTimestamp() >= maxRecordSoFar - 1);
        } else {
            long next = assigner.extractTimestamp("a", Long.MIN_VALUE);
            // increasing timestamps
            assertTrue(next >= maxRecordSoFar);
            // timestamps are never below or at the watermark
            assertTrue(next > maxWatermarkSoFar);
            maxRecordSoFar = next;
        }
        if (i % 9 == 0) {
            try {
                Thread.sleep(1);
            } catch (InterruptedException ignored) {
            }
        }
    }
}
Also used : Watermark(org.apache.flink.streaming.api.watermark.Watermark) Test(org.junit.Test)

Example 32 with Watermark

use of org.apache.flink.streaming.api.watermark.Watermark in project flink by apache.

the class ExtractTimestampsOperator method processElement.

@Override
public void processElement(StreamRecord<T> element) throws Exception {
    long newTimestamp = userFunction.extractTimestamp(element.getValue(), element.getTimestamp());
    output.collect(element.replace(element.getValue(), newTimestamp));
    long watermark = userFunction.extractWatermark(element.getValue(), newTimestamp);
    if (watermark > currentWatermark) {
        currentWatermark = watermark;
        output.emitWatermark(new Watermark(currentWatermark));
    }
}
Also used : Watermark(org.apache.flink.streaming.api.watermark.Watermark)

Example 33 with Watermark

use of org.apache.flink.streaming.api.watermark.Watermark in project flink by apache.

the class TimestampsAndPeriodicWatermarksOperator method onProcessingTime.

@Override
public void onProcessingTime(long timestamp) throws Exception {
    // register next timer
    Watermark newWatermark = userFunction.getCurrentWatermark();
    if (newWatermark != null && newWatermark.getTimestamp() > currentWatermark) {
        currentWatermark = newWatermark.getTimestamp();
        // emit watermark
        output.emitWatermark(newWatermark);
    }
    long now = getProcessingTimeService().getCurrentProcessingTime();
    getProcessingTimeService().registerTimer(now + watermarkInterval, this);
}
Also used : Watermark(org.apache.flink.streaming.api.watermark.Watermark)

Example 34 with Watermark

use of org.apache.flink.streaming.api.watermark.Watermark in project flink by apache.

the class TimestampsAndPeriodicWatermarksOperator method close.

@Override
public void close() throws Exception {
    super.close();
    // emit a final watermark
    Watermark newWatermark = userFunction.getCurrentWatermark();
    if (newWatermark != null && newWatermark.getTimestamp() > currentWatermark) {
        currentWatermark = newWatermark.getTimestamp();
        // emit watermark
        output.emitWatermark(newWatermark);
    }
}
Also used : Watermark(org.apache.flink.streaming.api.watermark.Watermark)

Example 35 with Watermark

use of org.apache.flink.streaming.api.watermark.Watermark in project flink by apache.

the class TimestampsAndPunctuatedWatermarksOperator method processElement.

@Override
public void processElement(StreamRecord<T> element) throws Exception {
    final T value = element.getValue();
    final long newTimestamp = userFunction.extractTimestamp(value, element.hasTimestamp() ? element.getTimestamp() : Long.MIN_VALUE);
    output.collect(element.replace(element.getValue(), newTimestamp));
    final Watermark nextWatermark = userFunction.checkAndGetNextWatermark(value, newTimestamp);
    if (nextWatermark != null && nextWatermark.getTimestamp() > currentWatermark) {
        currentWatermark = nextWatermark.getTimestamp();
        output.emitWatermark(nextWatermark);
    }
}
Also used : Watermark(org.apache.flink.streaming.api.watermark.Watermark)

Aggregations

Watermark (org.apache.flink.streaming.api.watermark.Watermark)117 Test (org.junit.Test)92 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)52 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)36 KeyedOneInputStreamOperatorTestHarness (org.apache.flink.streaming.util.KeyedOneInputStreamOperatorTestHarness)36 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)31 TimeWindow (org.apache.flink.streaming.api.windowing.windows.TimeWindow)29 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)21 StreamRecord (org.apache.flink.streaming.runtime.streamrecord.StreamRecord)17 ReducingStateDescriptor (org.apache.flink.api.common.state.ReducingStateDescriptor)16 OperatorStateHandles (org.apache.flink.streaming.runtime.tasks.OperatorStateHandles)16 Tuple3 (org.apache.flink.api.java.tuple.Tuple3)15 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)13 ArrayList (java.util.ArrayList)12 Map (java.util.Map)10 OneInputStreamOperatorTestHarness (org.apache.flink.streaming.util.OneInputStreamOperatorTestHarness)10 ListStateDescriptor (org.apache.flink.api.common.state.ListStateDescriptor)9 Event (org.apache.flink.cep.Event)9 SubEvent (org.apache.flink.cep.SubEvent)9 HashMap (java.util.HashMap)8