Github user tedyu commented on a diff in the pull request: https://github.com/apache/spark/pull/11826#discussion_r56918584 --- Diff: sql/core/src/main/scala/org/apache/spark/sql/execution/joins/ShuffledHashJoin.scala --- @@ -55,11 +56,45 @@ case class ShuffledHashJoin( override def requiredChildDistribution: Seq[Distribution] = ClusteredDistribution(leftKeys) :: ClusteredDistribution(rightKeys) :: Nil + private def buildHashedRelation(iter: Iterator[UnsafeRow]): HashedRelation = { + // try to acquire some memory for the hash table, it could trigger other operator to free some + // memory. The memory acquired here will mostly be used until the end of task. + val context = TaskContext.get() + val memoryManager = context.taskMemoryManager() + var acquired = 0L + var used = 0L + context.addTaskCompletionListener((t: TaskContext) => + memoryManager.releaseExecutionMemory(acquired, MemoryMode.ON_HEAP, null) + ) + + val copiedIter = iter.map { row => + // It's hard to guess what's exactly memory will be used, we have a rough guess here. + // TODO: use BytesToBytesMap instead of HashMap for memory efficiency + // Each pair in HashMap will have two UnsafeRows, one CompactBuffer, maybe 10+ pointers + val needed = 150 + row.getSizeInBytes + if (needed > acquired - used) { + val got = memoryManager.acquireExecutionMemory( + Math.max(memoryManager.pageSizeBytes(), needed), MemoryMode.ON_HEAP, null) + if (got < needed) { + throw new SparkException("Can't acquire enough memory to build hash map in shuffled" + --- End diff -- nit: missing space between shuffled and hash
--- 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