crafty-proto 0.2.2

Wire types and postcard codec shared across the crafty framework.
Documentation
//! Client API wire types sent over `/client/wire` (client-api, client-routing, read-consistency).

use serde::{Deserialize, Serialize};

use crate::{LogIndex, NodeId, Term};

/// A request from a client to the cluster.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ClientRequest {
    /// A write: application-encoded command replicated through the Raft log.
    Propose(Vec<u8>),
    /// A linearizable read: application-encoded query answered via `ReadIndex`.
    Query(Vec<u8>),
    /// A write routed to the Raft group owning `key` (multi-Raft, write-sharding-multi-raft).
    ProposeKeyed {
        /// Shard routing key (typically the same key the command mutates).
        key: Vec<u8>,
        /// Application-encoded command body.
        command: Vec<u8>,
    },
    /// A linearizable read routed to the Raft group owning `key`.
    QueryKeyed {
        /// Shard routing key.
        key: Vec<u8>,
        /// Application-encoded query body.
        query: Vec<u8>,
    },
    /// Ask the leader to confirm a linearizable read index without executing a
    /// query (etcd-style follower read setup, read-consistency).
    ReadIndexConfirm {
        /// Shard routing key for multi-Raft; `None` targets group 0.
        route_key: Option<Vec<u8>>,
    },
    /// Stage a command in leader memory for cross-shard 2PC (optional Tier 2).
    TwoPhasePrepare {
        /// Shared transaction id.
        tx_id: Vec<u8>,
        /// Shard routing key.
        key: Vec<u8>,
        /// Application-encoded command to commit later.
        command: Vec<u8>,
    },
    /// Commit a previously prepared command through the normal Raft log.
    TwoPhaseCommit {
        /// Shared transaction id.
        tx_id: Vec<u8>,
        /// Shard routing key.
        key: Vec<u8>,
    },
    /// Drop a previously prepared command without committing.
    TwoPhaseAbort {
        /// Shared transaction id.
        tx_id: Vec<u8>,
        /// Shard routing key.
        key: Vec<u8>,
    },
}

/// The cluster's response to a [`ClientRequest`].
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ClientResponse {
    /// Success with an application-encoded result body.
    Ok(Vec<u8>),
    /// `ReadIndex` confirmed at `index` in `term` (response to
    /// [`ClientRequest::ReadIndexConfirm`]).
    ReadIndexConfirmed {
        /// The linearizable read barrier index.
        index: LogIndex,
        /// The leader term that confirmed the read.
        term: Term,
    },
    /// The contacted node is not the leader (transparent forward usually
    /// hides this; the hint aids clients that route themselves).
    NotLeader {
        /// Best-known current leader, if any.
        leader: Option<NodeId>,
    },
    /// A processing error, human-readable.
    Error(String),
}