Cosmos DB更改提要触发Azure功能:丢失租约异常

时间:2020-10-28 10:20:18

标签: azure azure-functions azure-cosmosdb

在针对Cosmos DB更改Feed的应用程序见解中记录的异常以下触发了Azure功能:

Microsoft.Azure.Documents.ChangeFeedProcessor.Exceptions.LeaseLostException

[{“ severityLevel”:“错误”,“ outerId”:“ 0”,“消息”:“租约 “,” parsedStack“:[{” assembly“:” Microsoft.Azure.Documents.ChangeFeedProcessor, 版本= 2.2.6.0,文化=中性, PublicKeyToken = 31bf3856ad364e35“,”方法“:” Microsoft.Azure.Documents.ChangeFeedProcessor.LeaseManagement.DocumentServiceLeaseStoreManager + d__16.MoveNext“,” level“:0,” line“:0},{” assembly“:” System.Private。 CoreLib,版本= 4.0.0.0,文化=中性, PublicKeyToken = 7cec85d7bea7798e“,”方法“:” System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw“,”级别“:1,” line“:0},{” assembly“:” System.Private.CoreLib, 版本= 4.0.0.0,文化=中性, PublicKeyToken = 7cec85d7bea7798e“,”方法“:” System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess“,”级别“:2,” line“:0},{” assembly“:” System.Private.CoreLib, 版本= 4.0.0.0,文化=中性, PublicKeyToken = 7cec85d7bea7798e“,”方法“:” System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification“,” level“:3,” line“:0},{” assembly“:” Microsoft.Azure.Documents.ChangeFeedProcessor, 版本= 2.2.6.0,文化=中性, PublicKeyToken = 31bf3856ad364e35“,”方法“:” Microsoft.Azure.Documents.ChangeFeedProcessor.PartitionManagement.PartitionController + d__9.MoveNext“,” level“:4,” line“:0},{” assembly“:” System.Private。 CoreLib,版本= 4.0.0.0,文化=中性, PublicKeyToken = 7cec85d7bea7798e“,”方法“:” System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw“,”级别“:5,” line“:0},{” assembly“:” System.Private.CoreLib, 版本= 4.0.0.0,文化=中性, PublicKeyToken = 7cec85d7bea7798e“,”方法“:” System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess“,”级别“:6,”行“:0},{” assembly“:” System.Private.CoreLib, 版本= 4.0.0.0,文化=中性, PublicKeyToken = 7cec85d7bea7798e“,”方法“:” System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification“,” level“:7,” line“:0},{” assembly“:” Microsoft.Azure.Documents.ChangeFeedProcessor, 版本= 2.2.6.0,文化=中性, PublicKeyToken = 31bf3856ad364e35“,”方法“:” Microsoft.Azure.Documents.ChangeFeedProcessor.HealthMonitoringPartitionControllerDecorator + d__3.MoveNext“,” level“:8,” line“:0}”,“ type”:“ Microsoft.Azure.Documents。 ChangeFeedProcessor.Exceptions.LeaseLostException“,” id“:” 517071“}

Cosmos DB更改Feed触发Azure功能:

public static class NotificationChangeFeed
    {
        [FunctionName(nameof(NotificationChangeFeed))]
        public static async Task Run([CosmosDBTrigger(
            databaseName: CosmosDBConstants.DataBaseName,
            collectionName: CosmosDBConstants.NotificationContainer,
            ConnectionStringSetting = CosmosDBConstants.ConnectionStringName,
            CreateLeaseCollectionIfNotExists = true,
            LeaseCollectionName = CosmosDBConstants.LeaseConainer)]IReadOnlyList<Document> input,
            [Inject] ILoggingService loggingService,
            [Inject] IEmailProcessor emailProcessor)
        {
            var logger = new Logger(loggingService);

            try
            {
                if (input != null && input.Count > 0)
                {
                    foreach (Document document in input)
                    {
                        string requestBody = document.ToString();
                        var notification = requestBody.AsPoco<Notification>();

                        await emailProcessor.HandleEmailAsync(notification, logger);
                        logger.Info($"Email Notification sent successfully for file name: {document.Id}");
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error($"Unable to process Documents for Email Notification for Files: {input?.Count}", ex,
                    nameof(NotificationChangeFeed));
                throw;
            }
        }
    }

2 个答案:

答案 0 :(得分:0)

此错误表示lease is lost, that would typically happen when it is taken by another host. Other cases: communication failure, number of retries reached, lease not found.

  • 处理租赁的首选方法是使用自动租赁 Checkpoint实现(如果您完全使用manual checkpointing)。
  • 您还可以检查是否存在租赁集合。

答案 1 :(得分:0)

LeaseLost是一个正常信号,表示当前实例拥有一个租约,但是由于负载平衡(可能是另一个实例出现或实例数量正在更改),它被另一台主机占用了。在初始化(首次启动一组实例)或由于实例数量更改而进行的重新平衡期间,这是预期的。

没有什么希望用户做的,因为这是正常生命周期的一部分。现在,该租约由另一个实例处理,该实例将读取该租约作用于的分区中发生的更改。