Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server): support MemoryManagement for graph query framework #2649

Merged
merged 51 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 49 commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
7a652be
feat: framework for memoryManagement
Pengzna Aug 25, 2024
ccc918b
wip: memory pool and manager framework
Pengzna Aug 25, 2024
b266358
wip: memory allocation
Pengzna Oct 8, 2024
ff10c3a
wip: memory reclaim
Pengzna Oct 9, 2024
26b0cfe
rename class
Pengzna Oct 9, 2024
f6aeace
fix review
Pengzna Oct 9, 2024
72e5bf3
remove useless allocator
Pengzna Oct 9, 2024
48f4817
netty allocator
Pengzna Oct 10, 2024
d906d04
revert config.properties
Pengzna Oct 13, 2024
b308be0
fix and improvement for allocation and deallocation
Pengzna Oct 22, 2024
09367a1
Merge remote-tracking branch 'refs/remotes/base/master' into memory/m…
Pengzna Oct 22, 2024
ea9a459
move monitor
Pengzna Oct 22, 2024
f552fd2
Revert "move monitor"
Pengzna Oct 22, 2024
8a2c65c
improve memory arbitration
Pengzna Oct 22, 2024
c37f869
suspend query when arbitration & kill query when OOM
Pengzna Oct 23, 2024
5904909
fix review
Pengzna Oct 23, 2024
0e70e44
fury test
Pengzna Oct 23, 2024
5d71541
offHeap magic
Pengzna Oct 23, 2024
f73f0ab
Revert "fury test"
Pengzna Oct 23, 2024
871015e
offHeap magic util
Pengzna Oct 23, 2024
8344443
complete adoption for all id
Pengzna Oct 25, 2024
d9cf408
complete property adoption
Pengzna Oct 26, 2024
aaeacb5
release ByteBuf off heap memory block
Pengzna Oct 26, 2024
ef0d629
complete allocate memory test and fix bug
Pengzna Oct 27, 2024
54d1fd8
fix some bugs: arbitration & suspend
Pengzna Oct 27, 2024
bfe75c0
complete OOM UT and fix bugs
Pengzna Oct 27, 2024
ab1bcde
complete memory management framework UT and fix all bugs
Pengzna Oct 27, 2024
91df57a
fix ut
Pengzna Oct 27, 2024
52ca7af
keep format consistent with original version
Pengzna Oct 28, 2024
ee8e125
fix review
Pengzna Oct 28, 2024
1a7d461
Merge branch 'master' into memory/management
imbajin Oct 28, 2024
de9d7a1
wip: adoption to query chain & introduce factory
Pengzna Oct 28, 2024
46066eb
fix concurrent bug when local arbitrate
Pengzna Oct 29, 2024
4f1e966
add comments
Pengzna Oct 29, 2024
7be5069
Merge remote-tracking branch 'origin/memory/management' into memory/m…
Pengzna Oct 29, 2024
231b647
feat: off-heap object factory
Pengzna Oct 29, 2024
865f1fb
fix gc child bugs and add consumer test
Pengzna Oct 29, 2024
f34e233
fix deallocate netty memory block bug & add complexId test
Pengzna Oct 29, 2024
879390b
complete all ut
Pengzna Oct 29, 2024
a96e9ee
fix all bugs
Pengzna Oct 29, 2024
7c86e84
dependency
Pengzna Oct 29, 2024
5af2cb9
add comments
Pengzna Oct 29, 2024
5e47bb0
add private constructor for singleton
Pengzna Oct 29, 2024
b77346b
add memory management config
Pengzna Oct 29, 2024
d00a8df
improve robustness
Pengzna Oct 29, 2024
fecc909
remove duplicate
Pengzna Oct 29, 2024
31f1feb
improve condition usage
Pengzna Oct 29, 2024
d4035bd
improve log
Pengzna Oct 29, 2024
d87388b
fix memory conservation bug
Pengzna Oct 29, 2024
d25396d
Revert "dependency"
Pengzna Nov 4, 2024
b334c61
revert duplicate known-dependencies.txt under huge-common
Pengzna Nov 4, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why change this file?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why change this file?

maybe influenced by Fury dependencies🤔?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why change this file?

maybe influenced by Fury dependencies🤔?

Yes, and I ran wrong regenerate_known_dependencies.sh which is in hugegraph-dist/scripts/dependency, resulting in a duplicate known-dependencies.txt under hugegraph-dist/scripts/dependency.

Actually, only regenerate_known_dependencies.sh in /install-dist/scripts/dependency is enough and correct. regenerate_known_dependencies.sh in hugegraph-dist/scripts/dependency seems useless and may cause misunderstanding for programmers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@imbajin maybe could entirely remove hugegraph-commons/hugegraph-dist?

Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
animal-sniffer-annotations-1.18.jar
annotations-13.0.jar
annotations-4.1.1.4.jar
bolt-1.6.2.jar
checker-qual-3.5.0.jar
commons-beanutils-1.9.4.jar
commons-codec-1.13.jar
Expand All @@ -10,14 +13,25 @@ commons-lang-2.6.jar
commons-lang3-3.12.0.jar
commons-logging-1.1.1.jar
commons-text-1.9.jar
disruptor-3.3.7.jar
error_prone_annotations-2.3.4.jar
failureaccess-1.0.1.jar
grpc-api-1.28.1.jar
grpc-context-1.28.1.jar
grpc-core-1.28.1.jar
grpc-netty-shaded-1.28.0.jar
grpc-protobuf-1.28.0.jar
grpc-protobuf-lite-1.28.0.jar
grpc-stub-1.28.0.jar
gson-2.8.6.jar
guava-30.0-jre.jar
hamcrest-core-1.3.jar
hessian-3.3.7.jar
j2objc-annotations-1.3.jar
jackson-annotations-2.14.0-rc1.jar
jackson-core-2.14.0-rc1.jar
jackson-databind-2.14.0-rc1.jar
jackson-dataformat-yaml-2.9.3.jar
jackson-jaxrs-base-2.14.0-rc1.jar
jackson-jaxrs-json-provider-2.14.0-rc1.jar
jackson-module-jaxb-annotations-2.14.0-rc1.jar
Expand All @@ -39,7 +53,23 @@ log4j-api-2.18.0.jar
log4j-core-2.18.0.jar
log4j-slf4j-impl-2.18.0.jar
logging-interceptor-4.10.0.jar
lombok-1.18.8.jar
lookout-api-1.4.1.jar
netty-all-4.1.42.Final.jar
okhttp-4.10.0.jar
okio-jvm-3.0.0.jar
opentracing-api-0.22.0.jar
opentracing-mock-0.22.0.jar
opentracing-noop-0.22.0.jar
opentracing-util-0.22.0.jar
perfmark-api-0.19.0.jar
proto-google-common-protos-1.17.0.jar
protobuf-java-3.11.0.jar
slf4j-api-1.7.25.jar
snakeyaml-1.18.jar
sofa-common-tools-1.0.12.jar
sofa-rpc-all-5.7.6.jar
swagger-annotations-1.5.18.jar
swagger-core-1.5.18.jar
swagger-models-1.5.18.jar
tracer-core-3.0.8.jar
validation-api-1.1.0.Final.jar
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;
Pengzna marked this conversation as resolved.
Show resolved Hide resolved

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;
Copy link
Contributor

@javeme javeme Oct 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we keep primitive type long because a object reference also cause 64bits memory when it's off heap, and cause more memory when it's on heap.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, because primitive type long can't be set to null by hand. We need a data structure that can be manually set to null so that it can be GCed immediately to free up memory. Primitive long can only be GCed along with the original object.
image


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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems don't need to transform BinaryId, it's just short-term used during serialization. you cam double check

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok


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
Loading