这两天在做一个Windows Azure blob存储备份的的一个小功能,但是每次使用CloudBlockBlob.UploadFromStream上传本地文件到Blob Storage,总是不成功报出一个“Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host."的异常来。在网上Google了一下,听有人说是上传的文件太大了,把FileStream分段上传就OK了,于是做了如此尝试,但是发现仍然报出了同样的错误。一时觉得无解了,MSDN上一个偶然的发现,问题突然间有了转机。

  “Transient Fault Handling”,也就是偶然发现的关键字,中文姑且翻译成“短暂性故障处理”。

  那什么是“Transient Faults”?

  MSDN大致的定义是基于云的应用使用基于云的相关服务,往往会因为网络的问题、或是间歇性的服务基础水平的错误等临时性条件导致一些错误,常常在一段时间之后又会恢复正常。比如说SQL Azure可能会因为过度的资源使用、长时间的工作、因为失效切换到备用的SQL Azure、或者是负载平衡的考虑、网络的不佳等原因而短暂的限制database的连接,甚至使中断已有的连接。对于这类临时性的错误,把它叫做“Transient Faults”。对于这种错误可以通过少量的重试来解决。

  对于这种Transient Faults,我们一般使用Retry Policy来缓和它(这里不能保证一定可以解决,只是在一定程度上减轻错误的出现频率,是你的程序更加健壮)。怎么样使用Retry Policy首先我们看一张图。

  从图中可以看出Retry Policy是由Detection strategy和Retry Strategy结合起来的,它通过调用ExecuteAction方法去使用你想要的云服务,ExecuteAction方法包裹你使用云服务调用的具体的方法,后边会有代码的演示。

  Detection strategy能够识别可能导致transient fault的Exceptions,主要针对以下几个服务。 

  • SQL Azure
  • Windows Azure Service Bus
  • Windows Azure Storage Service
  • Windows Azure Caching Service

  接下来说一下这个Retry strategy,按照种类,这里分成了三种,Example给出了每种Retry strategy使用的时间间隔,Retry的次数都是5次。

  

Retry strategy

Example (intervals between retries in seconds)

Fixed interval

2,2,2,2,2,2

Incremental intervals

2,4,6,8,10,12

Random exponential back-off intervals

2, 3.755, 9.176, 14.306, 31.895

  下边我以上传文件到Blob-Store为例,来具体讲述如何使用Retry Policy来缓解transient fault,来增强Windows Azure云应用的健壮性、稳定性。

  首先,加入Transient Fault Handling Application Block assemblies的引用。

  选中Solution右击工程节点,点弹出菜单中选中"Manage NuGet Packages",  在弹出窗口中选中“Online”,然后在“Search Online”中输入“topaz”,点击安装“Enterprise Library 5.0 - Transient Fault Handing Application Block” package. 这样我们就可以导入需要用到的命名空间了。

  我们可以在代码或是Application Configuration File里边定义retry policies,如果你只是一个小的程序,调用retry logic的次数不多,你可以直接在代码里边定义,反之在配置文件里边定义。我们的示例是直接在程序里定义。如何在Configuration 里边配置可以点击这里查看。

  太啰嗦了,直接上代码。

            // Define your retry strategy: retry 5 times, starting 1 second apart
// and adding 2 seconds to the interval each retry.
var retryStrategy = new Incremental(, TimeSpan.FromSeconds(), TimeSpan.FromSeconds()); // Define your retry policy using the retry strategy and the Windows Azure storage
// transient fault detection strategy.
var retryPolicy = new RetryPolicy<StorageTransientErrorDetectionStrategy>(retryStrategy); // Receive notifications about retries.
retryPolicy.Retrying += (sender, args) =>
{
// Log details of the retry.
var msg = String.Format("Retry - Count:{0}, Delay:{1}, Exception:{2}",
args.CurrentRetryCount, args.Delay, args.LastException); Trace.WriteLine(msg, "Information");
}; try
{
retryPolicy.ExecuteAction(
() =>
{
// Call a method that uses Windows Azure storage and which may
// throw a transient exception.
backupBlob.UploadFromStream(fileStream);
}
);
}
catch (Exception ex)
{
Trace.WriteLine(ex, "Information");
}

  这样当UploadFromStream报Transient Fault时,Retry Policy会每隔2S重新调用此方法,直到调用成功,或者超过所定义的5次尝试。我自己的blob-store备份本地文件,在未使用之前UploadFromStream尝试了8~9次都没有成功,使用了之后Retry了1次就成功了,不排除是因为上传的文件过大,或是网络的原因,总之它是的程序更加健壮了。

  最后,说一下那些情况可以用Transient Fault Handling。

  Detection strategy中提到的四个Windows Azure Services,如果你的应用使用了它们,你可以使用Transient Fault Handling。

  还有一种情况是使用你自定义的Service也可以使用Transient Fault Handling,具体的使用方法可以点击这里参考

  

  

  附上资料参考的来源:

  http://msdn.microsoft.com/en-us/library/hh680901(v=pandp.50).aspx

Windows Azure 应用程序短暂性故障处理的更多相关文章

  1. 在 Visual Studio 2010 中开发和部署 Windows Azure 应用程序

    原文 在 Visual Studio 2010 中开发和部署 Windows Azure 应用程序 在 Visual Studio 2010 中开发和部署 Windows Azure 应用程序 Jim ...

  2. 将 Java Spring Framework 应用程序迁移到 Windows Azure

    我们刚刚发布了一个新教程和示例代码,以阐述如何在Windows Azure中使用 Java 相关技术.在该指南中,我们提供了分步教程,说明如何将 Java Spring Framework 应用程序( ...

  3. Windows Azure 安全最佳实践 - 第 6 部分:Azure 服务如何扩展应用程序安全性

    多种Windows Azure服务可以帮助您将应用程序安全性扩展到云. 有三种服务可提供多个提供程序之间的身份标识映射.内部部署数据中心间的连接和相互发送消息的应用程序功能(无论应用程序位于何处). ...

  4. 在 Windows Azure 上设计多租户应用程序

    作者:Suren Machiraju 和 Ralph Squillace 审校:Christian Martinez.James Podgorski.Valery Mizonov 和 Michael ...

  5. Windows Azure公有云服务相关方案

    http://www.cnblogs.com/sennly/p/4139675.html 1.公有云平台服务简介 Windows Azure 是一个灵活而开放的云平台,通过该平台,您可以在数据中心快速 ...

  6. Windows Azure 安全最佳实践 - 第 7 部分:提示、工具和编码最佳实践

    在撰写这一系列文章的过程中,我总结出了很多最佳实践.在这篇文章中,我介绍了在保护您的WindowsAzure应用程序时需要考虑的更多事项. 下面是一些工具和编码提示与最佳实践: · 在操作系统上运行 ...

  7. Windows Azure系列公开课 - 第二课:为什么选择Windows Azure(上)

    Windows Azure是微软的云平台,可以提供广泛服务.您可以通过它搭建.部署并管理解决方案,用于实现您可以想象的几乎任何目标.换言之,WindowsAzure是拥有无限可能的世界.无论您是需要运 ...

  8. [转]Windows Azure安全概述

    本文转自:http://blogs.msdn.com/b/azchina/archive/2011/03/06/windows_5f00_azure_5f00_security_5f00_overvi ...

  9. Windows Azure Cloud Service (11) PaaS之Web Role, Worker Role(上)

    <Windows Azure Platform 系列文章目录> 本文是对Windows Azure Platform (六) Windows Azure应用程序运行环境内容的补充. 我们知 ...

随机推荐

  1. angular 中不要使用location.href

    location.href = '#/HKorderList?gid='+gid+'&gname='+encodeURIComponent(gname)+'&cPeriod='+$(' ...

  2. jQuery插件实现表格隔行换色且感应鼠标高亮行变色

    实现表格隔行换色,且感应鼠标行变色的方法有很多,在本文将为大家介绍的是使用jQuery插件来实现,具体如下 看代码: : css代码:  

  3. HDU2181 哈密顿绕行世界问题

    解题思路:哈密顿环游世界问题.一道简单的题目,用回溯. #include<cstdio> #include<cstring> #include<algorithm> ...

  4. CF 148D D. Bag of mice (概率DP||数学期望)

    The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests ...

  5. HTTP请求报头及其处理

    ps:详细说明http://www.cnblogs.com/kkgreen/archive/2011/04/11/2012829.html

  6. bzoj 4852 炸弹攻击

    bzoj 4852 炸弹攻击 二维平面上的最优解问题,模拟退火是一个较为优秀的近似算法. 此题确定圆心后,便可 \(O(m)\) 算出收益,且最优解附近显然也较优,是连续变化的,可以直接模拟退火. 小 ...

  7. LOJ2362. 「NOIP2016」蚯蚓【单调队列】

    LINK 思路 良心来说这题还挺思维的 我没看题解也不知道要这样维护 把每次斩断的点分别放进两个队列里面 因为要维护增长,所以可以让新进队的节点来一个负增长? 是不是就好了? 然后很容易发现因为在原始 ...

  8. BZOJ3444 最后的晚餐【细节题+组合数学】*

    BZOJ3444 最后的晚餐 Description [问题背景] 高三的学长们就要离开学校,各奔东西了.某班n人在举行最后的离别晚餐时,饭店老板觉得十分纠结.因为有m名学生偷偷找他,要求和自己暗恋的 ...

  9. Oracle中用exp/imp命令快速导入导出数据

    from: http://blog.csdn.net/wangchunyu11155/article/details/53635602 [用 exp 数 据 导 出]: 1 将数据库TEST完全导出, ...

  10. Mac OS安装php-redis扩展

    下载php-redis(用于php5.x的版本),地址:https://nodeload.github.com/nicolasff/phpredis/zip/master. 如果是php7.2,选择p ...