Class TopologyAwareRouter
java.lang.Object
com.loomcache.server.cluster.TopologyAwareRouter
Routes read and write requests based on topology awareness.
Routing strategy:
- Reads: Prefers closest node (same rack > same zone > same region > any) respecting consistency level requirements
- Writes: Always routes to the partition leader regardless of topology
- Fallback: If preferred node is unavailable, tries next closest node
Thread safety: uses ReentrantLock for topology map updates and atomic counters for stats.
- Since:
- 1.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordRepresents a network endpoint for a cache node.static final recordStatistics for topology-aware routing. -
Constructor Summary
ConstructorsConstructorDescriptionTopologyAwareRouter(TopologyConfig config) Create a new TopologyAwareRouter. -
Method Summary
Modifier and TypeMethodDescriptionGet all topology information registered in this router.getStats()Get routing statistics.voidregisterNodeTopology(String nodeId, TopologyInfo topology) Register a node's topology information.voidReset statistics counters.routeRead(String key, ConsistencyLevel consistencyLevel, List<TopologyAwareRouter.NodeEndpoint> availableNodes, String leaderId) Route a read request based on topology and consistency level.routeWrite(List<TopologyAwareRouter.NodeEndpoint> availableNodes, String leaderId) Route a write request (always to leader regardless of topology).
-
Constructor Details
-
TopologyAwareRouter
Create a new TopologyAwareRouter.- Parameters:
config- TopologyConfig with local node topology
-
-
Method Details
-
registerNodeTopology
Register a node's topology information.- Parameters:
nodeId- the node identifiertopology- the TopologyInfo for the node
-
routeRead
public TopologyAwareRouter.NodeEndpoint routeRead(String key, ConsistencyLevel consistencyLevel, List<TopologyAwareRouter.NodeEndpoint> availableNodes, String leaderId) Route a read request based on topology and consistency level.Preference order:
- STRONG: leader only
- BOUNDED_STALENESS/EVENTUAL: same rack > same zone > same region > any available
- SESSION: try to return to same node if possible
- Parameters:
key- the cache keyconsistencyLevel- the required consistency levelavailableNodes- list of available nodes (with endpoints)leaderId- the current leader node ID- Returns:
- the NodeEndpoint to route the read to
- Throws:
IllegalArgumentException- if parameters are invalid or no available nodes
-
routeWrite
public TopologyAwareRouter.NodeEndpoint routeWrite(List<TopologyAwareRouter.NodeEndpoint> availableNodes, String leaderId) Route a write request (always to leader regardless of topology).- Parameters:
availableNodes- list of available nodesleaderId- the current leader node ID- Returns:
- the NodeEndpoint of the leader
- Throws:
IllegalArgumentException- if parameters are invalid or leader not found
-
getNodeTopologies
Get all topology information registered in this router.- Returns:
- map of nodeId → TopologyInfo (copy)
-
getStats
Get routing statistics.- Returns:
- TopologyRouterStats with current counters
-
resetStats
public void resetStats()Reset statistics counters.
-