pub struct CausalReplica<S: Lattice + Clone> {
durable: DurableState<S>,
volatile: VolatileState<S>,
pending: HashMap<ReplicaId, VecDeque<DeltaInterval<S>>>,
}Expand description
A causal δ-CRDT replica implementing Algorithm 2
Provides causal consistency guarantees by:
- Tracking per-peer delta intervals
- Only accepting deltas in causal order
- Supporting crash recovery via durable state
Fields§
§durable: DurableState<S>Durable state (survives crashes)
volatile: VolatileState<S>Volatile state (lost on crash)
pending: HashMap<ReplicaId, VecDeque<DeltaInterval<S>>>Pending deltas waiting for causal predecessors
Implementations§
Source§impl<S: Lattice + Clone> CausalReplica<S>
impl<S: Lattice + Clone> CausalReplica<S>
Sourcepub fn restore(durable: DurableState<S>) -> Self
pub fn restore(durable: DurableState<S>) -> Self
Restore from durable state (after crash)
Sourcepub fn durable_state(&self) -> &DurableState<S>
pub fn durable_state(&self) -> &DurableState<S>
Get durable state for persistence
Sourcepub fn register_peer(&mut self, peer_id: ReplicaId)
pub fn register_peer(&mut self, peer_id: ReplicaId)
Register a peer for causal anti-entropy
Sourcepub fn mutate<F>(&mut self, mutator: F) -> S
pub fn mutate<F>(&mut self, mutator: F) -> S
Apply a local mutation
Algorithm 2, step 1:
cᵢ := cᵢ + 1
d := mδ(Xᵢ)
Xᵢ := Xᵢ ⊔ d
∀j: Dᵢ[j] := Dᵢ[j] ⊔ dReturns the computed delta
Sourcepub fn prepare_interval(&mut self, peer_id: &str) -> Option<DeltaInterval<S>>
pub fn prepare_interval(&mut self, peer_id: &str) -> Option<DeltaInterval<S>>
Prepare a delta-interval to send to a peer
Returns Some(DeltaInterval) if there are pending deltas for this peer,
or None if the buffer is empty.
Sourcefn is_causally_ready(&self, interval: &DeltaInterval<S>) -> bool
fn is_causally_ready(&self, interval: &DeltaInterval<S>) -> bool
Check if a delta-interval is causally ready
A delta-interval is ready if its from_seq matches our last acked seq from that peer
Sourcepub fn receive_interval(
&mut self,
interval: DeltaInterval<S>,
) -> Option<IntervalAck>
pub fn receive_interval( &mut self, interval: DeltaInterval<S>, ) -> Option<IntervalAck>
Receive a delta-interval from a peer
Algorithm 2, step 3:
if n = Aᵢ[j] + 1 then // causally ready
Xᵢ := Xᵢ ⊔ d
Aᵢ[j] := m
send ack(m) to j
else
buffer for laterReturns Some(IntervalAck) if the interval was applied (causally ready),
or None if it was buffered for later.
Sourcefn try_apply_pending(&mut self, peer_id: &str) -> Vec<IntervalAck>
fn try_apply_pending(&mut self, peer_id: &str) -> Vec<IntervalAck>
Try to apply pending intervals that are now causally ready
Sourcepub fn receive_ack(&mut self, ack: &IntervalAck)
pub fn receive_ack(&mut self, ack: &IntervalAck)
Process an acknowledgment from a peer
Algorithm 2, step 4:
Dᵢ[j] := ⊥ // clear delta buffer for jSourcepub fn apply_snapshot(&mut self, state: S, seq: SeqNo, from: &str)
pub fn apply_snapshot(&mut self, state: S, seq: SeqNo, from: &str)
Apply a snapshot from another replica (for bootstrapping)
Sourcepub fn has_pending_deltas(&self) -> bool
pub fn has_pending_deltas(&self) -> bool
Check if we have pending deltas for any peer
Sourcepub fn pending_count(&self) -> usize
pub fn pending_count(&self) -> usize
Count of pending out-of-order intervals
Trait Implementations§
Source§impl<S: Clone + Lattice + Clone> Clone for CausalReplica<S>
impl<S: Clone + Lattice + Clone> Clone for CausalReplica<S>
Source§fn clone(&self) -> CausalReplica<S>
fn clone(&self) -> CausalReplica<S>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more