Exemplo n.º 1
0
        private void SendElectionTimeoutMessage(int delayInSeconds)
        {
            var becomeCandidate = new BecomeCandidate(_lastAppendEntriesMessageId);
            var sendToSelf      = new SendToSelf(becomeCandidate, delayInSeconds);

            _messageBus.Publish(sendToSelf);
        }
Exemplo n.º 2
0
        public void Receive(BecomeCandidate becomeCandidate)
        {
            if ((State is Candidate || State is Follower) && becomeCandidate.LastAppendEntriesMessageIdFromLeader == _lastAppendEntriesMessageId)
            {
                State = new Candidate();
                CurrentTerm++;
                VotedFor         = default(Guid);
                CurrentTermVotes = 0;
                var requestVoteResponse = new RequestVoteResponse(CurrentTerm, true, Id, Id);
                Receive(requestVoteResponse);
                var remoteServers = GetRemoteServers();
                var tasks         = new Task <RequestVoteResponse> [remoteServers.Count];
                for (int i = 0; i < tasks.Length; i++)
                {
                    var lastLogIndex = Log.Count > 0 ? Log.Count - 1 : 0;
                    var lastLogTerm  = Log.Count > 0 ? Log[lastLogIndex].Term : 0;
                    var requestVote  = new RequestVote(CurrentTerm, Id, lastLogIndex, lastLogTerm, remoteServers[i].Id);
                    tasks[i] = _messageBus.Send(requestVote);
                }

                Task.WaitAll(tasks);

                foreach (var task in tasks)
                {
                    Receive(task.Result);
                }
            }

            SendElectionTimeoutMessage(10);
        }
Exemplo n.º 3
0
 private void ServerReceives(BecomeCandidate becomeCandidate)
 {
     _server.Receive(becomeCandidate);
 }