Skip to content

Commit

Permalink
feat(server): support MemoryManagement for graph query framework (#2649)
Browse files Browse the repository at this point in the history
Users can use **following new options** to configure memory management:

_\# The memory management switch in HugeGraph. Options: off-heap, on-heap, disable._
**memory.mode=off-heap** 
_\# The maximum memory capacity that can be managed for all queries in HugeGraph._
**memory.max_capacity=1073741824** 
_\# The maximum memory capacity that can be managed for a query in HugeGraph._
**memory.one_query_max_capacity=104857600** 
_\# The alignment used for round memory size._
**memory.alignment=8** 

**Detailed docs**: https://github.com/apache/incubator-hugegraph/wiki/%5BMemory-Management%5D-GSoC-2024-Final-Report

![image](https://github.com/user-attachments/assets/411968f8-cbf5-4ea1-8c1f-10c572ecedf2)

---------

Co-authored-by: imbajin <[email protected]>
  • Loading branch information
Pengzna and imbajin authored Nov 5, 2024
1 parent 96315aa commit 35e5a8c
Show file tree
Hide file tree
Showing 42 changed files with 4,222 additions and 44 deletions.
5 changes: 5 additions & 0 deletions hugegraph-server/hugegraph-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@
<groupId>org.apache.tinkerpop</groupId>
<artifactId>gremlin-driver</artifactId>
</dependency>
<dependency>
<groupId>org.apache.fury</groupId>
<artifactId>fury-core</artifactId>
<version>0.9.0-SNAPSHOT</version>
</dependency>

<!-- jraft -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
import org.apache.hugegraph.masterelection.RoleElectionStateMachine;
import org.apache.hugegraph.masterelection.StandardClusterRoleStore;
import org.apache.hugegraph.masterelection.StandardRoleElectionStateMachine;
import org.apache.hugegraph.memory.MemoryManager;
import org.apache.hugegraph.memory.util.RoundUtil;
import org.apache.hugegraph.meta.MetaManager;
import org.apache.hugegraph.perf.PerfUtil.Watched;
import org.apache.hugegraph.rpc.RpcServiceConfig4Client;
Expand Down Expand Up @@ -215,6 +217,13 @@ public StandardHugeGraph(HugeConfig config) {
this.readMode = GraphReadMode.OLTP_ONLY;
this.schedulerType = config.get(CoreOptions.SCHEDULER_TYPE);

MemoryManager.setMemoryMode(
MemoryManager.MemoryMode.fromValue(config.get(CoreOptions.MEMORY_MODE)));
MemoryManager.setMaxMemoryCapacityInBytes(config.get(CoreOptions.MAX_MEMORY_CAPACITY));
MemoryManager.setMaxMemoryCapacityForOneQuery(
config.get(CoreOptions.ONE_QUERY_MAX_MEMORY_CAPACITY));
RoundUtil.setAlignment(config.get(CoreOptions.MEMORY_ALIGNMENT));

LockUtil.init(this.name);

try {
Expand Down Expand Up @@ -477,8 +486,8 @@ private ISchemaTransaction openSchemaTransaction() throws HugeException {
try {
if (isHstore()) {
return new CachedSchemaTransactionV2(
MetaManager.instance().metaDriver(),
MetaManager.instance().cluster(), this.params);
MetaManager.instance().metaDriver(),
MetaManager.instance().cluster(), this.params);
}
return new CachedSchemaTransaction(this.params, loadSchemaStore());
} catch (BackendException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,16 +215,21 @@ public Number queryNumber(Query query) {
/**
* Query as an Id for cache
*/
static class QueryId implements Id {
public static class QueryId implements Id {

private String query;
private int hashCode;
protected String query;
protected int hashCode;

public QueryId(Query q) {
this.query = q.toString();
this.hashCode = q.hashCode();
}

public QueryId(String query, int hashCode) {
this.query = query;
this.hashCode = hashCode;
}

@Override
public IdType type() {
return IdType.UNKNOWN;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
* > sortKeys > target-vertex-id }
* NOTE:
* <p>1. for edges with edgeLabelType = NORMAL: edgeLabelId = parentEdgeLabelId = subEdgeLabelId;
* for edges with edgeLabelType = PARENT: edgeLabelId = subEdgeLabelId, parentEdgeLabelId =
* edgeLabelId.fatherId
* for edges with edgeLabelType = PARENT: edgeLabelId = subEdgeLabelId, parentEdgeLabelId =
* edgeLabelId.fatherId
* <p>2.if we use `entry.type()` which is IN or OUT as a part of id,
* an edge's id will be different due to different directions (belongs
* to 2 owner vertex)
Expand All @@ -49,15 +49,14 @@ public class EdgeId implements Id {
HugeKeys.OTHER_VERTEX
};

private final Id ownerVertexId;
private final Directions direction;
private final Id edgeLabelId;
private final Id subLabelId;
private final String sortValues;
private final Id otherVertexId;

private final boolean directed;
private String cache;
protected final Id ownerVertexId;
protected final Id edgeLabelId;
protected final Id subLabelId;
protected final Id otherVertexId;
protected final Directions direction;
protected final boolean directed;
protected String sortValues;
protected String cache;

public EdgeId(HugeVertex ownerVertex, Directions direction,
Id edgeLabelId, Id subLabelId, String sortValues, HugeVertex otherVertex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,15 @@ public static IdType idType(Id id) {
return IdType.UNKNOWN;
}

private static int compareType(Id id1, Id id2) {
public static int compareType(Id id1, Id id2) {
return idType(id1).ordinal() - idType(id2).ordinal();
}

/****************************** id defines ******************************/

public static final class StringId implements Id {
public static class StringId implements Id {

private final String id;
protected String id;

public StringId(String id) {
E.checkArgument(!id.isEmpty(), "The id can't be empty");
Expand Down Expand Up @@ -196,11 +196,11 @@ public String toString() {
}
}

public static final class LongId extends Number implements Id {
public static class LongId extends Number implements Id {

private static final long serialVersionUID = -7732461469037400190L;

private final long id;
protected Long id;

public LongId(long id) {
this.id = id;
Expand Down Expand Up @@ -270,7 +270,7 @@ public String toString() {

@Override
public int intValue() {
return (int) this.id;
return this.id.intValue();
}

@Override
Expand All @@ -289,9 +289,9 @@ public double doubleValue() {
}
}

public static final class UuidId implements Id {
public static class UuidId implements Id {

private final UUID uuid;
protected UUID uuid;

public UuidId(String string) {
this(StringEncoding.uuid(string));
Expand Down Expand Up @@ -379,9 +379,9 @@ public String toString() {
/**
* This class is just used by backend store for wrapper object as Id
*/
public static final class ObjectId implements Id {
public static class ObjectId implements Id {

private final Object object;
protected Object object;

public ObjectId(Object object) {
E.checkNotNull(object, "object");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,10 @@ public int hashCode() {
return this.id().hashCode() ^ this.columns.size();
}

public static final class BinaryId implements Id {
public static class BinaryId implements Id {

private final byte[] bytes;
private final Id id;
protected byte[] bytes;
protected Id id;

public BinaryId(byte[] bytes, Id id) {
this.bytes = bytes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.apache.hugegraph.backend.tx.GraphTransaction.COMMIT_BATCH;
import static org.apache.hugegraph.config.OptionChecker.allowValues;
import static org.apache.hugegraph.config.OptionChecker.disallowEmpty;
import static org.apache.hugegraph.config.OptionChecker.nonNegativeInt;
import static org.apache.hugegraph.config.OptionChecker.positiveInt;
import static org.apache.hugegraph.config.OptionChecker.rangeInt;

Expand Down Expand Up @@ -681,4 +682,32 @@ public static synchronized CoreOptions instance() {
disallowEmpty(),
"127.0.0.1:8686"
);

public static final ConfigOption<String> MEMORY_MODE = new ConfigOption<>(
"memory.mode",
"The memory mode used for query in HugeGraph.",
disallowEmpty(),
"off-heap"
);

public static final ConfigOption<Long> MAX_MEMORY_CAPACITY = new ConfigOption<>(
"memory.max_capacity",
"The maximum memory capacity that can be managed for all queries in HugeGraph.",
nonNegativeInt(),
Bytes.GB
);

public static final ConfigOption<Long> ONE_QUERY_MAX_MEMORY_CAPACITY = new ConfigOption<>(
"memory.one_query_max_capacity",
"The maximum memory capacity that can be managed for a query in HugeGraph.",
nonNegativeInt(),
Bytes.MB * 100
);

public static final ConfigOption<Long> MEMORY_ALIGNMENT = new ConfigOption<>(
"memory.alignment",
"The alignment used for round memory size.",
nonNegativeInt(),
8L
);
}
Loading

0 comments on commit 35e5a8c

Please sign in to comment.