Example #1
0
        public static async Task ScaleAsync(this IDocumentClient documentClient, OfferV2 offer, int scaleBatch, int minThroughput, int maxThroughput)
        {
            var currentThroughput = offer.Content.OfferThroughput;

            var newThroughput = currentThroughput + scaleBatch;

            if (newThroughput < minThroughput)
            {
                newThroughput = minThroughput;
            }

            if (newThroughput > maxThroughput)
            {
                newThroughput = maxThroughput;
            }
            var updatedOffer = new OfferV2(offer, newThroughput);
            await documentClient.ReplaceOfferAsync(updatedOffer);
        }
        public async Task <ScaleResponse> AdjustThroughputAsync(ScaleRequest scaleRequest)
        {
            var currentOffer = await GetCurrentOfferAsync();

            if (NeedScale(scaleRequest, currentOffer))
            {
                Offer replaced = await _client.ReplaceOfferAsync(new OfferV2(currentOffer, scaleRequest.TargetThroughput));

                return(new ScaleResponse
                {
                    IsAccepted = true,
                    AdjustedThroughput = replaced.GetThroughput()
                });
            }

            return(new ScaleResponse()
            {
                IsAccepted = false,
                AdjustedThroughput = currentOffer.GetThroughput()
            });
        }
        public static async System.Threading.Tasks.Task <IActionResult> RunAsync([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log)
        {
            try
            {
                log.LogInformation("C# HTTP trigger function processed a request.");

                string action = req.Query["action"];

                if (string.IsNullOrEmpty(action) ||
                    (!action.Equals(_scaleUp, StringComparison.InvariantCultureIgnoreCase) &&
                     !action.Equals(_scaleDown, StringComparison.InvariantCultureIgnoreCase)))
                {
                    return(new BadRequestObjectResult($"Please pass an action on the query string: {_scaleUp} or {_scaleDown}"));
                }

                var collection = await _client.ReadDocumentCollectionAsync(_collectionUri);

                var offer = (OfferV2)_client.CreateOfferQuery()
                            .Where(r => r.ResourceLink == collection.Resource.SelfLink)
                            .AsEnumerable()
                            .SingleOrDefault();
                int newThroughput     = 0;
                var currentThroughput = offer.Content.OfferThroughput;

                if (action.Equals(_scaleUp, StringComparison.InvariantCultureIgnoreCase))
                {
                    // Note: trigger this operation from the Alerts view in Cosmos DB, based on the number of throttle requests
                    if (currentThroughput * 2 <= _maxAuthorizedRu)
                    {
                        newThroughput = currentThroughput * 2;
                    }
                    else if (currentThroughput + 1 <= _maxAuthorizedRu)
                    {
                        newThroughput = currentThroughput + 1;
                    }
                    else
                    {
                        return(new OkObjectResult("Max Throughput reached"));
                    }
                }
                else if (action.Equals(_scaleDown, StringComparison.InvariantCultureIgnoreCase))
                {
                    // Note: to trigger this operation from the Alerts view in Cosmos DB, based on the number of throttle requests
                    if (currentThroughput / 2 >= _minAuthorizedRu)
                    {
                        newThroughput = currentThroughput / 2;
                    }
                    else if (currentThroughput - 1 >= _minAuthorizedRu)
                    {
                        newThroughput = currentThroughput - 1;
                    }
                    else
                    {
                        return(new OkObjectResult("Min Throughput reached"));
                    }
                }

                offer = new OfferV2(offer, newThroughput);
                await _client.ReplaceOfferAsync(offer);

                return(new OkObjectResult($"Hello, {action}"));
            }
            catch (Exception e)
            {
                return(new BadRequestObjectResult($"{e.Message}"));
            }
        }