Github user brkyvz commented on a diff in the pull request:

    https://github.com/apache/spark/pull/15235#discussion_r80581975
  
    --- Diff: 
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/ListingFileCatalog.scala
 ---
    @@ -82,73 +85,185 @@ class ListingFileCatalog(
        * This is publicly visible for testing.
        */
       def listLeafFiles(paths: Seq[Path]): mutable.LinkedHashSet[FileStatus] = 
{
    -    if (paths.length >= 
sparkSession.sessionState.conf.parallelPartitionDiscoveryThreshold) {
    -      HadoopFsRelation.listLeafFilesInParallel(paths, hadoopConf, 
sparkSession)
    -    } else {
    -      // Right now, the number of paths is less than the value of
    -      // parallelPartitionDiscoveryThreshold. So, we will list file 
statues at the driver.
    -      // If there is any child that has more files than the threshold, we 
will use parallel
    -      // listing.
    -
    -      // Dummy jobconf to get to the pathFilter defined in configuration
    -      val jobConf = new JobConf(hadoopConf, this.getClass)
    -      val pathFilter = FileInputFormat.getInputPathFilter(jobConf)
    -
    -      val statuses: Seq[FileStatus] = paths.flatMap { path =>
    -        val fs = path.getFileSystem(hadoopConf)
    -        logTrace(s"Listing $path on driver")
    -
    -        val childStatuses = {
    -          try {
    -            val stats = fs.listStatus(path)
    -            if (pathFilter != null) stats.filter(f => 
pathFilter.accept(f.getPath)) else stats
    -          } catch {
    -            case _: FileNotFoundException =>
    -              logWarning(s"The directory $path was not found. Was it 
deleted very recently?")
    -              Array.empty[FileStatus]
    -          }
    -        }
    +    val files =
    +      if (paths.length >= 
sparkSession.sessionState.conf.parallelPartitionDiscoveryThreshold) {
    +        ListingFileCatalog.listLeafFilesInParallel(paths, hadoopConf, 
sparkSession)
    +      } else {
    +        ListingFileCatalog.listLeafFilesInSerial(paths, hadoopConf)
    +      }
    +
    +    mutable.LinkedHashSet(files: _*)
    +  }
    +
    +  override def equals(other: Any): Boolean = other match {
    +    case hdfs: ListingFileCatalog => paths.toSet == hdfs.paths.toSet
    +    case _ => false
    +  }
    +
    +  override def hashCode(): Int = paths.toSet.hashCode()
    +}
    +
    +
    +object ListingFileCatalog extends Logging {
    +
    +  // `FileStatus` is Writable but not serializable.  What make it worse, 
somehow it doesn't play
    +  // well with `SerializableWritable`.  So there seems to be no way to 
serialize a `FileStatus`.
    +  // Here we use `SerializableFileStatus` to extract key components of a 
`FileStatus` to serialize
    +  // it from executor side and reconstruct it on driver side.
    +  private case class SerializableBlockLocation(
    +      names: Array[String],
    +      hosts: Array[String],
    +      offset: Long,
    +      length: Long)
    +
    +  private case class SerializableFileStatus(
    +      path: String,
    +      length: Long,
    +      isDir: Boolean,
    +      blockReplication: Short,
    +      blockSize: Long,
    +      modificationTime: Long,
    +      accessTime: Long,
    +      blockLocations: Array[SerializableBlockLocation])
    +
    +  /**
    +   * List a collection of path recursively.
    +   */
    +  private def listLeafFilesInSerial(
    +      paths: Seq[Path],
    +      hadoopConf: Configuration): Seq[FileStatus] = {
    +    // Dummy jobconf to get to the pathFilter defined in configuration
    +    val jobConf = new JobConf(hadoopConf, this.getClass)
    +    val filter = FileInputFormat.getInputPathFilter(jobConf)
    +
    +    paths.flatMap { path =>
    +      logTrace(s"Listing $path")
    +      val fs = path.getFileSystem(hadoopConf)
    +
    +      // [SPARK-17599] Prevent ListingFileCatalog from failing if path 
doesn't exist
    +      val status: Option[FileStatus] = try Option(fs.getFileStatus(path)) 
catch {
    +        case _: FileNotFoundException =>
    +          logWarning(s"The directory $path was not found. Was it deleted 
very recently?")
    +          None
    +      }
     
    -        childStatuses.map {
    -          case f: LocatedFileStatus => f
    -
    -          // NOTE:
    -          //
    -          // - Although S3/S3A/S3N file system can be quite slow for 
remote file metadata
    -          //   operations, calling `getFileBlockLocations` does no harm 
here since these file system
    -          //   implementations don't actually issue RPC for this method.
    -          //
    -          // - Here we are calling `getFileBlockLocations` in a sequential 
manner, but it should not
    -          //   be a big deal since we always use to 
`listLeafFilesInParallel` when the number of
    -          //   paths exceeds threshold.
    -          case f =>
    -            if (f.isDirectory ) {
    -              // If f is a directory, we do not need to call 
getFileBlockLocations (SPARK-14959).
    -              f
    -            } else {
    -              HadoopFsRelation.createLocatedFileStatus(f, 
fs.getFileBlockLocations(f, 0, f.getLen))
    +      status.map(listLeafFiles0(fs, _, filter)).getOrElse(Seq.empty)
    +    }
    +  }
    +
    +  /**
    +   * List a collection of path recursively in parallel (using Spark 
executors).
    +   * Each task launched will use [[listLeafFilesInSerial]] to list.
    +   */
    +  private def listLeafFilesInParallel(
    +      paths: Seq[Path],
    +      hadoopConf: Configuration,
    +      sparkSession: SparkSession): Seq[FileStatus] = {
    +    assert(paths.size >= 
sparkSession.sessionState.conf.parallelPartitionDiscoveryThreshold)
    +    logInfo(s"Listing leaf files and directories in parallel under: 
${paths.mkString(", ")}")
    +
    +    val sparkContext = sparkSession.sparkContext
    +    val serializableConfiguration = new 
SerializableConfiguration(hadoopConf)
    +    val serializedPaths = paths.map(_.toString)
    +
    +    // Set the number of parallelism to prevent following file listing 
from generating many tasks
    +    // in case of large #defaultParallelism.
    +    val numParallelism = Math.min(paths.size, 10000)
    +
    +    val statuses = sparkContext
    +      .parallelize(serializedPaths, numParallelism)
    +      .mapPartitions { paths =>
    +        val hadoopConf = serializableConfiguration.value
    +        listLeafFilesInSerial(paths.map(new Path(_)).toSeq, 
hadoopConf).iterator
    +      }.map { status =>
    --- End diff --
    
    why don't you just call map on the iterator but call it on the rdd?


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to