From 9156c213dadd56e956c2ce7064e159560ad6a0d5 Mon Sep 17 00:00:00 2001 From: Jay Lee Date: Mon, 29 Apr 2019 16:34:31 +0800 Subject: [PATCH 1/4] introduce StatusRef to reduce clone --- src/lib.rs | 4 ++-- src/raw_node.rs | 9 ++++++++- src/status.rs | 36 +++++++++++++++++++++++++++++++++++- 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4c99d8603..412fca4cd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -305,7 +305,7 @@ pub use self::raft::{ pub use self::raft_log::{RaftLog, NO_LIMIT}; pub use self::raw_node::{is_empty_snap, Peer, RawNode, Ready, SnapshotStatus}; pub use self::read_only::{ReadOnlyOption, ReadState}; -pub use self::status::Status; +pub use self::status::{Status, StatusRef}; pub use self::storage::{RaftState, Storage}; pub mod prelude { @@ -335,7 +335,7 @@ pub mod prelude { pub use progress::Progress; - pub use status::Status; + pub use status::{Status, StatusRef}; pub use read_only::{ReadOnlyOption, ReadState}; } diff --git a/src/raw_node.rs b/src/raw_node.rs index 09f4fb625..ada7b533b 100644 --- a/src/raw_node.rs +++ b/src/raw_node.rs @@ -41,7 +41,7 @@ use protobuf::{self, RepeatedField}; use super::config::Config; use super::errors::{Error, Result}; use super::read_only::ReadState; -use super::Status; +use super::{Status, StatusRef}; use super::Storage; use super::{Raft, SoftState, INVALID_ID}; @@ -423,6 +423,13 @@ impl RawNode { Status::new(&self.raft) } + /// Returns the current status of the given group. + /// + /// It's borrows the internal progress set instead of copying. + pub fn status_ref(&self) -> StatusRef { + StatusRef::new(&self.raft) + } + /// ReportUnreachable reports the given node is not reachable for the last send. pub fn report_unreachable(&mut self, id: u64) { let mut m = Message::new(); diff --git a/src/status.rs b/src/status.rs index 9851971cd..54536eb6c 100644 --- a/src/status.rs +++ b/src/status.rs @@ -28,7 +28,7 @@ use eraftpb::HardState; use fxhash::FxHashMap; -use progress::Progress; +use progress::{Progress, ProgressSet}; use raft::{Raft, SoftState, StateRole}; use storage::Storage; @@ -66,3 +66,37 @@ impl Status { s } } + +/// Represents the current status of the raft +#[derive(Default)] +pub struct StatusRef<'a> { + /// The ID of the current node. + pub id: u64, + /// The hardstate of the raft, representing voted state. + pub hs: HardState, + /// The softstate of the raft, representing proposed state. + pub ss: SoftState, + /// The index of the last entry to have been applied. + pub applied: u64, + /// The progress towards catching up and applying logs. + pub progress: Option<&'a ProgressSet>, +} + +impl<'a> StatusRef<'a> { + /// Gets the current raft status. + pub fn new(raft: &'a Raft) -> StatusRef<'a> { + let mut s = StatusRef { + id: raft.id, + ..Default::default() + }; + s.hs = raft.hard_state(); + s.ss = raft.soft_state(); + s.applied = raft.raft_log.get_applied(); + if s.ss.raft_state == StateRole::Leader { + s.progress = Some(raft.prs()); + } + s + } +} + + From 0bb91aaa2af27f3773eb12cd44ab80333ddee6df Mon Sep 17 00:00:00 2001 From: Jay Lee Date: Tue, 23 Apr 2019 17:26:53 +0800 Subject: [PATCH 2/4] add ping --- src/raft.rs | 7 +++++++ src/raw_node.rs | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/src/raft.rs b/src/raft.rs index 307c058a6..3928d0ec2 100644 --- a/src/raft.rs +++ b/src/raft.rs @@ -566,6 +566,13 @@ impl Raft { self.set_prs(prs); } + /// Broadcasts heartbeats to all the followers if it's leader. + pub fn ping(&mut self) { + if self.state == StateRole::Leader { + self.bcast_heartbeat(); + } + } + /// Sends RPC, without entries to all the peers. pub fn bcast_heartbeat(&mut self) { let ctx = self.read_only.last_pending_request_ctx(); diff --git a/src/raw_node.rs b/src/raw_node.rs index ada7b533b..20d552618 100644 --- a/src/raw_node.rs +++ b/src/raw_node.rs @@ -286,6 +286,13 @@ impl RawNode { self.raft.step(m) } + /// Broadcast heartbeats to all the followers. + /// + /// If it's not leader, nothing will happen. + pub fn ping(&mut self) { + self.raft.ping() + } + /// ProposeConfChange proposes a config change. #[cfg_attr(feature = "cargo-clippy", allow(clippy::needless_pass_by_value))] pub fn propose_conf_change(&mut self, context: Vec, cc: ConfChange) -> Result<()> { From 2b8c1e3c22b4aba6bf6e05932bea26681ee14853 Mon Sep 17 00:00:00 2001 From: Jay Lee Date: Mon, 29 Apr 2019 16:42:01 +0800 Subject: [PATCH 3/4] remove redundant empty lines --- src/status.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/status.rs b/src/status.rs index 54536eb6c..15f020696 100644 --- a/src/status.rs +++ b/src/status.rs @@ -98,5 +98,3 @@ impl<'a> StatusRef<'a> { s } } - - From 4564d2f89531beb73f8c95b885edfb9cf6b3084f Mon Sep 17 00:00:00 2001 From: Jay Lee Date: Mon, 29 Apr 2019 18:14:37 +0800 Subject: [PATCH 4/4] format code --- src/raw_node.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/raw_node.rs b/src/raw_node.rs index 20d552618..94da81d85 100644 --- a/src/raw_node.rs +++ b/src/raw_node.rs @@ -41,9 +41,9 @@ use protobuf::{self, RepeatedField}; use super::config::Config; use super::errors::{Error, Result}; use super::read_only::ReadState; -use super::{Status, StatusRef}; use super::Storage; use super::{Raft, SoftState, INVALID_ID}; +use super::{Status, StatusRef}; /// Represents a Peer node in the cluster. #[derive(Debug, Default)] @@ -431,7 +431,7 @@ impl RawNode { } /// Returns the current status of the given group. - /// + /// /// It's borrows the internal progress set instead of copying. pub fn status_ref(&self) -> StatusRef { StatusRef::new(&self.raft)