amaliujia commented on code in PR #1621:
URL: https://github.com/apache/ratis/pull/1621#discussion_r4214063735


##########
ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderStateImpl.java:
##########
@@ -687,7 +691,11 @@ private Collection<LogAppender> 
addSenders(Collection<RaftPeer> newPeers, long n
     final List<LogAppender> newAppenders = newPeers.stream().map(peer -> {
       final FollowerInfo f = new FollowerInfoImpl(server.getMemberId(), peer, 
this::getPeer, t, nextIndex, caughtUp);
       followerInfoMap.put(peer.getId(), f);
-      raftServerMetrics.addFollower(peer.getId());
+      synchronized (followerMetricsLock) {
+        if (!isStopped.get()) {

Review Comment:
   Can we move the synchronization after the stop check? 
   
   I think this can minimize the critical section. The race only happens here 
if the `isStopped` is false, but right after that moment, this thread 
interrupts, and then another thread calls `stop()`.
   
   If `isStopped` already is true, we can skip the attempt to acquire the lock 
at all.



##########
ratis-server/src/test/java/org/apache/ratis/LogAppenderTests.java:
##########
@@ -128,50 +134,84 @@ public void testUnlimitedElementBuffer() throws Exception 
{
   }
 
   @Test
-  public void testFollowerHeartbeatMetric() throws IOException, 
InterruptedException {
-
-    // Start a 3 node Ratis ring.
+  public void testFollowerHeartbeatMetric() throws Exception {
     final MiniRaftCluster cluster = newCluster(3);
-    cluster.start();
-    final RaftServer.Division leaderServer = waitForLeader(cluster);
+    final Map<RaftPeerId, RatisMetricRegistryImpl> registries = new 
HashMap<>();
+    try {
+      cluster.start();
+      final RaftPeerId originalLeader = waitForLeader(cluster).getId();
+      for (RaftServer.Division server : cluster.iterateDivisions()) {
+        registries.put(server.getId(), (RatisMetricRegistryImpl)
+            ((RaftServerMetricsImpl) 
server.getRaftServerMetrics()).getRegistry());
+      }
 
-    // Write 10 messages to leader.
-    try(RaftClient client = cluster.createClient(leaderServer.getId())) {
-      for (int i = 1; i <= 10; i++) {
-        client.io().send(new RaftTestUtil.SimpleMessage("Msg to make leader 
ready " +  i));
+      // Check initial leadership, step-down, and re-election without 
restarting the servers.
+      for (int round = 0; round < 3; round++) {
+        final RaftPeerId leaderId = waitForLeader(cluster).getId();
+        try (RaftClient client = cluster.createClient(leaderId)) {
+          for (int i = 0; i < 10; i++) {
+            RaftTestUtil.assertSuccessReply(
+                client.io().send(new SimpleMessage("heartbeat metrics round " 
+ round + " message " + i)));
+          }
+          JavaUtils.attempt(() -> assertFollowerHeartbeatMetrics(cluster, 
registries),
+              100, HUNDRED_MILLIS, "check heartbeat metrics", LOG);
+          if (round < 2) {
+            final RatisMetricRegistryImpl previousLeaderRegistry = 
registries.get(leaderId);
+            final SortedMap<String, Gauge> commitIndexGauges = 
previousLeaderRegistry.getGauges(
+                (s, m) -> s.endsWith("_peerCommitIndex"));
+            assertEquals(3, commitIndexGauges.size());
+            final RaftPeerId nextLeader = round == 0 ? 
cluster.getFollowers().get(0).getId() : originalLeader;

Review Comment:
   nit: we can move `if (round <2)` down here because above assertEqual check 
can still be applied to the 3rd round?



##########
ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderStateImpl.java:
##########
@@ -1370,7 +1378,12 @@ public void checkHealth(FollowerInfo follower) {
       
server.getStateMachine().leaderEvent().notifyFollowerSlowness(leaderInfo, 
follower.getPeer());
     }
     final RaftPeerId followerId = follower.getId();
-    raftServerMetrics.recordFollowerHeartbeatElapsedTime(followerId, 
elapsedTime.toLong(TimeUnit.NANOSECONDS));
+    synchronized (followerMetricsLock) {

Review Comment:
   dito



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to