fix(稳定性): 修任务锁超时/实例转发/扫描降噪,补 rustfs 结论日志
线上日志扫描发现的缺陷批量修复: - TaskFileJobService.resetStuckRunningJobsDetailed 去掉跨行长事务:两节点 @Scheduled 各取 200 行在同一事务里逐行 UPDATE,互等行锁导致 biz_task_file_job 每天上百次 Lock wait timeout。改逐行独立提交(本就有 CAS 条件,幂等) - TaskOwnerForwardService 连接类失败重试 3 次:两节点滚动重启窗口内的 Connection refused 会直接丢掉用户提交的结果(读超时不重放,避免重复提交) - AdminApiGuardFilter 把 401/被顶下线降为 debug(线上单节点一天近 3000 条噪声) - NoResourceFoundException 单独处理:如实返回 404,不再刷 ERROR 堆栈、不再伪装 200(每天 580 条,绝大多数是外部扫描 /.env、/credentials、aliyun.json 等) - RustfsObjectStorageService 补「重试后成功 / 重试耗尽」结论日志:一天上千条 retrying 却无结论,无法判断数据是否落盘 - 连接池 keep-alive 300s→30s,减少复用已被对端关闭的空闲连接 - SimilarAsin 提交结果遇到已删除任务时带 TASK_NOT_FOUND(40401),客户端据此 停止每 30 秒一轮的无谓重试 - 修 SimilarAsinResultRowDto 的 GBK 乱码 JsonAlias("浠锋牸" → "价格")
This commit is contained in:
+61
-6
@@ -13,10 +13,12 @@ import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StreamUtils;
|
||||
import org.springframework.web.client.ResourceAccessException;
|
||||
import org.springframework.web.client.RestClient;
|
||||
import org.springframework.web.util.ContentCachingRequestWrapper;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.ConnectException;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
@@ -28,6 +30,11 @@ public class TaskOwnerForwardService {
|
||||
|
||||
public static final String FORWARDED_HEADER = "X-AIIMAGE-Owner-Forwarded";
|
||||
|
||||
/** 连接类失败的重试次数(含首次)。对端滚动重启时通常几秒内即可恢复。 */
|
||||
private static final int CONNECT_RETRY_TIMES = 3;
|
||||
/** 第 n 次重试前的退避:1s、2s(总等待不超过 3s,不长时间占用请求线程)。 */
|
||||
private static final long CONNECT_RETRY_BACKOFF_MILLIS = 1000L;
|
||||
|
||||
private static final Set<String> HOP_BY_HOP_HEADERS = Set.of(
|
||||
"connection",
|
||||
"keep-alive",
|
||||
@@ -58,12 +65,60 @@ public class TaskOwnerForwardService {
|
||||
HttpHeaders headers = copyForwardHeaders(request, ex.getCurrentInstanceId());
|
||||
log.info("[instance-routing] forward {} taskId={} operation={} owner={} current={} url={}",
|
||||
method, ex.getTaskId(), ex.getOperation(), ex.getOwnerInstanceId(), ex.getCurrentInstanceId(), url);
|
||||
return restClient().method(method)
|
||||
.uri(url)
|
||||
.headers(target -> target.addAll(headers))
|
||||
.body(body)
|
||||
.retrieve()
|
||||
.toEntity(byte[].class);
|
||||
return forwardWithConnectRetry(method, url, headers, body, ex);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转发带连接级重试。
|
||||
*
|
||||
* <p>对端实例在部署窗口(原地换 JAR + 两节点滚动重启)内会有几秒的 Connection refused。
|
||||
* 连接都没建立起来说明请求没到达对端,此时重放是安全的;而读超时不重试——对端可能
|
||||
* 已经在处理,盲目重放会造成重复提交。线上由此丢过用户提交的结果。
|
||||
*/
|
||||
private ResponseEntity<byte[]> forwardWithConnectRetry(HttpMethod method, String url,
|
||||
HttpHeaders headers, byte[] body,
|
||||
TaskOwnerMismatchException ex) {
|
||||
RuntimeException lastError = null;
|
||||
for (int attempt = 1; attempt <= CONNECT_RETRY_TIMES; attempt++) {
|
||||
try {
|
||||
return restClient().method(method)
|
||||
.uri(url)
|
||||
.headers(target -> target.addAll(headers))
|
||||
.body(body)
|
||||
.retrieve()
|
||||
.toEntity(byte[].class);
|
||||
} catch (ResourceAccessException accessError) {
|
||||
if (!isConnectFailure(accessError)) {
|
||||
throw accessError;
|
||||
}
|
||||
lastError = accessError;
|
||||
log.warn("[instance-routing] 转发连接失败,第 {}/{} 次 url={} taskId={} 原因={}",
|
||||
attempt, CONNECT_RETRY_TIMES, url, ex.getTaskId(), accessError.getMessage());
|
||||
if (attempt < CONNECT_RETRY_TIMES) {
|
||||
sleepQuietly(CONNECT_RETRY_BACKOFF_MILLIS * attempt);
|
||||
}
|
||||
}
|
||||
}
|
||||
throw lastError;
|
||||
}
|
||||
|
||||
private static boolean isConnectFailure(Throwable error) {
|
||||
Throwable cursor = error;
|
||||
while (cursor != null) {
|
||||
if (cursor instanceof ConnectException) {
|
||||
return true;
|
||||
}
|
||||
cursor = cursor.getCause();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static void sleepQuietly(long millis) {
|
||||
try {
|
||||
Thread.sleep(millis);
|
||||
} catch (InterruptedException interrupted) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
|
||||
private String resolveUrl(TaskOwnerMismatchException ex, String path) {
|
||||
|
||||
Reference in New Issue
Block a user