简体   繁体   English

在Azure表上读取重试

[英]Read retry on Azure Table

I got error No connection could be made because the target machine actively refused it often, when read from Azure Table Storage, sometime on 80(http) sometime on 443(https) 我收到错误消息No connection could be made because the target machine actively refused it经常No connection could be made because the target machine actively refused it ,当从Azure Table Storage读取时,有时在80(http)上有时在443(https)上

I know I can setup retry for write by .SaveChangesWithRetries() , but how to apply retry on read? 我知道我可以通过.SaveChangesWithRetries()设置写入重试,但是如何在读取时应用重试?

btw, I read by code 顺便说一句,我通过代码阅读

DataServiceQuery<datatype> query = tableContext.CreateQuery<datatype>("table");
IQueryable<datatype> results= from q in query select q

Last I had to use the semi-official Transient Fault Handling Framework , but this was over a year ago; 最后,我不得不使用半官方的“ 瞬时故障处理框架” ,但这是一年多以前的事了。 and the best practices guidelines may have changed. 并且最佳做法指南可能已更改。

EDIT 编辑

As pointed out in the comments, this solution has evolved into a The Transient Fault Handling Application Block , which is now available as a NuGet package. 正如评论中所指出的那样,该解决方案已经发展成一个“瞬时故障处理应用程序块” ,现在可以作为NuGet软件包使用。

For the uninitiated, the framework basically specifies retry (hammer) policy for a class of transport-type errors that occur naturally in any distributed environment and particularly in Azure. 对于初学者而言,该框架基本上为在任何分布式环境(尤其是Azure)中自然发生的一类传输类型错误指定重试(锤)策略。 For example SQL azure may return an error code "Server too busy, try again in a moment" or "Your data got lost on the way over here, oops", and you don't need to know all those codes in order to basically say "Retry". 例如,SQL azure可能返回错误代码“服务器太忙,请稍后重试”或“您的数据在此途中丢失了,哎呀”,而您基本上不需要知道所有这些代码即可说“重试”。

Use Polly: https://github.com/App-vNext/Polly to implement a retry handler which intercepts the error types / codes / etc you wish to handle and retry them. 使用Polly: https : //github.com/App-vNext/Polly来实现重试处理程序,该处理程序将拦截您希望处理的错误类型/代码/等并重试它们。 Polly is recommended by MS themselves for certain services (see: https://docs.microsoft.com/en-us/azure/architecture/best-practices/retry-service-specific and https://docs.microsoft.com/en-us/dotnet/standard/microservices-architecture/implement-resilient-applications/implement-http-call-retries-exponential-backoff-polly ) .. and i've found its fuild syntax to be a joy to use. MS本身建议对某些服务使用Polly(请参阅: https//docs.microsoft.com/zh-cn/azure/architecture/best-practices/retry-service-specifichttps://docs.microsoft.com/ zh-cn / dotnet / standard / microservices-architecture / implement-resilient-applications / implement-http-call-retries-exponential-backoff-polly )..我发现它的语法很有趣。 Here's an example of a Polly retry handler i created to handle StorageException from Azure Storage. 这是我创建的Polly重试处理程序的示例,用于处理来自Azure存储的StorageException。 It implements an Exponential backoff ( https://github.com/App-vNext/Polly/wiki/Retry#exponential-backoff ) strategy. 它实现了指数退避( https://github.com/App-vNext/Polly/wiki/Retry#exponential-backoff )策略。

int retries = 0;
int maxRetryAttempts = 5;

var retryStorageExceptionPolicy = Policy
    .Handle<StorageException>()
    .WaitAndRetry(maxRetryAttempts, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
        onRetry: (exception, calculatedWaitDuration) =>
        {
            retries++;
            loggingService.LogWarning(exception,
                new Dictionary<string, string>
                {
                    {"Message", $"A StorageException occurred when trying to Execute Query With Retry. This exception has been caught and will be retried." },
                    {"Current Retry Count", retries.ToString() },
                    {"tableName", table.Name},
                    {"ExtendedErrorInformation.ErrorCode", (exception as StorageException)?.RequestInformation.ExtendedErrorInformation.ErrorCode },
                    {"ExtendedErrorInformation.ErrorMessage", (exception as StorageException)?.RequestInformation.ExtendedErrorInformation.ErrorMessage }
                });
        });

retryStorageExceptionPolicy
    .ExecuteAndCapture(() =>
    {
        // your method accessing Azure Storage here                
    });

If you want a method to build a list of Transient error codes for Http calls let me know. 如果您想要一种为Http调用构建瞬态错误代码列表的方法,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM