-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
131 lines (102 loc) · 3.55 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System.Text.Json;
using DotNext.Net.Cluster;
using DotNext.Net.Cluster.Consensus.Raft;
using DotNext.Net.Cluster.Consensus.Raft.Http;
using DotNext.Net.Cluster.Messaging;
using Microsoft.AspNetCore.Connections;
using Raft3DockerClusterExample;
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddEnvironmentVariables("CLUSTER_");
var services = builder.Services;
var logprefix = builder.Configuration.GetSection("HttpClusterMember").Get<Uri>()?.ToString()?.Split("_").Last()?.Replace('/', ':') ?? throw new MissingFieldException("HttpClusterMember");
builder.Logging.ClearProviders();
builder.Logging.AddSimpleConsole(o =>
{
o.IncludeScopes = false;
o.SingleLine = true;
o.TimestampFormat = "HH:mm:ss.fff "+logprefix;
o.UseUtcTimestamp = true;
});
void ConfigureRaftCluster()
{
builder.JoinCluster();
//services.AddSingleton<MetricsCollector, Metrics>();
var me = builder.Configuration.GetSection("HttpClusterMember").Get<Uri>() ?? throw new MissingFieldException("HttpClusterMember");
var knownMembers = builder.Configuration.GetSection("HttpClusterMembers").Get<Uri[]>() ?? throw new MissingFieldException("HttpClusterMembers");
services.Configure<HttpClusterMemberConfiguration>(o =>
{
o.PublicEndPoint = me;
//o.LowerElectionTimeout = (int)TimeSpan.FromSeconds(5).TotalMilliseconds;
//o.UpperElectionTimeout = (int)TimeSpan.FromSeconds(10).TotalMilliseconds;
o.Id = ClusterMemberId.FromEndPoint(new UriEndPoint(me));
Console.WriteLine($"I am {me} {o.Id}");
o.ColdStart = knownMembers.Length <= 1;
});
services.UseInMemoryConfigurationStorage(endPoints =>
{
foreach (var peerUri in knownMembers)
{
var peer = new UriEndPoint(peerUri);
var peerId = ClusterMemberId.FromEndPoint(peer);
Console.WriteLine($"peer {peer} id {peerId}");
endPoints.Add(peerId, peer);
}
});
services.ConfigureCluster<ClusterConfigurator>();
services.AddSingleton<IHttpMessageHandlerFactory, RaftClientHandlerFactory>();
services.AddSingleton<IInputChannel, TryMeHandler>();
services.AddSingleton<IInputChannel, TryMeBroadcastHandler>();
}
ConfigureRaftCluster();
services.AddRazorPages();
services.AddControllers();
var app = builder.Build();
app.UseConsensusProtocolHandler()
.RedirectToLeader("/api/leader");
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
app.MapControllers();
app.Run();
public class Metrics : HttpMetricsCollector
{
private readonly ILogger<Metrics> _logger;
public Metrics(ILogger<Metrics> logger)
{
_logger = logger;
}
public override void ReportResponseTime(TimeSpan value)
{
_logger.LogInformation($"ResponseTime {value.TotalMilliseconds:F2}ms");
base.ReportResponseTime(value);
}
public override void MovedToCandidateState()
{
base.MovedToCandidateState();
}
public override void MovedToFollowerState()
{
base.MovedToFollowerState();
}
public override void MovedToLeaderState()
{
base.MovedToLeaderState();
}
public override void ReportBroadcastTime(TimeSpan value)
{
base.ReportBroadcastTime(value);
}
public override void ReportHeartbeat()
{
base.ReportHeartbeat();
}
}