spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cloud-fan <...@git.apache.org>
Subject [GitHub] spark pull request #17196: [SPARK-19855][SQL] Create an internal FilePartiti...
Date Tue, 07 Mar 2017 23:06:04 GMT
Github user cloud-fan commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17196#discussion_r104804221
  
    --- Diff: sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/FilePartitionStrategy.scala
---
    @@ -0,0 +1,156 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.spark.sql.execution.datasources
    +
    +import scala.collection.mutable.ArrayBuffer
    +
    +import org.apache.hadoop.fs.{BlockLocation, FileStatus, LocatedFileStatus}
    +
    +import org.apache.spark.internal.Logging
    +import org.apache.spark.sql.SparkSession
    +import org.apache.spark.sql.catalyst.InternalRow
    +
    +/**
    + * An (internal) interface that takes in a list of files and partitions them for parallelization.
    + */
    +trait FilePartitionStrategy {
    +  /**
    +   * `input` is a list of input files, in the form of (partition column value, file status).
    +   *
    +   * The function should return a list of file blocks to read for each partition. The
i-th position
    +   * indicates the list of file blocks to read for task i.
    +   */
    +  def partition(
    +      sparkSession: SparkSession,
    +      fileFormat: FileFormat,
    +      options: Map[String, String],
    +      input: Seq[(InternalRow, FileStatus)])
    +    : Seq[Seq[PartitionedFile]]
    +}
    +
    +
    +/**
    + * A default [[FilePartitionStrategy]] that binpacks files roughly into evenly sized
partitions.
    + */
    +class DefaultFilePartitionStrategy extends FilePartitionStrategy with Logging {
    +  import DefaultFilePartitionStrategy._
    +
    +  override def partition(
    +      sparkSession: SparkSession,
    +      fileFormat: FileFormat,
    +      options: Map[String, String],
    +      input: Seq[(InternalRow, FileStatus)])
    +    : Seq[Seq[PartitionedFile]] = {
    +
    +    val defaultMaxSplitBytes = sparkSession.sessionState.conf.filesMaxPartitionBytes
    +    val openCostInBytes = sparkSession.sessionState.conf.filesOpenCostInBytes
    +    val defaultParallelism = sparkSession.sparkContext.defaultParallelism
    +    val totalBytes = input.map(_._2.getLen + openCostInBytes).sum
    +    val bytesPerCore = totalBytes / defaultParallelism
    +
    +    val maxSplitBytes = Math.min(defaultMaxSplitBytes, Math.max(openCostInBytes, bytesPerCore))
    +    logInfo(s"Planning scan with bin packing, max size: $maxSplitBytes bytes, " +
    +      s"open cost is considered as scanning $openCostInBytes bytes.")
    +
    +    val splitFiles: Array[PartitionedFile] = input.flatMap { case (partitionValues, file)
=>
    +      val blockLocations = getBlockLocations(file)
    +      if (fileFormat.isSplitable(sparkSession, options, file.getPath)) {
    +        (0L until file.getLen by maxSplitBytes).map { offset =>
    +          val remaining = file.getLen - offset
    +          val size = if (remaining > maxSplitBytes) maxSplitBytes else remaining
    +          val hosts = getBlockHosts(blockLocations, offset, size)
    +          PartitionedFile(partitionValues, file.getPath.toUri.toString, offset, size,
hosts)
    +        }
    +      } else {
    +        val hosts = getBlockHosts(blockLocations, 0, file.getLen)
    +        Seq(PartitionedFile(partitionValues, file.getPath.toUri.toString, 0, file.getLen,
hosts))
    +      }
    +    }.toArray.sortBy(_.length)(implicitly[Ordering[Long]].reverse)
    +
    +    val partitions = new ArrayBuffer[Seq[PartitionedFile]]
    +    val currentFiles = new ArrayBuffer[PartitionedFile]
    +    var currentSize = 0L
    +
    +    /** Close the current partition and move to the next. */
    +    def closePartition(): Unit = {
    +      if (currentFiles.nonEmpty) {
    +        val newPartition = currentFiles.toArray.toSeq // Copy to a new Array.
    +        partitions += newPartition
    +      }
    +      currentFiles.clear()
    +      currentSize = 0
    +    }
    +
    +    // Assign files to partitions using "First Fit Decreasing" (FFD)
    +    splitFiles.foreach { file =>
    +      if (currentSize + file.length > maxSplitBytes) {
    +        closePartition()
    +      }
    +      // Add the given file to the current partition.
    +      currentSize += file.length + openCostInBytes
    +      currentFiles += file
    +    }
    +    closePartition()
    +
    +    partitions
    +  }
    +}
    +
    +
    +object DefaultFilePartitionStrategy {
    --- End diff --
    
    shall we just move functions here to `class DefaultFilePartitionStrategy`?


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

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


Mime
View raw message