fix(appearance-patent): LLM 连接层故障短路,行级写明服务不可达真因
invokeChat 的重试阶梯原先不区分故障层:网关根本连不上也把 llmRetryTimes 打满。照 BrandCheckClient 的 SERVICE_UNREACHABLE 同口径补短路——同一次 调用连续 2 次连接未建立(ConnectException/NoRouteToHost/UnknownHost, 顺 cause 链)且中间无一次成功 ⇒ 停止重试,行级原因写成「LLM 服务不可达 (连接未建立,非本行数据问题)」。读超时与 5xx 故意不算,非连接失败 清零证据。结论一字不变,差别只在耗时与归因文案。 新增 2 例互为非零对照:死端口必须在第 2 次连接失败处停(第 3 次不发); 连得上但 5xx 要跑满整个阶梯。
This commit is contained in:
+42
@@ -21,6 +21,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;
|
||||
@@ -33,6 +36,7 @@ import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Semaphore;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
@@ -65,6 +69,15 @@ public class AppearancePatentLlmClient {
|
||||
*/
|
||||
static final String BRAND_UPSTREAM_UNAVAILABLE =
|
||||
"商标查询失败:品牌库上游整体不可用(本批待查词无一返回结果,非本行数据问题)";
|
||||
/**
|
||||
* 连接层短路的证据数:同一次调用里连续这么多次「连接根本没建立」且中间没有一次成功,
|
||||
* 就判定 LLM 网关不可达,立刻停掉重试阶梯。与 {@code BrandCheckClient} 的
|
||||
* SERVICE_UNREACHABLE 同口径——2026-10-22 那条断链在 B 侧一窗白等 14,944 次、
|
||||
* 判失败 7,667 次的教训:连接层故障重试救不回来,只会把同一笔失败摊满整段退避预算。
|
||||
*/
|
||||
private static final int LLM_CONNECT_FAILURE_EVIDENCE = 2;
|
||||
/** 连接层判定不可达时交付给行级的原因文案:说清是运维故障,不是这行数据的问题。 */
|
||||
static final String REASON_LLM_UNREACHABLE = "LLM 服务不可达(连接未建立,非本行数据问题)";
|
||||
private static final String APPEARANCE_ANOMALY = "外观识别异常";
|
||||
private static final String MISSING_ROW_DATA = "爬虫数据缺失";
|
||||
|
||||
@@ -635,11 +648,26 @@ public class AppearancePatentLlmClient {
|
||||
String responseFormat) throws Exception {
|
||||
int attempts = Math.max(1, properties.getLlmRetryTimes());
|
||||
Exception lastFailure = null;
|
||||
AtomicInteger connectFailures = new AtomicInteger();
|
||||
for (int attempt = 1; attempt <= attempts; attempt++) {
|
||||
try {
|
||||
return invokeChatOnce(model, system, userText, images, apiKey, responseFormat, attempt);
|
||||
} catch (Exception ex) {
|
||||
lastFailure = ex;
|
||||
if (isConnectFailure(ex)) {
|
||||
if (connectFailures.incrementAndGet() >= LLM_CONNECT_FAILURE_EVIDENCE) {
|
||||
log.warn("[appearance-patent] 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.set(0);
|
||||
}
|
||||
if (attempt >= attempts) {
|
||||
break;
|
||||
}
|
||||
@@ -652,6 +680,20 @@ public class AppearancePatentLlmClient {
|
||||
throw lastFailure == null ? new IllegalStateException("LLM call failed") : 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;
|
||||
}
|
||||
|
||||
private String invokeChatOnce(String model,
|
||||
String system,
|
||||
String userText,
|
||||
|
||||
Reference in New Issue
Block a user