Skip to content

Commit f112f30

Browse files
BusyJayhicqu
authored andcommitted
Add more convenient lite-weight interfaces (tikv#227)
This PR introduces two simple and lite weight interfaces: - ping to trigger heartbeats without ticking, - status_ref to borrow the progress set instead of cloning.
1 parent 813fc4e commit f112f30

File tree

4 files changed

+43
-6
lines changed

4 files changed

+43
-6
lines changed

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ pub use self::raft::{vote_resp_msg_type, Raft, SoftState, StateRole, INVALID_ID,
406406
pub use self::raft_log::{RaftLog, NO_LIMIT};
407407
pub use self::raw_node::{is_empty_snap, Peer, RawNode, Ready, SnapshotStatus};
408408
pub use self::read_only::{ReadOnlyOption, ReadState};
409-
pub use self::status::Status;
409+
pub use self::status::{Status, StatusRef};
410410
pub use self::storage::{RaftState, Storage};
411411
pub use raft_proto::eraftpb;
412412
use slog::{Drain, Logger};
@@ -435,7 +435,7 @@ pub mod prelude {
435435

436436
pub use crate::progress::Progress;
437437

438-
pub use crate::status::Status;
438+
pub use crate::status::{Status, StatusRef};
439439

440440
pub use crate::read_only::{ReadOnlyOption, ReadState};
441441
}

src/raft.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -681,9 +681,7 @@ impl<T: Storage> Raft<T> {
681681
self.set_prs(prs);
682682
}
683683

684-
/// Broadcast heartbeats to all the followers.
685-
///
686-
/// If it's not leader, nothing will happen.
684+
/// Broadcasts heartbeats to all the followers if it's leader.
687685
pub fn ping(&mut self) {
688686
if self.state == StateRole::Leader {
689687
self.bcast_heartbeat();

src/raw_node.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use crate::eraftpb::{
4242
};
4343
use crate::errors::{Error, Result};
4444
use crate::read_only::ReadState;
45-
use crate::{Raft, SoftState, Status, Storage, INVALID_ID};
45+
use crate::{Raft, SoftState, Status, StatusRef, Storage, INVALID_ID};
4646
use slog::Logger;
4747

4848
/// Represents a Peer node in the cluster.
@@ -456,6 +456,13 @@ impl<T: Storage> RawNode<T> {
456456
Status::new(&self.raft)
457457
}
458458

459+
/// Returns the current status of the given group.
460+
///
461+
/// It's borrows the internal progress set instead of copying.
462+
pub fn status_ref(&self) -> StatusRef {
463+
StatusRef::new(&self.raft)
464+
}
465+
459466
/// ReportUnreachable reports the given node is not reachable for the last send.
460467
pub fn report_unreachable(&mut self, id: u64) {
461468
let mut m = Message::default();

src/status.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,35 @@ impl<'a> Status<'a> {
6262
s
6363
}
6464
}
65+
66+
/// Represents the current status of the raft
67+
#[derive(Default)]
68+
pub struct StatusRef<'a> {
69+
/// The ID of the current node.
70+
pub id: u64,
71+
/// The hardstate of the raft, representing voted state.
72+
pub hs: HardState,
73+
/// The softstate of the raft, representing proposed state.
74+
pub ss: SoftState,
75+
/// The index of the last entry to have been applied.
76+
pub applied: u64,
77+
/// The progress towards catching up and applying logs.
78+
pub progress: Option<&'a ProgressSet>,
79+
}
80+
81+
impl<'a> StatusRef<'a> {
82+
/// Gets the current raft status.
83+
pub fn new<T: Storage>(raft: &'a Raft<T>) -> StatusRef<'a> {
84+
let mut s = StatusRef {
85+
id: raft.id,
86+
..Default::default()
87+
};
88+
s.hs = raft.hard_state();
89+
s.ss = raft.soft_state();
90+
s.applied = raft.raft_log.get_applied();
91+
if s.ss.raft_state == StateRole::Leader {
92+
s.progress = Some(raft.prs());
93+
}
94+
s
95+
}
96+
}

0 commit comments

Comments
 (0)