state.rs

Overview

This file implements the core data structures and logic for managing the state of nodes and clusters in a distributed gossip protocol system. It maintains versioned key-value data per node, tracks heartbeats, and applies state deltas to reconcile differences between nodes, supporting efficient incremental synchronization. The module also supports garbage collection of deleted entries (tombstones) and prioritizes stale nodes for gossip updates based on a staleness metric.

Key responsibilities include:


Structs and Their Functionality

NodeState

Represents the state of a single node in the cluster. Each NodeState contains:

Important Notes on last_gc_version and max_version

Methods

Usage Example

let mut node_state = NodeState::new(chitchat_id, listeners);
node_state.set("key1", "value1");
let value = node_state.get("key1");
node_state.delete("key1");
node_state.gc_keys_marked_for_deletion(Duration::from_secs(60));

ClusterState

Represents the cluster-wide state, managing multiple NodeStates.

Fields:

Methods

Usage Example

let cluster_state = ClusterState::with_seed_addrs(seed_addrs_rx);
let node_state = cluster_state.node_state_mut(&some_chitchat_id);
node_state.set("foo", "bar");
let digest = cluster_state.compute_digest(&HashSet::new());
let delta = cluster_state.compute_partial_delta_respecting_mtu(&digest, 1024, &HashSet::new());
cluster_state.apply_delta(delta);

Staleness

A struct representing the staleness score of a node, used to prioritize gossip updates. Fields:

Ordering for prioritization:


SortedStaleNodes<'a>

Maintains a sorted collection of stale nodes for prioritization.

Methods:


StaleNode<'a>

Represents a node with stale information compared to a digest.

Fields:

Methods:


ClusterStateSnapshot

Serializable snapshot of the cluster state used for persistence or transmission.

Fields:

Conversion:


Important Functionality and Algorithms

Delta Application and Reset Logic (NodeState::prepare_apply_delta and apply_delta)

Garbage Collection of Tombstones (gc_keys_marked_for_deletion)

Partial Delta Computation with MTU Consideration (ClusterState::compute_partial_delta_respecting_mtu)

Staleness Score Calculation (staleness_score)


Interaction with Other Modules


Visual Diagram: Class Structure and Relationships

classDiagram
class NodeState {
-chitchat_id: ChitchatId
-heartbeat: Heartbeat
-key_values: BTreeMap<String, VersionedValue>
-listeners: Listeners
-max_version: Version
-last_gc_version: Version
+new()
+set()
+set_with_ttl()
+delete()
+delete_after_ttl()
+apply_delta()
+prepare_apply_delta()
+gc_keys_marked_for_deletion()
+get()
+get_versioned()
+contains_key()
+iter_prefix()
}
class ClusterState {
-node_states: BTreeMap<ChitchatId, NodeState>
-seed_addrs: watch::Receiver<HashSet<SocketAddr>>
-listeners: Listeners
+with_seed_addrs()
+node_state_mut()
+node_state()
+apply_delta()
+compute_digest()
+compute_partial_delta_respecting_mtu()
+gc_keys_marked_for_deletion()
}
class Staleness {
-is_unknown: bool
-max_version: u64
-num_stale_key_values: usize
+cmp()
}
class SortedStaleNodes {
-stale_nodes: BTreeMap<Staleness, Vec<StaleNode>>
+offer()
+into_iter()
}
class StaleNode {
-chitchat_id: &ChitchatId
-node_state: &NodeState
-from_version_excluded: u64
+stale_key_values()
}
class ClusterStateSnapshot {
-node_states: Vec<NodeState>
-seed_addrs: HashSet<SocketAddr>
}
ClusterState "1" o-- "many" NodeState : manages
SortedStaleNodes "1" o-- "many" StaleNode : contains
StaleNode "1" --> NodeState : references
NodeState ..> Listeners : uses
ClusterState ..> Listeners : uses
ClusterStateSnapshot ..> ClusterState : created from

Notes on Key Implementation Details


Additional Details