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

    https://github.com/apache/spark/pull/10225#discussion_r48708933
  
    --- Diff: 
core/src/main/scala/org/apache/spark/storage/DiskBlockManager.scala ---
    @@ -53,35 +53,98 @@ private[spark] class DiskBlockManager(blockManager: 
BlockManager, conf: SparkCon
     
       private val shutdownHook = addShutdownHook()
     
    +  private abstract class FileAllocationStrategy {
    +    def apply(filename: String): File
    +
    +    protected def getFile(filename: String, storageDirs: Array[File]): 
File = {
    +      require(storageDirs.nonEmpty, "could not find file when the 
directories are empty")
    +
    +      // Figure out which local directory it hashes to, and which 
subdirectory in that
    +      val hash = Utils.nonNegativeHash(filename)
    +      val dirId = localDirs.indexOf(storageDirs(hash % storageDirs.length))
    +      val subDirId = (hash / storageDirs.length) % subDirsPerLocalDir
    +
    +      // Create the subdirectory if it doesn't already exist
    +      val subDir = subDirs(dirId).synchronized {
    +        val old = subDirs(dirId)(subDirId)
    +        if (old != null) {
    +          old
    +        } else {
    +          val newDir = new File(localDirs(dirId), "%02x".format(subDirId))
    +          if (!newDir.exists() && !newDir.mkdir()) {
    +            throw new IOException(s"Failed to create local dir in 
$newDir.")
    +          }
    +          subDirs(dirId)(subDirId) = newDir
    +          newDir
    +        }
    +      }
    +
    +      new File(subDir, filename)
    +    }
    +  }
    +
       /** Looks up a file by hashing it into one of our local subdirectories. 
*/
       // This method should be kept in sync with
       // 
org.apache.spark.network.shuffle.ExternalShuffleBlockResolver#getFile().
    -  def getFile(filename: String): File = {
    -    // Figure out which local directory it hashes to, and which 
subdirectory in that
    -    val hash = Utils.nonNegativeHash(filename)
    -    val dirId = hash % localDirs.length
    -    val subDirId = (hash / localDirs.length) % subDirsPerLocalDir
    -
    -    // Create the subdirectory if it doesn't already exist
    -    val subDir = subDirs(dirId).synchronized {
    -      val old = subDirs(dirId)(subDirId)
    -      if (old != null) {
    -        old
    -      } else {
    -        val newDir = new File(localDirs(dirId), "%02x".format(subDirId))
    -        if (!newDir.exists() && !newDir.mkdir()) {
    -          throw new IOException(s"Failed to create local dir in $newDir.")
    -        }
    -        subDirs(dirId)(subDirId) = newDir
    -        newDir
    +  private object hashAllocator extends FileAllocationStrategy {
    +    def apply(filename: String): File = getFile(filename, localDirs)
    +  }
    +
    +  /** Looks up a file by hierarchy way in different speed storage devices. 
*/
    +  private val hierarchyStore = 
conf.getOption("spark.storage.hierarchyStore")
    +  private class HierarchyAllocator extends FileAllocationStrategy {
    +    case class LayerInfo(key: String, threshold: Long, dirs: Array[File])
    +    val hsSpecs: Array[(String, Long)] =
    +      // e.g.: hierarchyStore = "ssd 200GB, hdd 100GB"
    +      hierarchyStore.get.trim.split(",").map {
    +        s => val x = s.trim.split(" +")
    +          (x(0).toLowerCase, Utils.byteStringAsGb(x(1)))
           }
    +    val hsLayers: Array[LayerInfo] = hsSpecs.map(
    +      s => LayerInfo(s._1, s._2, 
localDirs.filter(_.getPath.toLowerCase.containsSlice(s._1)))
    +    )
    +    val lastLayerDirs = localDirs.filter(dir => 
!hsLayers.exists(_.dirs.contains(dir)))
    +    val allLayers: Array[LayerInfo] = hsLayers :+
    +      LayerInfo("Last Storage", 10.toLong, lastLayerDirs)
    +    val finalLayers: Array[LayerInfo] = allLayers.filter(_.dirs.nonEmpty)
    +    logInfo("Hierarchy store info:")
    +    for (layer <- finalLayers) {
    +      logInfo("Layer: %s, Threshold: %dGB".format(layer.key, 
layer.threshold))
    +      layer.dirs.foreach { dir => 
logInfo("\t%s".format(dir.getCanonicalPath)) }
         }
     
    -    new File(subDir, filename)
    +    def apply(filename: String): File = {
    +      var availableFile: File = null
    +      for (layer <- finalLayers) {
    +        val file = getFile(filename, layer.dirs)
    +        if (file.exists()) return file
    +
    +        if (availableFile == null && file.getParentFile.getUsableSpace>>30 
>= layer.threshold) {
    --- End diff --
    
    1. The ```file``` returned from FileAllocationStrategy.getFile definitely 
have parent directory, so the file.getParentFile could not be null.
    2. ```availableFile != null``` means we find an available file location 
which has enough disk space, but we cannot return at this point, because some 
file may have existed in the slower layer, we have to keep checking each layer 
in finalLayers.
    3. Others have been updated, thanks!


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