Skip to content
Merged
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
4 changes: 3 additions & 1 deletion docs/scaling.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ Here are the English definitions of the variables mentioned in this algorithm:
| *recommended_orchestrator_workers* | The number of worker replicas needed to handle all active and pending orchestrator events (i.e. `live_orchestrators`). Each orchestrator must run on a single worker at a time so the actual number of events per orchestrator does not matter. |
| *recommended_worker_count* | The total number of workers needed to handle all activity tasks and orchestrator events. |

This value can be calculated automatically using either the `dt.GetScaleRecommendation` SQL function, which takes concurrency settings as parameters, or the `SqlOrchestrationService.GetScaleRecommendation` .NET API, which discovers the concurrency settings from configuration. The final number can then be given to an auto-scale compute component to change the number of allocated worker replicas.
This value can be calculated automatically using either the `dt.GetScaleRecommendation` SQL function, which takes concurrency settings as parameters, or the `SqlOrchestrationService.GetRecommendedReplicaCountAsync` .NET API, which discovers the concurrency settings from configuration. The final number can then be given to an auto-scale compute component to change the number of allocated worker replicas.

Scale recommendation queries remain available after a task hub worker stops, including while an Azure Functions instance drains. The .NET API honors the caller's cancellation token independently of the worker's shutdown token.

If you're using the Durable SQL provider with [Azure Durable Functions](https://docs.microsoft.com/azure/azure-functions/durable) running on the [Elastic Premium Plan](https://docs.microsoft.com/azure/azure-functions/functions-premium-plan), then auto-scaling the number of app instances is managed automatically if you enable runtime scale monitoring as described [here](https://docs.microsoft.com/azure/azure-functions/functions-networking-options#premium-plan-with-virtual-network-triggers). Note that this doesn't require you to configure any virtual networking features.

Expand Down
5 changes: 4 additions & 1 deletion src/DurableTask.SqlServer/SqlOrchestrationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1034,10 +1034,13 @@ public async Task RewindTaskOrchestrationAsync(string instanceId, string reason)
/// <param name="currentReplicaCount">
/// If specified, results in a trace message if the recommended replica count differs from this value.
/// </param>
/// <param name="cancellationToken">Cancels the scale query independently of worker shutdown.</param>
/// <returns>Returns a non-negative recommended replica count integer value.</returns>
public async Task<int> GetRecommendedReplicaCountAsync(int? currentReplicaCount = null, CancellationToken cancellationToken = default)
{
using SqlConnection connection = await this.GetAndOpenConnectionAsync(cancellationToken);
// Scale queries must remain available after the worker stops during drain.
using SqlConnection connection = this.settings.CreateConnection();
await connection.OpenAsync(cancellationToken);
using SqlCommand command = connection.CreateCommand();
command.CommandText = $"SELECT {this.settings.SchemaName}.GetScaleRecommendation(@MaxConcurrentOrchestrations, @MaxConcurrentActivities)";
command.Parameters.Add("@MaxConcurrentOrchestrations", SqlDbType.Int).Value = this.MaxConcurrentTaskOrchestrationWorkItems;
Expand Down
27 changes: 27 additions & 0 deletions test/DurableTask.SqlServer.Tests/Integration/ScaleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,33 @@ public ScaleTests(ITestOutputHelper output)

Task IAsyncLifetime.DisposeAsync() => this.testService.DisposeAsync();

[Fact]
public async Task ScaleRecommendation_AfterServiceStopped()
{
this.testService.OrchestrationServiceOptions.MaxActiveOrchestrations = 1;
SqlOrchestrationService orchestrationService = this.testService.OrchestrationServiceMock.Object;
await orchestrationService.StartAsync();

await this.testService.RunOrchestration(
null as string,
"PendingOrchestration",
(ctx, input) => Task.FromResult(input));

Assert.Equal(1, await orchestrationService.GetRecommendedReplicaCountAsync());
await orchestrationService.StopAsync();

Assert.Equal(1, await orchestrationService.GetRecommendedReplicaCountAsync(
cancellationToken: CancellationToken.None));

using var cancellationSource = new CancellationTokenSource();
Assert.Equal(1, await orchestrationService.GetRecommendedReplicaCountAsync(
cancellationToken: cancellationSource.Token));

cancellationSource.Cancel();
await Assert.ThrowsAnyAsync<OperationCanceledException>(() =>
orchestrationService.GetRecommendedReplicaCountAsync(cancellationToken: cancellationSource.Token));
}

/// <summary>
/// Validates the scale recommendation behavior when scheduling new orchestration instances.
/// </summary>
Expand Down
Loading