CausalReplica

Struct CausalReplica 

Source
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:

  1. Tracking per-peer delta intervals
  2. Only accepting deltas in causal order
  3. 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>

Source

pub fn new(id: impl Into<ReplicaId>) -> Self

Create a new causal replica

Source

pub fn restore(durable: DurableState<S>) -> Self

Restore from durable state (after crash)

Source

pub fn id(&self) -> &ReplicaId

Get the replica ID

Source

pub fn state(&self) -> &S

Get current state (read-only)

Source

pub fn counter(&self) -> SeqNo

Get the durable counter (sequence number)

Source

pub fn durable_state(&self) -> &DurableState<S>

Get durable state for persistence

Source

pub fn register_peer(&mut self, peer_id: ReplicaId)

Register a peer for causal anti-entropy

Source

pub fn mutate<F>(&mut self, mutator: F) -> S
where F: FnOnce(&S) -> S,

Apply a local mutation

Algorithm 2, step 1:

cᵢ := cᵢ + 1
d := mδ(Xᵢ)
Xᵢ := Xᵢ ⊔ d
∀j: Dᵢ[j] := Dᵢ[j] ⊔ d

Returns the computed delta

Source

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.

Source

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

Source

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 later

Returns Some(IntervalAck) if the interval was applied (causally ready), or None if it was buffered for later.

Source

fn try_apply_pending(&mut self, peer_id: &str) -> Vec<IntervalAck>

Try to apply pending intervals that are now causally ready

Source

pub fn receive_ack(&mut self, ack: &IntervalAck)

Process an acknowledgment from a peer

Algorithm 2, step 4:

Dᵢ[j] := ⊥   // clear delta buffer for j
Source

pub fn snapshot(&self) -> (S, SeqNo)

Get a full state snapshot for bootstrapping

Source

pub fn apply_snapshot(&mut self, state: S, seq: SeqNo, from: &str)

Apply a snapshot from another replica (for bootstrapping)

Source

pub fn peers(&self) -> impl Iterator<Item = &ReplicaId>

Get all registered peer IDs

Source

pub fn has_pending_deltas(&self) -> bool

Check if we have pending deltas for any peer

Source

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>

Source§

fn clone(&self) -> CausalReplica<S>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<S: Debug + Lattice + Clone> Debug for CausalReplica<S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<S> Freeze for CausalReplica<S>
where S: Freeze,

§

impl<S> RefUnwindSafe for CausalReplica<S>
where S: RefUnwindSafe,

§

impl<S> Send for CausalReplica<S>
where S: Send,

§

impl<S> Sync for CausalReplica<S>
where S: Sync,

§

impl<S> Unpin for CausalReplica<S>
where S: Unpin,

§

impl<S> UnwindSafe for CausalReplica<S>
where S: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V