Skip to content

Commit

Permalink
Configurable Retry Logic (#966)
Browse files Browse the repository at this point in the history
  • Loading branch information
DavoudEshtehari authored Mar 15, 2021
1 parent 7b6e863 commit b35b3e3
Show file tree
Hide file tree
Showing 20 changed files with 907 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ The following table shows the inner transient error list.
|10053|Could not convert the data value due to reasons other than sign mismatch or overflow.|
|997|A connection was successfully established with the server, but then an error occurred during the login process. (provider: Named Pipes Provider, error: 0 - Overlapped I/O operation is in progress)|
|233|A connection was successfully established with the server, but then an error occurred during the login process. (provider: Shared Memory Provider, error: 0 - No process is on the other end of the pipe.) (Microsoft SQL Server, Error: 233)|
|64||A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0 - The specified network name is no longer available.)|
|20||
|0||
]]>
</format>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,12 @@
<Compile Include="..\..\src\Microsoft\Data\SqlClient\Reliability\SqlRetryIntervalEnumerators.cs">
<Link>Microsoft\Data\SqlClient\Reliability\SqlRetryIntervalEnumerators.cs</Link>
</Compile>
<Compile Include="..\..\src\Microsoft\Data\SqlClient\Reliability\SqlConfigurableRetryLogicManager.cs">
<Link>Microsoft\Data\SqlClient\Reliability\SqlConfigurableRetryLogicManager.cs</Link>
</Compile>
<Compile Include="..\..\src\Microsoft\Data\SqlClient\SqlUtil.cs">
<Link>Microsoft\Data\SqlClient\SqlUtil.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup Condition="'$(TargetGroup)' == 'netstandard' OR '$(TargetGroup)' == 'netcoreapp' OR '$(IsUAPAssembly)' == 'true'">
<Compile Include="Microsoft.Data.SqlClient.TypeForwards.cs" />
Expand Down Expand Up @@ -506,12 +512,6 @@
<Compile Include="Microsoft\Data\SqlClient\SqlColumnEncryptionEnclaveProvider.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlEnclaveAttestationParameters.cs" />
<Compile Include="Microsoft\Data\SqlClient\EnclaveDelegate.cs" />
<Compile Include="..\..\src\Microsoft\Data\SqlClient\Reliability\SqlConfigurableRetryLogicManager.cs">
<Link>Microsoft\Data\SqlClient\Reliability\SqlConfigurableRetryLogicManager.cs</Link>
</Compile>
<Compile Include="..\..\src\Microsoft\Data\SqlClient\SqlUtil.cs">
<Link>Microsoft\Data\SqlClient\SqlUtil.cs</Link>
</Compile>
</ItemGroup>
<!-- Windows only -->
<ItemGroup Condition="'$(TargetsWindows)' == 'true'">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,21 @@ private SqlInternalConnectionTds InternalTdsConnection
}
}

private bool? _isRetryEnabled;
private bool IsRetryEnabled
{
get
{
if (_isRetryEnabled == null)
{
bool result;
result = AppContext.TryGetSwitch(SqlRetryLogicProvider.EnableRetryLogicSwitch, out result) ? result : false;
_isRetryEnabled = result;
}
return (bool)_isRetryEnabled;
}
}

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlCommand.xml' path='docs/members[@name="SqlCommand"]/RetryLogicProvider/*' />
public SqlRetryLogicBaseProvider RetryLogicProvider
{
Expand Down Expand Up @@ -998,6 +1013,9 @@ protected override void Dispose(bool disposing)
base.Dispose(disposing);
}

private SqlDataReader RunExecuteReaderWithRetry(CommandBehavior cmdBehavior, RunBehavior runBehavior, bool returnStream, [CallerMemberName] string method = "")
=> RetryLogicProvider.Execute(this, () => RunExecuteReader(cmdBehavior, runBehavior, returnStream, method));

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlCommand.xml' path='docs/members[@name="SqlCommand"]/ExecuteScalar/*'/>
public override object ExecuteScalar()
{
Expand All @@ -1021,7 +1039,9 @@ public override object ExecuteScalar()
statistics = SqlStatistics.StartTimer(Statistics);
WriteBeginExecuteEvent();
SqlDataReader ds;
ds = RetryLogicProvider.Execute(this, () => RunExecuteReader(0, RunBehavior.ReturnImmediately, returnStream: true));
ds = IsRetryEnabled ?
RunExecuteReaderWithRetry(0, RunBehavior.ReturnImmediately, returnStream: true) :
RunExecuteReader(0, RunBehavior.ReturnImmediately, returnStream: true, method: nameof(ExecuteScalar));
success = true;

return CompleteExecuteScalar(ds, false);
Expand Down Expand Up @@ -1083,6 +1103,14 @@ private object CompleteExecuteScalar(SqlDataReader ds, bool returnSqlValue)
return retResult;
}

private Task InternalExecuteNonQueryWithRetry(bool sendToPipe, int timeout, out bool usedCache, bool asyncWrite, bool inRetry, [CallerMemberName] string methodName = "")
{
bool innerUsedCache = false;
Task result = RetryLogicProvider.Execute(this, () => InternalExecuteNonQuery(completion: null, sendToPipe, timeout, out innerUsedCache, asyncWrite, inRetry, methodName));
usedCache = innerUsedCache;
return result;
}

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlCommand.xml' path='docs/members[@name="SqlCommand"]/ExecuteNonQuery[@name="default"]/*'/>
public override int ExecuteNonQuery()
{
Expand All @@ -1101,7 +1129,14 @@ public override int ExecuteNonQuery()
{
statistics = SqlStatistics.StartTimer(Statistics);
WriteBeginExecuteEvent();
RetryLogicProvider.Execute(this, () => InternalExecuteNonQuery(completion: null, sendToPipe: false, timeout: CommandTimeout, out _, methodName: nameof(ExecuteNonQuery)));
if (IsRetryEnabled)
{
InternalExecuteNonQueryWithRetry(sendToPipe: false, timeout: CommandTimeout, out _, asyncWrite: false, inRetry: false);
}
else
{
InternalExecuteNonQuery(completion: null, sendToPipe: false, timeout: CommandTimeout, out _);
}
return _rowsAffected;
}
catch (Exception ex)
Expand Down Expand Up @@ -1594,7 +1629,9 @@ public XmlReader ExecuteXmlReader()
WriteBeginExecuteEvent();
// use the reader to consume metadata
SqlDataReader ds;
ds = RetryLogicProvider.Execute(this, () => RunExecuteReader(CommandBehavior.SequentialAccess, RunBehavior.ReturnImmediately, returnStream: true));
ds = IsRetryEnabled ?
RunExecuteReaderWithRetry(CommandBehavior.SequentialAccess, RunBehavior.ReturnImmediately, returnStream: true):
RunExecuteReader(CommandBehavior.SequentialAccess, RunBehavior.ReturnImmediately, returnStream: true);
success = true;
return CompleteXmlReader(ds);
}
Expand Down Expand Up @@ -1916,7 +1953,9 @@ protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)
{
WriteBeginExecuteEvent();
statistics = SqlStatistics.StartTimer(Statistics);
return RetryLogicProvider.Execute(this, () => RunExecuteReader(behavior, RunBehavior.ReturnImmediately, returnStream: true));
return IsRetryEnabled ?
RunExecuteReaderWithRetry(behavior, RunBehavior.ReturnImmediately, returnStream: true) :
RunExecuteReader(behavior, RunBehavior.ReturnImmediately, returnStream: true);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -2401,9 +2440,12 @@ private SqlDataReader InternalEndExecuteReader(IAsyncResult asyncResult, bool is

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlCommand.xml' path='docs/members[@name="SqlCommand"]/ExecuteNonQueryAsync[@name="CancellationToken"]/*'/>
public override Task<int> ExecuteNonQueryAsync(CancellationToken cancellationToken)
{
return RetryLogicProvider.ExecuteAsync(this, () => InternalExecuteNonQueryAsync(cancellationToken), cancellationToken);
}
=> IsRetryEnabled ?
InternalExecuteNonQueryWithRetryAsync(cancellationToken) :
InternalExecuteNonQueryAsync(cancellationToken);

private Task<int> InternalExecuteNonQueryWithRetryAsync(CancellationToken cancellationToken)
=> RetryLogicProvider.ExecuteAsync(this, () => InternalExecuteNonQueryAsync(cancellationToken), cancellationToken);

private Task<int> InternalExecuteNonQueryAsync(CancellationToken cancellationToken)
{
Expand Down Expand Up @@ -2493,9 +2535,12 @@ protected override Task<DbDataReader> ExecuteDbDataReaderAsync(CommandBehavior b

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlCommand.xml' path='docs/members[@name="SqlCommand"]/ExecuteReaderAsync[@name="commandBehaviorAndCancellationToken"]/*'/>
new public Task<SqlDataReader> ExecuteReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)
{
return RetryLogicProvider.ExecuteAsync(this, () => InternalExecuteReaderAsync(behavior, cancellationToken), cancellationToken);
}
=> IsRetryEnabled ?
InternalExecuteReaderWithRetryAsync(behavior, cancellationToken) :
InternalExecuteReaderAsync(behavior, cancellationToken);

private Task<SqlDataReader> InternalExecuteReaderWithRetryAsync(CommandBehavior behavior, CancellationToken cancellationToken)
=> RetryLogicProvider.ExecuteAsync(this, () => InternalExecuteReaderAsync(behavior, cancellationToken), cancellationToken);

private Task<SqlDataReader> InternalExecuteReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)
{
Expand Down Expand Up @@ -2568,9 +2613,12 @@ private void SetCachedCommandExecuteReaderAsyncContext(ExecuteReaderAsyncCallCon

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlCommand.xml' path='docs/members[@name="SqlCommand"]/ExecuteScalarAsync[@name="CancellationToken"]/*'/>
public override Task<object> ExecuteScalarAsync(CancellationToken cancellationToken)
{
return RetryLogicProvider.ExecuteAsync(this, () => InternalExecuteScalarAsync(cancellationToken), cancellationToken);
}
=> IsRetryEnabled ?
InternalExecuteScalarWithRetryAsync(cancellationToken) :
InternalExecuteScalarAsync(cancellationToken);

private Task<object> InternalExecuteScalarWithRetryAsync(CancellationToken cancellationToken)
=> RetryLogicProvider.ExecuteAsync(this, () => InternalExecuteScalarAsync(cancellationToken), cancellationToken);

private Task<object> InternalExecuteScalarAsync(CancellationToken cancellationToken)
{
Expand Down Expand Up @@ -2664,9 +2712,12 @@ public Task<XmlReader> ExecuteXmlReaderAsync()

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlCommand.xml' path='docs/members[@name="SqlCommand"]/ExecuteXmlReaderAsync[@name="CancellationToken"]/*'/>
public Task<XmlReader> ExecuteXmlReaderAsync(CancellationToken cancellationToken)
{
return RetryLogicProvider.ExecuteAsync(this, () => InternalExecuteXmlReaderAsync(cancellationToken), cancellationToken);
}
=> IsRetryEnabled ?
InternalExecuteXmlReaderWithRetryAsync(cancellationToken) :
InternalExecuteXmlReaderAsync(cancellationToken);

private Task<XmlReader> InternalExecuteXmlReaderWithRetryAsync(CancellationToken cancellationToken)
=> RetryLogicProvider.ExecuteAsync(this, () => InternalExecuteXmlReaderAsync(cancellationToken), cancellationToken);

private Task<XmlReader> InternalExecuteXmlReaderAsync(CancellationToken cancellationToken)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,21 @@ private static readonly ConcurrentDictionary<string, IList<string>> _ColumnEncry
private static readonly Action<object> s_openAsyncCancel = OpenAsyncCancel;
private static readonly Action<Task<object>, object> s_openAsyncComplete = OpenAsyncComplete;

private bool? _isRetryEnabled;
private bool IsRetryEnabled
{
get
{
if (_isRetryEnabled == null)
{
bool result;
result = AppContext.TryGetSwitch(SqlRetryLogicProvider.EnableRetryLogicSwitch, out result) ? result : false;
_isRetryEnabled = result;
}
return (bool)_isRetryEnabled;
}
}

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlConnection.xml' path='docs/members[@name="SqlConnection"]/RetryLogicProvider/*' />
public SqlRetryLogicBaseProvider RetryLogicProvider
{
Expand Down Expand Up @@ -1188,6 +1203,9 @@ public override void Open()
Open(SqlConnectionOverrides.None);
}

private bool TryOpenWithRetry(TaskCompletionSource<DbConnectionInternal> retry, SqlConnectionOverrides overrides)
=> RetryLogicProvider.Execute(this, () => TryOpen(retry, overrides));

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlConnection.xml' path='docs/members[@name="SqlConnection"]/OpenWithOverrides/*' />
public void Open(SqlConnectionOverrides overrides)
{
Expand All @@ -1205,7 +1223,7 @@ public void Open(SqlConnectionOverrides overrides)
try
{
statistics = SqlStatistics.StartTimer(Statistics);
if (!RetryLogicProvider.Execute(this, () => TryOpen(null, overrides)))
if (!(IsRetryEnabled ? TryOpenWithRetry(null, overrides) : TryOpen(null, overrides)))
{
throw ADP.InternalError(ADP.InternalErrorCode.SynchronousConnectReturnedPending);
}
Expand Down Expand Up @@ -1455,11 +1473,14 @@ private void CancelOpenAndWait()
Debug.Assert(_currentCompletion == null, "After waiting for an async call to complete, there should be no completion source");
}

private Task InternalOpenWithRetryAsync(CancellationToken cancellationToken)
=> RetryLogicProvider.ExecuteAsync(this, () => InternalOpenAsync(cancellationToken), cancellationToken);

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlConnection.xml' path='docs/members[@name="SqlConnection"]/OpenAsync/*' />
public override Task OpenAsync(CancellationToken cancellationToken)
{
return RetryLogicProvider.ExecuteAsync(this, () => InternalOpenAsync(cancellationToken), cancellationToken);
}
=> IsRetryEnabled ?
InternalOpenWithRetryAsync(cancellationToken) :
InternalOpenAsync(cancellationToken);

private Task InternalOpenAsync(CancellationToken cancellationToken)
{
Expand Down
Loading

0 comments on commit b35b3e3

Please sign in to comment.