fix(断点续传): 外观/结果作业/去重三处中断语义补齐,重启不再静默丢行
- 外观专利行标改两态(in-flight 15min / processed 24h):只有 CAS 写回 payload 成功 才转「已处理」,异常、覆盖率不足、CAS 打满等未落库出口一律撤销标记 —— 修「发 LLM 请求前就盖章,进程被杀的那一批永不再补检」 - 结果文件作业开机自回收:按 owner_instance_id 只收本机名下 RUNNING 且心跳超 90s 的行; 卡死阈值 30 → 5 分钟,重启接回从最长半小时压到 5 分钟 - 外观结果表缺检行显式写「未取得检测结果」并判失败,不再留空靠 0.5 覆盖率阈值蒙过 - 去重进度改 NodeSharedStore 跨节点共享,runId 用已落库的 task_no:轮询打到另一节点或 发版重启后仍能读到真实进度、已完成文件仍可下载;共享态缺失时按 task_no 从库重建; 逐文件心跳防 StaleTaskRepair 把长任务误判死;开机把本机残留 RUNNING 判失败并给提示
This commit is contained in:
+273
@@ -0,0 +1,273 @@
|
||||
package com.nanri.aiimage.modules.appearancepatent.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.service.DistributedJobLockService;
|
||||
import com.nanri.aiimage.common.service.ResultDownloadResolver;
|
||||
import com.nanri.aiimage.config.AppearancePatentProperties;
|
||||
import com.nanri.aiimage.config.InstanceMetadata;
|
||||
import com.nanri.aiimage.config.StorageProperties;
|
||||
import com.nanri.aiimage.modules.appearancepatent.client.AppearancePatentLlmClient;
|
||||
import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentResultRowDto;
|
||||
import com.nanri.aiimage.modules.file.service.LocalFileStorageService;
|
||||
import com.nanri.aiimage.modules.file.service.oss.OssStorageService;
|
||||
import com.nanri.aiimage.modules.task.mapper.FileResultMapper;
|
||||
import com.nanri.aiimage.modules.task.mapper.FileTaskMapper;
|
||||
import com.nanri.aiimage.modules.task.mapper.TaskChunkMapper;
|
||||
import com.nanri.aiimage.modules.task.mapper.TaskScopeStateMapper;
|
||||
import com.nanri.aiimage.modules.task.model.entity.FileResultEntity;
|
||||
import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
|
||||
import com.nanri.aiimage.modules.task.model.entity.TaskChunkEntity;
|
||||
import com.nanri.aiimage.modules.task.model.entity.TaskScopeStateEntity;
|
||||
import com.nanri.aiimage.modules.task.service.TaskDistributedLockService;
|
||||
import com.nanri.aiimage.modules.task.service.TaskFileJobService;
|
||||
import com.nanri.aiimage.modules.task.service.TaskHeartbeatPositionService;
|
||||
import com.nanri.aiimage.modules.task.service.TaskProgressLightAssembler;
|
||||
import com.nanri.aiimage.modules.task.service.TaskProgressSnapshotService;
|
||||
import com.nanri.aiimage.modules.task.service.TransientPayloadStorageService;
|
||||
import com.nanri.aiimage.modules.usersecret.service.UserApiSecretService;
|
||||
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.InOrder;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Spy;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
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.anyLong;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.inOrder;
|
||||
import static org.mockito.Mockito.lenient;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* 「行什么时候算检测完」的时序约束。
|
||||
*
|
||||
* <p>合批之后一次请求带走 5–10 行,一次中断就是整批白做。原实现在收集候选时(发请求**之前**)
|
||||
* 就把行标成已处理,所以发版重启留下的不是"稍后补检",而是"永不再检"。
|
||||
* 这里钉住两条:申领发生在收集阶段、完成标记只允许出现在载荷 CAS 落库成功之后。
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class AppearancePatentRowMarkTimingTest {
|
||||
|
||||
private static final Long TASK_ID = 29436L;
|
||||
private static final Long JOB_ID = 20788L;
|
||||
private static final String SCOPE_HASH = "scope-hash-1";
|
||||
private static final int CHUNK_INDEX = 1;
|
||||
private static final String POINTER = "ptr-chunk-1.json";
|
||||
private static final String UNRESOLVED_ROWS =
|
||||
"[{\"rowToken\":\"r1\",\"asin\":\"B0A0000001\",\"title\":\"RUNNING SHOE\",\"url\":\"http://img/1.jpg\",\"groupKey\":\"g1\"},"
|
||||
+ "{\"rowToken\":\"r2\",\"asin\":\"B0A0000002\",\"title\":\"COTTON BAG\",\"url\":\"http://img/2.jpg\",\"groupKey\":\"g2\"}]";
|
||||
private static final String RESOLVED_ROWS =
|
||||
"[{\"rowToken\":\"r1\",\"asin\":\"B0A0000001\",\"title\":\"RUNNING SHOE\",\"url\":\"http://img/1.jpg\",\"groupKey\":\"g1\",\"titleRisk\":\"无侵权\",\"appearanceRisk\":\"无侵权\",\"conclusion\":\"无侵权\"},"
|
||||
+ "{\"rowToken\":\"r2\",\"asin\":\"B0A0000002\",\"title\":\"COTTON BAG\",\"url\":\"http://img/2.jpg\",\"groupKey\":\"g2\",\"titleRisk\":\"侵权\",\"appearanceRisk\":\"侵权\",\"conclusion\":\"侵权\"}]";
|
||||
|
||||
@Mock private LocalFileStorageService localFileStorageService;
|
||||
@Mock private OssStorageService ossStorageService;
|
||||
@Mock private ResultDownloadResolver resultDownloadResolver;
|
||||
@Mock private StorageProperties storageProperties;
|
||||
@Mock private FileTaskMapper fileTaskMapper;
|
||||
@Mock private FileResultMapper fileResultMapper;
|
||||
@Mock private TaskScopeStateMapper taskScopeStateMapper;
|
||||
@Mock private TaskChunkMapper taskChunkMapper;
|
||||
@Spy private ObjectMapper objectMapper = new ObjectMapper();
|
||||
@Mock private AppearancePatentLlmClient llmClient;
|
||||
@Mock private AppearancePatentTaskCacheService taskCacheService;
|
||||
@Mock private AppearancePatentProperties properties;
|
||||
@Mock private TaskFileJobService taskFileJobService;
|
||||
@Mock private TaskProgressSnapshotService taskProgressSnapshotService;
|
||||
@Mock private TaskHeartbeatPositionService taskHeartbeatPositionService;
|
||||
@Mock private TransientPayloadStorageService transientPayloadStorageService;
|
||||
@Mock private PlatformTransactionManager transactionManager;
|
||||
@Mock private DistributedJobLockService distributedJobLockService;
|
||||
@Mock private TaskDistributedLockService taskDistributedLockService;
|
||||
@Mock private InstanceMetadata instanceMetadata;
|
||||
@Mock private TaskProgressLightAssembler taskProgressLightAssembler;
|
||||
@Mock private UserApiSecretService userApiSecretService;
|
||||
|
||||
@InjectMocks private AppearancePatentTaskService service;
|
||||
|
||||
@BeforeAll
|
||||
static void initializeMybatisMetadata() {
|
||||
MapperBuilderAssistant assistant = new MapperBuilderAssistant(new MybatisConfiguration(), "");
|
||||
TableInfoHelper.initTableInfo(assistant, FileTaskEntity.class);
|
||||
TableInfoHelper.initTableInfo(assistant, FileResultEntity.class);
|
||||
TableInfoHelper.initTableInfo(assistant, TaskChunkEntity.class);
|
||||
TableInfoHelper.initTableInfo(assistant, TaskScopeStateEntity.class);
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
lenient().when(properties.getLlmBatchSize()).thenReturn(2);
|
||||
lenient().when(properties.getFlushPendingMinutes()).thenReturn(1);
|
||||
lenient().when(transientPayloadStorageService.isSharedWriteEnabled()).thenReturn(true);
|
||||
lenient().when(userApiSecretService.findPlainValue(any(), anyString())).thenReturn("");
|
||||
lenient().when(transientPayloadStorageService.resolvePayload(eq(POINTER), anyString())).thenReturn(UNRESOLVED_ROWS);
|
||||
lenient().when(taskChunkMapper.selectList(any())).thenReturn(List.of(chunk()));
|
||||
lenient().when(taskChunkMapper.selectOne(any())).thenReturn(chunk());
|
||||
}
|
||||
|
||||
/** 收集阶段只申领、不完成:这是「先干活后标记」和旧行为唯一看得见的区别。 */
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void collectionClaimsRowsWithoutCompletingThem() {
|
||||
when(taskCacheService.beginRowInspection(eq(TASK_ID), anyString())).thenReturn(true);
|
||||
|
||||
List<AppearancePatentResultRowDto> candidates = (List<AppearancePatentResultRowDto>) ReflectionTestUtils
|
||||
.invokeMethod(service, "collectPendingLlmCandidates", List.of(chunk()));
|
||||
|
||||
assertEquals(2, candidates.size());
|
||||
verify(taskCacheService).beginRowInspection(TASK_ID, "r1");
|
||||
verify(taskCacheService).beginRowInspection(TASK_ID, "r2");
|
||||
verify(taskCacheService, never()).completeRowInspection(anyLong(), any());
|
||||
}
|
||||
|
||||
/** 已经被别的入口申领走的行不再进来,否则会双发 LLM、双扣次数。 */
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void rowsHeldByAnotherEntryAreSkipped() {
|
||||
when(taskCacheService.beginRowInspection(eq(TASK_ID), eq("r1"))).thenReturn(true);
|
||||
when(taskCacheService.beginRowInspection(eq(TASK_ID), eq("r2"))).thenReturn(false);
|
||||
|
||||
List<AppearancePatentResultRowDto> candidates = (List<AppearancePatentResultRowDto>) ReflectionTestUtils
|
||||
.invokeMethod(service, "collectPendingLlmCandidates", List.of(chunk()));
|
||||
|
||||
assertEquals(1, candidates.size());
|
||||
assertEquals("r1", candidates.get(0).getRowToken());
|
||||
}
|
||||
|
||||
/** 落库 CAS 成功之后才允许打完成标记——顺序错了就等于回到"发版丢行"的老 bug。 */
|
||||
@Test
|
||||
void completionMarkComesAfterPayloadCasUpdate() throws Exception {
|
||||
when(taskCacheService.beginRowInspection(eq(TASK_ID), anyString())).thenReturn(true);
|
||||
when(llmClient.inspectRows(any(), any(), any())).thenReturn(rows(RESOLVED_ROWS));
|
||||
stubMergeWrite(1);
|
||||
|
||||
submit();
|
||||
|
||||
InOrder order = inOrder(taskChunkMapper, taskCacheService);
|
||||
order.verify(taskChunkMapper).update(any(), any());
|
||||
order.verify(taskCacheService).completeRowInspection(eq(TASK_ID), eq(List.of("r1", "r2")));
|
||||
}
|
||||
|
||||
/**
|
||||
* LLM 整批失败:失败原因照样落库(用户看得见),但**不打完成标记**,而是撤销处理中标记——
|
||||
* assemble job 的 5 次重试几分钟内就跑完了,等 TTL 过期的话这批行在整个重试窗口里都申领不到。
|
||||
* 旧行为是"永远记着这次失败",再不会重检。
|
||||
*/
|
||||
@Test
|
||||
void failedLlmBatchLeavesRowsRetryable() throws Exception {
|
||||
when(taskCacheService.beginRowInspection(eq(TASK_ID), anyString())).thenReturn(true);
|
||||
when(llmClient.inspectRows(any(), any(), any())).thenThrow(new IllegalStateException("LLM 网关无应答"));
|
||||
when(llmClient.markRowsFailed(any(), anyString())).thenReturn(rows(UNRESOLVED_ROWS));
|
||||
stubMergeWrite(1);
|
||||
|
||||
submit();
|
||||
|
||||
verify(taskCacheService, never()).completeRowInspection(anyLong(), any());
|
||||
verify(taskCacheService).releaseRowInspection(TASK_ID, List.of("r1", "r2"));
|
||||
}
|
||||
|
||||
/** 成功批次不能撤销处理中标记:撤销是给"下一轮立刻重试"用的,成功行重跑就是重复扣次数。 */
|
||||
@Test
|
||||
void successfulBatchDoesNotReleaseInFlightMarks() throws Exception {
|
||||
when(taskCacheService.beginRowInspection(eq(TASK_ID), anyString())).thenReturn(true);
|
||||
when(llmClient.inspectRows(any(), any(), any())).thenReturn(rows(RESOLVED_ROWS));
|
||||
stubMergeWrite(1);
|
||||
|
||||
submit();
|
||||
|
||||
verify(taskCacheService, never()).releaseRowInspection(anyLong(), any());
|
||||
}
|
||||
|
||||
/** 结果拿到了但载荷写不进去(CAS 重试耗尽):同样不能算完成,否则结果是内存里的幻影。 */
|
||||
@Test
|
||||
void exhaustedCasDoesNotCompleteRows() throws Exception {
|
||||
when(taskCacheService.beginRowInspection(eq(TASK_ID), anyString())).thenReturn(true);
|
||||
when(llmClient.inspectRows(any(), any(), any())).thenReturn(rows(RESOLVED_ROWS));
|
||||
stubMergeWrite(0);
|
||||
|
||||
assertThrows(IllegalStateException.class, this::submit);
|
||||
|
||||
verify(taskCacheService, never()).completeRowInspection(anyLong(), any());
|
||||
verify(taskCacheService).releaseRowInspection(TASK_ID, List.of("r1", "r2"));
|
||||
}
|
||||
|
||||
/** 发送前续一次处理中标记,避免一批跑得比 TTL 长时被并发入口抢走。 */
|
||||
@Test
|
||||
void inFlightMarkIsRenewedRightBeforeSending() throws Exception {
|
||||
when(taskCacheService.beginRowInspection(eq(TASK_ID), anyString())).thenReturn(true);
|
||||
when(llmClient.inspectRows(any(), any(), any())).thenReturn(rows(RESOLVED_ROWS));
|
||||
stubMergeWrite(1);
|
||||
|
||||
submit();
|
||||
|
||||
ArgumentCaptor<List<String>> keys = ArgumentCaptor.forClass(List.class);
|
||||
InOrder order = inOrder(taskCacheService, llmClient);
|
||||
order.verify(taskCacheService).touchRowInspection(eq(TASK_ID), keys.capture());
|
||||
order.verify(llmClient).inspectRows(any(), any(), any());
|
||||
assertEquals(List.of("r1", "r2"), keys.getValue());
|
||||
}
|
||||
|
||||
// ===== 辅助 =====
|
||||
|
||||
private void submit() {
|
||||
ReflectionTestUtils.invokeMethod(service, "submitLlmBatches",
|
||||
task(), new FileResultEntity(), job(), List.of(chunk()), Map.of());
|
||||
}
|
||||
|
||||
private void stubMergeWrite(int updatedRows) {
|
||||
lenient().when(transientPayloadStorageService.storeChunkPayloadVersioned(
|
||||
anyString(), anyLong(), anyString(), any(), anyString())).thenReturn("sibling-1");
|
||||
when(taskChunkMapper.update(any(), any())).thenReturn(updatedRows);
|
||||
lenient().when(taskChunkMapper.selectById(anyLong())).thenReturn(chunk());
|
||||
}
|
||||
|
||||
private List<AppearancePatentResultRowDto> rows(String json) throws Exception {
|
||||
return List.of(new ObjectMapper().readValue(json, AppearancePatentResultRowDto[].class));
|
||||
}
|
||||
|
||||
private FileTaskEntity task() {
|
||||
FileTaskEntity task = new FileTaskEntity();
|
||||
task.setId(TASK_ID);
|
||||
task.setModuleType(AppearancePatentTaskService.MODULE_TYPE);
|
||||
task.setUserId(7L);
|
||||
return task;
|
||||
}
|
||||
|
||||
private com.nanri.aiimage.modules.task.model.entity.TaskFileJobEntity job() {
|
||||
com.nanri.aiimage.modules.task.model.entity.TaskFileJobEntity job =
|
||||
new com.nanri.aiimage.modules.task.model.entity.TaskFileJobEntity();
|
||||
job.setId(JOB_ID);
|
||||
job.setTaskId(TASK_ID);
|
||||
return job;
|
||||
}
|
||||
|
||||
private TaskChunkEntity chunk() {
|
||||
TaskChunkEntity chunk = new TaskChunkEntity();
|
||||
chunk.setId(11L);
|
||||
chunk.setTaskId(TASK_ID);
|
||||
chunk.setModuleType(AppearancePatentTaskService.MODULE_TYPE);
|
||||
chunk.setScopeHash(SCOPE_HASH);
|
||||
chunk.setChunkIndex(CHUNK_INDEX);
|
||||
chunk.setPayloadJson(POINTER);
|
||||
chunk.setPayloadHash("hash-old");
|
||||
return chunk;
|
||||
}
|
||||
}
|
||||
+155
@@ -0,0 +1,155 @@
|
||||
package com.nanri.aiimage.modules.appearancepatent.service;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
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.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.data.redis.core.ValueOperations;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.lenient;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* 外观专利行级检测标记的两态语义。
|
||||
*
|
||||
* <p>历史实现只有一个标记,且在**发 LLM 请求之前**就打({@code collectPendingLlmCandidates}),
|
||||
* TTL 24 小时、无撤销路径。于是进程被杀(发版、崩溃)或载荷落库失败时,那批行既没有结果、
|
||||
* 又被判定为「已处理」,续跑时直接跳过——线上表现为少数几行永远空白而任务报成功。
|
||||
* 现在拆成两态:**处理中**(短 TTL,谁在跑这行)与**已完成**(长 TTL,只在结果落库成功后打)。
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class AppearancePatentTaskCacheServiceRowMarkTest {
|
||||
|
||||
private static final Long TASK_ID = 29436L;
|
||||
private static final String ROW_KEY = "r1";
|
||||
|
||||
@Mock private StringRedisTemplate redisTemplate;
|
||||
@Mock private ValueOperations<String, String> valueOperations;
|
||||
|
||||
private AppearancePatentTaskCacheService cache;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
cache = new AppearancePatentTaskCacheService(redisTemplate, new ObjectMapper());
|
||||
lenient().when(redisTemplate.opsForValue()).thenReturn(valueOperations);
|
||||
}
|
||||
|
||||
/** 没任何标记时,第一次申领必须拿到这行。 */
|
||||
@Test
|
||||
void beginClaimsRowWhenNoMarkExists() {
|
||||
when(valueOperations.setIfAbsent(anyString(), anyString(), any(Duration.class))).thenReturn(true);
|
||||
|
||||
assertTrue(cache.beginRowInspection(TASK_ID, ROW_KEY));
|
||||
}
|
||||
|
||||
/** 处理中标记还在(另一个入口正在跑这行)时不能重复申领,否则会双发 LLM、双扣次数。 */
|
||||
@Test
|
||||
void beginRejectsRowStillInFlight() {
|
||||
when(valueOperations.setIfAbsent(anyString(), anyString(), any(Duration.class))).thenReturn(false);
|
||||
|
||||
assertFalse(cache.beginRowInspection(TASK_ID, ROW_KEY));
|
||||
}
|
||||
|
||||
/** 已完成的行连申领都不该发起:不能白占一个处理中名额。 */
|
||||
@Test
|
||||
void beginRejectsCompletedRowWithoutTouchingInFlightMark() {
|
||||
when(redisTemplate.hasKey(anyString())).thenReturn(true);
|
||||
|
||||
assertFalse(cache.beginRowInspection(TASK_ID, ROW_KEY));
|
||||
|
||||
verify(valueOperations, never()).setIfAbsent(anyString(), anyString(), any(Duration.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理中标记必须是分钟级:进程被杀后这些行要能重新成为候选。
|
||||
* 上限取 30 分钟——线上实测单批 10 行最慢 220 秒,再长就是拿丢行换并发安全。
|
||||
*/
|
||||
@Test
|
||||
void inFlightMarkExpiresWithinMinutes() {
|
||||
when(valueOperations.setIfAbsent(anyString(), anyString(), any(Duration.class))).thenReturn(true);
|
||||
|
||||
cache.beginRowInspection(TASK_ID, ROW_KEY);
|
||||
|
||||
ArgumentCaptor<Duration> ttl = ArgumentCaptor.forClass(Duration.class);
|
||||
verify(valueOperations).setIfAbsent(anyString(), anyString(), ttl.capture());
|
||||
assertTrue(ttl.getValue().toSeconds() > 0, "处理中标记必须带 TTL,否则一次中断就永久丢行");
|
||||
assertTrue(ttl.getValue().toMinutes() <= 30, "处理中标记不得超过 30 分钟,实测单批最慢约 220 秒");
|
||||
}
|
||||
|
||||
/** 完成标记只在落库成功后打:长 TTL,并且要把处理中名额让出来。 */
|
||||
@Test
|
||||
void completeMarksLongTtlAndReleasesInFlight() {
|
||||
cache.completeRowInspection(TASK_ID, List.of(ROW_KEY));
|
||||
|
||||
ArgumentCaptor<String> completed = ArgumentCaptor.forClass(String.class);
|
||||
ArgumentCaptor<Duration> ttl = ArgumentCaptor.forClass(Duration.class);
|
||||
verify(valueOperations).set(completed.capture(), anyString(), ttl.capture());
|
||||
assertEquals(Duration.ofHours(24), ttl.getValue());
|
||||
ArgumentCaptor<String> deleted = ArgumentCaptor.forClass(String.class);
|
||||
verify(redisTemplate).delete(deleted.capture());
|
||||
assertNotEquals(completed.getValue(), deleted.getValue(), "清除的必须是处理中键,不能把刚打的完成标记删掉");
|
||||
}
|
||||
|
||||
/** 两态必须落在不同键上:同键的话「完成」会把短 TTL 覆盖成长 TTL,或反过来互相清除。 */
|
||||
@Test
|
||||
void inFlightAndCompletedMarksUseDifferentKeys() {
|
||||
when(valueOperations.setIfAbsent(anyString(), anyString(), any(Duration.class))).thenReturn(true);
|
||||
|
||||
cache.beginRowInspection(TASK_ID, ROW_KEY);
|
||||
cache.completeRowInspection(TASK_ID, List.of(ROW_KEY));
|
||||
|
||||
ArgumentCaptor<String> inFlight = ArgumentCaptor.forClass(String.class);
|
||||
verify(valueOperations).setIfAbsent(inFlight.capture(), anyString(), any(Duration.class));
|
||||
ArgumentCaptor<String> completed = ArgumentCaptor.forClass(String.class);
|
||||
verify(valueOperations).set(completed.capture(), anyString(), any(Duration.class));
|
||||
assertNotEquals(inFlight.getValue(), completed.getValue());
|
||||
}
|
||||
|
||||
/** 发送前续期只延长处理中,绝不碰完成标记。 */
|
||||
@Test
|
||||
void touchRenewsOnlyInFlightMark() {
|
||||
cache.touchRowInspection(TASK_ID, List.of(ROW_KEY));
|
||||
|
||||
verify(redisTemplate).expire(anyString(), any(Duration.class));
|
||||
verify(valueOperations, never()).set(anyString(), anyString(), any(Duration.class));
|
||||
}
|
||||
|
||||
/** Redis 降级时必须放行:持久化层的 hasResolvedLlmFields 才是最终判据,不能因为 Redis 挂了就整单停摆。 */
|
||||
@Test
|
||||
void beginAllowsRowThroughWhenRedisFails() {
|
||||
when(redisTemplate.hasKey(anyString())).thenThrow(new IllegalStateException("redis down"));
|
||||
|
||||
assertTrue(cache.beginRowInspection(TASK_ID, ROW_KEY));
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测失败要立刻撤销处理中标记,而不是等 TTL 过期。
|
||||
*
|
||||
* <p>assemble job 的 5 次重试是在几分钟内跑完的,若失败后仍占着 15 分钟的处理中名额,
|
||||
* 这几行在重试窗口内永远申领不到——"能重检"就变成了"更快地彻底失败"。
|
||||
*/
|
||||
@Test
|
||||
void releaseDeletesInFlightMarkWithoutCompleting() {
|
||||
cache.releaseRowInspection(TASK_ID, List.of(ROW_KEY));
|
||||
|
||||
ArgumentCaptor<String> deleted = ArgumentCaptor.forClass(String.class);
|
||||
verify(redisTemplate).delete(deleted.capture());
|
||||
assertFalse(deleted.getValue().contains("processed-rows"), "撤销只能删处理中键,删完成标记会让已检测的行重跑并重复计费");
|
||||
verify(valueOperations, never()).set(anyString(), anyString(), any(Duration.class));
|
||||
}
|
||||
}
|
||||
+4
-3
@@ -209,8 +209,8 @@ class AppearancePatentTaskServiceDelegationTest {
|
||||
|
||||
try (XSSFWorkbook wb = new XSSFWorkbook(out)) {
|
||||
Row data = wb.getSheet("外观专利检测结果").getRow(1);
|
||||
assertEquals("", data.getCell(9).getStringCellValue(), "无结果行 LLM 列留空");
|
||||
assertEquals("", data.getCell(12).getStringCellValue(), "无结果行状态留空");
|
||||
assertEquals("未取得检测结果", data.getCell(9).getStringCellValue(), "无结果行判定列显式标注(空白与没风险无法区分)");
|
||||
assertEquals("失败", data.getCell(12).getStringCellValue(), "无结果行状态判失败");
|
||||
assertEquals("brand-A", data.getCell(4).getStringCellValue(), "品牌仍从解析行 values 回退");
|
||||
assertEquals("", data.getCell(5).getStringCellValue(), "无结果行价格留空(resolvePrice 仅读结果行,与现状一致)");
|
||||
}
|
||||
@@ -367,7 +367,8 @@ class AppearancePatentTaskServiceDelegationTest {
|
||||
row.setConclusion("");
|
||||
assertEquals("LLM http 503: This model is currently overloaded",
|
||||
AppearancePatentSheetBuilder.userFacingLlmCellValue(row, row.getTitleRisk()));
|
||||
assertEquals("失败", AppearancePatentSheetBuilder.userFacingStatus(row),
|
||||
assertEquals("失败", AppearancePatentSheetBuilder.resolveResultStatus(
|
||||
AppearancePatentSheetBuilder.userFacingConclusion(row)),
|
||||
"LLM 技术失败没得出判定 → 状态失败(旧口径按结论非空误判为成功)");
|
||||
}
|
||||
|
||||
|
||||
+72
-2
@@ -135,6 +135,12 @@ class AppearancePatentSheetBuilderTest {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 没有结果的行必须写明"没结果",不能留空。
|
||||
*
|
||||
* <p>覆盖率只要求过半(llm-coverage-min-ratio),所以少数缺失行照样交付;历史上这些格子是
|
||||
* 空字符串,用户在表格里看到的"空白"和"没风险"长得一模一样——静默漏判。
|
||||
*/
|
||||
@Test
|
||||
void test_sheet_builder_no_result_row_uses_parsed_fallback() throws Exception {
|
||||
try (Workbook wb = new XSSFWorkbook()) {
|
||||
@@ -145,8 +151,72 @@ class AppearancePatentSheetBuilderTest {
|
||||
assertEquals("title-a", data.getCell(6).getStringCellValue(), "无结果行回退解析行标题");
|
||||
assertEquals("http://img/a.jpg", data.getCell(7).getStringCellValue(), "回退解析行 URL");
|
||||
assertEquals("SKU-1", data.getCell(8).getStringCellValue(), "回退解析行 sku");
|
||||
assertEquals("", data.getCell(9).getStringCellValue(), "无结果行 LLM 列留空");
|
||||
assertEquals("", data.getCell(12).getStringCellValue(), "无结果行状态留空");
|
||||
assertEquals("未取得检测结果", data.getCell(9).getStringCellValue(), "无结果行标题维度显式标注");
|
||||
assertEquals("未取得检测结果", data.getCell(10).getStringCellValue(), "无结果行外观维度显式标注");
|
||||
assertEquals("未取得检测结果", data.getCell(11).getStringCellValue(), "无结果行结论显式标注");
|
||||
assertEquals("失败", data.getCell(12).getStringCellValue(), "无结果行状态判失败,不能留空");
|
||||
|
||||
Row reason = wb.getSheet("原因").getRow(1);
|
||||
assertEquals("B01A", reason.getCell(0).getStringCellValue());
|
||||
assertTrue(reason.getCell(1).getStringCellValue().contains("未取得检测结果"),
|
||||
"原因 sheet 也要说明这行为什么没有原因: " + reason.getCell(1).getStringCellValue());
|
||||
}
|
||||
}
|
||||
|
||||
/** 行存在但三个维度都没给出可用判定(客户端兜底成空串):同样要显式标注,且不能盖掉已有错误信息。 */
|
||||
@Test
|
||||
void test_sheet_builder_undeliverable_row_shows_missing_marker() throws Exception {
|
||||
try (Workbook wb = new XSSFWorkbook()) {
|
||||
AppearancePatentParsedRowVo p = parsedRow("2_1", "B01A", "US", "t", "", "");
|
||||
AppearancePatentResultRowDto r = resultRow("2_1", "B01A", "US");
|
||||
r.setTitleRisk("");
|
||||
r.setAppearanceRisk("");
|
||||
r.setConclusion("");
|
||||
r.setError("LLM 网关无应答");
|
||||
AppearancePatentSheetBuilder.buildResultSheet(wb, List.of(p), Map.of(
|
||||
AppearancePatentSheetBuilder.rowKey(r), r));
|
||||
|
||||
Row data = wb.getSheet("外观专利检测结果").getRow(1);
|
||||
assertEquals("LLM 网关无应答", data.getCell(9).getStringCellValue(), "有错误信息时展示错误信息");
|
||||
assertEquals("失败", data.getCell(12).getStringCellValue(), "结论列没内容时状态仍判失败");
|
||||
}
|
||||
}
|
||||
|
||||
/** 结论被兜底成非技术性文案(如"外观识别异常")时也属于没结果,不能空着交付。 */
|
||||
@Test
|
||||
void test_sheet_builder_blank_verdict_row_is_marked() throws Exception {
|
||||
try (Workbook wb = new XSSFWorkbook()) {
|
||||
AppearancePatentParsedRowVo p = parsedRow("2_1", "B01A", "US", "t", "", "");
|
||||
AppearancePatentResultRowDto r = resultRow("2_1", "B01A", "US");
|
||||
r.setTitleRisk("");
|
||||
r.setAppearanceRisk("");
|
||||
r.setConclusion("");
|
||||
r.setError("");
|
||||
AppearancePatentSheetBuilder.buildResultSheet(wb, List.of(p), Map.of(
|
||||
AppearancePatentSheetBuilder.rowKey(r), r));
|
||||
|
||||
Row data = wb.getSheet("外观专利检测结果").getRow(1);
|
||||
assertEquals("未取得检测结果", data.getCell(11).getStringCellValue(), "空白结论改写成显式标注");
|
||||
assertEquals("失败", data.getCell(12).getStringCellValue());
|
||||
}
|
||||
}
|
||||
|
||||
/** 有完整判定的行不受影响:显式标注只能出现在真的没结果的行上。 */
|
||||
@Test
|
||||
void test_sheet_builder_resolved_row_keeps_verdicts() throws Exception {
|
||||
try (Workbook wb = new XSSFWorkbook()) {
|
||||
AppearancePatentParsedRowVo p = parsedRow("2_1", "B01A", "US", "t", "", "");
|
||||
AppearancePatentResultRowDto r = resultRow("2_1", "B01A", "US");
|
||||
AppearancePatentSheetBuilder.buildResultSheet(wb, List.of(p), Map.of(
|
||||
AppearancePatentSheetBuilder.rowKey(r), r));
|
||||
|
||||
Row data = wb.getSheet("外观专利检测结果").getRow(1);
|
||||
assertEquals("无风险", data.getCell(9).getStringCellValue());
|
||||
assertEquals("无风险", data.getCell(10).getStringCellValue());
|
||||
assertEquals("已侵权", data.getCell(11).getStringCellValue());
|
||||
assertEquals("成功", data.getCell(12).getStringCellValue());
|
||||
assertEquals("", wb.getSheet("原因").getRow(1).getCell(1).getStringCellValue(),
|
||||
"有结果的行不写缺失说明");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user