Class RaftMetrics

java.lang.Object
com.loomcache.server.consensus.RaftMetrics

public final class RaftMetrics extends Object
Raft consensus node performance metrics.

Purpose: Tracks and exposes metrics for Raft operations including elections, log replication, commits, and snapshots. All metrics are thread-safe using AtomicLong and LongAdder, making them suitable for low-contention concurrent access.

Thread Safety: All metric operations are thread-safe and lock-free. Multiple threads can safely record metrics concurrently without synchronization.

Use Cases:

  • Monitor Raft consensus health and performance
  • Track replication latency and success rates
  • Detect issues with leader elections or log consistency
Since:
1.0
  • Constructor Details

    • RaftMetrics

      public RaftMetrics()
  • Method Details

    • recordElection

      public void recordElection()
      Record an election event.
    • recordAppendEntries

      public void recordAppendEntries(long latencyMs)
      Record an AppendEntries RPC with latency in milliseconds.
      Parameters:
      latencyMs - the round-trip latency in milliseconds (must be non-negative)
    • recordCommit

      public void recordCommit(long latencyMs)
      Record a commit operation with latency in milliseconds.
      Parameters:
      latencyMs - the operation latency in milliseconds (must be non-negative)
    • setLogSize

      public void setLogSize(long size)
      Set the current log size.
      Parameters:
      size - the number of entries in the log (must be non-negative)
    • setUncommittedEntries

      public void setUncommittedEntries(long count)
      Set the number of uncommitted entries.
      Parameters:
      count - the count of uncommitted entries (must be non-negative)
    • recordTermChange

      public void recordTermChange()
      Record a term change.
    • recordSnapshot

      public void recordSnapshot()
      Record a snapshot operation.
    • getElectionCount

      public long getElectionCount()
      Get total number of elections.
    • getAppendEntriesAvgLatency

      public double getAppendEntriesAvgLatency()
      Get average latency for AppendEntries RPC in milliseconds.
    • getCommitAvgLatency

      public double getCommitAvgLatency()
      Get average latency for commit in milliseconds.
    • getLogSize

      public long getLogSize()
      Get current log size in entries.
    • getUncommittedEntries

      public long getUncommittedEntries()
      Get count of uncommitted entries.
    • getTermChanges

      public long getTermChanges()
      Get total number of term changes.
    • getSnapshotCount

      public long getSnapshotCount()
      Get total number of snapshots.
    • getAppendEntriesCount

      public long getAppendEntriesCount()
      Get total AppendEntries RPC count.
    • getCommitCount

      public long getCommitCount()
      Get total commit count.
    • getMetrics

      public RaftMetrics.MetricsSnapshot getMetrics()
      Return a snapshot of all metrics at a point in time.
      Returns:
      a MetricsSnapshot with all current metric values (never null)