Skip to content

Commit

Permalink
more refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
TomPallister committed Apr 7, 2017
1 parent b1ad62e commit fd9509e
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/Rafty/Raft/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -130,11 +128,9 @@ public void Receive(BecomeCandidate becomeCandidate)

public async Task<AppendEntriesResponse> Receive(AppendEntries appendEntries)
{

if (!_serversInClusterInCluster.Contains(appendEntries.LeaderId))
{
var remoteServer = new ServerInCluster(appendEntries.LeaderId);
_serversInClusterInCluster.Add(remoteServer);
AddNewServerToServersInCluster(appendEntries);
}

if (State is Leader)
Expand Down Expand Up @@ -356,6 +352,12 @@ private void BecomeLeader()
}
}

private void AddNewServerToServersInCluster(RequestVote requestVote)
{
var remoteServer = new ServerInCluster(requestVote.CandidateId);
_serversInClusterInCluster.Add(remoteServer);
}

private List<ServerInCluster> GetRemoteServers()
{
return _serversInClusterInCluster.Get(x => x.Id != Id);
Expand Down Expand Up @@ -390,7 +392,6 @@ private void Receive(RequestVoteResponse requestVoteResponse)
}
}
}

private async Task Receive(AppendEntriesResponse appendEntriesResponse)
{
if (State is Leader)
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit fd9509e

Please sign in to comment.