feat(unreachable-cooldown): 连接层不可达客户端级冷却,四家判定下沉共用
BACKLOG B24(窗长 300s 已拍板):连续 2 次连接未建立即对该 host 进 冷却窗,窗内调用零探针快速失败(归因文案与调用内短路逐字一致);到期 后下一条真实调用兼作探针,失败重进、成功恢复。isConnectFailure 四处 副本(BrandCheckClient/AppearancePatentLlmClient/SimilarAsinLlmClient/ TaskOwnerForwardService)下沉 common/retry 共用。三家客户端接入同一 冷却表(两个 LLM 客户端共用 ai.t8star.org 一格,16890 独立一格), 窗长可配(0=关闭退回调用内短路)。另修 CollectDataBrandBatchFilter 首批预算竞态(1ms 预算下 GC 停顿误切首批,照 BrandCheckClient 首轮 不掐契约补 start>0 守卫)。变异验证:冷却禁用 6 例判红;定向 47/47 绿。
This commit is contained in:
+77
@@ -0,0 +1,77 @@
|
||||
package com.nanri.aiimage.common.retry;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.net.ConnectException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* 连接层不可达冷却表的口径(B24):阈值 2、证据可清零、窗长到点自然到期、
|
||||
* 到期后一次失败即重进、窗长 0 整体关闭、同 host 不同写法必须同格。
|
||||
*/
|
||||
class UnreachableCooldownTest {
|
||||
|
||||
private final UnreachableCooldown cooldown = new UnreachableCooldown();
|
||||
private static final long WINDOW = 60_000L;
|
||||
|
||||
@Test
|
||||
void twoConsecutiveFailuresEnterCooldownOneDoesNot() {
|
||||
assertThat(cooldown.isCoolingDown("https://gw.example.com", WINDOW)).isFalse();
|
||||
assertThat(cooldown.recordConnectFailure("https://gw.example.com", WINDOW)).isFalse();
|
||||
assertThat(cooldown.isCoolingDown("https://gw.example.com", WINDOW)).isFalse();
|
||||
// 第二次失败:冷却窗从无到有,返回 true 的那方打进入 warn;之后的失败不重复报
|
||||
assertThat(cooldown.recordConnectFailure("https://gw.example.com", WINDOW)).isTrue();
|
||||
assertThat(cooldown.isCoolingDown("https://gw.example.com", WINDOW)).isTrue();
|
||||
assertThat(cooldown.recordConnectFailure("https://gw.example.com", WINDOW)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void connectedResetsEvidenceSoTwoFreshFailuresAreRequiredAgain() {
|
||||
cooldown.recordConnectFailure("https://gw.example.com", WINDOW);
|
||||
cooldown.recordConnected("https://gw.example.com");
|
||||
assertThat(cooldown.recordConnectFailure("https://gw.example.com", WINDOW)).isFalse();
|
||||
assertThat(cooldown.isCoolingDown("https://gw.example.com", WINDOW)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void windowExpiresNaturallyAndSingleFailureReenters() throws InterruptedException {
|
||||
String host = "https://short.example.com";
|
||||
cooldown.recordConnectFailure(host, 50);
|
||||
cooldown.recordConnectFailure(host, 50);
|
||||
assertThat(cooldown.isCoolingDown(host, 50)).isTrue();
|
||||
|
||||
Thread.sleep(120);
|
||||
assertThat(cooldown.isCoolingDown(host, 50)).isFalse();
|
||||
|
||||
// 历史证据不清零:到期后探针再失败,一次即重进(B24 拍板口径)
|
||||
assertThat(cooldown.recordConnectFailure(host, 50)).isTrue();
|
||||
assertThat(cooldown.isCoolingDown(host, 50)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void zeroWindowDisablesCooldownEntirely() {
|
||||
for (int i = 0; i < 5; i++) {
|
||||
assertThat(cooldown.recordConnectFailure("https://gw.example.com", 0)).isFalse();
|
||||
}
|
||||
assertThat(cooldown.isCoolingDown("https://gw.example.com", 0)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void sameHostDifferentSpellingSharesOneCell() {
|
||||
cooldown.recordConnectFailure("https://GW.example.com", WINDOW);
|
||||
// 尾斜杠/大小写不同写法落进同一格:这条是第 2 次失败,必须进冷却
|
||||
assertThat(cooldown.recordConnectFailure("https://gw.example.com/", WINDOW)).isTrue();
|
||||
assertThat(cooldown.isCoolingDown("https://Gw.Example.Com", WINDOW)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void isConnectFailureWalksCauseChainAndIgnoresOtherFailures() {
|
||||
assertThat(UnreachableCooldown.isConnectFailure(new ConnectException("refused"))).isTrue();
|
||||
RuntimeException wrapped = new RuntimeException(
|
||||
new IllegalStateException(new ConnectException("refused")));
|
||||
assertThat(UnreachableCooldown.isConnectFailure(wrapped)).isTrue();
|
||||
assertThat(UnreachableCooldown.isConnectFailure(new RuntimeException("read timeout"))).isFalse();
|
||||
assertThat(UnreachableCooldown.isConnectFailure(null)).isFalse();
|
||||
}
|
||||
}
|
||||
+8
-7
@@ -1,6 +1,7 @@
|
||||
package com.nanri.aiimage.config;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.nanri.aiimage.common.retry.UnreachableCooldown;
|
||||
import com.nanri.aiimage.modules.brand.client.BrandCheckClient;
|
||||
import com.nanri.aiimage.modules.brand.client.BrandCheckClient.BrandCheckBatchResult;
|
||||
import com.nanri.aiimage.modules.similarasin.client.SimilarAsinLlmClient;
|
||||
@@ -96,7 +97,7 @@ class HttpClientConnectionReuseTest {
|
||||
void test_task_077_brand_normal_default_path() throws Exception {
|
||||
// 默认路径:品牌检查客户端通过共享池创建单例 RestClient,
|
||||
// 工厂为带 keep-alive 连接池的 JdkClientHttpRequestFactory。
|
||||
BrandCheckClient client = new BrandCheckClient(new BrandCheckProperties(), null, null);
|
||||
BrandCheckClient client = new BrandCheckClient(new BrandCheckProperties(), null, null, new UnreachableCooldown());
|
||||
BrandCheckBatchResult result = client.checkTitleText(" ", null, null);
|
||||
|
||||
assertTrue(result.brands().isEmpty(), "空标题安全跳过,不创建无效资源");
|
||||
@@ -107,8 +108,8 @@ class HttpClientConnectionReuseTest {
|
||||
void test_task_077_brand_normal_multiple_items() throws Exception {
|
||||
// 批量场景:LLM/品牌/紫鸟三个客户端各自持有独立 RestClient,
|
||||
// 但底层连接池共用同一 HttpClient 实例,不重复创建。
|
||||
SimilarAsinLlmClient llm = new SimilarAsinLlmClient(new SimilarAsinProperties(), new ObjectMapper(), null, null);
|
||||
BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null, null);
|
||||
SimilarAsinLlmClient llm = new SimilarAsinLlmClient(new SimilarAsinProperties(), new ObjectMapper(), null, null, new UnreachableCooldown());
|
||||
BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null, null, new UnreachableCooldown());
|
||||
ZiniaoClientImpl ziniao = new ZiniaoClientImpl(new ZiniaoProperties(), new ObjectMapper());
|
||||
|
||||
HttpClient llmClient = clientOf(factoryOf(restClientOf(llm)));
|
||||
@@ -124,7 +125,7 @@ class HttpClientConnectionReuseTest {
|
||||
void test_task_077_brand_normal_repeated_operation_is_idempotent() throws Exception {
|
||||
// 幂等:同一客户端重复触发请求创建逻辑只产生一个 RestClient,
|
||||
// 重复调用返回同一实例,不重复创建客户端对象。
|
||||
BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null, null);
|
||||
BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null, null, new UnreachableCooldown());
|
||||
assertSame(restClientOf(brand), restClientOf(brand), "品牌客户端复用同一 RestClient");
|
||||
|
||||
ZiniaoClientImpl ziniao = new ZiniaoClientImpl(new ZiniaoProperties(), new ObjectMapper());
|
||||
@@ -134,7 +135,7 @@ class HttpClientConnectionReuseTest {
|
||||
@Test
|
||||
void test_task_077_brand_boundary_empty_input() throws Exception {
|
||||
// 空输入:空品牌列表不发起任何 HTTP 请求、不创建客户端资源。
|
||||
BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null, null);
|
||||
BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null, null, new UnreachableCooldown());
|
||||
BrandCheckBatchResult result = brand.checkAll(List.of(), "Terms", null);
|
||||
|
||||
assertTrue(result.brands().isEmpty());
|
||||
@@ -145,7 +146,7 @@ class HttpClientConnectionReuseTest {
|
||||
@Test
|
||||
void test_task_077_brand_boundary_single_item() throws Exception {
|
||||
// 单元素:单客户端单请求走共享池,工厂带连接池,行为与批量一致。
|
||||
BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null, null);
|
||||
BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null, null, new UnreachableCooldown());
|
||||
assertPooled(factoryOf(restClientOf(brand)));
|
||||
}
|
||||
|
||||
@@ -155,7 +156,7 @@ class HttpClientConnectionReuseTest {
|
||||
// 不随实例数量线性增长连接资源。
|
||||
int instances = 8;
|
||||
for (int i = 0; i < instances; i++) {
|
||||
restClientOf(new BrandCheckClient(new BrandCheckProperties(), null, null));
|
||||
restClientOf(new BrandCheckClient(new BrandCheckProperties(), null, null, new UnreachableCooldown()));
|
||||
restClientOf(new ZiniaoClientImpl(new ZiniaoProperties(), new ObjectMapper()));
|
||||
}
|
||||
assertSame(HttpClientPool.sharedHttpClient(), HttpClientPool.sharedHttpClient(),
|
||||
|
||||
+10
-9
@@ -1,6 +1,7 @@
|
||||
package com.nanri.aiimage.metrics;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.nanri.aiimage.common.retry.UnreachableCooldown;
|
||||
import com.nanri.aiimage.config.BrandCheckProperties;
|
||||
import com.nanri.aiimage.config.SimilarAsinProperties;
|
||||
import com.nanri.aiimage.config.ZiniaoProperties;
|
||||
@@ -162,7 +163,7 @@ class ExternalCallMetricsRecorderTest {
|
||||
SimilarAsinProperties props = llmProps();
|
||||
SimilarAsinLlmClient client =
|
||||
new SimilarAsinLlmClient(props, objectMapper, new ExternalCallMetricsRecorder(registry),
|
||||
mock(UserSecretUsageService.class));
|
||||
mock(UserSecretUsageService.class), new UnreachableCooldown());
|
||||
|
||||
String content = client.invokeChat("test-model", "system", "hello", "test-key");
|
||||
|
||||
@@ -181,7 +182,7 @@ class ExternalCallMetricsRecorderTest {
|
||||
SimilarAsinProperties props = llmProps();
|
||||
SimilarAsinLlmClient client =
|
||||
new SimilarAsinLlmClient(props, objectMapper, new ExternalCallMetricsRecorder(registry),
|
||||
mock(UserSecretUsageService.class));
|
||||
mock(UserSecretUsageService.class), new UnreachableCooldown());
|
||||
|
||||
for (int i = 0; i < 3; i++) {
|
||||
client.invokeChat("test-model", "system", "prompt-" + i, "test-key");
|
||||
@@ -192,7 +193,7 @@ class ExternalCallMetricsRecorderTest {
|
||||
"client", "llm", "result", "success"), "三次成功全部记录");
|
||||
|
||||
// 全部外部客户端(LLM / 品牌检查 / 紫鸟)在同一次批量中各自记录指标
|
||||
BrandCheckClient brand = new BrandCheckClient(brandProps(), new ExternalCallMetricsRecorder(registry), stubbedUserSecrets());
|
||||
BrandCheckClient brand = new BrandCheckClient(brandProps(), new ExternalCallMetricsRecorder(registry), stubbedUserSecrets(), new UnreachableCooldown());
|
||||
brand.checkAll(List.of("Nintendo", "LEGO", "Sony"), "Terms", 7L);
|
||||
ZiniaoClientImpl ziniao = new ZiniaoClientImpl(ziniaoProps(), objectMapper, new ExternalCallMetricsRecorder(registry));
|
||||
assertEquals(1001L, ziniao.getCompanyIdByApiKey("test-api-key"));
|
||||
@@ -210,7 +211,7 @@ class ExternalCallMetricsRecorderTest {
|
||||
@Test
|
||||
void test_task_078_payload_metrics_normal_repeated_operation_is_idempotent() throws Exception {
|
||||
BrandCheckProperties props = brandProps();
|
||||
BrandCheckClient client = new BrandCheckClient(props, new ExternalCallMetricsRecorder(registry), stubbedUserSecrets());
|
||||
BrandCheckClient client = new BrandCheckClient(props, new ExternalCallMetricsRecorder(registry), stubbedUserSecrets(), new UnreachableCooldown());
|
||||
|
||||
client.check("Nintendo", "Terms", 7L);
|
||||
client.check("Nintendo", "Terms", 7L);
|
||||
@@ -227,7 +228,7 @@ class ExternalCallMetricsRecorderTest {
|
||||
@Test
|
||||
void test_task_078_payload_metrics_boundary_empty_input() throws Exception {
|
||||
BrandCheckProperties props = brandProps();
|
||||
BrandCheckClient client = new BrandCheckClient(props, new ExternalCallMetricsRecorder(registry), stubbedUserSecrets());
|
||||
BrandCheckClient client = new BrandCheckClient(props, new ExternalCallMetricsRecorder(registry), stubbedUserSecrets(), new UnreachableCooldown());
|
||||
|
||||
BrandCheckClient.BrandCheckBatchResult result = client.checkAll(List.of(), "Terms", 7L);
|
||||
|
||||
@@ -242,7 +243,7 @@ class ExternalCallMetricsRecorderTest {
|
||||
@Test
|
||||
void test_task_078_payload_metrics_boundary_single_item() throws Exception {
|
||||
BrandCheckProperties props = brandProps();
|
||||
BrandCheckClient client = new BrandCheckClient(props, new ExternalCallMetricsRecorder(registry), stubbedUserSecrets());
|
||||
BrandCheckClient client = new BrandCheckClient(props, new ExternalCallMetricsRecorder(registry), stubbedUserSecrets(), new UnreachableCooldown());
|
||||
|
||||
BrandCheckClient.BrandCheckResponse response = client.check("LEGO", "Terms", 7L);
|
||||
|
||||
@@ -264,7 +265,7 @@ class ExternalCallMetricsRecorderTest {
|
||||
int index = i;
|
||||
pool.submit(() -> {
|
||||
SimilarAsinLlmClient client = new SimilarAsinLlmClient(props, objectMapper, recorder,
|
||||
mock(UserSecretUsageService.class));
|
||||
mock(UserSecretUsageService.class), new UnreachableCooldown());
|
||||
try {
|
||||
client.invokeChat("test-model", "system", "prompt-" + index, "test-key");
|
||||
} catch (Exception ignored) {
|
||||
@@ -297,7 +298,7 @@ class ExternalCallMetricsRecorderTest {
|
||||
@Test
|
||||
void test_task_078_payload_metrics_invalid_input_rejected() throws Exception {
|
||||
BrandCheckProperties props = brandProps();
|
||||
BrandCheckClient client = new BrandCheckClient(props, new ExternalCallMetricsRecorder(registry), stubbedUserSecrets());
|
||||
BrandCheckClient client = new BrandCheckClient(props, new ExternalCallMetricsRecorder(registry), stubbedUserSecrets(), new UnreachableCooldown());
|
||||
|
||||
BrandCheckClient.BrandCheckBatchResult result = client.checkAll(null, "Terms", 7L);
|
||||
|
||||
@@ -315,7 +316,7 @@ class ExternalCallMetricsRecorderTest {
|
||||
SimilarAsinProperties props = llmProps();
|
||||
SimilarAsinLlmClient client =
|
||||
new SimilarAsinLlmClient(props, objectMapper, recorder,
|
||||
mock(UserSecretUsageService.class));
|
||||
mock(UserSecretUsageService.class), new UnreachableCooldown());
|
||||
|
||||
// 第一次调用走 500 失败路径,第二次调用恢复成功:错误可恢复
|
||||
llmFailNext.set(true);
|
||||
|
||||
+3
-2
@@ -1,6 +1,7 @@
|
||||
package com.nanri.aiimage.modules.appearancepatent.client;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.nanri.aiimage.common.retry.UnreachableCooldown;
|
||||
import com.nanri.aiimage.config.AppearancePatentProperties;
|
||||
import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentResultRowDto;
|
||||
import com.nanri.aiimage.modules.brand.client.BrandCheckClient;
|
||||
@@ -46,7 +47,7 @@ class AppearancePatentBrandBatchTest {
|
||||
null,
|
||||
brandCheckClient,
|
||||
mock(UserSecretUsageService.class)
|
||||
);
|
||||
, new UnreachableCooldown());
|
||||
|
||||
@Test
|
||||
void mergedBatchAsksBrandCheckOnceAndAttributesPerRow() throws Exception {
|
||||
@@ -115,7 +116,7 @@ class AppearancePatentBrandBatchTest {
|
||||
properties.setLlmRetryTimes(1);
|
||||
properties.setLlmReadTimeoutMillis(5_000);
|
||||
AppearancePatentLlmClient inspectClient = new AppearancePatentLlmClient(
|
||||
properties, new ObjectMapper(), null, brandCheckClient, mock(UserSecretUsageService.class));
|
||||
properties, new ObjectMapper(), null, brandCheckClient, mock(UserSecretUsageService.class), new UnreachableCooldown());
|
||||
|
||||
List<AppearancePatentResultRowDto> rows = inspectClient.inspectRows(
|
||||
List.of(
|
||||
|
||||
+4
-3
@@ -1,6 +1,7 @@
|
||||
package com.nanri.aiimage.modules.appearancepatent.client;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.nanri.aiimage.common.retry.UnreachableCooldown;
|
||||
import com.nanri.aiimage.config.AppearancePatentProperties;
|
||||
import com.nanri.aiimage.config.BrandCheckProperties;
|
||||
import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentResultRowDto;
|
||||
@@ -72,7 +73,7 @@ class AppearancePatentLlmClientHttpTest {
|
||||
null,
|
||||
localBrandCheckClient(),
|
||||
mock(UserSecretUsageService.class)
|
||||
);
|
||||
, new UnreachableCooldown());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -85,7 +86,7 @@ class AppearancePatentLlmClientHttpTest {
|
||||
brand.setRetryTimes(1);
|
||||
brand.setRetryIntervalMillis(0);
|
||||
brand.setTotalTimeoutMillis(5000);
|
||||
return new BrandCheckClient(brand, null, null);
|
||||
return new BrandCheckClient(brand, null, null, new UnreachableCooldown());
|
||||
}
|
||||
|
||||
private void handleBrandCheck(HttpExchange exchange) throws IOException {
|
||||
@@ -290,7 +291,7 @@ class AppearancePatentLlmClientHttpTest {
|
||||
properties.setLlmHost("http://127.0.0.1:" + server.getAddress().getPort());
|
||||
customizer.accept(properties);
|
||||
return new AppearancePatentLlmClient(properties, objectMapper, null,
|
||||
localBrandCheckClient(), mock(UserSecretUsageService.class));
|
||||
localBrandCheckClient(), mock(UserSecretUsageService.class), new UnreachableCooldown());
|
||||
}
|
||||
|
||||
/** 连接层故障短路:网关根本连不上时,重试阶梯必须在证据数处停下,行级拿到可读的真因。 */
|
||||
|
||||
+3
-2
@@ -1,6 +1,7 @@
|
||||
package com.nanri.aiimage.modules.appearancepatent.client;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.nanri.aiimage.common.retry.UnreachableCooldown;
|
||||
import com.nanri.aiimage.config.AppearancePatentProperties;
|
||||
import com.nanri.aiimage.config.BrandCheckProperties;
|
||||
import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentResultRowDto;
|
||||
@@ -68,7 +69,7 @@ class AppearancePatentLlmClientImageDedupeTest {
|
||||
properties.setLlmHost("http://127.0.0.1:" + server.getAddress().getPort());
|
||||
properties.setLlmRetryTimes(1);
|
||||
client = new AppearancePatentLlmClient(properties, objectMapper, null, localBrandCheckClient(),
|
||||
mock(UserSecretUsageService.class));
|
||||
mock(UserSecretUsageService.class), new UnreachableCooldown());
|
||||
}
|
||||
|
||||
private BrandCheckClient localBrandCheckClient() {
|
||||
@@ -77,7 +78,7 @@ class AppearancePatentLlmClientImageDedupeTest {
|
||||
brand.setRetryTimes(1);
|
||||
brand.setRetryIntervalMillis(0);
|
||||
brand.setTotalTimeoutMillis(5000);
|
||||
return new BrandCheckClient(brand, null, null);
|
||||
return new BrandCheckClient(brand, null, null, new UnreachableCooldown());
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
|
||||
+8
-7
@@ -1,6 +1,7 @@
|
||||
package com.nanri.aiimage.modules.appearancepatent.client;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.nanri.aiimage.common.retry.UnreachableCooldown;
|
||||
import com.nanri.aiimage.config.AppearancePatentProperties;
|
||||
import com.nanri.aiimage.config.BrandCheckProperties;
|
||||
import com.nanri.aiimage.modules.brand.client.BrandCheckClient;
|
||||
@@ -26,9 +27,9 @@ class AppearancePatentLlmClientTest {
|
||||
new AppearancePatentProperties(),
|
||||
new ObjectMapper(),
|
||||
null,
|
||||
new BrandCheckClient(new BrandCheckProperties(), null, null),
|
||||
new BrandCheckClient(new BrandCheckProperties(), null, null, new UnreachableCooldown()),
|
||||
mock(UserSecretUsageService.class)
|
||||
);
|
||||
, new UnreachableCooldown());
|
||||
|
||||
@Test
|
||||
void markRowsFailedLeavesUserFacingResultFilledWithReviewMessage() {
|
||||
@@ -123,7 +124,7 @@ class AppearancePatentLlmClientTest {
|
||||
null,
|
||||
brandCheckClient,
|
||||
mock(UserSecretUsageService.class)
|
||||
);
|
||||
, new UnreachableCooldown());
|
||||
AppearancePatentResultRowDto row = new AppearancePatentResultRowDto();
|
||||
row.setId("1");
|
||||
String dataText = """
|
||||
@@ -140,7 +141,7 @@ class AppearancePatentLlmClientTest {
|
||||
|
||||
private static AppearancePatentLlmClient clientWith(BrandCheckClient brandCheckClient) {
|
||||
return new AppearancePatentLlmClient(new AppearancePatentProperties(), new ObjectMapper(),
|
||||
null, brandCheckClient, mock(UserSecretUsageService.class));
|
||||
null, brandCheckClient, mock(UserSecretUsageService.class), new UnreachableCooldown());
|
||||
}
|
||||
|
||||
private static AppearancePatentResultRowDto newRow(String id) {
|
||||
@@ -204,7 +205,7 @@ class AppearancePatentLlmClientTest {
|
||||
null,
|
||||
brandCheckClient,
|
||||
mock(UserSecretUsageService.class)
|
||||
);
|
||||
, new UnreachableCooldown());
|
||||
AppearancePatentResultRowDto row = new AppearancePatentResultRowDto();
|
||||
row.setId("1");
|
||||
String dataText = """
|
||||
@@ -228,7 +229,7 @@ class AppearancePatentLlmClientTest {
|
||||
null,
|
||||
brandCheckClient,
|
||||
mock(UserSecretUsageService.class)
|
||||
);
|
||||
, new UnreachableCooldown());
|
||||
AppearancePatentResultRowDto row = new AppearancePatentResultRowDto();
|
||||
row.setId("1");
|
||||
String dataText = """
|
||||
@@ -251,7 +252,7 @@ class AppearancePatentLlmClientTest {
|
||||
null,
|
||||
brandCheckClient,
|
||||
mock(UserSecretUsageService.class)
|
||||
);
|
||||
, new UnreachableCooldown());
|
||||
AppearancePatentResultRowDto row = new AppearancePatentResultRowDto();
|
||||
row.setId("1");
|
||||
String dataText = """
|
||||
|
||||
+3
-2
@@ -1,6 +1,7 @@
|
||||
package com.nanri.aiimage.modules.appearancepatent.client;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.nanri.aiimage.common.retry.UnreachableCooldown;
|
||||
import com.nanri.aiimage.config.AppearancePatentProperties;
|
||||
import com.nanri.aiimage.config.BrandCheckProperties;
|
||||
import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentResultRowDto;
|
||||
@@ -59,7 +60,7 @@ class AppearancePatentLlmClientTokenUsageTest {
|
||||
AppearancePatentProperties properties = new AppearancePatentProperties();
|
||||
properties.setLlmHost("http://127.0.0.1:" + server.getAddress().getPort());
|
||||
properties.setLlmRetryTimes(1);
|
||||
client = new AppearancePatentLlmClient(properties, objectMapper, null, localBrandCheckClient(), usageService);
|
||||
client = new AppearancePatentLlmClient(properties, objectMapper, null, localBrandCheckClient(), usageService, new UnreachableCooldown());
|
||||
}
|
||||
|
||||
private BrandCheckClient localBrandCheckClient() {
|
||||
@@ -68,7 +69,7 @@ class AppearancePatentLlmClientTokenUsageTest {
|
||||
brand.setRetryTimes(1);
|
||||
brand.setRetryIntervalMillis(0);
|
||||
brand.setTotalTimeoutMillis(5000);
|
||||
return new BrandCheckClient(brand, null, null);
|
||||
return new BrandCheckClient(brand, null, null, new UnreachableCooldown());
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
|
||||
+37
-6
@@ -1,6 +1,7 @@
|
||||
package com.nanri.aiimage.modules.brand.client;
|
||||
|
||||
import com.nanri.aiimage.config.BrandCheckProperties;
|
||||
import com.nanri.aiimage.common.retry.UnreachableCooldown;
|
||||
import com.nanri.aiimage.modules.usersecret.service.UserApiSecretService;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -34,7 +35,7 @@ class BrandCheckClientTest {
|
||||
|
||||
@Test
|
||||
void splitTitleTextSupportsCommonSeparatorsAndQuotes() {
|
||||
BrandCheckClient client = new BrandCheckClient(new BrandCheckProperties(), null, null);
|
||||
BrandCheckClient client = new BrandCheckClient(new BrandCheckProperties(), null, null, new UnreachableCooldown());
|
||||
|
||||
List<String> brands = client.splitTitleText("'阿凡达,任天堂'; Disney、LEGO\nSony");
|
||||
|
||||
@@ -43,13 +44,43 @@ class BrandCheckClientTest {
|
||||
|
||||
@Test
|
||||
void splitTitleTextDeduplicatesBlankValues() {
|
||||
BrandCheckClient client = new BrandCheckClient(new BrandCheckProperties(), null, null);
|
||||
BrandCheckClient client = new BrandCheckClient(new BrandCheckProperties(), null, null, new UnreachableCooldown());
|
||||
|
||||
List<String> brands = client.splitTitleText(" 任天堂, ,任天堂,Sony ");
|
||||
|
||||
assertThat(brands).containsExactly("任天堂", "Sony");
|
||||
}
|
||||
|
||||
@Test
|
||||
void clientLevelCooldownFailsWholeBatchWithoutHittingUpstream() throws Exception {
|
||||
AtomicInteger attempts = new AtomicInteger();
|
||||
HttpServer server = startBrandCheckServer(exchange -> {
|
||||
attempts.incrementAndGet();
|
||||
respond(exchange, "{\"faild_data\":[],\"query_faild_data\":[]}");
|
||||
});
|
||||
try {
|
||||
UnreachableCooldown cooldown = new UnreachableCooldown();
|
||||
BrandCheckProperties props = new BrandCheckProperties();
|
||||
props.setBaseUrl("http://127.0.0.1:" + server.getAddress().getPort());
|
||||
props.setPath("/brand_check");
|
||||
props.setRetryTimes(3);
|
||||
props.setRetryIntervalMillis(1);
|
||||
BrandCheckClient client = new BrandCheckClient(props, null, stubbedUserSecrets(), cooldown);
|
||||
|
||||
cooldown.recordConnectFailure(props.getBaseUrl(), 300_000L);
|
||||
cooldown.recordConnectFailure(props.getBaseUrl(), 300_000L);
|
||||
|
||||
BrandCheckClient.BrandCheckBatchResult result = client.checkAll(List.of("Apple", "Nike"), "Terms", 7L);
|
||||
|
||||
assertThat(attempts.get()).as("冷却期内一个请求都不该发").isZero();
|
||||
assertThat(result.hasQueryFailedData()).isTrue();
|
||||
assertThat(result.queryFaildData()).containsExactly("Apple", "Nike");
|
||||
assertThat(result.reason()).contains("品牌检测服务不可达(连接未建立,非本行数据问题)");
|
||||
} finally {
|
||||
server.stop(0);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void queryFailureRetriesUntilSuccess() throws Exception {
|
||||
AtomicInteger attempts = new AtomicInteger();
|
||||
@@ -242,7 +273,7 @@ class BrandCheckClientTest {
|
||||
props.setRetryTimes(retryTimes);
|
||||
props.setRetryIntervalMillis(retryIntervalMillis);
|
||||
props.setTotalTimeoutMillis(totalTimeoutMillis);
|
||||
return new BrandCheckClient(props, null, stubbedUserSecrets());
|
||||
return new BrandCheckClient(props, null, stubbedUserSecrets(), new UnreachableCooldown());
|
||||
}
|
||||
|
||||
private BrandCheckClient newClient(HttpServer server, int retryTimes) {
|
||||
@@ -251,7 +282,7 @@ class BrandCheckClientTest {
|
||||
props.setPath("/brand_check");
|
||||
props.setRetryTimes(retryTimes);
|
||||
props.setRetryIntervalMillis(1);
|
||||
return new BrandCheckClient(props, null, stubbedUserSecrets());
|
||||
return new BrandCheckClient(props, null, stubbedUserSecrets(), new UnreachableCooldown());
|
||||
}
|
||||
|
||||
/** 重试阶梯可调的构造:用于验证「耗时预算掐的是重试,不是首轮」。 */
|
||||
@@ -262,7 +293,7 @@ class BrandCheckClientTest {
|
||||
props.setRetryTimes(retryTimes);
|
||||
props.setRetryIntervalMillis(retryIntervalMillis);
|
||||
props.setTotalTimeoutMillis(totalTimeoutMillis);
|
||||
return new BrandCheckClient(props, null, stubbedUserSecrets());
|
||||
return new BrandCheckClient(props, null, stubbedUserSecrets(), new UnreachableCooldown());
|
||||
}
|
||||
|
||||
/** 有真实 HTTP 断言的用例必须桩出非空代理,否则客户端零请求短路、阶梯与指标全测不到。 */
|
||||
@@ -361,7 +392,7 @@ class BrandCheckClientTest {
|
||||
props.setRetryIntervalMillis(1);
|
||||
props.setReadTimeoutMillis(1_000);
|
||||
props.setTotalTimeoutMillis(0);
|
||||
BrandCheckClient client = new BrandCheckClient(props, null, stubbedUserSecrets());
|
||||
BrandCheckClient client = new BrandCheckClient(props, null, stubbedUserSecrets(), new UnreachableCooldown());
|
||||
|
||||
ExecutorService pool = (ExecutorService) ReflectionTestUtils.getField(client, "checkExecutor");
|
||||
CountDownLatch occupied = new CountDownLatch(3);
|
||||
|
||||
+2
-1
@@ -1,6 +1,7 @@
|
||||
package com.nanri.aiimage.modules.brand.client;
|
||||
|
||||
import com.nanri.aiimage.config.BrandCheckProperties;
|
||||
import com.nanri.aiimage.common.retry.UnreachableCooldown;
|
||||
import com.nanri.aiimage.modules.usersecret.service.UserApiSecretService;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -137,7 +138,7 @@ class BrandCheckClientTitleBatchTest {
|
||||
props.setRetryIntervalMillis(1);
|
||||
UserApiSecretService secrets = mock(UserApiSecretService.class);
|
||||
when(secrets.findPlainValue(anyLong(), anyString())).thenReturn("http://user:pass@127.0.0.1:9");
|
||||
return new BrandCheckClient(props, null, secrets);
|
||||
return new BrandCheckClient(props, null, secrets, new UnreachableCooldown());
|
||||
}
|
||||
|
||||
private HttpServer startBrandCheckServer(com.sun.net.httpserver.HttpHandler handler) throws IOException {
|
||||
|
||||
+49
-1
@@ -1,6 +1,7 @@
|
||||
package com.nanri.aiimage.modules.similarasin.client;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.nanri.aiimage.common.retry.UnreachableCooldown;
|
||||
import com.nanri.aiimage.config.SimilarAsinProperties;
|
||||
import com.nanri.aiimage.modules.usersecret.service.UserSecretUsageService;
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
@@ -121,10 +122,57 @@ class SimilarAsinLlmClientHttpTest {
|
||||
assertThat(requestCount.get()).isEqualTo(2);
|
||||
}
|
||||
|
||||
/** B24:首次调用确认不可达即进客户端级冷却,冷却期内的后续调用零探针快速失败。 */
|
||||
@Test
|
||||
void cooldownMakesSubsequentCallsFailFastWithoutProbes() {
|
||||
SimilarAsinLlmClient deadHostClient = buildClient(p -> p.setLlmHost("http://127.0.0.1:1"));
|
||||
|
||||
assertThatThrownBy(() -> deadHostClient.invokeChat("test-model", "s", "u", "test-key"))
|
||||
.hasMessageContaining("LLM 服务不可达(连接未建立,非本行数据问题)");
|
||||
assertThat(lastCooldown.isCoolingDown(lastHost, lastWindow)).isTrue();
|
||||
|
||||
// 快速失败走的是入口冷却分支(";lastErr=冷却中"后缀只有该路径会拼),一次探针都不发
|
||||
assertThatThrownBy(() -> deadHostClient.invokeChat("test-model", "s", "u", "test-key"))
|
||||
.hasMessageContaining("LLM 服务不可达(连接未建立,非本行数据问题)")
|
||||
.hasMessageContaining(";lastErr=冷却中");
|
||||
}
|
||||
|
||||
/** B24:冷却到期后下一条真实调用兼作探针;探针仍连不上 ⇒ 立即重进冷却。 */
|
||||
@Test
|
||||
void expiredCooldownProbesAgainAndReentersOnFailure() throws InterruptedException {
|
||||
SimilarAsinLlmClient deadHostClient = buildClient(p -> {
|
||||
p.setLlmHost("http://127.0.0.1:1");
|
||||
p.setLlmUnreachableCooldownMillis(200);
|
||||
});
|
||||
assertThatThrownBy(() -> deadHostClient.invokeChat("test-model", "s", "u", "test-key"))
|
||||
.hasMessageContaining("LLM 服务不可达(连接未建立,非本行数据问题)");
|
||||
|
||||
Thread.sleep(400);
|
||||
assertThat(lastCooldown.isCoolingDown(lastHost, 200)).isFalse();
|
||||
|
||||
// 探针真发:lastErr 带的是连接异常而不是"冷却中";失败即重进
|
||||
assertThatThrownBy(() -> deadHostClient.invokeChat("test-model", "s", "u", "test-key"))
|
||||
.hasMessageContaining("LLM 服务不可达(连接未建立,非本行数据问题)")
|
||||
.hasMessageNotContaining("冷却中");
|
||||
assertThat(lastCooldown.isCoolingDown(lastHost, 200)).isTrue();
|
||||
|
||||
// 重进之后的调用回到零探针快速失败
|
||||
assertThatThrownBy(() -> deadHostClient.invokeChat("test-model", "s", "u", "test-key"))
|
||||
.hasMessageContaining(";lastErr=冷却中");
|
||||
}
|
||||
|
||||
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));
|
||||
lastCooldown = new UnreachableCooldown();
|
||||
lastHost = properties.getLlmHost();
|
||||
lastWindow = Math.max(0, properties.getLlmUnreachableCooldownMillis());
|
||||
return new SimilarAsinLlmClient(properties, objectMapper, null, mock(UserSecretUsageService.class), lastCooldown);
|
||||
}
|
||||
|
||||
/** 最近一次 buildClient 的冷却表与 host/窗长:冷却态断言直接读同一实例,不靠时间猜。 */
|
||||
private UnreachableCooldown lastCooldown;
|
||||
private String lastHost;
|
||||
private long lastWindow;
|
||||
}
|
||||
|
||||
+2
-1
@@ -1,6 +1,7 @@
|
||||
package com.nanri.aiimage.modules.similarasin.service;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.nanri.aiimage.common.retry.UnreachableCooldown;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.nanri.aiimage.config.OssProperties;
|
||||
import com.nanri.aiimage.config.SimilarAsinProperties;
|
||||
@@ -61,7 +62,7 @@ public class SimilarAsinLlmLocalVerify {
|
||||
props.setLlmRowConcurrency(2);
|
||||
props.setLlmImageDownloadTimeoutSeconds(10);
|
||||
|
||||
SimilarAsinLlmClient client = new SimilarAsinLlmClient(props, objectMapper, null, null);
|
||||
SimilarAsinLlmClient client = new SimilarAsinLlmClient(props, objectMapper, null, null, new UnreachableCooldown());
|
||||
OssProperties ossProps = new OssProperties();
|
||||
ossProps.setEndpoint("https://oss.aishufu.top");
|
||||
ossProps.setPublicEndpoint("https://oss.aishufu.top");
|
||||
|
||||
Reference in New Issue
Block a user