Skip to main content

crafty_proto/
client.rs

1//! Client API wire types sent over `/client/wire` (client-api, client-routing, read-consistency).
2
3use serde::{Deserialize, Serialize};
4
5use crate::{LogIndex, NodeId, Term};
6
7/// A request from a client to the cluster.
8#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
9pub enum ClientRequest {
10    /// A write: application-encoded command replicated through the Raft log.
11    Propose(Vec<u8>),
12    /// A linearizable read: application-encoded query answered via `ReadIndex`.
13    Query(Vec<u8>),
14    /// A write routed to the Raft group owning `key` (multi-Raft, write-sharding-multi-raft).
15    ProposeKeyed {
16        /// Shard routing key (typically the same key the command mutates).
17        key: Vec<u8>,
18        /// Application-encoded command body.
19        command: Vec<u8>,
20    },
21    /// A linearizable read routed to the Raft group owning `key`.
22    QueryKeyed {
23        /// Shard routing key.
24        key: Vec<u8>,
25        /// Application-encoded query body.
26        query: Vec<u8>,
27    },
28    /// Ask the leader to confirm a linearizable read index without executing a
29    /// query (etcd-style follower read setup, read-consistency).
30    ReadIndexConfirm {
31        /// Shard routing key for multi-Raft; `None` targets group 0.
32        route_key: Option<Vec<u8>>,
33    },
34    /// Stage a command in leader memory for cross-shard 2PC (optional Tier 2).
35    TwoPhasePrepare {
36        /// Shared transaction id.
37        tx_id: Vec<u8>,
38        /// Shard routing key.
39        key: Vec<u8>,
40        /// Application-encoded command to commit later.
41        command: Vec<u8>,
42    },
43    /// Commit a previously prepared command through the normal Raft log.
44    TwoPhaseCommit {
45        /// Shared transaction id.
46        tx_id: Vec<u8>,
47        /// Shard routing key.
48        key: Vec<u8>,
49    },
50    /// Drop a previously prepared command without committing.
51    TwoPhaseAbort {
52        /// Shared transaction id.
53        tx_id: Vec<u8>,
54        /// Shard routing key.
55        key: Vec<u8>,
56    },
57}
58
59/// The cluster's response to a [`ClientRequest`].
60#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
61pub enum ClientResponse {
62    /// Success with an application-encoded result body.
63    Ok(Vec<u8>),
64    /// `ReadIndex` confirmed at `index` in `term` (response to
65    /// [`ClientRequest::ReadIndexConfirm`]).
66    ReadIndexConfirmed {
67        /// The linearizable read barrier index.
68        index: LogIndex,
69        /// The leader term that confirmed the read.
70        term: Term,
71    },
72    /// The contacted node is not the leader (transparent forward usually
73    /// hides this; the hint aids clients that route themselves).
74    NotLeader {
75        /// Best-known current leader, if any.
76        leader: Option<NodeId>,
77    },
78    /// A processing error, human-readable.
79    Error(String),
80}