[KafkaIO] Use consumer position and lag to estimate end offsets#39285
[KafkaIO] Use consumer position and lag to estimate end offsets#39285sjvanrossum wants to merge 8 commits into
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request optimizes Kafka end offset estimation by leveraging local consumer state, which reduces network overhead by avoiding frequent broker requests. Additionally, the PR upgrades the Kafka client to version 3.9.2 and modernizes test infrastructure by replacing deprecated Kafka server utilities. These changes also include a cleanup of legacy build configurations to maintain a cleaner project structure. Highlights
New Features🧠 You can now enable Memory (public preview) to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request upgrades the Kafka dependency to version 3.9.2, cleans up older Kafka integration test modules, and refactors the offset estimation in ReadFromKafkaDoFn to lazily track partition lag using the polling consumer instead of a dedicated estimator consumer. Feedback on these changes highlights critical issues with using Long.MIN_VALUE as the default uninitialized offset, which can cause incorrect progress tracking (reporting zero remaining work) and negative backlog calculations that disrupt autoscaling. Additionally, the reviewer recommends logging exceptions in the empty catch block during polling and avoiding fragile Scala synthetic methods when instantiating KafkaServer in tests.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| final AtomicLong latestOffsetEstimator = | ||
| latestOffsetEstimatorCache.getUnchecked(kafkaSourceDescriptor); | ||
| return new UnsplittableRestrictionTracker<>( | ||
| new GrowableOffsetRangeTracker( | ||
| restriction.getFrom(), latestOffsetEstimatorCache.getUnchecked(kafkaSourceDescriptor))); | ||
| new GrowableOffsetRangeTracker(restriction.getFrom(), latestOffsetEstimator::get)); |
There was a problem hiding this comment.
Since latestOffsetEstimator is initialized to Long.MIN_VALUE, the GrowableOffsetRangeTracker will receive Long.MIN_VALUE as the end offset estimate before the first poll completes. This causes getProgress() to cap the end offset to the current position and report 0 remaining work, which can incorrectly signal to the runner that the partition is fully processed. Returning Long.MAX_VALUE when the estimate is unknown (i.e., Long.MIN_VALUE) represents an infinite/unknown end offset, which is much safer and more appropriate for an unbounded source.
final AtomicLong latestOffsetEstimator =
latestOffsetEstimatorCache.getUnchecked(kafkaSourceDescriptor);
return new UnsplittableRestrictionTracker<>(
new GrowableOffsetRangeTracker(
restriction.getFrom(),
() -> {
long estimate = latestOffsetEstimator.get();
return estimate == Long.MIN_VALUE ? Long.MAX_VALUE : estimate;
}));There was a problem hiding this comment.
This explanation and suggestion look wrong to me. Provide a source reference for the runner behavior.
Docs for GrowableOffsetRangeTracker.RangeEndEstimator state:
Return {@code Long.MIN_VALUE} if an estimate can not be provided.
8502286 to
ef99e9d
Compare
ebaffd4 to
b5317bd
Compare
|
Checks are failing. Will not request review until checks are succeeding. If you'd like to override that behavior, comment |
b5317bd to
2275ecc
Compare
2275ecc to
9ffbc88
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #39285 +/- ##
============================================
+ Coverage 54.76% 58.04% +3.28%
- Complexity 1716 13052 +11336
============================================
Files 1066 2515 +1449
Lines 169067 263741 +94674
Branches 1255 10761 +9506
============================================
+ Hits 92587 153090 +60503
- Misses 74263 104904 +30641
- Partials 2217 5747 +3530
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Use
Consumer.position(TopicPartition)andConsumer.currentLag(TopicPartition)to update the end offset estimator (reduced to anAtomicLongper restriction tracker) using local state.This change reuses local metadata from recently completed record fetches instead of periodically requesting the broker for end offsets.
This PR was split from #36834 and depends on the changes in #39284 and #39297.
Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:
addresses #123), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, commentfixes #<ISSUE NUMBER>instead.CHANGES.mdwith noteworthy changes.See the Contributor Guide for more tips on how to make review process smoother.
To check the build health, please visit https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md
GitHub Actions Tests Status (on master branch)
See CI.md for more information about GitHub Actions CI or the workflows README to see a list of phrases to trigger workflows.