|
| 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 | + |
1 | 18 | package org.apache.hugegraph.memory;
|
2 | 19 |
|
| 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; |
3 | 28 | 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; |
5 | 30 |
|
6 | 31 | public class MemoryManager {
|
7 | 32 |
|
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 | + } |
10 | 84 |
|
| 85 | + public static MemoryManager getInstance() { |
| 86 | + return MemoryManagerHolder.INSTANCE; |
| 87 | + } |
11 | 88 | }
|
0 commit comments