Ejemplo n.º 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));
        }
Ejemplo n.º 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));
        }
Ejemplo n.º 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));
        }
Ejemplo n.º 4
0
 public RequestVoteResponse Handle(RequestVote requestVote)
 {
     return(State.Handle(requestVote));
 }
Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 7
0
 public async Task <RequestVoteResponse> Handle(RequestVote requestVote)
 {
     return(await State.Handle(requestVote));
 }
Ejemplo n.º 8
0
 public RequestVoteResponse Request(RequestVote requestVote)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 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);
        }