dosubot[bot] commented on issue #2605:
URL:
https://github.com/apache/incubator-hugegraph/issues/2605#issuecomment-2256222067
<!-- Answer -->
The HugeGraph server sends batch requests to a specific hstore by
determining the partition ID for each batch entry. The Placement Driver (PD) is
responsible for selecting the appropriate hstore from a list of existing
hstores and informing the server. This is evident from the following code
snippet:
```java
@Override
public void batch(BatchReq request, StreamObserver<FeedbackRes> observer) {
String graph = request.getHeader().getGraph();
List<BatchEntry> list = request.getWriteReq().getEntryList();
PdProvider pd = getPD();
try {
GraphManager graphManager = pd.getGraphManager();
Graph managerGraph = graphManager.getGraph(graph);
if (managerGraph != null && graph.endsWith("/g")) {
Metapb.Graph g = managerGraph.getProtoObj();
if (g == null || g.getGraphState() == null) {
g = pd.getPDClient().getGraphWithOutException(graph);
managerGraph.setGraph(g);
}
if (g != null) {
Metapb.GraphState graphState = g.getGraphState();
if (graphState != null) {
GraphMode graphMode = graphState.getMode();
if (graphMode != null &&
graphMode.getNumber() == GraphMode.ReadOnly_VALUE) {
Metapb.Graph pdGraph =
pd.getPDClient().getGraph(graph);
Metapb.GraphState pdGraphState =
pdGraph.getGraphState();
if (pdGraphState != null &&
pdGraphState.getMode() != null &&
pdGraphState.getMode().getNumber() ==
GraphMode.ReadOnly_VALUE) {
throw new PDException(-1,
"the graph space size " +
"has " +
"reached the threshold");
}
managerGraph.setProtoObj(pdGraph);
}
}
}
}
} catch (PDException e) {
ResStatus status = ResStatus.newBuilder()
.setCode(ResCode.RES_CODE_EXCESS)
.setMsg(e.getMessage())
.build();
FeedbackRes feedbackRes = FeedbackRes.newBuilder()
.setStatus(status)
.build();
observer.onNext(feedbackRes);
observer.onCompleted();
return;
}
Map<Integer, List<BatchEntry>> groups = new HashMap<>();
list.forEach((entry) -> {
Key startKey = entry.getStartKey();
if (startKey.getCode() == HgStoreConst.SCAN_ALL_PARTITIONS_ID) {
List<Integer> ids =
storeService.getGraphLeaderPartitionIds(graph);
ids.forEach(id -> {
if (!groups.containsKey(id)) {
groups.put(id, new LinkedList<>());
}
groups.get(id).add(entry);
});
} else {
Integer partitionId =
pd.getPartitionByCode(graph, startKey.getCode())
.getId();
if (!groups.containsKey(partitionId)) {
groups.put(partitionId, new LinkedList<>());
}
groups.get(partitionId).add(entry);
}
});
BatchGrpcClosure<FeedbackRes> closure =
new BatchGrpcClosure<>(groups.size());
groups.forEach((partition, entries) -> {
storeService.addRaftTask(HgStoreNodeService.BATCH_OP, graph,
partition,
BatchReq.newBuilder()
.setHeader(request.getHeader())
.setWriteReq(
BatchWriteReq.newBuilder()
.addAllEntry(
entries))
.build(),
closure.newRaftClosure());
});
if (!graph.isEmpty()) {
closure.waitFinish(observer, r -> closure.selectError(r),
appConfig.getRaft().getRpcTimeOut());
} else {
observer.onNext(
FeedbackRes.newBuilder().setStatus(HgGrpc.success())
.build());
observer.onCompleted();
}
}
```
This code shows that the PD is queried to get the graph manager and the
appropriate graph state. The PD also provides the partition ID for each batch
entry, which is then used to group the entries by partition. The server then
sends the batch requests to the appropriate hstore based on these partition IDs
<sup>[[1]](https://github.com/apache/incubator-hugegraph/blob/master/hugegraph-store/hg-store-node/src/main/java/org/apache/hugegraph/store/node/grpc/HgStoreSessionImpl.java)</sup>.
Additionally, the `PDClient` class in the HugeGraph project provides methods
to interact with the PD, such as `getPartitionByCode` and
`getGraphWithOutException`, which are used to determine the appropriate hstore
for each partition. The `PDClient` also handles events like partition changes
and store updates, ensuring that the server always has the latest information
about the hstores
<sup>[[2]](https://github.com/apache/incubator-hugegraph/blob/master/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/PDClient.java)</sup>.
The `FakePdServiceProvider` class further illustrates how the PDClient is
used to manage partition and store information. It includes methods like
`getPartitionByID`, `getPartitionByCode`, and event listeners to handle changes
in the PD, ensuring that the server is always informed of the latest hstore
configurations
<sup>[[3]](https://github.com/apache/incubator-hugegraph/blob/master/hugegraph-store/hg-store-core/src/main/java/org/apache/hugegraph/store/pd/FakePdServiceProvider.java)</sup>.
<!-- Dosu Comment Footer -->
*To continue talking to [Dosu](https://dosu.dev), mention @dosu.*
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]