This is an automated email from the ASF dual-hosted git repository.

tanxinyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/master by this push:
     new e5b87219c08 [region migration] Ratis delete local peer when 
resetPeerList is called and myself not in correct peer list #13282
e5b87219c08 is described below

commit e5b87219c088daf534d00e97b27be240f500b926
Author: Li Yu Heng <[email protected]>
AuthorDate: Fri Aug 23 16:29:10 2024 +0800

    [region migration] Ratis delete local peer when resetPeerList is called and 
myself not in correct peer list #13282
---
 .../org/apache/iotdb/consensus/IConsensus.java     |  4 +--
 .../apache/iotdb/consensus/iot/IoTConsensus.java   |  3 +-
 .../iotdb/consensus/ratis/RatisConsensus.java      | 32 ++++++++++++++++++++--
 .../iotdb/consensus/simple/SimpleConsensus.java    |  3 +-
 4 files changed, 35 insertions(+), 7 deletions(-)

diff --git 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/IConsensus.java 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/IConsensus.java
index 7dc3fb6e94d..0e3af42d802 100644
--- 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/IConsensus.java
+++ 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/IConsensus.java
@@ -151,11 +151,11 @@ public interface IConsensus {
    * so it will only be less but not more.
    *
    * @param groupId the consensus group
-   * @param peers the new peer list
+   * @param correctPeers the correct peer list
    * @throws ConsensusException when resetPeerList doesn't success with other 
reasons
    * @throws ConsensusGroupNotExistException when the specified consensus 
group doesn't exist
    */
-  void resetPeerList(ConsensusGroupId groupId, List<Peer> peers) throws 
ConsensusException;
+  void resetPeerList(ConsensusGroupId groupId, List<Peer> correctPeers) throws 
ConsensusException;
 
   // management API
 
diff --git 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/iot/IoTConsensus.java
 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/iot/IoTConsensus.java
index 89894a4c01a..ed089ff63bf 100644
--- 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/iot/IoTConsensus.java
+++ 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/iot/IoTConsensus.java
@@ -477,12 +477,13 @@ public class IoTConsensus implements IConsensus {
   @Override
   public void resetPeerList(ConsensusGroupId groupId, List<Peer> correctPeers)
       throws ConsensusException {
+    logger.info("[RESET PEER LIST] Start to reset peer list to {}", 
correctPeers);
     IoTConsensusServerImpl impl =
         Optional.ofNullable(stateMachineMap.get(groupId))
             .orElseThrow(() -> new ConsensusGroupNotExistException(groupId));
     Peer localPeer = new Peer(groupId, thisNodeId, thisNode);
     if (!correctPeers.contains(localPeer)) {
-      logger.warn(
+      logger.info(
           "[RESET PEER LIST] Local peer is not in the correct configuration, 
delete local peer {}",
           groupId);
       deleteLocalPeer(groupId);
diff --git 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/RatisConsensus.java
 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/RatisConsensus.java
index f346d90ac35..3bf52b11f99 100644
--- 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/RatisConsensus.java
+++ 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/RatisConsensus.java
@@ -563,7 +563,9 @@ class RatisConsensus implements IConsensus {
   }
 
   @Override
-  public void resetPeerList(ConsensusGroupId groupId, List<Peer> peers) throws 
ConsensusException {
+  public void resetPeerList(ConsensusGroupId groupId, List<Peer> correctPeers)
+      throws ConsensusException {
+    logger.info("[RESET PEER LIST] Start to reset peer list to {}", 
correctPeers);
     final RaftGroupId raftGroupId = 
Utils.fromConsensusGroupIdToRaftGroupId(groupId);
     final RaftGroup group = getGroupInfo(raftGroupId);
 
@@ -571,11 +573,35 @@ class RatisConsensus implements IConsensus {
       throw new ConsensusGroupNotExistException(groupId);
     }
 
+    boolean myselfInCorrectPeers =
+        correctPeers.stream()
+            .map(
+                peer ->
+                    Utils.fromNodeInfoAndPriorityToRaftPeer(
+                        peer.getNodeId(), peer.getEndpoint(), 
DEFAULT_PRIORITY))
+            .anyMatch(
+                raftPeer ->
+                    myself.getId() == raftPeer.getId()
+                        && myself.getAddress().equals(raftPeer.getAddress()));
+    if (!myselfInCorrectPeers) {
+      logger.info(
+          "[RESET PEER LIST] Local peer is not in the correct peer list, 
delete local peer {}",
+          groupId);
+      deleteLocalPeer(groupId);
+      return;
+    }
+
     final List<RaftPeer> newGroupPeers =
-        Utils.fromPeersAndPriorityToRaftPeers(peers, DEFAULT_PRIORITY);
+        Utils.fromPeersAndPriorityToRaftPeers(correctPeers, DEFAULT_PRIORITY);
     final RaftGroup newGroup = RaftGroup.valueOf(raftGroupId, newGroupPeers);
 
-    sendReconfiguration(newGroup);
+    RaftClientReply reply = sendReconfiguration(newGroup);
+    if (reply.isSuccess()) {
+      logger.info("[RESET PEER LIST] Peer list has been reset to {}", 
newGroupPeers);
+    } else {
+      logger.warn(
+          "[RESET PEER LIST] Peer list failed to reset to {}, reply is {}", 
newGroup, reply);
+    }
   }
 
   /** NOTICE: transferLeader *does not guarantee* the leader be transferred to 
newLeader. */
diff --git 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/simple/SimpleConsensus.java
 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/simple/SimpleConsensus.java
index 8547b52b7c4..19258309028 100644
--- 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/simple/SimpleConsensus.java
+++ 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/simple/SimpleConsensus.java
@@ -270,7 +270,8 @@ class SimpleConsensus implements IConsensus {
   }
 
   @Override
-  public void resetPeerList(ConsensusGroupId groupId, List<Peer> peers) throws 
ConsensusException {
+  public void resetPeerList(ConsensusGroupId groupId, List<Peer> correctPeers)
+      throws ConsensusException {
     throw new ConsensusException("SimpleConsensus does not support reset peer 
list");
   }
 

Reply via email to