From 69090f1717d237eb2c0251ece45476ae2bec5bad Mon Sep 17 00:00:00 2001 From: Andy Staples Date: Thu, 12 Nov 2026 11:41:08 -0700 Subject: [PATCH] Keep SQL scaling available after worker shutdown Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- docs/scaling.md | 4 ++- .../SqlOrchestrationService.cs | 5 +++- .../Integration/ScaleTests.cs | 27 +++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/docs/scaling.md b/docs/scaling.md index f015734..4e625b3 100644 --- a/docs/scaling.md +++ b/docs/scaling.md @@ -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. diff --git a/src/DurableTask.SqlServer/SqlOrchestrationService.cs b/src/DurableTask.SqlServer/SqlOrchestrationService.cs index 9b65bc0..7ea34bb 100644 --- a/src/DurableTask.SqlServer/SqlOrchestrationService.cs +++ b/src/DurableTask.SqlServer/SqlOrchestrationService.cs @@ -1034,10 +1034,13 @@ public async Task RewindTaskOrchestrationAsync(string instanceId, string reason) /// /// If specified, results in a trace message if the recommended replica count differs from this value. /// + /// Cancels the scale query independently of worker shutdown. /// Returns a non-negative recommended replica count integer value. public async Task 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; diff --git a/test/DurableTask.SqlServer.Tests/Integration/ScaleTests.cs b/test/DurableTask.SqlServer.Tests/Integration/ScaleTests.cs index 9bcb7f4..29d8232 100644 --- a/test/DurableTask.SqlServer.Tests/Integration/ScaleTests.cs +++ b/test/DurableTask.SqlServer.Tests/Integration/ScaleTests.cs @@ -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(() => + orchestrationService.GetRecommendedReplicaCountAsync(cancellationToken: cancellationSource.Token)); + } + /// /// Validates the scale recommendation behavior when scheduling new orchestration instances. ///