pub struct CacheCoordinator {
pub clients: HashMap<NodeId, DistributedCacheClient>,
pub replication: ReplicationFactor,
}Expand description
Cluster-level coordinator.
Tracks all node clients and the replication policy for the cluster. In a real distributed system the coordinator would issue RPCs; here it simulates the routing and quorum decisions.
Fields§
§clients: HashMap<NodeId, DistributedCacheClient>Map from NodeId to per-node client.
replication: ReplicationFactorCluster-wide replication factor.
Implementations§
Source§impl CacheCoordinator
impl CacheCoordinator
Sourcepub fn new(replication: ReplicationFactor) -> Self
pub fn new(replication: ReplicationFactor) -> Self
Create a new CacheCoordinator with the given replication factor.
Sourcepub fn add_client(&mut self, client: DistributedCacheClient)
pub fn add_client(&mut self, client: DistributedCacheClient)
Register a DistributedCacheClient for its local_node.
Sourcepub fn remove_client(&mut self, node_id: NodeId)
pub fn remove_client(&mut self, node_id: NodeId)
Remove the client (and node) identified by node_id.
Sourcepub fn primary_node_for(&self, key: &[u8]) -> Option<NodeId>
pub fn primary_node_for(&self, key: &[u8]) -> Option<NodeId>
Determine the primary owner of key according to the first registered
client’s ring.
Returns None when no clients are registered.
Sourcepub fn replica_nodes_for(&self, key: &[u8], n: usize) -> Vec<NodeId>
pub fn replica_nodes_for(&self, key: &[u8], n: usize) -> Vec<NodeId>
Return up to n replica nodes for key according to the first
registered client’s ring.
Sourcepub fn can_write_quorum(&self, key: &[u8], available_nodes: &[NodeId]) -> bool
pub fn can_write_quorum(&self, key: &[u8], available_nodes: &[NodeId]) -> bool
Simulate a write operation: determine the owner nodes for key and
check whether a quorum can be formed from available_nodes.
Sourcepub fn can_read_quorum(&self, key: &[u8], available_nodes: &[NodeId]) -> bool
pub fn can_read_quorum(&self, key: &[u8], available_nodes: &[NodeId]) -> bool
Simulate a read operation: determine the owner nodes for key and
check whether a quorum can be formed from available_nodes.
Sourcepub fn node_count(&self) -> usize
pub fn node_count(&self) -> usize
Return the number of registered clients/nodes.