I want to know how to recognize self is leader when storage apply log
I want to do some thing when leader commit and apply to StateMachine , could some one teach me how to impl such feature
👋 Thanks for opening this issue!
Get help or engage by:
/help: to print help messages./assignme: to assign this issue to you.
/help
@drmingdrmer help! 可以解答一下这个问题吗,谢谢🙏
I use a global raft var solve this
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 leaderVote.node_id == self.id && Vote.committed == true: leaderVote.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!
thanks for you patient reply, It really help. it is very usefull framework