Class ReadReplicaManager

java.lang.Object
com.loomcache.server.replication.ReadReplicaManager

public class ReadReplicaManager extends Object
Manages read replicas that receive replicated data but do not participate in Raft votes.

Read replicas are non-voting followers that receive log entries from the leader. They can serve read requests at various consistency levels (EVENTUAL, BOUNDED_STALENESS, etc.) and automatically track their replication lag.

  • Constructor Details

    • ReadReplicaManager

      public ReadReplicaManager(String nodeId, int instanceNumber)
      Initialize the ReadReplicaManager.
      Parameters:
      nodeId - The node identifier (must not be null)
      instanceNumber - The instance number for logging
  • Method Details

    • registerReplica

      public String registerReplica(String address)
      Register a new read replica.
      Parameters:
      address - The address of the replica (must not be null)
      Returns:
      The replica ID (address itself)
    • unregisterReplica

      public void unregisterReplica(String replicaId)
      Unregister a read replica.
      Parameters:
      replicaId - The replica ID (must not be null)
    • getActiveReplicas

      public List<ReadReplicaManager.ReplicaInfo> getActiveReplicas()
      Get all active replicas (state == ACTIVE).
      Returns:
      List of active replica infos
    • getReplicasWithinLag

      public List<ReadReplicaManager.ReplicaInfo> getReplicasWithinLag(long maxLagMs)
      Get replicas that are safe to serve bounded-staleness reads within the given lag threshold. Excludes replicas that are still syncing or fully disconnected.
      Parameters:
      maxLagMs - maximum acceptable lag in milliseconds
      Returns:
      replicas whose lag is within the threshold and that are ready to serve reads
    • getReplicaLag

      public long getReplicaLag(String replicaId)
      Get the replication lag for a replica in milliseconds.
      Parameters:
      replicaId - The replica ID (must not be null)
      Returns:
      Lag in milliseconds, or -1 if replica not found
    • updateReplicaLag

      public void updateReplicaLag(String replicaId, long lastSyncedIndex, long leaderLastLogIndex)
      Update the synced index for a replica and check lag threshold.
      Parameters:
      replicaId - The replica ID (must not be null)
      lastSyncedIndex - The last index synced to this replica
      leaderLastLogIndex - The leader's current last log index
    • markDisconnected

      public void markDisconnected(String replicaId)
      Mark a replica as disconnected (e.g., on connection failure).
      Parameters:
      replicaId - The replica ID (must not be null)
    • markSyncing

      public void markSyncing(String replicaId)
      Mark a replica as syncing (e.g., on reconnection start).
      Parameters:
      replicaId - The replica ID (must not be null)
    • getReplicaInfo

      public @Nullable ReadReplicaManager.ReplicaInfo getReplicaInfo(String replicaId)
      Get replica info by ID.
      Parameters:
      replicaId - The replica ID (must not be null)
      Returns:
      ReplicaInfo or null if not found
    • getAllReplicas

      public List<ReadReplicaManager.ReplicaInfo> getAllReplicas()
      Get all registered replicas.
      Returns:
      List of all replica infos
    • getStats

      Get aggregated statistics about all replicas.
      Returns:
      ReadReplicaStats record