Github user JoshRosen commented on a diff in the pull request:
https://github.com/apache/spark/pull/9241#discussion_r43187836
--- Diff: core/src/main/java/org/apache/spark/memory/TaskMemoryManager.java
---
@@ -101,27 +106,95 @@
private final boolean inHeap;
/**
+ * The size of memory granted to each consumer.
+ */
+ private HashMap<MemoryConsumer, Long> consumers;
+
+ /**
* Construct a new TaskMemoryManager.
*/
public TaskMemoryManager(MemoryManager memoryManager, long
taskAttemptId) {
this.inHeap = memoryManager.tungstenMemoryIsAllocatedInHeap();
this.memoryManager = memoryManager;
this.taskAttemptId = taskAttemptId;
+ this.consumers = new HashMap<>();
}
/**
- * Acquire N bytes of memory for execution, evicting cached blocks if
necessary.
+ * Acquire N bytes of memory for a consumer. If there is no enough
memory, it will call
+ * spill() of consumers to release more memory.
+ *
* @return number of bytes successfully granted (<= N).
*/
- public long acquireExecutionMemory(long size) {
- return memoryManager.acquireExecutionMemory(size, taskAttemptId);
+ public long acquireExecutionMemory(long size, MemoryConsumer consumer)
throws IOException {
+ synchronized (this) {
+ long got = memoryManager.acquireExecutionMemory(size, taskAttemptId);
+
+ // call spill() on itself to release some memory
+ if (got < size && consumer != null) {
+ consumer.spill(size - got);
+ got += memoryManager.acquireExecutionMemory(size - got,
taskAttemptId);
+ }
+
+ if (got < size) {
+ long needed = size - got;
+ // call spill() on other consumers to release memory
+ for (MemoryConsumer c: consumers.keySet()) {
+ if (c != null && c != consumer) {
+ needed -= c.spill(size - got);
+ if (needed < 0) {
+ break;
+ }
+ }
+ }
+ got += memoryManager.acquireExecutionMemory(size - got,
taskAttemptId);
+ }
+
+ long old = 0L;
+ if (consumers.containsKey(consumer)) {
+ old = consumers.get(consumer);
+ }
+ consumers.put(consumer, got + old);
+
+ return got;
+ }
}
/**
- * Release N bytes of execution memory.
+ * Release N bytes of execution memory for a MemoryConsumer.
*/
- public void releaseExecutionMemory(long size) {
- memoryManager.releaseExecutionMemory(size, taskAttemptId);
+ public void releaseExecutionMemory(long size, MemoryConsumer consumer) {
+ if (size == 0) {
+ return;
+ }
+ synchronized (this) {
+ if (consumers.containsKey(consumer)) {
+ long old = consumers.get(consumer);
+ if (old > size) {
+ consumers.put(consumer, old - size);
+ } else {
+ if (old < size) {
+ if (Utils.isTesting()) {
+ Platform.throwException(
+ new SparkException("Release more memory " + size + "than
acquired " + old + " for "
+ + consumer));
+ } else {
+ logger.warn("Release more memory " + size + " than acquired
" + old + "for "
--- End diff --
Space before `for`
---
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]