fix(brand-check): 连接未建立即收口整批重试阶梯,并留 SERVICE_UNREACHABLE 锚点

现网读数(主机B java-server,窗口 10-21 07:05→10-22 18:26):B→A:16890 自 10-21
16:30:14 起一次也没通过(A 侧 journal 里来自 121.196.149.225 的调用 10-20 1283 次、
10-21 2033 次、10-22 零次;B17 摘掉兜底 DROP 只恢复了 80/443,16890 从没在放行清单里),
而这一侧的日志是 请求异常将重试 err=I/O error … 16890 **14,944 条**、
重试耗尽判定查询失败 同因 **7,667 条**——每个品牌都要烧满 10 轮(约 45~65 秒)才知道
同一个"连不上",而外观专利正在 B 上跑批(taskId 31891),交付表因此成片「商标查询失败」。

改的是白等,不是结论:批内连续两次连接未建立且中间没有任何一次成功 ⇒ 其余品牌不再重试,
每个品牌仍真发得出它那一次首轮请求(与既有 upstreamRejecting 同一条保守口径),
判失败的行一字不变,只是 reason 从空串写成「品牌检测服务不可达(连接未建立,非本行数据问题)」。
isConnectFailure 只认 ConnectException/NoRouteToHostException/UnknownHostException
(顺着 cause 链,Spring 的 ResourceAccessException 把真凶挂在下面,JDK 的
HttpConnectTimeoutException 本就是 ConnectException 子类);读超时与 5xx 故意不算——
那是"连上了但这次没答好",重试救得回来。

用例 2 支:确定性判据用真闭环(指向一个确定没人监听的端口)——变异验证把证据阈值改成
1000000(等于不短路)后该用例判红、实测 38.64s,收口后 <5s;另一支钉 predicate 的
正例/反例(含 null 与别处的 500)。后端面整量 4691 例/0 红/0 错/17 skip/BUILD SUCCESS。
This commit is contained in:
2026-10-22 19:19:38 +08:00
parent 16643f6912
commit 96315fe302
2 changed files with 154 additions and 2 deletions
@@ -8,7 +8,13 @@ import org.springframework.test.util.ReflectionTestUtils;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.InetSocketAddress;
import java.net.NoRouteToHostException;
import java.net.ServerSocket;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.net.http.HttpTimeoutException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.concurrent.CountDownLatch;
@@ -16,6 +22,8 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.springframework.web.client.ResourceAccessException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
@@ -167,6 +175,76 @@ class BrandCheckClientTest {
}
}
/**
* 连接根本没建立时,重试阶梯救不回来 —— 2026-10-22 现网那一刀的形状。
*
* <p>主机B→主机A:16890 被安全组挡死(B17 那次摘 DROP 只恢复了 80/443),35 小时窗口里
* {@code 请求异常将重试} 14,944 次、{@code 重试耗尽判定查询失败} 7,667 次,而外观专利
* 正在 B 上跑批:每个品牌烧满 10 轮(约 45~65 秒)只为了得到同一个「连不上」。
* 收口的判据是证据条数(批内连续两次连接失败、中间没有任何一次成功),
* 结论一字不变——每条品牌仍全部判查询失败,只是不再白等,且 reason 写明是服务不可达。
*/
@Test
void connectRefusalsStopTheWholeBatchFromBurningRetryLadders() throws Exception {
BrandCheckClient client = newClientToClosedPort(10, 300, 60_000);
List<String> brands = List.of("b1", "b2", "b3", "b4", "b5", "b6");
long startedAt = System.nanoTime();
BrandCheckClient.BrandCheckBatchResult result = client.checkAll(brands, "Terms", 60_000L, 7L);
long elapsedMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startedAt);
assertThat(result.queryFaildData()).as("短路只省白等,不改结论:6 个品牌仍全部判查询失败")
.hasSize(brands.size());
assertThat(result.reason()).as("归因必须写明「服务没连上」,不许再留空 reason").contains("品牌检测服务不可达");
// 不短路时:6 品牌按并发 3 分两波,每波阶梯 300×(1+…+9)≈13.5 秒 ≈ 27 秒;
// 判据取 5 秒——远低于"烧满"的最坏读数,又给机器负载留了十倍余量。
assertThat(elapsedMillis).as("连接层证据到两次即收口,实测耗时=%dms", elapsedMillis)
.isLessThan(5_000L);
}
/**
* 判据必须只认"会话没建立"这一类。读超时与 5xx 是"连上了但这次没答好",
* 把它们算进不可达证据,一次上游抖动就会掐掉同批别的品牌本该拿到的重试。
*/
@Test
void connectFailurePredicateSeparatesNoConnectionFromBadAnswer() {
assertThat(BrandCheckClient.isConnectFailure(
new ConnectException("Connection refused: connect"))).isTrue();
assertThat(BrandCheckClient.isConnectFailure(
new UnknownHostException("brand.aishufu.top"))).isTrue();
assertThat(BrandCheckClient.isConnectFailure(new NoRouteToHostException("no route"))).isTrue();
// Spring 把真凶挂在 cause 上,现网日志读到的就是外层那句 I/O error
assertThat(BrandCheckClient.isConnectFailure(new ResourceAccessException(
"I/O error on POST request for \"http://127.0.0.1:16890/brand_check\"",
new ConnectException("Connection refused")))).isTrue();
assertThat(BrandCheckClient.isConnectFailure(
new SocketTimeoutException("Read timed out"))).isFalse();
assertThat(BrandCheckClient.isConnectFailure(
new HttpTimeoutException("request timed out"))).isFalse();
assertThat(BrandCheckClient.isConnectFailure(
new IllegalStateException("500 Internal Server Error"))).isFalse();
assertThat(BrandCheckClient.isConnectFailure(null)).isFalse();
}
/** 一个确定没人监听的端口:connect 当场被拒,正是"服务不在了"而不是"服务很慢"。 */
private static int closedPort() throws IOException {
try (ServerSocket socket = new ServerSocket(0)) {
return socket.getLocalPort();
}
}
private BrandCheckClient newClientToClosedPort(int retryTimes, int retryIntervalMillis,
int totalTimeoutMillis) throws IOException {
BrandCheckProperties props = new BrandCheckProperties();
props.setBaseUrl("http://127.0.0.1:" + closedPort());
props.setPath("/brand_check");
props.setRetryTimes(retryTimes);
props.setRetryIntervalMillis(retryIntervalMillis);
props.setTotalTimeoutMillis(totalTimeoutMillis);
return new BrandCheckClient(props, null, stubbedUserSecrets());
}
private BrandCheckClient newClient(HttpServer server, int retryTimes) {
BrandCheckProperties props = new BrandCheckProperties();
props.setBaseUrl("http://127.0.0.1:" + server.getAddress().getPort());