|
|
@@ -35,6 +35,7 @@ import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import java.io.File;
|
|
|
import java.text.ParseException;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
@@ -51,6 +52,9 @@ import java.util.Map;
|
|
|
import java.util.Objects;
|
|
|
import java.util.Set;
|
|
|
import java.util.TimeZone;
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
+import java.util.concurrent.atomic.AtomicInteger;
|
|
|
+import java.util.concurrent.locks.ReentrantLock;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
@@ -88,6 +92,34 @@ public class ReportTraceService {
|
|
|
private static final int MAX_PAGE_CAP = 2000;
|
|
|
private static final int PAYLOAD_PREVIEW_LEN = 4000;
|
|
|
|
|
|
+ // =============================================================
|
|
|
+ // 重试防重入 / 限流(同一 traceId 维度 + 全局并发维度)
|
|
|
+ // =============================================================
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 同一 traceId 两次重试之间的最小冷却间隔(毫秒),默认 30 秒。
|
|
|
+ * 配置项:report.trace.retry.cool-down-ms
|
|
|
+ */
|
|
|
+ @Value("${report.trace.retry.cool-down-ms:30000}")
|
|
|
+ private long retryCoolDownMs;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 全局并发重试上限:同一时刻最多允许处理中的重试数,0 = 不限制。
|
|
|
+ * 防止管理员批量点击把下游解析服务(LangGraph)打爆。
|
|
|
+ * 配置项:report.trace.retry.global-concurrency-limit
|
|
|
+ */
|
|
|
+ @Value("${report.trace.retry.global-concurrency-limit:0}")
|
|
|
+ private int globalConcurrencyLimit;
|
|
|
+
|
|
|
+ /** 防重入锁:按 traceId 维度,同一 traceId 同一时刻只允许一个重试在执行 */
|
|
|
+ private final Map<String, ReentrantLock> retryLocks = new ConcurrentHashMap<>();
|
|
|
+
|
|
|
+ /** 同一 traceId 上次重试「完成」的时间戳(毫秒),用于冷却间隔判断(成功/失败都记录) */
|
|
|
+ private final Map<String, Long> retryLastFinishedAt = new ConcurrentHashMap<>();
|
|
|
+
|
|
|
+ /** 全局正在处理中的重试计数(用于全局并发上限判断) */
|
|
|
+ private final AtomicInteger globalRetryInFlight = new AtomicInteger(0);
|
|
|
+
|
|
|
/** 详情里原始字段的日期统一转可读字符串(ObjectMapper 会克隆 DateFormat,线程安全) */
|
|
|
private final ObjectMapper objectMapper = new ObjectMapper()
|
|
|
.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
|
|
|
@@ -315,6 +347,21 @@ public class ReportTraceService {
|
|
|
// 3. 重试路由
|
|
|
// =============================================================
|
|
|
|
|
|
+ /**
|
|
|
+ * 按来源类型路由重试(含防重入 + 限流保护)。
|
|
|
+ *
|
|
|
+ * <p>保护策略(管理员连点 / 并发点击防护):</p>
|
|
|
+ * <ol>
|
|
|
+ * <li><b>防重入</b>:按 traceId 维度加互斥锁,同一 traceId 上一次重试未结束时的再次请求
|
|
|
+ * 直接返回「重试进行中,请稍后再试」,不会再次触发 LLM / 重采。</li>
|
|
|
+ * <li><b>冷却限流</b>:同一 traceId 两次重试之间必须间隔 {@code report.trace.retry.cool-down-ms}
|
|
|
+ * (默认 30 秒),冷却期内返回「操作过于频繁,请 X 秒后重试」。</li>
|
|
|
+ * <li><b>全局并发上限(可选)</b>:{@code report.trace.retry.global-concurrency-limit}
|
|
|
+ * (默认 0 = 不限制),防止管理员批量点击打爆下游解析服务。</li>
|
|
|
+ * </ol>
|
|
|
+ *
|
|
|
+ * <p>锁在 finally 中释放,成功/异常路径都不会留下永久占用;合法重试不受影响。</p>
|
|
|
+ */
|
|
|
public Map<String, Object> retryBySource(String traceId, Long operatorUserId) {
|
|
|
String[] parts = splitTraceId(traceId);
|
|
|
if (parts == null) {
|
|
|
@@ -322,6 +369,49 @@ public class ReportTraceService {
|
|
|
}
|
|
|
String sourceType = parts[0];
|
|
|
long id = Long.parseLong(parts[1]);
|
|
|
+
|
|
|
+ // 防重入:同一 traceId 同一时刻只允许一个重试在执行(不阻塞等待,直接拒绝)
|
|
|
+ ReentrantLock lock = retryLocks.computeIfAbsent(traceId, k -> new ReentrantLock());
|
|
|
+ if (!lock.tryLock()) {
|
|
|
+ log.info("报告链路重试被防重入拦截 traceId={}", traceId);
|
|
|
+ throw new IllegalArgumentException("重试进行中,请稍后再试");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ // 冷却限流:同一 traceId 两次重试的最小间隔
|
|
|
+ Long lastFinished = retryLastFinishedAt.get(traceId);
|
|
|
+ if (lastFinished != null) {
|
|
|
+ long elapsed = System.currentTimeMillis() - lastFinished;
|
|
|
+ if (elapsed < retryCoolDownMs) {
|
|
|
+ long remainMs = retryCoolDownMs - elapsed;
|
|
|
+ int remainSec = (int) ((remainMs + 999) / 1000);
|
|
|
+ log.info("报告链路重试命中冷却 traceId={}, 剩余冷却={}秒", traceId, remainSec);
|
|
|
+ throw new IllegalArgumentException("操作过于频繁,请 " + remainSec + " 秒后重试");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 全局并发上限:防止批量点击打爆下游解析服务
|
|
|
+ if (globalConcurrencyLimit > 0 && globalRetryInFlight.get() >= globalConcurrencyLimit) {
|
|
|
+ log.info("报告链路重试命中全局并发上限 traceId={}, limit={}, inFlight={}",
|
|
|
+ traceId, globalConcurrencyLimit, globalRetryInFlight.get());
|
|
|
+ throw new IllegalArgumentException("系统繁忙,当前处理中的重试过多,请稍后再试");
|
|
|
+ }
|
|
|
+ globalRetryInFlight.incrementAndGet();
|
|
|
+ try {
|
|
|
+ return doRetryBySource(traceId, sourceType, id, operatorUserId);
|
|
|
+ } finally {
|
|
|
+ globalRetryInFlight.decrementAndGet();
|
|
|
+ // 无论成功还是失败都记录完成时间,进入冷却期,避免失败时疯狂重试
|
|
|
+ retryLastFinishedAt.put(traceId, System.currentTimeMillis());
|
|
|
+ }
|
|
|
+ } finally {
|
|
|
+ // 释放防重入锁(异常路径同样释放,避免死锁与永久占用)
|
|
|
+ lock.unlock();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 重试的实际业务路由(调用方已持有该 traceId 的防重入锁并通过冷却/全局限流检查)。
|
|
|
+ */
|
|
|
+ private Map<String, Object> doRetryBySource(String traceId, String sourceType, long id, Long operatorUserId) {
|
|
|
Map<String, Object> data = new LinkedHashMap<>();
|
|
|
data.put("traceId", traceId);
|
|
|
|