简体   繁体   English

Azure Cosmos DB - '请求率很大。 删除项目时可能需要更多请求单位的错误

[英]Azure Cosmos DB - 'Request rate is large. More Request Units may be needed' error while deleting the items

I am using below stored procedure to delete items from cosmos db collection.我正在使用以下存储过程从 cosmos db 集合中删除项目。

function bulkDeleteStoredProcedure(query) {
    var collection = getContext().getCollection();
    var collectionLink = collection.getSelfLink();
    var response = getContext().getResponse();
    var responseBody = {
        deleted: 0,
        continuation: true
    };

    // Validate input.
    if (!query) throw new Error("The query is undefined or null.");

    tryQueryAndDelete();

    // Recursively runs the query w/ support for continuation tokens.
    // Calls tryDelete(documents) as soon as the query returns documents.
    function tryQueryAndDelete(continuation) {
        var requestOptions = {continuation: continuation};

        var isAccepted = collection.queryDocuments(collectionLink, query, requestOptions, function (err, retrievedDocs, responseOptions) {
            if (err) throw err;

            if (retrievedDocs.length > 0) {
                // Begin deleting documents as soon as documents are returned form the query results.
                // tryDelete() resumes querying after deleting; no need to page through continuation tokens.
                //  - this is to prioritize writes over reads given timeout constraints.
                tryDelete(retrievedDocs);
            } else if (responseOptions.continuation) {
                // Else if the query came back empty, but with a continuation token; repeat the query w/ the token.
                tryQueryAndDelete(responseOptions.continuation);
            } else {
                // Else if there are no more documents and no continuation token - we are finished deleting documents.
                responseBody.continuation = false;
                response.setBody(responseBody);
            }
        });

        // If we hit execution bounds - return continuation: true.
        if (!isAccepted) {
            response.setBody(responseBody);
        }
    }

    // Recursively deletes documents passed in as an array argument.
    // Attempts to query for more on empty array.
    function tryDelete(documents) {
        if (documents.length > 0) {
            // Delete the first document in the array.
            var isAccepted = collection.deleteDocument(documents[0]._self, {}, function (err, responseOptions) {
                if (err) throw err;

                responseBody.deleted++;
                documents.shift();
                // Delete the next document in the array.
                tryDelete(documents);
            });

            // If we hit execution bounds - return continuation: true.
            if (!isAccepted) {
                response.setBody(responseBody);
            }
        } else {
            // If the document array is empty, query for more documents.
            tryQueryAndDelete();
        }
    }
}

While executing this store procedure I am getting following error:在执行此存储过程时,我收到以下错误:

Failed to execute stored procedure BulkDelete for container Notifications: {"code":429,"body":{"code":"429","message":"Message: {\"Errors\":[\"Request rate is large.无法为容器执行存储过程 BulkDelete 通知:{"code":429,"body":{"code":"429","message":"Message: {\"Errors\":[\"请求率为大的。 More Request Units may be needed, so no changes were made.可能需要更多请求单元,因此未进行任何更改。 Please retry this request later.请稍后重试此请求。 Learn more: http://aka.ms/cosmosdb-error-429 \"]}\r\nActivityId: cc616784-03ee-4b10-9481-d62c26e496e4, Request URI: /apps/2268c937-d7b4-449e-9d76-a2d50d5d3546/services/df84607d-8553-4938-aa0d-913563078a93/partitions/b37017a9-ab2c-4a88-bb51-0ae729299a7e/replicas/132314907336368334p/, RequestStats: \r\nRequestStartTime: 2020-05-20T07:55:16.8899325Z, RequestEndTime: 2020-05-20T07:55:17.5299234Z, Number of regions attempted:1\r\nResponseTime: 2020-05-20T07:55:17.5299234Z, StoreResult: StorePhysicalAddress: rntbd://cdb-ms-prod-northeurope1-fd25.documents.azure.com:14307/apps/2268c937-d7b4-449e-9d76-a2d50d5d3546/services/df84607d-8553-4938-aa0d-913563078a93/partitions/b37017a9-ab2c-4a88-bb51-0ae729299a7e/replicas/132314907336368334p/, LSN: 400340, GlobalCommittedLsn: 400339, PartitionKeyRangeId: , IsValid: True, StatusCode: 429, SubStatusCode: 3200, RequestCharge: 0.38, ItemLSN: -1, SessionToken: , Usin了解详情: http://aka.ms/cosmosdb-error-429 \"]}\r\nActivityId:cc616784-03ee-4b10-9481-d62c26e496e4,请求 URI:/apps/2268c937-d7b4-4549ed35476 /services/df84607d-8553-4938-aa0d-913563078a93/partitions/b37017a9-ab2c-4a88-bb51-0ae729299a7e/replicas/132314907336368334p/, RequestStats: \r\nRequestStartTime: 25520-05-2990T 2020-05-20T07:55:17.5299234Z,尝试的区域数:1\r\n响应时间:2020-05-20T07:55:17.5299234Z,StoreResult:StorePhysicalAddress:rntbd://cdb-ms-prod-northeurope1-fd25 .documents.azure.com:14307/apps/2268c937-d7b4-449e-9d76-a2d50d5d3546/services/df84607d-8553-4938-aa0d-913563078a93/partitions/b37017a9-ab2c-4a88-bb51-0ae729299a7e/replicas/132314907336368334p/, LSN:400340,GlobalCommittedLsn:400339,PartitionKeyRangeId:,IsValid:True,StatusCode:429,SubStatusCode:3200,RequestCharge:0.38,ItemLSN:-1,SessionToken:,Usin gLocalLSN: False, TransportException: null, ResourceType: StoredProcedure, OperationType: ExecuteJavaScript\r\n, SDK: Microsoft.Azure.Documents.Common/2.11.0"},"headers":{"access-control-allow-credentials":"true","access-control-allow-origin":" https://cosmos.azure.com ","content-type":"application/json","lsn":"400340","strict-transport-security":"max-age=31536000","x-ms-activity-id":"cc616784-03ee-4b10-9481-d62c26e496e4","x-ms-cosmos-llsn":"400340","x-ms-cosmos-quorum-acked-llsn":"400340","x-ms-current-replica-set-size":"4","x-ms-current-write-quorum":"3","x-ms-gatewayversion":"version=2.11.0","x-ms-global-committed-lsn":"400339","x-ms-number-of-read-regions":"1","x-ms-quorum-acked-lsn":"400340","x-ms-request-charge":"0.38","x-ms-retry-after-ms":"8538","x-ms-schemaversion":"1.9","x-ms-serviceversion":"version gLocalLSN: False, TransportException: null, ResourceType: StoredProcedure, OperationType: ExecuteJavaScript\r\n, SDK: Microsoft.Azure.Documents.Common/2.11.0"},"headers":{"access-control-allow-credentials" :"true","access-control-allow-origin":" https://cosmos.azure.Z4D236D9A2D102C5FE6AD1C50DA4BEC500Z "","/content-type"40DA4BEC500Z","/content-type":"30json""应用程序运输安全":"max-age=31536000","x-ms-activity-id":"cc616784-03ee-4b10-9481-d62c26e496e4","x-ms-cosmos-llsn":"400340"," x-ms-cosmos-quorum-acked-llsn":"400340","x-ms-current-replica-set-size":"4","x-ms-current-write-quorum":"3" ,"x-ms-gatewayversion":"version=2.11.0","x-ms-global-committed-lsn":"400339","x-ms-number-of-read-regions":"1" ,"x-ms-quorum-acked-lsn":"400340","x-ms-request-charge":"0.38","x-ms-retry-after-ms":"8538","x- ms-schemaversion":"1.9","x-ms-serviceversion":"版本=2.11.0.0","x-ms-substatus":"3200","x-ms-transport-request-id":"120","x-ms-xp-role":"1","x-ms-throttle-retry-count":5,"x-ms-throttle-retry-wait-time-ms":32087},"activityId":"cc616784-03ee-4b10-9481-d62c26e496e4","substatus":3200,"retryAfterInMs":8538} =2.11.0.0","x-ms-substatus":"3200","x-ms-transport-request-id":"120","x-ms-xp-role":"1","x -ms-throttle-retry-count":5,"x-ms-throttle-retry-wait-time-ms":32087},"activityId":"cc616784-03ee-4b10-9481-d62c26e496e4","substatus" :3200,"retryAfterInMs":8538}

How Can I fix this issue?我该如何解决这个问题? Is something wrong with the stored procedure?存储过程有问题吗?

429 error is caused by too many request,not your stored procedure is wrong. 429错误是由于请求过多引起的,而不是您的存储过程错误。

But stored procedure is best suited for write-heavy operations,not read or delete heavy.Instead,you can use Bulk Executor Lib SDK,there is BulkDelete feature.但是存储过程最适合写入繁重的操作,而不是读取或删除繁重的操作。相反,您可以使用 Bulk Executor Lib SDK,有 BulkDelete 功能。

Here is the document .这是文件

CosmosDB returns 429 when the current aggregate RU's + the RU for your query will exceed the threshold value you have set.当您查询的当前聚合 RU + RU 超过您设置的阈值时,CosmosDB 将返回 429。 For example, if your threshold is 400 and so far you have used 380 RU's and the next query needs 22 RU's to complete, cosmos will reject the query with code 429. If the next query only needs 3 RU's, it will succeed.例如,如果您的阈值是 400,并且到目前为止您已经使用了 380 RU,并且下一次查询需要 22 RU 才能完成,那么 cosmos 将拒绝代码为 429 的查询。如果下一次查询只需要 3 RU,它将成功。 1 Second later, the cumulative RU value is reset to zero and the 22 RU query will have succeed. 1 秒后,累积 RU 值重置为零,22 RU 查询将成功。

If you get a 429 you will also receive a "x-ms-retry-after-ms" header which will contain a number.如果您收到 429,您还将收到一个“x-ms-retry-after-ms”header,其中包含一个数字。 You should wait that number of milliseconds before retrying your query.在重试查询之前,您应该等待该毫秒数。

https://docs.microsoft.com/en-us/rest/api/cosmos-db/common-cosmosdb-rest-response-headers https://docs.microsoft.com/en-us/rest/api/cosmos-db/common-cosmosdb-rest-response-headers

Alternatively, you can avoid the 429 by increasing the threshold ( this will also increase the cost of the service).或者,您可以通过提高阈值来避免 429(这也会增加服务成本)。 So you have to decide if you prefer to retry or increase the threshold.因此,您必须决定是要重试还是提高阈值。 This depends on the nature of your application.这取决于您的应用程序的性质。

RU or resource units are calculated by the CosmosDB service based on the amount work the service needs to do. RU 或资源单位由 CosmosDB 服务根据服务需要完成的工作量来计算。 It's a combination of how large your index is, how much data is being transferred, how much CPU, disk, memory you use, etc... Charging for RU's is a way for Cosmos to understand the workload you are going to be running and to make the necessary backend changes as needed.它是您的索引有多大、正在传输多少数据、多少 CPU、磁盘、您使用的 memory 等的组合......为 RU 收费是 Cosmos 了解您将要运行的工作负载的一种方式,并且根据需要进行必要的后端更改。 The per second cost for cosmos is based on your RU threshold setting. cosmos 的每秒成本取决于您的 RU 阈值设置。 It also allows cosmos to make the necessary changes in the back end to accommodate your performance needs.它还允许 cosmos 在后端进行必要的更改以适应您的性能需求。 The RU calculation gets more complex if you are reading and writing from different regions around the world.如果您从世界各地的不同地区进行读写,则 RU 计算会变得更加复杂。

You can reduce the RU cost of your queries by restructuring your data in your index.您可以通过重组索引中的数据来降低查询的 RU 成本。 If you are fanning out to multiple partitions, the queries will run parallelly which does more work in a shorter time period.如果您要分散到多个分区,则查询将并行运行,从而在更短的时间内完成更多工作。 If you reduce or increase the kilo bytes that move across the network, memory and cpu components, it will also change the RU's.如果减少或增加通过网络移动的千字节、memory 和 cpu 组件,它也会改变 RU。

The easiest way would be to increase the Throughput in the azure portal, Depends on how frequent you gonna perform such an action, you may increase the Throughput and perform your operation and reset it -if it is one off- or find your optimum Throughput.最简单的方法是增加 azure 门户中的吞吐量,取决于您执行此类操作的频率,您可以增加吞吐量并执行您的操作并重置它 - 如果它是一次关闭 - 或找到您的最佳吞吐量。 You should play around to find the number but if it is one off try something like 1000 and multiply to 10 on each attempt until it works and do not forget to reset the value back to what it was, otherwise you'll get a big bill:)您应该四处寻找数字,但如果是一次尝试,请尝试 1000 并在每次尝试时乘以 10 直到它起作用,并且不要忘记将值重置回原来的值,否则您会收到一大笔账单:)
在此处输入图像描述 (you may also consider to auto scale it if your throughput is set to more or around 4000 and gain some cost advantage) (如果您的吞吐量设置为更多或大约 4000 并获得一些成本优势,您也可以考虑自动扩展它)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ARM 模板部署 - CosmosDB 配置失败 - 请求率很大。 可能需要更多请求单位,因此未进行任何更改 - ARM Template deployment - CosmosDB provision fails with - Request rate is large. More Request Units may be needed, so no changes were made Azure Cosmos DB:简单计数查询的“请求率很大” - Azure Cosmos DB: 'Request rate is large' for simple count query 如何使用Azure Cosmos DB消耗超过保留数量的请求单元? - How can I consume more than the reserved number of request units with Azure Cosmos DB? Cosmos DB Mongo API如何管理“请求率很大”情况 - Cosmos DB Mongo API How to manage “Request Rate is Large” condition Azure云服务“远程服务器返回错误:(413)请求实体太大。” - Azure Cloud Service “The remote server returned an error: (413) Request Entity Too Large.” DocumentDB返回“请求率很大”,解析天蓝色 - DocumentDB return “Request rate is large”, parse on azure Azure Cosmos db:批量删除记录时请求超出速率限制 - Azure Cosmos db : requests exceeding rate limit when bulk deleting records sdk v3 中 Azure Cosmos DB 中的请求超时 - Request Timeout in Azure Cosmos DB in sdk v3 要求率很高 - Request rate is large Azure Cosmos DB - 创建容器时出现“RequestDisallowedByPolicy”错误 - Azure Cosmos DB - 'RequestDisallowedByPolicy' error while creating container
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM