![](/img/trans.png)
[英]MongoWriteException: A write operation resulted in an error. The positional operator did not find the match needed from the query
[英]CosmosDb - A write operation resulted in an error. Error=16500
我有一個在 CosmosDB 上使用 MongoDB 的數據庫。
這是我使用 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));
這是用於在 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;
}
不幸的是,我在 CosmosDb 上遇到了以下問題:
寫操作導致錯誤。 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]]
按照此文檔,我知道 16500 錯誤代碼是數據庫上 RU/sec 的問題。 但它是一個MongoBulkWriteException
所以我想知道它是否由重試策略處理
根據本文檔, MongoBulkWriteException
不繼承自MongoCommandException
。 那么您能否確認 Polly 重試策略在這種情況下不適用?
我們目前正在為 MongoDB 用戶的服務器端重試新功能運行私人預覽版,我認為這將有益於您在此處所做的工作。
這是如何工作的,當遇到 429 時,我們將在返回給用戶之前自動重試請求長達 60 秒。 我們的測試表明,這幾乎解決了客戶在使用 MongoDB 客戶端進行批量攝取或使用 mongoimport 等工具時遇到的幾乎所有問題。
如果您有興趣參與此私人預覽版,請在我的 Twitter 個人資料 markjbrown 上給我 DM,並附上您希望我們為其啟用此功能的電子郵件地址和 Cosmos DB 帳戶名。
謝謝。
回答您的問題,是的,此策略不會處理批量寫入期間的限制錯誤。
您需要另外處理MongoBulkWriteException
。 比您應該查看此異常的內容,遍歷WriteErrors
集合並檢查特定元素是否因節流而出錯並安排重試(理想情況下,從此類失敗記錄組成新的批量寫入集合)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.