Github user tedyu commented on a diff in the pull request:
https://github.com/apache/spark/pull/9344#discussion_r44212241
--- Diff:
core/src/main/scala/org/apache/spark/memory/ExecutionMemoryPool.scala ---
@@ -0,0 +1,153 @@
+/*
+ * 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.memory
+
+import javax.annotation.concurrent.GuardedBy
+
+import scala.collection.mutable
+
+import org.apache.spark.Logging
+
+/**
+ * Implements policies and bookkeeping for sharing a adjustable-sized pool
of memory between tasks.
+ *
+ * Tries to ensure that each task gets a reasonable share of memory,
instead of some task ramping up
+ * to a large amount first and then causing others to spill to disk
repeatedly.
+ *
+ * If there are N tasks, it ensures that each task can acquire at least 1
/ 2N of the memory
+ * before it has to spill, and at most 1 / N. Because N varies
dynamically, we keep track of the
+ * set of active tasks and redo the calculations of 1 / 2N and 1 / N in
waiting tasks whenever this
+ * set changes. This is all done by synchronizing access to mutable state
and using wait() and
+ * notifyAll() to signal changes to callers. Prior to Spark 1.6, this
arbitration of memory across
+ * tasks was performed by the ShuffleMemoryManager.
+ *
+ * @param lock a [[MemoryManager]] instance to synchronize on
+ * @param poolName a human-readable name for this pool, for use in log
messages
+ */
+class ExecutionMemoryPool(
+ lock: Object,
+ poolName: String
+ ) extends MemoryPool(lock) with Logging {
+
+ /**
+ * Map from taskAttemptId -> memory consumption in bytes
+ */
+ @GuardedBy("lock")
+ private val memoryForTask = new mutable.HashMap[Long, Long]()
+
+ override def memoryUsed: Long = lock.synchronized {
+ memoryForTask.values.sum
+ }
+
+ /**
+ * Returns the memory consumption, in bytes, for the given task.
+ */
+ def getMemoryUsageForTask(taskAttemptId: Long): Long = lock.synchronized
{
+ memoryForTask.getOrElse(taskAttemptId, 0L)
+ }
+
+ /**
+ * Try to acquire up to `numBytes` of memory for the given task and
return the number of bytes
+ * obtained, or 0 if none can be allocated.
+ *
+ * This call may block until there is enough free memory in some
situations, to make sure each
+ * task has a chance to ramp up to at least 1 / 2N of the total memory
pool (where N is the # of
+ * active tasks) before it is forced to spill. This can happen if the
number of tasks increase
+ * but an older task had a lot of memory already.
+ *
+ * @return the number of bytes granted to the task.
+ */
+ def acquireMemory(numBytes: Long, taskAttemptId: Long): Long =
lock.synchronized {
+ assert(numBytes > 0, s"invalid number of bytes requested: $numBytes")
+
+ // Add this task to the taskMemory map just so we can keep an accurate
count of the number
+ // of active tasks, to let other tasks ramp down their memory in calls
to `acquireMemory`
+ if (!memoryForTask.contains(taskAttemptId)) {
+ memoryForTask(taskAttemptId) = 0L
+ // This will later cause waiting tasks to wake up and check numTasks
again
+ lock.notifyAll()
+ }
+
+ // Keep looping until we're either sure that we don't want to grant
this request (because this
+ // task would have more than 1 / numActiveTasks of the memory) or we
have enough free
+ // memory to give it (we always let each task get at least 1 / (2 *
numActiveTasks)).
+ // TODO: simplify this to limit each task to its own slot
+ while (true) {
+ val numActiveTasks = memoryForTask.keys.size
+ val curMem = memoryForTask(taskAttemptId)
+
+ // How much we can grant this task; don't let it grow to more than 1
/ numActiveTasks;
+ // don't let it be negative
+ val maxToGrant =
+ math.min(numBytes, math.max(0, (poolSize / numActiveTasks) -
curMem))
+ // Only give it as much memory as is free, which might be none if it
reached 1 / numTasks
+ val toGrant = math.min(maxToGrant, memoryFree)
+
+ if (curMem < poolSize / (2 * numActiveTasks)) {
+ // We want to let each task get at least 1 / (2 * numActiveTasks)
before blocking;
+ // if we can't give it this much now, wait for other tasks to free
up memory
+ // (this happens if older tasks allocated lots of memory before N
grew)
+ if (memoryFree >= math.min(maxToGrant, poolSize / (2 *
numActiveTasks) - curMem)) {
+ memoryForTask(taskAttemptId) += toGrant
+ return toGrant
+ } else {
+ logInfo(
+ s"TID $taskAttemptId waiting for at least 1/2N of $poolName
pool to be free")
+ lock.wait()
+ }
+ } else {
+ memoryForTask(taskAttemptId) += toGrant
+ return toGrant
+ }
+ }
+ 0L // Never reached
+ }
+
+ /**
+ * Release `numBytes` of memory acquired by the given task.
+ */
+ def releaseMemory(numBytes: Long, taskAttemptId: Long): Unit =
lock.synchronized {
+ val curMem = memoryForTask.getOrElse(taskAttemptId, 0L)
+ var memoryToFree = if (curMem < numBytes) {
+ logWarning(
+ s"Internal error: release called on $numBytes bytes but task only
has $curMem bytes " +
+ s"of memory from the $poolName pool")
+ curMem
+ } else {
+ numBytes
+ }
+ if (memoryForTask.contains(taskAttemptId)) {
+ memoryForTask(taskAttemptId) -= memoryToFree
+ if (memoryForTask(taskAttemptId) <= 0) {
+ memoryForTask.remove(taskAttemptId)
+ }
+ }
--- End diff --
Should we log the case where memoryForTask doesn't contain taskAttemptId ?
---
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]