|
1 | | -using CloudNative.CloudEvents; |
2 | | -using FaasNet.EventMesh.Client.Extensions; |
3 | | -using FaasNet.EventMesh.Client.Messages; |
4 | | -using FaasNet.EventMesh.Runtime.Events; |
| 1 | +using FaasNet.EventMesh.Client.Messages; |
5 | 2 | using FaasNet.EventMesh.Runtime.Exceptions; |
6 | 3 | using FaasNet.EventMesh.Runtime.Handlers; |
7 | 4 | using FaasNet.EventMesh.Runtime.Stores; |
| 5 | +using FaasNet.RaftConsensus.Core; |
| 6 | +using FaasNet.RaftConsensus.Core.Stores; |
8 | 7 | using Microsoft.Extensions.Logging; |
9 | 8 | using Microsoft.Extensions.Options; |
10 | 9 | using System; |
|
16 | 15 |
|
17 | 16 | namespace FaasNet.EventMesh.Runtime |
18 | 17 | { |
19 | | - public class RuntimeHost: IRuntimeHost |
| 18 | + public class EventMeshPeer: BasePeerHost |
20 | 19 | { |
21 | 20 | private readonly IEnumerable<IMessageHandler> _messageHandlers; |
22 | 21 | private readonly IEnumerable<IMessageConsumer> _messageConsumers; |
23 | 22 | private readonly IClientStore _clientStore; |
24 | 23 | private readonly IUdpClientServerFactory _udpClientFactory; |
25 | | - private readonly ILogger<RuntimeHost> _logger; |
| 24 | + private readonly ILogger<EventMeshPeer> _logger; |
26 | 25 | private readonly RuntimeOptions _options; |
27 | 26 | private CancellationTokenSource _tokenSource; |
28 | 27 | private CancellationToken _cancellationToken; |
29 | 28 | private UdpClient _udpClient; |
30 | 29 |
|
31 | | - public RuntimeHost( |
| 30 | + public EventMeshPeer(IEnumerable<IMessageHandler> messageHandlers, ILogger<BasePeerHost> logger, IOptions<ConsensusPeerOptions> options, IClusterStore clusterStore, ILogStore logStore, IPeerStore peerStore) : base(logger, options, clusterStore, logStore, peerStore) |
| 31 | + { |
| 32 | + _messageHandlers = messageHandlers; |
| 33 | + } |
| 34 | + |
| 35 | + protected override async Task<bool> HandlePackage(UdpReceiveResult udpResult) |
| 36 | + { |
| 37 | + var package = Package.Deserialize(new ReadBufferContext(udpResult.Buffer.ToArray())); |
| 38 | + using (var activity = EventMeshMeter.RequestActivitySource.StartActivity(package.Header.Command.Name)) |
| 39 | + { |
| 40 | + try |
| 41 | + { |
| 42 | + EventMeshMeter.IncrementNbIncomingRequest(); |
| 43 | + _logger.LogInformation("Command {command} is received with sequence {sequence}", package.Header.Command.Name, package.Header.Seq); |
| 44 | + var cmd = package.Header.Command; |
| 45 | + var messageHandler = _messageHandlers.First(m => m.Command == package.Header.Command); |
| 46 | + Package result = null; |
| 47 | + try |
| 48 | + { |
| 49 | + result = await messageHandler.Run(package, _cancellationToken); |
| 50 | + } |
| 51 | + catch (RuntimeException ex) |
| 52 | + { |
| 53 | + _logger.LogError("Command {command}, sequence {sequence}, exception {exception}", package.Header.Command.Name, package.Header.Seq, ex.ToString()); |
| 54 | + result = PackageResponseBuilder.Error(ex.SourceCommand, ex.SourceSeq, ex.Error); |
| 55 | + } |
| 56 | + catch (Exception ex) |
| 57 | + { |
| 58 | + _logger.LogError("Command {command}, sequence {sequence}, exception {exception}", package.Header.Command.Name, package.Header.Seq, ex.ToString()); |
| 59 | + result = PackageResponseBuilder.Error(package.Header.Command, package.Header.Seq, Errors.INTERNAL_ERROR); |
| 60 | + } |
| 61 | + |
| 62 | + if (result == null) |
| 63 | + { |
| 64 | + activity?.SetStatus(System.Diagnostics.ActivityStatusCode.Error); |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + EventMeshMeter.IncrementNbOutgoingRequest(); |
| 69 | + _logger.LogInformation("Command {command} with sequence {sequence} is going to be sent", result.Header.Command.Name, result.Header.Seq); |
| 70 | + var writeCtx = new WriteBufferContext(); |
| 71 | + result.Serialize(writeCtx); |
| 72 | + var resultPayload = writeCtx.Buffer.ToArray(); |
| 73 | + // await _udpClient.SendAsync(resultPayload, resultPayload.Count(), receiveResult.RemoteEndPoint).WithCancellation(_cancellationToken); |
| 74 | + activity?.SetStatus(System.Diagnostics.ActivityStatusCode.Ok); |
| 75 | + } |
| 76 | + catch (Exception) |
| 77 | + { |
| 78 | + activity?.SetStatus(System.Diagnostics.ActivityStatusCode.Error); |
| 79 | + throw; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return true; |
| 84 | + } |
| 85 | + |
| 86 | + /* |
| 87 | + public EventMeshPeer( |
32 | 88 | IEnumerable<IMessageHandler> messageHandlers, |
33 | 89 | IEnumerable<IMessageConsumer> messageConsumers, |
34 | 90 | IUdpClientServerFactory udpClientFactory, |
35 | | - ILogger<RuntimeHost> logger, |
| 91 | + ILogger<EventMeshPeer> logger, |
36 | 92 | IClientStore clientStore, |
37 | 93 | IOptions<RuntimeOptions> options) |
38 | 94 | { |
@@ -131,6 +187,8 @@ private async Task HandleEventMeshPackage() |
131 | 187 | EventMeshPackageReceived(this, new PackageEventArgs(package)); |
132 | 188 | } |
133 | 189 |
|
| 190 | + // La requête heartbeat peut être traitée que par le leader. |
| 191 | + // La requête hello message peut être traitée que par le leader. |
134 | 192 | using (var activity = EventMeshMeter.RequestActivitySource.StartActivity(package.Header.Command.Name)) |
135 | 193 | { |
136 | 194 | try |
@@ -238,5 +296,6 @@ private async Task HandleCloudEventReceived(object sender, CloudEventArgs e) |
238 | 296 | } |
239 | 297 |
|
240 | 298 | #endregion |
| 299 | + */ |
241 | 300 | } |
242 | 301 | } |
0 commit comments