Skip to content

Commit

Permalink
Added abilty to disable retries (#3819)
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyGaluzo authored Apr 19, 2024
1 parent 622932a commit 30bc3da
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public async Task<bool> PutJobHeartbeatAsync(JobInfo jobInfo, CancellationToken
cmd.Parameters.AddWithValue("@Version", jobInfo.Version);
var cancelParam = new SqlParameter("@CancelRequested", SqlDbType.Bit) { Direction = ParameterDirection.Output };
cmd.Parameters.Add(cancelParam);
await cmd.ExecuteNonQueryAsync(_sqlRetryService, _logger, cancellationToken);
await cmd.ExecuteNonQueryAsync(_sqlRetryService, _logger, cancellationToken, disableRetries: true); // this should be fire and forget
cancel = (bool)cancelParam.Value;
}
catch (Exception ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public interface ISqlRetryService

Task ExecuteSql(Func<SqlConnection, CancellationToken, SqlException, Task> action, CancellationToken cancellationToken, bool isReadOnly = false);

Task ExecuteSql<TLogger>(SqlCommand sqlCommand, Func<SqlCommand, CancellationToken, Task> action, ILogger<TLogger> logger, string logMessage, CancellationToken cancellationToken, bool isReadOnly = false);
Task ExecuteSql<TLogger>(SqlCommand sqlCommand, Func<SqlCommand, CancellationToken, Task> action, ILogger<TLogger> logger, string logMessage, CancellationToken cancellationToken, bool isReadOnly = false, bool disableRetries = false);

Task<IReadOnlyList<TResult>> ExecuteReaderAsync<TResult, TLogger>(SqlCommand sqlCommand, Func<SqlDataReader, TResult> readerToResult, ILogger<TLogger> logger, string logMessage, CancellationToken cancellationToken, bool isReadOnly = false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ namespace Microsoft.Health.Fhir.SqlServer.Features.Storage
{
public static class SqlCommandExtensions
{
public static async Task ExecuteNonQueryAsync<TLogger>(this SqlCommand cmd, ISqlRetryService retryService, ILogger<TLogger> logger, CancellationToken cancellationToken, string logMessage = null, bool isReadOnly = false)
public static async Task ExecuteNonQueryAsync<TLogger>(this SqlCommand cmd, ISqlRetryService retryService, ILogger<TLogger> logger, CancellationToken cancellationToken, string logMessage = null, bool isReadOnly = false, bool disableRetries = false)
{
await retryService.ExecuteSql(cmd, async (sql, cancel) => await sql.ExecuteNonQueryAsync(cancel), logger, logMessage, cancellationToken, isReadOnly);
await retryService.ExecuteSql(cmd, async (sql, cancel) => await sql.ExecuteNonQueryAsync(cancel), logger, logMessage, cancellationToken, isReadOnly, disableRetries);
}

public static async Task<IReadOnlyList<TResult>> ExecuteReaderAsync<TResult, TLogger>(this SqlCommand cmd, ISqlRetryService retryService, Func<SqlDataReader, TResult> readerToResult, ILogger<TLogger> logger, CancellationToken cancellationToken, string logMessage = null, bool isReadOnly = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,10 @@ public async Task ExecuteSql(Func<SqlConnection, CancellationToken, SqlException
/// <param name="logMessage">Message to be logged on error.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <param name="isReadOnly">"Flag indicating whether connection to read only replica can be used."</param>
/// <param name="disableRetries">"Flag indicating whether retries are disabled."</param>
/// <returns>A task representing the asynchronous operation.</returns>
/// <exception>When executing this method, if exception is thrown that is not retriable or if last retry fails, then same exception is thrown by this method.</exception>
public async Task ExecuteSql<TLogger>(SqlCommand sqlCommand, Func<SqlCommand, CancellationToken, Task> action, ILogger<TLogger> logger, string logMessage, CancellationToken cancellationToken, bool isReadOnly = false)
public async Task ExecuteSql<TLogger>(SqlCommand sqlCommand, Func<SqlCommand, CancellationToken, Task> action, ILogger<TLogger> logger, string logMessage, CancellationToken cancellationToken, bool isReadOnly = false, bool disableRetries = false)
{
EnsureArg.IsNotNull(sqlCommand, nameof(sqlCommand));
EnsureArg.IsNotNull(action, nameof(action));
Expand Down Expand Up @@ -283,7 +284,7 @@ public async Task ExecuteSql<TLogger>(SqlCommand sqlCommand, Func<SqlCommand, Ca
catch (Exception ex)
{
lastException = ex;
if (!IsRetriable(ex))
if (disableRetries || !IsRetriable(ex))
{
throw;
}
Expand Down

0 comments on commit 30bc3da

Please sign in to comment.