From 526f678f74610fa6134d91613bf6e36bcc396284 Mon Sep 17 00:00:00 2001 From: William Song <48054931+SzyWilliam@users.noreply.github.com> Date: Mon, 27 Jan 2025 08:48:36 +0800 Subject: [PATCH] [RatisConsensus] Prevent misuse of addRemotePeer (#14779) --- .../apache/iotdb/consensus/ratis/RatisConsensus.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) 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 384730a97421..7a7bdc2f9c5c 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 @@ -578,7 +578,16 @@ public void addRemotePeer(ConsensusGroupId groupId, Peer peer) throws ConsensusE RaftPeer peerToAdd = Utils.fromPeerAndPriorityToRaftPeer(peer, DEFAULT_PRIORITY); // pre-condition: peer not in this group - if (group.getPeers().contains(peerToAdd)) { + if (group.getPeers().stream() + .anyMatch( + p -> + p.getId().equals(peerToAdd.getId()) + || p.getAddress().equals(peerToAdd.getAddress()))) { + logger.warn( + "{}: try to add a peer {} with conflicting id or address in {}", + this, + peerToAdd, + group.getPeers()); throw new PeerAlreadyInConsensusGroupException(groupId, peer); }