Github user andrewor14 commented on a diff in the pull request:
https://github.com/apache/spark/pull/12352#discussion_r59765862
--- Diff:
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/FileScanRDD.scala
---
@@ -54,33 +56,70 @@ class FileScanRDD(
override def compute(split: Partition, context: TaskContext):
Iterator[InternalRow] = {
val iterator = new Iterator[Object] with AutoCloseable {
+ private val inputMetrics =
context.taskMetrics().registerInputMetrics(DataReadMethod.Hadoop)
+ private val existingBytesRead = inputMetrics.bytesRead
+
+ // Find a function that will return the FileSystem bytes read by
this thread. Do this before
+ // apply readFunction, because it might read some bytes.
+ private val getBytesReadCallback: Option[() => Long] =
+ SparkHadoopUtil.get.getFSBytesReadOnThreadCallback()
+
+ // For Hadoop 2.5+, we get our input bytes from thread-local Hadoop
FileSystem statistics.
+ // If we do a coalesce, however, we are likely to compute multiple
partitions in the same
+ // task and in the same thread, in which case we need to avoid
override values written by
+ // previous partitions (SPARK-13071).
+ private def updateBytesRead(): Unit = {
+ getBytesReadCallback.foreach { getBytesRead =>
+ inputMetrics.setBytesRead(existingBytesRead + getBytesRead())
+ }
+ }
+
+ // If we can't get the bytes read from the FS stats, fall back to
the file size,
+ // which may be inaccurate.
+ private def updateBytesReadWithFileSize(): Unit = {
+ if (getBytesReadCallback.isEmpty && currentFile != null) {
+ inputMetrics.incBytesReadInternal(currentFile.length)
+ }
+ }
+
private[this] val files =
split.asInstanceOf[FilePartition].files.toIterator
+ private[this] var currentFile: PartitionedFile = null
private[this] var currentIterator: Iterator[Object] = null
def hasNext = (currentIterator != null && currentIterator.hasNext)
|| nextIterator()
- def next() = currentIterator.next()
+ def next() = {
+ inputMetrics.incRecordsReadInternal(1)
+ if (inputMetrics.recordsRead %
SparkHadoopUtil.UPDATE_INPUT_METRICS_INTERVAL_RECORDS == 0) {
+ updateBytesRead()
--- End diff --
by the way this could be expensive; @nongli pointed this out in some other
place. But let's handle that separately.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]