diff --git a/crates/curp/src/client/unary/config.rs b/crates/curp/src/client/unary/config.rs new file mode 100644 index 000000000..2dee1335d --- /dev/null +++ b/crates/curp/src/client/unary/config.rs @@ -0,0 +1,50 @@ +use std::time::Duration; + +#[cfg(not(madsim))] +use tonic::transport::ClientTlsConfig; +#[cfg(madsim)] +use utils::ClientTlsConfig; + +use crate::members::ServerId; + +/// Client config +#[derive(Debug, Clone)] +pub(crate) struct Config { + /// Local server id, should be initialized on startup + local_server: Option, + /// Client tls config + tls_config: Option, + /// The rpc timeout of a propose request + propose_timeout: Duration, + /// The rpc timeout of a 2-RTT request, usually takes longer than propose timeout + /// + /// The recommended the values is within (propose_timeout, 2 * propose_timeout]. + wait_synced_timeout: Duration, +} + +impl Config { + /// Get the local server id + pub(crate) fn local_server(&self) -> Option { + self.local_server + } + + /// Get the client TLS config + pub(crate) fn tls_config(&self) -> Option<&ClientTlsConfig> { + self.tls_config.as_ref() + } + + /// Get the propose timeout + pub(crate) fn propose_timeout(&self) -> Duration { + self.propose_timeout + } + + /// Get the wait synced timeout + pub(crate) fn wait_synced_timeout(&self) -> Duration { + self.wait_synced_timeout + } + + /// Returns `true` if the current client is on the server + pub(crate) fn is_raw_curp(&self) -> bool { + self.local_server.is_some() + } +} diff --git a/crates/curp/src/client/unary/mod.rs b/crates/curp/src/client/unary/mod.rs index 90986bdb7..743c7d35d 100644 --- a/crates/curp/src/client/unary/mod.rs +++ b/crates/curp/src/client/unary/mod.rs @@ -1,6 +1,14 @@ /// Client propose implementation mod propose_impl; +#[allow(unused)] +/// State of the unary client +mod state; + +#[allow(unused)] +/// Config of the client +mod config; + use std::{ cmp::Ordering, marker::PhantomData, diff --git a/crates/curp/src/client/unary/state.rs b/crates/curp/src/client/unary/state.rs new file mode 100644 index 000000000..c85994566 --- /dev/null +++ b/crates/curp/src/client/unary/state.rs @@ -0,0 +1,46 @@ +use std::{collections::HashMap, sync::Arc}; + +use crate::{members::ServerId, rpc::connect::ConnectApi}; + +/// The cluster state +/// +/// The client must discover the cluster info before sending any propose +struct ClusterState { + /// Leader id. + leader: ServerId, + /// Term, initialize to 0, calibrated by the server. + term: u64, + /// Cluster version, initialize to 0, calibrated by the server. + cluster_version: u64, + /// Members' connect, calibrated by the server. + connects: HashMap>, +} + +impl std::fmt::Debug for ClusterState { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("State") + .field("leader", &self.leader) + .field("term", &self.term) + .field("cluster_version", &self.cluster_version) + .field("connects", &self.connects.keys()) + .finish() + } +} + +impl ClusterState { + /// Updates the current leader + fn update_leader(&mut self, leader: ServerId, term: u64) { + self.leader = leader; + self.term = term; + } + + /// Updates the cluster + fn update_cluster( + &mut self, + cluster_version: u64, + connects: HashMap>, + ) { + self.cluster_version = cluster_version; + self.connects = connects; + } +}