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

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


The following commit(s) were added to refs/heads/master by this push:
     new f40424422 RATIS-2051. Fix Sonar bugs for Ratis. (#1059)
f40424422 is described below

commit f40424422b692349b5496ee30e24335c8186093b
Author: Potato <[email protected]>
AuthorDate: Thu Apr 4 00:16:16 2024 +0800

    RATIS-2051. Fix Sonar bugs for Ratis. (#1059)
---
 ratis-common/src/main/java/org/apache/ratis/util/LeakDetector.java   | 1 +
 ratis-common/src/main/java/org/apache/ratis/util/PeerProxyMap.java   | 1 +
 ratis-common/src/main/java/org/apache/ratis/util/Preconditions.java  | 5 +++++
 .../src/main/java/org/apache/ratis/grpc/server/GrpcLogAppender.java  | 1 +
 .../main/java/org/apache/ratis/server/impl/ConfigurationManager.java | 2 ++
 .../src/main/java/org/apache/ratis/server/impl/FollowerState.java    | 1 +
 .../src/main/java/org/apache/ratis/server/impl/LeaderLease.java      | 2 +-
 .../src/main/java/org/apache/ratis/server/impl/LeaderStateImpl.java  | 3 ++-
 .../java/org/apache/ratis/server/impl/RaftConfigurationImpl.java     | 1 +
 .../src/main/java/org/apache/ratis/server/impl/ServerState.java      | 2 ++
 .../main/java/org/apache/ratis/server/impl/StateMachineUpdater.java  | 1 +
 .../java/org/apache/ratis/server/metrics/LeaderElectionMetrics.java  | 1 +
 .../java/org/apache/ratis/server/raftlog/segmented/LogSegment.java   | 4 ++--
 .../org/apache/ratis/server/raftlog/segmented/SegmentedRaftLog.java  | 1 +
 .../apache/ratis/server/raftlog/segmented/SegmentedRaftLogCache.java | 1 +
 .../ratis/server/raftlog/segmented/SegmentedRaftLogWorker.java       | 1 +
 .../org/apache/ratis/server/storage/RaftStorageDirectoryImpl.java    | 1 +
 .../main/java/org/apache/ratis/server/storage/SnapshotManager.java   | 1 +
 .../java/org/apache/ratis/statemachine/impl/BaseStateMachine.java    | 1 +
 .../org/apache/ratis/statemachine/impl/TransactionContextImpl.java   | 5 +++++
 20 files changed, 32 insertions(+), 4 deletions(-)

diff --git a/ratis-common/src/main/java/org/apache/ratis/util/LeakDetector.java 
b/ratis-common/src/main/java/org/apache/ratis/util/LeakDetector.java
index d80186816..82202f288 100644
--- a/ratis-common/src/main/java/org/apache/ratis/util/LeakDetector.java
+++ b/ratis-common/src/main/java/org/apache/ratis/util/LeakDetector.java
@@ -84,6 +84,7 @@ public class LeakDetector {
           tracker.reportLeak();
         }
       } catch (InterruptedException e) {
+        Thread.currentThread().interrupt();
         LOG.warn("Thread interrupted, exiting.", e);
         break;
       }
diff --git a/ratis-common/src/main/java/org/apache/ratis/util/PeerProxyMap.java 
b/ratis-common/src/main/java/org/apache/ratis/util/PeerProxyMap.java
index 0ce0595fa..eda41a009 100644
--- a/ratis-common/src/main/java/org/apache/ratis/util/PeerProxyMap.java
+++ b/ratis-common/src/main/java/org/apache/ratis/util/PeerProxyMap.java
@@ -45,6 +45,7 @@ public class PeerProxyMap<PROXY extends Closeable> implements 
RaftPeer.Add, Clos
   /** Peer and its proxy. */
   private class PeerAndProxy {
     private final RaftPeer peer;
+    @SuppressWarnings({"squid:S3077"}) // Suppress volatile for generic type
     private volatile PROXY proxy = null;
     private final LifeCycle lifeCycle;
 
diff --git 
a/ratis-common/src/main/java/org/apache/ratis/util/Preconditions.java 
b/ratis-common/src/main/java/org/apache/ratis/util/Preconditions.java
index c757de299..f37b250f5 100644
--- a/ratis-common/src/main/java/org/apache/ratis/util/Preconditions.java
+++ b/ratis-common/src/main/java/org/apache/ratis/util/Preconditions.java
@@ -78,6 +78,11 @@ public interface Preconditions {
     }
   }
 
+  static void assertSame(int expected, int computed, String name) {
+    assertTrue(expected == computed,
+        () -> name + ": expected == " + expected + " but computed == " + 
computed);
+  }
+
   static void assertSame(long expected, long computed, String name) {
     assertTrue(expected == computed,
         () -> name + ": expected == " + expected + " but computed == " + 
computed);
diff --git 
a/ratis-grpc/src/main/java/org/apache/ratis/grpc/server/GrpcLogAppender.java 
b/ratis-grpc/src/main/java/org/apache/ratis/grpc/server/GrpcLogAppender.java
index e23f2826e..351e7f109 100644
--- a/ratis-grpc/src/main/java/org/apache/ratis/grpc/server/GrpcLogAppender.java
+++ b/ratis-grpc/src/main/java/org/apache/ratis/grpc/server/GrpcLogAppender.java
@@ -156,6 +156,7 @@ public class GrpcLogAppender extends LogAppenderBase {
   private final TimeDuration logMessageBatchDuration;
   private final int maxOutstandingInstallSnapshots;
   private final TimeoutExecutor scheduler = TimeoutExecutor.getInstance();
+  @SuppressWarnings({"squid:S3077"}) // Suppress volatile for generic type
   private volatile StreamObservers appendLogRequestObserver;
   private final boolean useSeparateHBChannel;
 
diff --git 
a/ratis-server/src/main/java/org/apache/ratis/server/impl/ConfigurationManager.java
 
b/ratis-server/src/main/java/org/apache/ratis/server/impl/ConfigurationManager.java
index 0e020b7e3..10c59c8b1 100644
--- 
a/ratis-server/src/main/java/org/apache/ratis/server/impl/ConfigurationManager.java
+++ 
b/ratis-server/src/main/java/org/apache/ratis/server/impl/ConfigurationManager.java
@@ -41,8 +41,10 @@ public class ConfigurationManager {
    * The current raft configuration. If configurations is not empty, should be
    * the last entry of the map. Otherwise is initialConf.
    */
+  @SuppressWarnings({"squid:S3077"}) // Suppress volatile for generic type
   private volatile RaftConfigurationImpl currentConf;
   /** Cache the peer corresponding to {@link #id}. */
+  @SuppressWarnings({"squid:S3077"}) // Suppress volatile for generic type
   private volatile RaftPeer currentPeer;
 
   ConfigurationManager(RaftPeerId id, RaftConfigurationImpl initialConf) {
diff --git 
a/ratis-server/src/main/java/org/apache/ratis/server/impl/FollowerState.java 
b/ratis-server/src/main/java/org/apache/ratis/server/impl/FollowerState.java
index 3911e39a5..e980daede 100644
--- a/ratis-server/src/main/java/org/apache/ratis/server/impl/FollowerState.java
+++ b/ratis-server/src/main/java/org/apache/ratis/server/impl/FollowerState.java
@@ -59,6 +59,7 @@ class FollowerState extends Daemon {
   private final RaftServerImpl server;
 
   private final Timestamp creationTime = Timestamp.currentTime();
+  @SuppressWarnings({"squid:S3077"}) // Suppress volatile for generic type
   private volatile Timestamp lastRpcTime = creationTime;
   private volatile boolean isRunning = true;
   private final AtomicInteger outstandingOp = new AtomicInteger();
diff --git 
a/ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderLease.java 
b/ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderLease.java
index 315cc9f14..3b8f53da1 100644
--- a/ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderLease.java
+++ b/ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderLease.java
@@ -92,7 +92,7 @@ class LeaderLease {
       return Timestamp.currentTime();
     }
 
-    final int mid = followers.size() / 2;
+    final long mid = followers.size() / 2;
     return followers.stream()
         .map(FollowerInfo::getLastRespondedAppendEntriesSendTime)
         .sorted()
diff --git 
a/ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderStateImpl.java 
b/ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderStateImpl.java
index e8a4adc9c..28f2350c7 100644
--- 
a/ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderStateImpl.java
+++ 
b/ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderStateImpl.java
@@ -269,7 +269,7 @@ class LeaderStateImpl implements LeaderState {
 
   static class FollowerInfoMap {
     private final Map<RaftPeerId, FollowerInfo> map = new 
ConcurrentHashMap<>();
-
+    @SuppressWarnings({"squid:S3077"}) // Suppress volatile for generic type
     private volatile CurrentOldFollowerInfos followerInfos;
 
     void put(RaftPeerId id, FollowerInfo info) {
@@ -333,6 +333,7 @@ class LeaderStateImpl implements LeaderState {
   private final RaftServerImpl server;
   private final RaftLog raftLog;
   private final long currentTerm;
+  @SuppressWarnings({"squid:S3077"}) // Suppress volatile for generic type
   private volatile ConfigurationStagingState stagingState;
 
   private final FollowerInfoMap followerInfoMap = new FollowerInfoMap();
diff --git 
a/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftConfigurationImpl.java
 
b/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftConfigurationImpl.java
index 98f41020e..15a114cdb 100644
--- 
a/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftConfigurationImpl.java
+++ 
b/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftConfigurationImpl.java
@@ -148,6 +148,7 @@ final class RaftConfigurationImpl implements 
RaftConfiguration {
     return oldConf == null;
   }
 
+  @SuppressWarnings({"squid:S6466"}) // Suppress  
ArrayIndexOutOfBoundsException warning
   boolean containsInConf(RaftPeerId peerId, RaftPeerRole... roles) {
     if (roles == null || roles.length == 0) {
       return conf.contains(peerId);
diff --git 
a/ratis-server/src/main/java/org/apache/ratis/server/impl/ServerState.java 
b/ratis-server/src/main/java/org/apache/ratis/server/impl/ServerState.java
index af241a633..0f46c6b52 100644
--- a/ratis-server/src/main/java/org/apache/ratis/server/impl/ServerState.java
+++ b/ratis-server/src/main/java/org/apache/ratis/server/impl/ServerState.java
@@ -87,6 +87,7 @@ class ServerState {
   /**
    * Candidate that this peer granted vote for in current term (or null if 
none)
    */
+  @SuppressWarnings({"squid:S3077"}) // Suppress volatile for generic type
   private volatile RaftPeerId votedFor;
 
   /**
@@ -171,6 +172,7 @@ class ServerState {
     }
   }
 
+  @SuppressWarnings({"squid:S2095"}) // Suppress closeable  warning
   private static RaftLog initRaftLog(RaftGroupMemberId memberId, 
RaftServerImpl server, RaftStorage storage,
       Consumer<LogEntryProto> logConsumer, LongSupplier 
getSnapshotIndexFromStateMachine,
       RaftProperties prop) throws IOException {
diff --git 
a/ratis-server/src/main/java/org/apache/ratis/server/impl/StateMachineUpdater.java
 
b/ratis-server/src/main/java/org/apache/ratis/server/impl/StateMachineUpdater.java
index fbd7f013f..f13ee0d6d 100644
--- 
a/ratis-server/src/main/java/org/apache/ratis/server/impl/StateMachineUpdater.java
+++ 
b/ratis-server/src/main/java/org/apache/ratis/server/impl/StateMachineUpdater.java
@@ -199,6 +199,7 @@ class StateMachineUpdater implements Runnable {
         }
       } catch (Throwable t) {
         if (t instanceof InterruptedException && state == State.STOP) {
+          Thread.currentThread().interrupt();
           LOG.info("{} was interrupted.  Exiting ...", this);
         } else {
           state = State.EXCEPTION;
diff --git 
a/ratis-server/src/main/java/org/apache/ratis/server/metrics/LeaderElectionMetrics.java
 
b/ratis-server/src/main/java/org/apache/ratis/server/metrics/LeaderElectionMetrics.java
index 7447498d4..1d044bb6f 100644
--- 
a/ratis-server/src/main/java/org/apache/ratis/server/metrics/LeaderElectionMetrics.java
+++ 
b/ratis-server/src/main/java/org/apache/ratis/server/metrics/LeaderElectionMetrics.java
@@ -51,6 +51,7 @@ public final class LeaderElectionMetrics extends RatisMetrics 
{
 
   private final Timekeeper electionTime = 
getRegistry().timer(LEADER_ELECTION_TIME_TAKEN);
 
+  @SuppressWarnings({"squid:S3077"}) // Suppress volatile for generic type
   private volatile Timestamp lastElectionTime;
 
   private LeaderElectionMetrics(RaftGroupMemberId serverId, LongSupplier 
getLastLeaderElapsedTimeMs) {
diff --git 
a/ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/LogSegment.java
 
b/ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/LogSegment.java
index 2542e9998..f96e34e4c 100644
--- 
a/ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/LogSegment.java
+++ 
b/ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/LogSegment.java
@@ -294,10 +294,10 @@ public final class LogSegment {
   private volatile boolean isOpen;
   private long totalFileSize = SegmentedRaftLogFormat.getHeaderLength();
   /** Segment start index, inclusive. */
-  private long startIndex;
+  private final long startIndex;
   /** Segment end index, inclusive. */
   private volatile long endIndex;
-  private RaftStorage storage;
+  private final RaftStorage storage;
   private final SizeInBytes maxOpSize;
   private final LogEntryLoader cacheLoader;
   /** later replace it with a metric */
diff --git 
a/ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/SegmentedRaftLog.java
 
b/ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/SegmentedRaftLog.java
index a0f7c1e72..8d972a1e2 100644
--- 
a/ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/SegmentedRaftLog.java
+++ 
b/ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/SegmentedRaftLog.java
@@ -211,6 +211,7 @@ public final class SegmentedRaftLog extends RaftLogBase {
   private final boolean stateMachineCachingEnabled;
   private final SegmentedRaftLogMetrics metrics;
 
+  @SuppressWarnings({"squid:S2095"}) // Suppress closeable  warning
   private SegmentedRaftLog(Builder b) {
     super(b.memberId, b.snapshotIndexSupplier, b.properties);
     this.metrics = new SegmentedRaftLogMetrics(b.memberId);
diff --git 
a/ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/SegmentedRaftLogCache.java
 
b/ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/SegmentedRaftLogCache.java
index d22248201..ad1633232 100644
--- 
a/ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/SegmentedRaftLogCache.java
+++ 
b/ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/SegmentedRaftLogCache.java
@@ -396,6 +396,7 @@ public class SegmentedRaftLogCache {
   }
 
   private final String name;
+  @SuppressWarnings({"squid:S3077"}) // Suppress volatile for generic type
   private volatile LogSegment openSegment;
   private final LogSegmentList closedSegments;
   private final RaftStorage storage;
diff --git 
a/ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/SegmentedRaftLogWorker.java
 
b/ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/SegmentedRaftLogWorker.java
index 02506079f..7dc87faf7 100644
--- 
a/ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/SegmentedRaftLogWorker.java
+++ 
b/ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/SegmentedRaftLogWorker.java
@@ -143,6 +143,7 @@ class SegmentedRaftLogWorker {
   private volatile boolean running = true;
   private final ExecutorService workerThreadExecutor;
   private final RaftStorage storage;
+  @SuppressWarnings({"squid:S3077"}) // Suppress volatile for generic type
   private volatile SegmentedRaftLogOutputStream out;
   private final Runnable submitUpdateCommitEvent;
   private final StateMachine stateMachine;
diff --git 
a/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftStorageDirectoryImpl.java
 
b/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftStorageDirectoryImpl.java
index e7f69d1e2..119f7922d 100644
--- 
a/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftStorageDirectoryImpl.java
+++ 
b/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftStorageDirectoryImpl.java
@@ -210,6 +210,7 @@ class RaftStorageDirectoryImpl implements 
RaftStorageDirectory {
    * <code>null</code> if storage is already locked.
    * @throws IOException if locking fails.
    */
+  @SuppressWarnings({"squid:S2095"}) // Suppress closeable  warning
   private FileLock tryLock(File lockF) throws IOException {
     boolean deletionHookAdded = false;
     if (!lockF.exists()) {
diff --git 
a/ratis-server/src/main/java/org/apache/ratis/server/storage/SnapshotManager.java
 
b/ratis-server/src/main/java/org/apache/ratis/server/storage/SnapshotManager.java
index c49a86ec5..794604d66 100644
--- 
a/ratis-server/src/main/java/org/apache/ratis/server/storage/SnapshotManager.java
+++ 
b/ratis-server/src/main/java/org/apache/ratis/server/storage/SnapshotManager.java
@@ -77,6 +77,7 @@ public class SnapshotManager {
         new File(dir.get().getRoot(), c.getFilename()).toPath()).toString();
   }
 
+  @SuppressWarnings({"squid:S2095"}) // Suppress closeable  warning
   private FileChannel open(FileChunkProto chunk, File tmpSnapshotFile) throws 
IOException {
     final FileChannel out;
     final boolean exists = tmpSnapshotFile.exists();
diff --git 
a/ratis-server/src/main/java/org/apache/ratis/statemachine/impl/BaseStateMachine.java
 
b/ratis-server/src/main/java/org/apache/ratis/statemachine/impl/BaseStateMachine.java
index 98f270d2d..97cc3e638 100644
--- 
a/ratis-server/src/main/java/org/apache/ratis/statemachine/impl/BaseStateMachine.java
+++ 
b/ratis-server/src/main/java/org/apache/ratis/statemachine/impl/BaseStateMachine.java
@@ -50,6 +50,7 @@ import java.util.concurrent.atomic.AtomicReference;
 public class BaseStateMachine implements StateMachine, StateMachine.DataApi,
     StateMachine.EventApi, StateMachine.LeaderEventApi, 
StateMachine.FollowerEventApi {
   private final CompletableFuture<RaftServer> server = new 
CompletableFuture<>();
+  @SuppressWarnings({"squid:S3077"}) // Suppress volatile for generic type
   private volatile RaftGroupId groupId;
   private final LifeCycle lifeCycle = new 
LifeCycle(JavaUtils.getClassSimpleName(getClass()));
 
diff --git 
a/ratis-server/src/main/java/org/apache/ratis/statemachine/impl/TransactionContextImpl.java
 
b/ratis-server/src/main/java/org/apache/ratis/statemachine/impl/TransactionContextImpl.java
index 44bd32c67..e582fb9e1 100644
--- 
a/ratis-server/src/main/java/org/apache/ratis/statemachine/impl/TransactionContextImpl.java
+++ 
b/ratis-server/src/main/java/org/apache/ratis/statemachine/impl/TransactionContextImpl.java
@@ -49,6 +49,7 @@ public class TransactionContextImpl implements 
TransactionContext {
   private final RaftClientRequest clientRequest;
 
   /** Exception from the {@link StateMachine} or from the log */
+  @SuppressWarnings({"squid:S3077"}) // Suppress volatile for generic type
   private volatile Exception exception;
 
   /** Data from the {@link StateMachine} */
@@ -60,6 +61,7 @@ public class TransactionContextImpl implements 
TransactionContext {
    * {@link StateMachine#startTransaction(RaftClientRequest)} and
    * {@link StateMachine#applyTransaction(TransactionContext)}.
    */
+  @SuppressWarnings({"squid:S3077"}) // Suppress volatile for generic type
   private volatile Object stateMachineContext;
 
   /**
@@ -70,11 +72,14 @@ public class TransactionContextImpl implements 
TransactionContext {
   private boolean shouldCommit = true;
 
   /** Committed LogEntry. */
+  @SuppressWarnings({"squid:S3077"}) // Suppress volatile for generic type
   private volatile LogEntryProto logEntry;
   /** Committed LogEntry copy. */
+  @SuppressWarnings({"squid:S3077"}) // Suppress volatile for generic type
   private volatile Supplier<LogEntryProto> logEntryCopy;
 
   /** For wrapping {@link #logEntry} in order to release the underlying 
buffer. */
+  @SuppressWarnings({"squid:S3077"}) // Suppress volatile for generic type
   private volatile ReferenceCountedObject<?> delegatedRef;
 
   private final CompletableFuture<Long> logIndexFuture = new 
CompletableFuture<>();

Reply via email to