大きな質問!ここで理解すべき重要なことは、Storage Service自体が失敗した操作を再試行しないことです。各操作について、サービスは単にHTTPステータスコードを返します。 Onusはクライアント上に存在し、これらのステータスコードを推測し、必要に応じて再試行を行います。
.Netストレージクライアントライブラリの例を挙げると、エラーコードを傍受して再試行する組み込みの再試行メカニズムがあります。私が間違っていない場合、デフォルトの再試行のメカニズムはExponential Retry
です。
ただし、独自のリトライロジックを作成してコードに挿入することはできます。エラーが発生するたびに、リトライロジックが実行され、要求が再試行されます。
これまでのこのトピックに関するブログ記事を投稿しました。役立つ情報はhttp://gauravmantri.com/2012/12/30/storage-client-library-2-0-implementing-retry-policies/です。私はそれがかなり古い投稿であり、多くは変更されているが、それはあなた自身の再試行ポリシーを実装する方法についてのいくつかのアイデアを与えるはずです。
このブログ記事では、コンテナを削除して直ちに同じ名前で新しいコンテナを作成するときに再試行するサンプルコードを書きました。コンテナを削除するには時間がかかるため、通常は再試行できないサービスから409エラーコードが返されます。しかし、このカスタムリトライポリシーでは、あなたのコードは、 "x"回のブロブコンテナの作成を再試行し、あきらめます。
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(2), 10);
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(2), 10);
BlobRequestOptions requestOptions = new BlobRequestOptions()
{
RetryPolicy = containerBeingDeletedRetryPolicy,
};
blobContainer.Create(requestOptions);
Console.WriteLine("Blob container created.");
Console.ReadLine();
}
:
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;
}
}
、ここでは、この再試行ポリシーを使用するコードです