简体   繁体   English

CosmosDb - 写入操作导致错误。 错误=16500

[英]CosmosDb - A write operation resulted in an error. Error=16500

I have a database using MongoDB on CosmosDB.我有一个在 CosmosDB 上使用 MongoDB 的数据库。

Here is my retry pattern using Polly :这是我使用 Polly 的重试模式:

_retryPolicy = Policy
    .Handle<MongoCommandException>(e =>
    {
        if (e.Code != 16500 /*(RateLimitCode)*/ || !(e.Result is BsonDocument bsonDocument))
        {
            return false;
        }

        if (bsonDocument.TryGetValue("StatusCode", out var statusCode) && statusCode.IsInt32)
        {
            switch (statusCode.AsInt32)
            {
                case 429: //HttpThrottleErrorCode
                case 1: //HttpServiceIsUnavailable
                case 50: //HttpOperationExceededTimeLimit:
                    return true;
                default:
                    return false;
            }
        }

        return true;
    })
    .Or<MongoConnectionException>()
    .WaitAndRetryAsync(2, i => TimeSpan.FromSeconds(MongoRepositoryConstants.RETRY_POLICY_TIME_OUT_IN_SECOND));

And here is the code used to perform an UpdateMany with the MongoDb driver in C# :这是用于在 C# 中使用 MongoDb 驱动程序执行UpdateMany的代码:

public async Task<bool> UpdateManyAsync(IEnumerable<JObject> listRelatedQuotes, DateTime datetime, string quoteStatus)
{
    var listQuoteNumber = new BsonArray(listRelatedQuotes.Select(quote => quote[StdJsonDataLabel.toto][StdJsonDataLabel.QUOTE_IDENTIFIER_LABEL].ToString()));
    FilterDefinition<BsonDocument> filter = Builders<BsonDocument>.Filter.In(StdJsonDataPath.toto, listQuoteNumber);

    var update = Builders<BsonDocument>.Update.Set(StdJsonDataPath.fooooo, datetime.ToString("o"));

    if (!string.IsNullOrEmpty(quoteStatus))
    {
        update = update.Set(StdJsonDataPath.foooo2, quoteStatus);
    }

    bool res = false;
    await _retryPolicy.ExecuteAsync(async () =>
    {
        var result = await _collection.UpdateManyAsync(filter, update).ConfigureAwait(false);
        res = (result.MatchedCount > 0);
    });
    return res;
}

Unfortunaltely I get the following issue on CosmosDb :不幸的是,我在 CosmosDb 上遇到了以下问题:

A write operation resulted in an error.写操作导致错误。 Error=16500, RetryAfterMs=12, Details=' A bulk write operation resulted in one or more errors. Error=16500, RetryAfterMs=12, Details=' 批量写入操作导致一个或多个错误。 Error=16500, RetryAfterMs=12, Details='错误=16500,RetryAfterMs=12,详细信息=”
MongoDB.Driver.MongoBulkWriteException`1[[MongoDB.Bson.BsonDocument, MongoDB.Bson, Version=2.8.1.0, Culture=neutral, PublicKeyToken=null]] MongoDB.Driver.MongoBulkWriteException`1[[MongoDB.Bson.BsonDocument, MongoDB.Bson, Version=2.8.1.0, Culture=neutral, PublicKeyToken=null]]

Following this documentation I know that the 16500 error code is an issue with the RU/sec on the database.按照文档,我知道 16500 错误代码是数据库上 RU/sec 的问题。 But it is a MongoBulkWriteException so I'm wondering if it is handled by the retry policy但它是一个MongoBulkWriteException所以我想知道它是否由重试策略处理

Following this documentation, the MongoBulkWriteException doesn't inherit from MongoCommandException .根据文档, MongoBulkWriteException不继承自MongoCommandException So can you please confirm that the Polly retry policy doesn't apply in this case?那么您能否确认 Polly 重试策略在这种情况下不适用?

EDIT : watching the CosmosDb dashboard on Azure, it looks like the UpdateMany costs a lot of RU :编辑:在 Azure 上观看 CosmosDb 仪表板,看起来UpdateMany花费了很多 RU : 在此处输入图片说明

We are currently running a private preview for a new feature for server-side retries for MongoDB users and I think this would benefit what you are doing here.我们目前正在为 MongoDB 用户的服务器端重试新功能运行私人预览版,我认为这将有益于您在此处所做的工作。

How this works is when 429's are encountered we will automatically retry requests up to 60 seconds before returning to the user.这是如何工作的,当遇到 429 时,我们将在返回给用户之前自动重试请求长达 60 秒。 Our testing indicates this resolves nearly all of the issues customers see when doing bulk ingestion with MongoDB clients as you are or using tools like mongoimport.我们的测试表明,这几乎解决了客户在使用 MongoDB 客户端进行批量摄取或使用 mongoimport 等工具时遇到的几乎所有问题。

If you are interested in participating in this private preview, please DM me on my Twitter profile markjbrown with your email address and Cosmos DB account names you would like for us to enable this feature for.如果您有兴趣参与此私人预览版,请在我的 Twitter 个人资料 markjbrown 上给我 DM,并附上您希望我们为其启用此功能的电子邮件地址和 Cosmos DB 帐户名。

Thanks.谢谢。

Answering to your question, yes, this Policy won't handle throttling errors during bulk writes.回答您的问题,是的,此策略不会处理批量写入期间的限制错误。

You need to additionally handle MongoBulkWriteException .您需要另外处理MongoBulkWriteException Than you should look into the contents of this exception, iterate over WriteErrors collection and check if specific element errored because of throttling and schedule it for a retry(ideally, compose a new bulk write collection from such failed records).比您应该查看此异常的内容,遍历WriteErrors集合并检查特定元素是否因节流而出错并安排重试(理想情况下,从此类失败记录组成新的批量写入集合)。

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

相关问题 MongoWriteException:写入操作导致错误。 位置运算符未从查询中找到所需的匹配项 - MongoWriteException: A write operation resulted in an error. The positional operator did not find the match needed from the query SQL错误“算术运算导致溢出。” - Sql error “Arithmetic operation resulted in an overflow.” FileStream获取错误算术运算导致溢出 - FileStream get error Arithmetic operation resulted in an overflow 算术运算导致溢出错误C# - Arithmetic operation resulted in an overflow error c# 算术运算导致溢出错误-对于c#中的byte []数组 - Arithmetic operation resulted in an overflow ERROR - for byte[] arrays in c# ConcurrentQueue操作给出OutofMemory错误。 - ConcurrentQueue Operation is giving OutofMemory Error. MailBee Imap.Connect 抛出算术运算导致溢出错误 - MailBee Imap.Connect throws an Arithmetic operation resulted in an overflow error 检查过程是否间接导致错误 - Check if process indirectly resulted in an error 我得到命令查找失败:Error=16500, RetryAfterMs=64, Error while Fetching Record Count in Azure Cosmos Db - I am getting Command find failed: Error=16500, RetryAfterMs=64, Error while Fetching Record Count in Azure Cosmos Db 在 controller 中添加 [Authorize] 导致错误 401.0 - Adding [Authorize] in controller resulted in Error 401.0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM