예제 #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));
        }
예제 #2
0
파일: Follower.cs 프로젝트: rfum/Rafty
        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));
        }
예제 #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));
        }
예제 #4
0
 public RequestVoteResponse Handle(RequestVote requestVote)
 {
     return(State.Handle(requestVote));
 }
예제 #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);
        }
예제 #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);
        }
예제 #7
0
 public async Task <RequestVoteResponse> Handle(RequestVote requestVote)
 {
     return(await State.Handle(requestVote));
 }
예제 #8
0
파일: Peer.cs 프로젝트: zhaoyingju/Rafty
 public RequestVoteResponse Request(RequestVote requestVote)
 {
     throw new NotImplementedException();
 }
예제 #9
0
파일: Follower.cs 프로젝트: rfum/Rafty
        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);
        }