fix(similar-asin): LLM 连接层故障短路,行级写明服务不可达真因

BACKLOG B23:网关不可达时 invokeChat 原本对任何异常跑满 3 次阶梯,
批量失败再逐行回退把白等乘上行数。现对齐 BrandCheckClient /
AppearancePatentLlmClient 同款口径:连续 2 次连接未建立(读超时与
5xx 不计入且清零证据)即停,抛「LLM 服务不可达(连接未建立,非本行
数据问题)」。变异验证 23.4s 判红→收口 7.7s 绿;后端整量 4709 例
0 红 0 错 BUILD SUCCESS。
This commit is contained in:
2026-10-23 19:47:46 +08:00
parent b65b709700
commit 6af19b51ba
2 changed files with 172 additions and 0 deletions
@@ -17,6 +17,9 @@ import org.springframework.web.client.RestClient;
import java.io.IOException;
import java.io.InputStream;
import java.net.ConnectException;
import java.net.NoRouteToHostException;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.ArrayList;
@@ -35,6 +38,15 @@ public class SimilarAsinLlmClient {
private static final MediaType APPLICATION_JSON_UTF8 = new MediaType("application", "json", StandardCharsets.UTF_8);
private static final int MAX_LLM_RESPONSE_BYTES = 16 * 1024 * 1024;
/**
* 连接层短路的证据数:同一次调用里连续这么多次「连接根本没建立」且中间没有一次成功,
* 就判定 LLM 网关不可达,立刻停掉重试阶梯。与 {@code AppearancePatentLlmClient}/
* {@code BrandCheckClient} 同口径(B19/B23)——连接层故障重试救不回来,只会把同一笔
* 失败摊满整段退避预算;本侧批量失败后还逐行回退,白等会再乘一个行数。
*/
private static final int LLM_CONNECT_FAILURE_EVIDENCE = 2;
/** 连接层判定不可达时交付的归因文案:说清是运维故障,不是这行数据的问题。 */
static final String REASON_LLM_UNREACHABLE = "LLM 服务不可达(连接未建立,非本行数据问题)";
private final SimilarAsinProperties properties;
private final ObjectMapper objectMapper;
@@ -74,11 +86,27 @@ public class SimilarAsinLlmClient {
String resolvedKey = resolveApiKey(apiKey);
int attempts = Math.max(1, properties.getLlmRetryTimes());
Exception lastFailure = null;
int connectFailures = 0;
for (int attempt = 1; attempt <= attempts; attempt++) {
try {
return invokeChatOnce(model, system, userText, images, resolvedKey, responseFormat, attempt);
} catch (Exception ex) {
lastFailure = ex;
if (isConnectFailure(ex)) {
connectFailures++;
if (connectFailures >= LLM_CONNECT_FAILURE_EVIDENCE) {
log.warn("[similar-asin][llm] LLM_UNREACHABLE 连续 {} 次连接未建立,判定 LLM 服务不可达,"
+ "停止重试 attempt={} model={} err={}",
LLM_CONNECT_FAILURE_EVIDENCE, attempt, model, failureMessage(ex));
// 结论不变(这次调用拿不到 LLM 结论),差别只在耗时与归因:
// 调用方拿到的是"网关没连上",而不是一句裸的连接异常
throw new IllegalStateException(
REASON_LLM_UNREACHABLE + ";lastErr=" + failureMessage(ex), ex);
}
} else {
// 连上了只是这次没答好(读超时/解析失败/5xx):连接层证据不作数
connectFailures = 0;
}
if (attempt >= attempts) {
break;
}
@@ -95,6 +123,20 @@ public class SimilarAsinLlmClient {
: new IllegalStateException(lastFailure.getMessage(), lastFailure);
}
/**
* 只认连接层故障(顺着 cause 链找):连接被拒/无路由/主机名解不了。
* 读超时与 5xx 是"连上了没答好",故意不算——那类抖动重试是有意义的。
*/
static boolean isConnectFailure(Throwable error) {
for (Throwable t = error; t != null && t != t.getCause(); t = t.getCause()) {
if (t instanceof ConnectException || t instanceof NoRouteToHostException
|| t instanceof UnknownHostException) {
return true;
}
}
return false;
}
/** 只认任务/用户自己的 api_key(payload → 按 uid 的服务端密钥表已在 readApiKey 兜过);
* 为空即空、跳过 LLM,绝不回落平台公费密钥 —— 各用户的任务只能烧各自的密钥。 */
public String resolveApiKey(String apiKey) {