private VoteResponse RequestVoteCore(IHost host) { PersistentState state = _stateController.PersistentState; VoteReqeust request = new VoteReqeust { CandidateId = Id, Term = state.CurrentTerm, LastLogIndex = state.LastLogIndex, LastLogTerm = state.LastLogTerm }; return(host.VoteInvoke(request)); }
public VoteResponse VoteInvoke(VoteReqeust reqeust) { lock (this) { _node.EnsureExistGreaterTermAndChangeRole(reqeust.Term); if (_node.CurrentRole.Type != RoleType.Follower) { return(new VoteResponse() { Term = _stateController.PersistentState.CurrentTerm, VoteGranted = false }); } return(((Follower)_node.CurrentRole).GetVote(reqeust)); } }
public VoteResponse GetVote(VoteReqeust request) { VoteResponse response = new VoteResponse(); PersistentState state = _stateController.PersistentState; if (request.Term < state.CurrentTerm) { response.VoteGranted = false; } else if ((string.IsNullOrEmpty(state.VotedFor) || state.VotedFor == request.CandidateId) && request.LastLogTerm >= state.LastLogTerm && request.LastLogIndex >= state.LastLogIndex) { _stateController.UpdateVoteFor(request.CandidateId); response.VoteGranted = true; DebugConsole.WriteLine($"Voting for candidate {request.CandidateId} for term {request.Term}..."); } response.Term = state.CurrentTerm; return(response); }