代码之家  ›  专栏  ›  技术社区  ›  Mo B.

Azure服务总线不断抛出MessageLockLostException

  •  2
  • Mo B.  · 技术社区  · 6 年前

    我一直在 MessageLockLostExceptions

    现在,我想模拟一个运行时间稍长的消息处理任务(但仍然在 锁定持续时间 )加入 Task.Delay(10_000) . 但后来我得到了一份工作 MessageLockLostException 确切地说,每4条信息。

    即使我设置了 MaxAutoRenewDuration = TimeSpan.FromDays(30) PrefetchCount = 0


    这是消息处理方法,我稍微更改了它,以打印出剩余的锁定持续时间:

        private static async Task processMessagesAsync(Message message, CancellationToken token)
        {
            Console.Write($"Received message: {message.SystemProperties.SequenceNumber}. Remaining lock duration: {message.SystemProperties.LockedUntilUtc - DateTime.UtcNow}");
            await Task.Delay(10000);
            await queueClient.CompleteAsync(message.SystemProperties.LockToken);
            Console.WriteLine(" - Complete!");
        }
    

    样本输出:

    ======================================================
    Press ENTER key to exit after receiving all the messages.
    ======================================================
    Received message: 3659174697238584. Remaining lock duration: 00:00:30.8269132 - Complete!
    Received message: 19421773393035331. Remaining lock duration: 00:00:20.5271654 - Complete!
    Received message: 11540474045136941. Remaining lock duration: 00:00:10.3372697 - Complete!
    Received message: 15762598695796784. Remaining lock duration: 00:00:00.1776760
    Message handler encountered an exception     Microsoft.Azure.ServiceBus.MessageLockLostException: The lock supplied is invalid. Either the lock expired, or the message has already been removed from the queue. Reference:2c6caac3-e607-4130-a522-f75e4636e130, TrackingId:3ff82738-664d-4aca-b55f-ba3900f1c640_B17, SystemTracker:ocgtesting:queue:workflow~63, Timestamp:2018-12-12T17:01:59
    at Microsoft.Azure.ServiceBus.Core.MessageReceiver.OnRenewLockAsync(String lockToken) in C:\source\azure-service-bus-dotnet\src\Microsoft.Azure.ServiceBus\Core\MessageReceiver.cs:line 1260
    at Microsoft.Azure.ServiceBus.Core.MessageReceiver.<>c__DisplayClass74_0.<<RenewLockAsync>b__0>d.MoveNext() in C:\source\azure-service-bus-dotnet\src\Microsoft.Azure.ServiceBus\Core\MessageReceiver.cs:line 771
    --- End of stack trace from previous location where exception was thrown ---
    at Microsoft.Azure.ServiceBus.RetryPolicy.RunOperation(Func`1 operation, TimeSpan operationTimeout) in C:\source\azure-service-bus-dotnet\src\Microsoft.Azure.ServiceBus\RetryPolicy.cs:line 83
    at Microsoft.Azure.ServiceBus.RetryPolicy.RunOperation(Func`1 operation, TimeSpan operationTimeout) in C:\source\azure-service-bus-dotnet\src\Microsoft.Azure.ServiceBus\RetryPolicy.cs:line 105
    at Microsoft.Azure.ServiceBus.Core.MessageReceiver.RenewLockAsync(String lockToken) in C:\source\azure-service-bus-dotnet\src\Microsoft.Azure.ServiceBus\Core\MessageReceiver.cs:line 773
    at Microsoft.Azure.ServiceBus.Core.MessageReceiver.RenewLockAsync(Message message) in C:\source\azure-service-bus-dotnet\src\Microsoft.Azure.ServiceBus\Core\MessageReceiver.cs:line 742
    at Microsoft.Azure.ServiceBus.MessageReceivePump.RenewMessageLockTask(Message message, CancellationToken renewLockCancellationToken) in C:\source\azure-service-bus-dotnet\src\Microsoft.Azure.ServiceBus\MessageReceivePump.cs:line 248.
    

    完整代码如下: https://pastebin.com/sFGBgE0s

    2 回复  |  直到 6 年前
        1
  •  3
  •   Sean Feldman    6 年前

    复制中缺少的一件事是队列描述。注意这些细节很重要,因为您遇到的问题与客户机无关,很可能与代理或底层AMQP库有关。

    对于非分区队列,此设置工作正常。它不适用于分区队列(标准层)。可以在新老客户中观察到。我养了一个经纪人 issue 供Azure服务总线团队调查。

        2
  •  1
  •   Arunprabhu    6 年前

    你需要 Complete 消息在 Lock Token 过期了。锁定令牌过期后,您将收到 MessageLockLostException

    我可以看到,对于每条消息,线程执行延迟了10秒。但消息似乎是在同一时间点获取的,这就是为什么每条消息的剩余锁定持续时间不断减少的原因。

    对于第四条消息,剩余的锁定持续时间为 00:00:00.1776760 . 那么,在 177 milliseconds 10 seconds 在下一行。因此,锁将过期,您将获得 MessageLockLostException . 要避免此异常,请删除 Delay