refactor(phase-3): 3A 拆薄第十三笔——AP 剩余五簇出件,服务 1127→750(<800 达标)

- 新落五件:ChunkPayloadMerger(CAS 合并)/LlmSubmissionSupport(提交调度+凭据)/
  StaleTaskFinalizer(判死+超时恢复)/GroupingSupport(解析分组)/TaskRecordSupport(删除下载激活)
- 非事务回传支并进 ChunkSubmissionSupport.receiveWithoutTransaction(逐字搬移)
- 归一/首非空/基 id 改吃 AppearancePatentRowNormalizer,登记过的重复实现退场
- 状态标量改吃 TaskStatuses.FileTask(状态词表棘轮 70 不涨)
- 手写归属谓词文件数 16→15 是拆件真下降,棘轮同步登记(阈值 ≤130 未放宽)
- SourceScan 面数 39→44 / support 16→21,五枚新件逐枚 anyMatch 点名
- 等价:18 段符号归一逐行对撞,14 段 EQUAL,4 段仅登记过的回调/util 替换
This commit is contained in:
2026-10-21 09:30:17 +08:00
parent 7da9311265
commit 4d70a0d3f8
9 changed files with 832 additions and 465 deletions
@@ -1,31 +1,29 @@
package com.nanri.aiimage.modules.appearancepatent.service;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.common.task.TaskResultAdmission;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.modules.appearancepatent.service.support.AppearancePatentChunkPayloadMerger;
import com.nanri.aiimage.modules.appearancepatent.service.support.AppearancePatentChunkSubmissionSupport;
import com.nanri.aiimage.modules.appearancepatent.service.support.AppearancePatentChunkSubmissionSupport.PreparedSubmittedChunk;
import com.nanri.aiimage.modules.appearancepatent.service.support.AppearancePatentChunkSubmissionSupport.SubmitContext;
import com.nanri.aiimage.modules.appearancepatent.service.support.AppearancePatentGroupingSupport;
import com.nanri.aiimage.modules.appearancepatent.service.support.AppearancePatentLlmSubmissionSupport;
import com.nanri.aiimage.modules.appearancepatent.service.support.AppearancePatentRowNormalizer;
import com.nanri.aiimage.modules.appearancepatent.service.support.AppearancePatentStaleTaskFinalizer;
import com.nanri.aiimage.modules.appearancepatent.service.support.AppearancePatentTaskRecordSupport;
import com.nanri.aiimage.modules.appearancepatent.service.support.AppearancePatentDeliverySupport;
import com.nanri.aiimage.modules.appearancepatent.service.support.AppearancePatentExcelParser;
import com.nanri.aiimage.common.service.DistributedJobLockService;
import com.nanri.aiimage.common.util.FailedStatusRowFilter;
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.AppearancePatentParseRequest;
import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentParsedPayloadDto;
import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentResultGroupDto;
import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentResultRowDto;
import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentSourceFileDto;
import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentSubmitResultRequest;
import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentDashboardVo;
import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentHistoryItemVo;
import com.nanri.aiimage.modules.appearancepatent.service.support.AppearancePatentHistoryAssembler;
import com.nanri.aiimage.modules.appearancepatent.service.support.AppearancePatentLlmBatchPipeline;
import com.nanri.aiimage.modules.appearancepatent.service.support.AppearancePatentPayloadSupport;
@@ -33,12 +31,9 @@ import com.nanri.aiimage.modules.appearancepatent.service.support.AppearancePate
import com.nanri.aiimage.modules.appearancepatent.service.support.AppearancePatentProgressSupport;
import com.nanri.aiimage.modules.appearancepatent.service.support.AppearancePatentResultRowReader;
import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentHistoryVo;
import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentParsedGroupVo;
import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentParsedRowVo;
import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentParseVo;
import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentTaskBatchVo;
import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentTaskDetailVo;
import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentTaskItemVo;
import com.nanri.aiimage.modules.appearancepatent.service.support.AppearancePatentResultJobSupport;
import com.nanri.aiimage.modules.appearancepatent.service.support.AppearancePatentResultPackager;
import com.nanri.aiimage.modules.appearancepatent.service.support.AppearancePatentRowOutcome;
@@ -54,15 +49,12 @@ 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.TaskFileJobEntity;
import com.nanri.aiimage.modules.task.model.entity.TaskChunkEntity;
import com.nanri.aiimage.modules.task.model.entity.TaskProgressSnapshotEntity;
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.TaskProgressSnapshotService;
import com.nanri.aiimage.modules.task.service.TransientPayloadStorageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Transactional;
@@ -72,27 +64,16 @@ import org.springframework.transaction.support.TransactionSynchronizationManager
import org.springframework.transaction.support.TransactionTemplate;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Supplier;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import com.nanri.aiimage.modules.task.model.vo.TaskProgressLightBatchVo;
import com.nanri.aiimage.modules.task.service.TaskProgressLightAssembler;
import com.nanri.aiimage.modules.task.service.pipeline.AbstractFileTaskPipeline;
import com.nanri.aiimage.modules.usersecret.service.UserApiSecretService;
import com.nanri.aiimage.modules.usersecret.support.UserSecretModule;
@Service
@Slf4j
@@ -103,14 +84,11 @@ public class AppearancePatentTaskService extends AbstractFileTaskPipeline<Appear
public TaskProgressLightBatchVo progressLight(List<Long> taskIds, Long userId) {
return progressSupport().progressLight(taskIds, userId);
}
private static final String STATUS_PENDING = "PENDING";
private static final String STATUS_RUNNING = "RUNNING";
private static final String STATUS_SUCCESS = "SUCCESS";
private static final String STATUS_FAILED = "FAILED";
private static final int CHUNK_PAYLOAD_MERGE_RETRY_LIMIT = 3;
private static final Duration TASK_LOCK_TTL = Duration.ofMinutes(5);
private static final long TASK_LOCK_WAIT_MILLIS = 10000L;
private static final long TASK_LOCK_RETRY_DELAY_MILLIS = 200L;
private final LocalFileStorageService localFileStorageService;
/** 结果下载直链解析(2026-09 从本类抽到 common,消除 11 处逐字重复) */
@@ -211,30 +189,14 @@ public class AppearancePatentTaskService extends AbstractFileTaskPipeline<Appear
fileTaskMapper, fileResultMapper, taskScopeStateMapper,
this::requireUserId, this::newTaskHeader, this::insertTask, this::newScopeRow,
(value, message) -> writeJson(objectMapper, value, message),
this::payloadSupport, this::countParsedGroups,
this::normalize, this::firstNonBlank, this::baseId, this::buildGroupKey,
this::groupRowsByBaseId);
this::payloadSupport, grouping()::countParsedGroups,
this::normalize, this::firstNonBlank, this::baseId, grouping()::buildGroupKey,
grouping()::groupRowsByBaseId);
}
@Transactional
public void activateTask(Long taskId, Long userId) {
FileTaskEntity task = fileTaskMapper.selectById(taskId);
if (task == null || !MODULE_TYPE.equals(task.getModuleType()) || !Objects.equals(userId, task.getUserId())) {
throw new BusinessException("任务不存在");
}
// 2026-09-30 起外观专利已整体迁移 Linux 后台执行(无浏览器抓取):
// activate 不再把任务推给客户端浏览器。任务保持 PENDING,由内部认领端点
// (/api/internal/tasks/pull-pending + maixiang-ap worker)在等待窗口后自动接管。
// 这里保留接口仅做状态校验:旧前端(未刷新页面缓存)点「启动」时给出明确提示,
// 前端收到业务异常会中止入队逻辑,任务不会被执行两次。
if (STATUS_PENDING.equals(task.getStatus())) {
log.info("[外观专利] activate 不再推送客户端,任务保持 PENDING 等待 Linux 后台认领 taskId={} userId={}", taskId, userId);
throw new BusinessException("外观专利已切换后台自动执行,无需打开浏览器,稍候自动开始");
}
if (STATUS_RUNNING.equals(task.getStatus())) {
throw new BusinessException("任务已在执行中(可能已由后台自动接管),无需重复启动");
}
throw new BusinessException("任务已结束");
recordSupport().activateTask(taskId, userId);
}
public AppearancePatentDashboardVo dashboard(Long userId) {
@@ -279,89 +241,7 @@ public class AppearancePatentTaskService extends AbstractFileTaskPipeline<Appear
scheduleLlmPipelineForSubmittedChunk(context);
return;
}
FileTaskEntity task = fileTaskMapper.selectById(taskId);
if (task == null || !MODULE_TYPE.equals(task.getModuleType())) {
throw new BusinessException("任务不存在");
}
ensureTaskOwnedByCurrentInstance(task, "submit result");
if (!STATUS_RUNNING.equals(task.getStatus())) {
TaskResultAdmission.rejectNotRunning(task.getStatus());
}
int chunkIndex = request.getChunkIndex() == null ? 0 : request.getChunkIndex();
int chunkTotal = request.getChunkTotal() == null ? 1 : request.getChunkTotal();
String scopeKey = firstNonBlank(request.getSubmissionId(), "task:" + taskId);
String scopeHash = DigestUtil.sha256Hex(scopeKey);
taskCacheService.touchTaskHeartbeat(taskId);
TaskChunkEntity existing = taskChunkMapper.selectOne(new LambdaQueryWrapper<TaskChunkEntity>()
.eq(TaskChunkEntity::getTaskId, taskId)
.eq(TaskChunkEntity::getModuleType, MODULE_TYPE)
.eq(TaskChunkEntity::getScopeHash, scopeHash)
.eq(TaskChunkEntity::getChunkIndex, chunkIndex)
.last("limit 1"));
if (existing == null) {
List<AppearancePatentResultRowDto> rawRows = rowReader().flattenSubmittedRows(request);
String payloadJson = writeJson(objectMapper, rawRows, "结果序列化失败");
TaskChunkEntity chunk = new TaskChunkEntity();
chunk.setTaskId(taskId);
chunk.setModuleType(MODULE_TYPE);
chunk.setScopeKey(scopeKey);
chunk.setScopeHash(scopeHash);
chunk.setChunkIndex(chunkIndex);
chunk.setChunkTotal(chunkTotal);
String storedPayload = payloadSupport().storeSharedChunkPayload(taskId, scopeHash, chunkIndex, payloadJson);
chunk.setPayloadJson(storedPayload);
chunk.setPayloadHash(DigestUtil.sha256Hex(payloadJson));
chunk.setCreatedAt(LocalDateTime.now());
chunk.setUpdatedAt(LocalDateTime.now());
try {
taskChunkMapper.insert(chunk);
} catch (DuplicateKeyException ex) {
// storeChunkPayload uses a deterministic key like chunk-{index}. When two
// concurrent callbacks submit the same chunk, deleting the loser payload here
// can also remove the winner's shared object and break later assembly.
log.info("[appearance-patent] duplicate chunk inserted concurrently taskId={} scope={} chunk={}", taskId, scopeKey, chunkIndex);
}
} else {
log.info("[appearance-patent] duplicate chunk ignored taskId={} scope={} chunk={}", taskId, scopeKey, chunkIndex);
}
TaskScopeStateEntity scope = taskScopeStateMapper.selectOne(new LambdaQueryWrapper<TaskScopeStateEntity>()
.eq(TaskScopeStateEntity::getTaskId, taskId)
.eq(TaskScopeStateEntity::getModuleType, MODULE_TYPE)
.eq(TaskScopeStateEntity::getScopeHash, scopeHash)
.last("limit 1"));
if (scope == null) {
scope = new TaskScopeStateEntity();
scope.setTaskId(taskId);
scope.setModuleType(MODULE_TYPE);
scope.setScopeKey(scopeKey);
scope.setScopeHash(scopeHash);
scope.setCreatedAt(LocalDateTime.now());
}
scope.setChunkTotal(chunkTotal);
scope.setReceivedChunkCount(submissionSupport().countChunks(taskId, scopeHash));
scope.setLastChunkAt(LocalDateTime.now());
scope.setLastError(request.getError());
scope.setCompleted(Boolean.TRUE.equals(request.getDone()) ? 1 : 0);
scope.setUpdatedAt(LocalDateTime.now());
scope.setStateJson("{\"phase\":\"RECEIVED\",\"llm\":\"PENDING\"}");
if (scope.getId() == null) {
taskScopeStateMapper.insert(scope);
} else {
taskScopeStateMapper.updateById(scope);
}
SubmitContext context = new SubmitContext(task, scopeKey, scopeHash, chunkIndex,
Boolean.TRUE.equals(request.getDone()), request.getError());
if (Boolean.TRUE.equals(request.getDone()) || request.getError() != null && !request.getError().isBlank()) {
finalizeTask(task, request.getError(), rowReader().allRowCount(task), true);
} else {
task.setUpdatedAt(LocalDateTime.now());
fileTaskMapper.updateById(task);
}
SubmitContext context = submissionSupport().receiveWithoutTransaction(taskId, request);
scheduleLlmPipelineForSubmittedChunk(context);
}
@@ -373,45 +253,28 @@ public class AppearancePatentTaskService extends AbstractFileTaskPipeline<Appear
*/
@Transactional
public void deleteTask(Long taskId, Long userId) {
FileTaskEntity task = fileTaskMapper.selectById(taskId);
if (task == null || !MODULE_TYPE.equals(task.getModuleType()) || !Objects.equals(userId, task.getUserId())) {
throw new BusinessException("任务不存在");
}
List<String> payloads = payloadSupport().collectTransientTaskPayloads(taskId);
fileResultMapper.delete(new LambdaQueryWrapper<FileResultEntity>().eq(FileResultEntity::getTaskId, taskId).eq(FileResultEntity::getModuleType, MODULE_TYPE));
taskScopeStateMapper.delete(new LambdaQueryWrapper<TaskScopeStateEntity>().eq(TaskScopeStateEntity::getTaskId, taskId).eq(TaskScopeStateEntity::getModuleType, MODULE_TYPE));
taskChunkMapper.delete(new LambdaQueryWrapper<TaskChunkEntity>().eq(TaskChunkEntity::getTaskId, taskId).eq(TaskChunkEntity::getModuleType, MODULE_TYPE));
// 同步清理 task_file_job,避免被删除任务遗留的 PENDING/FAILED 行被 TaskResultFileJobWorker 反复扫描出 task not found。
taskFileJobService.deleteTaskJobs(taskId, MODULE_TYPE);
fileTaskMapper.deleteById(taskId);
// 事务提交后再做远端删除与缓存清理
payloadSupport().deletePayloadsAfterCommit(payloads, taskId);
runAfterCommit(() -> taskCacheService.deleteTaskCache(taskId));
recordSupport().deleteTask(taskId, userId);
}
public void deleteHistory(Long resultId, Long userId) {
FileResultEntity row = fileResultMapper.selectById(resultId);
if (row == null || !MODULE_TYPE.equals(row.getModuleType()) || !Objects.equals(userId, row.getUserId())) {
throw new BusinessException("记录不存在");
}
// 同步清理 task_file_job,避免被删除历史记录残留的 ASSEMBLE_RESULT job 被反复扫描出 result record not found。
taskFileJobService.deleteResultJobs(row.getTaskId(), MODULE_TYPE, resultId);
fileResultMapper.deleteById(resultId);
recordSupport().deleteHistory(resultId, userId);
}
public String resolveResultDownloadUrl(Long resultId, Long userId) {
// 2026-09 去重:与原实现等价(并修掉 userId.equals 的潜在 NPE),实现收口到 common
return resultDownloadResolver.resolveUrl(resultId, userId, MODULE_TYPE);
return recordSupport().resolveResultDownloadUrl(resultId, userId);
}
public String resolveResultDownloadFilename(Long resultId, Long userId) {
FileResultEntity row = fileResultMapper.selectById(resultId);
if (row == null || !MODULE_TYPE.equals(row.getModuleType()) || !Objects.equals(userId, row.getUserId())) {
throw new BusinessException("记录不存在");
}
return row.getResultFilename() == null || row.getResultFilename().isBlank()
? "appearance-patent-" + resultId + ".xlsx"
: row.getResultFilename();
return recordSupport().resolveResultDownloadFilename(resultId, userId);
}
/** 任务记录操作(删除/历史/下载/激活校验)住在 {@link AppearancePatentTaskRecordSupport}(3A 拆薄第十三笔)。 */
private AppearancePatentTaskRecordSupport recordSupport() {
return new AppearancePatentTaskRecordSupport(
MODULE_TYPE, fileTaskMapper, fileResultMapper, taskScopeStateMapper, taskChunkMapper,
taskFileJobService, taskCacheService, resultDownloadResolver,
payloadSupport()::collectTransientTaskPayloads, payloadSupport()::deletePayloadsAfterCommit,
this::runAfterCommit);
}
/**
@@ -428,11 +291,11 @@ public class AppearancePatentTaskService extends AbstractFileTaskPipeline<Appear
}
public void finalizeStaleTasks() {
staleTaskScanner().sweep(this::finalizeStaleTask);
staleTaskScanner().sweep(staleTaskFinalizer()::finalizeStaleTask);
}
public void debugFinalizeStaleTask(Long taskId) {
staleTaskScanner().finalizeOne(taskId, this::finalizeStaleTask);
staleTaskScanner().finalizeOne(taskId, staleTaskFinalizer()::finalizeStaleTask);
}
/**
@@ -443,7 +306,7 @@ public class AppearancePatentTaskService extends AbstractFileTaskPipeline<Appear
* 保留同名入口=保留测试接缝,也让「两条线各跑一次」这件事在服务侧仍然读得出来。
*/
void finalizeNoUploadStaleTasks() {
staleTaskScanner().sweepNoUpload(this::finalizeStaleTask);
staleTaskScanner().sweepNoUpload(staleTaskFinalizer()::finalizeStaleTask);
}
/**
@@ -491,11 +354,12 @@ public class AppearancePatentTaskService extends AbstractFileTaskPipeline<Appear
}
private AppearancePatentLlmBatchPipeline llmBatchPipeline() {
AppearancePatentLlmSubmissionSupport llmSubmission = llmSubmissionSupport();
return new AppearancePatentLlmBatchPipeline(
properties, llmClient, taskCacheService, taskFileJobService,
rowReader()::rowKey, this::shouldInspectRow, rowReader()::readChunkRows, this::loadSubmittedChunks,
this::isResultSubmissionComplete, this::touchJavaSideTaskActivity,
this::readAiPrompt, this::readApiKey, this::mergeChunkPayload, this::firstNonBlank);
llmSubmission::readAiPrompt, llmSubmission::readApiKey, this::mergeChunkPayload, this::firstNonBlank);
}
/**
@@ -507,7 +371,7 @@ public class AppearancePatentTaskService extends AbstractFileTaskPipeline<Appear
return new AppearancePatentPayloadSupport(
MODULE_TYPE, objectMapper, transientPayloadStorageService,
fileTaskMapper, taskChunkMapper, taskScopeStateMapper,
this::normalize, this::buildParsedGroups, this::currentInstanceId,
this::normalize, grouping()::buildParsedGroups, this::currentInstanceId,
this::ensureTaskOwnedByCurrentInstance, this::runAfterCommit,
(value, message) -> writeJson(objectMapper, value, message));
}
@@ -562,7 +426,7 @@ public class AppearancePatentTaskService extends AbstractFileTaskPipeline<Appear
llmBatchPipeline(), taskOwnership(),
this::acquireTaskLock, this::touchJavaSideTaskActivity,
AppearancePatentTaskService::resolveTaskExecutionStatus, this::firstNonBlank,
this::buildAggregateSourceFilenameLabel, this::buildAggregateScopeKey);
parsePipeline()::buildAggregateSourceFilenameLabel, parsePipeline()::buildAggregateScopeKey);
}
/**
@@ -587,87 +451,43 @@ public class AppearancePatentTaskService extends AbstractFileTaskPipeline<Appear
this::finalizeTask, this::firstNonBlank);
}
private void submitLlmForSubmittedChunk(SubmitContext context) {
if (context == null || context.task() == null || context.task().getId() == null) {
return;
}
FileTaskEntity task = fileTaskMapper.selectById(context.task().getId());
if (task == null || !MODULE_TYPE.equals(task.getModuleType()) || STATUS_SUCCESS.equals(task.getStatus())) {
return;
}
List<TaskChunkEntity> chunks = loadSubmittedChunks(task.getId());
if (chunks.isEmpty()) {
log.info("[appearance-patent] skip stale recovery llm submission because no submitted chunks remain taskId={}",
task.getId());
return;
}
FileResultEntity result = findOrCreateResultRecordForAssembly(task, rowReader().allRowCount(task));
if (result == null) {
log.warn("[appearance-patent] stale recovery could not create result record taskId={}", task.getId());
return;
}
TaskFileJobEntity job = taskFileJobService.enqueueAssembleResult(
task.getId(), MODULE_TYPE, result.getId(), buildTaskOwnerScopeKey(task));
if (job == null || "SUCCESS".equals(job.getStatus())) {
log.info("[appearance-patent] stale recovery skipped llm submission because result job unavailable taskId={} resultId={} jobStatus={}",
task.getId(), result.getId(), job == null ? null : job.getStatus());
return;
}
log.info("[appearance-patent] stale recovery created assemble job taskId={} resultId={} jobId={} jobStatus={} chunkCount={}",
task.getId(), result.getId(), job.getId(), job.getStatus(), chunks.size());
submitLlmBatches(task, result, job, chunks);
if (isResultSubmissionComplete(task.getId())) {
taskFileJobService.requeue(job.getId(), "检测结果已回流,正在组装 xlsx");
} else {
taskFileJobService.touchRunning(job.getId());
}
touchJavaSideTaskActivity(task.getId());
/** LLM 提交调度与凭据读取住在 {@link AppearancePatentLlmSubmissionSupport}(3A 拆薄第十三笔)。 */
private void scheduleLlmPipelineForSubmittedChunk(SubmitContext context) {
llmSubmissionSupport().scheduleLlmPipelineForSubmittedChunk(context);
}
private void scheduleLlmPipelineForSubmittedChunk(SubmitContext context) {
if (context == null || context.task() == null || context.task().getId() == null) {
return;
}
FileTaskEntity task = fileTaskMapper.selectById(context.task().getId());
if (task == null || !MODULE_TYPE.equals(task.getModuleType()) || STATUS_SUCCESS.equals(task.getStatus())) {
return;
}
List<TaskChunkEntity> chunks = loadSubmittedChunks(task.getId());
if (chunks.isEmpty()) {
return;
}
FileResultEntity result = findOrCreateResultRecordForAssembly(task, rowReader().allRowCount(task));
if (result == null) {
return;
}
TaskFileJobEntity job = taskFileJobService.enqueueAssembleResult(
task.getId(), MODULE_TYPE, result.getId(), buildTaskOwnerScopeKey(task));
if (job == null || "SUCCESS".equals(job.getStatus())) {
return;
}
taskFileJobService.requeue(job.getId(), "Appearance patent result uploaded, scheduling LLM/file assembly");
touchJavaSideTaskActivity(task.getId());
private AppearancePatentLlmSubmissionSupport llmSubmissionSupport() {
return new AppearancePatentLlmSubmissionSupport(
MODULE_TYPE, fileTaskMapper, taskFileJobService,
this::loadSubmittedChunks, rowReader()::allRowCount,
this::findOrCreateResultRecordForAssembly, this::submitLlmBatches,
this::isResultSubmissionComplete, this::touchJavaSideTaskActivity,
this::buildTaskOwnerScopeKey, payloadSupport()::readParsedPayload,
userApiSecretService, AppearancePatentRowNormalizer::normalize);
}
/**
* stale 判死收尾:事务只包住真正需要原子的两段(封口状态批量更新、任务终态 + 结果 + 组装作业入队),
* **LLM 批次留在事务之外**。
*
* <p>外层调用点曾把整个方法包进 {@code inNewTransaction},于是恢复分支里
* {@code submitLlmBatches} 每批开头 {@code touchRunning} 对 biz_task_file_job 取的 X 锁,
* 要一直活到串行发完整批 LLM 请求
* (线上实测单批 costMs=1077959)之后才释放——两台的卡死自愈扫描 UPDATE 同一行,
* 每 110 秒撞一次 50 秒锁等待,还顺手按住一条 Hikari 连接十几分钟、并把已扣过 LLM 次数的状态
* 置于可回滚的位置。同源的 {@code SimilarAsinTaskService#finalizeStaleTask} 一直是这个形状。
* 陈旧收尾(判死+Python 超时恢复)住在 {@link AppearancePatentStaleTaskFinalizer}(3A 拆薄第十三笔);
* 两处事务包装仍留在这里——它们定义的是现网事务边界,不跟着搬。
*/
private void finalizeStaleTask(Long taskId, String error) {
FileTaskEntity task = fileTaskMapper.selectById(taskId);
if (task == null || !MODULE_TYPE.equals(task.getModuleType()) || !STATUS_RUNNING.equals(task.getStatus())) {
return;
}
if (tryRecoverTimedOutPythonTask(task)) {
return;
}
private AppearancePatentStaleTaskFinalizer staleTaskFinalizer() {
return new AppearancePatentStaleTaskFinalizer(
MODULE_TYPE, fileTaskMapper, taskFileJobService,
rowReader()::allRowCount, this::isResultSubmissionComplete,
rowReader()::hasPersistedResultRows,
this::markSubmissionCompleteInNewTransaction,
this::loadSubmittedChunks, this::countUninspectedRows,
llmSubmissionSupport()::submitLlmForSubmittedChunk,
this::touchJavaSideTaskActivity, this::finalizeStaleTaskInTransaction);
}
private int markSubmissionCompleteInNewTransaction(Long taskId) {
return transactionManager == null
? submissionSupport().markSubmissionCompleteOnPythonTimeout(taskId)
: inNewTransaction(() -> submissionSupport().markSubmissionCompleteOnPythonTimeout(taskId));
}
private void finalizeStaleTaskInTransaction(FileTaskEntity task, String error) {
if (transactionManager == null) {
finalizeTask(task, error, rowReader().allRowCount(task), true);
return;
@@ -678,49 +498,6 @@ public class AppearancePatentTaskService extends AbstractFileTaskPipeline<Appear
});
}
private boolean tryRecoverTimedOutPythonTask(FileTaskEntity task) {
if (task == null || task.getId() == null) {
return false;
}
Long taskId = task.getId();
boolean uploadComplete = isResultSubmissionComplete(taskId);
long activeAssembleJobs = taskFileJobService.countActiveAssembleJobs(taskId, MODULE_TYPE);
log.info("[appearance-patent] stale recovery probe taskId={} uploadComplete={} activeAssembleJobs={} persistedRows={}",
taskId, uploadComplete, activeAssembleJobs, rowReader().hasPersistedResultRows(taskId));
// 已有「重试耗尽且已终态收尾」的 assemble job:说明恢复已经试过、缺失是永久的。
// 再重建只会每 30 秒空转一轮,而且恢复过程刷新任务心跳会让任务永远 RUNNING
// (线上任务 28459 实测:48 分钟里每隔 30 秒重建一次 job)。返回 false 交给
// finalizeStaleTask 按失败收尾,用户看到明确失败而不是无限等待。
if (taskFileJobService.hasExhaustedAssembleJob(taskId, MODULE_TYPE)) {
log.warn("[appearance-patent] stale recovery 放弃:已有重试耗尽的 assemble job,按失败收尾 taskId={}", taskId);
return false;
}
if (!rowReader().hasPersistedResultRows(taskId)) {
log.info("[appearance-patent] stale recovery aborted because no persisted rows taskId={}", taskId);
return false;
}
if (!uploadComplete) {
// 封口是多行 scope 状态更新,单独占一个短事务;紧随其后的 LLM 批次不在事务里
int forcedScopes = transactionManager == null
? submissionSupport().markSubmissionCompleteOnPythonTimeout(taskId)
: inNewTransaction(() -> submissionSupport().markSubmissionCompleteOnPythonTimeout(taskId));
if (forcedScopes <= 0) {
log.warn("[appearance-patent] stale recovery aborted because submission could not be marked complete taskId={}",
taskId);
return false;
}
int pendingRows = countUninspectedRows(loadSubmittedChunks(taskId));
log.warn("[appearance-patent] Python 回传心跳超时,已封口上传并继续检测/文件收尾 taskId={} pendingRows={} activeAssembleJobs={} forcedScopes={}",
taskId, pendingRows, activeAssembleJobs, forcedScopes);
} else {
log.warn("[appearance-patent] Python 超时恢复继续推进检测/文件收尾 taskId={} activeAssembleJobs={}",
taskId, activeAssembleJobs);
}
submitLlmForSubmittedChunk(new SubmitContext(task, null, null, 0, true, null));
touchJavaSideTaskActivity(taskId);
return true;
}
private <T> T inNewTransaction(Supplier<T> action) {
TransactionTemplate template = new TransactionTemplate(transactionManager);
template.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
@@ -728,148 +505,23 @@ public class AppearancePatentTaskService extends AbstractFileTaskPipeline<Appear
}
/** 返回真正写进这一片载荷的行键:一片也没写成时返回空集,调用方据此决定哪些行还能重发。 */
/** 分片载荷合并住在 {@link AppearancePatentChunkPayloadMerger}(3A 拆薄第十三笔);本同名入口是测试反射接缝。 */
private List<String> mergeChunkPayload(Long taskId, String scopeHash, Integer chunkIndex,
List<AppearancePatentResultRowDto> rows) {
if (rows == null || rows.isEmpty()) {
return List.of();
}
String conflictDetail = "未发生冲突";
for (int attempt = 1; attempt <= CHUNK_PAYLOAD_MERGE_RETRY_LIMIT; attempt++) {
TaskChunkEntity chunk = taskChunkMapper.selectOne(new LambdaQueryWrapper<TaskChunkEntity>()
.eq(TaskChunkEntity::getTaskId, taskId)
.eq(TaskChunkEntity::getModuleType, MODULE_TYPE)
.eq(TaskChunkEntity::getScopeHash, scopeHash)
.eq(TaskChunkEntity::getChunkIndex, chunkIndex)
.last("limit 1"));
if (chunk == null) {
return List.of();
}
Map<String, AppearancePatentResultRowDto> persistedRows = rowReader().readChunkRows(chunk);
for (AppearancePatentResultRowDto row : rows) {
String key = rowReader().rowKey(row);
AppearancePatentResultRowDto submittedRow = persistedRows.get(key);
if (submittedRow != null) {
row.setPrice(submittedRow.getPrice());
}
persistedRows.put(key, row);
}
String payloadJson = writeJson(objectMapper, new ArrayList<>(persistedRows.values()), "appearance patent chunk payload merge failed");
String oldPayload = chunk.getPayloadJson();
String oldPayloadHash = chunk.getPayloadHash();
String newPayloadHash = DigestUtil.sha256Hex(payloadJson);
String storedPayload = payloadSupport().storeSharedChunkPayloadVersioned(taskId, scopeHash, chunkIndex, payloadJson);
LambdaUpdateWrapper<TaskChunkEntity> updateWrapper = new LambdaUpdateWrapper<TaskChunkEntity>()
.eq(TaskChunkEntity::getId, chunk.getId())
.set(TaskChunkEntity::getPayloadJson, storedPayload)
.set(TaskChunkEntity::getPayloadHash, newPayloadHash)
.set(TaskChunkEntity::getUpdatedAt, LocalDateTime.now());
if (oldPayloadHash == null) {
updateWrapper.isNull(TaskChunkEntity::getPayloadHash);
} else {
updateWrapper.eq(TaskChunkEntity::getPayloadHash, oldPayloadHash);
}
int updated = taskChunkMapper.update(null, updateWrapper);
if (updated > 0) {
log.debug("[appearance-patent] chunk payload replaced taskId={} scopeHash={} chunk={} oldPayload={} newPayload={} attempt={}",
taskId, scopeHash, chunkIndex, oldPayload, storedPayload, attempt);
transientPayloadStorageService.deleteReplacedPayloadIfNeeded(oldPayload, storedPayload);
return rows.stream().map(rowReader()::rowKey).filter(key -> !key.isBlank()).toList();
}
// CAS 冲突:读回行上的当前哈希,否则只有一句 conflict 无从判断是谁改的(线上 28459 至今未能定位)
String currentHash = currentPayloadHash(chunk.getId());
conflictDetail = "期望hash=" + oldPayloadHash + " 当前hash=" + currentHash;
if (attempt < CHUNK_PAYLOAD_MERGE_RETRY_LIMIT) {
log.warn("[appearance-patent] chunk payload CAS conflict, retry merge taskId={} scopeHash={} chunk={} attempt={}/{} 期望hash={} 当前hash={}",
taskId, scopeHash, chunkIndex, attempt, CHUNK_PAYLOAD_MERGE_RETRY_LIMIT, oldPayloadHash, currentHash);
// 还要重试:这次写的对象会被下次重写,先删掉避免堆积
transientPayloadStorageService.deletePayloadIfPresent(storedPayload);
} else {
// 终局失败:保留这次写入的版本化对象,作为读路径「同槽位兄弟对象」兜底的恢复源。
// 行没指过去不该让该分片永久判死——删掉它才是线上 28459 丢数据的形态。
log.error("[appearance-patent] chunk payload 合并冲突重试耗尽,保留兜底对象 taskId={} scopeHash={} chunk={} 期望hash={} 当前hash={} 保留对象={}",
taskId, scopeHash, chunkIndex, oldPayloadHash, currentHash, storedPayload);
}
}
throw new IllegalStateException("appearance patent chunk payload update conflict " + conflictDetail);
return chunkPayloadMerger().mergeChunkPayload(taskId, scopeHash, chunkIndex, rows);
}
/** 读回 chunk 行当前的 payload_hash,用于 CAS 冲突定位(行已不存在/读取失败时返回可读标记)。 */
private String currentPayloadHash(Long chunkId) {
if (chunkId == null) {
return "chunkId 为空";
}
try {
TaskChunkEntity latest = taskChunkMapper.selectById(chunkId);
return latest == null ? "行已不存在" : latest.getPayloadHash();
} catch (Exception ex) {
return "读取失败:" + ex.getMessage();
}
private AppearancePatentChunkPayloadMerger chunkPayloadMerger() {
return new AppearancePatentChunkPayloadMerger(
MODULE_TYPE, taskChunkMapper, transientPayloadStorageService,
rowReader()::readChunkRows, rowReader()::rowKey,
payloadSupport()::storeSharedChunkPayloadVersioned,
(value, message) -> writeJson(objectMapper, value, message));
}
private List<AppearancePatentParsedGroupVo> buildParsedGroups(List<AppearancePatentParsedRowVo> rows) {
List<AppearancePatentParsedGroupVo> groups = new ArrayList<>();
for (List<AppearancePatentParsedRowVo> siblings : groupRowsByBaseId(rows).values()) {
if (siblings == null || siblings.isEmpty()) {
continue;
}
AppearancePatentParsedRowVo first = siblings.getFirst();
AppearancePatentParsedGroupVo group = new AppearancePatentParsedGroupVo();
group.setSourceFileKey(first.getSourceFileKey());
group.setSourceFilename(first.getSourceFilename());
group.setGroupKey(firstNonBlank(first.getGroupKey(), buildGroupKey(first.getSourceFileKey(), baseId(first.getDisplayId()), first.getRowIndex())));
group.setBaseId(baseId(first.getDisplayId()));
group.setDisplayId(firstNonBlank(first.getDisplayId(), first.getSourceId()));
group.setItemCount(siblings.size());
group.setItems(new ArrayList<>(siblings));
groups.add(group);
}
return groups;
}
private int countParsedGroups(List<AppearancePatentParsedRowVo> rows) {
return groupRowsByBaseId(rows).size();
}
private Map<String, List<AppearancePatentParsedRowVo>> groupRowsByBaseId(List<AppearancePatentParsedRowVo> rows) {
Map<String, List<AppearancePatentParsedRowVo>> result = new LinkedHashMap<>();
if (rows == null) {
return result;
}
for (AppearancePatentParsedRowVo row : rows) {
String key = firstNonBlank(normalize(row.getGroupKey()), baseId(row.getDisplayId()));
result.computeIfAbsent(key, ignored -> new ArrayList<>()).add(row);
}
return result;
}
private String readAiPrompt(FileTaskEntity task) {
try {
return payloadSupport().readParsedPayload(task).getAiPrompt();
} catch (Exception ignored) {
return "";
}
}
/** 密钥读取:任务 payload 优先(兼容老任务与显式覆盖),为空时按任务归属用户从服务端密钥表兜底。 */
private String readApiKey(FileTaskEntity task) {
try {
String fromPayload = normalize(payloadSupport().readParsedPayload(task).getApiKey());
if (!fromPayload.isEmpty()) {
return fromPayload;
}
} catch (Exception ex) {
log.warn("[appearance-patent] 读取任务 payload 密钥失败,尝试用户密钥兜底 taskId={} err={}",
task.getId(), ex.getMessage());
}
String fromUserSecret = userApiSecretService.findPlainValue(
task.getUserId(), UserSecretModule.APPEARANCE_PATENT.key());
if (fromUserSecret.isEmpty()) {
log.warn("[appearance-patent] 任务未携带密钥且用户未配置密钥,LLM 检测将保留原始行 taskId={} userId={}",
task.getId(), task.getUserId());
}
return fromUserSecret;
/** 解析行分组住在 {@link AppearancePatentGroupingSupport}(3A 拆薄第十三笔)。 */
private AppearancePatentGroupingSupport grouping() {
return new AppearancePatentGroupingSupport();
}
private void finalizeTask(FileTaskEntity task, String error, int rowCount, boolean assembleWorkbook) {
@@ -973,24 +625,11 @@ public class AppearancePatentTaskService extends AbstractFileTaskPipeline<Appear
return taskOwnership().currentInstanceId();
}
private boolean isJobOwnedByCurrentInstance(TaskFileJobEntity job) {
AppearancePatentTaskOwnership ownership = taskOwnership();
return job == null || ownership.isOwnerCurrent(ownership.ownerFromScopeKey(job.getScopeKey()));
}
@Override
protected void ensureTaskOwnedByCurrentInstance(FileTaskEntity task, String operation) {
taskOwnership().rejectIfOwnedByAnotherInstance(task, operation);
}
private String ownerFromTask(FileTaskEntity task) {
return taskOwnership().ownerFromTask(task);
}
private String ownerFromScopeKey(String scopeKey) {
return taskOwnership().ownerFromScopeKey(scopeKey);
}
private void saveFileBuildProgress(FileTaskEntity task,
TaskFileJobEntity job,
int total,
@@ -1067,31 +706,15 @@ public class AppearancePatentTaskService extends AbstractFileTaskPipeline<Appear
}
private String baseId(String id) {
String s = normalize(id);
int idx = s.indexOf('_');
return idx > 0 ? s.substring(0, idx) : s;
}
private String buildAggregateScopeKey(List<AppearancePatentSourceFileDto> sourceFiles) {
return parsePipeline().buildAggregateScopeKey(sourceFiles);
}
private String buildAggregateSourceFilenameLabel(List<AppearancePatentSourceFileDto> sourceFiles) {
return parsePipeline().buildAggregateSourceFilenameLabel(sourceFiles);
}
private String buildGroupKey(String sourceFileKey, String rowBaseId, Integer rowIndex) {
return normalize(sourceFileKey) + "::" + normalize(rowBaseId) + "@" + (rowIndex == null ? 0 : rowIndex);
return AppearancePatentRowNormalizer.baseId(id);
}
private String firstNonBlank(String preferred, String fallback) {
return preferred == null || preferred.isBlank() ? fallback : preferred.trim();
return AppearancePatentRowNormalizer.firstNonBlank(preferred, fallback);
}
private String normalize(String val) {
return val == null ? "" : val.replace(String.valueOf((char) 0xFEFF), "").replace((char) 0x3000, ' ').trim().replaceAll("\\s+", " ");
return AppearancePatentRowNormalizer.normalize(val);
}
private TaskDistributedLockService.LockHandle acquireTaskLock(Long taskId, long waitMillis) {
@@ -0,0 +1,140 @@
package com.nanri.aiimage.modules.appearancepatent.service.support;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentResultRowDto;
import com.nanri.aiimage.modules.task.mapper.TaskChunkMapper;
import com.nanri.aiimage.modules.task.model.entity.TaskChunkEntity;
import com.nanri.aiimage.modules.task.service.TransientPayloadStorageService;
import lombok.extern.slf4j.Slf4j;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
/**
* 外观专利的分片载荷合并(D 聚类的一格),从 {@code AppearancePatentTaskService}
* <b>原样搬移</b>(3A 拆薄第十三笔):CAS 重试、版本化对象保留、冲突哈希读回与全部日志文案
* 一字未改;读分片行、行键、载荷写入、序列化出口仍由服务侧以回调交入。
*/
@Slf4j
public class AppearancePatentChunkPayloadMerger {
@FunctionalInterface
public interface ChunkPayloadVersionWriter {
String storeVersioned(Long taskId, String scopeHash, Integer chunkIndex, String payloadJson);
}
@FunctionalInterface
public interface JsonWriter {
String write(Object value, String failureMessage);
}
private static final int CHUNK_PAYLOAD_MERGE_RETRY_LIMIT = 3;
private final String moduleType;
private final TaskChunkMapper taskChunkMapper;
private final TransientPayloadStorageService transientPayloadStorageService;
private final Function<TaskChunkEntity, Map<String, AppearancePatentResultRowDto>> chunkRowReader;
private final Function<AppearancePatentResultRowDto, String> rowKeyer;
private final ChunkPayloadVersionWriter versionWriter;
private final JsonWriter jsonWriter;
public AppearancePatentChunkPayloadMerger(String moduleType,
TaskChunkMapper taskChunkMapper,
TransientPayloadStorageService transientPayloadStorageService,
Function<TaskChunkEntity, Map<String, AppearancePatentResultRowDto>> chunkRowReader,
Function<AppearancePatentResultRowDto, String> rowKeyer,
ChunkPayloadVersionWriter versionWriter,
JsonWriter jsonWriter) {
this.moduleType = moduleType;
this.taskChunkMapper = taskChunkMapper;
this.transientPayloadStorageService = transientPayloadStorageService;
this.chunkRowReader = chunkRowReader;
this.rowKeyer = rowKeyer;
this.versionWriter = versionWriter;
this.jsonWriter = jsonWriter;
}
/** 返回真正写进这一片载荷的行键:一片也没写成时返回空集,调用方据此决定哪些行还能重发。 */
public List<String> mergeChunkPayload(Long taskId, String scopeHash, Integer chunkIndex,
List<AppearancePatentResultRowDto> rows) {
if (rows == null || rows.isEmpty()) {
return List.of();
}
String conflictDetail = "未发生冲突";
for (int attempt = 1; attempt <= CHUNK_PAYLOAD_MERGE_RETRY_LIMIT; attempt++) {
TaskChunkEntity chunk = taskChunkMapper.selectOne(new LambdaQueryWrapper<TaskChunkEntity>()
.eq(TaskChunkEntity::getTaskId, taskId)
.eq(TaskChunkEntity::getModuleType, moduleType)
.eq(TaskChunkEntity::getScopeHash, scopeHash)
.eq(TaskChunkEntity::getChunkIndex, chunkIndex)
.last("limit 1"));
if (chunk == null) {
return List.of();
}
Map<String, AppearancePatentResultRowDto> persistedRows = chunkRowReader.apply(chunk);
for (AppearancePatentResultRowDto row : rows) {
String key = rowKeyer.apply(row);
AppearancePatentResultRowDto submittedRow = persistedRows.get(key);
if (submittedRow != null) {
row.setPrice(submittedRow.getPrice());
}
persistedRows.put(key, row);
}
String payloadJson = jsonWriter.write(new ArrayList<>(persistedRows.values()), "appearance patent chunk payload merge failed");
String oldPayload = chunk.getPayloadJson();
String oldPayloadHash = chunk.getPayloadHash();
String newPayloadHash = DigestUtil.sha256Hex(payloadJson);
String storedPayload = versionWriter.storeVersioned(taskId, scopeHash, chunkIndex, payloadJson);
LambdaUpdateWrapper<TaskChunkEntity> updateWrapper = new LambdaUpdateWrapper<TaskChunkEntity>()
.eq(TaskChunkEntity::getId, chunk.getId())
.set(TaskChunkEntity::getPayloadJson, storedPayload)
.set(TaskChunkEntity::getPayloadHash, newPayloadHash)
.set(TaskChunkEntity::getUpdatedAt, LocalDateTime.now());
if (oldPayloadHash == null) {
updateWrapper.isNull(TaskChunkEntity::getPayloadHash);
} else {
updateWrapper.eq(TaskChunkEntity::getPayloadHash, oldPayloadHash);
}
int updated = taskChunkMapper.update(null, updateWrapper);
if (updated > 0) {
log.debug("[appearance-patent] chunk payload replaced taskId={} scopeHash={} chunk={} oldPayload={} newPayload={} attempt={}",
taskId, scopeHash, chunkIndex, oldPayload, storedPayload, attempt);
transientPayloadStorageService.deleteReplacedPayloadIfNeeded(oldPayload, storedPayload);
return rows.stream().map(rowKeyer).filter(key -> !key.isBlank()).toList();
}
// CAS 冲突:读回行上的当前哈希,否则只有一句 conflict 无从判断是谁改的(线上 28459 至今未能定位)
String currentHash = currentPayloadHash(chunk.getId());
conflictDetail = "期望hash=" + oldPayloadHash + " 当前hash=" + currentHash;
if (attempt < CHUNK_PAYLOAD_MERGE_RETRY_LIMIT) {
log.warn("[appearance-patent] chunk payload CAS conflict, retry merge taskId={} scopeHash={} chunk={} attempt={}/{} 期望hash={} 当前hash={}",
taskId, scopeHash, chunkIndex, attempt, CHUNK_PAYLOAD_MERGE_RETRY_LIMIT, oldPayloadHash, currentHash);
// 还要重试:这次写的对象会被下次重写,先删掉避免堆积
transientPayloadStorageService.deletePayloadIfPresent(storedPayload);
} else {
// 终局失败:保留这次写入的版本化对象,作为读路径「同槽位兄弟对象」兜底的恢复源。
// 行没指过去不该让该分片永久判死——删掉它才是线上 28459 丢数据的形态。
log.error("[appearance-patent] chunk payload 合并冲突重试耗尽,保留兜底对象 taskId={} scopeHash={} chunk={} 期望hash={} 当前hash={} 保留对象={}",
taskId, scopeHash, chunkIndex, oldPayloadHash, currentHash, storedPayload);
}
}
throw new IllegalStateException("appearance patent chunk payload update conflict " + conflictDetail);
}
/** 读回 chunk 行当前的 payload_hash,用于 CAS 冲突定位(行已不存在/读取失败时返回可读标记)。 */
private String currentPayloadHash(Long chunkId) {
if (chunkId == null) {
return "chunkId 为空";
}
try {
TaskChunkEntity latest = taskChunkMapper.selectById(chunkId);
return latest == null ? "行已不存在" : latest.getPayloadHash();
} catch (Exception ex) {
return "读取失败:" + ex.getMessage();
}
}
}
@@ -167,6 +167,98 @@ public class AppearancePatentChunkSubmissionSupport {
request.getError(), payloadJson, storedPayload, DigestUtil.sha256Hex(payloadJson));
}
/**
* 无事务管理器时的直写回传(原 {@code submitResultLocked} 的 {@code transactionManager == null}
* 分支,3A 拆薄第十三笔原样搬移):准入、归属、分片去重、scope 合并与收尾都在同一段里,
* 判序、日志与「重复分片不删赢家载荷」那条注释一字未改。
*/
public SubmitContext receiveWithoutTransaction(Long taskId, AppearancePatentSubmitResultRequest request) {
FileTaskEntity task = fileTaskMapper.selectById(taskId);
if (task == null || !moduleType.equals(task.getModuleType())) {
throw new BusinessException("任务不存在");
}
ownershipChecker.check(task, "submit result");
if (!STATUS_RUNNING.equals(task.getStatus())) {
TaskResultAdmission.rejectNotRunning(task.getStatus());
}
int chunkIndex = request.getChunkIndex() == null ? 0 : request.getChunkIndex();
int chunkTotal = request.getChunkTotal() == null ? 1 : request.getChunkTotal();
String scopeKey = firstNonBlank.pick(request.getSubmissionId(), "task:" + taskId);
String scopeHash = DigestUtil.sha256Hex(scopeKey);
taskCacheService.touchTaskHeartbeat(taskId);
TaskChunkEntity existing = taskChunkMapper.selectOne(new LambdaQueryWrapper<TaskChunkEntity>()
.eq(TaskChunkEntity::getTaskId, taskId)
.eq(TaskChunkEntity::getModuleType, moduleType)
.eq(TaskChunkEntity::getScopeHash, scopeHash)
.eq(TaskChunkEntity::getChunkIndex, chunkIndex)
.last("limit 1"));
if (existing == null) {
List<AppearancePatentResultRowDto> rawRows = rowFlattener.flatten(request);
String payloadJson = jsonWriter.write(rawRows, "结果序列化失败");
TaskChunkEntity chunk = new TaskChunkEntity();
chunk.setTaskId(taskId);
chunk.setModuleType(moduleType);
chunk.setScopeKey(scopeKey);
chunk.setScopeHash(scopeHash);
chunk.setChunkIndex(chunkIndex);
chunk.setChunkTotal(chunkTotal);
String storedPayload = chunkPayloadWriter.store(taskId, scopeHash, chunkIndex, payloadJson);
chunk.setPayloadJson(storedPayload);
chunk.setPayloadHash(DigestUtil.sha256Hex(payloadJson));
chunk.setCreatedAt(LocalDateTime.now());
chunk.setUpdatedAt(LocalDateTime.now());
try {
taskChunkMapper.insert(chunk);
} catch (DuplicateKeyException ex) {
// storeChunkPayload uses a deterministic key like chunk-{index}. When two
// concurrent callbacks submit the same chunk, deleting the loser payload here
// can also remove the winner's shared object and break later assembly.
log.info("[appearance-patent] duplicate chunk inserted concurrently taskId={} scope={} chunk={}", taskId, scopeKey, chunkIndex);
}
} else {
log.info("[appearance-patent] duplicate chunk ignored taskId={} scope={} chunk={}", taskId, scopeKey, chunkIndex);
}
TaskScopeStateEntity scope = taskScopeStateMapper.selectOne(new LambdaQueryWrapper<TaskScopeStateEntity>()
.eq(TaskScopeStateEntity::getTaskId, taskId)
.eq(TaskScopeStateEntity::getModuleType, moduleType)
.eq(TaskScopeStateEntity::getScopeHash, scopeHash)
.last("limit 1"));
if (scope == null) {
scope = new TaskScopeStateEntity();
scope.setTaskId(taskId);
scope.setModuleType(moduleType);
scope.setScopeKey(scopeKey);
scope.setScopeHash(scopeHash);
scope.setCreatedAt(LocalDateTime.now());
}
scope.setChunkTotal(chunkTotal);
scope.setReceivedChunkCount(countChunks(taskId, scopeHash));
scope.setLastChunkAt(LocalDateTime.now());
scope.setLastError(request.getError());
scope.setCompleted(Boolean.TRUE.equals(request.getDone()) ? 1 : 0);
scope.setUpdatedAt(LocalDateTime.now());
scope.setStateJson("{\"phase\":\"RECEIVED\",\"llm\":\"PENDING\"}");
if (scope.getId() == null) {
taskScopeStateMapper.insert(scope);
} else {
taskScopeStateMapper.updateById(scope);
}
SubmitContext context = new SubmitContext(task, scopeKey, scopeHash, chunkIndex,
Boolean.TRUE.equals(request.getDone()), request.getError());
if (Boolean.TRUE.equals(request.getDone()) || request.getError() != null && !request.getError().isBlank()) {
finalizer.finalizeTask(task, request.getError(), rowCounter.count(task), true);
} else {
task.setUpdatedAt(LocalDateTime.now());
fileTaskMapper.updateById(task);
}
return context;
}
public SubmitContext persistSubmittedChunk(PreparedSubmittedChunk prepared) {
Long taskId = prepared.task().getId();
FileTaskEntity task = fileTaskMapper.selectById(taskId);
@@ -0,0 +1,62 @@
package com.nanri.aiimage.modules.appearancepatent.service.support;
import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentParsedGroupVo;
import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentParsedRowVo;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* 外观专利的解析行分组(建单与解析载荷共用),从 {@code AppearancePatentTaskService}
* <b>原样搬移</b>(3A 拆薄第十三笔)。归一、首非空与基 id 直接引用
* {@link AppearancePatentRowNormalizer}——服务侧那两枚同名私有件的重复实现随之退场
* (2026-10-19 登记的那笔「服务对 util 零引用」的账)。
*/
public final class AppearancePatentGroupingSupport {
public List<AppearancePatentParsedGroupVo> buildParsedGroups(List<AppearancePatentParsedRowVo> rows) {
List<AppearancePatentParsedGroupVo> groups = new ArrayList<>();
for (List<AppearancePatentParsedRowVo> siblings : groupRowsByBaseId(rows).values()) {
if (siblings == null || siblings.isEmpty()) {
continue;
}
AppearancePatentParsedRowVo first = siblings.getFirst();
AppearancePatentParsedGroupVo group = new AppearancePatentParsedGroupVo();
group.setSourceFileKey(first.getSourceFileKey());
group.setSourceFilename(first.getSourceFilename());
group.setGroupKey(AppearancePatentRowNormalizer.firstNonBlank(first.getGroupKey(),
buildGroupKey(first.getSourceFileKey(), AppearancePatentRowNormalizer.baseId(first.getDisplayId()), first.getRowIndex())));
group.setBaseId(AppearancePatentRowNormalizer.baseId(first.getDisplayId()));
group.setDisplayId(AppearancePatentRowNormalizer.firstNonBlank(first.getDisplayId(), first.getSourceId()));
group.setItemCount(siblings.size());
group.setItems(new ArrayList<>(siblings));
groups.add(group);
}
return groups;
}
public int countParsedGroups(List<AppearancePatentParsedRowVo> rows) {
return groupRowsByBaseId(rows).size();
}
public Map<String, List<AppearancePatentParsedRowVo>> groupRowsByBaseId(List<AppearancePatentParsedRowVo> rows) {
Map<String, List<AppearancePatentParsedRowVo>> result = new LinkedHashMap<>();
if (rows == null) {
return result;
}
for (AppearancePatentParsedRowVo row : rows) {
String key = AppearancePatentRowNormalizer.firstNonBlank(
AppearancePatentRowNormalizer.normalize(row.getGroupKey()),
AppearancePatentRowNormalizer.baseId(row.getDisplayId()));
result.computeIfAbsent(key, ignored -> new ArrayList<>()).add(row);
}
return result;
}
public String buildGroupKey(String sourceFileKey, String rowBaseId, Integer rowIndex) {
return AppearancePatentRowNormalizer.normalize(sourceFileKey) + "::"
+ AppearancePatentRowNormalizer.normalize(rowBaseId) + "@" + (rowIndex == null ? 0 : rowIndex);
}
}
@@ -0,0 +1,171 @@
package com.nanri.aiimage.modules.appearancepatent.service.support;
import com.nanri.aiimage.common.task.TaskStatuses;
import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentParsedPayloadDto;
import com.nanri.aiimage.modules.appearancepatent.service.support.AppearancePatentChunkSubmissionSupport.SubmitContext;
import com.nanri.aiimage.modules.task.mapper.FileTaskMapper;
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.TaskFileJobEntity;
import com.nanri.aiimage.modules.task.service.TaskFileJobService;
import com.nanri.aiimage.modules.usersecret.service.UserApiSecretService;
import com.nanri.aiimage.modules.usersecret.support.UserSecretModule;
import lombok.extern.slf4j.Slf4j;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
/**
* 外观专利的 LLM 提交调度与凭据读取,从 {@code AppearancePatentTaskService} <b>原样搬移</b>
* (3A 拆薄第十三笔):回传后的组装 job 排期、Python 超时恢复时的补提交,以及
* 任务 payload/用户密钥两级读取,连同日志与判序一字未改。
*
* <p><b>搬的是调度,不是批管线本身。</b>批管线在 {@link AppearancePatentLlmBatchPipeline},
* 收尾、行计数、结果行创建与心跳触活仍由服务侧以回调交入。
*/
@Slf4j
public class AppearancePatentLlmSubmissionSupport {
@FunctionalInterface
public interface BatchSubmitter {
void submit(FileTaskEntity task, FileResultEntity result, TaskFileJobEntity job, List<TaskChunkEntity> chunks);
}
private static final String STATUS_SUCCESS = TaskStatuses.FileTask.SUCCESS;
private final String moduleType;
private final FileTaskMapper fileTaskMapper;
private final TaskFileJobService taskFileJobService;
private final Function<Long, List<TaskChunkEntity>> submittedChunksLoader;
private final Function<FileTaskEntity, Integer> rowCounter;
private final BiFunction<FileTaskEntity, Integer, FileResultEntity> resultRecordFinder;
private final BatchSubmitter batchSubmitter;
private final Predicate<Long> submissionComplete;
private final Consumer<Long> taskActivityToucher;
private final Function<FileTaskEntity, String> taskOwnerScopeKeyBuilder;
private final Function<FileTaskEntity, AppearancePatentParsedPayloadDto> payloadReader;
private final UserApiSecretService userApiSecretService;
private final Function<String, String> normalizer;
public AppearancePatentLlmSubmissionSupport(String moduleType,
FileTaskMapper fileTaskMapper,
TaskFileJobService taskFileJobService,
Function<Long, List<TaskChunkEntity>> submittedChunksLoader,
Function<FileTaskEntity, Integer> rowCounter,
BiFunction<FileTaskEntity, Integer, FileResultEntity> resultRecordFinder,
BatchSubmitter batchSubmitter,
Predicate<Long> submissionComplete,
Consumer<Long> taskActivityToucher,
Function<FileTaskEntity, String> taskOwnerScopeKeyBuilder,
Function<FileTaskEntity, AppearancePatentParsedPayloadDto> payloadReader,
UserApiSecretService userApiSecretService,
Function<String, String> normalizer) {
this.moduleType = moduleType;
this.fileTaskMapper = fileTaskMapper;
this.taskFileJobService = taskFileJobService;
this.submittedChunksLoader = submittedChunksLoader;
this.rowCounter = rowCounter;
this.resultRecordFinder = resultRecordFinder;
this.batchSubmitter = batchSubmitter;
this.submissionComplete = submissionComplete;
this.taskActivityToucher = taskActivityToucher;
this.taskOwnerScopeKeyBuilder = taskOwnerScopeKeyBuilder;
this.payloadReader = payloadReader;
this.userApiSecretService = userApiSecretService;
this.normalizer = normalizer;
}
public void submitLlmForSubmittedChunk(SubmitContext context) {
if (context == null || context.task() == null || context.task().getId() == null) {
return;
}
FileTaskEntity task = fileTaskMapper.selectById(context.task().getId());
if (task == null || !moduleType.equals(task.getModuleType()) || STATUS_SUCCESS.equals(task.getStatus())) {
return;
}
List<TaskChunkEntity> chunks = submittedChunksLoader.apply(task.getId());
if (chunks.isEmpty()) {
log.info("[appearance-patent] skip stale recovery llm submission because no submitted chunks remain taskId={}",
task.getId());
return;
}
FileResultEntity result = resultRecordFinder.apply(task, rowCounter.apply(task));
if (result == null) {
log.warn("[appearance-patent] stale recovery could not create result record taskId={}", task.getId());
return;
}
TaskFileJobEntity job = taskFileJobService.enqueueAssembleResult(
task.getId(), moduleType, result.getId(), taskOwnerScopeKeyBuilder.apply(task));
if (job == null || "SUCCESS".equals(job.getStatus())) {
log.info("[appearance-patent] stale recovery skipped llm submission because result job unavailable taskId={} resultId={} jobStatus={}",
task.getId(), result.getId(), job == null ? null : job.getStatus());
return;
}
log.info("[appearance-patent] stale recovery created assemble job taskId={} resultId={} jobId={} jobStatus={} chunkCount={}",
task.getId(), result.getId(), job.getId(), job.getStatus(), chunks.size());
batchSubmitter.submit(task, result, job, chunks);
if (submissionComplete.test(task.getId())) {
taskFileJobService.requeue(job.getId(), "检测结果已回流,正在组装 xlsx");
} else {
taskFileJobService.touchRunning(job.getId());
}
taskActivityToucher.accept(task.getId());
}
public void scheduleLlmPipelineForSubmittedChunk(SubmitContext context) {
if (context == null || context.task() == null || context.task().getId() == null) {
return;
}
FileTaskEntity task = fileTaskMapper.selectById(context.task().getId());
if (task == null || !moduleType.equals(task.getModuleType()) || STATUS_SUCCESS.equals(task.getStatus())) {
return;
}
List<TaskChunkEntity> chunks = submittedChunksLoader.apply(task.getId());
if (chunks.isEmpty()) {
return;
}
FileResultEntity result = resultRecordFinder.apply(task, rowCounter.apply(task));
if (result == null) {
return;
}
TaskFileJobEntity job = taskFileJobService.enqueueAssembleResult(
task.getId(), moduleType, result.getId(), taskOwnerScopeKeyBuilder.apply(task));
if (job == null || "SUCCESS".equals(job.getStatus())) {
return;
}
taskFileJobService.requeue(job.getId(), "Appearance patent result uploaded, scheduling LLM/file assembly");
taskActivityToucher.accept(task.getId());
}
public String readAiPrompt(FileTaskEntity task) {
try {
return payloadReader.apply(task).getAiPrompt();
} catch (Exception ignored) {
return "";
}
}
/** 密钥读取:任务 payload 优先(兼容老任务与显式覆盖),为空时按任务归属用户从服务端密钥表兜底。 */
public String readApiKey(FileTaskEntity task) {
try {
String fromPayload = normalizer.apply(payloadReader.apply(task).getApiKey());
if (!fromPayload.isEmpty()) {
return fromPayload;
}
} catch (Exception ex) {
log.warn("[appearance-patent] 读取任务 payload 密钥失败,尝试用户密钥兜底 taskId={} err={}",
task.getId(), ex.getMessage());
}
String fromUserSecret = userApiSecretService.findPlainValue(
task.getUserId(), UserSecretModule.APPEARANCE_PATENT.key());
if (fromUserSecret.isEmpty()) {
log.warn("[appearance-patent] 任务未携带密钥且用户未配置密钥,LLM 检测将保留原始行 taskId={} userId={}",
task.getId(), task.getUserId());
}
return fromUserSecret;
}
}
@@ -0,0 +1,132 @@
package com.nanri.aiimage.modules.appearancepatent.service.support;
import com.nanri.aiimage.common.task.TaskStatuses;
import com.nanri.aiimage.modules.appearancepatent.service.support.AppearancePatentChunkSubmissionSupport.SubmitContext;
import com.nanri.aiimage.modules.task.mapper.FileTaskMapper;
import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
import com.nanri.aiimage.modules.task.model.entity.TaskChunkEntity;
import com.nanri.aiimage.modules.task.service.TaskFileJobService;
import lombok.extern.slf4j.Slf4j;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
/**
* 外观专利的陈旧任务收尾(A 聚类的收尾一格),从 {@code AppearancePatentTaskService}
* <b>原样搬移</b>(3A 拆薄第十三笔):判死收尾、Python 超时恢复探针、封口与 LLM 补提交,
* 连同事务边界之外的形状与全部日志文案一字未改。
*
* <p><b>搬的是收尾编排,不是事务与封口本身。</b>{@code inNewTransaction} 两处包装仍留在服务侧
* 以 {@code markSubmissionComplete}/{@code finalizer} 两个回调交入——它们要动的是现网事务边界,
* 跟着一起搬只会把「哪个线程在哪个事务里」这句读丢。
*/
@Slf4j
public class AppearancePatentStaleTaskFinalizer {
private static final String STATUS_RUNNING = TaskStatuses.FileTask.RUNNING;
private final String moduleType;
private final FileTaskMapper fileTaskMapper;
private final TaskFileJobService taskFileJobService;
private final Function<FileTaskEntity, Integer> rowCounter;
private final Predicate<Long> submissionComplete;
private final Predicate<Long> persistedRowsPresent;
private final Function<Long, Integer> markSubmissionComplete;
private final Function<Long, List<TaskChunkEntity>> submittedChunksLoader;
private final Function<List<TaskChunkEntity>, Integer> uninspectedRowCounter;
private final Consumer<SubmitContext> llmSubmitter;
private final Consumer<Long> taskActivityToucher;
private final BiConsumer<FileTaskEntity, String> finalizer;
public AppearancePatentStaleTaskFinalizer(String moduleType,
FileTaskMapper fileTaskMapper,
TaskFileJobService taskFileJobService,
Function<FileTaskEntity, Integer> rowCounter,
Predicate<Long> submissionComplete,
Predicate<Long> persistedRowsPresent,
Function<Long, Integer> markSubmissionComplete,
Function<Long, List<TaskChunkEntity>> submittedChunksLoader,
Function<List<TaskChunkEntity>, Integer> uninspectedRowCounter,
Consumer<SubmitContext> llmSubmitter,
Consumer<Long> taskActivityToucher,
BiConsumer<FileTaskEntity, String> finalizer) {
this.moduleType = moduleType;
this.fileTaskMapper = fileTaskMapper;
this.taskFileJobService = taskFileJobService;
this.rowCounter = rowCounter;
this.submissionComplete = submissionComplete;
this.persistedRowsPresent = persistedRowsPresent;
this.markSubmissionComplete = markSubmissionComplete;
this.submittedChunksLoader = submittedChunksLoader;
this.uninspectedRowCounter = uninspectedRowCounter;
this.llmSubmitter = llmSubmitter;
this.taskActivityToucher = taskActivityToucher;
this.finalizer = finalizer;
}
/**
* stale 判死收尾:事务只包住真正需要原子的两段(封口状态批量更新、任务终态 + 结果 + 组装作业入队),
* **LLM 批次留在事务之外**。
*
* <p>外层调用点曾把整个方法包进 {@code inNewTransaction},于是恢复分支里
* {@code submitLlmBatches} 每批开头 {@code touchRunning} 对 biz_task_file_job 取的 X 锁,
* 要一直活到串行发完整批 LLM 请求
* (线上实测单批 costMs=1077959)之后才释放——两台的卡死自愈扫描 UPDATE 同一行,
* 每 110 秒撞一次 50 秒锁等待,还顺手按住一条 Hikari 连接十几分钟、并把已扣过 LLM 次数的状态
* 置于可回滚的位置。同源的 {@code SimilarAsinTaskService#finalizeStaleTask} 一直是这个形状。
*/
public void finalizeStaleTask(Long taskId, String error) {
FileTaskEntity task = fileTaskMapper.selectById(taskId);
if (task == null || !moduleType.equals(task.getModuleType()) || !STATUS_RUNNING.equals(task.getStatus())) {
return;
}
if (tryRecoverTimedOutPythonTask(task)) {
return;
}
finalizer.accept(task, error);
}
private boolean tryRecoverTimedOutPythonTask(FileTaskEntity task) {
if (task == null || task.getId() == null) {
return false;
}
Long taskId = task.getId();
boolean uploadComplete = submissionComplete.test(taskId);
long activeAssembleJobs = taskFileJobService.countActiveAssembleJobs(taskId, moduleType);
log.info("[appearance-patent] stale recovery probe taskId={} uploadComplete={} activeAssembleJobs={} persistedRows={}",
taskId, uploadComplete, activeAssembleJobs, persistedRowsPresent.test(taskId));
// 已有「重试耗尽且已终态收尾」的 assemble job:说明恢复已经试过、缺失是永久的。
// 再重建只会每 30 秒空转一轮,而且恢复过程刷新任务心跳会让任务永远 RUNNING
// (线上任务 28459 实测:48 分钟里每隔 30 秒重建一次 job)。返回 false 交给
// finalizeStaleTask 按失败收尾,用户看到明确失败而不是无限等待。
if (taskFileJobService.hasExhaustedAssembleJob(taskId, moduleType)) {
log.warn("[appearance-patent] stale recovery 放弃:已有重试耗尽的 assemble job,按失败收尾 taskId={}", taskId);
return false;
}
if (!persistedRowsPresent.test(taskId)) {
log.info("[appearance-patent] stale recovery aborted because no persisted rows taskId={}", taskId);
return false;
}
if (!uploadComplete) {
// 封口是多行 scope 状态更新,单独占一个短事务;紧随其后的 LLM 批次不在事务里
int forcedScopes = markSubmissionComplete.apply(taskId);
if (forcedScopes <= 0) {
log.warn("[appearance-patent] stale recovery aborted because submission could not be marked complete taskId={}",
taskId);
return false;
}
int pendingRows = uninspectedRowCounter.apply(submittedChunksLoader.apply(taskId));
log.warn("[appearance-patent] Python 回传心跳超时,已封口上传并继续检测/文件收尾 taskId={} pendingRows={} activeAssembleJobs={} forcedScopes={}",
taskId, pendingRows, activeAssembleJobs, forcedScopes);
} else {
log.warn("[appearance-patent] Python 超时恢复继续推进检测/文件收尾 taskId={} activeAssembleJobs={}",
taskId, activeAssembleJobs);
}
llmSubmitter.accept(new SubmitContext(task, null, null, 0, true, null));
taskActivityToucher.accept(taskId);
return true;
}
}
@@ -0,0 +1,139 @@
package com.nanri.aiimage.modules.appearancepatent.service.support;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.common.task.TaskStatuses;
import com.nanri.aiimage.common.service.ResultDownloadResolver;
import com.nanri.aiimage.modules.appearancepatent.service.AppearancePatentTaskCacheService;
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.TaskFileJobService;
import lombok.extern.slf4j.Slf4j;
import java.util.List;
import java.util.Objects;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
/**
* 外观专利的任务记录操作(删除、历史、下载与激活校验),从 {@code AppearancePatentTaskService}
* <b>原样搬移</b>(3A 拆薄第十三笔):事务注解仍留在服务侧入口(委托调用发生在同一事务内),
* 提交后远端载荷删除与缓存清理的顺序、文案一字未改。
*/
@Slf4j
public class AppearancePatentTaskRecordSupport {
private static final String STATUS_PENDING = TaskStatuses.FileTask.PENDING;
private static final String STATUS_RUNNING = TaskStatuses.FileTask.RUNNING;
private final String moduleType;
private final FileTaskMapper fileTaskMapper;
private final FileResultMapper fileResultMapper;
private final TaskScopeStateMapper taskScopeStateMapper;
private final TaskChunkMapper taskChunkMapper;
private final TaskFileJobService taskFileJobService;
private final AppearancePatentTaskCacheService taskCacheService;
private final ResultDownloadResolver resultDownloadResolver;
private final Function<Long, List<String>> payloadCollector;
private final BiConsumer<List<String>, Long> payloadDeleter;
private final Consumer<Runnable> afterCommitRunner;
public AppearancePatentTaskRecordSupport(String moduleType,
FileTaskMapper fileTaskMapper,
FileResultMapper fileResultMapper,
TaskScopeStateMapper taskScopeStateMapper,
TaskChunkMapper taskChunkMapper,
TaskFileJobService taskFileJobService,
AppearancePatentTaskCacheService taskCacheService,
ResultDownloadResolver resultDownloadResolver,
Function<Long, List<String>> payloadCollector,
BiConsumer<List<String>, Long> payloadDeleter,
Consumer<Runnable> afterCommitRunner) {
this.moduleType = moduleType;
this.fileTaskMapper = fileTaskMapper;
this.fileResultMapper = fileResultMapper;
this.taskScopeStateMapper = taskScopeStateMapper;
this.taskChunkMapper = taskChunkMapper;
this.taskFileJobService = taskFileJobService;
this.taskCacheService = taskCacheService;
this.resultDownloadResolver = resultDownloadResolver;
this.payloadCollector = payloadCollector;
this.payloadDeleter = payloadDeleter;
this.afterCommitRunner = afterCommitRunner;
}
public void activateTask(Long taskId, Long userId) {
FileTaskEntity task = fileTaskMapper.selectById(taskId);
if (task == null || !moduleType.equals(task.getModuleType()) || !Objects.equals(userId, task.getUserId())) {
throw new BusinessException("任务不存在");
}
// 2026-09-30 起外观专利已整体迁移 Linux 后台执行(无浏览器抓取):
// activate 不再把任务推给客户端浏览器。任务保持 PENDING,由内部认领端点
// (/api/internal/tasks/pull-pending + maixiang-ap worker)在等待窗口后自动接管。
// 这里保留接口仅做状态校验:旧前端(未刷新页面缓存)点「启动」时给出明确提示,
// 前端收到业务异常会中止入队逻辑,任务不会被执行两次。
if (STATUS_PENDING.equals(task.getStatus())) {
log.info("[外观专利] activate 不再推送客户端,任务保持 PENDING 等待 Linux 后台认领 taskId={} userId={}", taskId, userId);
throw new BusinessException("外观专利已切换后台自动执行,无需打开浏览器,稍候自动开始");
}
if (STATUS_RUNNING.equals(task.getStatus())) {
throw new BusinessException("任务已在执行中(可能已由后台自动接管),无需重复启动");
}
throw new BusinessException("任务已结束");
}
/**
* 删除任务。
*
* <p>事务边界:远端载荷删除与缓存清理移到事务提交后执行 —— 单次删除超时 120s、重试 3 次,
* 放在 DB 事务里会让连接被长时间占用(项目在 TaskScopePayloadStorageService 已写明该约束)。
*/
public void deleteTask(Long taskId, Long userId) {
FileTaskEntity task = fileTaskMapper.selectById(taskId);
if (task == null || !moduleType.equals(task.getModuleType()) || !Objects.equals(userId, task.getUserId())) {
throw new BusinessException("任务不存在");
}
List<String> payloads = payloadCollector.apply(taskId);
fileResultMapper.delete(new LambdaQueryWrapper<FileResultEntity>().eq(FileResultEntity::getTaskId, taskId).eq(FileResultEntity::getModuleType, moduleType));
taskScopeStateMapper.delete(new LambdaQueryWrapper<TaskScopeStateEntity>().eq(TaskScopeStateEntity::getTaskId, taskId).eq(TaskScopeStateEntity::getModuleType, moduleType));
taskChunkMapper.delete(new LambdaQueryWrapper<TaskChunkEntity>().eq(TaskChunkEntity::getTaskId, taskId).eq(TaskChunkEntity::getModuleType, moduleType));
// 同步清理 task_file_job,避免被删除任务遗留的 PENDING/FAILED 行被 TaskResultFileJobWorker 反复扫描出 task not found。
taskFileJobService.deleteTaskJobs(taskId, moduleType);
fileTaskMapper.deleteById(taskId);
// 事务提交后再做远端删除与缓存清理
payloadDeleter.accept(payloads, taskId);
afterCommitRunner.accept(() -> taskCacheService.deleteTaskCache(taskId));
}
public void deleteHistory(Long resultId, Long userId) {
FileResultEntity row = fileResultMapper.selectById(resultId);
if (row == null || !moduleType.equals(row.getModuleType()) || !Objects.equals(userId, row.getUserId())) {
throw new BusinessException("记录不存在");
}
// 同步清理 task_file_job,避免被删除历史记录残留的 ASSEMBLE_RESULT job 被反复扫描出 result record not found。
taskFileJobService.deleteResultJobs(row.getTaskId(), moduleType, resultId);
fileResultMapper.deleteById(resultId);
}
public String resolveResultDownloadUrl(Long resultId, Long userId) {
// 2026-09 去重:与原实现等价(并修掉 userId.equals 的潜在 NPE),实现收口到 common
return resultDownloadResolver.resolveUrl(resultId, userId, moduleType);
}
public String resolveResultDownloadFilename(Long resultId, Long userId) {
FileResultEntity row = fileResultMapper.selectById(resultId);
if (row == null || !moduleType.equals(row.getModuleType()) || !Objects.equals(userId, row.getUserId())) {
throw new BusinessException("记录不存在");
}
return row.getResultFilename() == null || row.getResultFilename().isBlank()
? "appearance-patent-" + resultId + ".xlsx"
: row.getResultFilename();
}
}