fix(货源): 主图转存对瞬时 TLS 重置做有界重试,失败行日志带上真实原因

生产取证:主机A 近 3 小时 252 条 `main download fail` 全是 m.media-amazon.com 的
"Remote host terminated the handshake",而这些 URL 事后重取 8 条里 7 条直接 200。
旧实现单发、握手被 RST 就 return null 并丢掉真实异常,导致整行白占一轮重发。

新增 aiimage.similar-asin.llm-image-download-retry-times(默认额外 2 次,置 0 退回旧行为),
只重试连接级异常,非 200 不重试;25 秒总预算防慢源放大尾延迟;重试耗尽抛 IOException
让行失败原因上行到日志。
This commit is contained in:
2026-09-28 11:52:11 +08:00
parent 9efb39b0a5
commit 3a39c36bde
5 changed files with 439 additions and 21 deletions
@@ -0,0 +1,226 @@
package com.nanri.aiimage.modules.similarasin.service;
import com.nanri.aiimage.config.OssProperties;
import com.nanri.aiimage.config.SimilarAsinProperties;
import com.nanri.aiimage.modules.file.service.oss.OssStorageService;
import com.nanri.aiimage.modules.productcategory.service.ProductCategoryService;
import com.nanri.aiimage.modules.similarasin.client.SimilarAsinLlmClient;
import com.nanri.aiimage.modules.similarasin.util.PuzzleImageMerger;
import com.nanri.aiimage.modules.similarasin.util.SimilarAsinImageEmbedder;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* 主图下载重试语义。动因是 2026-09-28 生产取证:主机A 近 3 小时 252 条
* "err=Remote host terminated the handshake" 全部来自 m.media-amazon.com,
* 而这些 URL 事后能直接取到 —— 旧实现一次都不重试,整行因此被判失败并重发。
*/
class SimilarAsinMainImageDownloadRetryTest {
private static final String URL = "https://m.media-amazon.com/images/I/61Yh2j3VZmL._AC_SX679_.jpg";
private static final String RESET = "Remote host terminated the handshake";
private static final byte[] BODY = new byte[]{1, 2, 3, 4, 5, 6, 7, 8};
@AfterEach
void clearInterruptFlag() {
// 中断用例会把标志位留在测试线程上,不清会污染后续测试。
Thread.interrupted();
}
private SimilarAsinLlmService service(SimilarAsinProperties props, HttpClient client) {
SimilarAsinLlmService svc = new SimilarAsinLlmService(
mock(SimilarAsinLlmClient.class),
props,
mock(OssProperties.class),
mock(ProductCategoryService.class),
mock(PuzzleImageMerger.class),
mock(OssStorageService.class),
mock(SimilarAsinImageEmbedder.class));
svc.setDownloadHttpClientForTest(client);
return svc;
}
private Object invokeDownload(SimilarAsinLlmService svc, String url) throws Throwable {
Method m = SimilarAsinLlmService.class.getDeclaredMethod("downloadBytes", String.class);
m.setAccessible(true);
try {
return m.invoke(svc, url);
} catch (InvocationTargetException ex) {
throw ex.getCause();
}
}
/** 前 {@code failTimes} 次连接级异常,之后返回 200。 */
private HttpClient flakyClient(int failTimes, AtomicInteger calls) {
HttpClient client = mock(HttpClient.class);
try {
when(client.send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class)))
.thenAnswer(invocation -> {
int n = calls.incrementAndGet();
if (n <= failTimes) {
throw new IOException(RESET);
}
return new StubResponse(200, BODY);
});
} catch (Exception ex) {
throw new IllegalStateException(ex);
}
return client;
}
@Test
void 抖动两次后第三次成功_不再丢整行() throws Throwable {
AtomicInteger calls = new AtomicInteger();
SimilarAsinLlmService svc = service(new SimilarAsinProperties(), flakyClient(2, calls));
byte[] bytes = (byte[]) invokeDownload(svc, URL);
assertArrayEquals(BODY, bytes);
assertEquals(3, calls.get(), "默认额外重试 2 次 → 第三次应成功");
}
@Test
void 重试耗尽后抛出带真实原因的IOException() throws Throwable {
AtomicInteger calls = new AtomicInteger();
SimilarAsinLlmService svc = service(new SimilarAsinProperties(), flakyClient(99, calls));
IOException ex = assertThrows(IOException.class, () -> invokeDownload(svc, URL));
assertTrue(ex.getMessage().contains(RESET), "要把真实异常带出来,不能再塌成一句「主图转存失败」");
assertEquals(3, calls.get());
}
@Test
void 源站明确回非200时不重试() throws Throwable {
AtomicInteger calls = new AtomicInteger();
HttpClient client = mock(HttpClient.class);
try {
when(client.send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class)))
.thenAnswer(invocation -> {
calls.incrementAndGet();
return new StubResponse(403, new byte[0]);
});
} catch (Exception ex) {
throw new IllegalStateException(ex);
}
assertNull(invokeDownload(service(new SimilarAsinProperties(), client), URL));
assertEquals(1, calls.get(), "403 是确定答复,重试只是白等超时");
}
@Test
void retryTimes置0退回单次尝试旧行为() throws Throwable {
SimilarAsinProperties props = new SimilarAsinProperties();
props.setLlmImageDownloadRetryTimes(0);
AtomicInteger calls = new AtomicInteger();
IOException ex = assertThrows(IOException.class,
() -> invokeDownload(service(props, flakyClient(99, calls)), URL));
assertTrue(ex.getMessage().contains(RESET));
assertEquals(1, calls.get());
}
@Test
void 线程被中断时立刻停止重试() throws Throwable {
AtomicInteger calls = new AtomicInteger();
HttpClient client = mock(HttpClient.class);
try {
when(client.send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class)))
.thenAnswer(invocation -> {
calls.incrementAndGet();
Thread.currentThread().interrupt();
throw new IOException(RESET);
});
} catch (Exception ex) {
throw new IllegalStateException(ex);
}
assertThrows(IOException.class, () -> invokeDownload(service(new SimilarAsinProperties(), client), URL));
assertEquals(1, calls.get(), "中断后不该再排下一次重试");
assertTrue(Thread.currentThread().isInterrupted(), "中断标志必须回设,不能吞掉");
}
@Test
void 空URL仍按取不到处理不抛异常() throws Throwable {
HttpClient client = mock(HttpClient.class);
SimilarAsinLlmService svc = service(new SimilarAsinProperties(), client);
assertNull(invokeDownload(svc, " "));
assertNull(invokeDownload(svc, null));
verify(client, never()).send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class));
}
private static final class StubResponse implements HttpResponse<byte[]> {
private final int statusCode;
private final byte[] body;
private StubResponse(int statusCode, byte[] body) {
this.statusCode = statusCode;
this.body = body;
}
@Override
public int statusCode() {
return statusCode;
}
@Override
public HttpRequest request() {
return Mockito.mock(HttpRequest.class);
}
@Override
public java.util.Optional<HttpResponse<byte[]>> previousResponse() {
return java.util.Optional.empty();
}
@Override
public java.net.http.HttpHeaders headers() {
return java.net.http.HttpHeaders.of(java.util.Map.of(), (a, b) -> true);
}
@Override
public byte[] body() {
return body;
}
@Override
public java.net.URI uri() {
return java.net.URI.create(URL);
}
@Override
public HttpClient.Version version() {
return HttpClient.Version.HTTP_1_1;
}
@Override
public java.util.Optional<javax.net.ssl.SSLSession> sslSession() {
return java.util.Optional.empty();
}
}
}