-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
SqlServerRetryingExecutionStrategy.cs
169 lines (155 loc) · 7.17 KB
/
SqlServerRetryingExecutionStrategy.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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using JetBrains.Annotations;
using Microsoft.Data.SqlClient; // Note: Hard reference to SqlClient here.
using Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal;
using Microsoft.EntityFrameworkCore.Storage;
// ReSharper disable once CheckNamespace
namespace Microsoft.EntityFrameworkCore
{
/// <summary>
/// An <see cref="IExecutionStrategy" /> implementation for retrying failed executions
/// on SQL Server.
/// </summary>
public class SqlServerRetryingExecutionStrategy : ExecutionStrategy
{
private readonly ICollection<int> _additionalErrorNumbers;
/// <summary>
/// Creates a new instance of <see cref="SqlServerRetryingExecutionStrategy" />.
/// </summary>
/// <param name="context"> The context on which the operations will be invoked. </param>
/// <remarks>
/// The default retry limit is 6, which means that the total amount of time spent before failing is about a minute.
/// </remarks>
public SqlServerRetryingExecutionStrategy(
[NotNull] DbContext context)
: this(context, DefaultMaxRetryCount)
{
}
/// <summary>
/// Creates a new instance of <see cref="SqlServerRetryingExecutionStrategy" />.
/// </summary>
/// <param name="dependencies"> Parameter object containing service dependencies. </param>
public SqlServerRetryingExecutionStrategy(
[NotNull] ExecutionStrategyDependencies dependencies)
: this(dependencies, DefaultMaxRetryCount)
{
}
/// <summary>
/// Creates a new instance of <see cref="SqlServerRetryingExecutionStrategy" />.
/// </summary>
/// <param name="context"> The context on which the operations will be invoked. </param>
/// <param name="maxRetryCount"> The maximum number of retry attempts. </param>
public SqlServerRetryingExecutionStrategy(
[NotNull] DbContext context,
int maxRetryCount)
: this(context, maxRetryCount, DefaultMaxDelay, errorNumbersToAdd: null)
{
}
/// <summary>
/// Creates a new instance of <see cref="SqlServerRetryingExecutionStrategy" />.
/// </summary>
/// <param name="dependencies"> Parameter object containing service dependencies. </param>
/// <param name="maxRetryCount"> The maximum number of retry attempts. </param>
public SqlServerRetryingExecutionStrategy(
[NotNull] ExecutionStrategyDependencies dependencies,
int maxRetryCount)
: this(dependencies, maxRetryCount, DefaultMaxDelay, errorNumbersToAdd: null)
{
}
/// <summary>
/// Creates a new instance of <see cref="SqlServerRetryingExecutionStrategy" />.
/// </summary>
/// <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>
/// <param name="errorNumbersToAdd"> Additional SQL error numbers that should be considered transient. </param>
public SqlServerRetryingExecutionStrategy(
[NotNull] DbContext context,
int maxRetryCount,
TimeSpan maxRetryDelay,
[CanBeNull] ICollection<int> errorNumbersToAdd)
: base(
context,
maxRetryCount,
maxRetryDelay)
=> _additionalErrorNumbers = errorNumbersToAdd;
/// <summary>
/// Creates a new instance of <see cref="SqlServerRetryingExecutionStrategy" />.
/// </summary>
/// <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>
/// <param name="errorNumbersToAdd"> Additional SQL error numbers that should be considered transient. </param>
public SqlServerRetryingExecutionStrategy(
[NotNull] ExecutionStrategyDependencies dependencies,
int maxRetryCount,
TimeSpan maxRetryDelay,
[CanBeNull] ICollection<int> errorNumbersToAdd)
: base(dependencies, maxRetryCount, maxRetryDelay)
=> _additionalErrorNumbers = errorNumbersToAdd;
/// <summary>
/// Determines whether the specified exception represents a transient failure that can be
/// compensated by a retry. Additional exceptions to retry on can be passed to the constructor.
/// </summary>
/// <param name="exception"> The exception object to be verified. </param>
/// <returns>
/// <c>true</c> if the specified exception is considered as transient, otherwise <c>false</c>.
/// </returns>
protected override bool ShouldRetryOn(Exception exception)
{
if (_additionalErrorNumbers != null
&& exception is SqlException sqlException)
{
foreach (SqlError err in sqlException.Errors)
{
if (_additionalErrorNumbers.Contains(err.Number))
{
return true;
}
}
}
return SqlServerTransientExceptionDetector.ShouldRetryOn(exception);
}
/// <summary>
/// Determines whether the operation should be retried and the delay before the next attempt.
/// </summary>
/// <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;
/// <c>null</c> otherwise
/// </returns>
protected override TimeSpan? GetNextDelay(Exception lastException)
{
var baseDelay = base.GetNextDelay(lastException);
if (baseDelay == null)
{
return null;
}
return CallOnWrappedException(lastException, IsMemoryOptimizedError)
? TimeSpan.FromMilliseconds(baseDelay.Value.TotalSeconds)
: baseDelay;
}
private static bool IsMemoryOptimizedError(Exception exception)
{
if (exception is SqlException sqlException)
{
foreach (SqlError err in sqlException.Errors)
{
switch (err.Number)
{
case 41301:
case 41302:
case 41305:
case 41325:
case 41839:
return true;
}
}
}
return false;
}
}
}