Skip to content

Commit ccc918b

Browse files
committed
wip: memory pool and manager framework
1 parent 7a652be commit ccc918b

15 files changed

+431
-60
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,88 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
118
package org.apache.hugegraph.memory;
219

20+
import java.util.Set;
21+
import java.util.concurrent.CopyOnWriteArraySet;
22+
import java.util.concurrent.atomic.AtomicLong;
23+
24+
import org.apache.hugegraph.memory.allocator.AbstractAllocator;
25+
import org.apache.hugegraph.memory.allocator.IMemoryAllocator;
26+
import org.apache.hugegraph.memory.arbitrator.IMemoryArbitrator;
27+
import org.apache.hugegraph.memory.arbitrator.MemoryArbitrator;
328
import org.apache.hugegraph.memory.pool.IMemoryPool;
4-
import org.apache.hugegraph.memory.pool.impl.RootMemoryPool;
29+
import org.apache.hugegraph.memory.pool.impl.QueryMemoryPool;
530

631
public class MemoryManager {
732

8-
private static final String ROOT_POOL_NAME = "RootQueryMemoryPool";
9-
private static final IMemoryPool ROOT = new RootMemoryPool(null, ROOT_POOL_NAME);
33+
private static final String QUERY_MEMORY_POOL_NAME_PREFIX = "QueryMemoryPool";
34+
private static final String DELIMINATOR = "_";
35+
// TODO: read it from conf, current 1G
36+
private final AtomicLong currentMemoryCapacityInBytes = new AtomicLong(1000_000_000);
37+
private final Set<IMemoryPool> queryMemoryPools = new CopyOnWriteArraySet<>();
38+
private final IMemoryArbitrator memoryArbitrator;
39+
// TODO: implement different allocate strategy.
40+
private final IMemoryAllocator memoryAllocator;
41+
// TODO: integrated with mingzhen's monitor thread
42+
// private final Runnable queryGCThread;
43+
44+
private MemoryManager() {
45+
this.memoryArbitrator = new MemoryArbitrator();
46+
this.memoryAllocator = new AbstractAllocator() {
47+
@Override
48+
public long tryToAllocateOffHeap(long size) {
49+
return 0;
50+
}
51+
52+
@Override
53+
public long forceAllocateOffHeap(long size) {
54+
return 0;
55+
}
56+
};
57+
}
58+
59+
public IMemoryPool addQueryMemoryPool() {
60+
int count = queryMemoryPools.size();
61+
String poolName =
62+
QUERY_MEMORY_POOL_NAME_PREFIX + DELIMINATOR + count + DELIMINATOR +
63+
System.currentTimeMillis();
64+
IMemoryPool queryPool = new QueryMemoryPool(poolName, this);
65+
queryMemoryPools.add(queryPool);
66+
return queryPool;
67+
}
68+
69+
public void gcQueryMemoryPool(IMemoryPool pool) {
70+
queryMemoryPools.remove(pool);
71+
long reclaimedMemory = pool.getAllocatedBytes();
72+
pool.releaseSelf();
73+
currentMemoryCapacityInBytes.addAndGet(reclaimedMemory);
74+
}
75+
76+
private static class MemoryManagerHolder {
77+
78+
private static final MemoryManager INSTANCE = new MemoryManager();
79+
80+
private MemoryManagerHolder() {
81+
// empty constructor
82+
}
83+
}
1084

85+
public static MemoryManager getInstance() {
86+
return MemoryManagerHolder.INSTANCE;
87+
}
1188
}

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/allocator/AbstractAllocator.java

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
118
package org.apache.hugegraph.memory.allocator;
219

3-
public abstract class AbstractAllocator implements IAllocator {
20+
public abstract class AbstractAllocator implements IMemoryAllocator {
421

522
@Override
623
public long tryToAllocateOnHeap(long size) {

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/allocator/IAllocator.java

-12
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.hugegraph.memory.allocator;
19+
20+
// TODO(pjz): implement different memory allocate strategy.
21+
public interface IMemoryAllocator {
22+
23+
long tryToAllocateOnHeap(long size);
24+
25+
long forceAllocateOnHeap(long size);
26+
27+
long tryToAllocateOffHeap(long size);
28+
29+
long forceAllocateOffHeap(long size);
30+
}

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/arbitrator/IMemoryArbitrator.java

+17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
118
package org.apache.hugegraph.memory.arbitrator;
219

320
public interface IMemoryArbitrator {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.hugegraph.memory.arbitrator;
19+
20+
public class MemoryArbitrator implements IMemoryArbitrator {
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
118
package org.apache.hugegraph.memory.consumer;
219

20+
// TODO(pjz): integrated it with HG objects such as edges and vertex.
321
public interface IMemoryConsumer {
422

523
}

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

+81-4
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,97 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
118
package org.apache.hugegraph.memory.pool;
219

320
import java.util.Set;
4-
import java.util.concurrent.CopyOnWriteArraySet;
21+
import java.util.TreeSet;
522

623
import org.apache.hugegraph.memory.pool.impl.MemoryPoolStats;
724

825
public abstract class AbstractMemoryPool implements IMemoryPool {
9-
private final IMemoryPool parent;
10-
private final Set<IMemoryPool> children = new CopyOnWriteArraySet<>();
11-
private final MemoryPoolStats stats;
26+
27+
private final Set<IMemoryPool> children =
28+
new TreeSet<>((o1, o2) -> (int) (o2.getFreeBytes() - o1.getFreeBytes()));
29+
private IMemoryPool parent;
30+
private MemoryPoolStats stats;
1231

1332
public AbstractMemoryPool(IMemoryPool parent, String memoryPoolName) {
1433
this.parent = parent;
1534
this.stats = new MemoryPoolStats(memoryPoolName);
1635
}
1736

37+
@Override
38+
public long tryToReclaimLocalMemory(long neededBytes) {
39+
long totalReclaimedBytes = 0;
40+
long currentNeededBytes = neededBytes;
41+
try {
42+
for (IMemoryPool child : this.children) {
43+
long reclaimedMemory = child.tryToReclaimLocalMemory(currentNeededBytes);
44+
if (reclaimedMemory > 0) {
45+
currentNeededBytes -= reclaimedMemory;
46+
totalReclaimedBytes += reclaimedMemory;
47+
// Reclaim enough memory.
48+
if (currentNeededBytes <= 0) {
49+
break;
50+
}
51+
}
52+
}
53+
return totalReclaimedBytes;
54+
} finally {
55+
this.stats.setNumShrinks(this.stats.getNumShrinks() + 1);
56+
this.stats.setAllocatedBytes(
57+
this.stats.getAllocatedBytes() - totalReclaimedBytes);
58+
}
59+
}
60+
61+
@Override
62+
public void releaseSelf() {
63+
try {
64+
for (IMemoryPool child : this.children) {
65+
child.releaseSelf();
66+
}
67+
} finally {
68+
// Make these objs be GCed by JVM quickly.
69+
this.stats = null;
70+
this.parent = null;
71+
this.children.clear();
72+
}
73+
}
74+
75+
@Override
76+
public long getMaxCapacityBytes() {
77+
return stats.getMaxCapacity();
78+
}
79+
80+
@Override
81+
public long getUsedBytes() {
82+
return stats.getUsedBytes();
83+
}
84+
85+
@Override
86+
public long getFreeBytes() {
87+
return stats.getAllocatedBytes() - stats.getUsedBytes();
88+
}
89+
90+
@Override
91+
public long getAllocatedBytes() {
92+
return stats.getAllocatedBytes();
93+
}
94+
1895
@Override
1996
public MemoryPoolStats getSnapShot() {
2097
return stats;
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
118
package org.apache.hugegraph.memory.pool;
219

320
import java.util.Set;
@@ -8,19 +25,29 @@ public interface IMemoryPool {
825

926
MemoryPoolStats getSnapShot();
1027

28+
long tryToReclaimLocalMemory(long neededBytes);
29+
30+
long tryToAcquireMemory(long bytes);
31+
32+
void releaseSelf();
33+
34+
boolean tryToDiskSpill();
35+
36+
long getAllocatedBytes();
37+
38+
long getUsedBytes();
39+
40+
long getFreeBytes();
41+
42+
long getMaxCapacityBytes();
43+
1144
String getName();
1245

1346
IMemoryPool getParentPool();
1447

1548
Set<IMemoryPool> getChildrenPools();
1649

17-
long allocate(long bytes);
18-
19-
long free(long bytes);
20-
2150
long requestMemory(long bytes);
2251

2352
long reclaimMemory(long bytes, long maxWaitMs);
24-
25-
// visitChildren,传递方法引用
2653
}

0 commit comments

Comments
 (0)