这两天在做一个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. 你必须了解的Session的本质

    http://netsecurity.51cto.com/art/201402/428721.htm Cookie和session由于实现手段不同,因此也各有优缺点和各自的应用场景: 1.   应用场 ...

  2. HAWQ中的行列转置

    行列转置是ETL或报表系统中的常见需求,HAWQ提供的内建函数和过程语言编程功能,使行列转置操作的实现变得更为简单. 一.行转列 1. 固定列数的行转列 原始数据如下: test=# select * ...

  3. Jenkins简单的使用

    1.每个版本开发提单子,写清楚发布那个项目.配置文件.所执行SQL语句等:QA开始部署测试环境 2.如下时发布项目 一.版本发布 登陆系统,选择对应的项目(以api-gateway为例,如果找不到对应 ...

  4. 将HTML的页脚固定在屏幕下方

    /********************************************************************* * 将HTML的页脚固定在屏幕下方 * 说明: * 处理的 ...

  5. autoburn eMMC hacking

    #!/bin/sh # autoburn eMMC hacking # 说明: # 看一下富林的自动烧录的执行脚本原理. # # -- 深圳 龙华樟坑村 曾剑锋 # 创建sd卡挂载目录 if [ ! ...

  6. libudev-dev : Depends: libudev0 (= 175-0ubuntu9) but 175-0ubuntu9.3 is to be installed 错误解决方案

    libudev-dev : Depends: libudev0 (= -0ubuntu9) but -0ubuntu9. is to be installed 错误解决方案 参考文章: 1. ubun ...

  7. 【Python】Python点滴(一)——pip工具使用

    首先我们来看一条命令:pip install uwsgi 这条命令按照操作名称,可以分为三个部分:pip.install和uwsgi.接下来,按照这样三个部分进行分析.   pip pip类似于Rea ...

  8. 【Git】Git的正确学习方式

    Git学习笔记 学习资料 git init git status git log 可以查看提交历史 git reset --hard commit_id git reflog查看命令历史 git ad ...

  9. BJOI 模拟赛 #3 题解

    T1 一个网格,每个点有权值,求有多少条路径权值乘积不小于 $n$ $R,C \leq 300, n \leq 10^6$ sol: 暴力 dp 是 $O(R \times C \times n)$ ...

  10. (function($){...})(jQuery)与$(function(){...})区别

    $(function(){...}) 这种写法和jQuery(function(){...}),$(document).ready(function(){...})作用一样,表示文档载入后需要运行的代 ...