/
Saga.cs
615 lines (540 loc) · 23.7 KB
/
Saga.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
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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Text;
using System.Text.Json.Nodes;
using Microsoft.Extensions.Logging;
using Sagaway.Telemetry;
namespace Sagaway
{
/// <summary>
/// The saga implementation class
/// </summary>
/// <typeparam name="TEOperations"></typeparam>
/// <remarks>Use <see cref="SagaBuilder"></see> to build a saga instance/> </remarks>
public partial class Saga<TEOperations> : ISaga<TEOperations> where TEOperations : Enum
{
private ILogger _logger;
private string _sagaUniqueId;
private readonly ISagaSupport _sagaSupportOperations;
private readonly List<SagaOperationExecution> _operations;
private readonly Action<string>? _onSuccessCallback;
private readonly Action<string>? _onFailedCallback;
private readonly Action<string>? _onRevertedCallback;
private readonly Action<string>? _onRevertFailureCallback;
private bool _isReverting;
private readonly StringBuilder _stepRecorder = new();
private bool _deactivated;
private bool _hasFailedReported;
private readonly ILockWrapper _lock;
private readonly SagaTelemetryContext _telemetryContext;
private string SagaStateName => $"Saga_{_sagaUniqueId}";
#region Telemetry
private ITelemetryAdapter TelemetryAdapter => _sagaSupportOperations.TelemetryAdapter;
private void RecordStartOperationTelemetry(TEOperations sagaOperation, bool isReverting)
{
try
{
TelemetryAdapter.StartOperationAsync(_telemetryContext, (isReverting ? "Revert" : "") + sagaOperation);
}
catch (Exception e)
{
_logger.LogError(e, "Error recording start operation telemetry");
}
}
private void RecordEndOperationTelemetry(TEOperations sagaOperation, OperationOutcome operationOutcome, bool isReverting)
{
try
{
TelemetryAdapter.EndOperationAsync(_telemetryContext, (isReverting ? "Revert" : "") + sagaOperation, operationOutcome);
}
catch (Exception e)
{
_logger.LogError(e, "Error recording end operation telemetry");
}
}
private void RecordRetryAttemptTelemetry(TEOperations sagaOperationOperation, int retryCount, bool isReverting)
{
try
{
TelemetryAdapter.RecordRetryAttemptAsync(_telemetryContext, (isReverting ? "Revert" : "") + sagaOperationOperation, retryCount);
}
catch (Exception e)
{
_logger.LogError(e, "Error recording retry attempt telemetry");
}
}
/// <summary>
/// Record custom telemetry event that is part of the Saga execution and can be traced using
/// services such as OpenTelemetry
/// </summary>
/// <param name="eventName">The custom event name</param>
/// <param name="properties">The custom event parameters</param>
protected void RecordCustomTelemetryEvent(string eventName, IDictionary<string, object> properties)
{
try
{
TelemetryAdapter.RecordCustomEventAsync(_telemetryContext, eventName, properties);
}
catch (Exception e)
{
_logger.LogError(e, "Error recording custom telemetry event");
}
}
/// <summary>
/// Records any exceptions or failures.
/// </summary>
/// <param name="exception">The exception that occurred.</param>
/// <param name="context">An optional context or description where the exception occurred.</param>
// ReSharper disable once MemberCanBePrivate.Global
protected void RecordException(Exception exception, string? context = null)
{
try
{
TelemetryAdapter.RecordExceptionAsync(_telemetryContext, exception, context);
}
catch (Exception e)
{
_logger.LogError(e, "Error recording exception");
}
}
#endregion //telemetry
/// <summary>
/// The saga is in progress
/// </summary>
public bool InProgress { get; private set; } = true;
/// <summary>
/// All operations have been executed successfully and the saga is completed
/// </summary>
public bool Succeeded { get; private set; }
/// <summary>
/// The saga has failed and is in the process of reverting
/// </summary>
public bool Failed { get; private set; }
/// <summary>
/// The saga has failed and has reverted all operations
/// </summary>
public bool Reverted { get; private set; }
/// <summary>
/// The saga has failed and has failed to revert all operations. It is considered done.
/// </summary>
public bool RevertFailed { get; private set; }
private Saga(ILogger logger, string sagaUniqueId, ISagaSupport sagaSupportOperations, IReadOnlyList<SagaOperation> operations, Action<string>? onSuccessCallback, Action<string>? onFailedCallback, Action<string>? onRevertedCallback, Action<string>? onRevertFailureCallback)
{
_logger = logger;
_sagaUniqueId = sagaUniqueId;
_sagaSupportOperations = sagaSupportOperations;
_operations = operations.Select(o => new SagaOperationExecution(this, o, logger)).ToList();
_onSuccessCallback = onSuccessCallback;
_onFailedCallback = onFailedCallback;
_onRevertedCallback = onRevertedCallback;
_onRevertFailureCallback = onRevertFailureCallback;
_lock = _sagaSupportOperations.CreateLock();
Validate();
SetExecutableOperationDependencies();
_telemetryContext = new SagaTelemetryContext(_sagaUniqueId, $"Saga{typeof(TEOperations).Name}",
logger, new TelemetryDataPersistence(this));
}
private void Validate()
{
//must provide logger
if (_logger == null)
{
throw new ValidationException(nameof(_logger));
}
//saga unique id must be provided
if (string.IsNullOrWhiteSpace(_sagaUniqueId))
{
throw new ValidationException("A unique saga id must be provided for the Saga");
}
//saga support must be provided
if (_sagaSupportOperations == null)
{
throw new ValidationException($"A saga support type the implement the interface {nameof(ISagaSupport)} must be provided");
}
//at least one operation must be provided
if (_operations == null || _operations.Count == 0)
{
throw new ValidationException("At least one operation must be provided");
}
//for all provided operations, a do operation must be provided
if (_operations.Any(o => o.Operation.DoOperationAsync == null))
{
throw new ValidationException("All operation must set the Do function");
}
if (_operations.Count == 0)
{
throw new ValidationException("No operations are defined");
}
//must have a success callback
if (_onSuccessCallback == null)
{
throw new ValidationException("No on success callback is defined");
}
//must have a revert callback
if (_onRevertedCallback == null && _onFailedCallback == null)
{
throw new ValidationException("No on reverted or on failed callback is defined");
}
//must have a revert failure callback
if (_onRevertFailureCallback == null && _onFailedCallback == null)
{
throw new ValidationException("No on revert failure or on failed callback is defined");
}
//for each operation that provided a validate function, a Retry number that is grater than 0 must be provided
if (_operations.Any(o => o.Operation is { ValidateAsync: { }, MaxRetries: <= 0 }))
{
throw new ValidationException("Retry count must be greater than 0 for all operations that provide a validate function");
}
//for each operation that provided a validate function, a positive Retry Interval or retry function must be provided
if (_operations.Any(o => o.Operation is { ValidateAsync: not null, RetryInterval.TotalSeconds: <= 0, RetryIntervalFunction: null }))
{
throw new ValidationException("Retry interval must be greater than 0 or a retry delay function is provided for all operations that provide a validate function");
}
}
//called by the ctor
private void SetExecutableOperationDependencies()
{
foreach (var operation in _operations)
{
var dependencies = _operations.Where(o => operation.Operation.Preconditions!.HasFlag(o.Operation.Operation)).ToList();
operation.SetDependencies(dependencies);
}
}
/// <summary>
/// Create a saga fluent interface builder
/// </summary>
/// <param name="uniqueId"></param>
/// <param name="sagaSupportOperations"></param>
/// <param name="logger"></param>
/// <returns>A saga builder</returns>
public static SagaBuilder Create(string uniqueId, ISagaSupport sagaSupportOperations, ILogger logger)
{
return new SagaBuilder(uniqueId, sagaSupportOperations, logger);
}
/// <summary>
/// Handles the activated event
/// </summary>
/// <returns>Async operation</returns>
public async Task InformActivatedAsync()
{
bool isNew = false;
await _lock.LockAsync(async () =>
{
_deactivated = false;
try
{
isNew = await LoadStateAsync();
}
catch (Exception ex)
{
_logger.LogError(ex, $"Error loading saga state {SagaStateName}");
throw;
}
if (!InProgress)
{
throw new InvalidOperationException("Saga is not in progress");
}
});
await TelemetryAdapter.StartSagaAsync(_telemetryContext, isNew);
}
/// <summary>
/// Handles the deactivated event
/// </summary>
/// <returns>Async operation</returns>
public async Task InformDeactivatedAsync()
{
await _lock.LockAsync(async () =>
{
if (!InProgress)
{
_logger.LogInformation($"Saga {_sagaUniqueId} is already completed, no need to deactivate.");
return;
}
try
{
_stepRecorder.AppendLine($"{SagaStateName} is deactivated.");
_deactivated = true;
await StoreStateAsync();
}
catch (Exception ex)
{
_logger.LogError(ex, $"Error saving saga state {SagaStateName}");
throw;
}
});
await TelemetryAdapter.ActivateLongOperationAsync(_telemetryContext);
}
//load the state of the saga, if there is no state, it is a new saga
private async Task<bool> LoadStateAsync()
{
var json = await _sagaSupportOperations.LoadSagaAsync(SagaStateName);
if (json is null)
{
_logger.LogInformation($"State {SagaStateName} is not found in persistence store, Assuming first run.");
return true;
}
var uniqueId = json["sagaUniqueId"]?.GetValue<string>();
_sagaUniqueId = uniqueId!;
_isReverting = json["isReverting"]?.GetValue<bool>() ?? false;
_hasFailedReported = json["hasFailedReported"]?.GetValue<bool>() ?? false;
_stepRecorder.Length = 0;
_stepRecorder.Append(json["stepRecorder"]?.GetValue<string>() ?? string.Empty);
_stepRecorder.AppendLine($"{SagaStateName} is activated.");
foreach (var operation in _operations)
{
operation.LoadState(json);
}
var telemetryStateStore = json["telemetryStateStore"]?.GetValue<string>() ?? string.Empty;
var telemetryStatePairs = telemetryStateStore.Split('|', StringSplitOptions.RemoveEmptyEntries);
foreach (var pair in telemetryStatePairs)
{
var keyValue = pair.Split(',', StringSplitOptions.RemoveEmptyEntries);
if (keyValue.Length == 2)
{
_telemetryStateStore[keyValue[0]] = keyValue[1];
}
}
CheckForCompletion();
return false; //new saga
}
private async Task StoreStateAsync()
{
var json = new JsonObject();
foreach (var operation in _operations)
{
operation.StoreState(json);
}
json["sagaUniqueId"] = _sagaUniqueId;
json["isReverting"] = _isReverting;
json["hasFailedReported"] = _hasFailedReported;
json["stepRecorder"] = _stepRecorder.ToString();
var telemetryStateStore = _telemetryStateStore.Aggregate(
new StringBuilder(), (sb, pair) => sb.Append($"{pair.Key},{pair.Value}|"), sb=>sb.ToString());
if (!string.IsNullOrWhiteSpace(telemetryStateStore))
json["telemetryStateStore"] = telemetryStateStore;
await _sagaSupportOperations.SaveSagaStateAsync(SagaStateName, json);
}
/// <summary>
/// Call when all saga operations are completed
/// </summary>
public event EventHandler<SagaCompletionEventArgs>? OnSagaCompleted;
/// <summary>
/// Execute the saga
/// </summary>
/// <returns>Async method</returns>
public async Task RunAsync()
{
await _lock.LockAsync(async () =>
{
while (InProgress && !_deactivated)
{
var allWaitingOperations = _operations.Where(o => o.CanExecute).ToList();
if (!allWaitingOperations.Any() || allWaitingOperations.All(o => o.Failed))
return;
foreach (var operation in allWaitingOperations)
{
await operation.StartExecuteAsync();
if (!InProgress || _deactivated)
break;
}
}
});
}
/// <summary>
/// Handles the outcome of an operation
/// </summary>
/// <param name="operation">The operation</param>
/// <param name="success">Success or failure</param>
/// <param name="failFast">If true, fail the Saga, stop retries and start revert</param>
/// <returns>Async operation</returns>
[Obsolete("Use ReportOperationOutcomeAsync with the FastOutcome enum instead")]
public async Task ReportOperationOutcomeAsync(TEOperations operation, bool success, bool failFast)
{
await ReportOperationOutcomeAsync(operation, success, failFast ? SagaFastOutcome.Failure : SagaFastOutcome.None);
}
/// <summary>
/// Implementer should call this method to inform the outcome of an operation
/// </summary>
/// <param name="operation">The operation</param>
/// <param name="success">Success or failure</param>
/// <param name="sagaFastOutcome">Inform a fast outcome for the Saga from a single operation, either fast fail or success
/// <remarks><see cref="SagaFastOutcome.Failure"/> fails the saga and start the compensation process</remarks>
/// <remarks><see cref="SagaFastOutcome.Success"/> Finish the saga successfully, marked all non-started operations as succeeded</remarks></param>
/// <returns>Async operation</returns>
public async Task ReportOperationOutcomeAsync(TEOperations operation, bool success, SagaFastOutcome sagaFastOutcome = SagaFastOutcome.None)
{
if (success && sagaFastOutcome == SagaFastOutcome.Failure)
throw new InvalidOperationException("Cannot have success and fail fast at the same time");
if (!success && sagaFastOutcome == SagaFastOutcome.Success)
throw new InvalidOperationException("Cannot have fast success without success");
await _lock.LockAsync(async () =>
{
if (!InProgress)
return;
try
{
if (sagaFastOutcome == SagaFastOutcome.Success)
{
var allNotStartedOperations = _operations.Where(o => o.NotStarted);
foreach (var op in allNotStartedOperations)
{
op.MarkSucceeded();
}
}
var operationExecution = _operations.Single(o => o.Operation.Operation.Equals(operation));
if (success)
{
await operationExecution.InformSuccessOperationAsync();
}
else
{
await operationExecution.InformFailureOperationAsync(sagaFastOutcome == SagaFastOutcome.Failure);
}
}
finally
{
await RunAsync();
}
});
}
/// <summary>
/// Handles the outcome of an undo operation
/// </summary>
/// <param name="operation">The operation</param>
/// <param name="success">Success or failure</param>
/// <returns>Async operation</returns>
public async Task ReportUndoOperationOutcomeAsync(TEOperations operation, bool success)
{
await _lock.LockAsync(async () =>
{
if (!InProgress)
return;
var operationExecution = _operations.Single(o => o.Operation.Operation.Equals(operation));
if (success)
{
await operationExecution.InformSuccessUndoOperationAsync();
}
else
{
await operationExecution.InformFailureUndoOperationAsync();
}
});
}
private void CheckForCompletion()
{
if (!InProgress)
return;
Succeeded = _operations.All(o => o.Succeeded);
Failed = _operations.Any(o => o.Failed);
Reverted = _operations.All(o => o.Reverted);
RevertFailed = _operations.Any(o => o.RevertFailed) && _operations.All(o => o.RevertFailed || o.Reverted);
InProgress = !Succeeded && !Reverted && !RevertFailed;
var recordedSteps = _stepRecorder.ToString();
try
{
if (Succeeded && _onSuccessCallback != null)
{
_onSuccessCallback(recordedSteps);
}
if (Failed && _onFailedCallback != null && !_hasFailedReported)
{
_hasFailedReported = true;
_onFailedCallback(recordedSteps);
TelemetryAdapter.RecordCustomEventAsync(_telemetryContext, "SagaFailure");
}
if (Reverted && _onRevertedCallback != null)
{
_onRevertedCallback(recordedSteps);
}
if (RevertFailed && _onRevertFailureCallback != null)
{
_onRevertFailureCallback(recordedSteps);
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Error calling report result callback");
}
if (!InProgress) //saga competed
{
_logger.LogInformation($"Saga {_sagaUniqueId} completed with status: " + (Succeeded ? "Success" : Reverted ? "Reverted" : "RevertFailed"));
SagaCompletionStatus sagaCompletionStatus = Succeeded ? SagaCompletionStatus.Succeeded : Reverted ? SagaCompletionStatus.Reverted : SagaCompletionStatus.RevertFailed;
try
{
OnSagaCompleted?.Invoke(this, new SagaCompletionEventArgs(_sagaUniqueId, sagaCompletionStatus, recordedSteps));
}
catch (Exception e)
{
_logger.LogError(e, "Error calling OnSagaCompleted event");
}
var telemetryOutcome = Succeeded ? SagaOutcome.Succeeded : Reverted ? SagaOutcome.Reverted : SagaOutcome.PartiallyReverted;
TelemetryAdapter.EndSagaAsync(_telemetryContext, telemetryOutcome);
}
else
{
_logger.LogInformation($"Saga {_sagaUniqueId} is still in progress.");
}
}
private void RecordStep(TEOperations operation, string step)
{
AppendRecordStep($"[{operation}]: {step}");
}
private void RecordMessage(string message)
{
AppendRecordStep($"{message}[{_sagaUniqueId}]");
}
private void AppendRecordStep(string message)
{
var time = DateTimeOffset.UtcNow.ToString("HH:mm:ss", CultureInfo.InvariantCulture);
_stepRecorder.AppendLine($"[{time}]{message}");
}
private async Task CompensateAsync()
{
if (_isReverting)
{
_logger.LogInformation($"Saga {_sagaUniqueId} is already in reverting state.");
return;
}
_isReverting = true;
foreach (var operation in _operations)
{
if (operation.NotStarted)
{
var message = $"Saga {_sagaUniqueId} is not reverting not started operation {operation.Operation.Operation}.";
RecordStep(operation.Operation.Operation, message);
_logger.LogInformation(message);
operation.MarkReverted();
continue;
}
await operation.RevertAsync();
}
CheckForCompletion();
}
/// <summary>
/// Handles reminder operations
/// </summary>
/// <param name="reminder"></param>
/// <returns>Async operation</returns>
public async Task ReportReminderAsync(string reminder)
{
await _lock.LockAsync(async () =>
{
try
{
if (_deactivated)
{
await InformActivatedAsync();
}
var operationName = reminder.Split(':')[0];
var operation = _operations.Single(o => o.Operation.Operation.ToString().Equals(operationName));
await operation.OnReminderAsync();
}
catch (Exception ex)
{
_logger.LogError(ex, $"Error handling reminder {reminder}, canceling reminder.");
await _sagaSupportOperations.CancelReminderAsync(reminder);
}
});
}
}
}