// example RequestVote RPC handler. func(rf *Raft) RequestVote(args *RequestVoteArgs, reply *RequestVoteReply) { // Your code here (PartA, PartB).
rf.mu.Lock() defer rf.mu.Unlock()
reply.Term = rf.currentTerm reply.VoteGranted = false // align the term if args.Term < rf.currentTerm { LOG(rf.me, rf.currentTerm, DVote, "-> S%d, Reject Vote, Higher term, T%d>T%d", args.CandidateId, rf.currentTerm, args.Term) return } if args.Term > rf.currentTerm { rf.becomeFollowerLocked(args.Term) }
// check for votedFor if rf.votedFor != -1 { LOG(rf.me, rf.currentTerm, DVote, "-> S%d, Reject Voted, Already voted to S%d", args.CandidateId, rf.votedFor) return }
// check log, only grante vote when the candidates have more up-to-date log if rf.isMoreUpToDateLocked(args.LastLogIndex,args.LastLogTerm) { LOG(rf.me, rf.currentTerm, DVote, "-> S%d, Reject Vote, S%d's log less up-to-date", args.CandidateId) return }