-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: reimplement curp client state
Signed-off-by: bsbds <[email protected]>
- Loading branch information
Showing
3 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |