Github user mateiz commented on a diff in the pull request:
https://github.com/apache/spark/pull/1165#discussion_r15331171
--- Diff: core/src/main/scala/org/apache/spark/storage/MemoryStore.scala ---
@@ -141,6 +189,104 @@ 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]] = {
+
+ // Number of elements unrolled so far
+ var elementsUnrolled = 0
+ // Whether there is still enough memory for us to continue unrolling
this block
+ var keepUnrolling = true
+ // Initial per-thread memory to request for unrolling blocks (bytes).
Exposed for testing.
+ val initialMemoryThreshold =
conf.getLong("spark.storage.unrollMemoryThreshold", 1024 * 1024)
+ // How often to check whether we need to request more memory. Exposed
for testing.
+ val memoryCheckPeriod =
conf.getLong("spark.storage.unrollCheckPeriod", 16)
+ // Memory currently reserved by this thread (bytes)
+ var memoryThreshold = initialMemoryThreshold
+ // Memory to request as a multiple of current vector size
+ val memoryGrowthFactor = 1.5
+
+ val threadId = Thread.currentThread().getId
+ val unrollMemoryMap = SparkEnv.get.unrollMemoryMap
+ var vector = new SizeTrackingVector[Any]
+
+ // Request memory for our vector and return whether request is granted.
+ // This involves synchronizing across all threads and can be expensive
if called frequently.
+ def requestMemory(memoryToRequest: Long): Boolean = {
+ unrollMemoryMap.synchronized {
+ val previouslyOccupiedMemory =
unrollMemoryMap.get(threadId).getOrElse(0L)
+ val otherThreadsMemory = unrollMemoryMap.values.sum -
previouslyOccupiedMemory
+ val availableMemory = freeMemory - otherThreadsMemory
+ val granted = availableMemory > memoryToRequest
+ if (granted) { unrollMemoryMap(threadId) = memoryToRequest }
+ granted
+ }
+ }
+
+ // Request enough memory to begin unrolling
+ keepUnrolling = requestMemory(memoryThreshold)
+
+ // Unroll this block safely, checking whether we have exceeded our
threshold periodically
+ try {
+ while (values.hasNext && keepUnrolling) {
+ vector += values.next()
+ if (elementsUnrolled % memoryCheckPeriod == 0) {
+ // If our vector's size has exceeded the threshold, request more
memory
+ val currentSize = vector.estimateSize()
+ if (currentSize >= memoryThreshold) {
+ val amountToRequest = (currentSize * memoryGrowthFactor).toLong
+ // Hold the put lock, in case another thread concurrently puts
a block that
+ // takes up the free space we just ensured for unrolling here
+ putLock.synchronized {
+ if (!requestMemory(amountToRequest)) {
+ // If the first request is not granted, try again after
ensuring free space
+ // If there is still not enough space, give up and drop
the partition
+ val result = ensureFreeSpace(blockId, globalUnrollMemory,
unrolling = true)
--- End diff --
Instead of trying to ensureFreeSpace on all of the globalUnrollMemory at
once, let's maybe ensure only enough for the amount we want to request.
If you change ensureFreeSpace to not do accounting differently when we're
unrolling (as per my comment below), I think this will become something like
this:
```
val maxWeCanFree = globalUnrollMemory - unrollMemory // (the sum of your
map)
if (maxWeCanFree > extraAmountWeNeed) {
ensureFreeSpace(blockId, extraAmountWeNeed)
}
```
Basically this means we can only request extra space until 20% of the
memory pool is allocated to unrolling. If we can fit our next request within
that 20%, we go ahead and do it. Otherwise we don't.
---
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.
---