Azure CosmosDB:批量删除期间出错

Posted

技术标签:

【中文标题】Azure CosmosDB:批量删除期间出错【英文标题】:Azure CosmosDB: Getting Error during bulk deletion 【发布时间】:2020-08-06 07:11:45 【问题描述】:

我正在删除批量项目并在我的本地计算机上收到以下错误。我正在使用 CosmosDB SDK 3.0。

尝试初始化 CosmosBb 时发生错误。一个或多个错误 发生了。 (响应状态码不表示成功: 服务不可用(503);子状态:0;活动编号: befe13e5-172f-4930-953f-e24bf9b0a14a;原因:(服务目前 不可用。 ActivityId:befe13e5-172f-4930-953f-e24bf9b0a14a, 请求开始时间:2020-08-06T07:04:58.1807989Z,请求结束时间: 2020-08-06T07:05:28.2986219Z,尝试的区域数:1 响应时间:2020-08-06T07:04:59.1993537Z,存储结果: 存储物理地址: rntbd://127.0.0.1:10253/apps/DocDbApp/services/DocDbServer18/partitions/a4cb495e-38c8-11e6-8106-8cdcd42c33be/replicas/1p/, LSN:-1,GlobalCommittedLsn:-1,PartitionKeyRangeId:,IsValid: 假,StatusCode:410,SubStatusCode:0,RequestCharge:0,ItemLSN: -1,SessionToken:,UsingLocalLSN:True,TransportException:发生客户端传输错误:无法连接到远程端点。 (时间:2020-08-06T07:04:59.1993537Z,活动ID: befe13e5-172f-4930-953f-e24bf9b0a14a,错误代码:ConnectFailed [0x0005],基本错误:套接字错误

这是我的删除方法。

private async Task DeleteAllExistingSubscriptions(string userUUId)
        
            var subscriptions = await _repository
                .GetItemsAsync(x => x.DistributionUserIds.Contains(userUUId), o => o.PayerNumber);

            if (subscriptions.Any())
            
                List<Task> bulkOperations = new List<Task>();
                foreach (var subscription in subscriptions)
                
                    bulkOperations.Add(_repository
                        .DeleteItemAsync(subscription.Id.ToString(), subscription.PayerNumber));
                
                await Task.WhenAll(bulkOperations);
            
        

Cosmos 客户端:

private static void RegisterCosmosClient(IServiceCollection serviceCollection, IConfiguration configuration)
        
            string cosmosDbEndpoint = configuration["CosmoDbEndpoint"];

            Ensure.ConditionIsMet(cosmosDbEndpoint.IsNotNullOrEmpty(),
                () => new InvalidOperationException("Unable to locate configured CosmosDB endpoint"));

            var cosmosDbAuthKey = configuration["CosmoDbAuthkey"];

            Ensure.ConditionIsMet(cosmosDbAuthKey.IsNotNullOrEmpty(),
                () => new InvalidOperationException("Unable to locate configured CosmosDB auth key"));

            serviceCollection.AddSingleton(s => new CosmosClient(cosmosDbEndpoint, cosmosDbAuthKey,
                new CosmosClientOptions  AllowBulkExecution = true ));
        

【问题讨论】:

我的猜测是很多绑定同时发生。我假设 _repository 有一个共享连接......每个连接的最大 sim 操作是多少。例如,使用 EF 到 sql 你不能在一个 DbInstance 上执行多个事务......所以也许每个任务都需要它自己的?但这似乎也不对。错误消息也没那么有用。 尝试在任何给定时间将数量限制为最多 5 个 【参考方案1】:

错误是客户端连接问题:

TransportException: A client transport error occurred: Failed to connect to the remote endpoint. 
(Time: 2020-08-06T07:04:59.1993537Z, 
activity ID: befe13e5-172f-4930-953f-e24bf9b0a14a, 
error code: ConnectFailed [0x0005], base error: socket error

常见的原因是资源匮乏:

您发送的数据量需要 CPU 处理,如果环境的 CPU 不足以处理数据量,您将看到 CPU 峰值 (>70 - 100%),这将导致 TCP 连接被延迟并会导致这些问题。在这种情况下,要么减少数据量,要么增加可用资源。 如果在云上运行,也可能是 [SNAT 限制](https://docs.microsoft.com/azure/cosmos-db/troubleshoot-dot-net-sdk#snat] 或实例的连接限制(取决于服务,连接限制会有所不同)。

参考:https://docs.microsoft.com/azure/cosmos-db/troubleshoot-dot-net-sdk-request-timeout#troubleshooting-steps

【讨论】:

以上是关于Azure CosmosDB:批量删除期间出错的主要内容,如果未能解决你的问题,请参考以下文章

Azure blob 批量删除西里尔字符

Azure DevOps Server:使用Postman批量删除测试用例

Azure DevOps Server:使用Postman批量删除测试用例

Azure blob 批量删除无法使用 rest API 删除多个文件

从 Azure 存储 Blob 中批量删除“x”天前的对象

使用服务帐户从 Azure Blob 存储中批量删除文件