digest.rs

Overview

This file defines data structures and serialization logic for maintaining and exchanging a digest that summarizes the staleness and versioning state of nodes in a distributed system. The digest essentially provides a snapshot of each peer node's heartbeat and version metadata, which is used to track freshness and garbage collection progress in the system.

The main components of this file are:

This functionality is critical for the system's peer-to-peer synchronization and consistency protocols, as it allows nodes to exchange summarized state to detect outdated or missing data.

Data Structures and Their Details

NodeDigest

pub(crate) struct NodeDigest {
    pub(crate) heartbeat: Heartbeat,
    pub(crate) last_gc_version: Version,
    pub(crate) max_version: Version,
}

Traits Implemented

Serialization Details

Example Usage

let node_digest = NodeDigest {
    heartbeat: Heartbeat(123),
    last_gc_version: 10,
    max_version: 20,
};
let mut buffer = Vec::new();
node_digest.serialize(&mut buffer);

Digest

pub struct Digest {
    pub(crate) node_digests: BTreeMap<ChitchatId, NodeDigest>,
}

Key Methods

Serialization and Compression Workflow

Example Usage

let mut digest = Digest::default();
digest.add_node(
    ChitchatId::for_local_test(1001),
    Heartbeat(50),
    5,
    10,
);
let mut buffer = Vec::new();
digest.serialize(&mut buffer);

Important Implementation Details and Algorithms

Interaction with Other System Components

This file primarily serves as a building block for the synchronization or gossip protocol layer, where nodes exchange their state digests to detect discrepancies or updates needed.


Mermaid Diagram: Structure of digest.rs

classDiagram
class NodeDigest {
+heartbeat: Heartbeat
+last_gc_version: Version
+max_version: Version
+serialize()
+serialized_len()
+deserialize()
}
class Digest {
-node_digests: BTreeMap<ChitchatId, NodeDigest>
+add_node()
-serialize_uncompressed()
-deserialize_uncompressed()
-serialize_compressed()
-deserialize_compressed()
+serialize()
+serialized_len()
+deserialize()
}
NodeDigest <|-- Digest : contains

This diagram illustrates the two primary structs: NodeDigest encapsulates the state of a single node, and Digest manages a collection of these, along with serialization methods supporting both compressed and uncompressed formats.