internal TargetScalerResult GetScaleResultInternal()

in src/WebJobs.Extensions.CosmosDB/Trigger/CosmosDBTargetScaler.cs [41:88]


        internal TargetScalerResult GetScaleResultInternal(TargetScalerContext context, long remainingWork, int partitionCount)
        {
            int concurrency;

            if (!context.InstanceConcurrency.HasValue)
            {
                concurrency = _maxItemsPerInvocation > 0 ? _maxItemsPerInvocation : DefaultMaxItemsPerInvocation;
            }
            else
            {
                concurrency = context.InstanceConcurrency.Value;
            }

            if (concurrency <= 0)
            {
                _logger.LogWarning($"Concurrency value for target based scale must be greater than 0. Using default value of {DefaultMaxItemsPerInvocation} as concurrency value.");
                concurrency = DefaultMaxItemsPerInvocation;
            }

            int targetWorkerCount;

            try
            {
                checked
                {
                    targetWorkerCount = (int)Math.Ceiling(remainingWork / (decimal)concurrency);
                }
            }
            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}').";

            if (partitionCount > 0 && targetWorkerCount > partitionCount)
            {
                targetScaleMessage += $" However, partition count is {partitionCount}. Adding more workers than partitions would not be helpful, so capping target worker count at {partitionCount}";
                targetWorkerCount = partitionCount;
            }

            _logger.LogInformation(targetScaleMessage);

            return new TargetScalerResult
            {
                TargetWorkerCount = targetWorkerCount
            };
        }