raft_state.rs 813 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. use crate::{
  2. log_array::LogArray, persister::PersistedRaftState, Index, Peer, Term,
  3. };
  4. #[derive(Debug, Eq, PartialEq)]
  5. pub(crate) enum State {
  6. Follower,
  7. Candidate,
  8. // TODO: add PreVote
  9. Leader,
  10. }
  11. pub(crate) struct RaftState<Command> {
  12. pub current_term: Term,
  13. pub voted_for: Option<Peer>,
  14. pub log: LogArray<Command>,
  15. pub commit_index: Index,
  16. pub last_applied: Index,
  17. pub next_index: Vec<Index>,
  18. pub match_index: Vec<Index>,
  19. pub current_step: Vec<i64>,
  20. pub state: State,
  21. pub leader_id: Peer,
  22. }
  23. impl<Command: Clone> RaftState<Command> {
  24. pub fn persisted_state(&self) -> PersistedRaftState<Command> {
  25. self.into()
  26. }
  27. }
  28. impl<Command> RaftState<Command> {
  29. pub fn is_leader(&self) -> bool {
  30. self.state == State::Leader
  31. }
  32. }