amaliujia commented on PR #1621:
URL: https://github.com/apache/ratis/pull/1621#issuecomment-5944959923

   AI suggests a source code fix, and I am wondering if you think this make 
sense:
   
   
   1. RaftServerMetricsImpl — symmetric teardown
   ```
   // In RaftServerMetricsImpl.java
   import java.util.ArrayList;
   import java.util.Collection;
   /**
    * Removes leader-only metrics for one remote peer (inverse of {@link 
#addFollower}).
    * Does not touch the self peer commit-index gauge registered in the 
constructor.
    */
   public void removeFollower(RaftPeerId followerId) {
     followerLastHeartbeatElapsedTimeMap.remove(followerId);
     
getRegistry().remove(String.format(FOLLOWER_LAST_HEARTBEAT_ELAPSED_TIME_METRIC, 
followerId));
     getRegistry().remove(getPeerCommitIndexGaugeKey(followerId));
   }
   /**
    * Removes all leader-only per-follower metrics when stepping down.
    * Uses {@link #followerLastHeartbeatElapsedTimeMap} as the source of truth 
for
    * which peers were registered via {@link #addFollower}.
    */
   public void clearLeaderFollowerMetrics() {
     // Copy keys: removeFollower mutates the map.
     final Collection<RaftPeerId> followerIds =
         new ArrayList<>(followerLastHeartbeatElapsedTimeMap.keySet());
     followerIds.forEach(this::removeFollower);
   }
   /**
    * Optional: if you still call {@link #unregister()} on step-down, re-export 
server metrics
    * for the follower role (create() is ref-counted / no-op if already 
registered).
    */
   public void ensureRegistered() {
     create(getRegistry().getMetricRegistryInfo());
   }
   ```
   
   2. LeaderStateImpl.stop() — call teardown before/instead of full unregister
   ```
   // In LeaderStateImpl.stop(), replace or narrow the metrics cleanup block:
       if (replyFlusher != null) {
         replyFlusher.stop();
       }
       // Leader-only: log appender registry is per LeaderStateImpl — 
unregister is correct.
       logAppenderMetrics.unregister();
       // Leader-only series on the shared RaftServerMetricsImpl registry.
       raftServerMetrics.clearLeaderFollowerMetrics();
       // Option A (recommended): keep server metrics exported while follower.
       // Do NOT call raftServerMetrics.unregister() here.
       // Option B: if you must unregister for JMX, re-register immediately 
after cleanup:
       // raftServerMetrics.unregister();
       // raftServerMetrics.ensureRegistered();
       pendingRequests.close();  // already removes leader pending-queue gauges
       watchRequests.close();
       leaderTracer.close();
   Today pendingRequests.close() already removes leader pending-queue gauges; 
clearLeaderFollowerMetrics() covers heartbeat + remote peer commit-index gauges.
   ```
   3. Test assertion — can use embedded registry again (after product fix)
   Once teardown runs on step-down, the old test idea becomes valid again:
   ```
   final RatisMetricRegistryImpl followerRegistry =
       (RatisMetricRegistryImpl) followerMetrics.getRegistry();
   assertTrue(
       followerRegistry.getGauges((s, m) -> 
s.contains("lastHeartbeatElapsedTime")).isEmpty(),
       "Follower " + followerId + " should not retain leader heartbeat gauges");
    ```   
   Or keep checking global export only — both should pass if cleanup + export 
policy are consistent.


-- 
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