perf(代码审查落地): 后端 A1-A8 与前端 C1-C8 加固,retention 清理补索引(V141)
A 组(backend-java): - A1 载荷引用计数不再无条件回落全表 COUNT - A2 结果行读取由 LIMIT 1 改整页 - A3 ziniao 索引缓存过期判断下推 SQL - A4 循环子任务认领改 CAS,失败一律抛 - A5 建单事务里不碰外部 IO - A6 连接池补网络层超时(connectTimeout/socketTimeout,env 可覆盖) - A7 日志留现场 + 异步落盘 - A8 其余:货源图片预取、结果工作簿装配等 - V141:三条 retention DELETE 的可用索引,其中 biz_price_track_loop_run 用 STORED 生成列承载 COALESCE 时间线(该表实测 881 行/17MB,COPY 秒级) C 组(frontend-vue):URL 取值走 safe-url、行号取值走 row-id、 交付视频任务状态抽成 delivery-video-task-state.ts,各补用例。
This commit is contained in:
+27
-15
@@ -6,6 +6,7 @@ import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.nanri.aiimage.config.ImageVideoProperties;
|
||||
import com.nanri.aiimage.common.service.DistributedJobLockService;
|
||||
import com.nanri.aiimage.modules.file.service.oss.OssStorageService;
|
||||
import com.nanri.aiimage.modules.imagevideo.mapper.ImageVideoAsyncTaskMapper;
|
||||
import com.nanri.aiimage.modules.imagevideo.model.entity.ImageVideoAsyncTaskEntity;
|
||||
@@ -46,6 +47,7 @@ public class ImageVideoArchiveService {
|
||||
private final OssStorageService ossStorageService;
|
||||
private final ImageVideoProperties properties;
|
||||
private final ObjectMapper objectMapper;
|
||||
private final com.nanri.aiimage.common.service.DistributedJobLockService distributedJobLockService;
|
||||
|
||||
void captureSubmitResponse(ImageVideoAsyncTaskEntity task, Object response) {
|
||||
if (!isWorkflowTask(task) || response == null) {
|
||||
@@ -107,22 +109,32 @@ public class ImageVideoArchiveService {
|
||||
|
||||
@Scheduled(fixedDelayString = "${aiimage.image-video.retention-cleanup-delay-ms:600000}")
|
||||
public void cleanupExpiredWorkflowTasks() {
|
||||
LocalDateTime cutoff = LocalDateTime.now().minusDays(RETENTION_DAYS);
|
||||
List<ImageVideoAsyncTaskEntity> expiredTasks = taskMapper.selectList(
|
||||
new LambdaQueryWrapper<ImageVideoAsyncTaskEntity>()
|
||||
.eq(ImageVideoAsyncTaskEntity::getTaskType, TASK_TYPE)
|
||||
.lt(ImageVideoAsyncTaskEntity::getSubmittedAt, cutoff)
|
||||
.orderByAsc(ImageVideoAsyncTaskEntity::getId)
|
||||
.last("LIMIT " + RETENTION_CLEANUP_BATCH_SIZE));
|
||||
int deleted = 0;
|
||||
for (ImageVideoAsyncTaskEntity task : expiredTasks) {
|
||||
if (!deleteArchivedObjects(task)) {
|
||||
continue;
|
||||
}
|
||||
deleted += taskMapper.deleteById(task.getId());
|
||||
// 归档认领有 CAS 兜底,但过期清理没有:两个节点各自 selectList 拿到同一批 200 行后
|
||||
// 会重复删同一批 OSS 对象、重复 deleteById,且任一侧中途失败都会给对方留下
|
||||
// 「对象已删、行还在」的半截状态。锁 TTL 给到 20 分钟,覆盖最慢的一批远端删除。
|
||||
DistributedJobLockService.LockHandle jobLock = distributedJobLockService.tryLock(
|
||||
"image-video:retention-cleanup", java.time.Duration.ofMinutes(20));
|
||||
if (jobLock == null) {
|
||||
return;
|
||||
}
|
||||
if (deleted > 0) {
|
||||
log.info("[image-video] expired workflow task cleanup deleted={} cutoff={}", deleted, cutoff);
|
||||
try (jobLock) {
|
||||
LocalDateTime cutoff = LocalDateTime.now().minusDays(RETENTION_DAYS);
|
||||
List<ImageVideoAsyncTaskEntity> expiredTasks = taskMapper.selectList(
|
||||
new LambdaQueryWrapper<ImageVideoAsyncTaskEntity>()
|
||||
.eq(ImageVideoAsyncTaskEntity::getTaskType, TASK_TYPE)
|
||||
.lt(ImageVideoAsyncTaskEntity::getSubmittedAt, cutoff)
|
||||
.orderByAsc(ImageVideoAsyncTaskEntity::getId)
|
||||
.last("LIMIT " + RETENTION_CLEANUP_BATCH_SIZE));
|
||||
int deleted = 0;
|
||||
for (ImageVideoAsyncTaskEntity task : expiredTasks) {
|
||||
if (!deleteArchivedObjects(task)) {
|
||||
continue;
|
||||
}
|
||||
deleted += taskMapper.deleteById(task.getId());
|
||||
}
|
||||
if (deleted > 0) {
|
||||
log.info("[image-video] expired workflow task cleanup deleted={} cutoff={}", deleted, cutoff);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+17
-7
@@ -238,13 +238,23 @@ public class ImageVideoAsyncTaskService {
|
||||
|
||||
@Scheduled(fixedDelayString = "${aiimage.image-video.failed-task-cleanup-delay-ms:60000}")
|
||||
public void cleanupFailedTasks() {
|
||||
LocalDateTime cutoff = LocalDateTime.now().minusMinutes(FAILED_TASK_RETENTION_MINUTES);
|
||||
int deleted = taskMapper.delete(new LambdaQueryWrapper<ImageVideoAsyncTaskEntity>()
|
||||
.eq(ImageVideoAsyncTaskEntity::getStatus, TaskStatus.FAILED.name())
|
||||
.lt(ImageVideoAsyncTaskEntity::getUpdatedAt, cutoff)
|
||||
.last("LIMIT " + EXPIRED_DELETE_BATCH_SIZE));
|
||||
if (deleted > 0) {
|
||||
log.info("[image-video] removed expired failed tasks count={}", deleted);
|
||||
// 本方法是全类唯一直接对共享表发 DELETE ... LIMIT 的定时任务:两节点各跑一份时会
|
||||
// 命中同一批 (status, updated_at) 区间并互相等行锁。扫描/轮询/接管三条已用任务锁,
|
||||
// 这里补齐;锁超时后另一节点接管,删除本身幂等,不会漏清也不会重复扣。
|
||||
DistributedJobLockService.LockHandle jobLock =
|
||||
distributedJobLockService.tryLock("image-video:failed-task-cleanup", java.time.Duration.ofSeconds(120));
|
||||
if (jobLock == null) {
|
||||
return;
|
||||
}
|
||||
try (jobLock) {
|
||||
LocalDateTime cutoff = LocalDateTime.now().minusMinutes(FAILED_TASK_RETENTION_MINUTES);
|
||||
int deleted = taskMapper.delete(new LambdaQueryWrapper<ImageVideoAsyncTaskEntity>()
|
||||
.eq(ImageVideoAsyncTaskEntity::getStatus, TaskStatus.FAILED.name())
|
||||
.lt(ImageVideoAsyncTaskEntity::getUpdatedAt, cutoff)
|
||||
.last("LIMIT " + EXPIRED_DELETE_BATCH_SIZE));
|
||||
if (deleted > 0) {
|
||||
log.info("[image-video] removed expired failed tasks count={}", deleted);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user