Github user tedyu commented on a diff in the pull request:
https://github.com/apache/spark/pull/9344#discussion_r44212193
--- 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 {
--- End diff --
Have you considered using ReadWriteLock (for lock) to improve performance ?
---
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]