You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ta...@apache.org on 2023/07/06 04:32:14 UTC

[iotdb] branch master updated: [IOTDB-6048] Refactor RatisClient related code (#10449)

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 3db2d2a6457 [IOTDB-6048] Refactor RatisClient related code (#10449)
3db2d2a6457 is described below

commit 3db2d2a64575947293a317702521eb4896f83111
Author: William Song <48...@users.noreply.github.com>
AuthorDate: Thu Jul 6 12:32:08 2023 +0800

    [IOTDB-6048] Refactor RatisClient related code (#10449)
---
 .../apache/iotdb/consensus/ratis/RatisClient.java  | 11 +++--
 .../iotdb/consensus/ratis/RatisConsensus.java      | 52 +++++-----------------
 2 files changed, 18 insertions(+), 45 deletions(-)

diff --git a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/RatisClient.java b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/RatisClient.java
index 454c6c6d3a3..21f3f2881a2 100644
--- a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/RatisClient.java
+++ b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/RatisClient.java
@@ -41,7 +41,7 @@ import java.io.IOException;
 import java.util.Optional;
 import java.util.concurrent.TimeUnit;
 
-class RatisClient {
+class RatisClient implements AutoCloseable {
 
   private final Logger logger = LoggerFactory.getLogger(RatisClient.class);
   private final RaftGroup serveGroup;
@@ -61,7 +61,7 @@ class RatisClient {
     return raftClient;
   }
 
-  private void close() {
+  private void invalidate() {
     try {
       raftClient.close();
     } catch (IOException e) {
@@ -69,6 +69,11 @@ class RatisClient {
     }
   }
 
+  @Override
+  public void close() throws Exception {
+    returnSelf();
+  }
+
   void returnSelf() {
     clientManager.returnClient(serveGroup, this);
   }
@@ -92,7 +97,7 @@ class RatisClient {
 
     @Override
     public void destroyObject(RaftGroup key, PooledObject<RatisClient> pooledObject) {
-      pooledObject.getObject().close();
+      pooledObject.getObject().invalidate();
     }
 
     @Override
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 94121348bb8..44d3b935136 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
@@ -294,10 +294,9 @@ class RatisConsensus implements IConsensus {
 
     // 2. try raft client
     TSStatus writeResult;
-    RatisClient client = null;
     try (AutoCloseable ignored =
-        RatisMetricsManager.getInstance().startWriteRemotelyTimer(consensusGroupType)) {
-      client = getRaftClient(raftGroup);
+            RatisMetricsManager.getInstance().startWriteRemotelyTimer(consensusGroupType);
+        RatisClient client = getRaftClient(raftGroup)) {
       RaftClientReply reply = writeRemotelyWithRetry(client, message);
       if (!reply.isSuccess()) {
         return failedWrite(new RatisRequestFailedException(reply.getException()));
@@ -305,10 +304,6 @@ class RatisConsensus implements IConsensus {
       writeResult = Utils.deserializeFrom(reply.getMessage().getContent().asReadOnlyByteBuffer());
     } catch (Exception e) {
       return failedWrite(new RatisRequestFailedException(e));
-    } finally {
-      if (client != null) {
-        client.returnSelf();
-      }
     }
 
     if (suggestedLeader != null) {
@@ -364,23 +359,15 @@ class RatisConsensus implements IConsensus {
 
   private ConsensusGenericResponse addNewGroupToServer(RaftGroup group, RaftPeer server) {
     RaftClientReply reply;
-    RatisClient client = null;
-    try {
-      if (group.getPeers().isEmpty()) {
-        client = getRaftClient(RaftGroup.valueOf(group.getGroupId(), server));
-      } else {
-        client = getRaftClient(group);
-      }
+    RaftGroup clientGroup =
+        group.getPeers().isEmpty() ? RaftGroup.valueOf(group.getGroupId(), server) : group;
+    try (RatisClient client = getRaftClient(clientGroup)) {
       reply = client.getRaftClient().getGroupManagementApi(server.getId()).add(group);
       if (!reply.isSuccess()) {
         return failed(new RatisRequestFailedException(reply.getException()));
       }
     } catch (Exception e) {
       return failed(new RatisRequestFailedException(e));
-    } finally {
-      if (client != null) {
-        client.returnSelf();
-      }
     }
     return ConsensusGenericResponse.newBuilder().setSuccess(reply.isSuccess()).build();
   }
@@ -555,9 +542,7 @@ class RatisConsensus implements IConsensus {
     }
 
     RaftClientReply reply;
-    RatisClient client = null;
-    try {
-      client = getRaftClient(raftGroup);
+    try (RatisClient client = getRaftClient(raftGroup)) {
       RaftClientReply configChangeReply =
           client.getRaftClient().admin().setConfiguration(newConfiguration);
       if (!configChangeReply.isSuccess()) {
@@ -570,34 +555,23 @@ class RatisConsensus implements IConsensus {
       }
     } catch (Exception e) {
       return failed(new RatisRequestFailedException(e));
-    } finally {
-      if (client != null) {
-        client.returnSelf();
-      }
     }
     return ConsensusGenericResponse.newBuilder().setSuccess(reply.isSuccess()).build();
   }
 
-  private void forceStepDownLeader(RaftGroup group) throws ClientManagerException, IOException {
+  private void forceStepDownLeader(RaftGroup group) throws Exception {
     // when newLeaderPeerId == null, ratis forces current leader to step down and raise new
     // election
     transferLeader(group, null);
   }
 
-  private RaftClientReply transferLeader(RaftGroup group, RaftPeer newLeader)
-      throws ClientManagerException, IOException {
-    RatisClient client = null;
-    try {
-      client = getRaftClient(group);
+  private RaftClientReply transferLeader(RaftGroup group, RaftPeer newLeader) throws Exception {
+    try (RatisClient client = getRaftClient(group)) {
       // TODO tuning for timeoutMs
       return client
           .getRaftClient()
           .admin()
           .transferLeadership(newLeader != null ? newLeader.getId() : null, 10000);
-    } finally {
-      if (client != null) {
-        client.returnSelf();
-      }
     }
   }
 
@@ -810,9 +784,7 @@ class RatisConsensus implements IConsensus {
       throws RatisRequestFailedException {
     // notify the group leader of configuration change
     RaftClientReply reply;
-    RatisClient client = null;
-    try {
-      client = getRaftClient(newGroupConf);
+    try (RatisClient client = getRaftClient(newGroupConf)) {
       reply =
           client.getRaftClient().admin().setConfiguration(new ArrayList<>(newGroupConf.getPeers()));
       if (!reply.isSuccess()) {
@@ -820,10 +792,6 @@ class RatisConsensus implements IConsensus {
       }
     } catch (Exception e) {
       throw new RatisRequestFailedException(e);
-    } finally {
-      if (client != null) {
-        client.returnSelf();
-      }
     }
     return reply;
   }