gpt4 book ai didi

c# - 并发访问时出现 Azure CloudAppendBlob 错误

转载 作者:太空狗 更新时间:2023-10-29 17:45:58 26 4
gpt4 key购买 nike

我的理解是,Azure CloudAppendBlob 不会出现并发问题,因为您只能附加到此 Blob 存储,并且不需要比较电子标签。正如这篇文章所述:

http://blogs.msdn.com/b/windowsazurestorage/archive/2015/04/13/introducing-azure-storage-append-blob.aspx

具体来说:

In addition, Append Blob supports having multiple clients writing to the same blob without any need for synchronization (unlike block and page blob)

但是以下单元测试会引发:

412 the append position condition specified was not met.

堆栈跟踪

Microsoft.WindowsAzure.Storage.Blob.BlobWriteStream.Flush()
Microsoft.WindowsAzure.Storage.Blob.BlobWriteStream.Commit()
Microsoft.WindowsAzure.Storage.Blob.CloudAppendBlob.UploadFromStreamHelper
Microsoft.WindowsAzure.Storage.Blob.CloudAppendBlob.AppendFromStream
Microsoft.WindowsAzure.Storage.Blob.CloudAppendBlob.AppendFromByteArray
Microsoft.WindowsAzure.Storage.Blob.CloudAppendBlob.AppendText

这是单元测试。也许该服务将处理来自不同上下文的请求,但不会像这样并行处理?

    [TestMethod]
public void test_append_text_concurrency()
{
AppendBlobStorage abs = new AppendBlobStorage(new TestConnectConfig(), "testappendblob");

string filename = "test-concurrent-blob";

abs.Delete(filename);

Parallel.Invoke(
() => { abs.AppendText(filename, "message1\r\n"); },
() => { abs.AppendText(filename, "message2\r\n"); }
);

string text = abs.ReadText(filename);

Assert.IsTrue(text.Contains("message1"));
Assert.IsTrue(text.Contains("message2"));
}

AppendBlobStorage 中的方法

    public void AppendText(string filename, string text)
{
CloudAppendBlob cab = m_BlobStorage.BlobContainer.GetAppendBlobReference(filename);

// Create if it doesn't exist
if (!cab.Exists())
{
try
{
cab.CreateOrReplace(AccessCondition.GenerateIfNotExistsCondition(), null, null);
}
catch { }
}

// Append the text
cab.AppendText(text);
}

也许我错过了一些东西。我尝试这样做的原因是因为我有多个网络作业,它们都可以写入此附加 blob,并且我认为这就是它的设计目的?

最佳答案

经过更多搜索后,看来这是一个实际问题。

我猜 AppendBlobStorage 是相当新的。 (目前 AppendBlobStorage 还存在其他问题。请参阅

http://blogs.msdn.com/b/windowsazurestorage/archive/2015/09/02/issue-in-azure-storage-client-library-5-0-0-and-5-0-1-preview-in-appendblob-functionality.aspx )

无论如何,我通过使用 AppendBlock 变体而不是这里建议的 AppendText 解决了这个问题:

https://azurekan.wordpress.com/2015/09/08/issues-with-adding-text-to-azure-storage-append-blob/

对appendtext方法的更改通过了上面定义的单元测试

    public void AppendText(string filename, string text)
{
if (string.IsNullOrWhiteSpace(filename))
throw new ArgumentException("filename cannot be null or empty");

if (!string.IsNullOrEmpty(text))
{
CloudAppendBlob cab = m_BlobStorage.BlobContainer.GetAppendBlobReference(filename);

// Create if it doesn't exist
if (!cab.Exists())
{
try
{
cab.CreateOrReplace(AccessCondition.GenerateIfNotExistsCondition(), null, null);
}
catch (StorageException) { }
}

// use append block as append text seems to have an error at the moment.
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(text)))
{
cab.AppendBlock(ms);
}
}

}

关于c# - 并发访问时出现 Azure CloudAppendBlob 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32530126/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com