perf(LLM任务): 首查短超时快速失败 + 退避抖动 + 标题失败跳过外观识别
生产 24h 数据:外观专利任务 LLM 重试失败 93+82 次几乎全是超时, 终态 41 行降级为「外观识别异常」;成功调用 p99=34s、超 60s 仅 0.02%。 原策略每轮重试都挂满 90s(被全局 call-timeout 钳制),且 1.5s/3s 密集重试整批落在同一劣化窗口内。 - 第 1 次尝试改用 llm-first-attempt-read-timeout-millis(默认 60s)快速失败, 重试走完整读超时,慢而成功的正常调用不被误杀 - 重试等待改 LlmRetryBackoff:2s/10s + ±30% 抖动,覆盖更长窗口并打散同批尖峰 - 外观专利标题识别失败时跳过外观请求(该行必走回退,外观结果本就会被丢弃)
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package com.nanri.aiimage.common.retry;
|
||||
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
/**
|
||||
* LLM 重试等待策略:指数退避 + 抖动。
|
||||
*
|
||||
* <p>背景(2026-09-13 生产实测):上游 ai.t8star.org 存在持续数十秒的劣化窗口,
|
||||
* 窗口内所有请求都不应答;原来的 1.5s/3s 密集重试会整批落在窗口内,三连失败后
|
||||
* 行降级(外观识别异常)。退避拉到 2s/10s 并带 ±30% 抖动,可覆盖更长的窗口,
|
||||
* 同时避免同批多行在同一时刻齐发重试形成尖峰。
|
||||
*/
|
||||
public final class LlmRetryBackoff {
|
||||
|
||||
private static final long[] BASE_DELAYS_MILLIS = {2_000L, 10_000L};
|
||||
private static final double JITTER_RATIO = 0.3d;
|
||||
private static final long MIN_DELAY_MILLIS = 200L;
|
||||
|
||||
private LlmRetryBackoff() {
|
||||
}
|
||||
|
||||
/** 第 attemptIndex 次失败后的等待毫秒数(attemptIndex 从 1 开始);超出档位取最后一档。 */
|
||||
public static long delayMillis(int attemptIndex) {
|
||||
return delayMillis(attemptIndex, ThreadLocalRandom.current().nextDouble());
|
||||
}
|
||||
|
||||
/** 固定抖动入口(测试用):random 取值 [0,1),0.5 表示无抖动。 */
|
||||
static long delayMillis(int attemptIndex, double random) {
|
||||
int index = Math.min(Math.max(1, attemptIndex), BASE_DELAYS_MILLIS.length) - 1;
|
||||
long base = BASE_DELAYS_MILLIS[index];
|
||||
double bounded = Math.min(Math.max(random, 0d), 1d);
|
||||
double factor = 1d + (bounded * 2d - 1d) * JITTER_RATIO;
|
||||
return Math.max(MIN_DELAY_MILLIS, Math.round(base * factor));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user