dosubot[bot] commented on issue #2576:
URL: 
https://github.com/apache/incubator-hugegraph/issues/2576#issuecomment-2225037702

   <!-- Answer -->
   To resolve the cache invalidation issue of the partition->leader shard in 
`ClientCache`, you need to ensure that the cache is properly reset and 
reinitialized when the leader changes in the PD cluster. Here are the key steps 
to address this:
   
   1. **Set Initialization Flag to False**: After resetting the cache, set the 
initialization flag to false to allow reinitialization.
   2. **Update Cache on Miss**: If the cache misses, update the cache with the 
results of the query from the PD.
   
   The provided code already includes mechanisms to handle leader change events 
and invalidate the cache:
   
   ```java
   storeWatcher = pdWatch.watchNode(new PDWatch.Listener<>() {
       @Override
       public void onNext(NodeEvent response) {
           log.info("PDClient receive store event {} {}",
                    response.getEventType(), 
Long.toHexString(response.getNodeId()));
   
           if (response.getEventType() == NODE_PD_LEADER_CHANGE) {
               // pd raft change
               var leaderIp = response.getGraph();
               log.info("watchNode: pd leader changed to {}, current watch:{}",
                        leaderIp, pdWatch.getCurrentHost());
               closeStub(!Objects.equals(pdWatch.getCurrentHost(), leaderIp));
               connectPdWatch(leaderIp);
           }
   
           invalidStoreCache(response.getNodeId());
           eventListeners.forEach(listener -> {
               listener.onStoreChanged(response);
           });
       }
   
       @Override
       public void onError(Throwable throwable) {
           log.error("watchNode exception {}", throwable.getMessage());
           closeStub(false);
       }
   });
   ```
   
   Additionally, the `updatePartitionLeader` method is used to update the cache 
when the leader changes:
   
   ```java
   public void updatePartitionLeader(String graphName, int partId, long 
leaderStoreId) {
       KVPair<Metapb.Partition, Metapb.Shard> partShard = null;
       try {
           partShard = this.getPartitionById(graphName, partId);
   
           if (partShard != null && partShard.getValue().getStoreId() != 
leaderStoreId) {
               var shardGroup = this.getShardGroup(partId);
               Metapb.Shard shard = null;
               List<Metapb.Shard> shards = new ArrayList<>();
   
               for (Metapb.Shard s : shardGroup.getShardsList()) {
                   if (s.getStoreId() == leaderStoreId) {
                       shard = s;
                       shards.add(Metapb.Shard.newBuilder(s)
                                              .setStoreId(s.getStoreId())
                                              
.setRole(Metapb.ShardRole.Leader).build());
                   } else {
                       shards.add(Metapb.Shard.newBuilder(s)
                                              .setStoreId(s.getStoreId())
                                              
.setRole(Metapb.ShardRole.Follower).build());
                   }
               }
   
               if (config.isEnableCache()) {
                   if (shard == null) {
                       cache.removePartition(graphName, partId);
                   }
               }
           }
       } catch (PDException e) {
           log.error("getPartitionException: {}", e.getMessage());
       }
   }
   ```
   
   Ensure these methods are correctly invoked and the cache is properly 
configured to handle these updates. This will help manage the cache 
invalidation issue effectively 
<sup>[[1]](https://github.com/apache/incubator-hugegraph/blob/master/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/PDClient.java)</sup><sup>[[2]](https://github.com/apache/incubator-hugegraph/blob/master/hugegraph-pd/hg-pd-common/src/main/java/org/apache/hugegraph/pd/common/PartitionCache.java)</sup><sup>[[3]](https://github.com/apache/incubator-hugegraph/blob/master/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/ClientCache.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]

Reply via email to