This page is meant as a template for writing a FLIP. To create a FLIP choose Tools->Copy on this page and modify with your content and replace the heading with the next FLIP number and a description of your issue. Replace anything in italics with your own description.

Document the state by adding a label to the FLIP page with one of "discussion", "accepted", "released", "rejected".

Please keep the discussion on the mailing list rather than commenting on the wiki (wiki discussions get unwieldy fast).

Motivation

Currently when using watermark with idleness in Flink, idleness can be incorrectly detected when reading records from a source that is blocked by the runtime. For example this can easily happen when source is either backpressured, or blocked by the watermark alignment. In those cases, despite there are more records to be read from the source (or source’s split), runtime is deciding not to poll (or being unable to) those records. In such case idleness timeout can kick in, marking source/source split as idle, which can lead to incorrect combined watermark calculations and dropping of incorrectly marked late records.

Watermark alignment

If there are two source splits,  A and B, and maxAllowedWatermarkDrift is set to 30s.

  1. Partition A emitted watermark 1042 sec, while partition B sits at watermark 1000 sec.
  2. 1042s - 1000s > maxAllowedWatermarkDrift , so partition A is blocked by the watermark alignment.

  3. For the duration of idleTimeout, partition B is emitting some large batch of records, that do not advance watermark of that partition by much. For example either watermark for partition B stays 1000s, or is updated by a small amount to for example 1005s.

  4. idleTimeout kicks in, marking partition A as idle

  5. partition B finishes emitting large batch of those older records, and let's say now there is a gap in rowtimes. Previously partition B was emitting records with rowtime ~1000s, now it jumps to for example ~5000s.

  6. As partition A is idle, combined watermark jumps to ~5000s as well.

  7. Watermark alignment unblocks partition A, and it continues emitting records with rowtime ~1042s. But now all of those records are dropped due to being late.

Backpressure

When there are two SourceOperator’s, A and B. Due to for example some data skew, it could happen that either only A gets backpressured, or A is backpressured quicker/sooner. Either way, during that time when A is backpressured, while B is not, B can bump the combined watermark high enough, so that when backpressure recedes, fresh records from A will be considered as late, leading to incorrect results.

Proposed changes

To solve the above mentioned problems, we propose to provide to the implementations of WatermarkStrategy and WatermarkGenerator a special clock, that returns the elapsed time but does not advance when the respective WatermarkGenerator is blocked.

For example WatermarkStrategyWithIdleness instead of using SystemClock, could use that special clock to calculate the idleness time. As this new clock will be pausing progress when WatermarkGenerator is backpressured or blocked by the watermark alignment, the idle timeout will be calculated correctly.

Public Interfaces

The following interface would be added alongside the existing org.apache.flink.util.clock.Clock.

/**
 * A clock that gives access to relative time, similar to System#nanoTime(), however the progress 
* of the relative time doesn't have to reflect the progress of a wall clock. Concrete classes
* can specify a different contract in that regard. */ @PublicEvolving public interface RelativeClock { /** Gets the current relative time, in nanoseconds. */ long relativeTimeNanos(); }

And that clock would be accessible via addition of a new getter in the WatermarkGeneratorSupplier#Context:

public interface WatermarkGeneratorSupplier<T> extends Serializable {

    WatermarkGenerator<T> createWatermarkGenerator(Context context);

    interface Context {
        (...)
        
        /**
         * Returns a RelativeClock that hides periods when input was not active
* and WatermarkGenerator could not have been executed due to execution
* being blocked by the runtime. For example a backpressure or watermark
* alignment blocking the progress. * * @see RelativeClock */ RelativeClock getInputActivityClock(); } }

Proposed Changes

Describe the new thing you want to do in appropriate detail. This may be fairly extensive and have large subsections of its own. Or it may be a few sentences. Use judgement based on the scope of the change.

Compatibility, Deprecation, and Migration Plan

None for the most part. As this is a bug fix, I propose to not make this behaviour configurable. After merging the fix, the built-in idleness detecting code would change it’s behaviour.

Users implementing their own versions of time sensitive WatermarkGenerators, would have to migrate their code to use WatermarkGeneratorSupplier.Context#getRelativeClock() instead of the SystemClock#getInstance().

As this is a bug fix, despite this changing semantic and behaviour of the existing jobs, I would also propose to back-port this change to previous releases.

Rejected Alternatives

There were a couple of alternatives that were rejected.

Ignoring markIdle calls when backpressured/blocked

Whenever operator is backpressured or blocked due to watermark alignment, we could ignore WatermarkOutput#markIdle calls. However the problem is that WatermarkGenerator implementations can be stateful and simply ignoring such calls could make internal state of the WatermarkGenerator invalid, leading to all kinds of problems.

For example for the built-in WatermarksWithIdleness:

  • It has the isIdleNow field. If we ignore one #markIdle call, the following calls might not be executed if for example backpressure recedes.

  • If idleTimeout is set to 30s, and source emitted it’s last record, then got backpressured for 29.9s, WatermarksWithIdleness shouldn’t mark given source/partition idle 0.1s after backpressured receded.

This leads us to think, WatermarkGenerator must be made aware of the problem, and it can not be solved by the runtime changes alone.

Exposing backpressured/blocked signals to WatermarkGenerators

WatermarkGenerator could have been expanded with some listener-like methods, that would for example inform it when idleTimeout should stop ticking. However we deemed that this would push too much complexity into the user code. RelativeClock idea was proposed as solution to hide this complexity away from the user, providing him with a simple, hopefully drop-in replacement (instead of using SystemClock.getInstance(), use provided RelativeClock).