【发布时间】:2016-11-08 22:51:56
【问题描述】:
有没有办法在 Azure 存储中为 Page Blob 原子地写入数据和元数据?
考虑一个有多个写入者的页面 blob。
我看到了将元数据用于记录计数、序列号、blob 数据的一般结构等方面的建议。但是,如果两个写入者写入数据然后必须更新元数据,是否存在每个写入并尝试通过读取当前计数然后更新来更新记录计数的竞争。读0写1,其实有2个。
这同样适用于元数据写入不是由特定于该写入的内容(例如,每个写入然后将 new 名称-值对写入元数据)。
以下建议似乎对我不起作用。
// 512 byte aligned stream with my data
Stream toWrite = PageAlignedStreamManager.Write(data);
long whereToWrite = this.MetaData.TotalWrittenSizeInBytes;
this.MetaData.TotalWrittenSizeInBytes += toWrite.Length;
await this.Blob.FetchAttributesAsync();
if (this.MetaData.TotalWrittenSizeInBytes > this.Blob.Properties.Length)
{
await this.Blob.ResizeAsync(PageAlignedMemoryStreamManager.PagesRequired(this.MetaData.TotalWrittenSizeInBytes) * PageAlignedMemoryStreamManager.PageSizeBytes * 2);
}
this.MetaData.RevisionNumber++;
this.Blob.Metadata[STREAM_METADATA_KEY] = JsonConvert.SerializeObject(this.MetaData);
// TODO: the below two lines should happen atomically
await this.Blob.WritePagesAsync(toWrite, whereToWrite, null, AccessCondition.GenerateLeaseCondition(this.BlobLeaseId), null, null);
await this.Blob.SetMetadataAsync(AccessCondition.GenerateLeaseCondition(this.BlobLeaseId), null, null);
toWrite.Dispose();
如果我没有明确调用 SetMetaData 作为下一个操作,它不会被设置:(
【问题讨论】: