Skip to content

Commit

Permalink
refactor: reimplement curp client state
Browse files Browse the repository at this point in the history
Signed-off-by: bsbds <[email protected]>
  • Loading branch information
bsbds committed Oct 9, 2024
1 parent fd409a5 commit d3ba151
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
47 changes: 47 additions & 0 deletions crates/curp/src/client/unary/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use std::time::Duration;

use tonic::transport::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<ServerId>,
/// Client tls config
tls_config: Option<ClientTlsConfig>,
/// 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<ServerId> {
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()
}
}
8 changes: 8 additions & 0 deletions crates/curp/src/client/unary/mod.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
46 changes: 46 additions & 0 deletions crates/curp/src/client/unary/state.rs
Original file line number Diff line number Diff line change
@@ -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<ServerId, Arc<dyn ConnectApi>>,
}

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<ServerId, Arc<dyn ConnectApi>>,
) {
self.cluster_version = cluster_version;
self.connects = connects;
}
}

0 comments on commit d3ba151

Please sign in to comment.