Class ReadRouter
java.lang.Object
com.loomcache.server.replication.ReadRouter
Routes read requests to appropriate replicas based on consistency level.
Implements routing strategies:
- STRONG: Token-bearing leader route via routeStrongRead()
- BOUNDED_STALENESS: To least-lagging replica within threshold
- EVENTUAL: Round-robin across active replicas
- SESSION: Conservative leader routing until per-session replication progress is tracked
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordAggregated stats about read routing.static final classToken-bearing route for a STRONG read. -
Constructor Summary
ConstructorsConstructorDescriptionReadRouter(String nodeId, String leaderId, int instanceNumber, ReadReplicaManager replicaManager) Initialize the ReadRouter. -
Method Summary
Modifier and TypeMethodDescriptionvoidclearSession(String sessionId) Clear the session cache (e.g., on session expiration).getStats()Get aggregated routing statistics.@Nullable StringrouteRead(ConsistencyLevel consistencyLevel, @Nullable String sessionId) Route a read request and get the target replica.@Nullable ReadRouter.StrongReadRouteRoute a STRONG read to the leader and return a token that the caller must revalidate after executing the actual read.voidsetBoundedStalenessThresholdMs(long thresholdMs) Set the bounded staleness threshold in milliseconds.voidsetReadIndexBarrier(Supplier<CompletableFuture<Long>> barrier) Set the legacy Raft readIndex barrier supplier.voidsetStrongReadBarrierTimeoutMs(long timeoutMs) Set the maximum time a STRONG read will wait to acquire a linearizable read token.voidsetStrongReadTokenProtocol(Supplier<CompletableFuture<Long>> readTokenSupplier, LongPredicate readTokenValidator) Set the token protocol used for linearizable STRONG reads.
-
Constructor Details
-
ReadRouter
public ReadRouter(String nodeId, String leaderId, int instanceNumber, ReadReplicaManager replicaManager) Initialize the ReadRouter.- Parameters:
nodeId- The local node identifier (leader)leaderId- The leader identifier (same as nodeId for now)instanceNumber- The instance number for loggingreplicaManager- The replica manager
-
-
Method Details
-
setReadIndexBarrier
Set the legacy Raft readIndex barrier supplier. This hook is insufficient for STRONG routing because it cannot revalidate the actual read after the router returns. UsesetStrongReadTokenProtocol(Supplier, LongPredicate)for linearizable STRONG reads.- Parameters:
barrier- supplier returning a future that completes when state machine has caught up under confirmed leadership
-
setStrongReadTokenProtocol
public void setStrongReadTokenProtocol(Supplier<CompletableFuture<Long>> readTokenSupplier, LongPredicate readTokenValidator) Set the token protocol used for linearizable STRONG reads.- Parameters:
readTokenSupplier- supplier returning a future that completes with a read tokenreadTokenValidator- validator used after the actual read before publishing
-
setBoundedStalenessThresholdMs
public void setBoundedStalenessThresholdMs(long thresholdMs) Set the bounded staleness threshold in milliseconds.- Parameters:
thresholdMs- The threshold
-
setStrongReadBarrierTimeoutMs
public void setStrongReadBarrierTimeoutMs(long timeoutMs) Set the maximum time a STRONG read will wait to acquire a linearizable read token.- Parameters:
timeoutMs- timeout in milliseconds, must be positive
-
routeRead
Route a read request and get the target replica.- Parameters:
consistencyLevel- The desired consistency levelsessionId- The session identifier (for SESSION consistency)- Returns:
- The replica ID to read from for non-STRONG reads, or null on error.
STRONG reads must use
routeStrongRead()so the caller can revalidate before publishing.
-
routeStrongRead
Route a STRONG read to the leader and return a token that the caller must revalidate after executing the actual read.- Returns:
- token-bearing leader route, or null when a linearizable route cannot be acquired
-
clearSession
Clear the session cache (e.g., on session expiration).- Parameters:
sessionId- The session to remove
-
getStats
Get aggregated routing statistics.- Returns:
- ReadRouterStats record
-