c# - CosmosDb 使用 insertMany 请求率很大

标签 c# mongodb azure azure-cosmosdb

我有以下存储库类,可将数据批量插入到 CosmosDb 数据库中:

public bool InsertZonierData(List<Zonier> zonierList)
{
    if (zonierList == null || !zonierList.Any())
    {
        throw new ZonierListNullOrEmptyException();
    }
    else
    {
        try
        {
            _collection.InsertMany(zonierList);
            return true;
        }
        catch (MongoBulkWriteException ex)
        {
            throw new DataBaseWritingException(ex.Message, ExceptionCodeConstants.DataBaseWritingExceptionCode);
        }
    }
}

不幸的是,zonierList 中的元素超过 30000 个,它在 CosmosDb 上抛出以下异常:

Unhandled Exception: MongoDB.Driver.MongoCommandException: Command insert failed: Message: {"Errors":["Request rate is large"]}

根据文档,这是与 Cosmos 上的 RU/秒相关的问题。当然,一个简单的方法是增加它,但这不是我想要做的。

是否有一种简单明了的方法来重构该方法,使我们能够在不破坏 CosmosDb 400 RU/秒的情况下插入数据。

最佳答案

Mongo 驱动程序会告诉您哪些记录出现错误以及哪些记录根本没有被处理。如果所有错误(通常是一个)的代码为 16500,那么您的问题是限制并重试错误,并且剩余记录是安全的。否则你的错误是由其他原因引起的,你应该进行分析并决定是否继续重试。

Mongo 驱动程序不返回 HTTP header ,而 Cosmos DB 建议在重试之前延迟,但这不是什么大问题。无论如何,延迟并不能保证成功,因为命中同一数据库的其他请求可能会耗尽 RU。您最好尝试并确定自己的重试规则。下面是简单的递归解决方案,它不断重试,直到一切顺利或达到重试限制。

    private async Task InsertManyWithRetry(IMongoCollection<BsonDocument> collection, 
        IEnumerable<BsonDocument> batch, int retries = 10, int delay = 300)
    {
        var batchArray = batch.ToArray();

        try
        {
            await collection.InsertManyAsync(batchArray);
        }
        catch (MongoBulkWriteException<BsonDocument> e)
        {
            if (retries <= 0)
                throw;

            //Check if there were any errors other than throttling.
            var realErrors = e.WriteErrors.Where(we => we.Code != 16500).ToArray();
            //Re-throw original exception for now.
            //TODO: We can make it more sophisticated by continuing with unprocessed records and collecting all errors from all retries.
            if (realErrors.Any())
                throw;

            //Take all records that had errors.
            var errors = e.WriteErrors.Select(we => batchArray[we.Index]);
            //Take all unprocessed records.
            var unprocessed = e.UnprocessedRequests
                .Where(ur => ur.ModelType == WriteModelType.InsertOne)
                .OfType<InsertOneModel<BsonDocument>>() 
                .Select(ur => ur.Document);

            var retryBatchArray = errors.Union(unprocessed).ToArray();

            _logger($"Retry {retryBatchArray.Length} records after {delay} ms");

            await Task.Delay(delay);

            await InsertManyWithRetry(collection, retryBatchArray, retries - 1, delay);
        }
    }

关于c# - CosmosDb 使用 insertMany 请求率很大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55263897/

相关文章:

c# - C#检查文件中的重复内容

c# - 如何使用 WebClient.DownloadData(到本地 DummyPage.aspx)

mongodb - 中断获取从 MongoDB 池中检索项目的许可

mongodb - 无法从节点连接到Docker中设置的mongo副本

azure - 如何在 Terraform 中使用日期进行计算

c# - 如何截断 .NET DateTime 的毫秒数

c# - 在生产环境中调试应用程序

mysql - 关于数据库更新/插入速率限制的一些查询(基于 SQL 或基于 NoSQL)

c# - DefaultAzureCredetials 无法通过 Visual Studio 进行身份验证 - 找不到 AzureServiceAuth\tokenProvider.json

azure - Simple.Data 和 Sql Azure transient 故障处理