Skip to content
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

[Cosmos DB] Handle cases where desired worker count exceeds int.MaxInt #873

Merged
merged 7 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
11 changes: 10 additions & 1 deletion src/WebJobs.Extensions.CosmosDB/Trigger/CosmosDBTargetScaler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,16 @@ internal TargetScalerResult GetScaleResultInternal(TargetScalerContext context,
concurrency = DefaultMaxItemsPerInvocation;
}

int targetWorkerCount = (int)Math.Ceiling(remainingWork / (decimal)concurrency);
int targetWorkerCount;

try
{
targetWorkerCount = (int)Math.Ceiling(remainingWork / (decimal)concurrency);
Copy link
Member

@paulbatum paulbatum Oct 24, 2023

Choose a reason for hiding this comment

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

I suggest wrapping in a checked block - doing so makes sure that this operation will always throw an overflow exception regardless of the project level settings

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/checked-and-unchecked

Copy link
Member Author

Choose a reason for hiding this comment

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

done, updated all other SDK PRs to match this implementation

}
catch (OverflowException)
{
targetWorkerCount = int.MaxValue;
}

string targetScaleMessage = $"Target worker count for function '{_functionId}' is '{targetWorkerCount}' (MonitoredContainerId='{_monitoredContainer.Id}', MonitoredContainerDatabaseId='{_monitoredContainer.Database.Id}', RemainingWork ='{remainingWork}', Concurrency='{concurrency}').";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ public CosmosDBTargetScalerTests()
[InlineData(null, 0, 3, 0)]
[InlineData(50, 200, 0, 4)]
[InlineData(-50, 200, 0, 2)]
[InlineData(1, 2147483650, 1, 1)]
[InlineData(1, 2147483650, 2147483647, 2147483647)]
[InlineData(2, 2147483650, 1073741825, 1073741825)]
public void GetScaleResultInternal(int? concurrency, long remainingWork, int partitionCount, int expectedTargetWorkerCount)
{
TargetScalerContext targetScalerContext = new TargetScalerContext
Expand Down