Esempio n. 1
0
 public static async Task <OfferV2> GetOfferFromSelfLinkAsync(this IDocumentClient documentClient, string selfLink)
 {
     return((await documentClient.CreateOfferQuery()
             .Where(o => o.ResourceLink == selfLink)
             .AsDocumentQuery()
             .ExecuteNextAsync <OfferV2>()).FirstOrDefault());
 }
Esempio n. 2
0
        private static async Task <bool> IsSharedThroughputEnabledAsync(IDocumentClient client)
        {
            Uri databaseUri = UriFactory.CreateDatabaseUri("TablesDB");

            try
            {
                return((await client.CreateOfferQuery($"SELECT * FROM offers o WHERE o.resource = '{(await client.ReadDatabaseAsync(databaseUri)).Resource.SelfLink}'")
                        .AsDocumentQuery().ExecuteNextAsync <Offer>()).AsEnumerable().SingleOrDefault() != null);
            }
            // catch (NotFoundException)
            // {
            //  return false;
            // }
            catch (DocumentClientException ex2)
            {
                if (ex2.StatusCode.HasValue && ex2.StatusCode.Value.Equals(HttpStatusCode.NotFound))
                {
                    return(false);
                }
                throw;
            }
            catch (Exception ex)
            {
                if (ex.GetType().Name == "NotFoundException")
                {
                    return(false);
                }
                throw;
            }
        }
        private async Task <Offer> GetCurrentOfferAsync()
        {
            // this proccess's RU is 1
            DocumentCollection collection = await _client.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(_databaseId, _collectionId));

            var currentOffer = _client.CreateOfferQuery()
                               .AsEnumerable()
                               .Single(o => o.ResourceLink == collection.SelfLink);

            return(currentOffer);
        }
        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}"));
            }
        }