func (s *RaftServer) LeaderElectionEventHandler() {
	if s.ServerConf.Info.Role == core.RoleLeader {
		return
	}
	countlog.Info(fmt.Sprintf("%s starts leader election for term %d, old leader %s is dead",
		s.String(), s.stableInfo.Term+1, s.ServerConf.LeaderInfo.String()))

	s.ServerConf.Info.Role.HandleMsg(core.LeaderElectionStartEvent)
	for {
		if s.ServerConf.Info.Role == core.RoleCandidate {
			voteResult := s.collectVotes()
			if voteResult.IsEnough() {
				s.ServerConf.Info.Role.HandleMsg(core.LeaderElectionSuccessEvent)
				becomeLeader(s)
				countlog.Info(fmt.Sprintf("%s becomes new leader", s))
			} else {
				countlog.Info(fmt.Sprintf("%s votes %s not enough", s.String(), voteResult.String()))
				s.stableInfo.LastVotedServerID = 0
				waitForNextRoundElection(s)
			}
		}
		if s.ServerConf.Info.Role != core.RoleCandidate {
			// exit loop when:
			// 1. role becomes follower when receiving append log rpc
			// 2. role becomes leader when it win the election
			break
		}
	}
	if s.ServerConf.Info.Role == core.RoleLeader {
		s.HeartbeatEventHandler()
	}
}
// LeaderElectionEventHandler is the event triggered by leaderElectionTimer
// LeaderElectionEventHandler sends request vote rpc to other servers
LeaderElectionEventHandler is the event triggered by leaderElectionTimer
LeaderElectionEventHandler sends request vote rpc to other servers
// LeaderElectionEventHandler is the event triggered by leaderElectionTimer // LeaderElectionEventHandler sends request vote rpc to other servers
func (s *RaftServer) LeaderElectionEventHandler() {
	if s.ServerConf.Info.Role == core.RoleLeader {
		return
	}
	countlog.Info(fmt.Sprintf("%s starts leader election for term %d, old leader %s is dead",
		s.String(), s.stableInfo.Term+1, s.ServerConf.LeaderInfo.String()))
	s.ServerConf.Info.Role.HandleMsg(core.LeaderElectionStartEvent)
	for {
		if s.ServerConf.Info.Role == core.RoleCandidate {
			voteResult := s.collectVotes()
			if voteResult.IsEnough() {
				s.ServerConf.Info.Role.HandleMsg(core.LeaderElectionSuccessEvent)
				becomeLeader(s)
				countlog.Info(fmt.Sprintf("%s becomes new leader", s))
			} else {
				countlog.Info(fmt.Sprintf("%s votes %s not enough", s.String(), voteResult.String()))
				s.stableInfo.LastVotedServerID = 0
				waitForNextRoundElection(s)
			}
		}
		if s.ServerConf.Info.Role != core.RoleCandidate {
			break
		}
	}
	if s.ServerConf.Info.Role == core.RoleLeader {
		s.HeartbeatEventHandler()
	}
}
func ( s * RaftServer ) LeaderElectionEventHandler ( ) { if s . ServerConf . Info . Role == core . RoleLeader { return } countlog . Info ( fmt . Sprintf ( " " , s . String ( ) , s . stableInfo . Term + 1 , s . ServerConf . LeaderInfo . String ( ) ) ) s . ServerConf . Info . Role . HandleMsg ( core . LeaderElectionStartEvent ) for { if s . ServerConf . Info . Role == core . RoleCandidate { voteResult := s . collectVotes ( ) if voteResult . IsEnough ( ) { s . ServerConf . Info . Role . HandleMsg ( core . LeaderElectionSuccessEvent ) becomeLeader ( s ) countlog . Info ( fmt . Sprintf ( " " , s ) ) } else { countlog . Info ( fmt . Sprintf ( " " , s . String ( ) , voteResult . String ( ) ) ) s . stableInfo . LastVotedServerID = 0 waitForNextRoundElection ( s ) } } if s . ServerConf . Info . Role != core . RoleCandidate { break } } if s . ServerConf . Info . Role == core . RoleLeader { s . HeartbeatEventHandler ( ) } }
LeaderElectionEventHandler is the event triggered by leaderElectionTimer
LeaderElectionEventHandler sends request vote rpc to other servers
LeaderElectionEventHandler is the event triggered by leaderElectionTimer LeaderElectionEventHandler sends request vote rpc to other servers
// exit loop when: // 1. role becomes follower when receiving append log rpc // 2. role becomes leader when it win the election