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:
+130
@@ -0,0 +1,130 @@
|
||||
package com.nanri.aiimage.modules.similarasin.client;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.nanri.aiimage.config.SimilarAsinProperties;
|
||||
import com.nanri.aiimage.modules.usersecret.service.UserSecretUsageService;
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* 连接层短路口径(B23):网关根本连不上时,重试阶梯必须在证据数处停下并给出可读归因;
|
||||
* 连得上只是没答好(5xx/读超时)不得短路,重试阶梯照跑满。
|
||||
*/
|
||||
class SimilarAsinLlmClientHttpTest {
|
||||
|
||||
private HttpServer server;
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
private final AtomicInteger requestCount = new AtomicInteger();
|
||||
/** 请求前挂起时长:模拟"连上了但不应答"的读超时形状。 */
|
||||
private volatile long responseDelayMillis = 0L;
|
||||
private volatile boolean failWith502 = false;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws IOException {
|
||||
server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
||||
server.createContext("/v1/chat/completions", this::handleChat);
|
||||
// 默认单线程派发:handler 睡眠模拟读超时时会吞掉超时后补发的下一次请求
|
||||
server.setExecutor(Executors.newCachedThreadPool());
|
||||
server.start();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
server.stop(0);
|
||||
}
|
||||
|
||||
private void handleChat(HttpExchange exchange) throws IOException {
|
||||
exchange.getRequestBody().readAllBytes();
|
||||
requestCount.incrementAndGet();
|
||||
long delay = responseDelayMillis;
|
||||
if (delay > 0L) {
|
||||
try {
|
||||
Thread.sleep(delay);
|
||||
} catch (InterruptedException interrupted) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
String body = failWith502
|
||||
? "{\"error\":{\"message\":\"bad gateway\"}}"
|
||||
: "{\"choices\":[{\"message\":{\"content\":\"{\\\"ok\\\":true}\"}}]}";
|
||||
respond(exchange, failWith502 ? 502 : 200, body);
|
||||
}
|
||||
|
||||
private void respond(HttpExchange exchange, int status, String body) throws IOException {
|
||||
byte[] bytes = body.getBytes(StandardCharsets.UTF_8);
|
||||
exchange.getResponseHeaders().set("Content-Type", "application/json; charset=utf-8");
|
||||
exchange.sendResponseHeaders(status, bytes.length);
|
||||
try (OutputStream os = exchange.getResponseBody()) {
|
||||
os.write(bytes);
|
||||
}
|
||||
}
|
||||
|
||||
/** 连接层故障短路:网关根本连不上时,第 2 次连接失败即停,归因文案说清是运维故障。 */
|
||||
@Test
|
||||
void unreachableGatewayShortCircuitsLadderAndSurfacesReason() {
|
||||
SimilarAsinLlmClient deadHostClient = buildClient(p -> {
|
||||
p.setLlmHost("http://127.0.0.1:1");
|
||||
p.setLlmRetryTimes(3);
|
||||
});
|
||||
|
||||
long startedAt = System.nanoTime();
|
||||
assertThatThrownBy(() -> deadHostClient.invokeChat("test-model", "s", "u", "test-key"))
|
||||
.isInstanceOf(IllegalStateException.class)
|
||||
.hasMessageContaining("LLM 服务不可达(连接未建立,非本行数据问题)")
|
||||
.hasMessageNotContaining("LLM http ");
|
||||
long elapsedMs = (System.nanoTime() - startedAt) / 1_000_000;
|
||||
|
||||
// 短路发生在第 2 次连接失败:只付 1 段退避(2s±30%);本机回环拒绝连接实测 ~2.3s/次
|
||||
// ⇒ 短路 ~7.7s。跑满 3 次阶梯要多付一段 10s±30%(≥7s)⇒ 实测 ~20s,12s 档两侧都稳
|
||||
assertThat(elapsedMs).isLessThan(12_000L);
|
||||
}
|
||||
|
||||
/** 反向对照:连得上只是 5xx(非连接层故障)不得触发短路,重试阶梯要跑满。 */
|
||||
@Test
|
||||
void httpFailureKeepsFullRetryLadder() {
|
||||
failWith502 = true;
|
||||
SimilarAsinLlmClient threeAttemptClient = buildClient(p -> p.setLlmRetryTimes(3));
|
||||
|
||||
assertThatThrownBy(() -> threeAttemptClient.invokeChat("test-model", "s", "u", "test-key"))
|
||||
.hasMessageContaining("LLM http 502");
|
||||
|
||||
assertThat(requestCount.get()).isEqualTo(3);
|
||||
}
|
||||
|
||||
/** 读超时是"连上了没答好",故意不算连接层证据:阶梯照跑满,归因文案不得冒充不可达。 */
|
||||
@Test
|
||||
void readTimeoutIsNotCountedAsConnectFailure() {
|
||||
responseDelayMillis = 1_000L;
|
||||
SimilarAsinLlmClient shortTimeoutClient = buildClient(p -> {
|
||||
p.setLlmRetryTimes(2);
|
||||
p.setLlmFirstAttemptReadTimeoutMillis(300);
|
||||
p.setLlmReadTimeoutMillis(400);
|
||||
});
|
||||
|
||||
assertThatThrownBy(() -> shortTimeoutClient.invokeChat("test-model", "s", "u", "test-key"))
|
||||
.hasMessageNotContaining("LLM 服务不可达");
|
||||
|
||||
assertThat(requestCount.get()).isEqualTo(2);
|
||||
}
|
||||
|
||||
private SimilarAsinLlmClient buildClient(java.util.function.Consumer<SimilarAsinProperties> customizer) {
|
||||
SimilarAsinProperties properties = new SimilarAsinProperties();
|
||||
properties.setLlmHost("http://127.0.0.1:" + server.getAddress().getPort());
|
||||
customizer.accept(properties);
|
||||
return new SimilarAsinLlmClient(properties, objectMapper, null, mock(UserSecretUsageService.class));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user