Skip to content
Snippets Groups Projects
Commit 04cd9562 authored by Georgy Moshkin's avatar Georgy Moshkin :speech_balloon:
Browse files

chore: add doc comments for yielding functions

parent 9fba1c89
No related branches found
No related tags found
1 merge request!183Refactor/remove mailbox
...@@ -89,6 +89,8 @@ impl std::fmt::Debug for Node { ...@@ -89,6 +89,8 @@ impl std::fmt::Debug for Node {
impl Node { impl Node {
pub const TICK: Duration = Duration::from_millis(100); pub const TICK: Duration = Duration::from_millis(100);
/// Initialize the raft node.
/// **This function yields**
pub fn new(cfg: &raft::Config) -> Result<Self, RaftError> { pub fn new(cfg: &raft::Config) -> Result<Self, RaftError> {
let raw_node = RawNode::new(cfg, Storage, &tlog::root())?; let raw_node = RawNode::new(cfg, Storage, &tlog::root())?;
let raw_node = Rc::new(Mutex::new(raw_node)); let raw_node = Rc::new(Mutex::new(raw_node));
...@@ -151,10 +153,13 @@ impl Node { ...@@ -151,10 +153,13 @@ impl Node {
event::broadcast(Event::StatusChanged); event::broadcast(Event::StatusChanged);
} }
/// Wait for the status to be changed.
/// **This function yields**
pub fn wait_status(&self) { pub fn wait_status(&self) {
event::wait(Event::StatusChanged).expect("Events system wasn't initialized"); event::wait(Event::StatusChanged).expect("Events system wasn't initialized");
} }
/// **This function yields**
pub fn read_index(&self, timeout: Duration) -> Result<RaftIndex, Error> { pub fn read_index(&self, timeout: Duration) -> Result<RaftIndex, Error> {
self.raw_operation(|raw_node| { self.raw_operation(|raw_node| {
// In some states `raft-rs` ignores the ReadIndex request. // In some states `raft-rs` ignores the ReadIndex request.
...@@ -181,6 +186,8 @@ impl Node { ...@@ -181,6 +186,8 @@ impl Node {
.recv_timeout::<RaftIndex>(timeout) .recv_timeout::<RaftIndex>(timeout)
} }
/// Propose an operation and wait for it's result.
/// **This function yields**
pub fn propose<T: OpResult + Into<traft::Op>>( pub fn propose<T: OpResult + Into<traft::Op>>(
&self, &self,
op: T, op: T,
...@@ -195,6 +202,9 @@ impl Node { ...@@ -195,6 +202,9 @@ impl Node {
.recv_timeout::<T::Result>(timeout) .recv_timeout::<T::Result>(timeout)
} }
/// Become a candidate and wait for a main loop round so that there's a
/// chance we become the leader.
/// **This function yields**
pub fn campaign(&self) -> Result<(), Error> { pub fn campaign(&self) -> Result<(), Error> {
self.raw_operation(|raw_node| raw_node.campaign().map_err(Into::into))?; self.raw_operation(|raw_node| raw_node.campaign().map_err(Into::into))?;
// Even though we don't expect a response, we still should let the // Even though we don't expect a response, we still should let the
...@@ -205,6 +215,7 @@ impl Node { ...@@ -205,6 +215,7 @@ impl Node {
Ok(()) Ok(())
} }
/// **This function yields**
pub fn step(&self, msg: raft::Message) { pub fn step(&self, msg: raft::Message) {
self.raw_operation(|raw_node| { self.raw_operation(|raw_node| {
if msg.to != raw_node.raft.id { if msg.to != raw_node.raft.id {
...@@ -221,6 +232,7 @@ impl Node { ...@@ -221,6 +232,7 @@ impl Node {
fiber::reschedule(); fiber::reschedule();
} }
/// **This function yields**
pub fn tick(&self, n_times: u32) { pub fn tick(&self, n_times: u32) {
self.raw_operation(|raw_node| { self.raw_operation(|raw_node| {
for _ in 0..n_times { for _ in 0..n_times {
...@@ -234,6 +246,7 @@ impl Node { ...@@ -234,6 +246,7 @@ impl Node {
fiber::reschedule(); fiber::reschedule();
} }
/// **This function yields**
pub fn timeout_now(&self) { pub fn timeout_now(&self) {
self.step(raft::Message { self.step(raft::Message {
to: self.raft_id, to: self.raft_id,
...@@ -243,6 +256,9 @@ impl Node { ...@@ -243,6 +256,9 @@ impl Node {
}) })
} }
/// Process the topology request and propose [`PersistPeer`] entry if
/// appropriate.
/// **This function yields**
pub fn handle_topology_request(&self, req: TopologyRequest) -> Result<traft::Peer, Error> { pub fn handle_topology_request(&self, req: TopologyRequest) -> Result<traft::Peer, Error> {
self.raw_operation(|raw_node| { self.raw_operation(|raw_node| {
if raw_node.raft.state != RaftStateRole::Leader { if raw_node.raft.state != RaftStateRole::Leader {
...@@ -296,6 +312,7 @@ impl Node { ...@@ -296,6 +312,7 @@ impl Node {
.recv::<Peer>() .recv::<Peer>()
} }
/// **This function yields**
fn propose_conf_change( fn propose_conf_change(
&self, &self,
term: RaftTerm, term: RaftTerm,
...@@ -359,6 +376,7 @@ impl Node { ...@@ -359,6 +376,7 @@ impl Node {
.recv() .recv()
} }
/// This function **may yield** if `self.raw_node` is acquired.
#[inline] #[inline]
fn raw_operation<R>( fn raw_operation<R>(
&self, &self,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment