Tom-Newton commented on code in PR #52729:
URL: https://github.com/apache/spark/pull/52729#discussion_r3364818530


##########
connector/kafka-0-10-sql/src/main/scala/org/apache/spark/sql/kafka010/KafkaMicroBatchStream.scala:
##########
@@ -235,7 +345,22 @@ private[kafka010] class KafkaMicroBatchStream(
   override def toString(): String = s"KafkaV2[$kafkaOffsetReader]"
 
   override def metrics(latestConsumedOffset: Optional[Offset]): ju.Map[String, 
String] = {
-    KafkaMicroBatchStream.metrics(latestConsumedOffset, latestPartitionOffsets)
+    val reCalculatedLatestPartitionOffsets =
+      if (inRealTimeMode) {
+        if (!latestConsumedOffset.isPresent) {
+          // this means a batch has no end offsets, which should not happen
+          None
+        } else {
+          Some {
+            kafkaOffsetReader.fetchLatestOffsets(
+              
Some(latestConsumedOffset.get.asInstanceOf[KafkaSourceOffset].partitionToOffsets))
+          }
+        }
+      } else {
+        Some(latestPartitionOffsets)

Review Comment:
   I think its possible for this to create `Some(null)`, which causes a crash:
   ```
   java.lang.NullPointerException: Cannot invoke 
"scala.collection.IterableOps.map(scala.Function1)" because the return value of 
"scala.Option.get()" is null
           at 
org.apache.spark.sql.kafka010.KafkaMicroBatchStream$.metrics(KafkaMicroBatchStream.scala:520)
 ~[kafka_extension_deploy.jar:?]
           at 
org.apache.spark.sql.kafka010.KafkaMicroBatchStream.metrics(KafkaMicroBatchStream.scala:363)
 ~[kafka_extension_deploy.jar:?]
           at 
org.apache.spark.sql.execution.streaming.runtime.ProgressContext.$anonfun$extractSourceProgress$2(ProgressReporter.scala:384)
 ~[spark-sql_2.13-4.1.1.jar:4.1.1]
           at org.apache.spark.util.Utils$.timeTakenMs(Utils.scala:496) 
~[spark-core_2.13-4.1.1.jar:4.1.1]
           at 
org.apache.spark.sql.execution.streaming.runtime.ProgressContext.$anonfun$extractSourceProgress$1(ProgressReporter.scala:380)
 ~[spark-sql_2.13-4.1.1.jar:4.1.1]
           at scala.collection.immutable.List.map(List.scala:236) 
~[scala-library-2.13.17.jar:?]
           at scala.collection.immutable.List.map(List.scala:79) 
~[scala-library-2.13.17.jar:?]
           at 
org.apache.spark.sql.execution.streaming.runtime.ProgressContext.extractSourceProgress(ProgressReporter.scala:379)
 ~[spark-sql_2.13-4.1.1.jar:4.1.1]
           at 
org.apache.spark.sql.execution.streaming.runtime.ProgressContext.constructNewProgress(ProgressReporter.scala:348)
 ~[spark-sql_2.13-4.1.1.jar:4.1.1]
           at 
org.apache.spark.sql.execution.streaming.runtime.ProgressContext.finishTrigger(ProgressReporter.scala:312)
 ~[spark-sql_2.13-4.1.1.jar:4.1.1]
           at 
org.apache.spark.sql.execution.streaming.runtime.ProgressContext.finishTrigger(ProgressReporter.scala:429)
 ~[spark-sql_2.13-4.1.1.jar:4.1.1]
           at 
org.apache.spark.sql.execution.streaming.runtime.MicroBatchExecution.executeOneBatch(MicroBatchExecution.scala:525)
 ~[spark-sql_2.13-4.1.1.jar:4.1.1]
           at 
org.apache.spark.sql.execution.streaming.runtime.MicroBatchExecution.$anonfun$runActivatedStream$1(MicroBatchExecution.scala:461)
 ~[spark-sql_2.13-4.1.1.jar:4.1.1]
           at 
org.apache.spark.sql.execution.streaming.runtime.MicroBatchExecution.$anonfun$runActivatedStream$1$adapted(MicroBatchExecution.scala:461)
 ~[spark-sql_2.13-4.1.1.jar:4.1.1]
           at 
org.apache.spark.sql.execution.streaming.runtime.TriggerExecutor.runOneBatch(TriggerExecutor.scala:40)
 ~[spark-sql_2.13-4.1.1.jar:4.1.1]
           at 
org.apache.spark.sql.execution.streaming.runtime.TriggerExecutor.runOneBatch$(TriggerExecutor.scala:38)
 ~[spark-sql_2.13-4.1.1.jar:4.1.1]
           at 
org.apache.spark.sql.execution.streaming.runtime.MultiBatchExecutor.runOneBatch(TriggerExecutor.scala:60)
 ~[spark-sql_2.13-4.1.1.jar:4.1.1]
           at 
org.apache.spark.sql.execution.streaming.runtime.MultiBatchExecutor.execute(TriggerExecutor.scala:65)
 ~[spark-sql_2.13-4.1.1.jar:4.1.1]
           at 
org.apache.spark.sql.execution.streaming.runtime.MicroBatchExecution.runActivatedStream(MicroBatchExecution.scala:461)
 ~[spark-sql_2.13-4.1.1.jar:4.1.1]
           at 
org.apache.spark.sql.execution.streaming.runtime.StreamExecution.$anonfun$runStream$1(StreamExecution.scala:347)
 [spark-sql_2.13-4.1.1.jar:4.1.1]
           at 
scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.scala:18) 
~[scala-library-2.13.17.jar:?]
           at 
org.apache.spark.sql.SparkSession.withActive(SparkSession.scala:804) 
~[spark-sql-api_2.13-4.1.1.jar:4.1.1]
           at 
org.apache.spark.sql.execution.streaming.runtime.StreamExecution.org$apache$spark$sql$execution$streaming$runtime$StreamExecution$$runStream(StreamExecution.scala:307)
 [spark-sql_2.13-4.1.1.jar:4.1.1]
           at 
org.apache.spark.sql.execution.streaming.runtime.StreamExecution$$anon$1.run(StreamExecution.scala:230)
 [spark-sql_2.13-4.1.1.jar:4.1.1]
   ```
   
   I built a version with the below change and my problem seems to be solved:
   ```suggestion
           Option(latestPartitionOffsets)
   ```



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to