use super::common::UniqueId; use parking_lot::Mutex; use ruaft::{Persister, Raft, RpcClient}; use std::collections::HashMap; use std::sync::atomic::AtomicBool; use std::sync::mpsc::{channel, Receiver}; use std::sync::Arc; struct KVServer { state: Mutex, rf: Raft, command_channel: Receiver<(usize, KVOp)>, shutdown: AtomicBool, // snapshot } #[derive(Clone, Default, Serialize, Deserialize)] struct KVOp { unique_id: UniqueId, } #[derive(Default)] struct KVServerState { kv: HashMap, debug_kv: HashMap, applied_op: HashMap, } impl KVServer { pub fn new( servers: Vec, me: usize, persister: Arc, ) -> Self { let (tx, rx) = channel(); let apply_command = move |index, command| { tx.send((index, command)) .expect("The receiving end of apply command channel should have not been dropped"); }; Self { state: Default::default(), rf: Raft::new( servers, me, persister, apply_command, None, Raft::::NO_SNAPSHOT, ), command_channel: rx, shutdown: AtomicBool::new(false), } } }