Cosmos DB 更改源触发 Azure 函数:租赁丢失异常

Posted

技术标签:

【中文标题】Cosmos DB 更改源触发 Azure 函数:租赁丢失异常【英文标题】:Cosmos DB Change Feed Trigger Azure Function: Lease Lost Exception 【发布时间】:2021-02-10 16:45:03 【问题描述】:

以下异常记录在 Cosmos DB 更改提要触发 Azure 函数的应用程序洞察中:

Microsoft.Azure.Documents.ChangeFeedProcessor.Exceptions.LeaseLostException

["severityLevel":"Error","outerId":"0","message":"租约是 丢失。","parsedStack":["assembly":"Microsoft.Azure.Documents.ChangeFeedProcessor, 版本=2.2.6.0,文化=中性, PublicKeyToken=31bf3856ad364e35","method":"Microsoft.Azure.Documents.ChangeFeedProcessor.LeaseManagement.DocumentServiceLeaseStoreManager+d__16.MoveNext","level":0,"line":0,"assembly":"System.Private. CoreLib,版本=4.0.0.0,文化=中性, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","level":1,"line":0,"assembly":"System.Private.CoreLib, 版本=4.0.0.0,文化=中性, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess","level":2,"line":0,"assembly":"System.Private.CoreLib, 版本=4.0.0.0,文化=中性, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","level":3,"line":0,"assembly":"Microsoft.Azure.Documents.ChangeFeedProcessor, 版本=2.2.6.0,文化=中性, PublicKeyToken=31bf3856ad364e35","method":"Microsoft.Azure.Documents.ChangeFeedProcessor.PartitionManagement.PartitionController+d__9.MoveNext","level":4,"line":0,"assembly":"System.Private. CoreLib,版本=4.0.0.0,文化=中性, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","level":5,"line":0,"assembly":"System.Private.CoreLib, 版本=4.0.0.0,文化=中性, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess","level":6,"line":0,"assembly":"System.Private.CoreLib, 版本=4.0.0.0,文化=中性, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","level":7,"line":0,"assembly":"Microsoft.Azure.Documents.ChangeFeedProcessor, 版本=2.2.6.0,文化=中性, PublicKeyToken=31bf3856ad364e35","method":"Microsoft.Azure.Documents.ChangeFeedProcessor.HealthMonitoringPartitionControllerDecorator+d__3.MoveNext","level":8,"line":0],"type":"Microsoft.Azure.Documents. ChangeFeedProcessor.Exceptions.LeaseLostException","id":"517071"

Cosmos DB 更改源触发 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;
            
        
    

【问题讨论】:

【参考方案1】:

这个错误意味着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)。 您还可以检查是否存在租赁集合。

【讨论】:

您能否建议我们如何检查我们是否实施了自动或手动检查点? 或者是否需要更改任何代码来解决此问题? 查看此链接:docs.microsoft.com/en-us/azure/event-hubs/…【参考方案2】:

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

用户不需要做任何事情,因为这是正常生命周期的一部分。租约现在由另一个实例处理,该实例将读取租约范围内的分区中发生的更改。

【讨论】:

好的,但是我们能避免这个异常吗? 为什么要避免它?它不是在用户代码上抛出的,它是库正在捕获和处理的一种良性信号机制

以上是关于Cosmos DB 更改源触发 Azure 函数:租赁丢失异常的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Python 中从 Azure 函数调用 Cosmos DB 存储过程?

Azure 流分析输出到 Azure Cosmos DB

带有 Cosmos DB 绑定的 Azure 函数返回格式化的 DateTime?

Azure Cosmos DB:使用 UpsertDocumentAsync 违反唯一索引约束

天蓝色函数http触发器输入cosmos db文档

在本地使用 Cosmos DB 运行 Azure Functions - 找不到 CosmosDBAttribute.ConnectionStringSetting