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

    https://github.com/apache/spark/pull/10705#discussion_r53876330
  
    --- Diff: 
core/src/main/scala/org/apache/spark/storage/BlockInfoManager.scala ---
    @@ -0,0 +1,438 @@
    +/*
    + * 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.storage
    +
    +import javax.annotation.concurrent.GuardedBy
    +
    +import scala.collection.JavaConverters._
    +import scala.collection.mutable
    +
    +import com.google.common.collect.ConcurrentHashMultiset
    +
    +import org.apache.spark.{Logging, SparkException, TaskContext}
    +
    +
    +/**
    + * Tracks metadata for an individual block.
    + *
    + * @param level the block's storage level. This is the requested 
persistence level, not the
    + *              effective storage level of the block (i.e. if this is 
MEMORY_AND_DISK, then this
    + *              does not imply that the block is actually resident in 
memory).
    + * @param tellMaster whether state changes for this block should be 
reported to the master. This
    + *                   is true for most blocks, but is false for broadcast 
blocks.
    + */
    +private[storage] class BlockInfo(val level: StorageLevel, val tellMaster: 
Boolean) {
    +
    +  /**
    +   * The size of the block (in bytes)
    +   */
    +  def size: Long = _size
    +  def size_=(s: Long): Unit = {
    +    _size = s
    +    checkInvariants()
    +  }
    +  private[this] var _size: Long = 0
    +
    +  /**
    +   * The number of times that this block has been locked for reading.
    +   */
    +  def readerCount: Int = _readerCount
    +  def readerCount_=(c: Int): Unit = {
    +    _readerCount = c
    +    checkInvariants()
    +  }
    +  private[this] var _readerCount: Int = 0
    +
    +  /**
    +   * The task attempt id of the task which currently holds the write lock 
for this block, or
    +   * [[BlockInfo.NON_TASK_WRITER]] if the write lock is held by non-task 
code, or
    +   * [[BlockInfo.NO_WRITER]] if this block is not locked for writing.
    +   */
    +  def writerTask: Long = _writerTask
    +  def writerTask_=(t: Long): Unit = {
    +    _writerTask = t
    +    checkInvariants()
    +  }
    +  private[this] var _writerTask: Long = 0
    +
    +  /**
    +   * True if this block has been removed from the BlockManager and false 
otherwise.
    +   * This field is used to communicate block deletion to blocked readers / 
writers (see its usage
    +   * in [[BlockInfoManager]]).
    +   */
    +  def removed: Boolean = _removed
    +  def removed_=(r: Boolean): Unit = {
    +    _removed = r
    +    checkInvariants()
    +  }
    +  private[this] var _removed: Boolean = false
    +
    +  private def checkInvariants(): Unit = {
    +    // A block's reader count must be non-negative:
    +    assert(_readerCount >= 0)
    +    // A block is either locked for reading or for writing, but not for 
both at the same time:
    +    assert(!(_readerCount != 0 && _writerTask != BlockInfo.NO_WRITER))
    +    // If a block is removed then it is not locked:
    +    assert(!_removed || (_readerCount == 0 && _writerTask == 
BlockInfo.NO_WRITER))
    +  }
    +
    +  checkInvariants()
    +}
    +
    +private[storage] object BlockInfo {
    +
    +  /**
    +   * Special task attempt id constant used to mark a block's write lock as 
being unlocked.
    +   */
    +  val NO_WRITER: Long = -1
    +
    +  /**
    +   * Special task attempt id constant used to mark a block's write lock as 
being held by
    +   * a non-task thread (e.g. by a driver thread or by unit test code).
    +   */
    +  val NON_TASK_WRITER: Long = -1024
    +}
    +
    +/**
    + * Component of the [[BlockManager]] which tracks metadata for blocks and 
manages block locking.
    + *
    + * The locking interface exposed by this class is readers-writer lock. 
Every lock acquisition is
    + * automatically associated with a running task and locks are 
automatically released upon task
    + * completion or failure.
    + *
    + * This class is thread-safe.
    + */
    +private[storage] class BlockInfoManager extends Logging {
    +
    +  private type TaskAttemptId = Long
    +
    +  /**
    +   * Used to look up metadata for individual blocks. Entries are added to 
this map via an atomic
    +   * set-if-not-exists operation ([[lockNewBlockForWriting()]]) and are 
removed
    +   * by [[removeBlock()]].
    +   */
    +  @GuardedBy("this")
    +  private[this] val infos = new mutable.HashMap[BlockId, BlockInfo]
    +
    +  /**
    +   * Tracks the set of blocks that each task has locked for writing.
    +   */
    +  @GuardedBy("this")
    +  private[this] val writeLocksByTask =
    +    new mutable.HashMap[TaskAttemptId, mutable.Set[BlockId]]
    +      with mutable.MultiMap[TaskAttemptId, BlockId]
    +
    +  /**
    +   * Tracks the set of blocks that each task has locked for reading, along 
with the number of times
    +   * that a block has been locked (since our read locks are re-entrant).
    +   */
    +  @GuardedBy("this")
    +  private[this] val readLocksByTask =
    +    new mutable.HashMap[TaskAttemptId, ConcurrentHashMultiset[BlockId]]
    +
    +  // 
----------------------------------------------------------------------------------------------
    +
    +  // Initialization for special task attempt ids:
    +  registerTask(BlockInfo.NON_TASK_WRITER)
    +
    +  // 
----------------------------------------------------------------------------------------------
    +
    +  /**
    +   * Called at the start of a task in order to register that task with 
this [[BlockInfoManager]].
    +   * This must be called prior to calling any other BlockInfoManager 
methods from that task.
    +   */
    +  def registerTask(taskAttemptId: TaskAttemptId): Unit = {
    +    require(!readLocksByTask.contains(taskAttemptId),
    +      s"Task attempt $taskAttemptId is already registered")
    +    readLocksByTask(taskAttemptId) = ConcurrentHashMultiset.create()
    +  }
    +
    +  /**
    +   * Returns the current task's task attempt id (which uniquely identifies 
the task), or
    +   * [[BlockInfo.NON_TASK_WRITER]] if called by a non-task thread.
    +   */
    +  private def currentTaskAttemptId: TaskAttemptId = {
    +    
Option(TaskContext.get()).map(_.taskAttemptId()).getOrElse(BlockInfo.NON_TASK_WRITER)
    +  }
    +
    +  /**
    +   * Lock a block for reading and return its metadata.
    +   *
    +   * If another task has already locked this block for reading, then the 
read lock will be
    +   * immediately granted to the calling task and its lock count will be 
incremented.
    +   *
    +   * If another task has locked this block for reading, then this call 
will block until the write
    --- End diff --
    
    "locked this block for writing"


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

Reply via email to