dosubot[bot] commented on issue #2605:
URL:
https://github.com/apache/incubator-hugegraph/issues/2605#issuecomment-2256401080
<!-- Answer -->
The HugeGraph server retrieves partition information from the Placement
Driver (PD) when creating a new graph by using the `PDClient` class.
Specifically, it uses the `getPartitionByCode` method to find the partition
based on a hash code. If the partition does not exist, it creates a new
partition using the `newPartition` method.
Here is the relevant code:
```java
public KVPair<Metapb.Partition, Metapb.Shard> getPartitionByCode(String
graphName, long code) throws PDException {
if (code < 0 || code >= PartitionUtils.MAX_VALUE) {
throw new PDException(Pdpb.ErrorType.NOT_FOUND_VALUE, "code error");
}
// Find the partition ID based on the code, and if it doesn't find,
create a new partition
KVPair<Metapb.Partition, Metapb.Shard> partShard =
cache.getPartitionByCode(graphName, code);
if (partShard == null) {
synchronized (this) {
if (partShard == null) {
partShard = newPartition(graphName, code);
}
}
}
log.debug("{} Partition get code = {}, partition id = {}, start = {},
end = {}, leader = {}",
graphName, (code), partShard.getKey().getId(),
partShard.getKey().getStartKey(), partShard.getKey().getEndKey(),
partShard.getValue());
return partShard;
}
private KVPair<Metapb.Partition, Metapb.Shard> newPartition(String
graphName, long code) throws PDException {
Metapb.Graph graph = getGraph(graphName);
int partitionSize = PartitionUtils.MAX_VALUE / graph.getPartitionCount();
if (PartitionUtils.MAX_VALUE % graph.getPartitionCount() != 0) {
// There is a remainder, and the partition is inexhaustible
partitionSize++;
}
int partitionId = (int) (code / partitionSize);
long startKey = (long) partitionSize * partitionId;
long endKey = (long) partitionSize * (partitionId + 1);
// Check Local
KVPair<Metapb.Partition, Metapb.Shard> partShard =
cache.getPartitionById(graphName, partitionId);
if (partShard == null) {
storeService.allocShards(null, partitionId);
// Assign a store
Metapb.Partition partition = Metapb.Partition.newBuilder()
.setId(partitionId)
.setVersion(0)
.setState(Metapb.PartitionState.PState_Normal)
.setStartKey(startKey)
.setEndKey(endKey)
.setGraphName(graphName)
.build();
log.info("Create newPartition {}", partition);
partShard = new KVPair<>(partition,
storeService.getLeader(partition, 0));
}
cache.updatePartition(partShard.getKey());
return partShard;
}
```
This code shows how the server calculates the partition hash code, checks if
the partition exists, and creates a new partition if necessary. The `PDClient`
class handles the communication with the PD server, including caching and
updating partition information
<sup>[[1]](https://github.com/apache/incubator-hugegraph/blob/master/hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/PartitionService.java)</sup><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>.
<!-- 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]