feat(retry): B24 冷却扩围到「不回包/解密失败成片」两形

连接层冷却只认「会话没建立」,而现网 B26 的成片失败是连接已建立的两种形状:
对端在给出响应前收连接、HTTP 200 里行级结论带解密失败。给冷却表加第二条
证据道(同 host 分格、同窗长、同阈值 2),品牌客户端把两形都记进这条道:
不回包按异常即时记证;解密形状先批内开票,批终态只在零可结算结论时按票
记证——一枚干净结论就整票作废,脏品牌名的行级差异证据面保持不变。
冷却期快失败沿用既有整批分支,归因文案单独一条以便交付侧与取证区分。
This commit is contained in:
2026-10-26 14:01:26 +08:00
parent 1d4b4decf3
commit a3f765c458
4 changed files with 390 additions and 9 deletions
@@ -74,4 +74,82 @@ class UnreachableCooldownTest {
assertThat(UnreachableCooldown.isConnectFailure(new RuntimeException("read timeout"))).isFalse();
assertThat(UnreachableCooldown.isConnectFailure(null)).isFalse();
}
// ---- no-answer 道(B24 扩围,2026-10-26 拍板):阈值/窗长/到期语义与 connect 道同款 ----
@Test
void twoNoAnswerFailuresEnterCooldownOneDoesNot() {
String host = "http://16890.example";
assertThat(cooldown.recordNoAnswerFailure(host, WINDOW)).isFalse();
assertThat(cooldown.isCoolingDown(host, WINDOW)).isFalse();
assertThat(cooldown.recordNoAnswerFailure(host, WINDOW)).isTrue();
assertThat(cooldown.noAnswerLaneCooling(host, WINDOW)).isTrue();
assertThat(cooldown.isCoolingDown(host, WINDOW)).isTrue();
}
@Test
void noAnswerWindowExpiresAndSingleFailureReenters() throws InterruptedException {
String host = "http://expire-16890.example";
cooldown.recordNoAnswerFailure(host, 50);
cooldown.recordNoAnswerFailure(host, 50);
assertThat(cooldown.noAnswerLaneCooling(host, 50)).isTrue();
Thread.sleep(120);
assertThat(cooldown.noAnswerLaneCooling(host, 50)).isFalse();
// 历史证据不清零:到期后批次再判成片,一次即重进(与 connect 道同拍板口径)
assertThat(cooldown.recordNoAnswerFailure(host, 50)).isTrue();
assertThat(cooldown.noAnswerLaneCooling(host, 50)).isTrue();
}
@Test
void clearNoAnswerEvidenceRequiresTwoFreshFailures() {
String host = "http://clear.example";
cooldown.recordNoAnswerFailure(host, WINDOW);
cooldown.clearNoAnswerEvidence(host);
assertThat(cooldown.recordNoAnswerFailure(host, WINDOW)).isFalse();
assertThat(cooldown.noAnswerLaneCooling(host, WINDOW)).isFalse();
}
@Test
void clearOnlyTouchesNoAnswerEvidenceAndNeverAnOpenedWindow() {
String host = "http://window-stays.example";
cooldown.recordNoAnswerFailure(host, WINDOW);
cooldown.recordNoAnswerFailure(host, WINDOW);
// 已开的窗不因清零提前放行:快失败只按窗长自然到期(connect 道同语义)
cooldown.clearNoAnswerEvidence(host);
assertThat(cooldown.noAnswerLaneCooling(host, WINDOW)).isTrue();
// 到期后靠那一次成片失败重进:清零历史后证据要重新攒满两条,这里只缺一条
cooldown.clearNoAnswerEvidence(host);
assertThat(cooldown.recordNoAnswerFailure(host, WINDOW)).isFalse();
}
@Test
void zeroWindowDisablesNoAnswerLaneToo() {
for (int i = 0; i < 5; i++) {
assertThat(cooldown.recordNoAnswerFailure("http://off.example", 0)).isFalse();
}
assertThat(cooldown.noAnswerLaneCooling("http://off.example", 0)).isFalse();
}
@Test
void connectLaneAndNoAnswerLaneAreSeparateAccounts() {
String host = "http://lanes.example";
cooldown.recordConnectFailure(host, WINDOW);
assertThat(cooldown.noAnswerLaneCooling(host, WINDOW)).isFalse();
cooldown.recordNoAnswerFailure(host, WINDOW);
// 两条道各计各的:一条 connect + 一条 no-answer 都不到阈值
assertThat(cooldown.connectLaneCooling(host, WINDOW)).isFalse();
assertThat(cooldown.noAnswerLaneCooling(host, WINDOW)).isFalse();
cooldown.recordConnectFailure(host, WINDOW);
assertThat(cooldown.connectLaneCooling(host, WINDOW)).isTrue();
assertThat(cooldown.noAnswerLaneCooling(host, WINDOW)).isFalse();
// recordConnected 只清 connect 证据;no-answer 证据必须活过它
cooldown.recordConnected(host);
assertThat(cooldown.recordNoAnswerFailure(host, WINDOW)).isTrue();
assertThat(cooldown.noAnswerLaneCooling(host, WINDOW)).isTrue();
assertThat(cooldown.connectLaneCooling(host, WINDOW)).isTrue();
}
}
@@ -81,6 +81,161 @@ class BrandCheckClientTest {
}
}
// ---- B24 扩围(2026-10-26 拍板):冷却纳入「连上了不回包」与「解密失败成片」两形 ----
/**
* 「连上了但不回包」(B26 主形 {@code header parser received no bytes})按次记进 no-answer 道:
* 首批两条阶梯证据把窗打开,第二批整批快失败、零请求。行级结论仍是查询失败,
* 变的只有归因与耗时。
*/
@Test
void peerClosedWithoutResponseOpensCooldownAndNextBatchHitsZeroProbes() throws Exception {
AtomicInteger connections = new AtomicInteger();
ServerSocket listener = new ServerSocket(0, 16, java.net.InetAddress.getByName("127.0.0.1"));
Thread closer = new Thread(() -> {
while (!listener.isClosed()) {
try (java.net.Socket socket = listener.accept()) {
connections.incrementAndGet();
// 不读不写直接关:JDK HttpClient 在响应解析器处拿到 0 字节
} catch (IOException ignored) {
return;
}
}
}, "no-bytes-closer");
closer.setDaemon(true);
closer.start();
try {
UnreachableCooldown cooldown = new UnreachableCooldown();
BrandCheckProperties props = new BrandCheckProperties();
props.setBaseUrl("http://127.0.0.1:" + listener.getLocalPort());
props.setPath("/brand_check");
props.setRetryTimes(2);
props.setRetryIntervalMillis(1);
props.setUnreachableCooldownMillis(300_000);
BrandCheckClient client =
new BrandCheckClient(props, null, stubbedUserSecrets(), cooldown);
BrandCheckClient.BrandCheckBatchResult first =
client.checkAll(List.of("Apple"), "Terms", 7L);
assertThat(connections.get()).as("首批两条阶梯都真发(no-answer 不改本批重试形状)").isGreaterThanOrEqualTo(2);
assertThat(cooldown.noAnswerLaneCooling(props.getBaseUrl(), 300_000L)).isTrue();
assertThat(first.reason()).doesNotContain("解密链路成片失败");
int connectionsBefore = connections.get();
BrandCheckClient.BrandCheckBatchResult second =
client.checkAll(List.of("Nike"), "Terms", 7L);
assertThat(connections.get()).as("冷却期内一个请求都不该发").isEqualTo(connectionsBefore);
assertThat(second.queryFaildData()).containsExactly("Nike");
assertThat(second.reason()).contains("品牌检测服务不可达(解密链路成片失败,非本行数据问题)");
} finally {
listener.close();
}
}
/**
* 「解密失败成片」在批终态按票记证(阈值 2 与 connect 道同数字):一批两枚脏名真探
* (上游标了不可重试,白等已被既有短路掐住)即开窗,第二批准一个请求不发。
*/
@Test
void decryptShapedBatchOpensCooldownAndNextBatchHitsZeroProbes() throws Exception {
AtomicInteger requests = new AtomicInteger();
HttpServer server = startBrandCheckServer(exchange -> {
requests.incrementAndGet();
respond(exchange, "{\"faild_data\":[],\"query_faild_data\":[\"x\"],"
+ "\"reason\":\"查询或解密失败: 解密失败: Malformed UTF-8 data\",\"retryable\":false}");
});
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(2);
props.setRetryIntervalMillis(1);
props.setUnreachableCooldownMillis(300_000);
BrandCheckClient client =
new BrandCheckClient(props, null, stubbedUserSecrets(), cooldown);
client.checkAll(List.of("Small)"), "Terms", 7L);
assertThat(cooldown.noAnswerLaneCooling(props.getBaseUrl(), 300_000L))
.as("一批只有一枚解密票:不到阈值不进冷却").isFalse();
client.checkAll(List.of("XX-Large)", "Small)"), "Terms", 7L);
assertThat(requests.get()).as("两批真发合计至少三枚请求").isGreaterThanOrEqualTo(3);
assertThat(cooldown.noAnswerLaneCooling(props.getBaseUrl(), 300_000L))
.as("零干净批次攒到两条解密票:进入冷却").isTrue();
int requestsBefore = requests.get();
BrandCheckClient.BrandCheckBatchResult cooled =
client.checkAll(List.of("4\" Width x 2\" Height)"), "Terms", 7L);
assertThat(requests.get()).as("冷却期内一个请求都不该发").isEqualTo(requestsBefore);
assertThat(cooled.reason()).contains("品牌检测服务不可达(解密链路成片失败,非本行数据问题)");
} finally {
server.stop(0);
}
}
/**
* 反向对照(行级差异必须保留):脏品牌名混在干净品牌里=本行数据问题,不是服务在掉。
* 零干净结论的条件不成立 ⇒ 永远不进冷却,各品牌照旧各查各的。
*/
@Test
void mixedDecryptFailuresWithCleanBrandsNeverEnterCooldown() throws Exception {
AtomicInteger decryptRequests = new AtomicInteger();
HttpServer server = startBrandCheckServer(exchange -> {
String body = new String(exchange.getRequestBody().readAllBytes(), StandardCharsets.UTF_8);
if (body.contains("Small")) {
decryptRequests.incrementAndGet();
respond(exchange, "{\"faild_data\":[],\"query_faild_data\":[\"Small)\"],"
+ "\"reason\":\"查询或解密失败: 解密失败: Malformed UTF-8 data\"}");
} else {
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(2);
props.setRetryIntervalMillis(1);
props.setUnreachableCooldownMillis(300_000);
BrandCheckClient client =
new BrandCheckClient(props, null, stubbedUserSecrets(), cooldown);
client.checkAll(List.of("Nike", "Small)"), "Terms", 7L);
client.checkAll(List.of("Apple", "Small)"), "Terms", 7L);
assertThat(cooldown.noAnswerLaneCooling(props.getBaseUrl(), 300_000L)).isFalse();
assertThat(decryptRequests.get())
.as("混批里脏名品牌照旧逐行真查(行级证据面不变)").isGreaterThanOrEqualTo(4);
} finally {
server.stop(0);
}
}
/** 两个形状判定的口径钉死:不回包只认那两句原文,读超时不算;解密形状认现网三形。 */
@Test
void noAnswerPredicatesPinTheLiveShapes() {
assertThat(BrandCheckClient.isPeerClosedWithoutResponse(new ResourceAccessException(
"I/O error on POST request for \"http://47.110.241.161:16890/brand_check\"",
new java.io.IOException("HTTP/1.1 header parser received no bytes")))).isTrue();
assertThat(BrandCheckClient.isPeerClosedWithoutResponse(
new java.io.IOException("EOF reached while reading"))).isTrue();
assertThat(BrandCheckClient.isPeerClosedWithoutResponse(
new SocketTimeoutException("Read timed out"))).isFalse();
assertThat(BrandCheckClient.isPeerClosedWithoutResponse(
new HttpTimeoutException("request timed out"))).isFalse();
assertThat(BrandCheckClient.isPeerClosedWithoutResponse(null)).isFalse();
assertThat(BrandCheckClient.isDecryptFailure("查询或解密失败: 解密失败: Malformed UTF-8 data")).isTrue();
assertThat(BrandCheckClient.isDecryptFailure("解密失败: 解密失败: Malformed UTF-8 data")).isTrue();
assertThat(BrandCheckClient.isDecryptFailure(
"查询或解密失败: Expecting value: line 1 column 1 (char 0)")).isTrue();
assertThat(BrandCheckClient.isDecryptFailure("有 2~3 行抓取失败(未取得商品页数据):B07P5TR7Q5")).isFalse();
assertThat(BrandCheckClient.isDecryptFailure("")).isFalse();
assertThat(BrandCheckClient.isDecryptFailure(null)).isFalse();
}
@Test
void pausedAccountStopsBatchWithoutHittingUpstream() throws Exception {
AtomicInteger attempts = new AtomicInteger();