types.rs

Overview

This file defines core types and data structures essential for managing node identities, versioned key-value pairs, deletion statuses, and mutations within a distributed system cluster. It provides serialization and deserialization capabilities, versioning semantics, and status tracking to support consistent state synchronization in a gossip-based or cluster-aware environment.

The types herein are foundational for cluster membership identification, key-value state versioning, and mutation representation, facilitating communication and state sharing between nodes.


Structs and Enums

ChitchatId

Represents a unique identifier for a node in the cluster. It encapsulates three components:

pub struct ChitchatId {
    pub node_id: String,
    pub generation_id: u64,
    pub gossip_advertise_addr: SocketAddr,
}

Key Details

Methods


DeletionStatus

An enum representing the deletion state of a key-value pair.

pub enum DeletionStatus {
    Set,
    Deleted(Instant),
    DeleteAfterTtl(Instant),
}

Important Note

The stored Instant is not the scheduled deletion time but a reference start time; the actual deletion time is obtained by adding the grace period.

Methods


VersionedValue

A key-value pair with an associated version and deletion status.

pub struct VersionedValue {
    pub value: String,
    pub version: Version,
    pub status: DeletionStatus,
}

Methods

Serialization

VersionedValue uses a custom serialization wrapper struct VersionedValueForSerialization that serializes the deletion status as a DeletionStatusMutation enum, which can result in some loss of TTL precision on deserialization.


KeyValueMutation

Represents a mutation operation to a key-value pair.

pub(crate) struct KeyValueMutation {
    pub(crate) key: String,
    pub(crate) value: String,
    pub(crate) version: Version,
    pub(crate) status: DeletionStatusMutation,
}

DeletionStatusMutation

A compact enum representing mutation states related to deletion, serialized as a u8.

pub enum DeletionStatusMutation {
    Set = 0u8,
    Delete = 1u8,
    DeleteAfterTtl = 2u8,
}

Methods


KeyValueMutationRef<'a>

A reference version of KeyValueMutation for zero-copy serialization.

pub(crate) struct KeyValueMutationRef<'a> {
    pub(crate) key: &'a str,
    pub(crate) value: &'a str,
    pub(crate) version: Version,
    pub(crate) state: DeletionStatusMutation,
}

VersionedValueForSerialization

An internal helper struct for serialization/deserialization of VersionedValue.

struct VersionedValueForSerialization {
    pub value: String,
    pub version: Version,
    pub status: DeletionStatusMutation,
}

Type Alias: Version

A type alias for the version number of a key, defined as u64.

pub type Version = u64;

Heartbeat

Represents a node's heartbeat value.

pub struct Heartbeat(pub(crate) u64);

Serialization and Deserialization

The file leverages the serde crate for serialization and deserialization of most structs and enums. Additionally, it defines custom traits Serializable and Deserializable (from other modules) to provide binary serialization for network transmission.


Interaction with Other System Parts


Important Implementation Details


Usage Examples

Creating a Node Identifier

use std::net::SocketAddr;

let addr: SocketAddr = "192.168.1.10:9000".parse().unwrap();
let node_id = ChitchatId::new("node-123".to_string(), 1, addr);
println!("{:?}", node_id); // Prints: node-123:1:192.168.1.10:9000

Creating a Versioned Value Marked as Deleted

use tokio::time::Instant;

let version = 10u64;
let val = VersionedValue::new("some data".to_string(), version, true);
assert!(val.is_deleted());

Serializing a KeyValueMutationRef

let mutation = KeyValueMutation {
    key: "key1".to_string(),
    value: "value1".to_string(),
    version: 7,
    status: DeletionStatusMutation::Set,
};

let mutation_ref: KeyValueMutationRef = (&mutation).into();
let mut buf = Vec::new();
mutation_ref.serialize(&mut buf);

Mermaid Diagram

classDiagram
class ChitchatId {
+node_id: String
+generation_id: u64
+gossip_advertise_addr: SocketAddr
+new()
}
class DeletionStatus {
<<enum>>
+Set
+Deleted
+DeleteAfterTtl
+time_of_start_scheduled_for_deletion()
}
class VersionedValue {
+value: String
+version: Version
+status: DeletionStatus
+new()
+is_deleted()
}
class KeyValueMutation {
+key: String
+value: String
+version: Version
+status: DeletionStatusMutation
}
class DeletionStatusMutation {
<<enum>>
+Set
+Delete
+DeleteAfterTtl
+into_status()
+scheduled_for_deletion()
}
class KeyValueMutationRef {
+key: &str
+value: &str
+version: Version
+state: DeletionStatusMutation
+serialize()
}
class Heartbeat {
+u64
+inc()
}
ChitchatId --> SocketAddr
VersionedValue --> DeletionStatus
KeyValueMutation --> DeletionStatusMutation
KeyValueMutationRef --> DeletionStatusMutation
VersionedValue --|> VersionedValueForSerialization
DeletionStatusMutation --|> u8
Heartbeat o-- u64

This diagram depicts the main structs and enums, their properties, and key methods, as well as their relationship to each other and primitive types. It highlights the core data structures used for node identification, state versioning, mutation, and deletion status management.