raftlog/node_state/follower/
mod.rs1use self::append::FollowerAppend;
2use self::idle::FollowerIdle;
3use self::init::FollowerInit;
4use self::snapshot::FollowerSnapshot;
5use super::{Common, NextState};
6use election::Role;
7use message::{Message, MessageHeader};
8use {Io, Result};
9
10mod append;
11mod idle;
12mod init;
13mod snapshot;
14
15pub enum Follower<IO: Io> {
22 Init(FollowerInit<IO>),
24
25 Idle(FollowerIdle<IO>),
27
28 Append(FollowerAppend<IO>),
30
31 Snapshot(FollowerSnapshot<IO>),
33}
34impl<IO: Io> Follower<IO> {
35 pub fn new(common: &mut Common<IO>, pending_vote: Option<MessageHeader>) -> Self {
36 common.set_timeout(Role::Follower);
37 let follower = FollowerInit::new(common, pending_vote);
38 Follower::Init(follower)
39 }
40 pub fn handle_timeout(&mut self, common: &mut Common<IO>) -> Result<NextState<IO>> {
41 Ok(Some(common.transit_to_candidate()))
42 }
43 pub fn handle_message(
44 &mut self,
45 common: &mut Common<IO>,
46 message: Message,
47 ) -> Result<NextState<IO>> {
48 if let Message::AppendEntriesCall { .. } = message {
49 common.set_timeout(Role::Follower);
50 if unsafe { common.io_mut().is_busy() } {
51 common.rpc_callee(message.header()).reply_busy();
52 return Ok(None);
53 }
54 }
55
56 match *self {
57 Follower::Init(ref mut t) => track!(t.handle_message(common, message)),
58 Follower::Idle(ref mut t) => track!(t.handle_message(common, message)),
59 Follower::Append(ref mut t) => track!(t.handle_message(common, message)),
60 Follower::Snapshot(ref mut t) => track!(t.handle_message(common, message)),
61 }
62 }
63 pub fn run_once(&mut self, common: &mut Common<IO>) -> Result<NextState<IO>> {
64 match *self {
65 Follower::Init(ref mut t) => track!(t.run_once(common)),
66 Follower::Idle(_) => Ok(None),
67 Follower::Append(ref mut t) => track!(t.run_once(common)),
68 Follower::Snapshot(ref mut t) => track!(t.run_once(common)),
69 }
70 }
71}