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 2022/12/29 13:56:48 UTC

[iotdb] branch master updated: fix some code smells (#8660)

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 fc8ecd0769 fix some code smells (#8660)
fc8ecd0769 is described below

commit fc8ecd0769ce9ba02409f607ab425ba751aac7f0
Author: BUAAserein <65...@users.noreply.github.com>
AuthorDate: Thu Dec 29 21:56:42 2022 +0800

    fix some code smells (#8660)
---
 .../main/java/org/apache/iotdb/consensus/common/Peer.java   |  4 ++--
 .../iotdb/consensus/common/response/ConsensusResponse.java  |  2 +-
 .../iotdb/consensus/iot/logdispatcher/SyncStatus.java       |  2 +-
 .../iotdb/consensus/ratis/ApplicationStateMachineProxy.java |  2 +-
 .../java/org/apache/iotdb/consensus/ratis/RatisClient.java  | 13 +++++--------
 .../org/apache/iotdb/consensus/ratis/RatisConsensus.java    | 10 ++++------
 .../org/apache/iotdb/consensus/ratis/SnapshotStorage.java   |  2 +-
 .../main/java/org/apache/iotdb/consensus/ratis/Utils.java   |  8 ++++----
 8 files changed, 19 insertions(+), 24 deletions(-)

diff --git a/consensus/src/main/java/org/apache/iotdb/consensus/common/Peer.java b/consensus/src/main/java/org/apache/iotdb/consensus/common/Peer.java
index 8438e90e30..b6b67fc84b 100644
--- a/consensus/src/main/java/org/apache/iotdb/consensus/common/Peer.java
+++ b/consensus/src/main/java/org/apache/iotdb/consensus/common/Peer.java
@@ -35,7 +35,7 @@ import java.util.Objects;
 // TODO Use a mature IDL framework such as Protobuf to manage this structure
 public class Peer {
 
-  private final Logger LOGGER = LoggerFactory.getLogger(Peer.class);
+  private final Logger logger = LoggerFactory.getLogger(Peer.class);
   private final ConsensusGroupId groupId;
   private final TEndPoint endpoint;
   private final int nodeId;
@@ -65,7 +65,7 @@ public class Peer {
       BasicStructureSerDeUtil.write(nodeId, stream);
       ThriftCommonsSerDeUtils.serializeTEndPoint(endpoint, stream);
     } catch (IOException e) {
-      LOGGER.error("Failed to serialize Peer", e);
+      logger.error("Failed to serialize Peer", e);
     }
   }
 
diff --git a/consensus/src/main/java/org/apache/iotdb/consensus/common/response/ConsensusResponse.java b/consensus/src/main/java/org/apache/iotdb/consensus/common/response/ConsensusResponse.java
index 2a14ada29d..4b4beff769 100644
--- a/consensus/src/main/java/org/apache/iotdb/consensus/common/response/ConsensusResponse.java
+++ b/consensus/src/main/java/org/apache/iotdb/consensus/common/response/ConsensusResponse.java
@@ -24,7 +24,7 @@ import org.apache.iotdb.consensus.exception.ConsensusException;
 public abstract class ConsensusResponse {
   protected final ConsensusException exception;
 
-  public ConsensusResponse(ConsensusException exception) {
+  protected ConsensusResponse(ConsensusException exception) {
     this.exception = exception;
   }
 
diff --git a/consensus/src/main/java/org/apache/iotdb/consensus/iot/logdispatcher/SyncStatus.java b/consensus/src/main/java/org/apache/iotdb/consensus/iot/logdispatcher/SyncStatus.java
index 14181f5053..891961d70b 100644
--- a/consensus/src/main/java/org/apache/iotdb/consensus/iot/logdispatcher/SyncStatus.java
+++ b/consensus/src/main/java/org/apache/iotdb/consensus/iot/logdispatcher/SyncStatus.java
@@ -58,7 +58,7 @@ public class SyncStatus {
   public void removeBatch(Batch batch) {
     synchronized (this) {
       batch.setSynced(true);
-      if (pendingBatches.size() > 0 && pendingBatches.get(0).equals(batch)) {
+      if (!pendingBatches.isEmpty() && pendingBatches.get(0).equals(batch)) {
         Iterator<Batch> iterator = pendingBatches.iterator();
         Batch current = iterator.next();
         while (current.isSynced()) {
diff --git a/consensus/src/main/java/org/apache/iotdb/consensus/ratis/ApplicationStateMachineProxy.java b/consensus/src/main/java/org/apache/iotdb/consensus/ratis/ApplicationStateMachineProxy.java
index 3eea453b3a..c262996089 100644
--- a/consensus/src/main/java/org/apache/iotdb/consensus/ratis/ApplicationStateMachineProxy.java
+++ b/consensus/src/main/java/org/apache/iotdb/consensus/ratis/ApplicationStateMachineProxy.java
@@ -242,7 +242,7 @@ public class ApplicationStateMachineProxy extends BaseStateMachine {
     // statemachine is supposed to clear snapshotDir on failure
     boolean isEmpty = snapshotDir.delete();
     if (!isEmpty) {
-      logger.info("Snapshot directory is incomplete, deleting " + snapshotDir.getAbsolutePath());
+      logger.info("Snapshot directory is incomplete, deleting {}", snapshotDir.getAbsolutePath());
       FileUtils.deleteFully(snapshotDir);
     }
   }
diff --git a/consensus/src/main/java/org/apache/iotdb/consensus/ratis/RatisClient.java b/consensus/src/main/java/org/apache/iotdb/consensus/ratis/RatisClient.java
index d9884f57d6..7f0b11d117 100644
--- a/consensus/src/main/java/org/apache/iotdb/consensus/ratis/RatisClient.java
+++ b/consensus/src/main/java/org/apache/iotdb/consensus/ratis/RatisClient.java
@@ -128,7 +128,7 @@ public class RatisClient {
   private static class RatisRetryPolicy implements RetryPolicy {
 
     private static final Logger logger = LoggerFactory.getLogger(RatisClient.class);
-    private static final int maxAttempts = 10;
+    private static final int MAX_ATTEMPTS = 10;
     RetryPolicy defaultPolicy;
 
     public RatisRetryPolicy(RatisConfig.Impl config) {
@@ -147,13 +147,10 @@ public class RatisClient {
     @Override
     public Action handleAttemptFailure(Event event) {
 
-      if (event.getCause() != null) {
-        if (event.getCause() instanceof IOException
-            && !(event.getCause() instanceof RaftException)) {
-          // unexpected. may be caused by statemachine.
-          logger.debug("raft client request failed and caught exception: ", event.getCause());
-          return NO_RETRY_ACTION;
-        }
+      if (event.getCause() instanceof IOException && !(event.getCause() instanceof RaftException)) {
+        // unexpected. may be caused by statemachine.
+        logger.debug("raft client request failed and caught exception: ", event.getCause());
+        return NO_RETRY_ACTION;
       }
 
       return defaultPolicy.handleAttemptFailure(event);
diff --git a/consensus/src/main/java/org/apache/iotdb/consensus/ratis/RatisConsensus.java b/consensus/src/main/java/org/apache/iotdb/consensus/ratis/RatisConsensus.java
index e00374c347..4fd5c46e95 100644
--- a/consensus/src/main/java/org/apache/iotdb/consensus/ratis/RatisConsensus.java
+++ b/consensus/src/main/java/org/apache/iotdb/consensus/ratis/RatisConsensus.java
@@ -183,9 +183,7 @@ class RatisConsensus implements IConsensus {
 
   private boolean shouldRetry(RaftClientReply reply) {
     // currently, we only retry when ResourceUnavailableException is caught
-    return !reply.isSuccess()
-        && (reply.getException() != null
-            && reply.getException() instanceof ResourceUnavailableException);
+    return !reply.isSuccess() && (reply.getException() instanceof ResourceUnavailableException);
   }
   /** launch a consensus write with retry mechanism */
   private RaftClientReply writeWithRetry(CheckedSupplier<RaftClientReply, IOException> caller)
@@ -348,7 +346,7 @@ class RatisConsensus implements IConsensus {
     RaftClientReply reply;
     RatisClient client = null;
     try {
-      if (group.getPeers().size() == 0) {
+      if (group.getPeers().isEmpty()) {
         client = getRaftClient(RaftGroup.valueOf(group.getGroupId(), server));
       } else {
         client = getRaftClient(group);
@@ -708,9 +706,9 @@ class RatisConsensus implements IConsensus {
         ConsensusGenericResponse consensusGenericResponse =
             triggerSnapshot(Utils.fromRaftGroupIdToConsensusGroupId(raftGroupId));
         if (consensusGenericResponse.isSuccess()) {
-          logger.info("Raft group " + raftGroupId + " took snapshot successfully");
+          logger.info("Raft group {} took snapshot successfully", raftGroupId);
         } else {
-          logger.warn("Raft group " + raftGroupId + " failed to take snapshot");
+          logger.warn("Raft group {} failed to take snapshot", raftGroupId);
         }
       }
     }
diff --git a/consensus/src/main/java/org/apache/iotdb/consensus/ratis/SnapshotStorage.java b/consensus/src/main/java/org/apache/iotdb/consensus/ratis/SnapshotStorage.java
index 55ece57848..0bbd970b84 100644
--- a/consensus/src/main/java/org/apache/iotdb/consensus/ratis/SnapshotStorage.java
+++ b/consensus/src/main/java/org/apache/iotdb/consensus/ratis/SnapshotStorage.java
@@ -47,7 +47,7 @@ public class SnapshotStorage implements StateMachineStorage {
   private final Logger logger = LoggerFactory.getLogger(SnapshotStorage.class);
   private final IStateMachine applicationStateMachine;
 
-  private final String TMP_PREFIX = ".tmp.";
+  private static final String TMP_PREFIX = ".tmp.";
   private File stateMachineDir;
   private final RaftGroupId groupId;
 
diff --git a/consensus/src/main/java/org/apache/iotdb/consensus/ratis/Utils.java b/consensus/src/main/java/org/apache/iotdb/consensus/ratis/Utils.java
index 1010367978..941dfc76f8 100644
--- a/consensus/src/main/java/org/apache/iotdb/consensus/ratis/Utils.java
+++ b/consensus/src/main/java/org/apache/iotdb/consensus/ratis/Utils.java
@@ -45,12 +45,12 @@ import java.util.List;
 import java.util.stream.Collectors;
 
 public class Utils {
-  private static final int tempBufferSize = 1024;
+  private static final int TEMP_BUFFER_SIZE = 1024;
   private static final byte PADDING_MAGIC = 0x47;
 
   private Utils() {}
 
-  public static String HostAddress(TEndPoint endpoint) {
+  public static String hostAddress(TEndPoint endpoint) {
     return String.format("%s:%d", endpoint.getIp(), endpoint.getPort());
   }
 
@@ -90,7 +90,7 @@ public class Utils {
       int nodeId, TEndPoint endpoint, int priority) {
     return RaftPeer.newBuilder()
         .setId(fromNodeIdToRaftPeerId(nodeId))
-        .setAddress(HostAddress(endpoint))
+        .setAddress(hostAddress(endpoint))
         .setPriority(priority)
         .build();
   }
@@ -147,7 +147,7 @@ public class Utils {
 
   public static ByteBuffer serializeTSStatus(TSStatus status) throws TException {
     AutoScalingBufferWriteTransport byteBuffer =
-        new AutoScalingBufferWriteTransport(tempBufferSize);
+        new AutoScalingBufferWriteTransport(TEMP_BUFFER_SIZE);
     TCompactProtocol protocol = new TCompactProtocol(byteBuffer);
     status.write(protocol);
     return ByteBuffer.wrap(byteBuffer.getBuffer());