Skip to content

Commit

Permalink
handle long desired worker count
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Chiang committed Oct 24, 2023
1 parent 4364766 commit e5a8756
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ internal static TargetScalerResult ThrottleScaleDownIfNecessaryInternal(TargetSc
internal TargetScalerResult GetScaleResultInternal(TargetScalerContext context, long eventCount, int partitionCount)
{
int desiredConcurrency = GetDesiredConcurrencyInternal(context);
int desiredWorkerCount = (int)Math.Ceiling(eventCount / (decimal)desiredConcurrency);
long desiredWorkerCount = (long)Math.Ceiling(eventCount / (decimal)desiredConcurrency);
int[] sortedValidWorkerCounts = GetSortedValidWorkerCountsForPartitionCount(partitionCount);
int validatedTargetWorkerCount = GetValidWorkerCount(desiredWorkerCount, sortedValidWorkerCounts);

Expand Down Expand Up @@ -163,9 +163,15 @@ internal static int[] GetSortedValidWorkerCountsForPartitionCount(int partitionC
/// <param name="workerCount">The value that we want to find in the sortedValues list (if it exists), or the next largest element.</param>
/// <param name="sortedValidWorkerCountList">The list of valid worker counts. This must be sorted.</param>
/// <returns></returns>
internal static int GetValidWorkerCount(int workerCount, int[] sortedValidWorkerCountList)
internal static int GetValidWorkerCount(long workerCount, int[] sortedValidWorkerCountList)
{
int i = Array.BinarySearch(sortedValidWorkerCountList, workerCount);
// Handling cases where workerCount exceeds int.MaxInt
if (workerCount > int.MaxValue)
{
return sortedValidWorkerCountList.Last();
}

int i = Array.BinarySearch(sortedValidWorkerCountList, (int)workerCount);
if (i >= 0)
{
return sortedValidWorkerCountList[i];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ public void TestGetSortedValidWorkerCountsForPartitionCount(int partitionCount,
[TestCase(21, new[] { 1, 4, 10, 20 }, 20)]
[TestCase(0, new[] { 1, 4, 10, 20 }, 1)]
[TestCase(10, new[] { 1, 4, 10, 20 }, 10)]
public void GetSortedValidWorkerCountsForPartitionCount_ReturnsExpected(int workerCount, int[] sortedWorkerCountList, int expectedValidWorkerCount)
[TestCase(2147483650, new[] { 1, 4, 10, 20 }, 20)] // workerCount exceeds MaxInt
[TestCase(2147483650, new[] { 1 }, 1)]
public void GetSortedValidWorkerCountsForPartitionCount_ReturnsExpected(long workerCount, int[] sortedWorkerCountList, int expectedValidWorkerCount)
{
int actualValidWorkerCount = EventHubsTargetScaler.GetValidWorkerCount(workerCount, sortedWorkerCountList);
Assert.AreEqual(expectedValidWorkerCount, actualValidWorkerCount);
Expand Down

0 comments on commit e5a8756

Please sign in to comment.