Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: reimplement curp client state #981

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions crates/curp/src/client/unary/config.rs
Original file line number Diff line number Diff line change
@@ -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<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;
}
}
Loading