在使用Windows Azure Storage Service 的时候, 通常会遇到各种各样的问题。

例如网络连接不稳定,导致请求没有发出去。删除一个Blob Container 之后又立刻创建同名的Container。

这些情况都有可能使得你的代码抛出异常。这种异常我们通常叫他:Transient Fault 翻译成中文就是短暂性异常。

处理这种异常最简单有效的途径就是过几秒钟之后再重新发送请求。

Retry Policy就是用来帮助我们完成这样一项工作的。

Retry Policy中自带三个已经封装好了可以直接使用的类,分别有着不同的重试策略。

1.LinearRetry

这个类提供一个平行线式的重试策略

上图展示了LinearRetry的重试策略。它一共重试了五次,每一次的时间间隔都是5秒,第六次由于已经超过了预设定的重试次数,所以就放弃了。

使用LinearRetry的方法如下。

static string accountName = "<storage account name>";
static string accountKey = "<storage account key>";
static void Main(string[] args)
{
var storageAccount = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);
string blobContainerName = "temp-" + DateTime.UtcNow.Ticks;
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
IRetryPolicy exponentialRetryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(), );
blobClient.RetryPolicy = exponentialRetryPolicy;
CloudBlobContainer blobContainer = blobClient.GetContainerReference(blobContainerName);
blobContainer.Create();
Console.WriteLine("Blob container created.");
Console.ReadLine();
}

2.ExponentialRetry

这个类提供了一个递增式的重试策略

很明显这是一个指数级别的重试策略,每一次重试的时间间隔都是之前的2倍。

使用ExponentialRetry方法如下:

static string accountName = "<storage account name>";
static string accountKey = "<storage account key>";
static void Main(string[] args)
{
var storageAccount = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);
string blobContainerName = "temp-" + DateTime.UtcNow.Ticks;
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
IRetryPolicy linearRetryPolicy = new LinearRetry(TimeSpan.FromSeconds(), );
blobClient.RetryPolicy = linearRetryPolicy;
CloudBlobContainer blobContainer = blobClient.GetContainerReference(blobContainerName);
IRetryPolicy noRetryPolicy = new NoRetry();
BlobRequestOptions requestOptions = new BlobRequestOptions()
{
RetryPolicy = noRetryPolicy,
};
blobContainer.Create(requestOptions);
Console.WriteLine("Blob container created.");
Console.ReadLine();
}

3.NoRetry

这个类的重试策略就是不重试,这个策略看似矛盾,其实它是用来配合之前两种策略来使用的,当你希望一个Blob Container的所有操作都能应用重试策略,唯独Create Blob不使用重试策略的时候你就需要用到NoRetry了。 代码如下:

static string accountName = "<storage account name>";
static string accountKey = "<storage account key>";
static void Main(string[] args)
{
var storageAccount = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);
string blobContainerName = "temp-" + DateTime.UtcNow.Ticks;
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
IRetryPolicy linearRetryPolicy = new LinearRetry(TimeSpan.FromSeconds(), );
blobClient.RetryPolicy = linearRetryPolicy;
CloudBlobContainer blobContainer = blobClient.GetContainerReference(blobContainerName);
IRetryPolicy noRetryPolicy = new NoRetry();
BlobRequestOptions requestOptions = new BlobRequestOptions()
{
RetryPolicy = noRetryPolicy,
};
blobContainer.Create(requestOptions);
Console.WriteLine("Blob container created.");
Console.ReadLine();
}

除了以上三个已经封装好的类以外,我们还可以自己创建自定义的重试策略类, 我们只需要实现IRetryPolicy接口即可。

实现自定义类的一个好处是可以自定义控制在何种特定情况下才进行重试。

下面代码将自定义一个实现了IRetryPolicy接口的 自定义类。

public class ContainerBeingDeletedRetryPolicy : IRetryPolicy
{
int maxRetryAttemps = 10; TimeSpan defaultRetryInterval = TimeSpan.FromSeconds(5); public ContainerBeingDeletedRetryPolicy(TimeSpan deltaBackoff, int retryAttempts)
{
maxRetryAttemps = retryAttempts;
defaultRetryInterval = deltaBackoff;
} public IRetryPolicy CreateInstance()
{
return new ContainerBeingDeletedRetryPolicy(TimeSpan.FromSeconds(2), 5);
} public bool ShouldRetry(int currentRetryCount, int statusCode, Exception lastException, out TimeSpan retryInterval, OperationContext operationContext)
{
retryInterval = defaultRetryInterval;
if (currentRetryCount >= maxRetryAttemps)
{
return false;
}
//Since we're only interested in 409 status code, let's not retry any other operation.
if ((HttpStatusCode)statusCode != HttpStatusCode.Conflict)
{
return false;
}
//We're only interested in storage exceptions so if there's any other exception, let's not retry it.
if (lastException.GetType() != typeof(StorageException))
{
return false;
}
else
{
var storageException = (StorageException)lastException;
string errorCode = storageException.RequestInformation.ExtendedErrorInformation.ErrorCode;
if (errorCode.Equals("ContainerBeingDeleted"))
{
return true;
}
else
{
return false;
}
}
return true;
}
}

 使用代码如下:

static string accountName = "<storage account name>";
static string accountKey = "<storage account key>";
static void Main(string[] args)
{
var storageAccount = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);
string blobContainerName = "temp-" + DateTime.UtcNow.Ticks;
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
IRetryPolicy linearRetryPolicy = new LinearRetry(TimeSpan.FromSeconds(), );
blobClient.RetryPolicy = linearRetryPolicy;
CloudBlobContainer blobContainer = blobClient.GetContainerReference(blobContainerName);
blobContainer.Create();
Console.WriteLine("Blob container created.");
blobContainer.Delete();
Console.WriteLine("Blob container deleted.");
IRetryPolicy containerBeingDeletedRetryPolicy = new ContainerBeingDeletedRetryPolicy(TimeSpan.FromSeconds(), );
BlobRequestOptions requestOptions = new BlobRequestOptions()
{
RetryPolicy = containerBeingDeletedRetryPolicy,
};
blobContainer.Create(requestOptions);
Console.WriteLine("Blob container created.");
Console.ReadLine();
}

这个类只用于处理返回409错误并且错误信息 "Blob container created." 的错误,其他错误则忽略掉了。

总结:

像上面这种,在一个方法中删除一个container并且重建同名container的情况很容易就产生 Transient Fault。

这时使用RetryPolicy就能够很好的解决这种异常了!

Windows Azure Storage 之 Retry Policy (用来处理短暂性错误-Transient Fault)的更多相关文章

  1. [New Portal]Windows Azure Storage (14) 使用Azure Blob的PutBlock方法,实现文件的分块、离线上传

    <Windows Azure Platform 系列文章目录> 相关内容 Windows Azure Platform (二十二) Windows Azure Storage Servic ...

  2. [SDK2.2]Windows Azure Storage (15) 使用WCF服务,将本地图片上传至Azure Storage (上) 服务器端代码

    <Windows Azure Platform 系列文章目录> 这几天工作上的内容,把项目文件和源代码拿出来给大家分享下. 源代码下载:Part1 Part2 Part3 我们在写WEB服 ...

  3. Windows Azure Storage (18) 使用HTML5 Portal的Azure CDN服务

    <Windows Azure Platform 系列文章目录> Update:2015-04-15 如果读者使用的是国内由世纪互联运维的Azure China服务,请参考笔者的文档:Azu ...

  4. Windows Azure Storage (19) 再谈Azure Block Blob和Page Blob

    <Windows Azure Platform 系列文章目录> 请读者在参考本文之前,预习相关背景知识:Windows Azure Storage (1) Windows Azure St ...

  5. Windows Azure Storage (21) 使用AzCopy工具,加快Azure Storage传输速度

    <Windows Azure Platform 系列文章目录> Update 2016-09-28 想要在Azure云端,使用AzCopy工具,从Azure China 上海数据中心存储账 ...

  6. Windows Azure Storage (22) Azure Storage如何支持多级目录

    <Windows Azure Platform 系列文章目录> 熟悉Azure平台的读者都知道,Azure Blob有三层架构.如下图:(注意blob.core.chinacloudapi ...

  7. Windows Azure Storage图形界面管理工具

    上一篇我们介绍了用PowerShell将Windows Azure的存储服务当网盘来使用.如果感觉还不够简单,那么这次我们来看看还有哪些使用起来更方便的图形界面管理工具吧.当然,这些工具必要支持中国版 ...

  8. [转]探索 Windows Azure Storage

    本文转自:https://msdn.microsoft.com/zh-tw/jj573842 概觀 儲存服務 (Storage services) 在 Windows Azure 運算模擬器中提供了可 ...

  9. Windows Azure Storage (6) Windows Azure Storage之Table

    <Windows Azure Platform 系列文章目录> 最近想了想,还是有必要把Windows Azure Table Storage 给说清楚. 1.概念 Windows Azu ...

随机推荐

  1. python3 抓取网页资源的 N 种方法

    1. 最简单 import urllib.request response = urllib.request.urlopen('http://python.org/') html = response ...

  2. UILabel笔记(待完善)

    UIlabel的换行由 numberOfLines 属性控制,当为0时,则会自动换到适合的行数: 换行的模式由 lineBreakMode 属性控制: public enum NSLineBreakM ...

  3. Andriod如何更改应用程序小图标

    1.之前我们安装的第一个应用图标是这样的(如下图) 2.在eclipse左侧项目中找到res文件下的drawable-hdpi         3.把自己找的LOGO图标拖到文件中,之后会弹出一个消息 ...

  4. 基于fab自动化部署

    fab是一个python库,强大好使,可以做很多帮助你减轻工作量的事情,比如在多台服务器上部署web项目,这里就讲讲使用它简单的方法来执行部署的过程. 关于fab的安装的基本使用,网上一搜一大把,内容 ...

  5. quicksort

    快排.... void quicksort(int *a,int left,int right){ if(left >= right){ return ; } int i = left; int ...

  6. Clustering Methods: Benefits and Limitations

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION

  7. 用mac的terminal通过公私钥和ssh登录Linux

    刚开始使用mac,会觉得很难用,在网上找的方法也差强人意,经过自己的实践,找到下面这种方法,很好用,步骤也很简单 1.在mac本的个人目录下创建一个文件夹:.ssh.    在这个文件夹下使用ssh- ...

  8. Python 面向对象(初级篇)

    51CTO同步发布地址:http://3060674.blog.51cto.com/3050674/1689163 概述 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后 ...

  9. 基于 debootstrap 和 busybox 构建 mini ubuntu

    基于 debootstrap 和 busybox 构建 mini ubuntu 最近的工作涉及到服务器自动安装和网络部署操作系统,然后使用 ansible 和 saltsatck 进行配置并安装 op ...

  10. 如何设置jvm内存

    本文向大家简单介绍一下进行JVM内存设置几种方法,安装Java开发软件时,默认安装包含两个文件夹,一个JDK(Java开发工具箱),一个JRE(Java运行环境,内含JVM),其中JDK内另含一个JR ...