Roman Khachatryan created FLINK-41720:
-----------------------------------------

             Summary: KafkaSource with latest or timestamp starting offsets 
silently skips to the log end when a restored or running position falls behind 
retention
                 Key: FLINK-41720
                 URL: https://issues.apache.org/jira/browse/FLINK-41720
             Project: Flink
          Issue Type: Bug
          Components: Connectors / Kafka
    Affects Versions: 2.3.0
            Reporter: Roman Khachatryan
            Assignee: Roman Khachatryan


h3. *Problem*

A {{KafkaSource}} whose starting offsets are {{OffsetsInitializer.latest()}} or 
{{OffsetsInitializer.timestamp(...)}} (SQL: {{scan.startup.mode}} = 
{{latest-offset}} or {{{}timestamp{}}}) jumps to the _end_ of the log when its 
position falls below the log start offset. It silently skips every record 
between the log start and the log end that is still in the log. There is no 
error and no metric; the job carries on from "now".

This happens whenever the source falls behind topic retention: a long stop and 
restore from a checkpoint or savepoint, a stalled or backpressured job, or a 
backlog on a topic with short retention. Records below the log start are gone 
anyway, but the records still in the log should be read, and are not. The 
{{earliest}} starting offsets are not affected.
h3. *Cause*
 * {{KafkaSourceBuilder#parseAndSetRequiredProperties}} sets 
{{auto.offset.reset}} from 
{{{}startingOffsetsInitializer.getAutoOffsetResetStrategy(){}}}.
 ** In released versions it overwrites any value the user set.
 ** Since FLINK-39888 (on {{{}main{}}}, not yet released) a user-set value 
wins, but the default is unchanged.
 * {{LatestOffsetsInitializer#getAutoOffsetResetStrategy}} and 
{{TimestampOffsetsInitializer#getAutoOffsetResetStrategy}} return 
{{{}LATEST{}}}.
 * Both initializers resolve _concrete_ starting offsets in the enumerator: 
{{{}endOffsets{}}}, or {{offsetsForTimes}} falling back to {{{}endOffsets{}}}. 
So the reset strategy never decides where a new split starts.
 * A restored or running split is a plain {{consumer.seek(tp, offset)}} 
({{{}KafkaPartitionSplitReader#parseStartingOffsets{}}}). When that offset is 
below the log start, the consumer's {{OffsetOutOfRange}} handling applies 
{{{}auto.offset.reset=latest{}}}. In effect, the startup mode leaks into 
recovery.

h3. *How to reproduce*
 # Start a {{KafkaSource}} with {{OffsetsInitializer.latest()}} on a topic and 
let it checkpoint.
 # Stop the job, or stall the reader, and keep producing.
 # Move the log start past the reader's position, either with 
{{Admin#deleteRecords}} or with short {{retention.ms}} and small segments.
 # Restore from the checkpoint, or resume the reader.

Expected: every record at or above the new log start is read exactly once. 
Actual: reading resumes at the log end; all records between the log start and 
the log end are skipped. The same happens with 
{{{}OffsetsInitializer.timestamp(...){}}}. With 
{{OffsetsInitializer.earliest()}} the records are read as expected.

A minimal unit-level reproduction: build the consumer properties through 
{{{}KafkaSource.builder().setStartingOffsets(OffsetsInitializer.latest()){}}}, 
give a {{KafkaPartitionSplitReader}} a split whose starting offset is below the 
partition's log start, and read. The reader returns nothing from that partition 
until new records arrive.
h3. *Proposed fix*

Make {{LatestOffsetsInitializer}} and {{TimestampOffsetsInitializer}} return 
{{{}OffsetResetStrategy.EARLIEST{}}}.
 * Since both resolve concrete offsets, this doesn't change where a new split 
starts. It only changes what happens to a position that later left the log, and 
there the earliest available offset is the only choice that doesn't skip 
records still in the log. {{earliest}} already behaves this way.
 * Side effect: a position _above_ the log end (topic deleted and recreated 
under the same name, or a truncated log) also resets to earliest instead of 
latest. That re-reads the retained log instead of skipping to the end, same as 
{{earliest}} today.

Not covered by this change: {{committedOffsets(OffsetResetStrategy.LATEST)}} / 
{{group-offsets}} with {{{}auto.offset.reset=latest{}}}. There the strategy 
also decides where a split without a committed offset starts, so it can't 
simply be changed. A complete fix would need the reader to tell a restored 
position from a new one, for example by handling {{OffsetOutOfRange}} itself 
for splits with a concrete offset.
h3. *Related*
 * FLINK-3037: make the behaviour configurable if the offsets to restore from 
are not available (open).
 * FLINK-39888: allow configuring the offset reset strategy independently (lets 
users work around this, but doesn't fix the default).
 * FLINK-41270: an explicit {{auto.offset.reset}} changes the 
{{committedOffsets()}} startup position.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to