feat(phase-5): B60 console 分页与矩阵预聚合+P1-B5/6/7/8 性能热点收口
接管另一路会话的 Phase 5 在飞件收口(5A/5C 主体由那一路落码,本笔补齐 P1-B7 与两处台账并整量验证)。 - 5A/B60:撞款 console 的行窗口在服务端真分页(响应 dup 行数 ≤ pageSize,total_dup 仍是未分页 总数)、dupGroup 判据收归服务端单一出处(前端 dupWithinGroup 删掉,两套同名规则不再分叉)、 排序先于切窗;后管矩阵改预聚合+列窗 40。读数(200 列 × 50 行):每行 3 店 8.4 → 5.3 → 0.7ms (12.4×),每行 40 店 83.7 → 18.4 → 1.8ms(45.3×)。服务端不设列窗是判断不是漏做:shops 每行 只有 5 个标量,而矩阵的列来自已分页的行——给 shops 再加分页会把"全局指标读全集"和"列窗读本页" 两套语义搅在一起。浏览器首屏/交互耗时本机拿不到,登记为 Phase 6 现网复量。 - P1-B6:claim 循环的父任务终态判定折成一次批量 IN(TaskFileJobBatchQueries+Mapper XML, 铁律 1 收口在 Mapper),逐行 CAS 认领一字不动。 - P1-B5:V187 只建 idx_biz_file_task_resume_of;审计给的候选集索引**有意不建**(V184 前缀已覆盖, OR/IS-NULL 与 COALESCE 不可索引),判据把这条负判定钉住。真库 EXPLAIN 本机无 MySQL,顺延现网。 - P1-B8:结果载荷的对象存储删除移到 afterCommit,事务里不再做 120s 超时的网络调用。 - P1-B7:结果集读取按窗提交+收割(窗口=线程池队列容量的一半)。审计把它记成"无 limit",但截断 是正确性错误不是性能取舍,真能收的是"整份结果集被读两遍"——一次性提交时每支 Future 把解好的 载荷扣在自己身上直到全部收割完,峰值=结果集+另一份整副本;分窗后副本只驻留一窗。流式组装 契约要改 4 个模块的调用方,本 Phase 未做,别读成 P1-B7 已闭环。 - SourceScanTest 台账随之登记:裸名 25 → 26、调用方 18 → 19(P1-B7 的窗口判据只能打在源码上—— 线程池只有 8 个线程,"同时开始读几行"改前改后读数相同,差异只体现在堆上,mock 数不出来)。
This commit is contained in:
@@ -76,7 +76,10 @@ class MigratedSqlEquivalenceTest {
|
||||
// NOT_MIGRATED(select 总数不变,只是记账位置换了)。
|
||||
// 100 → 102:P1-1 把两条认领查询的通道谓词下推进 SQL,FileTaskMapper 新写 2 条 select,
|
||||
// 逐条点名进 NOT_MIGRATED(SqlLayeringConventionTest.XML_STATEMENT_TOTAL 同轮 144 → 146)。
|
||||
private static final int XML_SELECT_TOTAL = 102;
|
||||
// 102 → 103:P1-B6 把 claim 循环的「父任务是否终态」折成批量档
|
||||
// TaskFileJobMapper#selectTerminalFileTaskIds,点名进下方 NOT_MIGRATED
|
||||
// (SqlLayeringConventionTest.XML_STATEMENT_TOTAL 同轮 146 → 147)。
|
||||
private static final int XML_SELECT_TOTAL = 103;
|
||||
|
||||
/**
|
||||
* 不是「从注解搬过来」的语句:注解时代没有原文可对撞,等价性这支只能跳过它们。
|
||||
@@ -118,6 +121,9 @@ class MigratedSqlEquivalenceTest {
|
||||
"UserSecretUsageMapper#sumForUser",
|
||||
// B28(2026-10-25)新写的 1 条主键点查:父任务是否已终态。
|
||||
"TaskFileJobMapper#selectFileTaskStatus",
|
||||
// P1-B6(2026-11-03)新写的 1 条批量档:一次问回这批父任务里已终态的 id
|
||||
// (终态值域由调用方传入,XML 里没有第二份字符串)。
|
||||
"TaskFileJobMapper#selectTerminalFileTaskIds",
|
||||
// CR-01(2026-10-25)新写的 1 条写侧语句:会话世代原子自增。
|
||||
"LoginUserMapper#incrementSessionEpoch",
|
||||
// 关键词使用记录管理端(2026-10-25)新写的 10 条只读 select。
|
||||
|
||||
@@ -95,7 +95,10 @@ class SqlLayeringConventionTest {
|
||||
// 原形状是先 limit 取页再 filter(isLocal()),混通道时较老的 N 条是 ONLINE 就把本机单永久饿死。
|
||||
// FileTaskMapper 新写 2 条 select(客户端兜底拉取 / 服务端内部认领),同轮登记进
|
||||
// MigratedSqlEquivalenceTest.NOT_MIGRATED(该文件 XML_SELECT_TOTAL 同轮 100 → 102)。
|
||||
private static final int XML_STATEMENT_TOTAL = 146;
|
||||
// 146 → 147:P1-B6 把 claim 循环里「父任务是否终态」的逐条点查折成批量档
|
||||
// {@code TaskFileJobMapper#selectTerminalFileTaskIds}(终态值域由调用方传入,XML 不抄字符串),
|
||||
// 同轮登记进 MigratedSqlEquivalenceTest.NOT_MIGRATED(该文件 XML_SELECT_TOTAL 同轮 102 → 103)。
|
||||
private static final int XML_STATEMENT_TOTAL = 147;
|
||||
|
||||
/**
|
||||
* wrapper 里夹带函数级 SQL 的现状("这不是简单单表"的证据),文件名 → 条数:
|
||||
|
||||
+113
@@ -0,0 +1,113 @@
|
||||
package com.nanri.aiimage.config;
|
||||
|
||||
import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* V187 给 {@code biz_file_task} 补 {@code resume_of_task_id} 索引,本类钉它的形状(Phase 5/审计 P1-B5)。
|
||||
*
|
||||
* <p>来历:自动续跑巡检每轮对每个候选单各做一次「这单是否已有重试子任务」的反查,而那列在
|
||||
* V129 只加了列、一支索引都没配 ⇒ 500 个候选=500 场全表扫。同族的「幂等清账」用的是同一列。
|
||||
*
|
||||
* <p>本类同时钉住**不建**的那一支:审计原文建议 {@code (status, module_type, resume_attempt, updated_at)},
|
||||
* 但候选集查询的等值前缀已由 V184 覆盖,而 {@code resume_attempt} 是 OR/IS-NULL 残差谓词、
|
||||
* {@code COALESCE(finished_at, updated_at)} 是表达式谓词——两者都不吃索引。再建一条同前缀索引
|
||||
* =只写不读,白白摊在 biz_file_task 每次写入上(V186 头注释「别让一条推测性索引进基线」同判)。
|
||||
* 谁要拓宽这条口径,先让这里红一次。
|
||||
*/
|
||||
class TaskResumeIndexMigrationV187Test {
|
||||
|
||||
private static final Path SQL = Path.of(System.getProperty("user.dir"),
|
||||
"src/main/resources/db/V187__file_task_resume_of_index.sql");
|
||||
|
||||
private static final Path RESUME_SERVICE = Path.of(System.getProperty("user.dir"),
|
||||
"src/main/java/com/nanri/aiimage/modules/task/service/TaskResumeService.java");
|
||||
|
||||
private static String sql() throws IOException {
|
||||
assertTrue(Files.exists(SQL), "V187 迁移脚本不在:" + SQL);
|
||||
return Files.readString(SQL);
|
||||
}
|
||||
|
||||
@Test
|
||||
void 只给反查列建一支索引且按判存包装() throws IOException {
|
||||
String text = sql();
|
||||
|
||||
assertTrue(text.contains("ALTER TABLE biz_file_task ADD INDEX idx_biz_file_task_resume_of (resume_of_task_id)"),
|
||||
"迁移对象与列名必须逐字对上:反查用的就是 resume_of_task_id");
|
||||
assertTrue(text.contains("information_schema.STATISTICS"),
|
||||
"必须判存后再建:重跑(或已手工建过)不能因 Duplicate key name 把启动掐断");
|
||||
assertTrue(text.contains("PREPARE stmt FROM @sql"),
|
||||
"判存要落到动态执行,光 SELECT 出计数不改表等于什么都没做");
|
||||
assertEquals(1, occurrences(text, "ADD INDEX idx_biz_file_task_resume_of"),
|
||||
"整份脚本只许建这一支索引");
|
||||
assertTrue(text.contains("DROP INDEX idx_biz_file_task_resume_of"),
|
||||
"回滚语句必须写在脚本注释里(与 V184/V186 同一口径)");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 有意不建候选集那一支同前缀索引() throws IOException {
|
||||
String text = sql();
|
||||
// 只看真正会执行的行:头注释里必须写下 resume_attempt/V184 的取舍理由,那是文档不是 DDL。
|
||||
StringBuilder executableText = new StringBuilder();
|
||||
for (String line : text.split("\n")) {
|
||||
String trimmed = line.trim();
|
||||
if (!trimmed.startsWith("--")) {
|
||||
executableText.append(trimmed).append('\n');
|
||||
}
|
||||
}
|
||||
String executable = executableText.toString().toUpperCase(java.util.Locale.ROOT);
|
||||
|
||||
assertFalse(executable.contains("RESUME_ATTEMPT"),
|
||||
"resume_attempt 是 OR/IS-NULL 残差谓词,不进索引;要加必须先拿出真库 EXPLAIN 读数");
|
||||
assertFalse(executable.contains("MODULE_TYPE"),
|
||||
"(status, module_type, resume_attempt) 那支同前缀索引有意不建:等值前缀已由 V184 覆盖");
|
||||
assertFalse(executable.contains("CREATE INDEX"),
|
||||
"本仓加索引一律走 ALTER TABLE ... ADD INDEX(判存包装的形状)");
|
||||
assertTrue(text.contains("idx_biz_file_task_status_module_created"),
|
||||
"「等值前缀已由 V184 覆盖」这条判断的出处必须写在头注释里,否则下一个人会当成漏配");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 索引列与实体字段同源() throws IOException {
|
||||
// MyBatis-Plus 靠驼峰↔下划线映射:字段名改了而索引列名没改,索引就成了永远命不中的摆设,
|
||||
// 而巡检的反查照样跑得动——只是每次都全表扫。
|
||||
assertTrue(java.util.Arrays.stream(FileTaskEntity.class.getDeclaredFields())
|
||||
.anyMatch(field -> field.getName().equals("resumeOfTaskId")),
|
||||
"实体里必须有 resumeOfTaskId 这个字段;反查一旦改名,V187 的索引与谓词会分叉");
|
||||
assertTrue(sql().contains("ADD INDEX idx_biz_file_task_resume_of (resume_of_task_id)"),
|
||||
"索引列名必须就是这个字段的下划线形(同源),别让它指向一列不存在的历史名");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 巡检里不留逐行反查的形状() throws IOException {
|
||||
assertTrue(Files.exists(RESUME_SERVICE), "续跑服务源码不在:" + RESUME_SERVICE);
|
||||
String text = Files.readString(RESUME_SERVICE);
|
||||
|
||||
assertFalse(text.contains("private boolean hasRetryChild("),
|
||||
"P1-B5 的病灶就是逐行反查;改回单条档=每候选一次全表扫,退回原状即红");
|
||||
assertFalse(text.contains("hasUnfinishedAssembleJob(original.getId())"),
|
||||
"补检判定同样必须走批量档,不许在循环里逐行问库");
|
||||
assertTrue(text.contains("parentsWithRetryChild(candidateIds)"),
|
||||
"循环前一次批量反查");
|
||||
assertTrue(text.contains("taskIdsWithUnfinishedAssembleJob(candidateIds)"),
|
||||
"循环前一次批量补检判定");
|
||||
}
|
||||
|
||||
private static int occurrences(String text, String needle) {
|
||||
int count = 0;
|
||||
int index = text.indexOf(needle);
|
||||
while (index >= 0) {
|
||||
count++;
|
||||
index = text.indexOf(needle, index + needle.length());
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
||||
+69
-3
@@ -156,7 +156,7 @@ class ShopDataDuplicateCheckQueryServiceTest {
|
||||
|
||||
@Test
|
||||
void consoleSuperAdminReturnsDupSetGroupsAndSummary() {
|
||||
Map<String, Object> data = service.consoleData(view, null, noFilter());
|
||||
Map<String, Object> data = service.consoleData(view, null, noFilter(), 1, 100, "");
|
||||
assertThat(data.get("pending")).isEqualTo(false);
|
||||
assertThat(data.get("total_dup")).isEqualTo(3);
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -184,7 +184,7 @@ class ShopDataDuplicateCheckQueryServiceTest {
|
||||
@Test
|
||||
void consoleLeaderCropRemovesCrossGroupDupAndKeepsOwnGroups() {
|
||||
Set<String> visible = Set.of("shopa", "shopb");
|
||||
Map<String, Object> data = service.consoleData(view, visible, noFilter());
|
||||
Map<String, Object> data = service.consoleData(view, visible, noFilter(), 1, 100, "");
|
||||
assertThat(data.get("total_dup")).isEqualTo(2);
|
||||
@SuppressWarnings("unchecked")
|
||||
List<DuplicateItem> dup = (List<DuplicateItem>) data.get("dup");
|
||||
@@ -201,11 +201,77 @@ class ShopDataDuplicateCheckQueryServiceTest {
|
||||
|
||||
@Test
|
||||
void consolePendingShapes() {
|
||||
Map<String, Object> data = service.pendingConsole();
|
||||
Map<String, Object> data = service.pendingConsole(1, 100);
|
||||
assertThat(data.get("pending")).isEqualTo(true);
|
||||
assertThat(data.get("dup")).isEqualTo(List.of());
|
||||
assertThat(data.get("groups")).isEqualTo(List.of());
|
||||
assertThat(data.get("summary")).isEqualTo(Map.of());
|
||||
assertThat(data.get("page")).isEqualTo(1);
|
||||
assertThat(data.get("page_size")).isEqualTo(100);
|
||||
}
|
||||
|
||||
/**
|
||||
* B60:console 的 {@code dup} 从「全集」改成「行窗口」。分页参数必须真生效——
|
||||
* 响应体行数 ≤ pageSize,而 {@code total_dup} 仍是未分页的总数(KPI/分页控件读它)。
|
||||
*/
|
||||
@Test
|
||||
void consoleRowWindowLimitsRowsButKeepsFullTotal() {
|
||||
Map<String, Object> data = service.consoleData(view, null, noFilter(), 1, 2, "");
|
||||
assertThat(data.get("total_dup")).isEqualTo(3);
|
||||
assertThat(data.get("page")).isEqualTo(1);
|
||||
assertThat(data.get("page_size")).isEqualTo(2);
|
||||
@SuppressWarnings("unchecked")
|
||||
List<DuplicateItem> dup = (List<DuplicateItem>) data.get("dup");
|
||||
assertThat(dup).hasSize(2);
|
||||
}
|
||||
|
||||
/**
|
||||
* 行窗口按 ASIN 升序<b>稳定</b>:把窗口翻到第二页必须拿到上一页之外的行,
|
||||
* 而不是每次刷新换一批(排序发生在切窗之前,这是分页可用的前提)。
|
||||
*/
|
||||
@Test
|
||||
void consoleRowWindowsAreStableAcrossPages() {
|
||||
@SuppressWarnings("unchecked")
|
||||
List<DuplicateItem> first = (List<DuplicateItem>)
|
||||
service.consoleData(view, null, noFilter(), 1, 2, "").get("dup");
|
||||
@SuppressWarnings("unchecked")
|
||||
List<DuplicateItem> second = (List<DuplicateItem>)
|
||||
service.consoleData(view, null, noFilter(), 2, 2, "").get("dup");
|
||||
assertThat(first).extracting(DuplicateItem::asin).containsExactly("A0000001", "E0000001");
|
||||
assertThat(second).extracting(DuplicateItem::asin).containsExactly("F0000001");
|
||||
}
|
||||
|
||||
/**
|
||||
* 分组范围(页面的「分组」chip)由服务端裁行:判据=组内 ≥2 店。
|
||||
* F0000001 是 ShopB(GroupA)×ShopD(GroupD) 的跨组撞款,落在 GroupA 范围内不算组内撞款。
|
||||
*
|
||||
* <p>只裁行:summary/shops/groups 三块必须与不带 dup_group 的那次逐字相同——
|
||||
* 指标跟着行窗口一起缩的话,页面顶部数字就成了「这一页的统计」,那是另一件事。
|
||||
*/
|
||||
@Test
|
||||
void consoleDupGroupScopesRowsOnlyAndLeavesStatsAlone() {
|
||||
Map<String, Object> scoped = service.consoleData(view, null, noFilter(), 1, 100, "GroupA");
|
||||
@SuppressWarnings("unchecked")
|
||||
List<DuplicateItem> rows = (List<DuplicateItem>) scoped.get("dup");
|
||||
assertThat(scoped.get("total_dup")).isEqualTo(2);
|
||||
assertThat(rows).extracting(DuplicateItem::asin).containsExactly("A0000001", "E0000001");
|
||||
|
||||
Map<String, Object> plain = service.consoleData(view, null, noFilter(), 1, 100, "");
|
||||
assertThat(scoped.get("summary")).isEqualTo(plain.get("summary"));
|
||||
assertThat(scoped.get("shops")).isEqualTo(plain.get("shops"));
|
||||
assertThat(scoped.get("groups")).isEqualTo(plain.get("groups"));
|
||||
}
|
||||
|
||||
/** 组内不足 2 店 ⇒ 行集为空(不是「退回全集」);组名不存在同理。 */
|
||||
@Test
|
||||
void consoleDupGroupWithoutPairReturnsEmptyWindow() {
|
||||
Map<String, Object> singleShopGroup = service.consoleData(view, null, noFilter(), 1, 100, "GroupC");
|
||||
assertThat(singleShopGroup.get("total_dup")).isEqualTo(0);
|
||||
assertThat((List<?>) singleShopGroup.get("dup")).isEmpty();
|
||||
|
||||
Map<String, Object> unknownGroup = service.consoleData(view, null, noFilter(), 1, 100, "不存在组");
|
||||
assertThat(unknownGroup.get("total_dup")).isEqualTo(0);
|
||||
assertThat((List<?>) unknownGroup.get("dup")).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+189
@@ -0,0 +1,189 @@
|
||||
package com.nanri.aiimage.modules.task.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||
import com.nanri.aiimage.common.task.TaskStatuses;
|
||||
import com.nanri.aiimage.modules.task.mapper.TaskFileJobMapper;
|
||||
import com.nanri.aiimage.modules.task.model.entity.TaskFileJobEntity;
|
||||
import org.apache.ibatis.builder.MapperBuilderAssistant;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.isNull;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* P1-B6/P1-B5:认领循环与补检判定都必须走批量档,不许退回逐行点查。
|
||||
*
|
||||
* <p>来历:{@code claimCandidates} 原先对每个候选各做一次 {@code selectFileTaskStatus}(一批 100
|
||||
* 个候选=100 次 SELECT),而 P1-13 之后这份结果**只用来打一行日志**——终态父任务的出表作业照旧放行。
|
||||
* 纯开销的 N 次往返折成一次 {@code IN} 批查。逐行 CAS 认领({@code markRunning})一字不动:
|
||||
* 那才是并发不重复派发的根据,不能被"顺手批量化"掉。
|
||||
*
|
||||
* <p>反向自证:把批量档改回循环里逐条 {@code isOwningTaskTerminal},本类第一格即红。
|
||||
* 批量档的本体在 {@link TaskFileJobBatchQueries}(静态只读工具),{@code TaskFileJobService}
|
||||
* 只留一个同名门面——门面不许长回逻辑,否则服务侧又变超长文件。
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class TaskFileJobBatchQueryGuardrailTest {
|
||||
|
||||
@Mock private TaskFileJobMapper taskFileJobMapper;
|
||||
@Mock private ApplicationEventPublisher applicationEventPublisher;
|
||||
|
||||
private TaskFileJobService service;
|
||||
|
||||
@BeforeAll
|
||||
static void initializeTableInfo() {
|
||||
TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""),
|
||||
TaskFileJobEntity.class);
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
service = new TaskFileJobService(taskFileJobMapper, applicationEventPublisher);
|
||||
}
|
||||
|
||||
private static TaskFileJobEntity job(Long id, Long taskId, String status) {
|
||||
TaskFileJobEntity entity = new TaskFileJobEntity();
|
||||
entity.setId(id);
|
||||
entity.setTaskId(taskId);
|
||||
entity.setResultId(2000L + id);
|
||||
entity.setModuleType("APPEARANCE_PATENT");
|
||||
entity.setJobType("ASSEMBLE_RESULT");
|
||||
entity.setStatus(status);
|
||||
entity.setRetryCount(0);
|
||||
entity.setUpdatedAt(LocalDateTime.now());
|
||||
return entity;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static <T> ArgumentCaptor<T> captorOf(Class<?> type) {
|
||||
return (ArgumentCaptor<T>) ArgumentCaptor.forClass(type);
|
||||
}
|
||||
|
||||
@Test
|
||||
void 一批候选只问一次父任务状态() {
|
||||
when(taskFileJobMapper.selectList(any(LambdaQueryWrapper.class)))
|
||||
.thenReturn(List.of(job(21L, 500L, "PENDING"), job(22L, 500L, "PENDING"), job(23L, 501L, "FAILED")));
|
||||
when(taskFileJobMapper.selectTerminalFileTaskIds(any(), any())).thenReturn(List.of(500L));
|
||||
when(taskFileJobMapper.update(isNull(), any(LambdaUpdateWrapper.class))).thenReturn(1, 1, 1);
|
||||
when(taskFileJobMapper.selectBatchIds(any()))
|
||||
.thenReturn(List.of(job(21L, 500L, "RUNNING"), job(22L, 500L, "RUNNING"), job(23L, 501L, "RUNNING")));
|
||||
|
||||
List<TaskFileJobEntity> claimed = service.claimRunnableJobs(10);
|
||||
|
||||
assertEquals(3, claimed.size());
|
||||
ArgumentCaptor<Collection<Long>> ids = captorOf(Collection.class);
|
||||
ArgumentCaptor<Collection<String>> statuses = captorOf(Collection.class);
|
||||
verify(taskFileJobMapper, times(1)).selectTerminalFileTaskIds(ids.capture(), statuses.capture());
|
||||
assertEquals(List.of(500L, 501L), List.copyOf(ids.getValue()),
|
||||
"同一父任务的多个作业必须去重后一次问回;出现两次以上调用就是退回逐行点查");
|
||||
verify(taskFileJobMapper, never()).selectFileTaskStatus(any());
|
||||
}
|
||||
|
||||
/** 终态值域的唯一正本仍是 {@code TaskStatuses}:它作为参数进 SQL,XML 里不许抄第二份字符串。 */
|
||||
@Test
|
||||
void 终态值域取自词表单点() {
|
||||
when(taskFileJobMapper.selectList(any(LambdaQueryWrapper.class)))
|
||||
.thenReturn(List.of(job(21L, 500L, "PENDING")));
|
||||
when(taskFileJobMapper.selectTerminalFileTaskIds(any(), any())).thenReturn(List.of());
|
||||
when(taskFileJobMapper.update(isNull(), any(LambdaUpdateWrapper.class))).thenReturn(1);
|
||||
when(taskFileJobMapper.selectBatchIds(any())).thenReturn(List.of(job(21L, 500L, "RUNNING")));
|
||||
|
||||
service.claimRunnableJobs(10);
|
||||
|
||||
ArgumentCaptor<Collection<String>> statuses = captorOf(Collection.class);
|
||||
verify(taskFileJobMapper).selectTerminalFileTaskIds(any(), statuses.capture());
|
||||
assertEquals(TaskStatuses.FileTask.TERMINAL, Set.copyOf(statuses.getValue()),
|
||||
"批量档的终态值域必须逐字等于 TaskStatuses.FileTask.TERMINAL");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 空候选不发批量查() {
|
||||
when(taskFileJobMapper.selectList(any(LambdaQueryWrapper.class))).thenReturn(List.of());
|
||||
|
||||
assertEquals(List.of(), service.claimRunnableJobs(10));
|
||||
|
||||
verify(taskFileJobMapper, never()).selectTerminalFileTaskIds(any(), any());
|
||||
verify(taskFileJobMapper, never()).selectFileTaskStatus(any());
|
||||
}
|
||||
|
||||
/** 查不到行的 taskId(品牌任务的 job)按非终态走,与单条档「返回 null ⇒ 非终态」逐字同语义。 */
|
||||
@Test
|
||||
void 查不到行的任务照常被认领() {
|
||||
when(taskFileJobMapper.selectList(any(LambdaQueryWrapper.class)))
|
||||
.thenReturn(List.of(job(31L, 900L, "PENDING")));
|
||||
when(taskFileJobMapper.selectTerminalFileTaskIds(any(), any())).thenReturn(List.of());
|
||||
when(taskFileJobMapper.update(isNull(), any(LambdaUpdateWrapper.class))).thenReturn(1);
|
||||
when(taskFileJobMapper.selectBatchIds(any())).thenReturn(List.of(job(31L, 900L, "RUNNING")));
|
||||
|
||||
List<TaskFileJobEntity> claimed = service.claimRunnableJobs(10);
|
||||
|
||||
assertEquals(1, claimed.size());
|
||||
assertEquals("RUNNING", claimed.get(0).getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 补检批量档的空入参不发SQL() {
|
||||
assertEquals(Set.of(), service.taskIdsWithUnfinishedAssembleJob(List.of()));
|
||||
assertEquals(Set.of(), service.taskIdsWithUnfinishedAssembleJob(null));
|
||||
assertEquals(Set.of(), TaskFileJobBatchQueries.terminalOwningTaskIds(taskFileJobMapper, List.of()));
|
||||
assertEquals(Set.of(), TaskFileJobBatchQueries.terminalOwningTaskIds(taskFileJobMapper, null));
|
||||
|
||||
verify(taskFileJobMapper, never()).selectList(any(LambdaQueryWrapper.class));
|
||||
verify(taskFileJobMapper, never()).selectTerminalFileTaskIds(any(), any());
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量档与它替代的单条档必须同判据:同一 jobType、同两个未终态状态。
|
||||
* 分叉的形状是「批量档少筛一项」=把已完成的作业也当成在补检,续跑会被永久饿死。
|
||||
*/
|
||||
@Test
|
||||
void 补检批量档与单条档判据同源() {
|
||||
when(taskFileJobMapper.selectList(any(LambdaQueryWrapper.class)))
|
||||
.thenReturn(List.of(job(41L, 700L, "PENDING")));
|
||||
|
||||
assertEquals(Set.of(700L), service.taskIdsWithUnfinishedAssembleJob(List.of(700L, 701L)));
|
||||
|
||||
ArgumentCaptor<LambdaQueryWrapper<TaskFileJobEntity>> captured = captorOf(LambdaQueryWrapper.class);
|
||||
verify(taskFileJobMapper).selectList(captured.capture());
|
||||
// MP 把参数装配推迟到生成 SQL 那一刻;不先取一次 sqlSegment,paramNameValuePairs 是空的。
|
||||
String where = captured.getValue().getSqlSegment();
|
||||
assertTrue(where.contains("task_id IN") || where.contains("task_id in"),
|
||||
"批量档必须按 task_id 一整批问,实参段=" + where);
|
||||
java.util.Set<Object> values = new java.util.HashSet<>();
|
||||
for (Object value : captured.getValue().getParamNameValuePairs().values()) {
|
||||
if (value instanceof java.util.Collection<?> nested) {
|
||||
values.addAll(nested);
|
||||
} else {
|
||||
values.add(value);
|
||||
}
|
||||
}
|
||||
assertTrue(values.contains("ASSEMBLE_RESULT"), "批量档必须仍按 ASSEMBLE_RESULT 过滤,实参=" + values + " sql=" + where);
|
||||
assertTrue(values.contains("PENDING") && values.contains("RUNNING"),
|
||||
"批量档必须仍只看 PENDING/RUNNING 两态,实参=" + values);
|
||||
assertTrue(values.contains(700L) && values.contains(701L),
|
||||
"批量档必须把这批 taskId 一起带进 IN,实参=" + values);
|
||||
assertTrue(String.valueOf(captured.getValue().getSqlSelect()).contains("task_id"),
|
||||
"批量档只取 task_id 投影,别把整行拉回来");
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -107,8 +107,8 @@ class TaskFileJobTerminalTaskGuardTest {
|
||||
TaskFileJobEntity active = job(22L, 501L);
|
||||
when(taskFileJobMapper.selectList(any(LambdaQueryWrapper.class)))
|
||||
.thenReturn(List.of(terminal, active));
|
||||
when(taskFileJobMapper.selectFileTaskStatus(500L)).thenReturn("FAILED");
|
||||
when(taskFileJobMapper.selectFileTaskStatus(501L)).thenReturn("RUNNING");
|
||||
// P1-B6:claim 循环的父任务状态改为一次批量档(500=500L 已终态)。
|
||||
when(taskFileJobMapper.selectTerminalFileTaskIds(any(), any())).thenReturn(List.of(500L));
|
||||
when(taskFileJobMapper.update(isNull(), any(LambdaUpdateWrapper.class))).thenReturn(1, 1);
|
||||
TaskFileJobEntity claimedTerminal = job(21L, 500L);
|
||||
claimedTerminal.setStatus("RUNNING");
|
||||
|
||||
+208
@@ -0,0 +1,208 @@
|
||||
package com.nanri.aiimage.modules.task.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.common.support.ThreadPools;
|
||||
import com.nanri.aiimage.modules.task.mapper.TaskResultItemMapper;
|
||||
import com.nanri.aiimage.modules.task.model.entity.TaskResultItemEntity;
|
||||
import com.nanri.aiimage.testutil.SourceScan;
|
||||
import org.apache.ibatis.builder.MapperBuilderAssistant;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* P1-B7:指针化载荷的结果集读取必须**分窗提交并收割**,同时整份一行不许漏。
|
||||
*
|
||||
* <p>来历:审计把 {@code listResultSnapshots} 的「无 limit」记成性能项,但**截断是正确性错误不是性能取舍**
|
||||
* ——调用方(patroldelete/queryasin/shopdatacrawl/withdraw)拿它组装完整结果文件。真正能收的是
|
||||
* 「整份结果集被读两遍」:原先把 N 行一次性交进线程池,每支 Future 完成后把解好的载荷扣在自己身上
|
||||
* ({@code FutureTask.outcome} 不清),而这一整批要到收割完才释放 ⇒ 峰值=结果集+同样多的另一份副本。
|
||||
* 分窗之后副本只驻留一窗。
|
||||
*
|
||||
* <p>判据为什么打在源码上而不是只打行为:窗口效应体现在**堆**上(同时在飞的载荷数),
|
||||
* 线程池本身只有 8 个线程,"同时开始读几行"这个observable 在改前改后都是 ≤8,用 mock 数不出来;
|
||||
* 行为侧能钉住的只有「不漏行、不乱序、异常口径不变」这三件——那些恰恰是拆窗最容易被改坏的地方,
|
||||
* 所以两类都要,缺一个就是半只眼睛。
|
||||
*
|
||||
* <p>反向自证:把窗口改回 {@code for (row : rows) futures.add(submit(...))} 整量一次性提交,
|
||||
* <b>只有源码绊线那一格红</b>(实测 6 例中 1 失败),行为四格照样全绿——窗口化本来就不改可观察行为,
|
||||
* 这恰恰是上面那段「行为侧钉不住它」的实证,不是本类在凑数。
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class TaskResultItemReadWindowTest {
|
||||
|
||||
@Mock private TaskResultItemMapper taskResultItemMapper;
|
||||
@Mock private TransientPayloadStorageService transientPayloadStorageService;
|
||||
|
||||
private TaskResultItemService service;
|
||||
|
||||
@BeforeAll
|
||||
static void initializeTableInfo() {
|
||||
TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""),
|
||||
TaskResultItemEntity.class);
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
service = new TaskResultItemService(taskResultItemMapper, new ObjectMapper(), transientPayloadStorageService);
|
||||
}
|
||||
|
||||
/** 指针化载荷行:payloadJson 只放指针形状,真正的内容由 resolvePayload 现编。 */
|
||||
private static List<TaskResultItemEntity> pointerRows(int count) {
|
||||
List<TaskResultItemEntity> rows = new ArrayList<>(count);
|
||||
for (int i = 0; i < count; i++) {
|
||||
TaskResultItemEntity row = new TaskResultItemEntity();
|
||||
row.setId((long) i + 1);
|
||||
row.setTaskId(9001L);
|
||||
row.setModuleType("APPEARANCE_PATENT");
|
||||
row.setResultId((long) i + 1);
|
||||
row.setItemKey("result:" + (i + 1));
|
||||
row.setPayloadJson("pointer-" + i);
|
||||
rows.add(row);
|
||||
}
|
||||
return rows;
|
||||
}
|
||||
|
||||
/** 从指针串里还原行号:让第 i 行的载荷就是 {"n":i},顺序对不对一眼可见。 */
|
||||
private static int indexOfArgument(String pointer) {
|
||||
return Integer.parseInt(pointer.substring("pointer-".length()));
|
||||
}
|
||||
|
||||
// ---------- 行为:拆窗不许改掉「整份、按序、口径不变」 ----------
|
||||
|
||||
@Test
|
||||
void crossWindowResultIsCompleteAndKeepsRowOrder() {
|
||||
int total = TaskResultItemService.SNAPSHOT_READ_WINDOW * 2 + 3;
|
||||
when(taskResultItemMapper.selectList(any())).thenReturn(pointerRows(total));
|
||||
when(transientPayloadStorageService.extractPointer(anyString())).thenReturn("pointer");
|
||||
when(transientPayloadStorageService.resolvePayload(anyString(), anyString()))
|
||||
.thenAnswer(inv -> "{\"n\":" + indexOfArgument(inv.getArgument(0)) + "}");
|
||||
|
||||
List<Map> out = service.listResultSnapshots(9001L, "APPEARANCE_PATENT", Map.class);
|
||||
|
||||
assertThat(out).as("跨了三个窗口也不能少一行").hasSize(total);
|
||||
for (int i = 0; i < total; i++) {
|
||||
assertThat(out.get(i).get("n")).as("第 " + i + " 行的位置").isEqualTo(i);
|
||||
}
|
||||
verify(transientPayloadStorageService, times(total)).resolvePayload(anyString(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void blankPayloadSkipsOnlyThatRowAndDoesNotTruncateTheRest() {
|
||||
int total = TaskResultItemService.SNAPSHOT_READ_WINDOW + 5;
|
||||
List<TaskResultItemEntity> rows = pointerRows(total);
|
||||
rows.get(3).setPayloadJson(""); // 首窗内的一行空载荷
|
||||
rows.get(total - 1).setPayloadJson(" "); // 末窗内的一行空载荷
|
||||
when(taskResultItemMapper.selectList(any())).thenReturn(rows);
|
||||
when(transientPayloadStorageService.extractPointer(anyString())).thenReturn("pointer");
|
||||
when(transientPayloadStorageService.resolvePayload(anyString(), anyString()))
|
||||
.thenAnswer(inv -> "{\"n\":" + indexOfArgument(inv.getArgument(0)) + "}");
|
||||
|
||||
List<Map> out = service.listResultSnapshots(9001L, "APPEARANCE_PATENT", Map.class);
|
||||
|
||||
assertThat(out).as("空载荷只跳这一行,后面的行必须照样来").hasSize(total - 2);
|
||||
assertThat(out).extracting(m -> m.get("n"))
|
||||
.containsExactlyElementsOf(rows.stream()
|
||||
.filter(r -> r.getPayloadJson() != null && !r.getPayloadJson().trim().isEmpty())
|
||||
.map(r -> indexOfArgument(r.getPayloadJson())).toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void businessFailureFromTheStoreStillSurfacesUnchanged() {
|
||||
int total = TaskResultItemService.SNAPSHOT_READ_WINDOW + 2;
|
||||
when(taskResultItemMapper.selectList(any())).thenReturn(pointerRows(total));
|
||||
when(transientPayloadStorageService.extractPointer(anyString())).thenReturn("pointer");
|
||||
when(transientPayloadStorageService.resolvePayload(anyString(), anyString()))
|
||||
.thenAnswer(inv -> "{\"n\":" + indexOfArgument(inv.getArgument(0)) + "}");
|
||||
when(transientPayloadStorageService.resolvePayload(org.mockito.ArgumentMatchers.eq("pointer-1"), anyString()))
|
||||
.thenThrow(new BusinessException("读取任务结果明细失败"));
|
||||
|
||||
BusinessException thrown = assertThrows(BusinessException.class,
|
||||
() -> service.listResultSnapshots(9001L, "APPEARANCE_PATENT", Map.class));
|
||||
|
||||
assertThat(thrown).hasMessage("读取任务结果明细失败");
|
||||
}
|
||||
|
||||
@Test
|
||||
void unexpectedStoreFailureIsWrappedIntoBusinessExceptionAsBefore() {
|
||||
when(taskResultItemMapper.selectList(any())).thenReturn(pointerRows(TaskResultItemService.SNAPSHOT_READ_WINDOW + 2));
|
||||
when(transientPayloadStorageService.extractPointer(anyString())).thenReturn("pointer");
|
||||
when(transientPayloadStorageService.resolvePayload(anyString(), anyString()))
|
||||
.thenThrow(new IllegalStateException("对象存储 500"));
|
||||
|
||||
BusinessException thrown = assertThrows(BusinessException.class,
|
||||
() -> service.listResultSnapshots(9001L, "APPEARANCE_PATENT", Map.class));
|
||||
|
||||
assertThat(thrown).hasMessage("读取任务结果明细失败");
|
||||
}
|
||||
|
||||
// ---------- 源码绊线:窗口这道闸本身 ----------
|
||||
|
||||
@Test
|
||||
void submissionIsWindowBoundedNotWholeResultSetAtOnce() {
|
||||
String body = methodBody(SourceScan.withoutComments(SourceScan.sourceText("TaskResultItemService.java")),
|
||||
"List<T> listResultSnapshots(Long taskId, String moduleType, Class<T> clazz)");
|
||||
|
||||
assertThat(body).as("提交循环必须由窗口常量钳制").contains("from += SNAPSHOT_READ_WINDOW");
|
||||
assertThat(body).as("每次只提交一窗的行").contains("Math.min(from + SNAPSHOT_READ_WINDOW");
|
||||
assertThat(body.indexOf("from += SNAPSHOT_READ_WINDOW"))
|
||||
.as("submit 必须落在窗口循环里面,不是回到整量 for (row : rows)")
|
||||
.isLessThan(body.indexOf("snapshotReadPool.submit("));
|
||||
assertThat(countOf(body, "snapshotReadPool.submit("))
|
||||
.as("提交点只许一处:多一处就多一份没人收割的副本").isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void windowMustStayBelowThePoolQueueSoTheCallerNeverReadsInline() {
|
||||
assertThat(TaskResultItemService.SNAPSHOT_READ_WINDOW)
|
||||
.as("窗口大于线程池队列就会撞上 CallerRunsPolicy,把调用线程变成同步读对象存储的那个人")
|
||||
.isBetween(1, ThreadPools.DEFAULT_QUEUE_CAPACITY);
|
||||
}
|
||||
|
||||
/** 从方法签名起做括号配对,取出方法体(注释已由 withoutComments 剥掉)。 */
|
||||
private static String methodBody(String source, String signature) {
|
||||
int start = source.indexOf(signature);
|
||||
assertThat(start).as("必须存在方法 " + signature).isGreaterThan(-1);
|
||||
int open = source.indexOf('{', start);
|
||||
assertThat(open).as(signature + " 的方法体起点").isGreaterThan(-1);
|
||||
int depth = 0;
|
||||
for (int i = open; i < source.length(); i++) {
|
||||
char c = source.charAt(i);
|
||||
if (c == '{') {
|
||||
depth++;
|
||||
} else if (c == '}') {
|
||||
depth--;
|
||||
if (depth == 0) {
|
||||
return source.substring(open + 1, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new AssertionError(signature + " 括号未闭合");
|
||||
}
|
||||
|
||||
private static int countOf(String haystack, String needle) {
|
||||
int count = 0;
|
||||
for (int at = haystack.indexOf(needle); at >= 0; at = haystack.indexOf(needle, at + needle.length())) {
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
||||
+143
@@ -0,0 +1,143 @@
|
||||
package com.nanri.aiimage.modules.task.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.nanri.aiimage.modules.task.mapper.TaskResultPayloadMapper;
|
||||
import com.nanri.aiimage.modules.task.model.entity.TaskResultPayloadEntity;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.transaction.support.TransactionSynchronization;
|
||||
import org.springframework.transaction.support.TransactionSynchronizationManager;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* P1-B8:结果载荷的<b>对象存储删除</b>不得落在 DB 事务里。
|
||||
*
|
||||
* <p>原形状是 {@code @Transactional} 的 {@code deleteLatest} 里先同步删对象、再删行。两头的代价都真:
|
||||
* 一次删除超时 120s、重试 3 次,还要带一次引用计数反查——DB 连接被这段时间占住(连接池单节点 30,
|
||||
* 几次并发删除即可打满并波及其它请求);而回滚时对象已经没了、行还在=读得到一个不存在的载荷。
|
||||
* 结果明细行({@link TaskResultItemService} 的 deletePayloadsAfterCommit,2026-09)早已改成提交后删,
|
||||
* 本类钉的是当时漏掉的这一处。
|
||||
*
|
||||
* <p>反向自证:把 {@code deletePayloadIfPresent} 挪回事务内(或去掉 afterCommit 分支),第一格即红;
|
||||
* 把顺序倒回来(先删对象再删行),第三格即红。
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class TaskResultPayloadDeleteTxBoundaryTest {
|
||||
|
||||
private static final String POINTER = "rustfs:task-payload/APPEARANCE_PATENT/9001/abc.json";
|
||||
|
||||
@Mock private TaskResultPayloadMapper taskResultPayloadMapper;
|
||||
@Mock private TransientPayloadStorageService transientPayloadStorageService;
|
||||
|
||||
private TaskResultPayloadService service;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
service = new TaskResultPayloadService(taskResultPayloadMapper, new ObjectMapper(),
|
||||
transientPayloadStorageService);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void clearSynchronizationIfLeftActive() {
|
||||
if (TransactionSynchronizationManager.isSynchronizationActive()) {
|
||||
TransactionSynchronizationManager.clearSynchronization();
|
||||
}
|
||||
}
|
||||
|
||||
private void stubRowWithPayload(String payloadJson) {
|
||||
TaskResultPayloadEntity existing = new TaskResultPayloadEntity();
|
||||
existing.setId(11L);
|
||||
existing.setTaskId(9001L);
|
||||
existing.setModuleType("APPEARANCE_PATENT");
|
||||
existing.setPayloadJson(payloadJson);
|
||||
when(taskResultPayloadMapper.selectOne(any(LambdaQueryWrapper.class))).thenReturn(existing);
|
||||
}
|
||||
|
||||
/** 没有活动同步器时 Spring 直接抛 IllegalState;测试里按「没注册任何同步器」读成空。 */
|
||||
private static List<TransactionSynchronization> registered() {
|
||||
return TransactionSynchronizationManager.isSynchronizationActive()
|
||||
? TransactionSynchronizationManager.getSynchronizations()
|
||||
: List.of();
|
||||
}
|
||||
|
||||
@Test
|
||||
void 有事务上下文时对象存储要等提交之后才动() {
|
||||
stubRowWithPayload(POINTER);
|
||||
TransactionSynchronizationManager.initSynchronization();
|
||||
|
||||
service.deleteLatest(9001L, "APPEARANCE_PATENT", "scope-a");
|
||||
|
||||
verify(taskResultPayloadMapper, times(1)).deleteById(11L);
|
||||
verify(transientPayloadStorageService, never()).deletePayloadIfPresent(anyString());
|
||||
|
||||
List<TransactionSynchronization> synchronizations = registered();
|
||||
assertEquals(1, synchronizations.size(), "提交后删除必须挂在事务同步器上,否则回滚也会照删");
|
||||
synchronizations.forEach(TransactionSynchronization::afterCommit);
|
||||
verify(transientPayloadStorageService, times(1)).deletePayloadIfPresent(POINTER);
|
||||
}
|
||||
|
||||
/** 没有事务上下文(被非事务调用方直接用)时**立即删**——静默不删就是留孤儿对象。 */
|
||||
@Test
|
||||
void 无事务上下文时仍然删到() {
|
||||
stubRowWithPayload(POINTER);
|
||||
|
||||
service.deleteLatest(9001L, "APPEARANCE_PATENT", "scope-a");
|
||||
|
||||
verify(taskResultPayloadMapper, times(1)).deleteById(11L);
|
||||
verify(transientPayloadStorageService, times(1)).deletePayloadIfPresent(POINTER);
|
||||
assertEquals(0, registered().size(), "不在事务里就别往线程上挂同步器(挂了也没人触发它)");
|
||||
}
|
||||
|
||||
/** 落库失败=这单还没删:对象存储一次都不许碰(否则回滚后行还在、载荷已经没了)。 */
|
||||
@Test
|
||||
void 落库失败不留半成品() {
|
||||
stubRowWithPayload(POINTER);
|
||||
when(taskResultPayloadMapper.deleteById(11L)).thenThrow(new IllegalStateException("db down"));
|
||||
|
||||
assertThrows(IllegalStateException.class,
|
||||
() -> service.deleteLatest(9001L, "APPEARANCE_PATENT", "scope-a"));
|
||||
|
||||
verify(transientPayloadStorageService, never()).deletePayloadIfPresent(anyString());
|
||||
}
|
||||
|
||||
/** 行里没有载荷值时不注册提交后删除:一次空的网络往返都不该发。 */
|
||||
@Test
|
||||
void 空载荷不注册提交后删除() {
|
||||
stubRowWithPayload(null);
|
||||
TransactionSynchronizationManager.initSynchronization();
|
||||
|
||||
service.deleteLatest(9001L, "APPEARANCE_PATENT", "scope-a");
|
||||
|
||||
verify(taskResultPayloadMapper, times(1)).deleteById(11L);
|
||||
assertEquals(0, registered().size(), "没有指针就没有要删的对象,别挂一个注定空跑的同步器");
|
||||
verify(transientPayloadStorageService, never()).deletePayloadIfPresent(anyString());
|
||||
}
|
||||
|
||||
/** 没有这一行=什么都不做(既不删库也不注册删除)。 */
|
||||
@Test
|
||||
void 行不存在时整条链路不动() {
|
||||
when(taskResultPayloadMapper.selectOne(any(LambdaQueryWrapper.class))).thenReturn(null);
|
||||
TransactionSynchronizationManager.initSynchronization();
|
||||
|
||||
service.deleteLatest(9001L, "APPEARANCE_PATENT", "scope-a");
|
||||
|
||||
verify(taskResultPayloadMapper, never()).deleteById(11L);
|
||||
assertEquals(0, registered().size());
|
||||
verify(transientPayloadStorageService, never()).deletePayloadIfPresent(anyString());
|
||||
}
|
||||
}
|
||||
+48
-5
@@ -10,11 +10,13 @@ import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
|
||||
import com.nanri.aiimage.modules.task.spi.ClientTaskRetrySpi;
|
||||
import org.apache.ibatis.builder.MapperBuilderAssistant;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
@@ -23,6 +25,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
@@ -143,10 +146,18 @@ class TaskResumeServiceTest {
|
||||
@Test
|
||||
void 已有重试任务时跳过避免重复克隆() {
|
||||
FileTaskMapper mapper = mock(FileTaskMapper.class);
|
||||
when(mapper.selectList(any())).thenReturn(List.of(
|
||||
failedTask(28587L, "SIMILAR_ASIN", "任务心跳超时,已自动失败")));
|
||||
// 第一次是"不支持重试的中断任务数",第二次是"是否已有重试子任务"
|
||||
when(mapper.selectCount(any())).thenReturn(0L, 1L);
|
||||
FileTaskEntity origin = failedTask(28587L, "SIMILAR_ASIN", "任务心跳超时,已自动失败");
|
||||
FileTaskEntity child = new FileTaskEntity();
|
||||
child.setId(28600L);
|
||||
child.setResumeOfTaskId(28587L);
|
||||
// P1-B5 之后「是否已有重试子任务」不再是每行一次 selectCount,而是循环前一次批量 selectList。
|
||||
// 候选查询与反查都落在同一个 selectList 上,按投影列区分:反查那一支只取 resume_of_task_id。
|
||||
when(mapper.selectList(any())).thenAnswer(invocation -> {
|
||||
com.baomidou.mybatisplus.core.conditions.AbstractWrapper<?, ?, ?> wrapper = invocation.getArgument(0);
|
||||
String projection = String.valueOf(wrapper.getSqlSelect());
|
||||
return projection.contains("resume_of_task_id") ? List.of(child) : List.of(origin);
|
||||
});
|
||||
when(mapper.selectCount(any())).thenReturn(0L);
|
||||
ClientTaskRetrySpi spi = mock(ClientTaskRetrySpi.class);
|
||||
when(spi.moduleType()).thenReturn("SIMILAR_ASIN");
|
||||
TaskResumeService service = service(mapper, List.of(spi), true, 3);
|
||||
@@ -157,6 +168,37 @@ class TaskResumeServiceTest {
|
||||
assertEquals(0, stats.resumedTaskCount);
|
||||
}
|
||||
|
||||
/**
|
||||
* P1-B6 同族:反查子任务与「补检中」两条判定都必须**批量**取(循环前一次),不许退回逐行点查——
|
||||
* 逐行形状在 500 个候选时是 1,000 次往返,且 {@code resume_of_task_id} 那列在 V187 之前无索引。
|
||||
* 判据=selectList 只发生「清账 + 候选 + 反查」三次、selectCount 只发生一次(不支持重试的计数),
|
||||
* 且批量档拿到的是这一轮的全部候选 id。
|
||||
*/
|
||||
@Test
|
||||
void 两条前置判定按批量档取不逐行点查() {
|
||||
FileTaskMapper mapper = mock(FileTaskMapper.class);
|
||||
when(mapper.selectList(any())).thenReturn(List.of(
|
||||
failedTask(28587L, "SIMILAR_ASIN", "任务心跳超时,已自动失败"),
|
||||
failedTask(28588L, "SIMILAR_ASIN", "任务心跳超时,已自动失败")));
|
||||
when(mapper.selectCount(any())).thenReturn(0L);
|
||||
TaskFileJobService jobs = mock(TaskFileJobService.class);
|
||||
ClientTaskRetrySpi spi = mock(ClientTaskRetrySpi.class);
|
||||
when(spi.moduleType()).thenReturn("SIMILAR_ASIN");
|
||||
TaskResumeService service = service(mapper, jobs, List.of(spi), true, 3);
|
||||
|
||||
TaskResumeService.ResumeStats stats = service.resumeInterruptedTasks();
|
||||
|
||||
assertEquals(2, stats.resumedTaskCount);
|
||||
@SuppressWarnings("unchecked")
|
||||
ArgumentCaptor<Collection<Long>> captor =
|
||||
(ArgumentCaptor<Collection<Long>>) (ArgumentCaptor<?>) ArgumentCaptor.forClass(Collection.class);
|
||||
verify(jobs).taskIdsWithUnfinishedAssembleJob(captor.capture());
|
||||
assertTrue(captor.getValue().containsAll(List.of(28587L, 28588L)),
|
||||
"批量档必须一次带走本轮全部候选,漏一个就是退回逐行点查");
|
||||
verify(mapper, times(3)).selectList(any());
|
||||
verify(mapper, times(1)).selectCount(any());
|
||||
}
|
||||
|
||||
/**
|
||||
* 现网 2026-10-24:一枚 760 行的外观专利任务因交付缺口把组装作业排回补检(作业还在
|
||||
* PENDING/RUNNING),任务行却已是 FAILED,于是被巡检连克隆成 4 枚、每枚全量重跑一遍,
|
||||
@@ -170,7 +212,8 @@ class TaskResumeServiceTest {
|
||||
failedTask(32201L, "APPEARANCE_PATENT", "有 3 行抓取失败(未取得商品页数据):B07P5TR7Q5")));
|
||||
when(mapper.selectCount(any())).thenReturn(0L);
|
||||
TaskFileJobService jobs = mock(TaskFileJobService.class);
|
||||
when(jobs.hasUnfinishedAssembleJob(32201L)).thenReturn(true);
|
||||
// P1-B5:补检判定从逐行 hasUnfinishedAssembleJob 折成循环前一次批量档。
|
||||
when(jobs.taskIdsWithUnfinishedAssembleJob(any())).thenReturn(java.util.Set.of(32201L));
|
||||
ClientTaskRetrySpi spi = mock(ClientTaskRetrySpi.class);
|
||||
when(spi.moduleType()).thenReturn("APPEARANCE_PATENT");
|
||||
TaskResumeService service = service(mapper, jobs, List.of(spi), true, 3);
|
||||
|
||||
@@ -38,8 +38,8 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
* <p><b>一处与 plan 原文不符的读数,按"先怀疑尺子"的流程处理并留在脸上</b>:plan/AGENTS §1 记的是「9 支文件」,
|
||||
* 那条数是 {@code grep -rl 'sourceText('} 数出来的,而第 9 支是 {@code SourceScan.java:22} 里
|
||||
* {@code public static String sourceText(String fileName)} 这行<b>被调方自己的声明</b>——它不是调用点。
|
||||
* 所以真实形状是 <b>调用点/调用方/引用方三类数各自成立</b>(2026-10-28 v1.1 Phase 0 起调用方 16 支、
|
||||
* 裸名 25 个,增量逐笔登记在 {@code REGISTERED_BARE_NAMES} 的注释里)。下面第 3 格把这些数同时钉住,
|
||||
* 所以真实形状是 <b>调用点/调用方/引用方三类数各自成立</b>(2026-11-03 Phase 5C 起调用方 19 支、
|
||||
* 裸名 26 个,增量逐笔登记在 {@code REGISTERED_BARE_NAMES} 的注释里)。下面第 3 格把这些数同时钉住,
|
||||
* 免得下一个人拿 {@code grep} 再数一遍又把声明算进去。</p>
|
||||
*/
|
||||
class SourceScanTest {
|
||||
@@ -94,7 +94,13 @@ class SourceScanTest {
|
||||
// TaskFailureCategoryGuardrailTest 要钉「写了失败文案就必须同笔写分类列」这条连线:
|
||||
// 用 mock 造任务实体测分类,挡不住"新增一处 setErrorMessage 忘了写列"——
|
||||
// 那正是"改一句文案静默改判死"的复发路径,只能打在源码上。顺带钉 ledger 不抄第二份 markers。
|
||||
"TaskFailureLedger.java");
|
||||
"TaskFailureLedger.java",
|
||||
// —— 2026-11-03 Phase 5C(P1-B7 结果集分窗读取)追加(1 名)——
|
||||
// TaskResultItemReadWindowTest 要钉「提交必须被窗口钳住」这条:线程池只有 8 个线程,
|
||||
// 一次提交 600 行与一次提交 256 行在"同时开始读几行"这个 observable 上读数相同
|
||||
// (都被 8 卡住),差异只体现在**堆**上(同时在飞的载荷副本),mock 数不出来;
|
||||
// 行为四格只能钉住"不漏行/不乱序/异常口径不变",窗口这道闸本身必须打在源码上。
|
||||
"TaskResultItemService.java");
|
||||
|
||||
@TempDir
|
||||
Path tmp;
|
||||
@@ -110,7 +116,7 @@ class SourceScanTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void census_唯一裸名恰为25且与已登记集合相等() {
|
||||
void census_唯一裸名恰为26且与已登记集合相等() {
|
||||
Set<String> found = SourceScan.censusSourceTextCallSites().stream()
|
||||
.map(SourceScan.CallSite::bareName)
|
||||
.collect(Collectors.toSet());
|
||||
@@ -118,11 +124,11 @@ class SourceScanTest {
|
||||
assertThat(found)
|
||||
.as("唯一裸名集合必须与类头台账逐字相等——多一个说明有人新引了单文件口径,少一个说明尺子漏扫")
|
||||
.isEqualTo(REGISTERED_BARE_NAMES)
|
||||
.hasSize(25);
|
||||
.hasSize(26);
|
||||
}
|
||||
|
||||
@Test
|
||||
void census_调用方文件恰为18而声明件在外() {
|
||||
void census_调用方文件恰为19而声明件在外() {
|
||||
List<SourceScan.CallSite> sites = SourceScan.censusSourceTextCallSites();
|
||||
Set<String> callerFiles = sites.stream()
|
||||
.map(SourceScan.CallSite::relativeFile)
|
||||
@@ -131,9 +137,10 @@ class SourceScanTest {
|
||||
String declarationFile = "src/test/java/com/nanri/aiimage/testutil/SourceScan.java";
|
||||
|
||||
assertThat(callerFiles)
|
||||
.as("真实字面量调用点落在 18 支调用方文件里(8 支既有 + 2026-10-28 v1.1 Phase 0 预置契约 8 支"
|
||||
+ " + 2026-11-02 归属死锁绊线 1 支 + 2026-11-03 B51-R1 双写绊线 1 支)")
|
||||
.hasSize(18);
|
||||
.as("真实字面量调用点落在 19 支调用方文件里(8 支既有 + 2026-10-28 v1.1 Phase 0 预置契约 8 支"
|
||||
+ " + 2026-11-02 归属死锁绊线 1 支 + 2026-11-03 B51-R1 双写绊线 1 支"
|
||||
+ " + 2026-11-03 Phase 5C P1-B7 窗口绊线 1 支)")
|
||||
.hasSize(19);
|
||||
assertThat(callerFiles)
|
||||
.as("plan 原文记的「9 支」= grep -rl 'sourceText(' 的条数,第 9 支是被调方自己的声明行"
|
||||
+ "(SourceScan.java:22 的 `public static String sourceText(String fileName)`),"
|
||||
|
||||
Reference in New Issue
Block a user