fix: 第四批修复(结果读取并发化/分页钳制/JWT 密钥强校验/凭据按 id)
性能
- listResultSnapshots 对指针化载荷改并发读取(有界池 + 保序汇总):此前逐行同步对象存储读,
500 行结果文件生成要多花数十秒。仅在确有指针行时才走池——内联 JSON 直接串行读,
避免为本地读取引入调度抖动(性能基准测试容差会被影响)
安全/正确性
- 管理端 GET /{id}/credential 改为按路径 id 查询(此前忽略 id、改用 shop_name,
会出现「路径声明的店铺」与「实际读取凭据的店铺」不一致);凭据 VO 构造抽公共方法
- server profile 下 JWT 密钥缺失即拒绝启动:此前静默回退到公开默认值(等同无防护,
任何人可伪造 token),只留 warn 日志拦不住发布事故;本地/测试 profile 保持宽松
边界
- 分页/条数参数补齐上限钳制(文档早已声明"最大 N"但无校验):
PriceTrack/ShopMatch 的 page_size → 2000;TaskFileJob 的 limit → 1000;
AppearancePatent/Publish 的 limit → 100
This commit is contained in:
+3
-1
@@ -31,7 +31,9 @@ public class TaskFileJobController {
|
||||
@RequestParam(value = "module_type", required = false) String moduleType,
|
||||
@RequestParam(value = "task_id", required = false) Long taskId,
|
||||
@RequestParam(value = "limit", defaultValue = "100") int limit) {
|
||||
return ApiResponse.success(taskFileJobService.listJobs(status, moduleType, taskId, limit));
|
||||
// 上限钳制(2026-09):该运维端点此前对 limit 无校验
|
||||
return ApiResponse.success(taskFileJobService.listJobs(status, moduleType, taskId,
|
||||
Math.min(Math.max(1, limit), 1000)));
|
||||
}
|
||||
|
||||
@PostMapping("/{jobId}/retry")
|
||||
|
||||
+58
-4
@@ -20,12 +20,21 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class TaskResultItemService {
|
||||
|
||||
private final TaskResultItemMapper taskResultItemMapper;
|
||||
/**
|
||||
* 结果快照并发读取池(2026-09):readPayload 对指针化载荷是一次同步对象存储读,
|
||||
* 串行读取会让结果文件生成多花数十秒。有界队列 + daemon 线程,随进程退出。
|
||||
*/
|
||||
private final ExecutorService snapshotReadPool =
|
||||
com.nanri.aiimage.common.util.ThreadPools.boundedFixed("task-result-read", 8);
|
||||
private final ObjectMapper objectMapper;
|
||||
private final TransientPayloadStorageService transientPayloadStorageService;
|
||||
|
||||
@@ -90,11 +99,48 @@ public class TaskResultItemService {
|
||||
.likeRight(TaskResultItemEntity::getItemKey, "result:")
|
||||
.orderByAsc(TaskResultItemEntity::getResultId)
|
||||
.orderByAsc(TaskResultItemEntity::getId));
|
||||
List<T> out = new ArrayList<>();
|
||||
if (rows.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
// 2026-09 优化:readPayload 对**指针化**载荷才是一次同步对象存储读(10~100ms),
|
||||
// 串行 N 行会让结果文件生成多花数十秒。
|
||||
// 只在确有指针行时才走线程池:内联 JSON(本地/mock 场景)直接串行读,
|
||||
// 避免为本地读取引入线程池调度抖动(基准测试曾因此波动超容差)。
|
||||
boolean hasRemotePointer = rows.stream().anyMatch(this::isPointerPayload);
|
||||
if (!hasRemotePointer) {
|
||||
List<T> inlineOut = new ArrayList<>(rows.size());
|
||||
for (TaskResultItemEntity row : rows) {
|
||||
T value = readPayload(row, clazz);
|
||||
if (value != null) {
|
||||
inlineOut.add(value);
|
||||
}
|
||||
}
|
||||
return inlineOut;
|
||||
}
|
||||
if (rows.size() == 1) {
|
||||
T value = readPayload(rows.get(0), clazz);
|
||||
return value == null ? List.of() : List.of(value);
|
||||
}
|
||||
List<Future<T>> futures = new ArrayList<>(rows.size());
|
||||
for (TaskResultItemEntity row : rows) {
|
||||
T value = readPayload(row, clazz);
|
||||
if (value != null) {
|
||||
out.add(value);
|
||||
futures.add(snapshotReadPool.submit(() -> readPayload(row, clazz)));
|
||||
}
|
||||
List<T> out = new ArrayList<>(rows.size());
|
||||
for (Future<T> future : futures) {
|
||||
try {
|
||||
T value = future.get();
|
||||
if (value != null) {
|
||||
out.add(value);
|
||||
}
|
||||
} catch (InterruptedException ex) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new BusinessException("读取任务结果明细被中断");
|
||||
} catch (ExecutionException ex) {
|
||||
Throwable cause = ex.getCause();
|
||||
if (cause instanceof BusinessException businessException) {
|
||||
throw businessException;
|
||||
}
|
||||
throw new BusinessException("读取任务结果明细失败", cause);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
@@ -368,6 +414,14 @@ public class TaskResultItemService {
|
||||
}
|
||||
}
|
||||
|
||||
/** 载荷值是否是指向对象存储的指针(决定是否需要走并发远程读)。 */
|
||||
private boolean isPointerPayload(TaskResultItemEntity row) {
|
||||
if (row == null) {
|
||||
return false;
|
||||
}
|
||||
return transientPayloadStorageService.extractPointer(row.getPayloadJson()) != null;
|
||||
}
|
||||
|
||||
private String firstNonBlank(String primary, String fallback) {
|
||||
return primary == null || primary.isBlank() ? fallback : primary;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user