Skip to content

Commit

Permalink
refactoring tests and namspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
TomPallister committed May 27, 2018
1 parent 72732d8 commit 6b5fdd1
Show file tree
Hide file tree
Showing 53 changed files with 385 additions and 290 deletions.
2 changes: 1 addition & 1 deletion Rafty.sln
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
version.ps1 = version.ps1
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rafty.IntegrationTests", "test\Rafty.IntegrationTests\Rafty.IntegrationTests.csproj", "{BBB04228-BC0E-4711-9C80-769C5FAD777A}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Rafty.IntegrationTests", "test\Rafty.IntegrationTests\Rafty.IntegrationTests.csproj", "{BBB04228-BC0E-4711-9C80-769C5FAD777A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
3 changes: 2 additions & 1 deletion src/Rafty/Concensus/Messages/AppendEntries.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
namespace Rafty.Concensus
namespace Rafty.Concensus.Messages
{
using System;
using System.Collections.Generic;
using Infrastructure;
using Log;

public sealed class AppendEntries : Message
Expand Down
3 changes: 1 addition & 2 deletions src/Rafty/Concensus/Messages/AppendEntriesBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace Rafty.Concensus
namespace Rafty.Concensus.Messages
{
using System;
using System.Collections.Generic;
using Log;

Expand Down
2 changes: 1 addition & 1 deletion src/Rafty/Concensus/Messages/AppendEntriesResponse.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Rafty.Concensus
namespace Rafty.Concensus.Messages
{
public sealed class AppendEntriesResponse
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Rafty.Concensus
namespace Rafty.Concensus.Messages
{
public class AppendEntriesResponseBuilder
{
Expand Down
4 changes: 1 addition & 3 deletions src/Rafty/Concensus/Messages/RequestVote.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;

namespace Rafty.Concensus
namespace Rafty.Concensus.Messages
{
public sealed class RequestVote
{
Expand Down
4 changes: 1 addition & 3 deletions src/Rafty/Concensus/Messages/RequestVoteBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;

namespace Rafty.Concensus
namespace Rafty.Concensus.Messages
{
public class RequestVoteBuilder
{
Expand Down
2 changes: 1 addition & 1 deletion src/Rafty/Concensus/Messages/RequestVoteResponse.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Rafty.Concensus
namespace Rafty.Concensus.Messages
{
public sealed class RequestVoteResponse
{
Expand Down
2 changes: 1 addition & 1 deletion src/Rafty/Concensus/Messages/RequestVoteResponseBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Rafty.Concensus
namespace Rafty.Concensus.Messages
{
public class RequestVoteResponseBuilder
{
Expand Down
11 changes: 6 additions & 5 deletions src/Rafty/Concensus/Node/INode.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System;
using Rafty.FiniteStateMachine;

namespace Rafty.Concensus
namespace Rafty.Concensus.Node
{
using System.Threading.Tasks;
using FiniteStateMachine;
using Infrastructure;
using Messages;
using States;

public interface INode
{
Expand All @@ -13,7 +14,7 @@ public interface INode
void BecomeCandidate(CurrentState state);
Task<AppendEntriesResponse> Handle(AppendEntries appendEntries);
Task<RequestVoteResponse> Handle(RequestVote requestVote);
void Start(string id);
void Start(NodeId id);
void Stop();
Task<Response<T>> Accept<T>(T command) where T : ICommand;
}
Expand Down
29 changes: 15 additions & 14 deletions src/Rafty/Concensus/Node/Node.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Rafty.Concensus.States;
using Rafty.FiniteStateMachine;
using Rafty.Infrastructure;
using Rafty.Log;

namespace Rafty.Concensus
namespace Rafty.Concensus.Node
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FiniteStateMachine;
using Infrastructure;
using Log;
using Messages;
using Microsoft.Extensions.Logging;
using Peers;
using States;

public class Node : INode
{
Expand All @@ -32,8 +33,6 @@ public Node(
{
_loggerFactory = loggerFactory;
_logger = _loggerFactory.CreateLogger<Node>();
//dont think rules should be injected at the moment..EEK UNCLE BOB
_rules = new Rules();
_fsm = fsm;
_log = log;
_random = new RandomDelay();
Expand All @@ -48,11 +47,13 @@ public Node(

public IState State { get; private set; }

public void Start(string id)
public void Start(NodeId id)
{
if(State?.CurrentState == null)
_rules = new Rules(_loggerFactory, id);

if (State?.CurrentState == null)
{
BecomeFollower(new CurrentState(id, 0, default(string), 0, 0, default(string)));
BecomeFollower(new CurrentState(id.Id, 0, default(string), 0, 0, default(string)));
}
else
{
Expand Down
12 changes: 6 additions & 6 deletions src/Rafty/Concensus/Node/NodePeer.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;
using System.Net.Http;
using Rafty.Concensus;
using Rafty.FiniteStateMachine;

namespace Rafty.AcceptanceTests
namespace Rafty.Concensus.Node
{
using System;
using System.Threading.Tasks;
using FiniteStateMachine;
using Infrastructure;
using Messages;
using Peers;

public class NodePeer : IPeer
{
Expand Down
8 changes: 4 additions & 4 deletions src/Rafty/Concensus/Peers/IPeer.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using Rafty.FiniteStateMachine;

namespace Rafty.Concensus
namespace Rafty.Concensus.Peers
{
using System.Threading.Tasks;
using FiniteStateMachine;
using Infrastructure;
using Messages;

public interface IPeer
{
Expand Down
7 changes: 4 additions & 3 deletions src/Rafty/Concensus/Peers/Peer.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using Rafty.FiniteStateMachine;

namespace Rafty.Concensus
namespace Rafty.Concensus.Peers
{
using System;
using System.Threading.Tasks;
using FiniteStateMachine;
using Infrastructure;
using Messages;

public class Peer : IPeer
{
Expand Down
29 changes: 15 additions & 14 deletions src/Rafty/Concensus/States/Candidate.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Rafty.Concensus.States;
using Rafty.FiniteStateMachine;
using Rafty.Log;

namespace Rafty.Concensus
namespace Rafty.Concensus.States
{
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using FiniteStateMachine;
using Infrastructure;
using Log;
using Messages;
using Microsoft.Extensions.Logging;
using Node;
using Peers;

public sealed class Candidate : IState
{
Expand Down Expand Up @@ -106,7 +107,7 @@ public async Task<AppendEntriesResponse> Handle(AppendEntries appendEntries)
return response.appendEntriesResponse;
}

await _rules.DeleteAnyConflictsInLog(appendEntries, _log, _logger, CurrentState.Id);
await _rules.DeleteAnyConflictsInLog(appendEntries, _log);

if (_applied > 1 && appendEntries.Entries.Any())
{
Expand All @@ -117,7 +118,7 @@ public async Task<AppendEntriesResponse> Handle(AppendEntries appendEntries)

_logger.LogInformation($"{CurrentState.Id} as {nameof(Candidate)} applying entry to log");

await _rules.ApplyNewEntriesToLog(appendEntries, _log, _logger, CurrentState.Id);
await _rules.ApplyNewEntriesToLog(appendEntries, _log);

var commitIndexAndLastApplied = await _rules.CommitIndexAndLastApplied(appendEntries, _log, CurrentState);

Expand Down
4 changes: 1 addition & 3 deletions src/Rafty/Concensus/States/CurrentState.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
namespace Rafty.Concensus
namespace Rafty.Concensus.States
{
using System;
using System.Collections.Generic;
using Rafty.Log;

public class CurrentState
{
Expand Down
28 changes: 14 additions & 14 deletions src/Rafty/Concensus/States/Follower.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualBasic;
using Newtonsoft.Json;
using Rafty.Concensus.States;
using Rafty.FiniteStateMachine;
using Rafty.Log;

namespace Rafty.Concensus
namespace Rafty.Concensus.States
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using FiniteStateMachine;
using Infrastructure;
using Log;
using Messages;
using Microsoft.Extensions.Logging;
using Node;
using Peers;

public sealed class Follower : IState
{
Expand Down Expand Up @@ -72,13 +72,13 @@ public async Task<AppendEntriesResponse> Handle(AppendEntries appendEntries)
return response.appendEntriesResponse;
}

await _rules.DeleteAnyConflictsInLog(appendEntries, _log, _logger, CurrentState.Id);
await _rules.DeleteAnyConflictsInLog(appendEntries, _log);

var terms = appendEntries.Entries.Any() ? string.Join(",", appendEntries.Entries.Select(x => x.Term)) : string.Empty;

_logger.LogInformation($"{CurrentState.Id} as {nameof(Follower)} applying {appendEntries.Entries.Count} to log, term {terms}");

await _rules.ApplyNewEntriesToLog(appendEntries, _log, _logger, CurrentState.Id);
await _rules.ApplyNewEntriesToLog(appendEntries, _log);

var commitIndexAndLastApplied = await _rules.CommitIndexAndLastApplied(appendEntries, _log, CurrentState);

Expand Down
7 changes: 4 additions & 3 deletions src/Rafty/Concensus/States/IState.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using Rafty.FiniteStateMachine;

namespace Rafty.Concensus
namespace Rafty.Concensus.States
{
using System.Threading.Tasks;
using FiniteStateMachine;
using Infrastructure;
using Messages;

public interface IState
{
Expand Down
26 changes: 13 additions & 13 deletions src/Rafty/Concensus/States/Leader.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
namespace Rafty.Concensus
{
namespace Rafty.Concensus.States
{
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Rafty.Concensus.States;
using Rafty.FiniteStateMachine;
using Rafty.Log;
using System.Diagnostics;
using FiniteStateMachine;
using Infrastructure;
using Log;
using Messages;
using Microsoft.Extensions.Logging;
using Node;
using Peers;

public sealed class Leader : IState
{
private readonly IFiniteStateMachine _fsm;
Func<CurrentState, List<IPeer>> _getPeers;
readonly Func<CurrentState, List<IPeer>> _getPeers;
private readonly ILog _log;
private readonly INode _node;
private readonly ISettings _settings;
private IRules _rules;
private readonly object _lock = new object();
private bool _handled;
private Stopwatch _stopWatch;
private Timer _electionTimer;
public long SendAppendEntriesCount;
private bool _appendingEntries;
private ILogger<Leader> _logger;
private SemaphoreSlim _semaphore = new SemaphoreSlim(1,1);
private readonly ILogger<Leader> _logger;

public Leader(
CurrentState currentState,
Expand Down
4 changes: 3 additions & 1 deletion src/Rafty/Concensus/States/MatchIndex.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
namespace Rafty.Concensus
namespace Rafty.Concensus.States
{
using Peers;

public class MatchIndex
{
public MatchIndex(IPeer peer, int indexOfHighestKnownReplicatedLog)
Expand Down
4 changes: 3 additions & 1 deletion src/Rafty/Concensus/States/NextIndex.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
namespace Rafty.Concensus
namespace Rafty.Concensus.States
{
using Peers;

public class NextIndex
{
public NextIndex(IPeer peer, int nextLogIndexToSendToPeer)
Expand Down
4 changes: 3 additions & 1 deletion src/Rafty/Concensus/States/PeerState.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
namespace Rafty.Concensus
namespace Rafty.Concensus.States
{
using Peers;

public class PeerState
{
public PeerState(IPeer peer, MatchIndex matchIndex, NextIndex nextIndex)
Expand Down
Loading

0 comments on commit 6b5fdd1

Please sign in to comment.