fhan688 commented on code in PR #18882:
URL: https://github.com/apache/hudi/pull/18882#discussion_r3333446294


##########
hudi-spark-datasource/hudi-spark-common/src/main/scala/org/apache/hudi/HoodieMergeOnReadRDDV2.scala:
##########
@@ -259,6 +255,34 @@ class HoodieMergeOnReadRDDV2(@transient sc: SparkContext,
     }
   }
 
+  private def withInputMetrics(iter: Iterator[InternalRow],
+                               closeableIter: Iterator[InternalRow],
+                               context: TaskContext,
+                               bytesReadCallback: () => Long): 
Iterator[InternalRow] = {
+    val metricIter = new Iterator[InternalRow] with Closeable {
+      override def hasNext: Boolean = iter.hasNext
+
+      override def next(): InternalRow = {
+        val row = iter.next()
+        HoodieSparkInputMetricsUtils.incRecordsRead(context, 1)
+        row
+      }
+
+      override def close(): Unit = {
+        closeableIter match {
+          case closeable: Closeable => closeable.close()
+          case _ =>
+        }
+      }
+    }
+
+    context.addTaskCompletionListener[Unit] { _ =>
+      HoodieSparkInputMetricsUtils.incBytesRead(context, bytesReadCallback())

Review Comment:
   > will this incur perf/resource cost, do you have micro-benchmark for this?
   
   The listener is registered once per Spark task, not per record, so the 
runtime overhead should be negligible compared with file IO and MOR merge cost. 
It retains the
     iterator until task completion, but that is intentional to guarantee the 
underlying closeable iterator is closed even when the result iterator is not 
fully consumed.
     This follows the same pattern as CloseableIteratorListener used elsewhere 
in Hudi.



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

Reply via email to