Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,20 @@ The `connectionStringName` setting is required and must be set to the name of th

The `schemaName` settings is an optional string value that defaults to `null`. This property is required when database objects are provisioned under a custom schema name. If not specified, a schema name of `dt` is assumed. See the [Multitenancy](multitenancy.md) documentation for more details.

The `transientErrorNumbers` setting is an optional array of SQL error numbers, such as `[47073]`, that should be treated as transient. The provider already retries a built-in set of transient errors (deadlocks, connection drops, Azure SQL throttling, and so on). Errors listed here are retried with the same policy on top of that set. Use it for errors that are transient only in your environment, like Azure SQL error 47073 when DNS resolution briefly falls back to the public endpoint of a database that only allows private endpoint access. The default value is `null`.

The following errors are treated as transient by default and don't need to be configured:

| Category | SQL error numbers |
|---|---|
| Azure SQL throttling and resource limits | 10928, 10929, 10936, 40501, 49918, 49919, 49920 |
| Azure SQL service unavailable | 40197, 40613 |
| In-memory OLTP commit and validation conflicts | 41301, 41302, 41305, 41325, 41839 |
| Network and connection errors | 20, 64, 121, 233, 10053, 10054, 10060 |
| Deadlocks | 1205 |

A `TimeoutException` is also retried. SQL error `-2` (command timeout) is deliberately not retried, because the command may have completed on the server even though the client timed out.

To enable diagnostic logging, you can also add the following `logging` configuration to your **host.json** file:

```json
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class SqlDurabilityOptions
[JsonProperty("schemaName")]
public string? SchemaName { get; set; }

[JsonProperty("transientErrorNumbers")]
public int[]? TransientErrorNumbers { get; set; }

internal ILoggerFactory LoggerFactory { get; set; } = NullLoggerFactory.Instance;

internal SqlOrchestrationServiceSettings GetOrchestrationServiceSettings(
Expand Down Expand Up @@ -65,6 +68,7 @@ internal SqlOrchestrationServiceSettings GetOrchestrationServiceSettings(
LoggerFactory = this.LoggerFactory,
WorkItemBatchSize = this.TaskEventBatchSize,
WorkItemLockTimeout = this.TaskEventLockTimeout,
TransientErrorNumbers = this.TransientErrorNumbers,
};

if (extensionOptions.MaxConcurrentActivityFunctions.HasValue)
Expand Down
8 changes: 4 additions & 4 deletions src/DurableTask.SqlServer/SqlDbManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public async Task CreateOrUpgradeSchemaAsync(bool recreateIfExists)

try
{
using DbDataReader reader = await SqlUtils.ExecuteReaderAsync(command, this.traceHelper);
using DbDataReader reader = await SqlUtils.ExecuteReaderAsync(command, this.traceHelper, this.settings.TransientErrorNumbers);
if (await reader.ReadAsync())
{
// The first result contains the latest version
Expand Down Expand Up @@ -114,7 +114,7 @@ public async Task CreateOrUpgradeSchemaAsync(bool recreateIfExists)
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@SemanticVersion", SqlDbType.NVarChar, 100).Value = DTUtils.ExtensionVersion.ToString();

await SqlUtils.ExecuteNonQueryAsync(command, this.traceHelper);
await SqlUtils.ExecuteNonQueryAsync(command, this.traceHelper, this.settings.TransientErrorNumbers);
}

await dbLock.CommitAsync();
Expand Down Expand Up @@ -194,7 +194,7 @@ async Task<bool> DoesDatabaseExistAsync(string databaseName, SqlConnection conne
command.CommandText = "SELECT 1 FROM sys.databases WHERE name = @databaseName";
command.Parameters.AddWithValue("@databaseName", databaseName);

bool exists = (int?)await SqlUtils.ExecuteScalarAsync(command, this.traceHelper) == 1;
bool exists = (int?)await SqlUtils.ExecuteScalarAsync(command, this.traceHelper, this.settings.TransientErrorNumbers) == 1;
return exists;
}

Expand All @@ -205,7 +205,7 @@ async Task<bool> CreateDatabaseAsync(string databaseName, SqlConnection connecti

try
{
await SqlUtils.ExecuteNonQueryAsync(command, this.traceHelper);
await SqlUtils.ExecuteNonQueryAsync(command, this.traceHelper, this.settings.TransientErrorNumbers);
this.traceHelper.CreatedDatabase(databaseName);
return true;
}
Expand Down
27 changes: 16 additions & 11 deletions src/DurableTask.SqlServer/SqlOrchestrationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ public override async Task<TaskOrchestrationWorkItem> LockNextEntityWorkItemAsyn
reader = await SqlUtils.ExecuteReaderAsync(
command,
this.traceHelper,
this.settings.TransientErrorNumbers,
instanceId: null,
cancellationToken);
}
Expand Down Expand Up @@ -311,6 +312,7 @@ public override async Task<TaskOrchestrationWorkItem> LockNextEntityWorkItemAsyn
await SqlUtils.ExecuteNonQueryAsync(
discardCommand,
this.traceHelper,
this.settings.TransientErrorNumbers,
instanceId,
cancellationToken);
}
Expand Down Expand Up @@ -411,7 +413,7 @@ public override async Task RenewTaskOrchestrationWorkItemLockAsync(TaskOrchestra

try
{
await SqlUtils.ExecuteNonQueryAsync(command, this.traceHelper, workItem.InstanceId);
await SqlUtils.ExecuteNonQueryAsync(command, this.traceHelper, this.settings.TransientErrorNumbers, workItem.InstanceId);
}
catch (SqlException e) when (SqlUtils.HasErrorNumber(e, SqlOrchestrationSession.LockLostErrorNumber))
{
Expand Down Expand Up @@ -501,7 +503,7 @@ public override async Task CompleteTaskOrchestrationWorkItemAsync(

try
{
await SqlUtils.ExecuteNonQueryAsync(command, this.traceHelper, instance.InstanceId);
await SqlUtils.ExecuteNonQueryAsync(command, this.traceHelper, this.settings.TransientErrorNumbers, instance.InstanceId);
}
catch (SqlException e) when (SqlUtils.IsUniqueKeyViolation(e))
{
Expand Down Expand Up @@ -581,6 +583,7 @@ public override Task ReleaseTaskOrchestrationWorkItemAsync(TaskOrchestrationWork
using DbDataReader reader = await SqlUtils.ExecuteReaderAsync(
command,
this.traceHelper,
this.settings.TransientErrorNumbers,
instanceId: null,
shutdownCancellationToken);
if (!await reader.ReadAsync(shutdownCancellationToken))
Expand Down Expand Up @@ -619,7 +622,7 @@ public override async Task<TaskActivityWorkItem> RenewTaskActivityWorkItemLockAs
command.Parameters.Add("@LockExpiration", SqlDbType.DateTime2).Value = lockExpiration;

string instanceId = workItem.TaskMessage.OrchestrationInstance.InstanceId;
await SqlUtils.ExecuteNonQueryAsync(command, this.traceHelper, instanceId);
await SqlUtils.ExecuteNonQueryAsync(command, this.traceHelper, this.settings.TransientErrorNumbers, instanceId);

workItem.LockedUntilUtc = lockExpiration;
return workItem;
Expand All @@ -636,7 +639,7 @@ public override async Task CompleteTaskActivityWorkItemAsync(TaskActivityWorkIte
OrchestrationInstance instance = workItem.TaskMessage.OrchestrationInstance;
try
{
await SqlUtils.ExecuteNonQueryAsync(command, this.traceHelper, instance.InstanceId);
await SqlUtils.ExecuteNonQueryAsync(command, this.traceHelper, this.settings.TransientErrorNumbers, instance.InstanceId);
}
catch (SqlException e) when (e.Number == 50002)
{
Expand Down Expand Up @@ -679,7 +682,7 @@ public override async Task CreateTaskOrchestrationAsync(TaskMessage creationMess

try
{
await SqlUtils.ExecuteNonQueryAsync(command, this.traceHelper, instance.InstanceId);
await SqlUtils.ExecuteNonQueryAsync(command, this.traceHelper, this.settings.TransientErrorNumbers, instance.InstanceId);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still won't retry the error reported in #182: GetAndOpenConnectionAsync() calls OpenAsync() before this helper is reached, so a login/network failure such as 47073 escapes before the configured number is classified. Could the retry cover connection opening as well as command execution?

}
catch (SqlException e) when (e.Number == 50001 /* Instance ID for pending/running instance already exists */)
{
Expand All @@ -696,7 +699,7 @@ public override async Task SendTaskOrchestrationMessageAsync(TaskMessage message
command.Parameters.AddOrchestrationEventsParameter("@NewOrchestrationEvents", message, this.settings.SchemaName, this.traceHelper);

string instanceId = message.OrchestrationInstance.InstanceId;
await SqlUtils.ExecuteNonQueryAsync(command, this.traceHelper, instanceId);
await SqlUtils.ExecuteNonQueryAsync(command, this.traceHelper, this.settings.TransientErrorNumbers, instanceId);
}

public override async Task<OrchestrationState> WaitForOrchestrationAsync(
Expand Down Expand Up @@ -748,6 +751,7 @@ public override async Task<OrchestrationState> WaitForOrchestrationAsync(
using DbDataReader reader = await SqlUtils.ExecuteReaderAsync(
command,
this.traceHelper,
this.settings.TransientErrorNumbers,
instanceId,
cancellationToken);

Expand All @@ -769,7 +773,7 @@ public override async Task<string> GetOrchestrationHistoryAsync(string instanceI
command.Parameters.Add("@InstanceID", SqlDbType.VarChar, size: 100).Value = instanceId;
command.Parameters.Add("@GetInputsAndOutputs", SqlDbType.Bit).Value = true; // There's no clean way for the caller to specify this currently

using DbDataReader reader = await SqlUtils.ExecuteReaderAsync(command, this.traceHelper, instanceId);
using DbDataReader reader = await SqlUtils.ExecuteReaderAsync(command, this.traceHelper, this.settings.TransientErrorNumbers, instanceId);

List<HistoryEvent> history = ReadHistoryEvents(reader, executionIdFilter);
return DTUtils.SerializeToJson(history);
Expand Down Expand Up @@ -814,7 +818,7 @@ public override async Task ForceTerminateTaskOrchestrationAsync(string instanceI
command.Parameters.Add("@InstanceID", SqlDbType.VarChar, size: 100).Value = instanceId;
command.Parameters.Add("@Reason", SqlDbType.VarChar, size: -1 /* max */).Value = reason;

await SqlUtils.ExecuteNonQueryAsync(command, this.traceHelper, instanceId);
await SqlUtils.ExecuteNonQueryAsync(command, this.traceHelper, this.settings.TransientErrorNumbers, instanceId);
}

public async Task<int> PurgeOrchestrationHistoryAsync(IEnumerable<string> instanceIds)
Expand All @@ -833,7 +837,7 @@ public async Task<int> PurgeOrchestrationHistoryAsync(IEnumerable<string> instan
command.Parameters.AddInstanceIDsParameter("@InstanceIDs", instanceIds, this.settings.SchemaName);

Stopwatch latencyStopwatch = Stopwatch.StartNew();
await SqlUtils.ExecuteNonQueryAsync(command, this.traceHelper);
await SqlUtils.ExecuteNonQueryAsync(command, this.traceHelper, this.settings.TransientErrorNumbers);
int purgedInstanceCount = (int)instancesDeletedReturnValue.Value;
if (purgedInstanceCount > 0)
{
Expand All @@ -856,7 +860,7 @@ public override async Task PurgeOrchestrationHistoryAsync(
command.Parameters.Add("@ThresholdTime", SqlDbType.DateTime2).Value = maxThresholdDateTimeUtc;
command.Parameters.Add("@FilterType", SqlDbType.TinyInt).Value = (int)timeRangeFilterType;

await SqlUtils.ExecuteNonQueryAsync(command, this.traceHelper);
await SqlUtils.ExecuteNonQueryAsync(command, this.traceHelper, this.settings.TransientErrorNumbers);
}

public override async Task<PurgeResult> PurgeInstanceStateAsync(string instanceId)
Expand Down Expand Up @@ -987,6 +991,7 @@ public async Task<IReadOnlyCollection<OrchestrationState>> GetManyOrchestrations
using DbDataReader reader = await SqlUtils.ExecuteReaderAsync(
command,
this.traceHelper,
this.settings.TransientErrorNumbers,
instanceId: null,
cancellationToken);

Expand All @@ -1013,7 +1018,7 @@ public async Task RewindTaskOrchestrationAsync(string instanceId, string reason)
command.Parameters.Add("@InstanceID", SqlDbType.VarChar, size: 100).Value = instanceId;
command.Parameters.Add("@Reason", SqlDbType.VarChar, size: -1 /* max */).Value = reason;

await SqlUtils.ExecuteNonQueryAsync(command, this.traceHelper);
await SqlUtils.ExecuteNonQueryAsync(command, this.traceHelper, this.settings.TransientErrorNumbers);
}

/// <summary>
Expand Down
14 changes: 14 additions & 0 deletions src/DurableTask.SqlServer/SqlOrchestrationServiceSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace DurableTask.SqlServer
{
using System;
using System.Collections.Generic;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
Expand Down Expand Up @@ -198,6 +199,19 @@ public SqlOrchestrationServiceSettings(string connectionString, string? taskHubN
[JsonProperty("extendedSessionIdleTimeout")]
public TimeSpan ExtendedSessionIdleTimeout { get; set; } = TimeSpan.FromSeconds(30);

/// <summary>
/// Gets or sets additional SQL error numbers that should be treated as transient and retried,
/// on top of the built-in list of transient errors.
/// </summary>
/// <remarks>
/// Use this for errors that are not normally transient but occur transiently in a particular environment,
/// for example Azure SQL error 47073 when DNS resolution briefly falls back to the public endpoint of a
/// database that only allows private endpoint access. Commands that fail with one of these errors are
/// retried using the same retry policy as the built-in transient errors.
/// </remarks>
[JsonProperty("transientErrorNumbers")]
public IReadOnlyCollection<int>? TransientErrorNumbers { get; set; }

/// <summary>
/// Gets a SQL connection string associated with the configured task hub.
/// </summary>
Expand Down
5 changes: 3 additions & 2 deletions src/DurableTask.SqlServer/SqlOrchestrationSession.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace DurableTask.SqlServer
Expand Down Expand Up @@ -108,7 +108,7 @@ public async Task ReleaseLockAsync()
command.Parameters.Add("@InstanceID", SqlDbType.VarChar, 100).Value = this.instanceId;
command.Parameters.Add("@LockedBy", SqlDbType.VarChar, 100).Value = this.lockedByValue;

await SqlUtils.ExecuteNonQueryAsync(command, this.traceHelper, this.instanceId);
await SqlUtils.ExecuteNonQueryAsync(command, this.traceHelper, this.settings.TransientErrorNumbers, this.instanceId);
}
catch (Exception e)
{
Expand All @@ -135,6 +135,7 @@ async Task<IList<TaskMessage>> FetchAsync(DateTime newLockExpiration)
using DbDataReader reader = await SqlUtils.ExecuteReaderAsync(
command,
this.traceHelper,
this.settings.TransientErrorNumbers,
this.instanceId,
this.shutdownToken);

Expand Down
Loading