Skip to content

Commit

Permalink
Merge pull request #8 from TomPallister/feature/id-as-string
Browse files Browse the repository at this point in the history
Feature/id as string
  • Loading branch information
TomPallister authored Dec 22, 2017
2 parents 9544fa4 + f2e0be3 commit 9157fb0
Show file tree
Hide file tree
Showing 33 changed files with 117 additions and 125 deletions.
4 changes: 2 additions & 2 deletions src/Rafty/Concensus/Messages/AppendEntries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Rafty.Concensus

public sealed class AppendEntries : Message
{
public AppendEntries(long term, Guid leaderId, int previousLogIndex, long previousLogTerm,
public AppendEntries(long term, string leaderId, int previousLogIndex, long previousLogTerm,
List<LogEntry> entries, int leaderCommitIndex)
: base(Guid.NewGuid())
{
Expand All @@ -26,7 +26,7 @@ public AppendEntries(long term, Guid leaderId, int previousLogIndex, long previo
/// <summary>
// LeaderId so follower can redirect clients.
/// </summary>
public Guid LeaderId { get; private set; }
public string LeaderId { get; private set; }

/// <summary>
// PrevLogIndex index of log entry immediately preceding new ones.
Expand Down
4 changes: 2 additions & 2 deletions src/Rafty/Concensus/Messages/AppendEntriesBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class AppendEntriesBuilder
{
private List<LogEntry> _entries;
private int _leaderCommitIndex;
private Guid _leaderId;
private string _leaderId;
private int _previousLogIndex;
private long _previousLogTerm;
private long _term;
Expand All @@ -19,7 +19,7 @@ public AppendEntriesBuilder WithTerm(long term)
return this;
}

public AppendEntriesBuilder WithLeaderId(Guid leaderId)
public AppendEntriesBuilder WithLeaderId(string leaderId)
{
_leaderId = leaderId;
return this;
Expand Down
4 changes: 2 additions & 2 deletions src/Rafty/Concensus/Messages/RequestVote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Rafty.Concensus
{
public sealed class RequestVote
{
public RequestVote(long term, Guid candidateId, long lastLogIndex, long lastLogTerm)
public RequestVote(long term, string candidateId, long lastLogIndex, long lastLogTerm)
{
Term = term;
CandidateId = candidateId;
Expand All @@ -20,7 +20,7 @@ public RequestVote(long term, Guid candidateId, long lastLogIndex, long lastLogT
/// <summary>
// CandidateId candidate requesting vote.
/// </summary>
public Guid CandidateId {get;private set;}
public string CandidateId {get;private set;}

/// <summary>
// LastLogIndex index of candidate’s last log entry (§5.4).
Expand Down
4 changes: 2 additions & 2 deletions src/Rafty/Concensus/Messages/RequestVoteBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Rafty.Concensus
public class RequestVoteBuilder
{
private long _term;
private Guid _candidateId;
private string _candidateId;
private long _lastLogIndex;
private long _lastLogTerm;

Expand All @@ -15,7 +15,7 @@ public RequestVoteBuilder WithTerm(long term)
return this;
}

public RequestVoteBuilder WithCandidateId(Guid candidateId)
public RequestVoteBuilder WithCandidateId(string candidateId)
{
_candidateId = candidateId;
return this;
Expand Down
2 changes: 1 addition & 1 deletion src/Rafty/Concensus/Node/INode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public interface INode
void BecomeCandidate(CurrentState state);
AppendEntriesResponse Handle(AppendEntries appendEntries);
RequestVoteResponse Handle(RequestVote requestVote);
void Start(Guid id);
void Start(string id);
void Stop();
Response<T> Accept<T>(T command) where T : ICommand;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Rafty/Concensus/Node/Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ public Node(

public IState State { get; private set; }

public void Start(Guid id)
public void Start(string id)
{
if(State?.CurrentState == null)
{
BecomeFollower(new CurrentState(id, 0, default(Guid), 0, 0, default(Guid)));
BecomeFollower(new CurrentState(id, 0, default(string), 0, 0, default(string)));
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions src/Rafty/Concensus/Node/NodePeer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class NodePeer : IPeer
{
private Node _node;

public Guid Id
public string Id
{
get
{
Expand All @@ -18,7 +18,7 @@ public Guid Id
return _node.State.CurrentState.Id;
}

return default(Guid);
return default(string);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Rafty/Concensus/Peers/IPeer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public interface IPeer
/// <summary>
/// This will return the peers ID.
/// </summary>
Guid Id {get;}
string Id {get;}

/// <summary>
/// This will make a requestvote request to the given peer. You must implement the transport.
Expand Down
2 changes: 1 addition & 1 deletion src/Rafty/Concensus/Peers/Peer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Rafty.Concensus

public class Peer : IPeer
{
public Guid Id => throw new NotImplementedException();
public string Id => throw new NotImplementedException();

public RequestVoteResponse Request(RequestVote requestVote)
{
Expand Down
8 changes: 4 additions & 4 deletions src/Rafty/Concensus/States/CurrentState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Rafty.Concensus

public class CurrentState
{
public CurrentState(Guid id, long currentTerm, Guid votedFor, int commitIndex, int lastApplied, Guid leaderId)
public CurrentState(string id, long currentTerm, string votedFor, int commitIndex, int lastApplied, string leaderId)
{
Id = id;
CurrentTerm = currentTerm;
Expand All @@ -17,11 +17,11 @@ public CurrentState(Guid id, long currentTerm, Guid votedFor, int commitIndex, i
}

public long CurrentTerm { get; private set; }
public Guid VotedFor { get; private set; }
public string VotedFor { get; private set; }
public int CommitIndex { get; private set; }
public int LastApplied { get; private set; }
public Uri Address { get; private set; }
public Guid Id { get; private set; }
public Guid LeaderId { get; private set; }
public string Id { get; private set; }
public string LeaderId { get; private set; }
}
}
2 changes: 1 addition & 1 deletion src/Rafty/Concensus/States/Rules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class Rules : IRules
// todo - consolidate with candidate and pass in as function
public (RequestVoteResponse requestVoteResponse, bool shouldReturn) VotedForIsNotThisOrNobody(RequestVote requestVote, CurrentState currentState)
{
if (currentState.VotedFor == currentState.Id || currentState.VotedFor != default(Guid))
if (currentState.VotedFor == currentState.Id || currentState.VotedFor != default(string))
{
return (new RequestVoteResponse(false, currentState.CurrentTerm), true);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Rafty/Infrastructure/NodeId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ namespace Rafty.Infrastructure
{
public class NodeId
{
public NodeId(Guid id)
public NodeId(string id)
{
Id = id;
}

public Guid Id {get;private set;}
public string Id {get;private set;}
}
}
8 changes: 4 additions & 4 deletions test/Rafty.AcceptanceTests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ private void AddNewServers(int count)
peer.SetNode(server.Node);
var nextIndex = _servers.Count;
_servers.TryAdd(nextIndex, server);
node.Start(Guid.NewGuid());
node.Start(Guid.NewGuid().ToString());
}
}

Expand Down Expand Up @@ -361,7 +361,7 @@ private void AssertLeaderElectedAndRemainsLeader()
{
var stopwatch = Stopwatch.StartNew();
var passed = false;
var leaderId = default(Guid);
var leaderId = default(string);
while(stopwatch.Elapsed.TotalSeconds < 25)
{
Thread.Sleep(1000);
Expand All @@ -373,7 +373,7 @@ private void AssertLeaderElectedAndRemainsLeader()
if (leader.Count == 1 && followers.Count == 4)
{
//if the leader id hasnt been set set it...
if(leaderId == default(Guid))
if(leaderId == default(string))
{
leaderId = leader[0].State.CurrentState.Id;
}
Expand Down Expand Up @@ -429,7 +429,7 @@ private void StartNodes()
_output.WriteLine("start the nodes");
foreach(var server in _servers)
{
server.Value.Node.Start(Guid.NewGuid());
server.Value.Node.Start(Guid.NewGuid().ToString());
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/Rafty.IntegrationTests/FileFsm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Rafty.IntegrationTests
{
public class FileFsm : IFiniteStateMachine
{
private Guid _id;
private string _id;
public FileFsm(NodeId nodeId)
{
_id = nodeId.Id;
Expand All @@ -20,7 +20,7 @@ public void Handle(LogEntry log)
try
{
var json = JsonConvert.SerializeObject(log.CommandData);
File.AppendAllText(_id.ToString(), json);
File.AppendAllText(_id.Replace("/","").Replace(":","").ToString(), json);
}
catch(Exception exception)
{
Expand Down
1 change: 0 additions & 1 deletion test/Rafty.IntegrationTests/FilePeer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ namespace Rafty.IntegrationTests
{
public class FilePeer
{
public string Id { get; set; }
public string HostAndPort { get; set; }
}
}
2 changes: 1 addition & 1 deletion test/Rafty.IntegrationTests/FilePeersProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public FilePeersProvider(IOptions<FilePeers> options)
foreach (var item in _options.Value.Peers)
{
var httpClient = new HttpClient();
var httpPeer = new HttpPeer(item.HostAndPort, Guid.Parse(item.Id), httpClient);
var httpPeer = new HttpPeer(item.HostAndPort, httpClient);
_peers.Add(httpPeer);
}
}
Expand Down
6 changes: 3 additions & 3 deletions test/Rafty.IntegrationTests/HttpPeer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ public class HttpPeer : IPeer
private HttpClient _httpClient;
private JsonSerializerSettings _jsonSerializerSettings;

public HttpPeer(string hostAndPort, Guid id, HttpClient httpClient)
public HttpPeer(string hostAndPort, HttpClient httpClient)
{
Id = id;
Id = hostAndPort;
_hostAndPort = hostAndPort;
_httpClient = httpClient;
_jsonSerializerSettings = new JsonSerializerSettings() {
TypeNameHandling = TypeNameHandling.All
};
}

public Guid Id {get; private set;}
public string Id {get; private set;}

public RequestVoteResponse Request(RequestVote requestVote)
{
Expand Down
2 changes: 1 addition & 1 deletion test/Rafty.IntegrationTests/SqlLiteLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class SqlLiteLog : ILog

public SqlLiteLog(NodeId nodeId)
{
_path = $"{nodeId.Id.ToString()}.db";
_path = $"{nodeId.Id.Replace("/","").Replace(":","").ToString()}.db";
if(!File.Exists(_path))
{
lock(_lock)
Expand Down
4 changes: 2 additions & 2 deletions test/Rafty.IntegrationTests/SqlLiteLogTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ namespace Rafty.UnitTests
public class SqlLiteLogTests : IDisposable
{
private SqlLiteLog _log;
private Guid _id;
private string _id;

public SqlLiteLogTests()
{
_id = Guid.NewGuid();
_id = Guid.NewGuid().ToString();
_log = new SqlLiteLog(new NodeId(_id));
}

Expand Down
2 changes: 1 addition & 1 deletion test/Rafty.IntegrationTests/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
var node = (INode)app.ApplicationServices.GetService(typeof(INode));
var nodeId = (NodeId)app.ApplicationServices.GetService(typeof(NodeId));
var logger = loggerFactory.CreateLogger<Startup>();
node.Start(nodeId.Id);
node.Start(nodeId.Id.ToString());

var jsonSerializerSettings = new JsonSerializerSettings() {
TypeNameHandling = TypeNameHandling.All
Expand Down
14 changes: 6 additions & 8 deletions test/Rafty.IntegrationTests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public void Dispose()

foreach (var peer in _peers.Peers)
{
File.Delete(peer.Id);
File.Delete($"{peer.Id.ToString()}.db");
File.Delete(peer.HostAndPort.Replace("/", "").Replace(":", ""));
File.Delete($"{peer.HostAndPort.Replace("/", "").Replace(":", "")}.db");
}
}

Expand All @@ -56,18 +56,16 @@ public void ShouldPersistCommandToFiveServers()
ThenTheCommandIsReplicatedToAllStateMachines(command);
}

private void GivenAServerIsRunning(string url, string id)
private void GivenAServerIsRunning(string url)
{
var guid = Guid.Parse(id);

IWebHostBuilder webHostBuilder = new WebHostBuilder();
webHostBuilder.UseUrls(url)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.ConfigureServices(x =>
{
x.AddSingleton(webHostBuilder);
x.AddSingleton(new NodeId(guid));
x.AddSingleton(new NodeId(url));
})
.UseStartup<Startup>();

Expand All @@ -85,7 +83,7 @@ private void GivenFiveServersAreRunning()

foreach (var peer in _peers.Peers)
{
var thread = new Thread(() => GivenAServerIsRunning(peer.HostAndPort, peer.Id));
var thread = new Thread(() => GivenAServerIsRunning(peer.HostAndPort));
thread.Start();
_threads.Add(thread);
}
Expand Down Expand Up @@ -140,7 +138,7 @@ bool CommandCalledOnAllStateMachines()
foreach (var peer in _peers.Peers)
{
string fsmData;
fsmData = File.ReadAllText(peer.Id);
fsmData = File.ReadAllText(peer.HostAndPort.Replace("/", "").Replace(":", ""));
fsmData.ShouldNotBeNullOrEmpty();
var fakeCommand = JsonConvert.DeserializeObject<FakeCommand>(fsmData);
fakeCommand.Value.ShouldBe(command.Value);
Expand Down
5 changes: 0 additions & 5 deletions test/Rafty.IntegrationTests/peers.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
{
"Peers": [{
"Id": "2f40ace0-5023-4c4e-853d-50db97ba4e25",
"HostAndPort": "http://localhost:5000"
},
{
"Id": "51e4ef33-52ad-4cb8-b571-5aa791adeda8",
"HostAndPort": "http://localhost:5002"
},
{
"Id": "ce79e5fe-9c44-4c4b-9b64-4ee4bb124177",
"HostAndPort": "http://localhost:5003"
},
{
"Id": "9a589455-24b4-46fe-a243-49e7fbf1c028",
"HostAndPort": "http://localhost:5004"
},
{
"Id": "41f54603-9898-4850-ae40-29b40630110e",
"HostAndPort": "http://localhost:5001"
}
]
Expand Down
Loading

0 comments on commit 9157fb0

Please sign in to comment.