-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathOutsideRuntimeClient.cs
More file actions
593 lines (507 loc) · 23.7 KB
/
Copy pathOutsideRuntimeClient.cs
File metadata and controls
593 lines (507 loc) · 23.7 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Orleans.ClientObservers;
using Orleans.CodeGeneration;
using Orleans.Configuration;
using Orleans.Messaging;
using Orleans.Runtime;
using Orleans.Serialization;
using Orleans.Serialization.Invocation;
using static Orleans.Internal.StandardExtensions;
#nullable disable
namespace Orleans
{
internal partial class OutsideRuntimeClient : IRuntimeClient, IDisposable, IClusterConnectionStatusListener
{
internal static bool TestOnlyThrowExceptionDuringInit { get; set; }
private readonly ILogger logger;
private readonly ClientMessagingOptions clientMessagingOptions;
private readonly StripedCallbackDictionary<CallbackData> callbacks;
private InvokableObjectManager localObjects;
private bool disposing;
private bool disposed;
private readonly MessagingTrace messagingTrace;
private readonly InterfaceToImplementationMappingCache _interfaceToImplementationMapping;
private readonly ApplicationRequestInstruments _applicationRequestInstruments;
private IGrainCallCancellationManager _cancellationManager;
private IClusterConnectionStatusObserver[] _statusObservers;
public IInternalGrainFactory InternalGrainFactory { get; private set; }
private ClientClusterManifestProvider _manifestProvider;
private MessageFactory messageFactory;
private readonly LocalClientDetails _localClientDetails;
private readonly ILoggerFactory loggerFactory;
private readonly SharedCallbackData sharedCallbackData;
private readonly PeriodicTimer callbackTimer;
private Task callbackTimerTask;
public GrainAddress CurrentActivationAddress
{
get;
private set;
}
public ClientGatewayObserver gatewayObserver { get; private set; }
public string CurrentActivationIdentity
{
get { return CurrentActivationAddress.ToString(); }
}
public IGrainReferenceRuntime GrainReferenceRuntime { get; private set; }
internal ClientMessageCenter MessageCenter { get; private set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope",
Justification = "MessageCenter is IDisposable but cannot call Dispose yet as it lives past the end of this method call.")]
public OutsideRuntimeClient(
LocalClientDetails localClientDetails,
ILoggerFactory loggerFactory,
IOptions<ClientMessagingOptions> clientMessagingOptions,
MessagingTrace messagingTrace,
IServiceProvider serviceProvider,
TimeProvider timeProvider,
InterfaceToImplementationMappingCache interfaceToImplementationMapping,
OrleansInstruments orleansInstruments)
{
TimeProvider = timeProvider;
_interfaceToImplementationMapping = interfaceToImplementationMapping;
_applicationRequestInstruments = new(orleansInstruments);
this.ServiceProvider = serviceProvider;
_localClientDetails = localClientDetails;
this.loggerFactory = loggerFactory;
this.messagingTrace = messagingTrace;
this.logger = loggerFactory.CreateLogger<OutsideRuntimeClient>();
callbacks = new StripedCallbackDictionary<CallbackData>();
this.clientMessagingOptions = clientMessagingOptions.Value;
var period = Max(
TimeSpan.FromMilliseconds(1),
Min(
this.clientMessagingOptions.ResponseTimeout,
TimeSpan.FromSeconds(1)));
this.callbackTimer = new PeriodicTimer(period, timeProvider);
this.sharedCallbackData = new SharedCallbackData(
msg => this.UnregisterCallback(msg.Id),
this.loggerFactory.CreateLogger<CallbackData>(),
this.clientMessagingOptions.ResponseTimeout,
this.clientMessagingOptions.CancelRequestOnTimeout,
this.clientMessagingOptions.WaitForCancellationAcknowledgement,
null);
}
internal void ConsumeServices()
{
try
{
_statusObservers = this.ServiceProvider.GetServices<IClusterConnectionStatusObserver>().ToArray();
_manifestProvider = ServiceProvider.GetRequiredService<ClientClusterManifestProvider>();
this.InternalGrainFactory = this.ServiceProvider.GetRequiredService<IInternalGrainFactory>();
_cancellationManager = sharedCallbackData.CancellationManager = ServiceProvider.GetRequiredService<IGrainCallCancellationManager>();
this.messageFactory = this.ServiceProvider.GetService<MessageFactory>();
this.localObjects = new InvokableObjectManager(
ServiceProvider.GetRequiredService<ClientGrainContext>(),
this,
ServiceProvider.GetRequiredService<DeepCopier>(),
messagingTrace,
ServiceProvider.GetRequiredService<DeepCopier<Response>>(),
_interfaceToImplementationMapping,
loggerFactory.CreateLogger<ClientGrainContext>());
this.callbackTimerTask = Task.Run(MonitorCallbackExpiry);
this.GrainReferenceRuntime = this.ServiceProvider.GetRequiredService<IGrainReferenceRuntime>();
// Client init / sign-on message
LogStartingClient(logger, RuntimeVersion.Current, _localClientDetails.ClientAddress, _localClientDetails.ClientId);
if (TestOnlyThrowExceptionDuringInit)
{
throw new InvalidOperationException("TestOnlyThrowExceptionDuringInit");
}
}
catch (Exception exc)
{
LogConstructorException(logger, exc);
ConstructorReset();
throw;
}
}
public IServiceProvider ServiceProvider { get; private set; }
public TimeProvider TimeProvider { get; }
public async Task StartAsync(CancellationToken cancellationToken)
{
// Deliberately avoid capturing the current synchronization context during startup and execute on the default scheduler.
// This helps to avoid any issues (such as deadlocks) caused by executing with the client's synchronization context/scheduler.
await Task.Run(() => this.StartInternal(cancellationToken)).ConfigureAwait(false);
LogStartedClient(logger, CurrentActivationAddress, _localClientDetails.ClientId);
}
public async Task StopAsync(CancellationToken cancellationToken)
{
this.callbackTimer.Dispose();
if (this.callbackTimerTask is { } task)
{
await task.WaitAsync(cancellationToken);
}
if (MessageCenter is { } messageCenter)
{
await messageCenter.StopAsync(cancellationToken);
}
if (_manifestProvider is { } provider)
{
await provider.StopAsync(cancellationToken);
}
ConstructorReset();
}
// used for testing to (carefully!) allow two clients in the same process
private async Task StartInternal(CancellationToken cancellationToken)
{
var retryFilter = ServiceProvider.GetService<IClientConnectionRetryFilter>();
var gatewayManager = this.ServiceProvider.GetRequiredService<GatewayManager>();
await ExecuteWithRetries(
async () => await gatewayManager.StartAsync(cancellationToken),
retryFilter,
cancellationToken);
MessageCenter = ActivatorUtilities.CreateInstance<ClientMessageCenter>(this.ServiceProvider);
MessageCenter.RegisterLocalMessageHandler(this.HandleMessage);
await ExecuteWithRetries(
async () => await MessageCenter.StartAsync(cancellationToken),
retryFilter,
cancellationToken);
CurrentActivationAddress = GrainAddress.NewActivationAddress(MessageCenter.MyAddress, _localClientDetails.ClientId.GrainId);
this.gatewayObserver = new ClientGatewayObserver(gatewayManager);
this.InternalGrainFactory.CreateObjectReference<IClientGatewayObserver>(this.gatewayObserver);
await ExecuteWithRetries(
_manifestProvider.StartAsync,
retryFilter,
cancellationToken);
static async Task ExecuteWithRetries(Func<Task> task, IClientConnectionRetryFilter retryFilter, CancellationToken cancellationToken)
{
do
{
try
{
await task();
return;
}
catch (Exception exception) when (retryFilter is not null && !cancellationToken.IsCancellationRequested)
{
var shouldRetry = await retryFilter.ShouldRetryConnectionAttempt(exception, cancellationToken);
if (cancellationToken.IsCancellationRequested || !shouldRetry)
{
throw;
}
}
}
while (!cancellationToken.IsCancellationRequested);
}
}
private void HandleMessage(Message message)
{
switch (message.Direction)
{
case Message.Directions.Response:
{
ReceiveResponse(message);
break;
}
case Message.Directions.OneWay:
case Message.Directions.Request:
{
this.localObjects.Dispatch(message);
break;
}
default:
LogMessageNotSupported(logger, message);
break;
}
}
public void SendResponse(Message request, Response response)
{
ThrowIfDisposed();
var message = this.messageFactory.CreateResponseMessage(request);
OrleansOutsideRuntimeClientEvent.Instance.SendResponse(message);
message.BodyObject = response;
MessageCenter.SendMessage(message);
}
public void SendRequest(GrainReference target, IInvokable request, IResponseCompletionSource context, InvokeMethodOptions options)
{
ThrowIfDisposed();
var cancellationToken = request.GetCancellationToken();
cancellationToken.ThrowIfCancellationRequested();
var message = this.messageFactory.CreateMessage(request, options);
OrleansOutsideRuntimeClientEvent.Instance.SendRequest(message);
message.InterfaceType = target.InterfaceType;
message.InterfaceVersion = target.InterfaceVersion;
var targetGrainId = target.GrainId;
var oneWay = (options & InvokeMethodOptions.OneWay) != 0;
message.SendingGrain = CurrentActivationAddress.GrainId;
message.TargetGrain = targetGrainId;
if (SystemTargetGrainId.TryParse(targetGrainId, out var systemTargetGrainId))
{
// If the silo isn't be supplied, it will be filled in by the sender to be the gateway silo
message.TargetSilo = systemTargetGrainId.GetSiloAddress();
}
if (this.clientMessagingOptions.DropExpiredMessages && message.IsExpirableMessage())
{
// don't set expiration for system target messages.
var ttl = request.GetDefaultResponseTimeout() ?? this.clientMessagingOptions.ResponseTimeout;
message.TimeToLive = ttl;
}
if (!oneWay)
{
var callbackData = new CallbackData(this.sharedCallbackData, context, message, _applicationRequestInstruments);
callbackData.SubscribeForCancellation(cancellationToken);
callbacks.TryAdd(message.Id, callbackData);
}
else
{
context?.Complete();
}
LogSendingMessage(logger, message);
MessageCenter.SendMessage(message);
}
public void ReceiveResponse(Message response)
{
OrleansOutsideRuntimeClientEvent.Instance.ReceiveResponse(response);
LogReceivedMessage(logger, response);
if (response.Result is Message.ResponseTypes.Status)
{
var status = (StatusResponse)response.BodyObject;
callbacks.TryGetValue(response.Id, out var callback);
var request = callback?.Message;
if (request is not null)
{
callback.OnStatusUpdate(status);
if (status.Diagnostics != null && status.Diagnostics.Count > 0)
{
LogReceivedStatusUpdateForPendingRequest(logger, request, new(status.Diagnostics));
}
}
else
{
if (clientMessagingOptions.CancelUnknownRequestOnStatusUpdate)
{
// Cancel the call since the caller has abandoned it.
// Note that the target and sender arguments are swapped because this is a response to the original request.
_cancellationManager?.SignalCancellation(
response.SendingSilo,
targetGrainId: response.SendingGrain,
sendingGrainId: response.TargetGrain,
messageId: response.Id);
}
if (status.Diagnostics != null && status.Diagnostics.Count > 0)
{
LogReceivedStatusUpdateForUnknownRequest(logger, response, new(status.Diagnostics));
}
}
return;
}
CallbackData callbackData;
var found = callbacks.TryRemove(response.Id, out callbackData);
if (found)
{
// We need to import the RequestContext here as well.
// Unfortunately, it is not enough, since CallContext.LogicalGetData will not flow "up" from task completion source into the resolved task.
// RequestContextExtensions.Import(response.RequestContextData);
callbackData.DoCallback(response);
}
else
{
LogDebugNoCallbackForResponseMessage(logger, response);
}
}
private void UnregisterCallback(CorrelationId id)
{
callbacks.TryRemove(id, out _);
}
private void ConstructorReset()
{
Utils.SafeExecute(() => this.Dispose());
}
/// <inheritdoc />
public TimeSpan GetResponseTimeout() => this.sharedCallbackData.ResponseTimeout;
/// <inheritdoc />
public void SetResponseTimeout(TimeSpan timeout) => this.sharedCallbackData.ResponseTimeout = timeout;
public IAddressable CreateObjectReference(IAddressable obj)
{
if (obj is GrainReference)
throw new ArgumentException("Argument obj is already a grain reference.", nameof(obj));
if (obj is IGrainBase)
throw new ArgumentException("Argument must not be a grain class.", nameof(obj));
var observerId = obj is ClientObserver clientObserver
? clientObserver.GetObserverGrainId(_localClientDetails.ClientId)
: ObserverGrainId.Create(_localClientDetails.ClientId);
var reference = this.InternalGrainFactory.GetGrain(observerId.GrainId);
if (!localObjects.TryRegister(obj, observerId))
{
throw new ArgumentException($"Failed to add new observer {reference} to localObjects collection.", "reference");
}
return reference;
}
public void DeleteObjectReference(IAddressable obj)
{
if (!(obj is GrainReference reference))
{
throw new ArgumentException("Argument reference is not a grain reference.");
}
if (!ObserverGrainId.TryParse(reference.GrainId, out var observerId))
{
throw new ArgumentException($"Reference {reference.GrainId} is not an observer reference");
}
if (!localObjects.TryDeregister(observerId))
{
throw new ArgumentException("Reference is not associated with a local object.", "reference");
}
}
public void Dispose()
{
if (this.disposing) return;
this.disposing = true;
Utils.SafeExecute(() => this.callbackTimer.Dispose());
Utils.SafeExecute(() => MessageCenter?.Dispose());
GC.SuppressFinalize(this);
disposed = true;
}
public void BreakOutstandingMessagesToSilo(SiloAddress deadSilo)
{
foreach (var callback in callbacks)
{
if (deadSilo.Equals(callback.Value.Message.TargetSilo))
{
callback.Value.OnTargetSiloFail();
}
}
}
public int GetRunningRequestsCount(GrainInterfaceType grainInterfaceType)
=> this.callbacks.CountWhere(c => c.Value.Message.InterfaceType == grainInterfaceType);
/// <inheritdoc />
public void NotifyClusterConnectionLost()
{
foreach (var observer in _statusObservers)
{
try
{
observer.NotifyClusterConnectionLost();
}
catch (Exception ex)
{
LogErrorSendingClusterDisconnectionNotification(logger, ex);
}
}
}
/// <inheritdoc />
public void NotifyGatewayCountChanged(int currentNumberOfGateways, int previousNumberOfGateways)
{
foreach (var observer in _statusObservers)
{
try
{
observer.NotifyGatewayCountChanged(
currentNumberOfGateways,
previousNumberOfGateways,
currentNumberOfGateways > 0 && previousNumberOfGateways <= 0);
}
catch (Exception ex)
{
LogErrorSendingGatewayCountChangedNotification(logger, ex);
}
}
}
private async Task MonitorCallbackExpiry()
{
while (await callbackTimer.WaitForNextTickAsync())
{
try
{
var currentStopwatchTicks = ValueStopwatch.GetTimestamp();
foreach (var (_, callback) in callbacks)
{
if (callback.IsCompleted)
{
continue;
}
if (callback.IsExpired(currentStopwatchTicks))
{
callback.OnTimeout();
}
}
}
catch (Exception ex)
{
LogErrorWhileProcessingCallbackExpiry(logger, ex);
}
}
}
private void ThrowIfDisposed()
{
if (disposed)
{
ThrowObjectDisposedException();
}
void ThrowObjectDisposedException() => throw new ObjectDisposedException(nameof(OutsideRuntimeClient));
}
[LoggerMessage(
Level = LogLevel.Information,
EventId = (int)ErrorCode.ClientStarting,
Message = "Starting Orleans client with runtime version '{RuntimeVersion}', local address '{LocalAddress}' and client id '{ClientId}'."
)]
private static partial void LogStartingClient(ILogger logger, string runtimeVersion, SiloAddress localAddress, ClientGrainId clientId);
[LoggerMessage(
Level = LogLevel.Error,
EventId = (int)ErrorCode.Runtime_Error_100319,
Message = "OutsideRuntimeClient constructor failed."
)]
private static partial void LogConstructorException(ILogger logger, Exception exc);
[LoggerMessage(
Level = LogLevel.Information,
EventId = (int)ErrorCode.ProxyClient_StartDone,
Message = "Started client with address '{ActivationAddress}' and id '{ClientId}'."
)]
private static partial void LogStartedClient(ILogger logger, GrainAddress activationAddress, ClientGrainId clientId);
[LoggerMessage(
Level = LogLevel.Trace,
Message = "Send '{Message}'."
)]
private static partial void LogSendingMessage(ILogger logger, Message message);
[LoggerMessage(
Level = LogLevel.Trace,
Message = "Received '{Message}'."
)]
private static partial void LogReceivedMessage(ILogger logger, Message message);
[LoggerMessage(
Level = LogLevel.Error,
Message = "Message not supported: '{Message}'."
)]
private static partial void LogMessageNotSupported(ILogger logger, Message message);
[LoggerMessage(
Level = LogLevel.Warning,
Message = "Error sending cluster disconnection notification."
)]
private static partial void LogErrorSendingClusterDisconnectionNotification(ILogger logger, Exception ex);
[LoggerMessage(
Level = LogLevel.Warning,
Message = "Error sending gateway count changed notification."
)]
private static partial void LogErrorSendingGatewayCountChangedNotification(ILogger logger, Exception ex);
[LoggerMessage(
Level = LogLevel.Warning,
Message = "Error while processing callback expiry."
)]
private static partial void LogErrorWhileProcessingCallbackExpiry(ILogger logger, Exception ex);
[LoggerMessage(
Level = LogLevel.Debug,
EventId = (int)ErrorCode.Runtime_Error_100011,
Message = "No callback for response message '{ResponseMessage}'"
)]
private static partial void LogDebugNoCallbackForResponseMessage(ILogger logger, Message responseMessage);
private readonly struct DiagnosticsLogData(List<string> diagnostics)
{
public override string ToString() => string.Join("\n", diagnostics);
}
[LoggerMessage(
Level = LogLevel.Information,
Message = "Received status update for pending request, Request: '{RequestMessage}'. Status: '{Diagnostics}'."
)]
private static partial void LogReceivedStatusUpdateForPendingRequest(ILogger logger, Message requestMessage, DiagnosticsLogData diagnostics);
[LoggerMessage(
Level = LogLevel.Information,
Message = "Received status update for unknown request. Message: '{StatusMessage}'. Status: '{Diagnostics}'."
)]
private static partial void LogReceivedStatusUpdateForUnknownRequest(ILogger logger, Message statusMessage, DiagnosticsLogData diagnostics);
}
}