Exemple #1
0
        public RequestVoteResponse Handle(RequestVote requestVote)
        {
            var response = RequestVoteTermIsGreaterThanCurrentTerm(requestVote);

            if (response.shouldReturn)
            {
                return(response.requestVoteResponse);
            }

            response = _rules.RequestVoteTermIsLessThanCurrentTerm(requestVote, CurrentState);

            if (response.shouldReturn)
            {
                return(response.requestVoteResponse);
            }

            response = _rules.VotedForIsNotThisOrNobody(requestVote, CurrentState);

            if (response.shouldReturn)
            {
                return(response.requestVoteResponse);
            }

            response = LastLogIndexAndLastLogTermMatchesThis(requestVote);

            if (response.shouldReturn)
            {
                return(response.requestVoteResponse);
            }

            return(new RequestVoteResponse(false, CurrentState.CurrentTerm));
        }
Exemple #2
0
        public async Task <RequestVoteResponse> Handle(RequestVote requestVote)
        {
            var response = RequestVoteTermIsGreaterThanCurrentTerm(requestVote);

            if (response.shouldReturn)
            {
                return(response.requestVoteResponse);
            }

            response = _rules.RequestVoteTermIsLessThanCurrentTerm(requestVote, CurrentState);

            if (response.shouldReturn)
            {
                return(response.requestVoteResponse);
            }

            response = _rules.VotedForIsNotThisOrNobody(requestVote, CurrentState);

            if (response.shouldReturn)
            {
                return(response.requestVoteResponse);
            }

            response = await LastLogIndexAndLastLogTermMatchesThis(requestVote);

            _messagesSinceLastElectionExpiry++;

            if (response.shouldReturn)
            {
                return(response.requestVoteResponse);
            }

            return(new RequestVoteResponse(false, CurrentState.CurrentTerm));
        }
Exemple #3
0
        public async Task <RequestVoteResponse> Handle(RequestVote requestVote)
        {
            var response = RequestVoteTermIsGreaterThanCurrentTerm(requestVote);

            if (response.shouldReturn)
            {
                return(response.requestVoteResponse);
            }

            return(new RequestVoteResponse(false, CurrentState.CurrentTerm));
        }
Exemple #4
0
 public RequestVoteResponse Handle(RequestVote requestVote)
 {
     return(State.Handle(requestVote));
 }
Exemple #5
0
        private (RequestVoteResponse requestVoteResponse, bool shouldReturn) LastLogIndexAndLastLogTermMatchesThis(RequestVote requestVote)
        {
            if (requestVote.LastLogIndex == _log.LastLogIndex &&
                requestVote.LastLogTerm == _log.LastLogTerm)
            {
                CurrentState = new CurrentState(CurrentState.Id, CurrentState.CurrentTerm, requestVote.CandidateId, CurrentState.CommitIndex, CurrentState.LastApplied, CurrentState.LeaderId);
                BecomeFollower();
                return(new RequestVoteResponse(true, CurrentState.CurrentTerm), true);
            }

            return(null, false);
        }
Exemple #6
0
        private (RequestVoteResponse requestVoteResponse, bool shouldReturn) RequestVoteTermIsGreaterThanCurrentTerm(RequestVote requestVote)
        {
            if (requestVote.Term > CurrentState.CurrentTerm)
            {
                CurrentState = new CurrentState(CurrentState.Id, requestVote.Term, requestVote.CandidateId,
                                                CurrentState.CommitIndex, CurrentState.LastApplied, CurrentState.LeaderId);
                BecomeFollower();
                return(new RequestVoteResponse(true, CurrentState.CurrentTerm), true);
            }

            return(null, false);
        }
Exemple #7
0
 public async Task <RequestVoteResponse> Handle(RequestVote requestVote)
 {
     return(await State.Handle(requestVote));
 }
Exemple #8
0
 public RequestVoteResponse Request(RequestVote requestVote)
 {
     throw new NotImplementedException();
 }
Exemple #9
0
        private async Task <(RequestVoteResponse requestVoteResponse, bool shouldReturn)> LastLogIndexAndLastLogTermMatchesThis(RequestVote requestVote)
        {
            if (requestVote.LastLogIndex == await _log.LastLogIndex() &&
                requestVote.LastLogTerm == await _log.LastLogTerm())
            {
                CurrentState = new CurrentState(CurrentState.Id, CurrentState.CurrentTerm, requestVote.CandidateId, CurrentState.CommitIndex, CurrentState.LastApplied, CurrentState.LeaderId);

                return(new RequestVoteResponse(true, CurrentState.CurrentTerm), true);
            }

            return(null, false);
        }