[SPARK-57438][SS] Fix NullPointerException in Kafka source metrics when latest partition offsets are unavailable#56526
Open
yadavay-amzn wants to merge 1 commit into
Conversation
…en latest partition offsets are unavailable
Tom-Newton
approved these changes
Jun 15, 2026
| } | ||
| } else { | ||
| Some(latestPartitionOffsets) | ||
| Option(latestPartitionOffsets) |
There was a problem hiding this comment.
Is it possible to write a test to assert that this prevents the Some(null)?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changes were proposed in this pull request?
Fix a
NullPointerExceptionin the Kafka micro-batch source when reporting custom metrics before the latest partition offsets are known.Two changes in
KafkaMicroBatchStream.scala:metrics(),Some(latestPartitionOffsets)is changed toOption(latestPartitionOffsets).latestPartitionOffsetsis avarinitialized tonulland only populated bylatestOffset(); ifmetrics()runs first, the old code producedSome(null).KafkaMicroBatchStream.metrics(...)(a package-visible method called directly in tests) now collapsesSome(null)toNoneviaflatMap(Option(_))before checkingisDefined/get, so the public method cannot NPE on a degenerate input.Why are the changes needed?
latestPartitionOffsetsisprivate var ... = _(null) untillatestOffset()is invoked during batch planning.metrics()can be called earlier (e.g. progress reporting before the first batch completes), so the non-RTM branch wrappednullasSome(null). The companionmetricsthen evaluatedlatestAvailablePartitionOffsets.isDefinedastrue, called.get(returningnull), and invoked.mapon it:This was introduced by #52729 (SPARK-54027, Kafka RTM support). With
Option(latestPartitionOffsets), a null becomesNoneand the metrics computation is correctly skipped (empty map).Does this PR introduce any user-facing change?
No behavioral change for well-formed inputs. It only prevents the streaming query from crashing when metrics are requested before latest partition offsets are available; in that case an empty metrics map is returned (as intended).
How was this patch tested?
Added a test in
KafkaMicroBatchSourceSuitethat calls the companionmetrics(...)withSome(null)for the latest available partition offsets and asserts an empty map is returned. Verified it fails (NPE atKafkaMicroBatchStream.scala:520) without the fix and passes with it; existing custom-metrics tests still pass.Credit
Thanks to Thomas Newton, who reported this issue (SPARK-57438), diagnosed the root cause (identifying #52729 and the
Some(null)at the non-RTM branch), and proposed theSome(...)→Option(...)call-site patch that this PR is based on.Was this patch authored or co-authored using generative AI tooling?
No.