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:
@@ -186,4 +186,13 @@ public class SimilarAsinProperties {
|
||||
|
||||
/** 拼接图/主图下载超时(秒),慢源图片较多时放大该值。 */
|
||||
private int llmImageDownloadTimeoutSeconds = 10;
|
||||
|
||||
/**
|
||||
* 主图转存的<b>额外</b>重试次数(0=不重试,与原行为一致)。
|
||||
* 依据 2026-09-28 生产取证:主机A 近 3 小时 252 条主图下载失败全部是
|
||||
* {@code m.media-amazon.com} 的 "Remote host terminated the handshake",
|
||||
* 而这些 URL 事后 curl 8 条里 7 条直接 200 —— 瞬时抖动,重试就能救回整行
|
||||
* (不重试则整行判失败并重发一轮流程)。设 0 可退回旧行为。
|
||||
*/
|
||||
private int llmImageDownloadRetryTimes = 2;
|
||||
}
|
||||
|
||||
+71
-21
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -377,6 +377,7 @@ aiimage:
|
||||
llm-retry-times: ${AIIMAGE_SIMILAR_ASIN_LLM_RETRY_TIMES:3}
|
||||
llm-row-concurrency: ${AIIMAGE_SIMILAR_ASIN_LLM_ROW_CONCURRENCY:5}
|
||||
llm-image-download-timeout-seconds: ${AIIMAGE_SIMILAR_ASIN_LLM_IMAGE_DOWNLOAD_TIMEOUT_SECONDS:10}
|
||||
llm-image-download-retry-times: ${AIIMAGE_SIMILAR_ASIN_LLM_IMAGE_DOWNLOAD_RETRY_TIMES:2}
|
||||
collect-data:
|
||||
stale-timeout-minutes: ${AIIMAGE_COLLECT_DATA_STALE_TIMEOUT_MINUTES:30}
|
||||
no-result-upload-timeout-minutes: ${AIIMAGE_COLLECT_DATA_NO_RESULT_UPLOAD_TIMEOUT_MINUTES:180}
|
||||
|
||||
Reference in New Issue
Block a user