Exemplo n.º 1
0
        private async Task <CosmosOperationResponse> ReplaceDocumentAsync(string oldDocumentId, object newDocument, int retryCount, CosmosOperationResponse result)
        {
            try
            {
                var docUri = UriFactory.CreateDocumentUri(_databaseName, _collectionName, oldDocumentId);
                await _client.ReplaceDocumentAsync(docUri, newDocument);

                result.Success      = true;
                result.TotalRetries = retryCount;
                return(result);
            }
            catch (Exception e)
            {
                if (e.Message.Contains("Request rate is large"))
                {
                    if (retryCount > _maximumRetryCount)
                    {
                        result.Success      = false;
                        result.TotalRetries = retryCount;
                        return(result);
                    }
                    else
                    {
                        var op = ScaleLogic.ScaleUpCollectionAsync(_client, _metaDataOperator, _databaseName, _collectionName, _minRu, _maxRu);
                        result.ScaleOperations.Add(op);
                        return(await ReplaceDocumentAsync(oldDocumentId, newDocument, retryCount ++, result));
                    }
                }
                else
                {
                    throw;
                }
            }
        }
Exemplo n.º 2
0
 private async Task <CosmosOperationResponse> DeleteDocumentAsync(string id, int retryCount, CosmosOperationResponse result, object partitionKey = null)
 {
     try
     {
         var docUri = UriFactory.CreateDocumentUri(_databaseName, _collectionName, id);
         if (partitionKey == null)
         {
             await _client.DeleteDocumentAsync(docUri, new RequestOptions()
             {
                 PartitionKey = new PartitionKey(id)
             });
         }
         else
         {
             await _client.DeleteDocumentAsync(docUri, new RequestOptions()
             {
                 PartitionKey = new PartitionKey(partitionKey)
             });
         }
         result.Success      = true;
         result.TotalRetries = retryCount;
         return(result);
     }
     catch (Exception e)
     {
         if (e.Message.Contains("Request rate is large"))
         {
             if (retryCount > _maximumRetryCount)
             {
                 result.Success      = false;
                 result.TotalRetries = retryCount;
                 return(result);
             }
             else
             {
                 var op = ScaleLogic.ScaleUpCollectionAsync(_client, _metaDataOperator, _databaseName, _collectionName, _minRu, _maxRu);
                 result.ScaleOperations.Add(op);
                 return(await DeleteDocumentAsync(id, retryCount ++, result, partitionKey));
             }
         }
         else
         {
             throw;
         }
     }
 }
Exemplo n.º 3
0
        public BulkInsertOpeartionResult BulkDeleteDocuments(string query, int?deleteBatchSize = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            _metaDataOperator.AddActivity(_databaseName, _collectionName, DateTimeOffset.Now, ActivityStrength.Hot).Wait();
            lock (bulkDeleteLock)
            {
                var scaleOperation = ScaleLogic.ScaleUpMaxCollectionAsync(_client, _metaDataOperator, _databaseName, _collectionName, _minRu, _maxRu).GetAwaiter().GetResult();
                var response       = _bulkExecutor.BulkDeleteAsync(query, deleteBatchSize, cancellationToken).GetAwaiter().GetResult();

                return(new BulkInsertOpeartionResult
                {
                    ScaleOperations = new List <ScaleOperation>()
                    {
                        scaleOperation
                    },
                    OperationSuccess = true
                });
            }
        }
Exemplo n.º 4
0
        public BulkInsertOpeartionResult BulkInsertDocuments(IEnumerable <object> documents, bool enableUpsert = false, bool disableAutomaticIdGeneration = true, int?maxConcurrencyPerPartitionKeyRange = null,
                                                             int?maxInMemorySortingBatchSize = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            _metaDataOperator.AddActivity(_databaseName, _collectionName, DateTimeOffset.Now, ActivityStrength.Hot).Wait();
            lock (bulkInsertLock)
            {
                var scaleOperation = ScaleLogic.ScaleUpMaxCollectionAsync(_client, _metaDataOperator, _databaseName, _collectionName, _minRu, _maxRu).GetAwaiter().GetResult();
                _bulkExecutor.BulkImportAsync(documents, enableUpsert, disableAutomaticIdGeneration, maxConcurrencyPerPartitionKeyRange, maxInMemorySortingBatchSize, cancellationToken).Wait();

                return(new BulkInsertOpeartionResult
                {
                    ScaleOperations = new List <ScaleOperation>()
                    {
                        scaleOperation
                    },
                    OperationSuccess = true
                });
            }
        }
Exemplo n.º 5
0
        private static void OnTimedEvent(Object source, ElapsedEventArgs e)
        {
            foreach (var collection in _metaDataOperator.GetAllActiveCollections())
            {
                var latestActivityForCollection = _metaDataOperator.GetLatestActivity(collection.DatabaseName, collection.CollectionName);

                if (latestActivityForCollection != null)
                {
                    var databaseName  = collection.DatabaseName;
                    var collectioName = collection.CollectionName;
                    var minRu         = collection.MinimumRU;

                    var latestActivityDateForCollection     = latestActivityForCollection.ActivityTime;
                    var latestActivityStrengthForCollection = latestActivityForCollection.ActivityStrength;

                    DateTime dateToCompare = DateTime.MinValue;
                    switch (latestActivityStrengthForCollection)
                    {
                    case ActivityStrength.Hot:
                        dateToCompare = DateTime.Now.AddMinutes(-3);     //3min inactivity
                        break;

                    case ActivityStrength.Medium:
                        dateToCompare = DateTime.Now.AddMinutes(-1);     //1min inactivity
                        break;

                    case ActivityStrength.Cold:
                        dateToCompare = DateTime.Now.AddSeconds(-10);     //10sec inactivity
                        break;
                    }

                    if (dateToCompare > latestActivityDateForCollection)
                    {
                        //no activity for 5 minutes.. scale back down to minRu
                        ScaleLogic.ScaleDownCollectionAsync(_client, _metaDataOperator, databaseName, collectioName, minRu).Wait();
                    }
                }
            }
        }