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

查看:23
本文介绍了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.

我想要发生的事情如下:

What I want to happen is the following:

// 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 时,它不会同步等待直到 Blob 租约从请求 1 中释放,它只是返回一个错误,表明该 Blob 上已经有一个活动租约,然后继续.

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 Function 中可以有上述同步系统吗?

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 Lease 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天全站免登陆