openraft icon indicating copy to clipboard operation
openraft copied to clipboard

I want to know how to recognize self is leader when storage apply log

Open ChiaoTeo opened this issue 4 years ago • 6 comments

I want to do some thing when leader commit and apply to StateMachine , could some one teach me how to impl such feature

ChiaoTeo avatar Nov 02 '22 10:11 ChiaoTeo

👋 Thanks for opening this issue!

Get help or engage by:

  • /help : to print help messages.
  • /assignme : to assign this issue to you.

github-actions[bot] avatar Nov 02 '22 10:11 github-actions[bot]

/help

ChiaoTeo avatar Nov 02 '22 10:11 ChiaoTeo

@drmingdrmer help! 可以解答一下这个问题吗,谢谢🙏

ChiaoTeo avatar Nov 02 '22 11:11 ChiaoTeo

I use a global raft var solve this

ChiaoTeo avatar Nov 02 '22 13:11 ChiaoTeo

Since a RaftStorage implementation stores the Vote via save_vote(): https://github.com/datafuselabs/openraft/blob/3111e7e6c8649f7068f959bc58e484f3b95732bb/memstore/src/lib.rs#L248-L254

The storage implementation can get the saved vote when applying a log. The structure of Vote looks like the following, and the value of the vote defines the server-state of the raft node:

pub struct Vote<NID: NodeId> {
    pub term: u64,
    pub node_id: NID,
    pub committed: bool,
}
  • Vote.node_id == self.id: candidate or leader
    • Vote.node_id == self.id && Vote.committed == true: leader
    • Vote.node_id == self.id && Vote.committed == false: candidate
  • Otherwise, it is a follower or a learner.

You can find how to determine the server-state by vote here: https://github.com/datafuselabs/openraft/blob/e4036a0fc6e8b80f62580b42eb349da8d1055f96/openraft/src/engine/engine_impl.rs#L1153-L1171

And it's a good question. I should add it to the doc:)

Thank you!

drmingdrmer avatar Nov 02 '22 16:11 drmingdrmer

thanks for you patient reply, It really help. it is very usefull framework

ChiaoTeo avatar Nov 02 '22 17:11 ChiaoTeo