Skip to content

Commit

Permalink
mucking around
Browse files Browse the repository at this point in the history
  • Loading branch information
tom.pallister committed Jul 14, 2017
1 parent 5a65b2d commit 8c85ea9
Show file tree
Hide file tree
Showing 25 changed files with 390 additions and 321 deletions.
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"projects":["src","test"]}
{ "projects": ["src", "test"] }
27 changes: 14 additions & 13 deletions src/Rafty/Concensus/AppendEntries.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System;
using System.Collections.Generic;
using Rafty.Log;

namespace Rafty.Concensus
{
using System;
using System.Collections.Generic;
using Log;

public sealed class AppendEntries : Message
{
public AppendEntries(long term, Guid leaderId, long previousLogIndex, long previousLogTerm, List<LogEntry> entries, long leaderCommitIndex)
:base(Guid.NewGuid())
public AppendEntries(long term, Guid leaderId, long previousLogIndex, long previousLogTerm,
List<LogEntry> entries, long leaderCommitIndex)
: base(Guid.NewGuid())
{
Term = term;
LeaderId = leaderId;
Expand All @@ -16,18 +17,18 @@ public AppendEntries(long term, Guid leaderId, long previousLogIndex, long previ
Entries = entries;
LeaderCommitIndex = leaderCommitIndex;
}

// term leader’s term
public long Term {get;private set;}
public long Term { get; private set; }
// leaderId so follower can redirect clients
public Guid LeaderId {get;private set;}
public Guid LeaderId { get; private set; }
// prevLogIndex index of log entry immediately preceding new ones
public long PreviousLogIndex {get;private set;}
public long PreviousLogIndex { get; private set; }
// prevLogTerm term of prevLogIndex entry
public long PreviousLogTerm {get;private set;}
public long PreviousLogTerm { get; private set; }
// entries[] log entries to store (empty for heartbeat may send more than one for efficiency)
public List<LogEntry> Entries {get;private set;}
public List<LogEntry> Entries { get; private set; }
// leaderCommit leader’s commitIndex
public long LeaderCommitIndex {get;private set;}
public long LeaderCommitIndex { get; private set; }
}
}
18 changes: 9 additions & 9 deletions src/Rafty/Concensus/AppendEntriesBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
using System;
using System.Collections.Generic;
using Rafty.Log;

namespace Rafty.Concensus
{
using System;
using System.Collections.Generic;
using Log;

public class AppendEntriesBuilder
{
private long _term;
private List<LogEntry> _entries;
private long _leaderCommitIndex;
private Guid _leaderId;
private long _previousLogIndex;
private long _previousLogIndex;
private long _previousLogTerm;
private List<LogEntry> _entries;
private long _leaderCommitIndex;
private long _term;

public AppendEntriesBuilder WithTerm(long term)
{
Expand Down Expand Up @@ -51,7 +51,7 @@ public AppendEntriesBuilder WithLeaderCommitIndex(long leaderCommitIndex)

public AppendEntriesBuilder WithEntry(LogEntry entry)
{
_entries = new List<LogEntry>{entry};
_entries = new List<LogEntry> {entry};
return this;
}

Expand Down
6 changes: 3 additions & 3 deletions src/Rafty/Concensus/BeginElection.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;

namespace Rafty.Concensus
{
using System;

public class BeginElection : Message
{
public BeginElection()
public BeginElection()
: base(Guid.NewGuid())
{
}
Expand Down
27 changes: 19 additions & 8 deletions src/Rafty/Concensus/Candidate.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using System.Collections.Generic;

namespace Rafty.Concensus
{
public sealed class Candidate : IState
{
private readonly ISendToSelf _sendToSelf;
private int _votesThisElection;
private ISendToSelf _sendToSelf;

public Candidate(CurrentState currentState, ISendToSelf sendToSelf)
public Candidate(CurrentState currentState, ISendToSelf sendToSelf)
{
_sendToSelf = sendToSelf;
// • On conversion to candidate, start election:
Expand All @@ -16,11 +14,12 @@ public Candidate(CurrentState currentState, ISendToSelf sendToSelf)
// • Vote for self
_votesThisElection++;
var votedFor = currentState.Id;
var nextState = new CurrentState(currentState.Id, currentState.Peers, nextTerm, votedFor, currentState.Timeout);
var nextState = new CurrentState(currentState.Id, currentState.Peers, nextTerm, votedFor,
currentState.Timeout);
CurrentState = nextState;
}

public CurrentState CurrentState {get;private set;}
public CurrentState CurrentState { get; }

public IState Handle(Timeout timeout)
{
Expand All @@ -35,9 +34,21 @@ public IState Handle(BeginElection beginElection)
// • Send RequestVote RPCs to all other servers
CurrentState.Peers.ForEach(peer =>
{
peer.Request(new RequestVote());
var requestVoteResponse = peer.Request(new RequestVote());

if (requestVoteResponse.Grant)
{
_votesThisElection++;
}
});
return this;

//If votes received from majority of servers: become leader
if (_votesThisElection >= (CurrentState.Peers.Count + 1) / 2 + 1)
{
return new Leader(CurrentState);
}

return new Follower(CurrentState, _sendToSelf);
}
}
}
8 changes: 4 additions & 4 deletions src/Rafty/Concensus/CurrentState.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;

namespace Rafty.Concensus
{
using System;
using System.Collections.Generic;

public class CurrentState
{
public CurrentState(Guid id, List<IPeer> peers, long currentTerm, Guid votedFor, TimeSpan timeout)
Expand All @@ -21,6 +21,6 @@ public CurrentState(Guid id, List<IPeer> peers, long currentTerm, Guid votedFor,
public Uri Address { get; private set; }
public Guid Id { get; private set; }
public List<IPeer> Peers { get; private set; }
public TimeSpan Timeout {get;private set;}
public TimeSpan Timeout { get; private set; }
}
}
5 changes: 1 addition & 4 deletions src/Rafty/Concensus/Follower.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System;
using System.Collections.Generic;

namespace Rafty.Concensus
{
public sealed class Follower : IState
Expand All @@ -13,7 +10,7 @@ public Follower(CurrentState state, ISendToSelf sendToSelf)
_sendToSelf = sendToSelf;
}

public CurrentState CurrentState {get;private set;}
public CurrentState CurrentState { get; }

public IState Handle(Timeout timeout)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Rafty/Concensus/IState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ namespace Rafty.Concensus
{
public interface IState
{
CurrentState CurrentState { get; }
IState Handle(Timeout timeout);
IState Handle(BeginElection beginElection);
CurrentState CurrentState {get;}
}
}
9 changes: 4 additions & 5 deletions src/Rafty/Concensus/Leader.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
using System;
using System.Collections.Generic;

namespace Rafty.Concensus
{
using System;

public sealed class Leader : IState
{
public Leader(CurrentState currentState)
public Leader(CurrentState currentState)
{
CurrentState = currentState;
}

public CurrentState CurrentState {get;private set;}
public CurrentState CurrentState { get; }

public IState Handle(Timeout timeout)
{
Expand Down
8 changes: 4 additions & 4 deletions src/Rafty/Concensus/Message.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;

namespace Rafty.Concensus
{
using System;

public abstract class Message
{
public Message(Guid messageId)
Expand All @@ -16,7 +16,7 @@ public Message(Guid messageId, TimeSpan delay)
Delay = delay;
}

public Guid MessageId {get;private set;}
public TimeSpan Delay {get;private set;}
public Guid MessageId { get; private set; }
public TimeSpan Delay { get; private set; }
}
}
36 changes: 17 additions & 19 deletions src/Rafty/Concensus/Node.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

namespace Rafty.Concensus
{
{
using System;
using System.Collections.Generic;
using System.Linq;

public class Node : IDisposable, INode
{
private readonly List<Guid> _appendEntriesIdsReceived;
private Guid _appendEntriesAtPreviousHeartbeat;
private readonly ISendToSelf _sendToSelf;
private Guid _appendEntriesAtPreviousHeartbeat;

public Node(CurrentState initialState, ISendToSelf sendToSelf)
{
Expand All @@ -18,20 +17,25 @@ public Node(CurrentState initialState, ISendToSelf sendToSelf)
State = new Follower(initialState, _sendToSelf);
}

public void Dispose()
{
_sendToSelf.Dispose();
}

public IState State { get; private set; }

public void Handle(Message message)
{
//todo - could run middleware type functions here?
//todo - these handlers should be in a dictionary
if(message.GetType() == typeof(BeginElection))
if (message.GetType() == typeof(BeginElection))
{
Handle((BeginElection)message);
Handle((BeginElection) message);
}

if(message.GetType() == typeof(Timeout))
if (message.GetType() == typeof(Timeout))
{
Handle((Timeout)message);
Handle((Timeout) message);
}
}

Expand All @@ -41,21 +45,15 @@ public AppendEntriesResponse Handle(AppendEntries appendEntries)
return new AppendEntriesResponse();
}

public void Dispose()
{
_sendToSelf.Dispose();
}

private void Handle(BeginElection beginElection)
{
State = State.Handle(beginElection);

_sendToSelf.Publish(new Timeout(State.CurrentState.Timeout));
}

private void Handle(Timeout timeout)
{
if(NoHeartbeatSinceLastTimeout())
if (NoHeartbeatSinceLastTimeout())
{
State = State.Handle(timeout);
}
Expand All @@ -73,7 +71,7 @@ private bool AppendEntriesReceived()

private bool NoHeartbeatSinceLastTimeout()
{
if(!_appendEntriesIdsReceived.Any())
if (!_appendEntriesIdsReceived.Any())
{
return true;
}
Expand Down
6 changes: 6 additions & 0 deletions src/Rafty/Concensus/RequestVoteResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,11 @@ namespace Rafty.Concensus
{
public sealed class RequestVoteResponse
{
public RequestVoteResponse(bool grant)
{
Grant = grant;
}

public bool Grant { get; private set; }
}
}
Loading

0 comments on commit 8c85ea9

Please sign in to comment.