Azure Blob存储| AcquireLeaseAsync,同步等待,直到释放锁 [英] Azure Blob Storage | AcquireLeaseAsync, synchronously wait until lock is released

查看:124
本文介绍了Azure Blob存储| AcquireLeaseAsync,同步等待,直到释放锁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我正在使用Azure Functions来实现HTTP无服务器功能,并且在访问存储帐户上的单个Blob存储项时,我需要使传入的请求同步.

Right now I'm making a HTTP serverless function with Azure Functions and with that I need to make my incoming requests synchronous when accessing a single Blob Storage Item on my Storage Account.

我想发生的事情如下:

// Incoming Request 1

// .. Get blob

await myBlob.AcquireLeaseAsync(TimeSpan.FromMinutes(1), leaseId);

// .. 10 seconds later

await myblob.ReleaseLeaseAsync(AccessCondition.GenerateEmptyCondition());


//
// MEANWHILE
//

// Incoming Request 2 (Happens 1-2 seconds after Request 1)

// .. Get blob    
// .. -- Wait until Lease is released from Request 1 --


await myBlob.AcquireLeaseAsync(TimeSpan.FromMinutes(1), leaseId);

// Repeat...

但是我注意到,当我尝试在请求2上获取Blob时,它不会同步等待直到从请求1释放Blob租约,它只是返回一个错误,表明该Blob上已有有效的Lease,然后继续.

However I noticed when I try to get Blob on Request 2, it doesn't synchronously wait until the Blob lease is released from Request 1, it simply just returns an error which says that there's already an active Lease on this blob, and then just continues.

在Azure功能中是否可以具有上述同步系统?

Is it possible to have the above synchronous system in an Azure Function?

推荐答案

获取租约Blob时收到的错误(可能是代码= 409)是正确的.对于无服务器的分布式模型,这是一种完美的行为,在该模型中,更多功能(工作,工作者等)希望对租用Blob具有独占访问权.

The error (probably a code=409) what you are received is correct when you acquired a lease blob. This is a perfect behavior for serverless distributed model, where more functions (jobs, workers, etc.) want to have an exclusive access on the lease blob.

他们中只有一个可以成为赢家,而其他人则必须定期再次要求其购置租约. 建议在此获取期间使用随机的等待时间.

Only one of them can be a winner and the others must periodically asking again for their acquire lease. It is recommended during this acquiring period use a random waiting time.

以下代码段显示了azure函数中此循环逻辑"的示例:

The following code snippet shows an example of this "loop logic" in the azure function:

    // Acquireing a Lease blob
    public async static Task<string> LockingEntity(dynamic entity, int leaseTimeInSec = 60, string leaseId = null)
    {
       while (true)
       {
          try
          {
             string lid = await entity.AcquireLeaseAsync(TimeSpan.FromSeconds(leaseTimeInSec), leaseId);
             if (string.IsNullOrEmpty(lid))
                await Task.Delay(TimeSpan.FromMilliseconds(new Random(Guid.NewGuid().GetHashCode()).Next(250, 1000)));
             else
                return lid;
          }
          catch (StorageException ex)
          {
             if (ex.RequestInformation.HttpStatusCode != 409)
                throw;
          }
      }
   }

上述方法在azure函数中的用法:

the usage of the above method in the azure function:

    string leaseId = await LockingEntity(blockBlob);
    if (string.IsNullOrEmpty(leaseId) == false)
    {
       try
       {
          string esp = await blockBlob.DownloadTextAsync(Encoding.UTF8, AccessCondition.GenerateLeaseCondition(leaseId), null, null);
          state = JsonConvert.DeserializeObject<State>(esp);
          // …
       }
       finally
       {
          await blockBlob.ReleaseLeaseAsync(AccessCondition.GenerateLeaseCondition(leaseId));
       }
    }

另外,请查看我的文章使用Azure租赁Blob 在无服务器事件驱动的分布式体系结构中获取更多模式等.

Also, have a look at my article Using Azure Lease Blob for more patterns, etc. in the serverless event driven distributed architecture.

这篇关于Azure Blob存储| AcquireLeaseAsync,同步等待,直到释放锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆