You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ratis.apache.org by ta...@apache.org on 2024/10/09 09:20:01 UTC

(ratis) branch snapshot-3 updated (964e3caf2 -> 536419c50)

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

tanxinyu pushed a change to branch snapshot-3
in repository https://gitbox.apache.org/repos/asf/ratis.git


 discard 964e3caf2 release 3.1.1-0133c90-SNAPSHOT
 discard 0133c90a7 RATIS-2140. Thread wait when installing snapshot. (#1137)
 discard 5f5f34c35 RATIS-2144. SegmentedRaftLogWorker should close the stream before releasing the buffer. (#1138)
 discard cbdfe6314 RATIS-2138 Remove uncessary error log in JVMPauseMonitor When GC is serious (#1133)
 discard 303da68d9 RATIS-2135. The leader keeps sending inconsistent entries repeatedly to followers. (#1132)
 discard 40cfa13ab RATIS-2136 Fixed the issue that RatisServer could not start after frequent restarts (#1131)
    omit 89062974c Change version for the version 3.1.0 -rc2
     add 7159c5943 ./mvnw versions:set -DnewVersion=3.1.1-SNAPSHOT
     add ce6838a5d RATIS-2095. Extract common logic of ratis-shell to RaftUtils for reuse (#1098)
     add f2ab7d51d RATIS-2110. Publish SBOM artifacts (#1110)
     add 54e6fee7c RATIS-2117. No need for manual assembly:single execution (#1115)
     add 8383845dd RATIS-2118. Improve assembly process (#1117)
     add 76fb1a316 RATIS-2121. Set javac --release when compiling with JDK 9+ (#1119)
     add 6390a28bd RATIS-2116. Fix the issue where RaftServerImpl.appendEntries may be blocked indefinitely (#1116)
     add cfb95777f RATIS-1840. Avoid including build timestamp in artifacts (#1114)
     add 054cfb707 RATIS-2125. javadoc jars should not include non-doc items (#1123)
     add 53b80c6dc RATIS-1071. NettyClientRpc supports sendRequestAsync. Contributed by Rui Wang and Tsz-Wo Nicholas Sze. (#1122)
     add 6a6caa270 RATIS-2131. Surround with [] only if hostName is a IPv6 string (#1125)
     add 443436455 RATIS-2126. Optimize NotLeaderException message (#1124)
     add 1ff10882b RATIS-2133. Ignore http proxy for ratis grpc client & server (#1128)
     add bd154742c RATIS-2104. Double shutdown in TestLeaderInstallSnapshot. (#1108)
     add 49f1247a0 RATIS-2134. `logMetadata` could miss appending the `metadataEntry` with the lastCommitIndex (#1130)
     add 5946796d5 RATIS-2136 Fixed the issue that RatisServer could not start after frequent restarts (#1131)
     add 58327e26c RATIS-2115. Fix flaky RetryCacheTests and InstallSnapshotFromLeaderTests. (#1129)
     add a78e6e2df RATIS-2135. The leader keeps sending inconsistent entries repeatedly to followers. (#1132)
     add d3fa63b5e RATIS-2138 Remove uncessary error log in JVMPauseMonitor When GC is serious (#1133)
     add 94692f3cb RATIS-2139. Fix checkstyle:ParameterNumber in GrpcService. (#1134)
     add 099d23f25 RATIS-2137. Fix LogAppenderDefault in handling of INCONSISTENCY. (#1136)
     add 557675f12 RATIS-2144. SegmentedRaftLogWorker should close the stream before releasing the buffer. (#1138)
     add 2e7cb458c RATIS-2140. Thread wait when installing snapshot. (#1137)
     add fc89b0405 RATIS-2113. Use consistent method names and parameter types in RaftUtils. (#1135)
     add cdfd4f10d Revert "RATIS-1983. Refactor client request processing to support reference count. (#998)"
     add 6aa7349e9 Revert "RATIS-2101. Move TermIndex.PRIVATE_CACHE to Util.CACHE (#1103)"
     add d621c14ef Revert "RATIS-2099. Cache TermIndexImpl instead of using anonymous class (#1100)"
     add 5b8a74fc2 RATIS-2145. Follower hangs until the next trigger to take a snapshot. (#1139)
     add 211278e56 RATIS-2146. Fixed possible issues caused by concurrent deletion and election when member changes (#1140)
     add fc61d9650 Change version for the version 3.1.1 -rc2
     add 342688de9 RATIS-2153. ratis-version.properties missing from src bundle (#1147)
     add 1e10b7186 RATIS-2149.Do not perform leader election if the current RaftServer has not started yet (#1143)
     add 54a991623 RATIS-2148. Snapshot transfer may cause followers to trigger reloadStateMachine incorrectly (#1145)
     add 5578be7fb RATIS-2154. The old leader may send appendEntries after term changed. (#1148)
     add f23f9b7c1 RATIS-2152. GrpcLogAppender stucks while sending an installSnapshot notification request (#1146)
     add 356ad8a27 RATIS-2150. No need for manual assembly:single execution when mvn deploy #1144
     add 5a470a2b2 RATIS-2147. Md5 mismatch when snapshot install (#1142)
     add 313c51a13 RATIS-2157 Enhance make_rc.sh for non-first rc at release time #1149
     add fd260d8fd RATIS-2155. Add a builder for RatisShell. (#1150)
     add d7709da87 RATIS-2158. Let the snapshot sender and receiver use a new digester each time (#1151)
     add 45a30d890 Change version for the version 3.1.1 -rc3
     new 3021c0244 RATIS-2160. MetricRegistriesLoader should not print INFO message in CLI. (#1152)
     new d8482f1f0 RATIS-2162. When closing leaderState, if the logAppender thread sends a snapshot, a deadlock may occur. (#1154)
     new 5d1a5ea93 RATIS-2165. Do not include RaftClientRequest Message in ReadIndexRequest (#1160)
     new 536419c50 RATIS-2166. Update nextChunkIndex when snapshot is ALREADY_INSTALLED (#1159)

This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version.  This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:

 * -- * -- B -- O -- O -- O   (964e3caf2)
            \
             N -- N -- N   refs/heads/snapshot-3 (536419c50)

You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.

Any revisions marked "omit" are not gone; other references still
refer to them.  Any revisions marked "discard" are gone forever.

The 4 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .github/workflows/post-commit.yml                  |  14 +-
 dev-support/checks/{build.sh => repro.sh}          |   2 +-
 dev-support/make_rc.sh                             |  14 +-
 pom.xml                                            | 104 ++++++---
 ratis-assembly/pom.xml                             |  13 +-
 ratis-assembly/src/main/assembly/bin-pkg.xml       |  43 ----
 ratis-assembly/src/main/assembly/bin.xml           |  67 +++++-
 ratis-assembly/src/main/assembly/examples-bin.xml  |  73 ------
 ratis-assembly/src/main/assembly/shell-bin.xml     |  61 -----
 ratis-assembly/src/main/assembly/src.xml           |   2 +
 ratis-client/pom.xml                               |   2 +-
 .../apache/ratis/client/impl/ClientProtoUtils.java |   6 +-
 ratis-common/pom.xml                               |   2 +-
 .../src/main/java/org/apache/ratis/io/MD5Hash.java |   8 +-
 .../protocol/RaftClientAsynchronousProtocol.java   |  34 +--
 .../protocol/exceptions/NotLeaderException.java    |   3 +-
 .../org/apache/ratis/util/DataBlockingQueue.java   |   7 +
 .../main/java/org/apache/ratis/util/DataQueue.java |   5 +
 .../main/java/org/apache/ratis/util/NetUtils.java  |  10 +-
 .../apache/ratis/util/ReferenceCountedObject.java  |  24 --
 ratis-docs/pom.xml                                 |   2 +-
 ratis-examples/pom.xml                             |   2 +-
 .../examples/filestore/FileStoreStateMachine.java  |  18 +-
 ratis-experiments/pom.xml                          |   2 +-
 ratis-grpc/pom.xml                                 |   2 +-
 .../grpc/client/GrpcClientProtocolClient.java      |   3 +
 .../apache/ratis/grpc/server/GrpcLogAppender.java  |  40 ++--
 .../grpc/server/GrpcServerProtocolClient.java      |   2 +
 .../org/apache/ratis/grpc/server/GrpcService.java  | 257 ++++++++++++---------
 .../apache/ratis/grpc/MiniRaftClusterWithGrpc.java |   6 +-
 ratis-metrics-api/pom.xml                          |   2 +-
 .../ratis/metrics/MetricRegistriesLoader.java      |  37 ++-
 ratis-metrics-default/pom.xml                      |   2 +-
 .../ratis/metrics/TestMetricRegistriesLoader.java  |   6 +
 ratis-metrics-dropwizard3/pom.xml                  |   2 +-
 ratis-netty/pom.xml                                |   2 +-
 .../java/org/apache/ratis/netty/NettyRpcProxy.java |   6 +
 .../apache/ratis/netty/client/NettyClientRpc.java  |  67 ++++--
 ratis-proto/pom.xml                                |   2 +-
 ratis-replicated-map/pom.xml                       |   2 +-
 ratis-resource-bundle/pom.xml                      |   2 +-
 ratis-server-api/pom.xml                           |   2 +-
 .../apache/ratis/server/protocol/TermIndex.java    |  18 +-
 .../ratis/statemachine/TransactionContext.java     |   8 -
 ratis-server/pom.xml                               |   2 +-
 .../ratis/server/impl/ConfigurationManager.java    |  10 +-
 .../apache/ratis/server/impl/FollowerState.java    |  14 +-
 .../apache/ratis/server/impl/LeaderElection.java   |  18 +-
 .../apache/ratis/server/impl/LeaderStateImpl.java  |  24 +-
 .../apache/ratis/server/impl/RaftServerImpl.java   | 132 +++++------
 .../apache/ratis/server/impl/RaftServerProxy.java  |  13 +-
 .../org/apache/ratis/server/impl/RoleInfo.java     |  26 +--
 .../apache/ratis/server/impl/ServerProtoUtils.java |   2 +-
 .../server/impl/SnapshotInstallationHandler.java   |  14 ++
 .../ratis/server/leader/LogAppenderBase.java       |   2 +-
 .../ratis/server/leader/LogAppenderDefault.java    |  23 +-
 .../apache/ratis/server/raftlog/RaftLogBase.java   |  17 +-
 .../ratis/server/raftlog/segmented/LogSegment.java | 132 ++++-------
 .../server/raftlog/segmented/SegmentedRaftLog.java |  19 +-
 .../raftlog/segmented/SegmentedRaftLogCache.java   |   5 +-
 .../raftlog/segmented/SegmentedRaftLogWorker.java  |  14 +-
 .../ratis/server/storage/FileChunkReader.java      |   2 +-
 .../ratis/server/storage/SnapshotManager.java      |  10 +-
 .../statemachine/impl/TransactionContextImpl.java  |  18 --
 .../ratis/InstallSnapshotFromLeaderTests.java      | 165 +++++++------
 .../java/org/apache/ratis/RetryCacheTests.java     |   6 +-
 ratis-shell/pom.xml                                |   2 +-
 .../java/org/apache/ratis/shell/cli/CliUtils.java  | 177 ++++++++++++++
 .../java/org/apache/ratis/shell/cli/RaftUtils.java |  89 -------
 .../org/apache/ratis/shell/cli/sh/RatisShell.java  |  44 +++-
 .../shell/cli/sh/command/AbstractCommand.java      |  23 +-
 .../shell/cli/sh/command/AbstractRatisCommand.java | 107 +++------
 .../apache/ratis/shell/cli/sh/command/Context.java |  69 ++++++
 .../ratis/shell/cli/sh/election/PauseCommand.java  |   3 +-
 .../ratis/shell/cli/sh/election/ResumeCommand.java |   3 +-
 .../shell/cli/sh/election/StepDownCommand.java     |   3 +-
 .../shell/cli/sh/election/TransferCommand.java     |   3 +-
 .../ratis/shell/cli/sh/group/GroupListCommand.java |   8 +-
 .../shell/cli/sh/local/RaftMetaConfCommand.java    |   6 +-
 .../apache/ratis/shell/cli/sh/peer/AddCommand.java |  13 +-
 .../ratis/shell/cli/sh/peer/RemoveCommand.java     |   3 +-
 .../shell/cli/sh/peer/SetPriorityCommand.java      |   3 +-
 .../shell/cli/sh/snapshot/TakeSnapshotCommand.java |   3 +-
 ratis-test/pom.xml                                 |   2 +-
 ...java => TestLeaderInstallSnapshotWithGrpc.java} |   4 +-
 ...rWithNetty.java => TestRaftAsyncWithNetty.java} |   8 +-
 .../apache/ratis/security/SecurityTestUtils.java   |  17 ++
 .../server/raftlog/segmented/TestLogSegment.java   |  24 +-
 .../raftlog/segmented/TestSegmentedRaftLog.java    |  84 +++++++
 .../segmented/TestSegmentedRaftLogCache.java       |  12 +-
 .../ratis/shell/cli/sh/TestSecureRatisShell.java   | 123 ++++++++++
 ratis-tools/pom.xml                                |   2 +-
 .../java/org/apache/ratis/tools/ParseRatisLog.java |   7 +-
 .../main/resources/ratis-version.properties        |   6 +-
 94 files changed, 1373 insertions(+), 1129 deletions(-)
 copy dev-support/checks/{build.sh => repro.sh} (95%)
 delete mode 100644 ratis-assembly/src/main/assembly/bin-pkg.xml
 delete mode 100644 ratis-assembly/src/main/assembly/examples-bin.xml
 delete mode 100644 ratis-assembly/src/main/assembly/shell-bin.xml
 create mode 100644 ratis-shell/src/main/java/org/apache/ratis/shell/cli/CliUtils.java
 delete mode 100644 ratis-shell/src/main/java/org/apache/ratis/shell/cli/RaftUtils.java
 rename ratis-test/src/test/java/org/apache/ratis/grpc/{TestLeaderInstallSnapshot.java => TestLeaderInstallSnapshotWithGrpc.java} (97%)
 copy ratis-test/src/test/java/org/apache/ratis/netty/{TestLogAppenderWithNetty.java => TestRaftAsyncWithNetty.java} (86%)
 create mode 100644 ratis-test/src/test/java/org/apache/ratis/shell/cli/sh/TestSecureRatisShell.java
 copy ratis-metrics-default/src/main/resources/META-INF/services/org.apache.ratis.metrics.MetricRegistries => src/main/resources/ratis-version.properties (84%)


(ratis) 03/04: RATIS-2165. Do not include RaftClientRequest Message in ReadIndexRequest (#1160)

Posted by ta...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

tanxinyu pushed a commit to branch snapshot-3
in repository https://gitbox.apache.org/repos/asf/ratis.git

commit 5d1a5ea93c6b79963f80e2b520cdbfde62e1efcc
Author: 133tosakarin <97...@users.noreply.github.com>
AuthorDate: Wed Oct 9 08:06:22 2024 +0800

    RATIS-2165. Do not include RaftClientRequest Message in ReadIndexRequest (#1160)
---
 .../main/java/org/apache/ratis/client/impl/ClientProtoUtils.java    | 6 +++++-
 .../main/java/org/apache/ratis/server/impl/ServerProtoUtils.java    | 2 +-
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/ratis-client/src/main/java/org/apache/ratis/client/impl/ClientProtoUtils.java b/ratis-client/src/main/java/org/apache/ratis/client/impl/ClientProtoUtils.java
index 5e217e7da..44e3a5cd6 100644
--- a/ratis-client/src/main/java/org/apache/ratis/client/impl/ClientProtoUtils.java
+++ b/ratis-client/src/main/java/org/apache/ratis/client/impl/ClientProtoUtils.java
@@ -204,9 +204,13 @@ public interface ClientProtoUtils {
   }
 
   static RaftClientRequestProto toRaftClientRequestProto(RaftClientRequest request) {
+    return toRaftClientRequestProto(request, true);
+  }
+
+  static RaftClientRequestProto toRaftClientRequestProto(RaftClientRequest request, boolean withMsg) {
     final RaftClientRequestProto.Builder b = RaftClientRequestProto.newBuilder()
         .setRpcRequest(toRaftRpcRequestProtoBuilder(request));
-    if (request.getMessage() != null) {
+    if (withMsg && request.getMessage() != null) {
       b.setMessage(toClientMessageEntryProtoBuilder(request.getMessage()));
     }
 
diff --git a/ratis-server/src/main/java/org/apache/ratis/server/impl/ServerProtoUtils.java b/ratis-server/src/main/java/org/apache/ratis/server/impl/ServerProtoUtils.java
index e35cb2386..29a42f65a 100644
--- a/ratis-server/src/main/java/org/apache/ratis/server/impl/ServerProtoUtils.java
+++ b/ratis-server/src/main/java/org/apache/ratis/server/impl/ServerProtoUtils.java
@@ -114,7 +114,7 @@ final class ServerProtoUtils {
       RaftClientRequest clientRequest, RaftGroupMemberId requestorId, RaftPeerId replyId) {
     return ReadIndexRequestProto.newBuilder()
         .setServerRequest(ClientProtoUtils.toRaftRpcRequestProtoBuilder(requestorId, replyId))
-        .setClientRequest(ClientProtoUtils.toRaftClientRequestProto(clientRequest))
+        .setClientRequest(ClientProtoUtils.toRaftClientRequestProto(clientRequest, false))
         .build();
   }
 


(ratis) 04/04: RATIS-2166. Update nextChunkIndex when snapshot is ALREADY_INSTALLED (#1159)

Posted by ta...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

tanxinyu pushed a commit to branch snapshot-3
in repository https://gitbox.apache.org/repos/asf/ratis.git

commit 536419c50365e1765fe303e3452a967aae4cd1dd
Author: 133tosakarin <97...@users.noreply.github.com>
AuthorDate: Wed Oct 9 12:22:15 2024 +0800

    RATIS-2166. Update nextChunkIndex when snapshot is ALREADY_INSTALLED (#1159)
---
 .../org/apache/ratis/server/impl/SnapshotInstallationHandler.java | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/ratis-server/src/main/java/org/apache/ratis/server/impl/SnapshotInstallationHandler.java b/ratis-server/src/main/java/org/apache/ratis/server/impl/SnapshotInstallationHandler.java
index 9b84e8f1d..537b384c6 100644
--- a/ratis-server/src/main/java/org/apache/ratis/server/impl/SnapshotInstallationHandler.java
+++ b/ratis-server/src/main/java/org/apache/ratis/server/impl/SnapshotInstallationHandler.java
@@ -185,6 +185,7 @@ class SnapshotInstallationHandler {
         // considering a follower peer requiring a snapshot installation does not
         // have a lot of requests
         if (state.getLog().getLastCommittedIndex() >= lastIncludedIndex) {
+          nextChunkIndex.set(snapshotChunkRequest.getRequestIndex() + 1);
           return toInstallSnapshotReplyProto(leaderId, getMemberId(),
               currentTerm, snapshotChunkRequest.getRequestIndex(), InstallSnapshotResult.ALREADY_INSTALLED);
         }
@@ -192,8 +193,11 @@ class SnapshotInstallationHandler {
         //TODO: We should only update State with installed snapshot once the request is done.
         state.installSnapshot(request);
 
-        int idx = nextChunkIndex.getAndIncrement();
-        Preconditions.assertEquals(snapshotChunkRequest.getRequestIndex(), idx, "nextChunkIndex");
+        final int expectedChunkIndex = nextChunkIndex.getAndIncrement();
+        if (expectedChunkIndex != snapshotChunkRequest.getRequestIndex()) {
+          throw new IOException("Unexpected request chunk index: " + snapshotChunkRequest.getRequestIndex()
+              + " (the expected index is " + expectedChunkIndex + ")");
+        }
         // update the committed index
         // re-load the state machine if this is the last chunk
         if (snapshotChunkRequest.getDone()) {


(ratis) 01/04: RATIS-2160. MetricRegistriesLoader should not print INFO message in CLI. (#1152)

Posted by ta...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

tanxinyu pushed a commit to branch snapshot-3
in repository https://gitbox.apache.org/repos/asf/ratis.git

commit 3021c02440907d62f39fa752ab0edd06482bb785
Author: Tsz-Wo Nicholas Sze <sz...@apache.org>
AuthorDate: Sun Sep 29 15:47:51 2024 -0700

    RATIS-2160. MetricRegistriesLoader should not print INFO message in CLI. (#1152)
---
 .../ratis/metrics/MetricRegistriesLoader.java      | 37 +++++++++-------------
 .../ratis/metrics/TestMetricRegistriesLoader.java  |  6 ++++
 2 files changed, 21 insertions(+), 22 deletions(-)

diff --git a/ratis-metrics-api/src/main/java/org/apache/ratis/metrics/MetricRegistriesLoader.java b/ratis-metrics-api/src/main/java/org/apache/ratis/metrics/MetricRegistriesLoader.java
index 0837b0bdd..8baac7a46 100644
--- a/ratis-metrics-api/src/main/java/org/apache/ratis/metrics/MetricRegistriesLoader.java
+++ b/ratis-metrics-api/src/main/java/org/apache/ratis/metrics/MetricRegistriesLoader.java
@@ -22,6 +22,7 @@ package org.apache.ratis.metrics;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.ServiceLoader;
+import java.util.stream.Collectors;
 
 import org.apache.ratis.thirdparty.com.google.common.annotations.VisibleForTesting;
 import org.apache.ratis.util.ReflectionUtils;
@@ -30,10 +31,9 @@ import org.slf4j.LoggerFactory;
 
 
 public final class MetricRegistriesLoader {
-  private static final Logger LOG = LoggerFactory.getLogger(MetricRegistries.class);
+  private static final Logger LOG = LoggerFactory.getLogger(MetricRegistriesLoader.class);
 
-  private static final String DEFAULT_CLASS
-      = "org.apache.ratis.metrics.impl.MetricRegistriesImpl";
+  static final String DEFAULT_CLASS = "org.apache.ratis.metrics.impl.MetricRegistriesImpl";
 
   private MetricRegistriesLoader() {
   }
@@ -56,32 +56,25 @@ public final class MetricRegistriesLoader {
    * @return A {@link MetricRegistries} implementation.
    */
   @VisibleForTesting
-  static MetricRegistries load(List<MetricRegistries> availableImplementations) {
-
-    if (availableImplementations.size() == 1) {
-      // One and only one instance -- what we want/expect
-      MetricRegistries impl = availableImplementations.get(0);
-      LOG.info("Loaded MetricRegistries " + impl.getClass());
-      return impl;
-    } else if (availableImplementations.isEmpty()) {
+  static MetricRegistries load(List<MetricRegistries> registries) {
+    if (registries.isEmpty()) {
       try {
         return ReflectionUtils.newInstance(Class.forName(DEFAULT_CLASS).asSubclass(MetricRegistries.class));
       } catch (ClassNotFoundException e) {
-        throw new RuntimeException(e);
+        throw new IllegalStateException("Failed to load default MetricRegistries " + DEFAULT_CLASS, e);
       }
+    }
+
+    final MetricRegistries first = registries.get(0);
+    if (registries.size() == 1) {
+      // One and only one instance -- what we want/expect
+      LOG.debug("Loaded {}", first.getClass());
     } else {
       // Tell the user they're doing something wrong, and choose the first impl.
-      StringBuilder sb = new StringBuilder();
-      for (MetricRegistries factory : availableImplementations) {
-        if (sb.length() > 0) {
-          sb.append(", ");
-        }
-        sb.append(factory.getClass());
-      }
-      LOG.warn("Found multiple MetricRegistries implementations: " + sb
-          + ". Using first found implementation: " + availableImplementations.get(0));
-      return availableImplementations.get(0);
+      final List<? extends Class<?>> classes = registries.stream().map(Object::getClass).collect(Collectors.toList());
+      LOG.warn("Found multiple MetricRegistries: {}. Using the first: {}", classes, first.getClass());
     }
+    return first;
   }
 
   private static List<MetricRegistries> getDefinedImplementations() {
diff --git a/ratis-metrics-default/src/test/java/org/apache/ratis/metrics/TestMetricRegistriesLoader.java b/ratis-metrics-default/src/test/java/org/apache/ratis/metrics/TestMetricRegistriesLoader.java
index cefc6b794..9816cc99c 100644
--- a/ratis-metrics-default/src/test/java/org/apache/ratis/metrics/TestMetricRegistriesLoader.java
+++ b/ratis-metrics-default/src/test/java/org/apache/ratis/metrics/TestMetricRegistriesLoader.java
@@ -34,6 +34,12 @@ import java.util.Collections;
  * Test class for {@link MetricRegistriesLoader}.
  */
 public class TestMetricRegistriesLoader {
+  @Test
+  public void testLoadEmptyInstance() {
+    MetricRegistries instance = MetricRegistriesLoader.load(Collections.emptyList());
+    assertEquals(MetricRegistriesLoader.DEFAULT_CLASS, instance.getClass().getName());
+  }
+
   @Test
   public void testLoadSingleInstance() {
     MetricRegistries loader = mock(MetricRegistries.class);


(ratis) 02/04: RATIS-2162. When closing leaderState, if the logAppender thread sends a snapshot, a deadlock may occur. (#1154)

Posted by ta...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

tanxinyu pushed a commit to branch snapshot-3
in repository https://gitbox.apache.org/repos/asf/ratis.git

commit d8482f1f0a72420e50aeeb2401a669d43bbfbf6d
Author: 133tosakarin <97...@users.noreply.github.com>
AuthorDate: Mon Sep 30 14:43:38 2024 +0800

    RATIS-2162. When closing leaderState, if the logAppender thread sends a snapshot, a deadlock may occur. (#1154)
---
 .../ratis/server/impl/ConfigurationManager.java    | 10 +++----
 .../apache/ratis/server/impl/FollowerState.java    | 14 +++++++++-
 .../apache/ratis/server/impl/LeaderElection.java   | 14 +++++++++-
 .../apache/ratis/server/impl/LeaderStateImpl.java  |  7 ++---
 .../apache/ratis/server/impl/RaftServerImpl.java   | 32 ++++++++++++----------
 .../org/apache/ratis/server/impl/RoleInfo.java     | 26 +++++++++---------
 .../ratis/server/leader/LogAppenderBase.java       |  2 +-
 7 files changed, 64 insertions(+), 41 deletions(-)

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 10c59c8b1..6d3f68d5c 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,11 +41,9 @@ 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;
+  private RaftConfigurationImpl currentConf;
   /** Cache the peer corresponding to {@link #id}. */
-  @SuppressWarnings({"squid:S3077"}) // Suppress volatile for generic type
-  private volatile RaftPeer currentPeer;
+  private RaftPeer currentPeer;
 
   ConfigurationManager(RaftPeerId id, RaftConfigurationImpl initialConf) {
     this.id = id;
@@ -78,11 +76,11 @@ public class ConfigurationManager {
     }
   }
 
-  RaftConfigurationImpl getCurrent() {
+  synchronized RaftConfigurationImpl getCurrent() {
     return currentConf;
   }
 
-  RaftPeer getCurrentPeer() {
+  synchronized RaftPeer getCurrentPeer() {
     return currentPeer;
   }
 
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 e980daede..fa61e9088 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
@@ -26,6 +26,7 @@ import org.apache.ratis.util.Timestamp;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.function.ToIntFunction;
@@ -62,6 +63,7 @@ class FollowerState extends Daemon {
   @SuppressWarnings({"squid:S3077"}) // Suppress volatile for generic type
   private volatile Timestamp lastRpcTime = creationTime;
   private volatile boolean isRunning = true;
+  private final CompletableFuture<Void> stopped = new CompletableFuture<>();
   private final AtomicInteger outstandingOp = new AtomicInteger();
 
   FollowerState(RaftServerImpl server, Object reason) {
@@ -93,8 +95,10 @@ class FollowerState extends Daemon {
     return lastRpcTime.elapsedTime().compareTo(server.properties().minRpcTimeout()) < 0;
   }
 
-  void stopRunning() {
+  CompletableFuture<Void> stopRunning() {
     this.isRunning = false;
+    interrupt();
+    return stopped;
   }
 
   boolean lostMajorityHeartbeatsRecently() {
@@ -122,6 +126,14 @@ class FollowerState extends Daemon {
 
   @Override
   public  void run() {
+    try {
+      runImpl();
+    } finally {
+      stopped.complete(null);
+    }
+  }
+
+  private void runImpl() {
     final TimeDuration sleepDeviationThreshold = server.getSleepDeviationThreshold();
     while (shouldRun()) {
       final TimeDuration electionTimeout = server.getRandomElectionTimeout();
diff --git a/ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderElection.java b/ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderElection.java
index af25ae912..a5bfba7be 100644
--- a/ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderElection.java
+++ b/ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderElection.java
@@ -46,6 +46,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Optional;
 import java.util.concurrent.Callable;
+import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorCompletionService;
 import java.util.concurrent.ExecutorService;
@@ -183,6 +184,7 @@ class LeaderElection implements Runnable {
   private final String name;
   private final LifeCycle lifeCycle;
   private final Daemon daemon;
+  private final CompletableFuture<Void> stopped = new CompletableFuture<>();
 
   private final RaftServerImpl server;
   private final boolean skipPreVote;
@@ -223,8 +225,10 @@ class LeaderElection implements Runnable {
     }
   }
 
-  void shutdown() {
+  CompletableFuture<Void> shutdown() {
     lifeCycle.checkStateAndClose();
+    stopped.complete(null);
+    return stopped;
   }
 
   @VisibleForTesting
@@ -234,6 +238,14 @@ class LeaderElection implements Runnable {
 
   @Override
   public void run() {
+    try {
+      runImpl();
+    } finally {
+      stopped.complete(null);
+    }
+  }
+
+  private void runImpl() {
     if (!lifeCycle.compareAndTransition(STARTING, RUNNING)) {
       final LifeCycle.State state = lifeCycle.getCurrentState();
       LOG.info("{}: skip running since this is already {}", this, state);
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 68121cef6..3d8bc2219 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
@@ -223,11 +223,8 @@ class LeaderStateImpl implements LeaderState {
     }
 
     CompletableFuture<Void> stopAll() {
-      final CompletableFuture<?>[] futures = new CompletableFuture<?>[senders.size()];
-      for(int i = 0; i < futures.length; i++) {
-        futures[i] = senders.get(i).stopAsync();
-      }
-      return CompletableFuture.allOf(futures);
+      return CompletableFuture.allOf(senders.stream().
+              map(LogAppender::stopAsync).toArray(CompletableFuture[]::new));
     }
   }
 
diff --git a/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java b/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java
index 0473564b3..ae158ad75 100644
--- a/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java
+++ b/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java
@@ -132,6 +132,7 @@ import java.util.concurrent.ExecutorService;
 import java.util.concurrent.ThreadLocalRandom;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicReference;
 import java.util.function.Function;
 import java.util.function.Supplier;
 import java.util.stream.Collectors;
@@ -565,20 +566,23 @@ class RaftServerImpl implements RaftServer.Division,
    * @param force Force to start a new {@link FollowerState} even if this server is already a follower.
    * @return if the term/votedFor should be updated to the new term
    */
-  private synchronized boolean changeToFollower(
-      long newTerm,
-      boolean force,
-      boolean allowListener,
-      Object reason) {
+  private boolean changeToFollower(long newTerm, boolean force, boolean allowListener, Object reason) {
+    final AtomicReference<Boolean> metadataUpdated = new AtomicReference<>();
+    changeToFollowerAsync(newTerm, force, allowListener, reason, metadataUpdated).join();
+    return metadataUpdated.get();
+  }
+
+  private synchronized CompletableFuture<Void> changeToFollowerAsync(
+      long newTerm, boolean force, boolean allowListener, Object reason, AtomicReference<Boolean> metadataUpdated) {
     final RaftPeerRole old = role.getCurrentRole();
     if (old == RaftPeerRole.LISTENER && !allowListener) {
       throw new IllegalStateException("Unexpected role " + old);
     }
-    boolean metadataUpdated;
+    CompletableFuture<Void> future = CompletableFuture.completedFuture(null);
     if ((old != RaftPeerRole.FOLLOWER || force) && old != RaftPeerRole.LISTENER) {
       setRole(RaftPeerRole.FOLLOWER, reason);
       if (old == RaftPeerRole.LEADER) {
-        role.shutdownLeaderState(false)
+        future = role.shutdownLeaderState(false)
             .exceptionally(e -> {
               if (e != null) {
                 if (!getInfo().isAlive()) {
@@ -587,21 +591,21 @@ class RaftServerImpl implements RaftServer.Division,
                 }
               }
               throw new CompletionException("Failed to shutdownLeaderState: " + this, e);
-            })
-            .join();
+            });
         state.setLeader(null, reason);
       } else if (old == RaftPeerRole.CANDIDATE) {
-        role.shutdownLeaderElection();
+        future = role.shutdownLeaderElection();
       } else if (old == RaftPeerRole.FOLLOWER) {
-        role.shutdownFollowerState();
+        future = role.shutdownFollowerState();
       }
-      metadataUpdated = state.updateCurrentTerm(newTerm);
+
+      metadataUpdated.set(state.updateCurrentTerm(newTerm));
       role.startFollowerState(this, reason);
       setFirstElection(reason);
     } else {
-      metadataUpdated = state.updateCurrentTerm(newTerm);
+      metadataUpdated.set(state.updateCurrentTerm(newTerm));
     }
-    return metadataUpdated;
+    return future;
   }
 
   synchronized void changeToFollowerAndPersistMetadata(
diff --git a/ratis-server/src/main/java/org/apache/ratis/server/impl/RoleInfo.java b/ratis-server/src/main/java/org/apache/ratis/server/impl/RoleInfo.java
index 5eb01a9d6..a5cd7da66 100644
--- a/ratis-server/src/main/java/org/apache/ratis/server/impl/RoleInfo.java
+++ b/ratis-server/src/main/java/org/apache/ratis/server/impl/RoleInfo.java
@@ -47,7 +47,7 @@ class RoleInfo {
   public static final Logger LOG = LoggerFactory.getLogger(RoleInfo.class);
 
   private final RaftPeerId id;
-  private volatile RaftPeerRole role;
+  private final AtomicReference<RaftPeerRole> role = new AtomicReference<>();
   /** Used when the peer is leader */
   private final AtomicReference<LeaderStateImpl> leaderState = new AtomicReference<>();
   /** Used when the peer is follower, to monitor election timeout */
@@ -64,7 +64,7 @@ class RoleInfo {
   }
 
   void transitionRole(RaftPeerRole newRole) {
-    this.role = newRole;
+    this.role.set(newRole);
     this.transitionTime.set(Timestamp.currentTime());
   }
 
@@ -73,7 +73,7 @@ class RoleInfo {
   }
 
   RaftPeerRole getCurrentRole() {
-    return role;
+    return role.get();
   }
 
   boolean isLeaderReady() {
@@ -113,13 +113,13 @@ class RoleInfo {
     updateAndGet(followerState, new FollowerState(server, reason)).start();
   }
 
-  void shutdownFollowerState() {
+  CompletableFuture<Void> shutdownFollowerState() {
     final FollowerState follower = followerState.getAndSet(null);
-    if (follower != null) {
-      LOG.info("{}: shutdown {}", id, follower);
-      follower.stopRunning();
-      follower.interrupt();
+    if (follower == null) {
+      return CompletableFuture.completedFuture(null);
     }
+    LOG.info("{}: shutdown {}", id, follower);
+    return follower.stopRunning();
   }
 
   void startLeaderElection(RaftServerImpl server, boolean force) {
@@ -133,13 +133,13 @@ class RoleInfo {
     pauseLeaderElection.set(pause);
   }
 
-  void shutdownLeaderElection() {
+  CompletableFuture<Void> shutdownLeaderElection() {
     final LeaderElection election = leaderElection.getAndSet(null);
-    if (election != null) {
-      LOG.info("{}: shutdown {}", id, election);
-      election.shutdown();
-      // no need to interrupt the election thread
+    if (election == null) {
+      return CompletableFuture.completedFuture(null);
     }
+    LOG.info("{}: shutdown {}", id, election);
+    return election.shutdown();
   }
 
   private <T> T updateAndGet(AtomicReference<T> ref, T current) {
diff --git a/ratis-server/src/main/java/org/apache/ratis/server/leader/LogAppenderBase.java b/ratis-server/src/main/java/org/apache/ratis/server/leader/LogAppenderBase.java
index 958cc6fa8..5a27cda51 100644
--- a/ratis-server/src/main/java/org/apache/ratis/server/leader/LogAppenderBase.java
+++ b/ratis-server/src/main/java/org/apache/ratis/server/leader/LogAppenderBase.java
@@ -124,7 +124,7 @@ public abstract class LogAppenderBase implements LogAppender {
 
   @Override
   public boolean isRunning() {
-    return daemon.isWorking();
+    return daemon.isWorking() && server.getInfo().isLeader();
   }
 
   @Override