-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add target based scaling support for MSSQL (#169)
Co-authored-by: Chris Gillum <[email protected]>
- Loading branch information
Showing
11 changed files
with
181 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/DurableTask.SqlServer.AzureFunctions/SqlMetricsProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
namespace DurableTask.SqlServer.AzureFunctions | ||
{ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
public class SqlMetricsProvider | ||
{ | ||
readonly SqlOrchestrationService service; | ||
|
||
public SqlMetricsProvider(SqlOrchestrationService service) | ||
{ | ||
this.service = service; | ||
} | ||
|
||
public virtual async Task<SqlScaleMetric> GetMetricsAsync(int? previousWorkerCount = null) | ||
{ | ||
// GetRecommendedReplicaCountAsync will write a trace if the recommendation results | ||
// in a worker count that is different from the worker count we pass in as an argument. | ||
int recommendedReplicaCount = await this.service.GetRecommendedReplicaCountAsync( | ||
previousWorkerCount, | ||
CancellationToken.None); | ||
|
||
return new SqlScaleMetric { RecommendedReplicaCount = recommendedReplicaCount }; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/DurableTask.SqlServer.AzureFunctions/SqlTargetScaler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
#if FUNCTIONS_V4 | ||
namespace DurableTask.SqlServer.AzureFunctions | ||
{ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.Azure.WebJobs.Host.Scale; | ||
|
||
public class SqlTargetScaler : ITargetScaler | ||
{ | ||
readonly SqlMetricsProvider sqlMetricsProvider; | ||
|
||
public SqlTargetScaler(string taskHubName, SqlMetricsProvider sqlMetricsProvider) | ||
{ | ||
this.sqlMetricsProvider = sqlMetricsProvider; | ||
|
||
// Scalers in Durable Functions are shared for all functions in the same task hub. | ||
// So instead of using a function ID, we use the task hub name as the basis for the descriptor ID. | ||
string id = $"DurableTask-SqlServer:{taskHubName ?? "default"}"; | ||
this.TargetScalerDescriptor = new TargetScalerDescriptor(id); | ||
} | ||
|
||
public TargetScalerDescriptor TargetScalerDescriptor { get; } | ||
|
||
public async Task<TargetScalerResult> GetScaleResultAsync(TargetScalerContext context) | ||
{ | ||
SqlScaleMetric sqlScaleMetric = await this.sqlMetricsProvider.GetMetricsAsync(); | ||
return new TargetScalerResult | ||
{ | ||
TargetWorkerCount = Math.Max(0, sqlScaleMetric.RecommendedReplicaCount), | ||
}; | ||
} | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
test/DurableTask.SqlServer.AzureFunctions.Tests/TargetBasedScalingTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
namespace DurableTask.SqlServer.AzureFunctions.Tests | ||
{ | ||
using DurableTask.Core; | ||
using Microsoft.Azure.WebJobs.Extensions.DurableTask; | ||
using Microsoft.Azure.WebJobs.Host.Scale; | ||
using Moq; | ||
using Xunit; | ||
|
||
public class TargetBasedScalingTests | ||
{ | ||
readonly Mock<SqlMetricsProvider> metricsProviderMock; | ||
readonly Mock<IOrchestrationService> orchestrationServiceMock; | ||
|
||
public TargetBasedScalingTests() | ||
{ | ||
this.orchestrationServiceMock = new Mock<IOrchestrationService>(MockBehavior.Strict); | ||
|
||
SqlOrchestrationService? nullServiceArg = null; // not needed for this test | ||
this.metricsProviderMock = new Mock<SqlMetricsProvider>( | ||
behavior: MockBehavior.Strict, | ||
nullServiceArg); | ||
} | ||
|
||
[Theory] | ||
[InlineData(0)] | ||
[InlineData(10)] | ||
[InlineData(20)] | ||
public async void TargetBasedScalingTest(int expectedTargetWorkerCount) | ||
{ | ||
var durabilityProviderMock = new Mock<DurabilityProvider>( | ||
MockBehavior.Strict, | ||
"storageProviderName", | ||
this.orchestrationServiceMock.Object, | ||
new Mock<IOrchestrationServiceClient>().Object, | ||
"connectionName"); | ||
|
||
var sqlScaleMetric = new SqlScaleMetric() | ||
{ | ||
RecommendedReplicaCount = expectedTargetWorkerCount, | ||
}; | ||
|
||
this.metricsProviderMock.Setup(m => m.GetMetricsAsync(null)).ReturnsAsync(sqlScaleMetric); | ||
|
||
var targetScaler = new SqlTargetScaler( | ||
"functionId", | ||
this.metricsProviderMock.Object); | ||
|
||
TargetScalerResult result = await targetScaler.GetScaleResultAsync(new TargetScalerContext()); | ||
|
||
Assert.Equal(expectedTargetWorkerCount, result.TargetWorkerCount); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters