Skip to content

Commit

Permalink
Merge pull request #3 from TomPallister/feature/refactor
Browse files Browse the repository at this point in the history
Feature/refactor
  • Loading branch information
TomPallister authored Apr 3, 2017
2 parents 5773b8c + 6120fb8 commit bb3bf8a
Show file tree
Hide file tree
Showing 56 changed files with 490 additions and 219 deletions.
Empty file modified build.sh
100644 → 100755
Empty file.
7 changes: 0 additions & 7 deletions src/Rafty/BecomeLeader.cs

This file was deleted.

12 changes: 0 additions & 12 deletions src/Rafty/Command.cs

This file was deleted.

20 changes: 20 additions & 0 deletions src/Rafty/Commands/Command.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using Rafty.Messages;

namespace Rafty.Commands
{
public abstract class Command : ICommand, IMessage
{
protected Command()
{
MessageId = Guid.NewGuid();
}

public Guid MessageId { get; }
}

public interface ICommand
{

}
}
14 changes: 0 additions & 14 deletions src/Rafty/FakeCommand.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Rafty
namespace Rafty.Infrastructure
{
public interface IReportable
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;
using Newtonsoft.Json;
using Rafty.Commands;
using Rafty.Messages;
using Rafty.Messaging;
using Rafty.Raft;
using Rafty.ServiceDiscovery;
using Rafty.State;

namespace Rafty
namespace Rafty.Infrastructure
{
public static class RaftyConfigurationExtensions
{
Expand All @@ -21,11 +23,11 @@ public static IApplicationBuilder UseRafty(this IApplicationBuilder builder,
IStateMachine stateMachine,
IServiceRegistry serviceRegistry,
ILogger logger,
List<ServerInCluster> remoteServers,
IServersInCluster serversInCluster,
string raftyBasePath = null)
{
builder.UseRaftyForTesting(baseUri, messageSender, messageBus, stateMachine, serviceRegistry,
logger, remoteServers, raftyBasePath);
logger, serversInCluster, raftyBasePath);

return builder;
}
Expand All @@ -37,20 +39,20 @@ public static (IApplicationBuilder builder, Server server, ServerInCluster serve
IStateMachine stateMachine,
IServiceRegistry serviceRegistry,
ILogger logger,
List<ServerInCluster> remoteServers,
IServersInCluster serversInCluster,
string raftyBasePath = null)
{
var urlConfig = RaftyUrlConfig.Get(raftyBasePath);

var server = new Server(messageBus, remoteServers, stateMachine, logger);
var server = new Server(messageBus, serversInCluster, stateMachine, logger);

serviceRegistry.Register(new RegisterService(RaftyServiceDiscoveryName.Get(), server.Id, baseUri));

messageSender.SetServer(server);

var serverInCluster = new ServerInCluster(server.Id);

remoteServers.Add(serverInCluster);
serversInCluster.Add(serverInCluster);

builder.Map(urlConfig.appendEntriesUrl, app =>
{
Expand All @@ -60,7 +62,10 @@ public static (IApplicationBuilder builder, Server server, ServerInCluster serve
{
var reader = new StreamReader(context.Request.Body);
var content = reader.ReadToEnd();
var appendEntries = JsonConvert.DeserializeObject<AppendEntries>(content);
var appendEntries = JsonConvert.DeserializeObject<AppendEntries>(content, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All
});
var appendEntriesResponse = server.Receive(appendEntries);
await context.Response.WriteAsync(JsonConvert.SerializeObject(appendEntriesResponse));
}
Expand All @@ -79,7 +84,10 @@ public static (IApplicationBuilder builder, Server server, ServerInCluster serve
{
var reader = new StreamReader(context.Request.Body);
var content = reader.ReadToEnd();
var requestVote = JsonConvert.DeserializeObject<RequestVote>(content);
var requestVote = JsonConvert.DeserializeObject<RequestVote>(content, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All
});
var requestVoteResponse = server.Receive(requestVote);
await context.Response.WriteAsync(JsonConvert.SerializeObject(requestVoteResponse));
}
Expand All @@ -98,7 +106,10 @@ public static (IApplicationBuilder builder, Server server, ServerInCluster serve
{
var reader = new StreamReader(context.Request.Body);
var content = reader.ReadToEnd();
var command = JsonConvert.DeserializeObject<FakeCommand>(content);
var command = JsonConvert.DeserializeObject<Command>(content, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All
});
var sendCommandToLeaderResponse = server.Receive(command);
await context.Response.WriteAsync(JsonConvert.SerializeObject(sendCommandToLeaderResponse));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Rafty
namespace Rafty.Infrastructure
{
public static class RaftyUrlConfig
{
Expand Down
7 changes: 0 additions & 7 deletions src/Rafty/Leader.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using Rafty.State;

namespace Rafty
namespace Rafty.Messages
{
public class AppendEntries : Message
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System;

namespace Rafty
namespace Rafty.Messages
{
public class BecomeCandidate : Message
{
public BecomeCandidate(Guid lastAppendEntriesMessageIdFromLeader)
{
this.LastAppendEntriesMessageIdFromLeader = lastAppendEntriesMessageIdFromLeader;
LastAppendEntriesMessageIdFromLeader = lastAppendEntriesMessageIdFromLeader;
}
public Guid LastAppendEntriesMessageIdFromLeader { get; private set; }
}
Expand Down
6 changes: 3 additions & 3 deletions src/Rafty/IMessage.cs → src/Rafty/Messages/IMessage.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace Rafty
{
using System;
using System;

namespace Rafty.Messages
{
public interface IMessage
{
Guid MessageId { get; }
Expand Down
2 changes: 1 addition & 1 deletion src/Rafty/Message.cs → src/Rafty/Messages/Message.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace Rafty
namespace Rafty.Messages
{
public class Message : IMessage
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace Rafty
namespace Rafty.Messages
{
public class RequestVote : Message
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Rafty
namespace Rafty.Messages
{
public class SendHeartbeat : Message
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using Newtonsoft.Json;
using Rafty.Commands;

namespace Rafty
namespace Rafty.Messages
{
public class SendLeaderCommand : Message
{
Expand All @@ -11,13 +11,6 @@ public SendLeaderCommand(ICommand command, Guid leaderId)
LeaderId = leaderId;
}

[JsonConstructor]
public SendLeaderCommand(FakeCommand command, Guid leaderId)
{
Command = command;
LeaderId = leaderId;
}

public ICommand Command { get; private set; }
public Guid LeaderId { get; private set; }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Rafty
namespace Rafty.Messages
{
public class SendToSelf : Message
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Rafty.Commands;
using Rafty.Messages;
using Rafty.Responses;

namespace Rafty
namespace Rafty.Messaging
{
using System.Threading.Tasks;

public class FakeMessageBus : IMessageBus
{
public List<IMessage> SendToSelfMessages { get; private set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

namespace Rafty
using Rafty.Commands;
using Rafty.Infrastructure;
using Rafty.Messages;
using Rafty.Raft;
using Rafty.Responses;
using Rafty.ServiceDiscovery;

namespace Rafty.Messaging
{
using System.Threading;
using System.Threading.Tasks;

public class HttpClientMessageSender : IMessageSender
{
private readonly IServiceRegistry _serviceRegistry;
Expand All @@ -20,16 +26,17 @@ public class HttpClientMessageSender : IMessageSender
private readonly string _appendEntriesUrl;
private readonly string _requestVoteUrl;
private readonly string _commandUrl;
private readonly ILogger _logger;

public HttpClientMessageSender(IServiceRegistry serviceRegistry, string raftyBasePath = null)
public HttpClientMessageSender(IServiceRegistry serviceRegistry, ILogger logger, string raftyBasePath = null)
{
var urlConfig = RaftyUrlConfig.Get(raftyBasePath);

_appendEntriesUrl = urlConfig.appendEntriesUrl;
_requestVoteUrl = urlConfig.requestVoteUrl;
_commandUrl = urlConfig.commandUrl;

_serviceRegistry = serviceRegistry;
_logger = logger;
_sendToSelfHandlers = new Dictionary<Type, Action<IMessage>>
{
{typeof(BecomeCandidate), x => _server.Receive((BecomeCandidate) x)},
Expand All @@ -43,7 +50,10 @@ public async Task<AppendEntriesResponse> Send(AppendEntries appendEntries)
try
{
var serverToSendMessageTo = _serviceRegistry.Get(RaftyServiceDiscoveryName.Get()).First(x => x.Id == appendEntries.FollowerId);
var json = JsonConvert.SerializeObject(appendEntries);
var json = JsonConvert.SerializeObject(appendEntries, Formatting.None, new JsonSerializerSettings
{
TypeNameHandling= TypeNameHandling.All
});
var httpContent = new StringContent(json);
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

Expand All @@ -59,7 +69,7 @@ public async Task<AppendEntriesResponse> Send(AppendEntries appendEntries)
}
catch (Exception exception)
{
Console.WriteLine(exception);
_logger.LogError(new EventId(1), exception, "Error in Send(AppendEntries appendEntries)");
throw;
}
}
Expand All @@ -69,7 +79,10 @@ public async Task<RequestVoteResponse> Send(RequestVote requestVote)
try
{
var serverToSendMessageTo = _serviceRegistry.Get(RaftyServiceDiscoveryName.Get()).First(x => x.Id == requestVote.VoterId);
var json = JsonConvert.SerializeObject(requestVote);
var json = JsonConvert.SerializeObject(requestVote, Formatting.None, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All
});
var httpContent = new StringContent(json);
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

Expand All @@ -85,7 +98,7 @@ public async Task<RequestVoteResponse> Send(RequestVote requestVote)
}
catch (Exception exception)
{
Console.WriteLine(exception);
_logger.LogError(new EventId(1), exception, "Error in Send(RequestVote requestVote)");
throw;
}
}
Expand All @@ -95,7 +108,10 @@ public async Task<SendLeaderCommandResponse> Send(ICommand command, Guid leaderI
try
{
var serverToSendMessageTo = _serviceRegistry.Get(RaftyServiceDiscoveryName.Get()).First(x => x.Id == leaderId);
var json = JsonConvert.SerializeObject(command);
var json = JsonConvert.SerializeObject(command, Formatting.None, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All
});
var httpContent = new StringContent(json);
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

Expand All @@ -111,7 +127,7 @@ public async Task<SendLeaderCommandResponse> Send(ICommand command, Guid leaderI
}
catch (Exception exception)
{
Console.WriteLine(exception);
_logger.LogError(new EventId(1), exception, "Error in Send(ICommand command, Guid leaderId)");
throw;
}
}
Expand Down Expand Up @@ -151,7 +167,5 @@ public void SetServer(Server server)
{
_server = server;
}


}
}
Loading

0 comments on commit bb3bf8a

Please sign in to comment.