コード例 #1
0
        private void HandleFailedRequest(
            HttpStatusCode statusCode,
            int subStatusCode,
            string lastContinuation)
        {
            DocDbError docDbError = ExceptionClassifier.ClassifyStatusCodes(statusCode, subStatusCode);

            switch (docDbError)
            {
            case DocDbError.PartitionSplit:
                throw new FeedRangeGoneException("Partition split.", lastContinuation);

            case DocDbError.PartitionNotFound:
                throw new FeedNotFoundException("Partition not found.", lastContinuation);

            case DocDbError.ReadSessionNotAvailable:
                throw new FeedReadSessionNotAvailableException("Read session not availalbe.", lastContinuation);

            case DocDbError.Undefined:
                throw new InvalidOperationException($"Undefined DocDbError for status code {statusCode} and substatus code {subStatusCode}");

            default:
                DefaultTrace.TraceCritical($"Unrecognized DocDbError enum value {docDbError}");
                Debug.Fail($"Unrecognized DocDbError enum value {docDbError}");
                throw new InvalidOperationException($"Unrecognized DocDbError enum value {docDbError} for status code {statusCode} and substatus code {subStatusCode}");
            }
        }
コード例 #2
0
        private bool IncludeThisOne(string exception)
        {
            if (Filter == ExceptionClassification.NotClassified)
            {
                return(true);
            }

            var classification = ExceptionClassifier.Classify(exception);

            return(classification == Filter);
        }
コード例 #3
0
        public override async Task RunAsync(CancellationToken cancellationToken)
        {
            while (!cancellationToken.IsCancellationRequested)
            {
                TimeSpan delay = this.monitoringDelay;

                try
                {
                    long estimation = await this.remainingWorkEstimator.GetEstimatedRemainingWorkAsync(cancellationToken).ConfigureAwait(false);

                    await this.dispatcher.DispatchEstimation(estimation, cancellationToken);
                }
                catch (DocumentClientException clientException)
                {
                    this.logger.WarnException("exception within estimator", clientException);
                    DocDbError docDbError = ExceptionClassifier.ClassifyClientException(clientException);
                    switch (docDbError)
                    {
                    case DocDbError.Undefined:
                        throw;

                    case DocDbError.PartitionNotFound:
                    case DocDbError.PartitionSplit:
                    case DocDbError.TransientError:
                        // Retry on transient (429) errors
                        break;

                    default:
                        this.logger.Fatal($"Unrecognized DocDbError enum value {docDbError}");
                        Debug.Fail($"Unrecognized DocDbError enum value {docDbError}");
                        throw;
                    }

                    if (clientException.RetryAfter != TimeSpan.Zero)
                    {
                        delay = clientException.RetryAfter;
                    }
                }
                catch (TaskCanceledException canceledException)
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        throw;
                    }

                    this.logger.WarnException("exception within estimator", canceledException);

                    // ignore as it is caused by DocumentDB client
                }

                await Task.Delay(delay, cancellationToken).ConfigureAwait(false);
            }
        }
コード例 #4
0
 public Polly.Policy[] CreatePolicies()
 {
     return(new Polly.Policy[]
     {
         Polly.Policy.Handle <DocumentClientException>(ex => ExceptionClassifier.IsTimeout(ex))
         .WaitAndRetry(5, (retry) => TimeSpan.FromSeconds(1 * retry)),
         Polly.Policy.Handle <DocumentClientException>(ex => ExceptionClassifier.IsTooManyRequests(ex))
         .WaitAndRetry(new [] { TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2) }),
         Polly.Policy.Handle <DocumentClientException>(ex => ExceptionClassifier.IsServiceUnavaiable(ex))
         .WaitAndRetryForever(_ => TimeSpan.FromSeconds(2)),
         Polly.Policy.Handle <DocumentClientException>(ex => ExceptionClassifier.IsInternalServerError(ex))
         .Retry(1),
         Polly.Policy.Handle <StorageException>()
         .Retry(1)
     });
 }
コード例 #5
0
        private void HandleFailedRequest(
            ResponseMessage responseMessage,
            string lastContinuation)
        {
            DocDbError docDbError = ExceptionClassifier.ClassifyStatusCodes(responseMessage.StatusCode, (int)responseMessage.Headers.SubStatusCode);

            switch (docDbError)
            {
            case DocDbError.PartitionSplit:
                throw new FeedRangeGoneException("Partition split.", lastContinuation);

            case DocDbError.Undefined:
                throw CosmosExceptionFactory.Create(responseMessage);

            default:
                DefaultTrace.TraceCritical($"Unrecognized DocDbError enum value {docDbError}");
                Debug.Fail($"Unrecognized DocDbError enum value {docDbError}");
                throw new InvalidOperationException($"Unrecognized DocDbError enum value {docDbError} for status code {responseMessage.StatusCode} and substatus code {responseMessage.Headers.SubStatusCode}");
            }
        }
コード例 #6
0
        public MessageProcessorSpec()
        {
            _repository        = Substitute.For <IRepository>();
            _repositoryFactory = Substitute.For <IRepositoryFactory>();
            _repositoryFactory.Create().Returns(_repository);
            _poisonMessageRepository = Substitute.For <IPoisonMessageRepository>();
            var policyRegistry = Substitute.For <IPolicyRegistry>();

            policyRegistry.CreateAsyncPolicies().Returns(new[]
            {
                Policy.Handle <DocumentClientException>(ex => ExceptionClassifier.IsInternalServerError(ex))
                .RetryAsync(2),
                Policy.Handle <DocumentClientException>(ex => ExceptionClassifier.IsServiceUnavaiable(ex))
                .RetryAsync(2),
            });

            _container = new ContainerBuilder()
                         .AddTranscient <IMessageProcessor, MessageProcessor>()
                         .AddInstance(Substitute.For <ILogger>())
                         .AddInstance(_repositoryFactory)
                         .AddInstance(_poisonMessageRepository)
                         .AddInstance(policyRegistry)
                         .Build();
        }
コード例 #7
0
 public void Commit()
 {
     try
     {
         Context.SaveChanges();
     }
     catch (Exception ex) //DbUpdateConcurrencyException ex)
     {
         var category = ExceptionClassifier.Classify(ex.InnerException.InnerException);
         if (category == ExceptionType.ForeignKey)
         {
             throw new Exception(ex.InnerException.InnerException.Message);
         }
         else
         {
             throw;
         }
         //throw;
         //if (ex is DbUpdateConcurrencyException )
         //{
         //    var dbCCExp = ex as DbUpdateConcurrencyException ;
         //    dbCCExp.Entries.Single().Reload();
         //    Context.SaveChanges();
         //}
         //if (ex.InnerException != null)
         //{
         //    var category = ExceptionClassifier.Classify(ex.InnerException.InnerException);
         //    switch (category)
         //    {
         //        case ExceptionType.ForeignKey:
         //            throw;
         //            break;
         //    }
         //}
     }
 }
        public async Task RunAsync(CancellationToken cancellationToken)
        {
            string lastContinuation = this.settings.StartContinuation;

            while (!cancellationToken.IsCancellationRequested)
            {
                TimeSpan delay = this.settings.FeedPollDelay;

                try
                {
                    do
                    {
                        IFeedResponse <Document> response = await this.query.ExecuteNextAsync <Document>(cancellationToken).ConfigureAwait(false);

                        lastContinuation = response.ResponseContinuation;
                        if (response.Count > 0)
                        {
                            await this.DispatchChanges(response, cancellationToken).ConfigureAwait(false);
                        }
                    }while (this.query.HasMoreResults && !cancellationToken.IsCancellationRequested);

                    if (this.options.MaxItemCount != this.settings.MaxItemCount)
                    {
                        this.options.MaxItemCount = this.settings.MaxItemCount;   // Reset after successful execution.
                    }
                }
                catch (DocumentClientException clientException)
                {
                    this.logger.WarnException("exception: partition '{0}'", clientException, this.settings.PartitionKeyRangeId);
                    DocDbError docDbError = ExceptionClassifier.ClassifyClientException(clientException);
                    switch (docDbError)
                    {
                    case DocDbError.PartitionNotFound:
                        throw new PartitionNotFoundException("Partition not found.", lastContinuation);

                    case DocDbError.PartitionSplit:
                        throw new PartitionSplitException("Partition split.", lastContinuation);

                    case DocDbError.Undefined:
                        throw;

                    case DocDbError.TransientError:
                        // Retry on transient (429) errors
                        break;

                    case DocDbError.MaxItemCountTooLarge:
                        if (!this.options.MaxItemCount.HasValue)
                        {
                            this.options.MaxItemCount = DefaultMaxItemCount;
                        }
                        else if (this.options.MaxItemCount <= 1)
                        {
                            this.logger.ErrorFormat("Cannot reduce maxItemCount further as it's already at {0}.", this.options.MaxItemCount);
                            throw;
                        }

                        this.options.MaxItemCount /= 2;
                        this.logger.WarnFormat("Reducing maxItemCount, new value: {0}.", this.options.MaxItemCount);
                        break;

                    default:
                        this.logger.Fatal($"Unrecognized DocDbError enum value {docDbError}");
                        Debug.Fail($"Unrecognized DocDbError enum value {docDbError}");
                        throw;
                    }

                    if (clientException.RetryAfter != TimeSpan.Zero)
                    {
                        delay = clientException.RetryAfter;
                    }
                }
                catch (TaskCanceledException canceledException)
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        throw;
                    }

                    this.logger.WarnException("exception: partition '{0}'", canceledException, this.settings.PartitionKeyRangeId);

                    // ignore as it is caused by DocumentDB client
                }

                await Task.Delay(delay, cancellationToken).ConfigureAwait(false);
            }
        }
コード例 #9
0
        public async Task RunAsync(CancellationToken cancellationToken)
        {
            string requestContinuation = this.settings.RequestContinuation;

            while (!cancellationToken.IsCancellationRequested)
            {
                TimeSpan delay = this.settings.FeedPollDelay;

                try
                {
                    requestContinuation = await this.ProcessBatch(cancellationToken).ConfigureAwait(false);

                    if (this.options.MaxItemCount != this.settings.MaxItemCount)
                    {
                        this.options.MaxItemCount = this.settings.MaxItemCount;   // Reset after successful execution.
                    }
                }
                catch (DocumentClientException clientException)
                {
                    this.logger.WarnException("exception: partition '{0}'", clientException, this.settings.PartitionKeyRangeId);
                    DocDbError docDbError = ExceptionClassifier.ClassifyClientException(clientException);
                    switch (docDbError)
                    {
                    case DocDbError.PartitionNotFound:
                        throw new PartitionNotFoundException(requestContinuation);

                    case DocDbError.PartitionSplit:
                        throw new PartitionSplitException(requestContinuation);

                    case DocDbError.Undefined:
                        throw;

                    case DocDbError.MaxItemCountTooLarge:
                        if (!this.options.MaxItemCount.HasValue)
                        {
                            this.options.MaxItemCount = DefaultMaxItemCount;
                        }
                        else if (this.options.MaxItemCount <= 1)
                        {
                            this.logger.ErrorFormat("Cannot reduce maxItemCount further as it's already at {0}.", this.options.MaxItemCount);
                            throw;
                        }

                        this.options.MaxItemCount /= 2;
                        this.logger.WarnFormat("Reducing maxItemCount, new value: {0}.", this.options.MaxItemCount);
                        break;
                    }

                    if (clientException.RetryAfter != TimeSpan.Zero)
                    {
                        delay = clientException.RetryAfter;
                    }
                }
                catch (TaskCanceledException canceledException)
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        throw;
                    }

                    this.logger.WarnException("exception: partition '{0}'", canceledException, this.settings.PartitionKeyRangeId);

                    // ignore as it is caused by DocumentDB client
                }

                await Task.Delay(delay, cancellationToken).ConfigureAwait(false);
            }
        }