message.rs

Overview

This file implements the ChitchatMessage enum and associated serialization and deserialization logic for the gossip protocol messages used in a distributed system's cluster communication. The messages represent different phases of a gossip handshake inspired by TCP handshake semantics (SYN, SYN-ACK, ACK). The file defines message variants, protocol and message type enums, and methods for encoding and decoding these messages to/from byte buffers. It also includes tests verifying correct serialization and deserialization behavior.

Enumerations and Structs

ChitchatMessage

An enum representing the set of possible gossip messages exchanged between nodes A and B during the handshake:

Fields:

Usage Example:

let syn = ChitchatMessage::Syn {
    cluster_id: "cluster-1".to_string(),
    digest: some_digest,
};

Digest and Delta represent compact state summaries and updates, respectively, and are crucial for efficient gossip state synchronization.


ProtocolVersion

An enum representing the protocol version currently supported.

Methods:


MessageType

An enum representing the message types corresponding to variants of ChitchatMessage with explicit codes:

Variant

Code

Syn

0

SynAck

1

Ack

2

BadCluster

3

Methods:


Traits Implemented

Serializable for ChitchatMessage

Enables serialization of messages into byte buffers.


Deserializable for ChitchatMessage

Enables reconstructing a ChitchatMessage from a byte slice.

Errors during deserialization are returned as anyhow::Error with context.


Constants


Implementation Details and Algorithms


Interaction with Other Components


Tests


Mermaid Class Diagram

classDiagram
class ChitchatMessage {
<<enum>>
+Syn
+SynAck
+Ack
+BadCluster
+serialize()
+serialized_len()
+deserialize()
}
class ProtocolVersion {
<<enum>>
+from_code()
+to_code()
}
class MessageType {
<<enum>>
+from_code()
+to_code()
}
ChitchatMessage ..> ProtocolVersion : uses
ChitchatMessage ..> MessageType : uses
ChitchatMessage ..> Digest : contains
ChitchatMessage ..> Delta : contains

This diagram illustrates the main enums and their relationships: ChitchatMessage depends on ProtocolVersion and MessageType for message encoding, and contains or references Digest and Delta for state data.


Summary of Key Methods

Method

Description

Parameters

Returns

serialize(&self, buf: &mut Vec<u8>)

Serializes a ChitchatMessage into the given buffer.

Mutable byte vector

()

serialized_len(&self) -> usize

Calculates length in bytes of serialized message.

None

Size in bytes

deserialize(buf: &mut &[u8]) -> anyhow::Result<Self>

Deserializes a message from a byte slice, validating format.

Mutable byte slice

ChitchatMessage or error


Important Notes


For further details on related topics, see Digest, Delta, and Serialization.