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

    https://github.com/apache/spark/pull/1165#discussion_r15270377
  
    --- Diff: core/src/main/scala/org/apache/spark/storage/MemoryStore.scala ---
    @@ -141,6 +188,88 @@ private class MemoryStore(blockManager: BlockManager, 
maxMemory: Long)
       }
     
       /**
    +   * Unroll the given block in memory safely.
    +   *
    +   * The safety of this operation refers to avoiding potential OOM 
exceptions caused by
    +   * unrolling the entirety of the block in memory at once. This is 
achieved by periodically
    +   * checking whether the memory restrictions for unrolling blocks are 
still satisfied,
    +   * stopping immediately if not. This check is a safeguard against the 
scenario in which
    +   * there is not enough free memory to accommodate the entirety of a 
single block.
    +   *
    +   * This method returns either an array with the contents of the entire 
block or an iterator
    +   * containing the values of the block (if the array would have exceeded 
available memory).
    +   */
    +  def unrollSafely(
    +      blockId: BlockId,
    +      values: Iterator[Any],
    +      droppedBlocks: ArrayBuffer[(BlockId, BlockStatus)])
    +    : Either[Array[Any], Iterator[Any]] = {
    +
    +    var count = 0                     // The number of elements unrolled 
so far
    +    var atMemoryLimit = false         // Whether we are at the memory 
limit for unrolling blocks
    +    var previousSize = 0L             // Previous estimate of the size of 
our buffer
    +    val memoryRequestPeriod = 1000    // How frequently we request more 
memory for our buffer
    +    val memoryRequestThreshold = 100  // Before this is exceeded, request 
memory every round
    +
    +    val threadId = Thread.currentThread().getId
    +    val unrollMemoryMap = SparkEnv.get.unrollMemoryMap
    +    var buffer = new SizeTrackingVector[Any]
    +
    +    try {
    +      while (values.hasNext && !atMemoryLimit) {
    +        buffer += values.next()
    +        count += 1
    +        if (count % memoryRequestPeriod == 1 || count < 
memoryRequestThreshold) {
    --- End diff --
    
    Doing a synchronized on the first 100 elements will be quite expensive. Why 
not just grab a certain fraction of free memory first (even if it's more than 
we need) and then only do the synchronized when we exceed that? You can also 
double it in that case instead of requesting stuff every 1000 items. Similar to 
how EAOM worked.


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

Reply via email to