-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Raft fixes + ASP.NET Plugin that will redirect HTTP requests to the l…
…eader #20 Signed-off-by: Tomasz Maruszak <maruszaktomasz@gmail.com>
- Loading branch information
Showing
40 changed files
with
596 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
kubectl delete -f deployment.yaml | ||
kubectl delete -f service.yaml | ||
#kubectl delete -f service.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
src/Samples/SlimCluster.Samples.Service/State/Logs/AbstractCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
namespace SlimCluster.Samples.ConsoleApp.State.Logs; | ||
|
||
public abstract record AbstractCommand | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ metadata: | |
spec: | ||
type: NodePort | ||
ports: | ||
- port: 5000 | ||
- port: 8080 | ||
protocol: TCP | ||
selector: | ||
run: sc-service |
12 changes: 12 additions & 0 deletions
12
src/SlimCluster.AspNetCore/Configuration/ClusterAspNetOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace SlimCluster.Consensus.Raft; | ||
|
||
using Microsoft.AspNetCore.Http; | ||
|
||
public class ClusterAspNetOptions | ||
{ | ||
/// <summary> | ||
/// Selects the request to be routed to the Leader node of the cluster. When not set then no requests are being routed to leader. | ||
/// </summary> | ||
public Func<HttpRequest, bool>? DelegateRequestToLeader { get; set; } | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
src/SlimCluster.AspNetCore/Configuration/ClusterConfigurationExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
namespace SlimCluster.Consensus.Raft; | ||
|
||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
using SlimCluster.AspNetCore; | ||
|
||
public static class ClusterConfigurationExtensions | ||
{ | ||
public static ClusterConfiguration AddAspNetCore(this ClusterConfiguration cfg, Action<ClusterAspNetOptions> options) | ||
{ | ||
cfg.PostConfigurationActions.Add(services => | ||
{ | ||
services.AddHttpClient<RequestDelegatingClient>(); | ||
services.Configure(options); | ||
}); | ||
return cfg; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/SlimCluster.AspNetCore/Middleware/ClusterLeaderRequestDelegatingMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
namespace SlimCluster.AspNetCore; | ||
|
||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Options; | ||
|
||
using SlimCluster.Consensus.Raft; | ||
|
||
public class ClusterLeaderRequestDelegatingMiddleware | ||
{ | ||
private readonly RequestDelegate _next; | ||
private readonly ICluster _cluster; | ||
private readonly RequestDelegatingClient _requestDelegatingClient; | ||
private readonly ClusterAspNetOptions _options; | ||
|
||
public ClusterLeaderRequestDelegatingMiddleware(RequestDelegate next, ICluster cluster, RequestDelegatingClient requestDelegatingClient, IOptions<ClusterAspNetOptions> options) | ||
{ | ||
_next = next; | ||
_cluster = cluster; | ||
_requestDelegatingClient = requestDelegatingClient; | ||
_options = options.Value; | ||
} | ||
|
||
public async Task InvokeAsync(HttpContext context) | ||
{ | ||
// Check if request should be routed to leader | ||
if (_options.DelegateRequestToLeader != null && _options.DelegateRequestToLeader(context.Request)) | ||
{ | ||
var leaderAddress = _cluster.LeaderNode?.Address; | ||
if (leaderAddress == null) | ||
{ | ||
throw new ClusterException("Leader not known at this time, retry request later on when leader is established"); | ||
} | ||
|
||
if (!_cluster.SelfNode.Equals(_cluster.LeaderNode)) | ||
{ | ||
// This is a follower, so need to delegate the call to the leader | ||
await _requestDelegatingClient.Delegate(context.Request, context.Response, leaderAddress, localPort: context.Connection.LocalPort); | ||
return; | ||
} | ||
} | ||
// Not subject for routing, or this is the leader node (safe to pass the request processing by self) | ||
await _next(context); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/SlimCluster.AspNetCore/Middleware/ClusterLeaderRequestDelegatingMiddlewareExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace SlimCluster.AspNetCore; | ||
|
||
using Microsoft.AspNetCore.Builder; | ||
|
||
public static class ClusterLeaderRequestDelegatingMiddlewareExtensions | ||
{ | ||
/// <summary> | ||
/// Use the Request Delegation to Leader node (if self is not a leader). | ||
/// </summary> | ||
/// <param name="builder"></param> | ||
/// <returns></returns> | ||
public static IApplicationBuilder UseClusterLeaderRequestDelegation(this IApplicationBuilder builder) | ||
=> builder.UseMiddleware<ClusterLeaderRequestDelegatingMiddleware>(); | ||
} |
Oops, something went wrong.