在我的团队的应用程序中发现以下行为:

多个线程尝试使用下面的行在同一个Blob上获取租约:

await blob.AcquireLeaseAsync(TimeSpan.FromSeconds(lockTime), null, null, new BlobRequestOptions() { RetryPolicy = RetryPolicy }, null);

多个线程在大约同一时间成功获得了租约,每个线程都获得了不同的租约。
这是因为AcquireLeaseAsync是NOT thread-safe对吗?
我以为租约是一种锁,因此一次只能授予一个线程吗?

我们试图添加一个租约ID为null的访问条件,这似乎很有帮助。
await blob.AcquireLeaseAsync(TimeSpan.FromSeconds(lockTime), null, new AccessCondition { LeaseId = null}, new BlobRequestOptions() { RetryPolicy = RetryPolicy }, null);

但是不确定是否万无一失,即尝试获取Blob上的租约时是否有多个线程可以通过此访问条件?

下面是我用来生成此问题的代码:
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("started");
        var storageConnStr = "";
        var client = CloudStorageAccount.Parse(storageConnStr).CreateCloudBlobClient();
        var container = client.GetContainerReference("test");
        var blob = container.GetBlobReference("Program.cs");
        List<Task> list = new List<Task>();

        for (int i = 0; i < 100; i++)
        {
            list.Add(Task.Run(() => AcquireLease(blob)));
        }
        Task.WhenAll(list).Wait();

    }
    static void AcquireLease(CloudBlob blob)
    {
        try
        {
            var id = blob.AcquireLeaseAsync(TimeSpan.FromSeconds(30), null, null, null, null).Result;
            Console.WriteLine($"Successfully acquired lease on thread {Thread.CurrentThread.ManagedThreadId}, lease id {id}, time is {DateTime.UtcNow}");
        }
        catch (Exception)
        {
            Console.WriteLine($"Got exception at {Thread.CurrentThread.ManagedThreadId}");
        }
    }
}

示例输出:

最佳答案

var id = blob.AcquireLeaseAsync更改为var id = await blob.AcquireLeaseAsync

关于c# - 同时在同一 azure Blob上获取租约,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53235616/

10-11 07:45