5

y query is something like

this.ProcessRequestSync(() => this.Client.CreateDocumentQuery<Model>(this.DocumentDBCollectionLink).Where(d => d.name.Equals(name) && d.code.Equals(code) && d.Type.Equals(this.documentType) && d.CreatedBy.Equals(myName).ToList<Model>());

 public dynamic ProcessRequestSync(Func<dynamic> getRequest)
{
    var delay = TimeSpan.Zero;
    var minDelayTime = new TimeSpan(0, 0, 1);
    for (;;)
    {
        try
        {
            Thread.Sleep(delay);
            return getRequest();
        }
        catch (DocumentClientException documentClientException)
        {
            var statusCode = (int)documentClientException.StatusCode;
            if (statusCode == 429 || statusCode == 503)
            {
                string errorMessage = string.Format("failed at DocumentDB with {0} status and {1} retry time", statusCode, documentClientException.RetryAfter);
                this.Logger.Log(errorMessage );

                // Back off if the request rate is too large or the service is temporarily unavailable
                delay = TimeSpan.Compare(documentClientException.RetryAfter, minDelayTime) >= 0 ? documentClientException.RetryAfter: minDelayTime;
            }
            else
            {
                throw;
            }
        }
    }
}

This is the method for retry logic when requestRateTooLarge exception raise.

I am not sure, whether it is working fine or not,

I am getting Exception: Microsoft.Azure.Documents.RequestRateTooLargeException while querying and inserting around 4000 records at a time,

I used the same retry logic for inserting, its working fine. I am not getting any error and also successfully inserted all records but unable to get query data.

4

2 回答 2

5

基于上面/下面的@aravind Ramachandra 和@Ryan CrawCour 的回答,这就是我用来解决这个问题的方法。

    public async Task SaveToDocDb(dynamic jsonDocToSave)
    {

        using (var client = new DocumentClient(endpoint, authKey))
        {
            var queryDone = false;
            while (!queryDone)
            {
                try
                {
                    await client.CreateDocumentAsync(docCollectionlink, jsonDocToSave);
                    queryDone = true; 
                }
                catch (DocumentClientException documentClientException)
                {
                    var statusCode = (int)documentClientException.StatusCode;
                    if (statusCode == 429 || statusCode == 503)   
                        Thread.Sleep(documentClientException.RetryAfter);
                    else
                        throw;
                }
                catch (AggregateException aggregateException)
                {
                    if(aggregateException.InnerException.GetType() == typeof(DocumentClientException)){

                        var docExcep = aggregateException.InnerException as DocumentClientException;
                        var statusCode = (int)docExcep.StatusCode;
                        if (statusCode == 429 || statusCode == 503)
                            Thread.Sleep(docExcep.RetryAfter);
                        else
                            throw;
                    }
                     else 
                       throw;
                }
            }
        }
    }
于 2015-04-11T14:00:18.810 回答
5

您还需要一个用于 AggregateException 的 catch 块,并检查 AggregateException.InnerException 是否为 DocumentClientException 并对 StatusCode == 429 执行相同的检查。由于查询执行是异步的,因此您可能会在 AggregateException 中获取限制异常。

如果您可以发布完整的复制品,我们可能能够明确地确定问题。

于 2015-03-26T15:19:25.820 回答