I am using Azure SDK version 2.2. I am having problem, that in my code potentially multiple machines call this code
CloudPageBlob _pageBlob = .....
try
{
_pageBlob.FetchAttributes();
}
catch
{
//container does not exist
_pageBlob.Create(AllocatedBlobSize);
}
I have tested it, and my conclusion is that multiple calls of _pageBlob.Create(AllocatedBlobSize);
clears previous data. How to ensure that only one machine wins and creates CloudPageBlob? (I want others to fail) I was expecting that multiple Create
calls would result in exception but that is not what is happening :(.
EDIT: In our architecture there is one leader and X followers. I am using CloudPageBlob + ETAG to elect new leader (only one machine will be able to write it's metadata to CloudPageBlob = new leader). But there is extreme case, when this cloud page blob can get corrupted + leader dies(or we are starting and there is no leader yet). In this case, I need to create new CloudPageBlob (normaly leader creates it) => each machine is trying to create it and write it's metadata to it (everyone want to be leader). In theory, if 2 machine were executing this code. Machine A calls _pageBlob.Create()
, no error, writes metadata thinks it's leader. Machine B call _pageBlob.Create()
, no error, metadata of machine A(+ potentially more data) are rewritten by machine B. I want machine B to fail :). The biggest problem is that I am loosing data.
Solution I have found is to use AccessCondition.IfNotModifiedSince.
var timeOffset = new DateTimeOffset(new DateTime(1, 1, 1), TimeSpan.FromMilliseconds(0));
var cond = AccessCondition.GenerateIfNotModifiedSinceCondition(timeOffset);
var options = new BlobRequestOptions ();
pageBlob.Create(AllocatedBlobSize, cond, options);
Now If I call CloudPageBlob.Create() for second time with same condition, second call fails. What is exactly I wanted. Thanks all for help.