-
Notifications
You must be signed in to change notification settings - Fork 303
/
Copy pathRetryLogicConfigHelper.cs
219 lines (185 loc) · 11.9 KB
/
RetryLogicConfigHelper.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Reflection;
using Xunit;
namespace Microsoft.Data.SqlClient.ManualTesting.Tests
{
public class RetryLogicConfigHelper
{
private const string ConfigurationLoaderTypeName = "Microsoft.Data.SqlClient.SqlConfigurableRetryLogicLoader";
private const string AppContextSwitchManagerTypeName = "Microsoft.Data.SqlClient.SqlAppContextSwitchManager";
private const string ApplyContextSwitchesMethodName = "ApplyContextSwitches";
private const string InterfaceCnnCfgTypeName = "Microsoft.Data.SqlClient.ISqlConfigurableRetryConnectionSection";
private const string InterfaceCmdCfgTypeName = "Microsoft.Data.SqlClient.ISqlConfigurableRetryCommandSection";
private const string CnnCfgTypeName = "Microsoft.Data.SqlClient.SqlConfigurableRetryConnectionSection";
private const string CmdCfgTypeName = "Microsoft.Data.SqlClient.SqlConfigurableRetryCommandSection";
private const string AppCtxCfgTypeName = "Microsoft.Data.SqlClient.AppContextSwitchOverridesSection";
public const string RetryMethodName_Fix = "CreateFixedRetryProvider";
public const string RetryMethodName_Inc = "CreateIncrementalRetryProvider";
public const string RetryMethodName_Exp = "CreateExponentialRetryProvider";
public const string RetryMethodName_None = "CreateNoneRetryProvider";
private const string SqlRetryLogicTypeName = "Microsoft.Data.SqlClient.SqlRetryLogic";
private const string CreateExceptionMethodName = "CreateException";
private const string AddMethodName = "Add";
public const string DefaultTransientErrors = "1204, 1205, 1222, 49918, 49919, 49920, 4060, 4221, 40143, 40613, 40501, 40540, 40197, 42108, 42109, 10929, 10928, 10060, 10054, 10053, 997, 233, 64, 20, 0, -2, 207, 102, 2812";
private static readonly Random s_random = new Random();
private static readonly Assembly s_sqlClientAssembly = typeof(SqlConnection).Assembly;
private static readonly Type s_appContextSwitchManagerType = s_sqlClientAssembly.GetType(AppContextSwitchManagerTypeName);
private static readonly Type s_sqlretrylogicType = s_sqlClientAssembly.GetType(SqlRetryLogicTypeName);
private static readonly Type s_configurationLoaderType = s_sqlClientAssembly.GetType(ConfigurationLoaderTypeName);
private static readonly Type s_sqlErrorType = typeof(SqlError);
private static readonly Type s_sqlErrorCollectionType = typeof(SqlErrorCollection);
private static readonly Type[] s_cfgLoaderParamsType = new Type[]
{
s_sqlClientAssembly.GetType(InterfaceCnnCfgTypeName),
s_sqlClientAssembly.GetType(InterfaceCmdCfgTypeName),
typeof(string), typeof(string)
};
private static readonly Type[] s_sqlErrorParamsType = new Type[]
{
typeof(int), typeof(byte), typeof(byte),
typeof(string), typeof(string), typeof(string),
typeof(int), typeof(Exception)
};
private static readonly ConstructorInfo s_loaderCtorInfo = s_configurationLoaderType.GetConstructor(s_cfgLoaderParamsType);
private static readonly ConstructorInfo s_sqlErrorCtorInfo = s_sqlErrorType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, s_sqlErrorParamsType, null);
private static readonly ConstructorInfo s_sqlErrorCollectionCtorInfo = s_sqlErrorCollectionType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, Type.EmptyTypes, null);
public static object CreateLoader(RetryLogicConfigs cnnConfig, RetryLogicConfigs cmdConfig)
{
var cnnCfgType = s_sqlClientAssembly.GetType(CnnCfgTypeName);
var cnnCfgObj = Activator.CreateInstance(cnnCfgType);
SetValue(cnnCfgObj, cnnCfgType, "DeltaTime", cnnConfig.DeltaTime);
SetValue(cnnCfgObj, cnnCfgType, "MinTimeInterval", cnnConfig.MinTimeInterval);
SetValue(cnnCfgObj, cnnCfgType, "MaxTimeInterval", cnnConfig.MaxTimeInterval);
SetValue(cnnCfgObj, cnnCfgType, "NumberOfTries", cnnConfig.NumberOfTries);
SetValue(cnnCfgObj, cnnCfgType, "AuthorizedSqlCondition", cnnConfig.AuthorizedSqlCondition);
SetValue(cnnCfgObj, cnnCfgType, "TransientErrors", cnnConfig.TransientErrors);
SetValue(cnnCfgObj, cnnCfgType, "RetryLogicType", cnnConfig.RetryLogicType);
SetValue(cnnCfgObj, cnnCfgType, "RetryMethod", cnnConfig.RetryMethodName);
var cmdCfgType = s_sqlClientAssembly.GetType(CmdCfgTypeName);
var cmdCfgObj = Activator.CreateInstance(cmdCfgType);
SetValue(cmdCfgObj, cmdCfgType, "DeltaTime", cmdConfig.DeltaTime);
SetValue(cmdCfgObj, cmdCfgType, "MinTimeInterval", cmdConfig.MinTimeInterval);
SetValue(cmdCfgObj, cmdCfgType, "MaxTimeInterval", cmdConfig.MaxTimeInterval);
SetValue(cmdCfgObj, cmdCfgType, "NumberOfTries", cmdConfig.NumberOfTries);
SetValue(cmdCfgObj, cmdCfgType, "AuthorizedSqlCondition", cmdConfig.AuthorizedSqlCondition);
SetValue(cmdCfgObj, cmdCfgType, "TransientErrors", cmdConfig.TransientErrors);
SetValue(cmdCfgObj, cmdCfgType, "RetryLogicType", cmdConfig.RetryLogicType);
SetValue(cmdCfgObj, cmdCfgType, "RetryMethod", cmdConfig.RetryMethodName);
return s_loaderCtorInfo.Invoke(new object[] { cnnCfgObj, cmdCfgObj, default, default });
}
public static void ApplyContextSwitchByManager(string name, bool value)
{
#if NETCOREAPP
var appCtxType = s_sqlClientAssembly.GetType(AppCtxCfgTypeName);
var appCtxObj = Activator.CreateInstance(appCtxType);
SetValue(appCtxObj, appCtxType, "Value", string.Concat(name, "=", value));
var method = s_appContextSwitchManagerType.GetMethod(ApplyContextSwitchesMethodName, BindingFlags.Static | BindingFlags.NonPublic);
method.Invoke(null, new object[] { appCtxObj });
#else
AppContext.SetSwitch(name, value);
#endif
}
public static SqlRetryLogicBaseProvider GetConnectionProvider(object loader)
=> GetValue<SqlRetryLogicBaseProvider>(loader, s_configurationLoaderType, "ConnectionProvider");
public static SqlRetryLogicBaseProvider GetCommandProvider(object loader)
=> GetValue<SqlRetryLogicBaseProvider>(loader, s_configurationLoaderType, "CommandProvider");
public static void AssessProvider(SqlRetryLogicBaseProvider provider, RetryLogicConfigs option)
=> AssessRetryLogic(provider.RetryLogic, option);
public static void AssessRetryLogic(SqlRetryLogicBase retryLogic, RetryLogicConfigs option)
{
Assert.Equal(option.DeltaTime, retryLogic.RetryIntervalEnumerator.GapTimeInterval);
Assert.Equal(option.MinTimeInterval, retryLogic.RetryIntervalEnumerator.MinTimeInterval);
Assert.Equal(option.MaxTimeInterval, retryLogic.RetryIntervalEnumerator.MaxTimeInterval);
Assert.Equal(option.NumberOfTries, retryLogic.NumberOfTries);
var preCondition = GetValue<Predicate<string>>(retryLogic, s_sqlretrylogicType, "PreCondition");
if (string.IsNullOrEmpty(option.AuthorizedSqlCondition))
{
Assert.Null(preCondition);
}
else
{
Assert.NotNull(preCondition);
}
}
public static T GetValue<T>(object obj, Type type, string propName, BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
=> (T)type.GetProperty(propName, flags)?.GetValue(obj);
public static void SetValue<T>(object obj, Type type, string propName, T value, BindingFlags flags = BindingFlags.Public | BindingFlags.Instance)
=> type.GetProperty(propName, flags)?.SetValue(obj, value);
public static IEnumerable<object[]> GetInvalidInternalMethodNames()
{
yield return new object[] { RetryMethodName_Fix.ToUpper() };
yield return new object[] { RetryMethodName_Inc.ToUpper() };
yield return new object[] { RetryMethodName_Exp.ToUpper() };
yield return new object[] { RetryMethodName_Fix.ToLower() };
yield return new object[] { RetryMethodName_Inc.ToLower() };
yield return new object[] { RetryMethodName_Exp.ToLower() };
}
public static IEnumerable<object[]> GetIivalidTimes()
{
var start = TimeSpan.FromSeconds(121);
var end = TimeSpan.FromHours(24);
for (int i = 0; i < 10; i++)
{
yield return new object[] { GenerateTimeSpan(start, end), GenerateTimeSpan(start, end), GenerateTimeSpan(start, end) };
}
}
public static object ReturnLoaderAndProviders(RetryLogicConfigs cnnCfg, RetryLogicConfigs cmdCfg, out SqlRetryLogicBaseProvider cnnProvider, out SqlRetryLogicBaseProvider cmdProvider)
{
var loaderObj = CreateLoader(cnnCfg, cmdCfg);
cnnProvider = GetConnectionProvider(loaderObj);
cmdProvider = GetCommandProvider(loaderObj);
return loaderObj;
}
public static RetryLogicConfigs CreateRandomConfig(string method, string authorizedSqlCondition = null, string transientErrors = DefaultTransientErrors)
{
TimeSpan start = TimeSpan.Zero;
TimeSpan end = TimeSpan.FromSeconds(60);
var min = GenerateTimeSpan(start, end);
return new RetryLogicConfigs()
{
DeltaTime = GenerateTimeSpan(start, end),
MinTimeInterval = min,
MaxTimeInterval = GenerateTimeSpan(min, end),
NumberOfTries = s_random.Next(1, 60),
AuthorizedSqlCondition = authorizedSqlCondition,
TransientErrors = transientErrors,
RetryMethodName = method
};
}
public static SqlException CreateSqlException(int errorNumber)
{
MethodInfo addSqlErrorMethod = typeof(SqlErrorCollection).GetMethod(AddMethodName, BindingFlags.Instance | BindingFlags.NonPublic);
MethodInfo createExceptionMethod = typeof(SqlException).GetMethod(CreateExceptionMethodName, BindingFlags.Static | BindingFlags.NonPublic,
null, new Type[] { typeof(SqlErrorCollection), typeof(string) }, null);
SqlError sqlError = s_sqlErrorCtorInfo.Invoke(new object[] { errorNumber, (byte)0, (byte)0, string.Empty, string.Empty, string.Empty, 0, null }) as SqlError;
SqlErrorCollection sqlErrorCollection = s_sqlErrorCollectionCtorInfo.Invoke(new object[0] { }) as SqlErrorCollection;
addSqlErrorMethod.Invoke(sqlErrorCollection, new object[] { sqlError });
SqlException sqlException = createExceptionMethod.Invoke(null, new object[] { sqlErrorCollection, string.Empty }) as SqlException;
return sqlException;
}
private static TimeSpan GenerateTimeSpan(TimeSpan start, TimeSpan end)
{
int max = (int)(end - start).TotalSeconds;
return start.Add(TimeSpan.FromSeconds(s_random.Next(max)));
}
}
public class RetryLogicConfigs : ICloneable
{
public TimeSpan DeltaTime { get; set; }
public TimeSpan MaxTimeInterval { get; set; }
public TimeSpan MinTimeInterval { get; set; }
public int NumberOfTries { get; set; }
public string TransientErrors { get; set; }
public string AuthorizedSqlCondition { get; set; }
public string RetryLogicType { get; set; }
public string RetryMethodName { get; set; }
public object Clone()
{
return MemberwiseClone();
}
}
}