You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by "sqd (via GitHub)" <gi...@apache.org> on 2026/10/14 19:07:11 UTC

[PR] [FLINK-36532][Connectors/Kafka] Commit offsets on stop-with-savepoint [flink-connector-kafka]

sqd opened a new pull request, #333:
URL: https://github.com/apache/flink-connector-kafka/pull/333

   ## What is the purpose of the change
   
   This pull request makes the Kafka source commit the offsets of the savepoint when a job is stopped with a savepoint (FLINK-36532).
   
   The source commits the offsets of a completed checkpoint by queueing an asynchronous commit on the split fetcher. Stop-with-savepoint closes the reader right after the savepoint completes, and the fetcher shuts down without running the queued commit, so the savepoint's offsets never reach Kafka. With `group.protocol=consumer`, closing the consumer also drops a commit that is already in flight. This happens with and without `--drain`.
   
   The consumer group then lags behind the savepoint. Lag monitoring is wrong, and a job that starts from the committed offsets instead of the savepoint re-reads records.
   
   The reader now commits the offsets of the latest completed checkpoint synchronously when it closes, unless their asynchronous commit has already finished.
   
   
   ## Brief change log
   
     - `KafkaSourceReader` records the latest completed checkpoint and whether its asynchronous offset commit has finished. When it closes before that commit has finished, it hands the checkpoint's offsets to the fetcher manager.
     - `KafkaSourceFetcherManager` passes the offsets to the split reader of a running fetcher. When no fetcher can take them, it starts one for the commit.
     - `KafkaPartitionSplitReader` commits the offsets with `commitSync` (10 second timeout) on the fetcher thread before it closes the consumer, and creates the consumer first if the fetcher has not created it yet. A failed commit is logged and counted in the failed-commit metric, as on checkpoint, and does not fail the close.
     - Offsets of a checkpoint that did not complete, and of the snapshot that `DynamicKafkaSourceReader` takes outside a checkpoint, are never committed on close.
     - The "Consumer Offset Committing" section of the Kafka docs, in `docs/content` and `docs/content.zh`, describes the commit on close.
   
   
   ## Verifying this change
   
   This change added tests and can be verified as follows:
   
     - Added `KafkaSourceStopWithSavepointITCase`, which stops a job with a savepoint, with and without drain, and checks that every partition's committed offset matches the savepoint. Periodic checkpoints are off, so the savepoint is the only checkpoint that can commit. Without the change, no partition has a committed offset.
     - Added tests to `KafkaSourceReaderTest`:
       - closing the reader commits the latest completed checkpoint, with both `group.protocol=classic` and `group.protocol=consumer`;
       - closing it does not commit a checkpoint that did not complete;
       - closing it does not commit a snapshot taken outside a checkpoint.
     - Added tests to `KafkaPartitionSplitReaderTest`:
       - the commit on close survives a wakeup that is still pending from shutting down the fetcher;
       - the commit on close creates the consumer when the reader has none;
       - offsets handed over after `close()` has started are rejected.
   
   ## Does this pull request potentially affect one of the following parts:
   
     - Dependencies (does it add or upgrade a dependency, including `kafka.version` or `flink.version`): no
     - The public API, i.e., is any changed class annotated with `@Public(Evolving)` or `@Experimental`, or are the Table options or the PyFlink wrappers changed: no
     - Checkpointed state, its serializers, or exactly-once delivery (splits, enumerator state, writer state, committables, transactions): no
     - The per-record code paths (split reader, record emitter, writer, serialization schemas; performance sensitive): no. The split reader changes only in `close()`.
   
   
   ## Documentation
   
     - Does this pull request introduce a new feature? no
     - If yes, how is the feature documented? not applicable
     - If the docs changed, are both `docs/content` and `docs/content.zh` updated? yes
   
   ---
   
   ##### Was generative AI tooling used to co-author this PR?
   
   - [X] Yes (please specify the tool below)
   
   Generated-by: Claude Code (Claude Opus 5.5)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-36532][Connectors/Kafka] Commit offsets on stop-with-savepoint [flink-connector-kafka]

Posted by "sqd (via GitHub) via issues" <is...@flink.apache.org>.
sqd commented on PR #333:
URL: https://github.com/apache/flink-connector-kafka/pull/333#issuecomment-6670938103

   Hey @dalelane , I really appreciate the very detailed pointers and explanations! I largely followed what you suggested and added the tests you listed.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-36532][Connectors/Kafka] Commit offsets on stop-with-savepoint [flink-connector-kafka]

Posted by "sqd (via GitHub) via issues" <is...@flink.apache.org>.
sqd commented on code in PR #333:
URL: https://github.com/apache/flink-connector-kafka/pull/333#discussion_r4486512435


##########
flink-connector-kafka/src/main/java/org/apache/flink/connector/kafka/source/reader/KafkaPartitionSplitReader.java:
##########
@@ -64,6 +66,12 @@
 public class KafkaPartitionSplitReader
         implements SplitReader<ConsumerRecord<byte[], byte[]>, KafkaPartitionSplit> {
     private static final Logger LOG = LoggerFactory.getLogger(KafkaPartitionSplitReader.class);
+    // The commit delays closing the consumer. The reader waits for its fetcher to close for at most
+    // source.reader.close.timeout, 30 seconds by default.
+    private static final Duration COMMIT_ON_CLOSE_TIMEOUT = Duration.ofSeconds(10);

Review Comment:
   This is a timeout. Actual time waited is however long it takes for Kafka broker to ack, usually within milliseconds. It is also just a last line of defense, which only fires when the latest completed checkpoint's commit hasn't finished.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-36532][Connectors/Kafka] Commit offsets on stop-with-savepoint [flink-connector-kafka]

Posted by "boring-cyborg[bot] (via GitHub)" <gi...@apache.org>.
boring-cyborg[bot] commented on PR #333:
URL: https://github.com/apache/flink-connector-kafka/pull/333#issuecomment-6176157818

   Thanks for opening this pull request! Please check out our contributing guidelines. (https://flink.apache.org/contributing/how-to-contribute.html)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-36532][Connectors/Kafka] Commit offsets on stop-with-savepoint [flink-connector-kafka]

Posted by "sqd (via GitHub)" <gi...@apache.org>.
sqd commented on PR #333:
URL: https://github.com/apache/flink-connector-kafka/pull/333#issuecomment-6471206348

   Hi all, thank you very much for the feedbacks! I'll fold those in ASAP.
   
   @Efrat19 In our internal fork, we mostly use this feature when the jobgraph has changed, making recovering from a savepoint impossible/undesirable. And yes we can specify starting offsets, but without a PR like this, it's not always easy to derive the offsets to specify.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-36532][Connectors/Kafka] Commit offsets on stop-with-savepoint [flink-connector-kafka]

Posted by "Efrat19 (via GitHub)" <gi...@apache.org>.
Efrat19 commented on PR #333:
URL: https://github.com/apache/flink-connector-kafka/pull/333#issuecomment-6410288735

   What is actually the use case this PR is addressing? can't we achieve same results by restarting from savepoint / specifying starting offsets?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-36532][Connectors/Kafka] Commit offsets on stop-with-savepoint [flink-connector-kafka]

Posted by "dalelane (via GitHub) via issues" <is...@flink.apache.org>.
dalelane commented on code in PR #333:
URL: https://github.com/apache/flink-connector-kafka/pull/333#discussion_r4514144740


##########
flink-connector-kafka/src/main/java/org/apache/flink/connector/kafka/source/reader/fetcher/KafkaSourceFetcherManager.java:
##########
@@ -98,6 +98,31 @@ public void commitOffsets(
         }
     }
 
+    /**
+     * Commits the given offsets synchronously when the fetcher closes its consumer, after {@link
+     * #close(long)} shut the fetcher down. Must be called before {@link #close(long)}.
+     */
+    public void commitOffsetsOnClose(
+            Map<TopicPartition, OffsetAndMetadata> offsetsToCommit,
+            Set<TopicPartition> partitionsWithoutOffset) {
+        // A fetcher shut down by close() or by an error stays listed until it exits, so try each
+        // of them.
+        for (SplitFetcher<ConsumerRecord<byte[], byte[]>, KafkaPartitionSplit> fetcher :
+                fetchers.values()) {
+            if (((KafkaPartitionSplitReader) fetcher.getSplitReader())
+                    .commitOffsetsOnClose(offsetsToCommit, partitionsWithoutOffset)) {
+                return;
+            }
+        }
+        // No fetcher runs, or all are already closing their consumers. Only a fetcher thread may
+        // use a consumer, so start one that commits as it closes.
+        final SplitFetcher<ConsumerRecord<byte[], byte[]>, KafkaPartitionSplit> splitFetcher =
+                createSplitFetcher();

Review Comment:
   In my earlier comment I said the close commit runs in the same KafkaPartitionSplitReader that fetched the records, so reconciliation has the state it needs. That's true when a fetcher is still running, but I missed this path.
   
   When no fetcher is left, for example after a fetcher fails and the job fails over, this starts a new one. Its split reader has no memory of earlier commits, so it can't stop the committed offset from moving backwards:
   
   1. A checkpoint holds offset 1. Reconciliation moves it past the commit marker, and 2 is committed.
   2. The next checkpoint also holds 1, and completes. Its commit is queued.
   3. The fetcher fails before running that commit.
   4. On close, a new fetcher commits 1. The committed offset goes back from 2 to 1.
   
   The gap isn't new: commitOffsets already starts fresh fetchers like this, and my PR only stopped the offset moving backwards while a partition stays assigned to a reader. But this commit adds another way in.
   
   Could a fetcher started only to commit check the group's committed offsets first (consumer.committed), and skip anything lower? 
   
   Checking first would also mean that in cases where there's nothing it can commit (partitions without an offset) it'd avoid unnecessarily creating a consumer 



##########
flink-connector-kafka/src/main/java/org/apache/flink/connector/kafka/source/reader/KafkaPartitionSplitReader.java:
##########
@@ -64,6 +66,10 @@
 public class KafkaPartitionSplitReader
         implements SplitReader<ConsumerRecord<byte[], byte[]>, KafkaPartitionSplit> {
     private static final Logger LOG = LoggerFactory.getLogger(KafkaPartitionSplitReader.class);
+    // The commit returns once Kafka acknowledges it, and only an unreachable group coordinator
+    // makes it delay closing the consumer by this long. The reader waits for its fetcher to close
+    // for at most source.reader.close.timeout, 30 seconds by default.
+    private static final Duration COMMIT_ON_CLOSE_TIMEOUT = Duration.ofSeconds(10);

Review Comment:
   I agree that this would [usually be within milliseconds](https://github.com/apache/flink-connector-kafka/pull/333/changes#r4486512435), given that it returns as soon as the broker acks. But what happens when there is no ack from the group coordinator - if the group coordinator can't be reached or has stopped responding while the job is stopping or falling over?  (With the classic protocol, that is when the latest async commit is still in flight)
   
   In that case, closing the fetcher does this:
    
   (10 seconds after commit request) `commitSync` gives up - the request is still in flight, `consumer.close()` starts waiting for it
   
   (30 seconds after commit request) commit request times out (request.timeout.ms). `close()` stops waiting and returns
    
   This means the fetcher can take about 30 seconds to close. The reader only waits 30 seconds (source.reader.close.timeout) then logs and continues. The fetcher thread keeps running in the background with the consumer. 
   
   The comment on COMMIT_ON_CLOSE_TIMEOUT suggest the aim was for the commit to fit in the time budget, but this doesn't count `consumer.close()`
   
   This matters beyond stop-with-savepoint, because the commit runs on every close (failover, cancel, and DynamicKafkaSourceReader restarting its sub-readers). Failovers caused by broker trouble could hit this. DynamicKafkaSourceReader closes its sub-readers one at a time on the task thread, so each cluster that has stopped responding adds its own wait.
   
   Could the commit and consumer.close() share what's left of the close budget, so together they stay inside source.reader.close.timeout? 
   
   (And it'd help to make it clear in the PR description that this applies to every close, not only stop-with-savepoint)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-36532][Connectors/Kafka] Commit offsets on stop-with-savepoint [flink-connector-kafka]

Posted by "Efrat19 (via GitHub)" <gi...@apache.org>.
Efrat19 commented on code in PR #333:
URL: https://github.com/apache/flink-connector-kafka/pull/333#discussion_r4351254056


##########
flink-connector-kafka/src/main/java/org/apache/flink/connector/kafka/source/reader/KafkaPartitionSplitReader.java:
##########
@@ -64,6 +66,12 @@
 public class KafkaPartitionSplitReader
         implements SplitReader<ConsumerRecord<byte[], byte[]>, KafkaPartitionSplit> {
     private static final Logger LOG = LoggerFactory.getLogger(KafkaPartitionSplitReader.class);
+    // The commit delays closing the consumer. The reader waits for its fetcher to close for at most
+    // source.reader.close.timeout, 30 seconds by default.
+    private static final Duration COMMIT_ON_CLOSE_TIMEOUT = Duration.ofSeconds(10);

Review Comment:
   Doesn't this mean a mandatory 10 seconds delay for all jobs?
   Should we make it configurable?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-36532][Connectors/Kafka] Commit offsets on stop-with-savepoint [flink-connector-kafka]

Posted by "sqd (via GitHub) via issues" <is...@flink.apache.org>.
sqd commented on PR #333:
URL: https://github.com/apache/flink-connector-kafka/pull/333#issuecomment-6670950696

   @MartijnVisser This is done. Apology for the delay.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-36532][Connectors/Kafka] Commit offsets on stop-with-savepoint [flink-connector-kafka]

Posted by "Efrat19 (via GitHub) via issues" <is...@flink.apache.org>.
Efrat19 commented on PR #333:
URL: https://github.com/apache/flink-connector-kafka/pull/333#issuecomment-7080444548

   > Hi all, thank you very much for the feedbacks! I'll fold those in ASAP.
   > 
   > @Efrat19 In our internal fork, we mostly use this feature when the jobgraph has changed, making recovering from a savepoint impossible/undesirable. And yes we can specify starting offsets, but without a PR like this, it's not always easy to derive the offsets to specify.
   
   Understood - yet IIMO this mechanism is fragile compared to the flink-native alternatives (restore from savepoint / starting offsets) given that offsets are committed in best-effort manner (until timeout) while not failing stop-with-savepoint
   wdyt


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org