diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/appearancepatent/client/AppearancePatentLlmClient.java b/backend-java/src/main/java/com/nanri/aiimage/modules/appearancepatent/client/AppearancePatentLlmClient.java index 16d580eb5..7d86ab10f 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/appearancepatent/client/AppearancePatentLlmClient.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/appearancepatent/client/AppearancePatentLlmClient.java @@ -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, diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/client/AppearancePatentLlmClientHttpTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/client/AppearancePatentLlmClientHttpTest.java index 92384ce42..eda983869 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/client/AppearancePatentLlmClientHttpTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/client/AppearancePatentLlmClientHttpTest.java @@ -293,6 +293,39 @@ class AppearancePatentLlmClientHttpTest { localBrandCheckClient(), mock(UserSecretUsageService.class)); } + /** 连接层故障短路:网关根本连不上时,重试阶梯必须在证据数处停下,行级拿到可读的真因。 */ + @Test + void unreachableGatewayShortCircuitsLadderAndSurfacesReason() { + AppearancePatentLlmClient deadHostClient = buildClient(p -> { + p.setLlmHost("http://127.0.0.1:1"); + p.setLlmRetryTimes(3); + }); + + List result = deadHostClient.inspectRows( + List.of(row("9", "B009", "任意标题", "", "https://img.example.com/9.jpg")), null, "test-key"); + + assertThat(result).hasSize(1); + AppearancePatentResultRowDto failed = result.get(0); + // 行级原因走标题回退链路(invokeTitleBatchSafely 把异常折进 titleErrors) + assertThat(failed.getTitleReason()).contains("LLM 服务不可达(连接未建立,非本行数据问题)"); + assertThat(failed.getTitleReason()).doesNotContain("LLM http "); + // 短路发生在第 2 次连接失败,第 3 次尝试不再发出:连接层故障重试救不回来 + assertThat(textRequestCount.get()).isZero(); + assertThat(imageRequestCount.get()).isZero(); + } + + /** 反向对照:连得上只是 5xx(非连接层故障)不得触发短路,重试阶梯要跑满。 */ + @Test + void httpFailureKeepsFullRetryLadder() { + failTextRequests = true; + AppearancePatentLlmClient threeAttemptClient = buildClient(p -> p.setLlmRetryTimes(3)); + + threeAttemptClient.inspectRows( + List.of(row("10", "B010", "任意标题", "", "https://img.example.com/10.jpg")), null, "test-key"); + + assertThat(textRequestCount.get()).isEqualTo(3); + } + private boolean messagesBodyContains(HttpExchange exchange, String text) { return capturedBodies.stream().anyMatch(b -> b.contains(text)); }