lib.rs

Overview

This file implements a gossip-based cluster membership and state synchronization service using the chitchat crate. It provides:

The core functionality revolves around maintaining and exposing the cluster state via the Chitchat protocol, which enables nodes to gossip their state to each other. This file bridges the Chitchat cluster with a RESTful API for external interaction.


Structs and Types

ApiResponse

pub struct ApiResponse {
    pub cluster_id: String,
    pub cluster_state: ClusterStateSnapshot,
    pub live_nodes: Vec<ChitchatId>,
    pub dead_nodes: Vec<ChitchatId>,
}

SetKeyValueResponse

pub struct SetKeyValueResponse {
    pub status: bool,
}

Api

struct Api {
    chitchat: ChitchatRef,
}

GossipConfig

pub struct GossipConfig {
    pub listen_addr: SocketAddr,
    pub advertise_addr: Option<SocketAddr>,
    pub seeds: Vec<SocketAddr>,
    pub cluster_id: String,
}

Functions and Methods

Api::index

async fn index(&self) -> Json<serde_json::Value>

Usage example:

Request:

GET http://<node_address>/

Response:

{
  "cluster_id": "acki_nacki",
  "cluster_state": { /* snapshot data */ },
  "live_nodes": ["server:127.0.0.1:10000-xyz"],
  "dead_nodes": []
}

Api::set_kv

async fn set_kv(&self, key: Query<String>, value: Query<String>) -> Json<serde_json::Value>

Usage example:

Request:

GET http://<node_address>/set_kv/?key=foo&value=bar

Response:

{
  "status": true
}

generate_server_id

fn generate_server_id(public_addr: SocketAddr) -> String

GossipConfig::advertise_addr

pub fn advertise_addr(&self) -> SocketAddr

default_gossip_listen_addr

fn default_gossip_listen_addr() -> SocketAddr

default_chitchat_cluster_id

fn default_chitchat_cluster_id() -> String

run

pub async fn run(
    _shutdown_rx: tokio::sync::watch::Receiver<bool>,
    config_rx: tokio::sync::watch::Receiver<GossipConfig>,
    transport: impl chitchat::transport::Transport,
) -> anyhow::Result<(ChitchatHandle, JoinHandle<anyhow::Result<()>>)>

Implementation Details and Algorithms


Interaction with Other System Components

Together, these dependencies enable this module to serve as a networked cluster membership service with a RESTful interface.


Visual Diagram

flowchart TD
Run["run()"]
Run --> Config["Load GossipConfig"]
Run --> GenID["Generate ChitchatId"]
Run --> ChitchatConfig["Build ChitchatConfig"]
Run --> SpawnChitchat["spawn_chitchat()"]
SpawnChitchat --> ChitchatHandle["ChitchatHandle"]
ChitchatHandle --> Api["Api Struct (holds ChitchatRef)"]
Api --> OpenApiService["OpenApiService"]
OpenApiService --> RESTServer["REST Server (poem)"]
RESTServer --> TcpListener["TcpListener binds listen_addr"]
RESTServer --> ServeEndpoints["Serve / and /set_kv endpoints"]
subgraph REST API Endpoints
ServeEndpoints --> Index["GET / (index)"]
ServeEndpoints --> SetKV["GET /set_kv/ (set_kv)"]
end

References