1

当尝试同时更新多个 blob 时,我试图使用 Task.WaitAll 来提高性能

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.RetryPolicies;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse("UseDevelopmentStorage=true;DevelopmentStorageProxyUri=http://localhost.fiddler;");
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");

            // Create the container if it doesn't already exist.
            container.CreateIfNotExists();

            CloudBlockBlob blockBlob1 = container.GetBlockBlobReference("myblob1");
            CloudBlockBlob blockBlob2 = container.GetBlockBlobReference("myblob2");
            CloudBlockBlob blockBlob3 = container.GetBlockBlobReference("myblob3");
            CloudBlockBlob blockBlob4 = container.GetBlockBlobReference("myblob4");
            CloudBlockBlob blockBlob5 = container.GetBlockBlobReference("myblob5");

            blobClient.DefaultRequestOptions = new BlobRequestOptions()
            {
                RetryPolicy = new NoRetry()
            };
            var str = "To list the blobs in a container, first get a container reference. You can then use the container's ListBlobs method to retrieve the blobs and/or directories within it. To access the rich set of properties and methods for a returned IListBlobItem, you must cast it to a CloudBlockBlob, CloudPageBlob, or CloudBlobDirectory object. If the type is ";

            Task.WaitAll(UploadBlobAsync(blockBlob1, str), UploadBlobAsync(blockBlob2, str),  UploadBlobAsync(blockBlob3, str), UploadBlobAsync(blockBlob4, str), UploadBlobAsync(blockBlob5, str));            
        }


        private static async Task UploadBlobAsync(CloudBlockBlob blob, string content)
        {
            await blob.UploadTextAsync(content);
        }
    }
}

然后我发现模拟器有时会抛出 500 异常:

HTTP/1.1 500 Server encountered an internal error. Please try again after some time.
Content-Length: 253
Content-Type: application/xml
Server: Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0
x-ms-request-id: 53a5fe0c-496a-4925-b87d-48cb59a7df37
x-ms-version: 2016-05-31
Date: Fri, 26 May 2017 16:46:28 GMT

<?xml version="1.0" encoding="utf-8"?><Error><Code>InternalError</Code><Message>Server encountered an internal error. Please try again after some time.
RequestId:53a5fe0c-496a-4925-b87d-48cb59a7df37
Time:2017-05-26T16:46:28.9534026Z</Message></Error>

如果我启用入口逻辑,它会成功,但需要的时间比我想象的要长得多,我可以通过 fiddler 看到其中一个 blob 操作总是在第一次失败并在重试时成功

它不会每次都失败,但是 500 失败的几率很高

这是用模拟器 5.0 和客户端库 8.1.3 测试的。我没有使用 azure 云存储测试此代码。

4

1 回答 1

0

我还可以使用 azure storage emulator v5.0 在我身边通过 5 个 uploadblob 任务重现此问题。如果任务数少于 5 个,则无法重现。正如您所提到的,one of the blob operation always failed at the first time. 但我无法使用 Azure Cloud Storage 重现它,它适用于 Azure Cloud Storage。如果对存储模拟器有任何想法,我们可以向 Azure 团队提供反馈。

于 2017-05-29T06:08:08.380 回答