Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ private[kafka010] class KafkaMicroBatchStream(
}
}
} else {
Some(latestPartitionOffsets)
Option(latestPartitionOffsets)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to write a test to assert that this prevents the Some(null)?

}

KafkaMicroBatchStream.metrics(latestConsumedOffset, reCalculatedLatestPartitionOffsets)
Expand Down Expand Up @@ -513,10 +513,11 @@ object KafkaMicroBatchStream extends Logging {
latestConsumedOffset: Optional[Offset],
latestAvailablePartitionOffsets: Option[PartitionOffsetMap]): ju.Map[String, String] = {
val offset = Option(latestConsumedOffset.orElse(null))
val availableOffsets = latestAvailablePartitionOffsets.flatMap(Option(_))

if (offset.nonEmpty && latestAvailablePartitionOffsets.isDefined) {
if (offset.nonEmpty && availableOffsets.isDefined) {
val consumedPartitionOffsets = offset.map(KafkaSourceOffset(_)).get.partitionToOffsets
val offsetsBehindLatest = latestAvailablePartitionOffsets.get
val offsetsBehindLatest = availableOffsets.get
.map(partitionOffset => partitionOffset._2 -
consumedPartitionOffsets.getOrElse(partitionOffset._1, 0L))
if (offsetsBehindLatest.nonEmpty) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1883,6 +1883,14 @@ abstract class KafkaMicroBatchV2SourceSuite extends KafkaMicroBatchSourceSuiteBa
// test null latestAvailablePartitionOffsets
assert(KafkaMicroBatchStream.metrics(Optional.ofNullable(offset), None).isEmpty)
}

test("SPARK-57438: metrics should not NPE when latest partition offsets are unavailable" +
" (Some(null))") {
val topicPartition = new TopicPartition(newTopic(), 0)
val offset = KafkaSourceOffset(Map[TopicPartition, Long]((topicPartition, 1L)))
// Some(null) simulates latestPartitionOffsets being null before latestOffset() is called
assert(KafkaMicroBatchStream.metrics(Optional.of(offset), Some(null)).isEmpty)
}
}

class KafkaMicroBatchV1SourceWithAdminSuite extends KafkaMicroBatchV1SourceSuite {
Expand Down