|
| 1 | +use std::collections::BTreeMap; |
| 2 | +use std::collections::BTreeSet; |
| 3 | + |
| 4 | +use openraft::BasicNode; |
| 5 | +use openraft::ReadPolicy; |
| 6 | + |
| 7 | +use crate::app::App; |
| 8 | +use crate::decode; |
| 9 | +use crate::encode; |
| 10 | +use crate::typ::*; |
| 11 | +use crate::NodeId; |
| 12 | + |
| 13 | +/// Write a key-value pair to the group's state machine |
| 14 | +pub async fn write(app: &mut App, req: String) -> String { |
| 15 | + let res = app.raft.client_write(decode(&req)).await; |
| 16 | + encode(res) |
| 17 | +} |
| 18 | + |
| 19 | +/// Read a value from the group's state machine using linearizable read |
| 20 | +pub async fn read(app: &mut App, req: String) -> String { |
| 21 | + let key: String = decode(&req); |
| 22 | + |
| 23 | + let ret = app.raft.get_read_linearizer(ReadPolicy::ReadIndex).await; |
| 24 | + |
| 25 | + let res = match ret { |
| 26 | + Ok(linearizer) => { |
| 27 | + linearizer.await_ready(&app.raft).await.unwrap(); |
| 28 | + |
| 29 | + let state_machine = app.state_machine.state_machine.lock().await; |
| 30 | + let value = state_machine.data.get(&key).cloned(); |
| 31 | + |
| 32 | + let res: Result<String, RaftError<LinearizableReadError>> = Ok(value.unwrap_or_default()); |
| 33 | + res |
| 34 | + } |
| 35 | + Err(e) => Err(e), |
| 36 | + }; |
| 37 | + encode(res) |
| 38 | +} |
| 39 | + |
| 40 | +// ============================================================================ |
| 41 | +// Raft Protocol API |
| 42 | +// ============================================================================ |
| 43 | + |
| 44 | +/// Handle vote request |
| 45 | +pub async fn vote(app: &mut App, req: String) -> String { |
| 46 | + let res = app.raft.vote(decode(&req)).await; |
| 47 | + encode(res) |
| 48 | +} |
| 49 | + |
| 50 | +/// Handle append entries request |
| 51 | +pub async fn append(app: &mut App, req: String) -> String { |
| 52 | + let res = app.raft.append_entries(decode(&req)).await; |
| 53 | + encode(res) |
| 54 | +} |
| 55 | + |
| 56 | +/// Receive a snapshot and install it |
| 57 | +pub async fn snapshot(app: &mut App, req: String) -> String { |
| 58 | + let (vote, snapshot_meta, snapshot_data): (Vote, SnapshotMeta, SnapshotData) = decode(&req); |
| 59 | + let snapshot = Snapshot { |
| 60 | + meta: snapshot_meta, |
| 61 | + snapshot: snapshot_data, |
| 62 | + }; |
| 63 | + let res = app.raft.install_full_snapshot(vote, snapshot).await.map_err(RaftError::<Infallible>::Fatal); |
| 64 | + encode(res) |
| 65 | +} |
| 66 | + |
| 67 | +// ============================================================================ |
| 68 | +// Management API |
| 69 | +// ============================================================================ |
| 70 | + |
| 71 | +/// Add a node as **Learner** to this group. |
| 72 | +/// |
| 73 | +/// This should be done before adding a node as a member into the cluster |
| 74 | +/// (by calling `change-membership`) |
| 75 | +pub async fn add_learner(app: &mut App, req: String) -> String { |
| 76 | + let node_id: NodeId = decode(&req); |
| 77 | + let node = BasicNode { addr: "".to_string() }; |
| 78 | + let res = app.raft.add_learner(node_id, node, true).await; |
| 79 | + encode(res) |
| 80 | +} |
| 81 | + |
| 82 | +/// Changes specified learners to members, or remove members from this group. |
| 83 | +pub async fn change_membership(app: &mut App, req: String) -> String { |
| 84 | + let node_ids: BTreeSet<NodeId> = decode(&req); |
| 85 | + let res = app.raft.change_membership(node_ids, false).await; |
| 86 | + encode(res) |
| 87 | +} |
| 88 | + |
| 89 | +/// Initialize a single-node cluster for this group. |
| 90 | +pub async fn init(app: &mut App) -> String { |
| 91 | + let mut nodes = BTreeMap::new(); |
| 92 | + nodes.insert(app.node_id, BasicNode { addr: "".to_string() }); |
| 93 | + let res = app.raft.initialize(nodes).await; |
| 94 | + encode(res) |
| 95 | +} |
| 96 | + |
| 97 | +/// Get the latest metrics of this Raft group |
| 98 | +pub async fn metrics(app: &mut App) -> String { |
| 99 | + let metrics = app.raft.metrics().borrow().clone(); |
| 100 | + |
| 101 | + let res: Result<RaftMetrics, Infallible> = Ok(metrics); |
| 102 | + encode(res) |
| 103 | +} |
0 commit comments