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
@@ -16,6 +16,7 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
@@ -58,6 +59,9 @@ public class SimilarAsinLlmService {
private static final String STATUS_NOT_EXISTS = "不存在";
private static final String NO_MATCH = "无";
/** 单行主图下载(含重试)的耗时上限,超过就带着最后一次异常退出重试。 */
private static final long MAIN_DOWNLOAD_BUDGET_MILLIS = 25_000L;
/**
* 一级/二级类目合并匹配系统提示词(工作流 109409/1544733 两次调用合一):
* 候选以「一级 → 其下二级」的两层清单一次给出,ID 字段仍不进输出契约
@@ -318,7 +322,13 @@ public class SimilarAsinLlmService {
row.setMainUrl("");
return;
}
String stored = uploadExternalUrlToSupplyImages(primaryUrl);
String stored;
try {
stored = uploadExternalUrlToSupplyImages(primaryUrl);
} catch (IOException ex) {
throw new IllegalStateException("主图转存失败: " + abbreviate(primaryUrl, 160)
+ " 原因: " + failureMessage(ex), ex);
}
if (stored == null) {
throw new IllegalStateException("主图转存失败: " + abbreviate(primaryUrl, 160));
}
@@ -499,8 +509,12 @@ public class SimilarAsinLlmService {
return ossStorageService.getPublicUrl(objectKey);
}
/** 下载外部图片并转存到 MinIO supply_images,失败返回 null。 */
private String uploadExternalUrlToSupplyImages(String url) {
/**
* 下载外部图片并转存到 MinIO supply_images。
* 返回 null="这张图确实取不到"(URL 为空或源站给了非 200);连接级失败重试耗尽后抛 IOException,
* 让行失败原因带上真实错误(旧实现只回 null,日志里塌成一句"主图转存失败: <url>",看不出是可重试的抖动)。
*/
private String uploadExternalUrlToSupplyImages(String url) throws IOException {
byte[] bytes = downloadBytes(url);
if (bytes == null) {
return null;
@@ -534,30 +548,66 @@ public class SimilarAsinLlmService {
return raw;
}
private byte[] downloadBytes(String url) {
/**
* 主图下载:连接级异常按 {@code aiimage.similar-asin.llm-image-download-retry-times} 重试(默认额外 2 次)。
* 只重试连接级失败——非 200 是源站的确定答复,重试只是白等超时。
* 总耗时受 {@link #MAIN_DOWNLOAD_BUDGET_MILLIS} 约束:慢源 hang 住时不放大尾延迟
* (同模块的 alicdn 拼图已被这条咬过,见 SimilarAsinProperties#imageDownloadTimeoutSeconds 注释)。
*/
private byte[] downloadBytes(String url) throws IOException {
String trimmed = url == null ? "" : url.trim();
if (trimmed.isBlank()) {
return null;
}
int timeoutSeconds = Math.max(1, properties.getLlmImageDownloadTimeoutSeconds());
try {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(trimmed))
.timeout(Duration.ofSeconds(timeoutSeconds))
.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)")
// 刻意不发 Referer:阿里图床按 Referer 白名单防盗链,外链域名会被判 403
// (实测 cbu01.alicdn.com 带外站 Referer 稳定 403、无 Referer 直接 200)。
.GET()
.build();
HttpResponse<byte[]> response = downloadHttpClient().send(request, HttpResponse.BodyHandlers.ofByteArray());
if (response.statusCode() == 200 && response.body() != null && response.body().length > 0) {
return response.body();
int retryTimes = Math.max(0, properties.getLlmImageDownloadRetryTimes());
long deadline = System.currentTimeMillis() + MAIN_DOWNLOAD_BUDGET_MILLIS;
IOException last = null;
for (int attempt = 0; attempt <= retryTimes; attempt++) {
if (attempt > 0 && (System.currentTimeMillis() > deadline || !sleepBeforeRetry(attempt))) {
break;
}
log.warn("[similar-asin][llm] main download http {} url={}", response.statusCode(), abbreviate(trimmed, 160));
return null;
} catch (Exception ex) {
log.warn("[similar-asin][llm] main download fail url={} err={}", abbreviate(trimmed, 160), failureMessage(ex));
return null;
try {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(trimmed))
.timeout(Duration.ofSeconds(timeoutSeconds))
.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)")
// 刻意不发 Referer:阿里图床按 Referer 白名单防盗链,外链域名会被判 403
// (实测 cbu01.alicdn.com 带外站 Referer 稳定 403、无 Referer 直接 200)。
.GET()
.build();
HttpResponse<byte[]> response = downloadHttpClient().send(request, HttpResponse.BodyHandlers.ofByteArray());
if (response.statusCode() == 200 && response.body() != null && response.body().length > 0) {
if (attempt > 0) {
log.info("[similar-asin][llm] main download retry-success url={} attempt={}/{}",
abbreviate(trimmed, 160), attempt + 1, retryTimes + 1);
}
return response.body();
}
log.warn("[similar-asin][llm] main download http {} url={}", response.statusCode(), abbreviate(trimmed, 160));
return null;
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
return null;
} catch (Exception ex) {
last = new IOException("主图下载失败(" + failureMessage(ex) + ")", ex);
log.warn("[similar-asin][llm] main download fail url={} attempt={}/{} err={}",
abbreviate(trimmed, 160), attempt + 1, retryTimes + 1, failureMessage(ex));
if (Thread.currentThread().isInterrupted()) {
break;
}
}
}
throw last == null ? new IOException("主图下载失败: " + abbreviate(trimmed, 160)) : last;
}
private static boolean sleepBeforeRetry(int attempt) {
try {
Thread.sleep(Math.min(1000L, 250L * attempt));
return true;
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
return false;
}
}