-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathExecutionStrategy.cs
501 lines (442 loc) · 20.3 KB
/
ExecutionStrategy.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Transactions;
namespace Microsoft.EntityFrameworkCore.Storage;
/// <summary>
/// The base class for <see cref="IExecutionStrategy" /> implementations.
/// </summary>
/// <remarks>
/// <para>
/// The service lifetime is <see cref="ServiceLifetime.Scoped" />. This means that each
/// <see cref="DbContext" /> instance will use its own instance of this service.
/// The implementation may depend on other services registered with any lifetime.
/// The implementation does not need to be thread-safe.
/// </para>
/// <para>
/// See <see href="https://aka.ms/efcore-docs-connection-resiliency">Connection resiliency and database retries</see>
/// for more information and examples.
/// </para>
/// </remarks>
public abstract class ExecutionStrategy : IExecutionStrategy
{
/// <summary>
/// The default number of retry attempts.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-connection-resiliency">Connection resiliency and database retries</see>
/// for more information and examples.
/// </remarks>
protected static readonly int DefaultMaxRetryCount = 6;
/// <summary>
/// The default maximum time delay between retries, must be nonnegative.
/// </summary>
protected static readonly TimeSpan DefaultMaxDelay = TimeSpan.FromSeconds(30);
/// <summary>
/// The default maximum random factor, must not be lesser than 1.
/// </summary>
private const double DefaultRandomFactor = 1.1;
/// <summary>
/// The default base for the exponential function used to compute the delay between retries, must be positive.
/// </summary>
private const double DefaultExponentialBase = 2;
/// <summary>
/// The default coefficient for the exponential function used to compute the delay between retries, must be nonnegative.
/// </summary>
private static readonly TimeSpan DefaultCoefficient = TimeSpan.FromSeconds(1);
/// <summary>
/// Creates a new instance of <see cref="ExecutionStrategy" />.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-connection-resiliency">Connection resiliency and database retries</see>
/// for more information and examples.
/// </remarks>
/// <param name="context">The context on which the operations will be invoked.</param>
/// <param name="maxRetryCount">The maximum number of retry attempts.</param>
/// <param name="maxRetryDelay">The maximum delay between retries.</param>
protected ExecutionStrategy(
DbContext context,
int maxRetryCount,
TimeSpan maxRetryDelay)
: this(
context.GetService<ExecutionStrategyDependencies>(),
maxRetryCount,
maxRetryDelay)
{
}
/// <summary>
/// Creates a new instance of <see cref="ExecutionStrategy" />.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-connection-resiliency">Connection resiliency and database retries</see>
/// for more information and examples.
/// </remarks>
/// <param name="dependencies">Parameter object containing service dependencies.</param>
/// <param name="maxRetryCount">The maximum number of retry attempts.</param>
/// <param name="maxRetryDelay">The maximum delay between retries.</param>
protected ExecutionStrategy(
ExecutionStrategyDependencies dependencies,
int maxRetryCount,
TimeSpan maxRetryDelay)
{
ArgumentOutOfRangeException.ThrowIfNegative(maxRetryCount);
if (maxRetryDelay.TotalMilliseconds < 0.0)
{
throw new ArgumentOutOfRangeException(nameof(maxRetryDelay));
}
Dependencies = dependencies;
MaxRetryCount = maxRetryCount;
MaxRetryDelay = maxRetryDelay;
}
/// <summary>
/// The list of exceptions that caused the operation to be retried so far.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-connection-resiliency">Connection resiliency and database retries</see>
/// for more information and examples.
/// </remarks>
protected virtual List<Exception> ExceptionsEncountered { get; } = [];
/// <summary>
/// A pseudo-random number generator that can be used to vary the delay between retries.
/// </summary>
protected virtual Random Random { get; } = new();
/// <summary>
/// The maximum number of retry attempts.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-connection-resiliency">Connection resiliency and database retries</see>
/// for more information and examples.
/// </remarks>
public virtual int MaxRetryCount { get; }
/// <summary>
/// The maximum delay between retries.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-connection-resiliency">Connection resiliency and database retries</see>
/// for more information and examples.
/// </remarks>
public virtual TimeSpan MaxRetryDelay { get; }
/// <summary>
/// Dependencies for this service.
/// </summary>
protected virtual ExecutionStrategyDependencies Dependencies { get; }
private static readonly AsyncLocal<ExecutionStrategy?> CurrentExecutionStrategy = new();
/// <summary>
/// Gets or sets the currently executing strategy. All nested calls will be handled by the outermost strategy.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-connection-resiliency">Connection resiliency and database retries</see>
/// for more information and examples.
/// </remarks>
public static ExecutionStrategy? Current
{
get => CurrentExecutionStrategy.Value;
protected set => CurrentExecutionStrategy.Value = value;
}
/// <summary>
/// Indicates whether this <see cref="IExecutionStrategy" /> might retry the execution after a failure.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-connection-resiliency">Connection resiliency and database retries</see>
/// for more information and examples.
/// </remarks>
public virtual bool RetriesOnFailure
{
get
{
var current = Current;
return (current == null || current == this) && MaxRetryCount > 0;
}
}
/// <summary>
/// Executes the specified operation and returns the result.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-connection-resiliency">Connection resiliency and database retries</see>
/// for more information and examples.
/// </remarks>
/// <param name="state">The state that will be passed to the operation.</param>
/// <param name="operation">
/// A delegate representing an executable operation that returns the result of type <typeparamref name="TResult" />.
/// </param>
/// <param name="verifySucceeded">A delegate that tests whether the operation succeeded even though an exception was thrown.</param>
/// <typeparam name="TState">The type of the state.</typeparam>
/// <typeparam name="TResult">The return type of <paramref name="operation" />.</typeparam>
/// <returns>The result from the operation.</returns>
/// <exception cref="RetryLimitExceededException">
/// The operation has not succeeded after the configured number of retries.
/// </exception>
public virtual TResult Execute<TState, TResult>(
TState state,
Func<DbContext, TState, TResult> operation,
Func<DbContext, TState, ExecutionResult<TResult>>? verifySucceeded)
{
Check.NotNull(operation, nameof(operation));
if (Current != null)
{
return operation(Dependencies.CurrentContext.Context, state);
}
OnFirstExecution();
// In order to avoid infinite recursive generics, wrap operation with ExecutionResult
return ExecuteImplementation(
(context, state) => new ExecutionResult<TResult>(true, operation(context, state)),
verifySucceeded,
state).Result;
}
private ExecutionResult<TResult> ExecuteImplementation<TState, TResult>(
Func<DbContext, TState, ExecutionResult<TResult>> operation,
Func<DbContext, TState, ExecutionResult<TResult>>? verifySucceeded,
TState state)
{
while (true)
{
try
{
Check.DebugAssert(Current == null, "Current != null");
Current = this;
var result = operation(Dependencies.CurrentContext.Context, state);
Current = null;
return result;
}
catch (Exception ex)
{
Current = null;
EntityFrameworkMetricsData.ReportExecutionStrategyOperationFailure();
if (verifySucceeded != null
&& CallOnWrappedException(ex, ShouldVerifySuccessOn))
{
var result = ExecuteImplementation(verifySucceeded, null, state);
if (result.IsSuccessful)
{
return result;
}
}
if (!CallOnWrappedException(ex, ShouldRetryOn))
{
throw;
}
ExceptionsEncountered.Add(ex);
var delay = GetNextDelay(ex);
if (delay == null)
{
throw new RetryLimitExceededException(CoreStrings.RetryLimitExceeded(MaxRetryCount, GetType().Name), ex);
}
Dependencies.Logger.ExecutionStrategyRetrying(ExceptionsEncountered, delay.Value, async: true);
OnRetry();
Thread.Sleep(delay.Value);
}
}
}
/// <summary>
/// Executes the specified asynchronous operation and returns the result.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-connection-resiliency">Connection resiliency and database retries</see>
/// for more information and examples.
/// </remarks>
/// <param name="state">The state that will be passed to the operation.</param>
/// <param name="operation">
/// A function that returns a started task of type <typeparamref name="TResult" />.
/// </param>
/// <param name="verifySucceeded">A delegate that tests whether the operation succeeded even though an exception was thrown.</param>
/// <param name="cancellationToken">
/// A cancellation token used to cancel the retry operation, but not operations that are already in flight
/// or that already completed successfully.
/// </param>
/// <typeparam name="TState">The type of the state.</typeparam>
/// <typeparam name="TResult">The result type of the <see cref="Task{T}" /> returned by <paramref name="operation" />.</typeparam>
/// <returns>
/// A task that will run to completion if the original task completes successfully (either the
/// first time or after retrying transient failures). If the task fails with a non-transient error or
/// the retry limit is reached, the returned task will become faulted and the exception must be observed.
/// </returns>
/// <exception cref="RetryLimitExceededException">
/// The operation has not succeeded after the configured number of retries.
/// </exception>
/// <exception cref="OperationCanceledException">If the <see cref="CancellationToken" /> is canceled.</exception>
public virtual async Task<TResult> ExecuteAsync<TState, TResult>(
TState state,
Func<DbContext, TState, CancellationToken, Task<TResult>> operation,
Func<DbContext, TState, CancellationToken, Task<ExecutionResult<TResult>>>? verifySucceeded,
CancellationToken cancellationToken = default)
{
Check.NotNull(operation, nameof(operation));
if (Current != null)
{
return await operation(Dependencies.CurrentContext.Context, state, cancellationToken).ConfigureAwait(false);
}
OnFirstExecution();
// In order to avoid infinite recursive generics, wrap operation with ExecutionResult
var result = await ExecuteImplementationAsync(
async (context, state, cancellationToken) => new ExecutionResult<TResult>(
true, await operation(context, state, cancellationToken).ConfigureAwait(false)),
verifySucceeded,
state,
cancellationToken).ConfigureAwait(false);
return result.Result;
}
private async Task<ExecutionResult<TResult>> ExecuteImplementationAsync<TState, TResult>(
Func<DbContext, TState, CancellationToken, Task<ExecutionResult<TResult>>> operation,
Func<DbContext, TState, CancellationToken, Task<ExecutionResult<TResult>>>? verifySucceeded,
TState state,
CancellationToken cancellationToken)
{
while (true)
{
cancellationToken.ThrowIfCancellationRequested();
try
{
Check.DebugAssert(Current == null, "Current != null");
Current = this;
var result = await operation(Dependencies.CurrentContext.Context, state, cancellationToken)
.ConfigureAwait(true);
Current = null;
return result;
}
catch (Exception ex)
{
Current = null;
EntityFrameworkMetricsData.ReportExecutionStrategyOperationFailure();
if (verifySucceeded != null
&& CallOnWrappedException(ex, ShouldVerifySuccessOn))
{
var result = await ExecuteImplementationAsync(verifySucceeded, null, state, cancellationToken)
.ConfigureAwait(true);
if (result.IsSuccessful)
{
return result;
}
}
if (!CallOnWrappedException(ex, ShouldRetryOn))
{
throw;
}
ExceptionsEncountered.Add(ex);
var delay = GetNextDelay(ex);
if (delay == null)
{
throw new RetryLimitExceededException(CoreStrings.RetryLimitExceeded(MaxRetryCount, GetType().Name), ex);
}
Dependencies.Logger.ExecutionStrategyRetrying(ExceptionsEncountered, delay.Value, async: true);
OnRetry();
await Task.Delay(delay.Value, cancellationToken).ConfigureAwait(true);
}
}
}
/// <summary>
/// Method called before the first operation execution
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-connection-resiliency">Connection resiliency and database retries</see>
/// for more information and examples.
/// </remarks>
protected virtual void OnFirstExecution()
{
if (RetriesOnFailure
&& (Dependencies.CurrentContext.Context.Database.CurrentTransaction is not null
|| Dependencies.CurrentContext.Context.Database.GetEnlistedTransaction() is not null
|| (((IDatabaseFacadeDependenciesAccessor)Dependencies.CurrentContext.Context.Database).Dependencies
.TransactionManager as
ITransactionEnlistmentManager)?.CurrentAmbientTransaction is not null))
{
throw new InvalidOperationException(
CoreStrings.ExecutionStrategyExistingTransaction(
GetType().Name,
nameof(DbContext)
+ "."
+ nameof(DbContext.Database)
+ "."
+ nameof(DatabaseFacade.CreateExecutionStrategy)
+ "()"));
}
ExceptionsEncountered.Clear();
}
/// <summary>
/// Method called before retrying the operation execution
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-connection-resiliency">Connection resiliency and database retries</see>
/// for more information and examples.
/// </remarks>
protected virtual void OnRetry()
{
}
/// <summary>
/// Determines whether the operation should be retried and the delay before the next attempt.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-connection-resiliency">Connection resiliency and database retries</see>
/// for more information and examples.
/// </remarks>
/// <param name="lastException">The exception thrown during the last execution attempt.</param>
/// <returns>
/// Returns the delay indicating how long to wait for before the next execution attempt if the operation should be retried;
/// <see langword="null" /> otherwise
/// </returns>
protected virtual TimeSpan? GetNextDelay(Exception lastException)
{
var currentRetryCount = ExceptionsEncountered.Count - 1;
if (currentRetryCount < MaxRetryCount)
{
var delta = (Math.Pow(DefaultExponentialBase, currentRetryCount) - 1.0)
* (1.0 + Random.NextDouble() * (DefaultRandomFactor - 1.0));
var delay = Math.Min(
DefaultCoefficient.TotalMilliseconds * delta,
MaxRetryDelay.TotalMilliseconds);
return TimeSpan.FromMilliseconds(delay);
}
return null;
}
/// <summary>
/// Determines whether the specified exception could be thrown after a successful execution.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-connection-resiliency">Connection resiliency and database retries</see>
/// for more information and examples.
/// </remarks>
/// <param name="exception">The exception object to be verified.</param>
/// <returns>
/// <see langword="true" /> if the specified exception could be thrown after a successful execution, otherwise <see langword="false" />.
/// </returns>
protected internal virtual bool ShouldVerifySuccessOn(Exception exception)
=> ShouldRetryOn(exception);
/// <summary>
/// Determines whether the specified exception represents a transient failure that can be compensated by a retry.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-connection-resiliency">Connection resiliency and database retries</see>
/// for more information and examples.
/// </remarks>
/// <param name="exception">The exception object to be verified.</param>
/// <returns>
/// <see langword="true" /> if the specified exception is considered as transient, otherwise <see langword="false" />.
/// </returns>
protected internal abstract bool ShouldRetryOn(Exception exception);
/// <summary>
/// Recursively gets InnerException from <paramref name="exception" /> as long as it is an
/// exception created by Entity Framework and calls <paramref name="exceptionHandler" /> on the innermost one.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-connection-resiliency">Connection resiliency and database retries</see>
/// for more information and examples.
/// </remarks>
/// <param name="exception">The exception to be unwrapped.</param>
/// <param name="exceptionHandler">A delegate that will be called with the unwrapped exception.</param>
/// <typeparam name="TResult">The return type of <paramref name="exceptionHandler" />.</typeparam>
/// <returns>
/// The result from <paramref name="exceptionHandler" />.
/// </returns>
public static TResult CallOnWrappedException<TResult>(
Exception exception,
Func<Exception, TResult> exceptionHandler)
{
while (true)
{
if (exception is DbUpdateException { InnerException: Exception innerException })
{
exception = innerException;
continue;
}
return exceptionHandler(exception);
}
}
}