Azure 搜索重试策略

Azure Search RetryPolicy

我们正在使用 Azure 搜索,需要实施重试策略并按照所述存储失败文档的 ID。

有没有关于如何在 Azure 搜索中实施 RetryPolicy 策略的documentation/samples。

谢谢

目前没有显示如何在 IndexBatchException 上正确重试的示例。但是,有一种方法可以使它更容易实现:IndexBatchException.FindFailedActionsToRetry。此方法从 IndexBatchException 中提取失败文档的 ID,将它们与给定批次中的操作相关联,并且 returns 一个仅包含需要重试的失败操作的新批次。

关于重试逻辑的其余部分,您可能会发现 ClientRuntime library 中的这段代码很有用。您需要根据负载的特性调整参数。重要的是要记住,您应该在重试之前使用指数退避来帮助您的服务恢复,否则您的请求可能会受到限制。

这是我用的:

 private async Task<DocumentIndexResult> IndexWithExponentialBackoffAsync(IndexBatch<IndexModel> indexBatch)
 {
      return await Policy
           .Handle<IndexBatchException>()
           .WaitAndRetryAsync(5, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (ex, span) =>
           {
                indexBatch = ((IndexBatchException)ex).FindFailedActionsToRetry(indexBatch, x => x.Id);
           })
           .ExecuteAsync(async () => await _searchClient.IndexAsync(indexBatch));
 }

它使用 Polly library 来处理指数退避。在本例中,我使用了一个模型 IndexModel,它有一个名为 Id 的 id 字段。 如果您想记录或存储失败尝试的 ID,您可以在 WaitAndRetryAsync 函数中执行此操作,例如

((IndexBatchException)ex)ex.IndexingResults.Where(r => !r.Succeeded).Select(r => r.Key).<Do something here>