Skip to content

Commit

Permalink
fix some bugs: arbitration & suspend
Browse files Browse the repository at this point in the history
  • Loading branch information
Pengzna committed Oct 27, 2024
1 parent ef0d629 commit 54d1fd8
Show file tree
Hide file tree
Showing 11 changed files with 256 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@

package org.apache.hugegraph.memory;

import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
Expand All @@ -38,18 +40,22 @@
public class MemoryManager {

private static final Logger LOG = LoggerFactory.getLogger(MemoryManager.class);

Check warning on line 42 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/MemoryManager.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/MemoryManager.java#L42

Added line #L42 was not covered by tests
private static final int ARBITRATE_MEMORY_THREAD_NUM = 12;
private static final String QUERY_MEMORY_POOL_NAME_PREFIX = "QueryMemoryPool";
private static final String ARBITRATE_MEMORY_POOL_NAME = "ArbitrateMemoryPool";
private static final String DELIMINATOR = "_";
private static final int ARBITRATE_MEMORY_THREAD_NUM = 12;
public static final String DELIMINATOR = "_";

// TODO: read it from conf, current 1G
public static final long MAX_MEMORY_CAPACITY_IN_BYTES = Bytes.GB;
private final AtomicLong currentAvailableMemoryInBytes =

Check warning on line 50 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/MemoryManager.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/MemoryManager.java#L50

Added line #L50 was not covered by tests
new AtomicLong(MAX_MEMORY_CAPACITY_IN_BYTES);
private final AtomicLong currentOffHeapAllocatedMemoryInBytes = new AtomicLong(0);
private final AtomicLong currentOnHeapAllocatedMemoryInBytes = new AtomicLong(0);

Check warning on line 53 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/MemoryManager.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/MemoryManager.java#L52-L53

Added lines #L52 - L53 were not covered by tests

private final Queue<MemoryPool> queryMemoryPools =
new PriorityQueue<>((o1, o2) -> (int) (o2.getFreeBytes() - o1.getFreeBytes()));
private final Map<String, MemoryPool> threadName2TaskMemoryPoolMap = new ConcurrentHashMap<>();

Check warning on line 57 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/MemoryManager.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/MemoryManager.java#L55-L57

Added lines #L55 - L57 were not covered by tests

private final MemoryArbitrator memoryArbitrator;
private final ExecutorService arbitrateExecutor;

Expand Down Expand Up @@ -128,6 +134,21 @@ public synchronized long handleRequestFromQueryPool(long size) {
return size;

Check warning on line 134 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/MemoryManager.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/MemoryManager.java#L130-L134

Added lines #L130 - L134 were not covered by tests
}

/**
* Used by task thread to find its memory pool to release self's memory resource when exiting.
*/
public MemoryPool getCorrespondingTaskMemoryPool(String threadName) {
return threadName2TaskMemoryPoolMap.getOrDefault(threadName, null);

Check warning on line 141 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/MemoryManager.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/MemoryManager.java#L141

Added line #L141 was not covered by tests
}

public void bindCorrespondingTaskMemoryPool(String threadName, MemoryPool memoryPool) {
threadName2TaskMemoryPoolMap.computeIfAbsent(threadName, key -> memoryPool);
}

Check warning on line 146 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/MemoryManager.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/MemoryManager.java#L145-L146

Added lines #L145 - L146 were not covered by tests

public void removeCorrespondingTaskMemoryPool(String threadName) {
threadName2TaskMemoryPoolMap.remove(threadName);
}

Check warning on line 150 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/MemoryManager.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/MemoryManager.java#L149-L150

Added lines #L149 - L150 were not covered by tests

public Queue<MemoryPool> getCurrentQueryMemoryPools() {
return new PriorityQueue<>(queryMemoryPools);

Check warning on line 153 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/MemoryManager.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/MemoryManager.java#L153

Added line #L153 was not covered by tests
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

public interface MemoryArbitrator {

long MAX_WAIT_TIME_FOR_LOCAL_RECLAIM = 1000;
long MAX_WAIT_TIME_FOR_LOCAL_RECLAIM = 2000;

long MAX_WAIT_TIME_FOR_GLOBAL_RECLAIM = 5000;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,19 @@
import org.apache.hugegraph.memory.MemoryManager;
import org.apache.hugegraph.memory.consumer.MemoryConsumer;
import org.apache.hugegraph.memory.pool.impl.MemoryPoolStats;
import org.jetbrains.annotations.TestOnly;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class AbstractMemoryPool implements MemoryPool {

private static final Logger LOG = LoggerFactory.getLogger(AbstractMemoryPool.class);
private final Queue<MemoryPool> children =
protected final Queue<MemoryPool> children =
new PriorityQueue<>((o1, o2) -> (int) (o2.getFreeBytes() - o1.getFreeBytes()));

Check warning on line 38 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/AbstractMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/AbstractMemoryPool.java#L36-L38

Added lines #L36 - L38 were not covered by tests
protected final MemoryManager memoryManager;
protected final ReentrantLock arbitrationLock = new ReentrantLock();
protected final Condition condition = arbitrationLock.newCondition();
// Allocation, deAllocation, arbitration must be serial which is controlled by this lock.
protected final ReentrantLock memoryActionLock = new ReentrantLock();
protected final Condition condition = memoryActionLock.newCondition();
protected final AtomicBoolean isBeingArbitrated = new AtomicBoolean(false);

Check warning on line 43 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/AbstractMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/AbstractMemoryPool.java#L41-L43

Added lines #L41 - L43 were not covered by tests
protected final MemoryPoolStats stats;
protected boolean isClosed = false;

Check warning on line 45 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/AbstractMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/AbstractMemoryPool.java#L45

Added line #L45 was not covered by tests
Expand All @@ -60,7 +62,7 @@ public long tryToReclaimLocalMemory(long neededBytes) {
long totalReclaimedBytes = 0;
long currentNeededBytes = neededBytes;

Check warning on line 63 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/AbstractMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/AbstractMemoryPool.java#L61-L63

Added lines #L61 - L63 were not covered by tests
try {
this.arbitrationLock.lock();
this.memoryActionLock.lock();
this.isBeingArbitrated.set(true);

Check warning on line 66 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/AbstractMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/AbstractMemoryPool.java#L65-L66

Added lines #L65 - L66 were not covered by tests
for (MemoryPool child : this.children) {
long reclaimedMemory = child.tryToReclaimLocalMemory(currentNeededBytes);

Check warning on line 68 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/AbstractMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/AbstractMemoryPool.java#L68

Added line #L68 was not covered by tests
Expand All @@ -79,20 +81,23 @@ public long tryToReclaimLocalMemory(long neededBytes) {
totalReclaimedBytes, neededBytes);
return totalReclaimedBytes;

Check warning on line 82 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/AbstractMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/AbstractMemoryPool.java#L81-L82

Added lines #L81 - L82 were not covered by tests
} finally {
this.stats.setNumShrinks(this.stats.getNumShrinks() + 1);
if (totalReclaimedBytes > 0) {
this.stats.setNumShrinks(this.stats.getNumShrinks() + 1);

Check warning on line 85 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/AbstractMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/AbstractMemoryPool.java#L85

Added line #L85 was not covered by tests
}
this.stats.setAllocatedBytes(
this.stats.getAllocatedBytes() - totalReclaimedBytes);
this.isBeingArbitrated.set(false);
this.arbitrationLock.unlock();
this.condition.signalAll();
this.memoryActionLock.unlock();

Check warning on line 91 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/AbstractMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/AbstractMemoryPool.java#L87-L91

Added lines #L87 - L91 were not covered by tests
}
}

/**
* called when one layer pool is successfully executed and exited.
*/
@Override
public synchronized void releaseSelf(String reason) {
public void releaseSelf(String reason) {
this.memoryActionLock.lock();

Check warning on line 100 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/AbstractMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/AbstractMemoryPool.java#L100

Added line #L100 was not covered by tests
try {
if (this.isBeingArbitrated.get()) {
this.condition.await();

Check warning on line 103 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/AbstractMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/AbstractMemoryPool.java#L103

Added line #L103 was not covered by tests
Expand All @@ -109,6 +114,7 @@ public synchronized void releaseSelf(String reason) {
LOG.error("Failed to release self because ", e);
Thread.currentThread().interrupt();

Check warning on line 115 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/AbstractMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/AbstractMemoryPool.java#L110-L115

Added lines #L110 - L115 were not covered by tests
} finally {
this.memoryActionLock.unlock();

Check warning on line 117 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/AbstractMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/AbstractMemoryPool.java#L117

Added line #L117 was not covered by tests
// Make these objs be GCed by JVM quickly.
this.parent = null;
this.children.clear();

Check warning on line 120 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/AbstractMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/AbstractMemoryPool.java#L119-L120

Added lines #L119 - L120 were not covered by tests
Expand Down Expand Up @@ -198,4 +204,9 @@ public MemoryPool findRootQueryPool() {
}
return getParentPool().findRootQueryPool();

Check warning on line 205 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/AbstractMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/AbstractMemoryPool.java#L205

Added line #L205 was not covered by tests
}

@TestOnly
public int getChildrenCount() {
return this.children.size();

Check warning on line 210 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/AbstractMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/AbstractMemoryPool.java#L210

Added line #L210 was not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.hugegraph.memory.consumer.MemoryConsumer;
import org.apache.hugegraph.memory.pool.impl.MemoryPoolStats;
import org.apache.hugegraph.memory.util.QueryOutOfMemoryException;
import org.jetbrains.annotations.TestOnly;

public interface MemoryPool {

Expand Down Expand Up @@ -51,5 +52,10 @@ public interface MemoryPool {

MemoryPool findRootQueryPool();

MemoryPool addChildPool();

void bindMemoryConsumer(MemoryConsumer memoryConsumer);

@TestOnly
int getChildrenCount();
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ public class MemoryPoolStats {
private final String memoryPoolName;
private long maxCapacity;
private long usedBytes;
// it represents the cumulative used bytes.
private long cumulativeBytes;
private long allocatedBytes;

// it represents the shrinking num of allocatedBytes
private long numShrinks;
// for query pool, it represents the enlarging num of maxCapacity; for other pools, it
// represents the enlarging num of allocatedBytes
private long numExpands;
private long numAborts;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,27 @@ public OperatorMemoryPool(MemoryPool parent, String poolName,
this.memoryConsumers = new HashSet<>();
}

Check warning on line 44 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/OperatorMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/OperatorMemoryPool.java#L41-L44

Added lines #L41 - L44 were not covered by tests

@Override
public MemoryPool addChildPool() {
throw new UnsupportedOperationException();

Check warning on line 48 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/OperatorMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/OperatorMemoryPool.java#L48

Added line #L48 was not covered by tests
}

@Override
public void bindMemoryConsumer(MemoryConsumer memoryConsumer) {
this.memoryConsumers.add(memoryConsumer);
}

Check warning on line 54 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/OperatorMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/OperatorMemoryPool.java#L53-L54

Added lines #L53 - L54 were not covered by tests

@Override
public synchronized void releaseSelf(String reason) {
public void releaseSelf(String reason) {
super.releaseSelf(reason);

Check warning on line 58 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/OperatorMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/OperatorMemoryPool.java#L58

Added line #L58 was not covered by tests
// since it is already closed, its stats will not be updated. so here we can use its
// stats out of memoryActionLock.
this.memoryAllocator.returnMemoryToManager(getAllocatedBytes());

Check warning on line 61 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/OperatorMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/OperatorMemoryPool.java#L61

Added line #L61 was not covered by tests
// release memory consumer, release byte buffer.
this.memoryConsumers.forEach(memoryConsumer -> {
memoryConsumer.getAllMemoryBlock().forEach(memoryAllocator::releaseMemoryBlock);
});
this.memoryConsumers.clear();
super.releaseSelf(reason);
// TODO: release memory consumer, release byte buffer.
}

Check warning on line 67 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/OperatorMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/OperatorMemoryPool.java#L63-L67

Added lines #L63 - L67 were not covered by tests

@Override
Expand All @@ -66,11 +73,12 @@ public long tryToReclaimLocalMemory(long neededBytes) {
return 0;

Check warning on line 73 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/OperatorMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/OperatorMemoryPool.java#L72-L73

Added lines #L72 - L73 were not covered by tests
}
LOG.info("[{}] tryToReclaimLocalMemory: neededBytes={}", this, neededBytes);
long reclaimableBytes = 0;

Check warning on line 76 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/OperatorMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/OperatorMemoryPool.java#L75-L76

Added lines #L75 - L76 were not covered by tests
try {
this.arbitrationLock.lock();
this.memoryActionLock.lock();
this.isBeingArbitrated.set(true);

Check warning on line 79 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/OperatorMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/OperatorMemoryPool.java#L78-L79

Added lines #L78 - L79 were not covered by tests
// 1. try to reclaim self free memory
long reclaimableBytes = getFreeBytes();
reclaimableBytes = getFreeBytes();

Check warning on line 81 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/OperatorMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/OperatorMemoryPool.java#L81

Added line #L81 was not covered by tests
// try its best to reclaim memory
if (reclaimableBytes <= neededBytes) {
// 2. update stats
Expand All @@ -93,9 +101,12 @@ public long tryToReclaimLocalMemory(long neededBytes) {

return neededBytes;

Check warning on line 102 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/OperatorMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/OperatorMemoryPool.java#L102

Added line #L102 was not covered by tests
} finally {
if (reclaimableBytes > 0) {
this.stats.setNumShrinks(this.stats.getNumShrinks() + 1);

Check warning on line 105 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/OperatorMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/OperatorMemoryPool.java#L105

Added line #L105 was not covered by tests
}
this.isBeingArbitrated.set(false);
this.arbitrationLock.unlock();
this.condition.signalAll();
this.memoryActionLock.unlock();

Check warning on line 109 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/OperatorMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/OperatorMemoryPool.java#L107-L109

Added lines #L107 - L109 were not covered by tests
}
}

Expand All @@ -106,9 +117,9 @@ public long tryToReclaimLocalMemory(long neededBytes) {
public Object requireMemory(long bytes) {
try {
// use lock to ensure the atomicity of the two-step operation
this.arbitrationLock.lock();
long realBytes = requestMemoryInternal(bytes);
return tryToAcquireMemoryInternal(realBytes);
this.memoryActionLock.lock();
long ignoredRealAllocatedBytes = requestMemoryInternal(bytes);
return tryToAcquireMemoryInternal(bytes);
} catch (QueryOutOfMemoryException e) {

Check warning on line 123 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/OperatorMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/OperatorMemoryPool.java#L120-L123

Added lines #L120 - L123 were not covered by tests
// Abort this query
LOG.warn("[{}] detected an OOM exception when request memory, will ABORT this " +

Check warning on line 125 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/OperatorMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/OperatorMemoryPool.java#L125

Added line #L125 was not covered by tests
Expand All @@ -117,7 +128,7 @@ public Object requireMemory(long bytes) {
findRootQueryPool().releaseSelf(String.format(e.getMessage()));
return null;

Check warning on line 129 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/OperatorMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/OperatorMemoryPool.java#L128-L129

Added lines #L128 - L129 were not covered by tests
} finally {
this.arbitrationLock.unlock();
this.memoryActionLock.unlock();

Check warning on line 131 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/OperatorMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/OperatorMemoryPool.java#L131

Added line #L131 was not covered by tests
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@

package org.apache.hugegraph.memory.pool.impl;

import static org.apache.hugegraph.memory.MemoryManager.DELIMINATOR;

import org.apache.hugegraph.memory.MemoryManager;
import org.apache.hugegraph.memory.pool.AbstractMemoryPool;
import org.apache.hugegraph.memory.pool.MemoryPool;
import org.apache.hugegraph.memory.util.MemoryManageUtils;
import org.apache.hugegraph.util.Bytes;
import org.slf4j.Logger;
Expand All @@ -27,6 +30,7 @@
public class QueryMemoryPool extends AbstractMemoryPool {

private static final Logger LOG = LoggerFactory.getLogger(QueryMemoryPool.class);

Check warning on line 32 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/QueryMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/QueryMemoryPool.java#L32

Added line #L32 was not covered by tests
private static final String TASK_MEMORY_POOL_NAME_PREFIX = "TaskMemoryPool";
// TODO: read from conf
private static final long QUERY_POOL_MAX_CAPACITY = Bytes.MB * 100;

Expand All @@ -35,6 +39,18 @@ public QueryMemoryPool(String poolName, MemoryManager memoryManager) {
this.stats.setMaxCapacity(QUERY_POOL_MAX_CAPACITY);
}

Check warning on line 40 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/QueryMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/QueryMemoryPool.java#L38-L40

Added lines #L38 - L40 were not covered by tests

@Override
public MemoryPool addChildPool() {
int count = this.children.size();
String poolName =

Check warning on line 45 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/QueryMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/QueryMemoryPool.java#L44-L45

Added lines #L44 - L45 were not covered by tests
TASK_MEMORY_POOL_NAME_PREFIX + DELIMINATOR + count + DELIMINATOR +
System.currentTimeMillis();
MemoryPool taskMemoryPool = new TaskMemoryPool(this, poolName, this.memoryManager);
this.children.add(taskMemoryPool);
LOG.info("QueryPool-{} added task memory pool {}", this, taskMemoryPool);
return taskMemoryPool;

Check warning on line 51 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/QueryMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/QueryMemoryPool.java#L47-L51

Added lines #L47 - L51 were not covered by tests
}

@Override
public long requestMemoryInternal(long bytes) {
if (this.isClosed) {
Expand Down Expand Up @@ -83,8 +99,10 @@ private long tryToExpandSelfCapacity(long size) {
private long requestMemoryThroughArbitration(long bytes) {
LOG.info("[{}] try to request memory from manager through arbitration: size={}", this,
bytes);
this.stats.setNumExpands(this.stats.getNumExpands() + 1);
long reclaimedBytes = this.memoryManager.triggerLocalArbitration(this, bytes);

Check warning on line 102 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/QueryMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/QueryMemoryPool.java#L100-L102

Added lines #L100 - L102 were not covered by tests
if (reclaimedBytes > 0) {
this.stats.setNumExpands(this.stats.getNumExpands() + 1);

Check warning on line 104 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/QueryMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/QueryMemoryPool.java#L104

Added line #L104 was not covered by tests
}
// 1. if arbitrate successes, update stats and return success
if (reclaimedBytes - bytes >= 0) {
// here we don't update capacity & reserved & allocated, because memory is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@

package org.apache.hugegraph.memory.pool.impl;

import static org.apache.hugegraph.memory.MemoryManager.DELIMINATOR;

import org.apache.hugegraph.memory.MemoryManager;
import org.apache.hugegraph.memory.allocator.NettyMemoryAllocator;
import org.apache.hugegraph.memory.pool.AbstractMemoryPool;
import org.apache.hugegraph.memory.pool.MemoryPool;
import org.apache.hugegraph.memory.util.QueryOutOfMemoryException;
Expand All @@ -27,11 +30,33 @@
public class TaskMemoryPool extends AbstractMemoryPool {

private static final Logger LOG = LoggerFactory.getLogger(TaskMemoryPool.class);

Check warning on line 32 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/TaskMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/TaskMemoryPool.java#L32

Added line #L32 was not covered by tests
private static final String OPERATOR_MEMORY_POOL_NAME_PREFIX = "OperatorMemoryPool";

public TaskMemoryPool(MemoryPool parent, String poolName, MemoryManager memoryManager) {
super(parent, poolName, memoryManager);
}

Check warning on line 37 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/TaskMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/TaskMemoryPool.java#L36-L37

Added lines #L36 - L37 were not covered by tests

@Override
public synchronized void releaseSelf(String reason) {
this.memoryManager.removeCorrespondingTaskMemoryPool(Thread.currentThread().getName());
super.releaseSelf(reason);
}

Check warning on line 43 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/TaskMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/TaskMemoryPool.java#L41-L43

Added lines #L41 - L43 were not covered by tests

@Override
public MemoryPool addChildPool() {
int count = this.children.size();
String poolName =

Check warning on line 48 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/TaskMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/TaskMemoryPool.java#L47-L48

Added lines #L47 - L48 were not covered by tests
OPERATOR_MEMORY_POOL_NAME_PREFIX + DELIMINATOR + count + DELIMINATOR +
System.currentTimeMillis();
MemoryPool operatorPool =

Check warning on line 51 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/TaskMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/TaskMemoryPool.java#L50-L51

Added lines #L50 - L51 were not covered by tests
new OperatorMemoryPool(this, poolName,
new NettyMemoryAllocator(this.memoryManager),
this.memoryManager);
this.children.add(operatorPool);
LOG.info("TaskPool-{} added operator memory pool {}", this, operatorPool);
return operatorPool;

Check warning on line 57 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/TaskMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/TaskMemoryPool.java#L55-L57

Added lines #L55 - L57 were not covered by tests
}

@Override
public long requestMemoryInternal(long bytes) throws QueryOutOfMemoryException {
if (this.isClosed) {
Expand All @@ -45,6 +70,7 @@ public long requestMemoryInternal(long bytes) throws QueryOutOfMemoryException {
long parentRes = getParentPool().requestMemoryInternal(bytes);

Check warning on line 70 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/TaskMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/TaskMemoryPool.java#L70

Added line #L70 was not covered by tests
if (parentRes > 0) {
this.stats.setAllocatedBytes(this.stats.getAllocatedBytes() + parentRes);
this.stats.setNumExpands(this.stats.getNumExpands() + 1);

Check warning on line 73 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/TaskMemoryPool.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/pool/impl/TaskMemoryPool.java#L72-L73

Added lines #L72 - L73 were not covered by tests
}
return parentRes;
} catch (InterruptedException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,22 @@
package org.apache.hugegraph.core.memory;public class MemoryConsumerTest {
/*
* 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.hugegraph.core.memory;

public class MemoryConsumerTest extends MemoryManageTest {

}
Loading

0 comments on commit 54d1fd8

Please sign in to comment.