dalelane commented on code in PR #293:
URL: 
https://github.com/apache/flink-connector-kafka/pull/293#discussion_r3883799591


##########
flink-connector-kafka/src/main/java/org/apache/flink/connector/kafka/source/reader/KafkaPartitionSplitReader.java:
##########
@@ -298,6 +307,58 @@ long getConsumerPosition(TopicPartition tp, String msg) {
         return retryOnWakeup(() -> consumer.position(tp), msg);
     }
 
+    private void trackLastFetchedRecordOffsets(ConsumerRecords<byte[], byte[]> 
consumerRecords) {
+        for (TopicPartition tp : consumerRecords.partitions()) {
+            List<ConsumerRecord<byte[], byte[]>> partitionRecords = 
consumerRecords.records(tp);
+            if (!partitionRecords.isEmpty()) {
+                lastFetchedOffsets.put(
+                        tp, partitionRecords.get(partitionRecords.size() - 
1).offset());
+            }
+        }
+    }
+
+    /**
+     * Advances the offsets to commit over the entries that the Kafka consumer 
read but never
+     * delivered, such as transaction control markers and records of aborted 
transactions.
+     *
+     * <p>{@link KafkaRecordEmitter} derives the offset to commit from the 
records it receives, so
+     * the offset stops at the first entry that Kafka does not deliver, for as 
long as the partition
+     * is idle. The consumer's own position accounts for those entries, so it 
is the offset that
+     * external tooling expects to see.
+     *
+     * <p>This relies on {@link #lastKnownPositions}, populated as a side 
effect of the regular
+     * {@link #fetch()} poll loop, rather than querying the consumer for the 
position again here.
+     * {@link #notifyCheckpointComplete} runs on this same split fetcher 
thread, but at a point
+     * outside that poll loop, so this allows us to avoid a separate blocking 
call to the consumer.
+     */
+    private Map<TopicPartition, OffsetAndMetadata> reconcileOffsetsToCommit(
+            Map<TopicPartition, OffsetAndMetadata> offsetsToCommit) {
+        Map<TopicPartition, OffsetAndMetadata> reconciled = new 
HashMap<>(offsetsToCommit);
+        Set<TopicPartition> assignment = consumer.assignment();
+        offsetsToCommit.forEach(
+                (tp, offsetAndMetadata) -> {
+                    if (!assignment.contains(tp) || 
stoppingOffsets.containsKey(tp)) {

Review Comment:
   added an example test in 984d50b6d905a1883bb99676a6fd3b6cde5b99a3 
   It's a bit contrived - had to resort to asking Claude to set it up



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to