diff --git a/src/Rafty/Raft/Server.cs b/src/Rafty/Raft/Server.cs index 4326814..9cfd424 100644 --- a/src/Rafty/Raft/Server.cs +++ b/src/Rafty/Raft/Server.cs @@ -51,15 +51,13 @@ public Server(IMessageBus messageBus, public int CurrentTermVotes { get; private set; } public int CurrentTermAppendEntriesResponse { get; private set; } public Guid LeaderId { get; private set; } - public RequestVoteResponse Receive(RequestVote requestVote) { _logger.LogDebug($"Server: {Id} received request vote in term: {CurrentTerm}"); if (!_serversInClusterInCluster.Contains(requestVote.CandidateId)) { - var remoteServer = new ServerInCluster(requestVote.CandidateId); - _serversInClusterInCluster.Add(remoteServer); + AddNewServerToServersInCluster(requestVote); } // If RPC request or response contains term T > currentTerm: @@ -130,11 +128,9 @@ public void Receive(BecomeCandidate becomeCandidate) public async Task Receive(AppendEntries appendEntries) { - if (!_serversInClusterInCluster.Contains(appendEntries.LeaderId)) { - var remoteServer = new ServerInCluster(appendEntries.LeaderId); - _serversInClusterInCluster.Add(remoteServer); + AddNewServerToServersInCluster(appendEntries); } if (State is Leader) @@ -356,6 +352,12 @@ private void BecomeLeader() } } + private void AddNewServerToServersInCluster(RequestVote requestVote) + { + var remoteServer = new ServerInCluster(requestVote.CandidateId); + _serversInClusterInCluster.Add(remoteServer); + } + private List GetRemoteServers() { return _serversInClusterInCluster.Get(x => x.Id != Id); @@ -390,7 +392,6 @@ private void Receive(RequestVoteResponse requestVoteResponse) } } } - private async Task Receive(AppendEntriesResponse appendEntriesResponse) { if (State is Leader) @@ -474,5 +475,11 @@ private void SendElectionTimeoutMessage(int delayInSeconds) var sendToSelf = new SendToSelf(becomeCandidate, delayInSeconds); _messageBus.Publish(sendToSelf); } + + private void AddNewServerToServersInCluster(AppendEntries appendEntries) + { + var remoteServer = new ServerInCluster(appendEntries.LeaderId); + _serversInClusterInCluster.Add(remoteServer); + } } } \ No newline at end of file