-
Notifications
You must be signed in to change notification settings - Fork 33
Add target based scaling support for MSSQL #169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fae10ca
initial commit
bachuv f4527f6
update csproj
bachuv a469297
added previousWorkerCount as argument for GetMetricsAsync()
bachuv 4b952a5
adding tests
bachuv 26dedfc
Merge branch 'main' into vabachu/tbs
cgillum c4498c4
PR feedback updates, fix build warnings, update CHANGELOG.md, etc.
cgillum b9d6891
Fix failing tests caused by version update
cgillum 830934f
PR feedback and minor cleanup
cgillum 994d78c
Merge branch 'main' into vabachu/tbs
cgillum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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) | ||
cgillum marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.