Commit 3a9f11f
[SPARK-55271][SS] Fix NullPointerException in Kafka micro-batch streaming metrics reporting
### What changes were proposed in this pull request?
This PR fixes a `NullPointerException` in `KafkaMicroBatchStream.metrics()` during Kafka micro-batch streaming progress reporting (`finishTrigger`).
Specifically, `KafkaMicroBatchStream.metrics()` checked `latestAvailablePartitionOffsets.isDefined` before extracting partition offsets. If `latestAvailablePartitionOffsets` was `Some(null)`, `isDefined` returned `true`, causing `latestAvailablePartitionOffsets.get` to return `null` and throwing a `NullPointerException` when `.map()` was invoked on it.
This PR updates the condition to `latestAvailablePartitionOffsets.exists(_ != null)` to safely ensure partition offsets are non-null before invoking `.map()`.
### Why are the changes needed?
When uninitialized partition offsets or race conditions occur during progress reporting (`finishTrigger`), `latestAvailablePartitionOffsets` can be `Some(null)`. Without this check, calling `.map()` on `null` throws a `NullPointerException`, crashing the entire streaming query job in production after batch execution.
### Does this PR introduce _any_ user-facing change?
No API changes. Fixes a `NullPointerException` in progress reporting metrics.
### How was this patch tested?
Added unit test assertion in `KafkaMicroBatchSourceSuite`.
### Was this patch authored or co-authored using generative AI tooling?
No.
Closes #58133 from zahed1994/SPARK-55271-kafka-streaming-metrics-npe.
Authored-by: zahed1994 <zahedshareef@gmail.com>
Signed-off-by: Liang-Chi Hsieh <viirya@gmail.com>1 parent c3d9631 commit 3a9f11f
2 files changed
Lines changed: 3 additions & 1 deletion
File tree
- connector/kafka-0-10-sql/src
- main/scala/org/apache/spark/sql/kafka010
- test/scala/org/apache/spark/sql/kafka010
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
514 | 514 | | |
515 | 515 | | |
516 | 516 | | |
517 | | - | |
| 517 | + | |
518 | 518 | | |
519 | 519 | | |
520 | 520 | | |
| |||
Lines changed: 2 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1930 | 1930 | | |
1931 | 1931 | | |
1932 | 1932 | | |
| 1933 | + | |
| 1934 | + | |
1933 | 1935 | | |
1934 | 1936 | | |
1935 | 1937 | | |
| |||
0 commit comments