This is an automated email from the ASF dual-hosted git repository.
tanxinyu pushed a commit to branch region_migration
in repository https://gitbox.apache.org/repos/asf/iotdb.git
The following commit(s) were added to refs/heads/region_migration by this push:
new 5314136277c [To region_migration] Refactor resetPeer in IConsensus
(#12124)
5314136277c is described below
commit 5314136277c74a79e31c83504a9c1ea4dcff8dee
Author: William Song <[email protected]>
AuthorDate: Tue Mar 5 17:58:39 2024 +0800
[To region_migration] Refactor resetPeer in IConsensus (#12124)
---
.../org/apache/iotdb/consensus/IConsensus.java | 3 +-
.../apache/iotdb/consensus/iot/IoTConsensus.java | 11 +++----
.../iotdb/consensus/ratis/RatisConsensus.java | 36 +++++-----------------
.../iotdb/consensus/simple/SimpleConsensus.java | 3 +-
4 files changed, 14 insertions(+), 39 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 dc3942ca071..e9860379ed4 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
@@ -150,11 +150,10 @@ public interface IConsensus {
*
* @param groupId the consensus group
* @param peers the new peer list
- * @return reset result
* @throws ConsensusException when resetPeerList doesn't success with other
reasons
* @throws ConsensusGroupNotExistException when the specified consensus
group doesn't exist
*/
- TSStatus resetPeerList(ConsensusGroupId groupId, List<Peer> peers) throws
ConsensusException;
+ void resetPeerList(ConsensusGroupId groupId, List<Peer> peers) 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 fd42f127244..4142c853c31 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
@@ -393,16 +393,14 @@ public class IoTConsensus implements IConsensus {
return new ArrayList<>(stateMachineMap.keySet());
}
- public TSStatus resetPeerList(ConsensusGroupId groupId, List<Peer> peers)
- throws ConsensusException {
+ public void resetPeerList(ConsensusGroupId groupId, List<Peer> peers) throws
ConsensusException {
IoTConsensusServerImpl impl =
Optional.ofNullable(stateMachineMap.get(groupId))
.orElseThrow(() -> new ConsensusGroupNotExistException(groupId));
if (impl.isReadOnly()) {
- return StatusUtils.getStatus(TSStatusCode.SYSTEM_READ_ONLY);
+ throw new ConsensusException("system is in read-only status now");
} else if (!impl.isActive()) {
- return RpcUtils.getStatus(
- TSStatusCode.WRITE_PROCESS_REJECT,
+ throw new ConsensusException(
"peer is inactive and not ready to receive reset configuration
request.");
} else {
for (Peer peer : impl.getConfiguration()) {
@@ -411,12 +409,11 @@ public class IoTConsensus implements IConsensus {
removeRemotePeer(groupId, peer);
} catch (ConsensusException e) {
logger.error("Failed to remove peer {} from group {}", peer,
groupId, e);
- return RpcUtils.getStatus(TSStatusCode.INTERNAL_SERVER_ERROR,
e.getMessage());
+ throw e;
}
}
}
impl.resetConfiguration(peers);
- return RpcUtils.getStatus(TSStatusCode.SUCCESS_STATUS);
}
}
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 86b77e16c8e..67d3b947b44 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
@@ -51,7 +51,6 @@ import
org.apache.iotdb.consensus.ratis.metrics.RatisMetricsManager;
import org.apache.iotdb.consensus.ratis.utils.Retriable;
import org.apache.iotdb.consensus.ratis.utils.RetryPolicy;
import org.apache.iotdb.consensus.ratis.utils.Utils;
-import org.apache.iotdb.rpc.RpcUtils;
import org.apache.iotdb.rpc.TSStatusCode;
import org.apache.commons.pool2.KeyedObjectPool;
@@ -539,39 +538,20 @@ class RatisConsensus implements IConsensus {
}
@Override
- public TSStatus resetPeerList(ConsensusGroupId groupId, List<Peer> peers)
- throws ConsensusException {
- RaftGroupId raftGroupId = Utils.fromConsensusGroupIdToRaftGroupId(groupId);
- RaftGroup group = getGroupInfo(raftGroupId);
+ public void resetPeerList(ConsensusGroupId groupId, List<Peer> peers) throws
ConsensusException {
+ final RaftGroupId raftGroupId =
Utils.fromConsensusGroupIdToRaftGroupId(groupId);
+ final RaftGroup group = getGroupInfo(raftGroupId);
// pre-conditions: group exists and myself in this group
if (group == null || !group.getPeers().contains(myself)) {
throw new ConsensusGroupNotExistException(groupId);
}
- TSStatus writeResult = RpcUtils.getStatus(TSStatusCode.SUCCESS_STATUS);
- for (Peer peer : peers) {
- RaftPeer peerToRemove = Utils.fromPeerAndPriorityToRaftPeer(peer,
DEFAULT_PRIORITY);
- // pre-condition: peer is a member of groupId
- if (!group.getPeers().contains(peerToRemove)) {
- throw new PeerAlreadyInConsensusGroupException(groupId, peer);
- }
- // update group peer information
- List<RaftPeer> newConfig =
- group.getPeers().stream()
- .filter(raftPeer -> !raftPeer.equals(peerToRemove))
- .collect(Collectors.toList());
- RaftClientReply reply =
sendReconfiguration(RaftGroup.valueOf(raftGroupId, newConfig));
- if (!reply.isSuccess()) {
- throw new RatisRequestFailedException(reply.getException());
- }
- try {
- writeResult =
Utils.deserializeFrom(reply.getMessage().getContent().asReadOnlyByteBuffer());
- } catch (Exception e) {
- throw new RatisRequestFailedException(e);
- }
- }
- return writeResult;
+ final List<RaftPeer> newGroupPeers =
+ Utils.fromPeersAndPriorityToRaftPeers(peers, DEFAULT_PRIORITY);
+ final RaftGroup newGroup = RaftGroup.valueOf(raftGroupId, newGroupPeers);
+
+ sendReconfiguration(newGroup);
}
/** 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 d9c0aca85de..71a48b54bf4 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
@@ -242,8 +242,7 @@ class SimpleConsensus implements IConsensus {
}
@Override
- public TSStatus resetPeerList(ConsensusGroupId groupId, List<Peer> peers)
- throws ConsensusException {
+ public void resetPeerList(ConsensusGroupId groupId, List<Peer> peers) throws
ConsensusException {
throw new ConsensusException("SimpleConsensus does not support reset peer
list");
}