tableclient.RetryPolicy Vs. TransientFaultHandling

时间:2013-09-23 22:03:45

标签: c#-4.0 asp.net-mvc-4 azure

我和同事都负责为Azure表存储找到连接重试逻辑。经过一番搜索,我发现这个非常酷的Enterprise Library套件包含Microsoft.Practices.TransientFaultHandling命名空间。

根据一些代码示例,我最终创建了Incremental重试策略,并使用retryPolicy的{​​{1}}回调处理程序包装了一个存储调用:< / p>

ExecuteAction

感觉很棒,我去展示我的同事,他沾沾自喜地注意到我们可以做同样的事情,而不必包括企业库,因为/// <inheritdoc /> public void SaveSetting(int userId, string bookId, string settingId, string itemId, JObject value) { // Define your retry strategy: retry 5 times, starting 1 second apart, adding 2 seconds to the interval each retry. var retryStrategy = new Incremental(5, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2)); var storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting(StorageConnectionStringName)); try { retryPolicy.ExecuteAction(() => { var tableClient = storageAccount.CreateCloudTableClient(); var table = tableClient.GetTableReference(SettingsTableName); table.CreateIfNotExists(); var entity = new Models.Azure.Setting { PartitionKey = GetPartitionKey(userId, bookId), RowKey = GetRowKey(settingId, itemId), UserId = userId, BookId = bookId.ToLowerInvariant(), SettingId = settingId.ToLowerInvariant(), ItemId = itemId.ToLowerInvariant(), Value = value.ToString(Formatting.None) }; table.Execute(TableOperation.InsertOrReplace(entity)); }); } catch (StorageException exception) { ExceptionHelpers.CheckForPropertyValueTooLargeMessage(exception); throw; } } } 对象已经有一个重试策略的setter。他的代码最终看起来像:

CloudTableClient

我的问题:

除了实施之外,这两种方法之间是否有任何重大差异?他们似乎都达到了同样的目标,但有些情况下使用一个比另一个更好吗?

1 个答案:

答案 0 :(得分:4)

从功能上讲两者都是相同的 - 它们都会在发生瞬时错误时重试请求。然而,差异很小:

  • 存储客户端库中的重试策略处理仅处理存储操作的重试,而临时故障处理重试不仅处理存储操作,还会在发生瞬时错误时重试SQL Azure,Service Bus和Cache操作。因此,如果您有一个项目,您正在使用更多的存储,但希望只有一种方法来处理瞬态错误,您可能希望使用瞬态故障处理应用程序块。
  • 我对瞬态故障处理块的一个看法是,您可以拦截重试操作,而不能使用重试策略。例如,请查看以下代码:

        var retryManager = EnterpriseLibraryContainer.Current.GetInstance<RetryManager>();
        var retryPolicy = retryManager.GetRetryPolicy<StorageTransientErrorDetectionStrategy>(ConfigurationHelper.ReadFromServiceConfigFile(Constants.DefaultRetryStrategyForTableStorageOperationsKey));
        retryPolicy.Retrying += (sender, args) =>
        {
            // Log details of the retry.
            var message = string.Format(CultureInfo.InvariantCulture, TableOperationRetryTraceFormat, "TableStorageHelper::CreateTableIfNotExist", storageAccount.Credentials.AccountName,
                tableName, args.CurrentRetryCount, args.Delay);
            TraceHelper.TraceError(message, args.LastException);
        };
        try
        {
            var isTableCreated = retryPolicy.ExecuteAction(() =>
            {
                var table = storageAccount.CreateCloudTableClient().GetTableReference(tableName);
                return table.CreateIfNotExists(requestOptions, operationContext);
            });
            return isTableCreated;
        }
        catch (Exception)
        {
            throw;
        }
    

在上面的代码示例中,我可以拦截重试操作并在那里做一些事情。存储客户端库无法实现这一点。

说完所有这些之后,通常建议使用存储客户端库重试策略来重试存储操作,因为它是程序包的一个组成部分,因此可以及时更新库的最新更改。