refactor(phase-3): 首迁 ShopMatch 接入管线底座,顺手补齐三件实测缺件

蓝图 §5 Phase 2 那条「两支公共件目前零生产调用方、契约可用只由单测证明」在本笔解除:
ShopMatchTaskService 现在真的继承 AbstractShopPayloadTaskPipeline,submitResult 的
「准入→取锁→按店名建索引→逐行合并→changed 闸门→重算状态」整段改为骨架调用,
本支只留 applyShopResultRow 与 onShopRowsRecomputed 两个钩子——判序(先合并再判 error)
留在被迁支里,家族层 javadoc 点名「刻意不沉」的就是它,没动。

底座缺件三件,都是「换不上就是本类缺件」那条规则的实测产物:
① newTaskHeader/newScopeRow 各加一枚带显式时刻的重载。ShopMatch 建单用的是
   now()=LocalDateTime.now(Asia/Shanghai) 且同一枚时刻复用给每一行结果行,
   底座原来只有默认时区、逐行各取一次那一版——直接套上去就把建单时间整体挪了,
   而这条快照量不出来(时钟刻意不进快照)。PriceTrack 那一派仍用五参/六参默认版。
② writeJson:Phase 2 登记「随 Phase 3 首迁同笔落」的那件,这笔落了。
   捕获账因此是净减(整树 total 1003→1002、RETHROW 387→386 正落在下限上、decoy 0),
   没有放宽任何一格;task 模块内部那三份逐字同名件仍未归并——归并会跌破 RETHROW 下限,
   那是「调整门禁判据」动作,与 Phase 0 三格守卫同批做,不在迁移笔里顺手干。
③ onPayloadIndexed(默认空实现):底座刻意不打日志,而 PriceTrack:617 有一句
   「received payloadShopCount/payloadKeys」卡在准入之后、行循环之前、每次回传恰好一次。
   没有这一格,那句要么丢、要么得挪位置——挪位置就是改现网可观测行为。

迁移登记的三处步序微差(可见结果相同,逐条对过 golden):shops 非空检查从锁内移到锁外、
锁内那次重复的 taskId 检查改由准入序列承担、旧 :670 那枚没人用的 workRoot 死局部变量摘掉
(组装在 processResultFileJob 里各建各的,本层从未写过那个目录)。

证据:shopmatch 两条 golden(建单 5 例+回传流水 5 例)迁移后逐字节未动;
CatchSilence/TaskLifecycleDebt/TaskStatusVocabulary/ArchitectureBoundary(task→业务 0)
四把尺同批复跑全绿;ShopMatchTaskService 2027→2026 行(readability 只许降这条满足)。
This commit is contained in:
2026-10-18 20:47:10 +08:00
parent 121ae9b064
commit 1be4d766bb
5 changed files with 384 additions and 144 deletions
@@ -1,11 +1,9 @@
package com.nanri.aiimage.modules.shopmatch.service;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.common.security.AdminAuthSupport;
import com.nanri.aiimage.config.TaskPressureProperties;
import com.nanri.aiimage.modules.file.service.oss.OssStorageService;
import com.nanri.aiimage.modules.productrisk.model.enums.ProductRiskCountryCode;
@@ -38,10 +36,12 @@ 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.TaskFileJobEntity;
import com.nanri.aiimage.modules.task.service.AbstractModuleTaskCacheService;
import com.nanri.aiimage.modules.task.service.TaskDistributedLockService;
import com.nanri.aiimage.modules.task.service.TaskFileJobService;
import com.nanri.aiimage.modules.task.service.TaskFinalizeSupport;
import com.nanri.aiimage.modules.task.service.TaskResultPayloadService;
import com.nanri.aiimage.modules.task.service.pipeline.AbstractShopPayloadTaskPipeline;
import com.nanri.aiimage.modules.ziniao.service.ZiniaoShopSwitchService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@@ -63,9 +63,8 @@ import com.nanri.aiimage.modules.task.model.vo.TaskProgressLightBatchVo;
import com.nanri.aiimage.modules.task.service.TaskProgressLightAssembler;
@Service
@RequiredArgsConstructor
@Slf4j
public class ShopMatchTaskService {
public class ShopMatchTaskService extends AbstractShopPayloadTaskPipeline<ShopMatchShopPayloadDto> {
private static final String MODULE_TYPE = "SHOP_MATCH";
@@ -75,20 +74,15 @@ public class ShopMatchTaskService {
private static final String CONTENT_TYPE_XLSX = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
private static final ZoneId BUSINESS_ZONE = ZoneId.of("Asia/Shanghai");
private final FileTaskMapper fileTaskMapper;
private final FileResultMapper fileResultMapper;
private final ShopMatchShopCandidateMapper candidateMapper;
private final ZiniaoShopSwitchService ziniaoShopSwitchService;
private final ShopMatchExcelAssemblyService excelAssemblyService;
private final OssStorageService ossStorageService;
/** 结果下载直链解析(2026-09 从本类抽到 common,消除 11 处逐字重复) */
private final com.nanri.aiimage.common.service.ResultDownloadResolver resultDownloadResolver;
private final ObjectMapper objectMapper;
private final ShopMatchTaskCacheService shopMatchTaskCacheService;
private final TaskPressureProperties taskPressureProperties;
private final TaskResultPayloadService taskResultPayloadService;
private final TaskFileJobService taskFileJobService;
private final TaskDistributedLockService taskDistributedLockService;
private final SkipPriceAsinService skipPriceAsinService;
private final QueryAsinMapper queryAsinMapper;
private final TaskProgressLightAssembler taskProgressLightAssembler;
@@ -97,6 +91,74 @@ public class ShopMatchTaskService {
*/
private final org.springframework.transaction.PlatformTransactionManager transactionManager;
/**
* 参数表顺序=原先 {@code @RequiredArgsConstructor} 按字段声明序生成的那一个(测试夹具按位置 new)。
* 头两枚加锁/Job/存储三枚交 {@code super} 由底座持有,本类那 30 余处引用改读继承来的 protected 字段、调用点未变。
*/
public ShopMatchTaskService(FileTaskMapper fileTaskMapper,
FileResultMapper fileResultMapper,
ShopMatchShopCandidateMapper candidateMapper,
ZiniaoShopSwitchService ziniaoShopSwitchService,
ShopMatchExcelAssemblyService excelAssemblyService,
OssStorageService ossStorageService,
com.nanri.aiimage.common.service.ResultDownloadResolver resultDownloadResolver,
ObjectMapper objectMapper,
ShopMatchTaskCacheService shopMatchTaskCacheService,
TaskPressureProperties taskPressureProperties,
TaskResultPayloadService taskResultPayloadService,
TaskFileJobService taskFileJobService,
TaskDistributedLockService taskDistributedLockService,
SkipPriceAsinService skipPriceAsinService,
QueryAsinMapper queryAsinMapper,
TaskProgressLightAssembler taskProgressLightAssembler,
org.springframework.transaction.PlatformTransactionManager transactionManager) {
super(fileTaskMapper, fileResultMapper, taskDistributedLockService, taskFileJobService, ossStorageService);
this.candidateMapper = candidateMapper;
this.ziniaoShopSwitchService = ziniaoShopSwitchService;
this.excelAssemblyService = excelAssemblyService;
this.resultDownloadResolver = resultDownloadResolver;
this.objectMapper = objectMapper;
this.shopMatchTaskCacheService = shopMatchTaskCacheService;
this.taskPressureProperties = taskPressureProperties;
this.taskResultPayloadService = taskResultPayloadService;
this.skipPriceAsinService = skipPriceAsinService;
this.queryAsinMapper = queryAsinMapper;
this.taskProgressLightAssembler = taskProgressLightAssembler;
this.transactionManager = transactionManager;
}
// ------------------------------------------------------------------
// 管线钩子(Phase 3 首迁):形状在底座,模块语义留在下面这几个方法里
// ------------------------------------------------------------------
@Override
protected String moduleType() {
return MODULE_TYPE;
}
@Override
protected FileTaskEntity loadTaskForSubmission(Long taskId) {
return loadTaskForExecution(taskId); // 本支是缓存优先派(带历史复活防护的本地变体),不在底座里
}
@Override
protected AbstractModuleTaskCacheService<ShopMatchShopPayloadDto> taskCacheService() {
return shopMatchTaskCacheService;
}
@Override
protected String submissionLockLogTag() {
// 历史标签是小写短名,不是 moduleType();锁竞争日志有人按它数过
return "shop-match";
}
@Override
protected String normalizeShopKey(String rawShopName) {
// 建单与回传必须同一条规则(:499 写进 sourceFilename 的就是它);该 bean 在 modules.ziniao 下,
// 底座直接依赖会把 task→业务 的冻结线(0)顶开,所以留成钩子。
return ziniaoShopSwitchService.normalizeShopName(rawShopName);
}
private FileTaskEntity loadTaskForExecution(Long taskId) {
Map<Long, FileTaskEntity> cachedTasks = shopMatchTaskCacheService.getTaskCacheBatch(List.of(taskId));
FileTaskEntity cached = cachedTasks.get(taskId);
@@ -470,21 +532,12 @@ public class ShopMatchTaskService {
persistedRequest.setCurrentStageIndex(0);
}
FileTaskEntity task = new FileTaskEntity();
task.setTaskNo(MODULE_TYPE + "-" + IdUtil.getSnowflakeNextIdStr());
task.setModuleType(MODULE_TYPE);
task.setTaskMode("PYTHON_QUEUE");
task.setStatus(scheduleTimes.isEmpty() ? "RUNNING" : "SCHEDULED");
task.setSourceFileCount(uniqueItems.size());
FileTaskEntity task = newTaskHeader(scheduleTimes.isEmpty() ? "RUNNING" : "SCHEDULED",
"PYTHON_QUEUE", request.getUserId(), deviceId, uniqueItems.size(), now);
task.setSuccessFileCount(0);
task.setFailedFileCount(0);
task.setCreatedBy("user:" + request.getUserId());
task.setUserId(request.getUserId());
task.setDeviceId(AdminAuthSupport.normalizeDeviceId(deviceId));
task.setCreatedAt(now);
task.setUpdatedAt(now);
task.setScheduledAt(scheduleTimes.isEmpty() ? null : scheduleTimes.get(0));
fileTaskMapper.insert(task);
insertTask(task);
shopMatchTaskCacheService.saveTaskCache(task);
List<ProductRiskResultItemVo> snapshot = new ArrayList<>();
@@ -493,23 +546,14 @@ public class ShopMatchTaskService {
if (normalized.isBlank()) {
throw new BusinessException("任务数据已失效,请刷新后重试");
}
FileResultEntity result = new FileResultEntity();
result.setTaskId(task.getId());
result.setModuleType(MODULE_TYPE);
result.setSourceFilename(normalized);
result.setSourceFileUrl(item.getShopId());
result.setUserId(request.getUserId());
result.setSuccess(0);
result.setCreatedAt(now);
// rowCount 留 null(本支建单时不写这列)、success 显式 0;时刻复用建单那一枚 now
FileResultEntity result = newScopeRow(task.getId(), request.getUserId(), normalized,
item.getShopId(), null, 0, now);
fileResultMapper.insert(result);
snapshot.add(toSnapshotVo(result, item, task.getStatus()));
}
try {
task.setRequestJson(objectMapper.writeValueAsString(persistedRequest));
task.setResultJson(objectMapper.writeValueAsString(snapshot));
} catch (Exception ex) {
throw new BusinessException("task 不存在");
}
task.setRequestJson(writeJson(objectMapper, persistedRequest, "task 不存在"));
task.setResultJson(writeJson(objectMapper, snapshot, "task 不存在"));
updateTaskAndRefreshCache(task);
if ("RUNNING".equals(task.getStatus())) {
shopMatchTaskCacheService.touchTaskHeartbeat(task.getId());
@@ -628,85 +672,51 @@ public class ShopMatchTaskService {
* 且终态判断读的是最长 60s 的缓存)。
*/
public void submitResult(Long taskId, ShopMatchSubmitResultRequest request) {
if (taskId == null || taskId <= 0) {
throw new BusinessException("taskId 不合法");
// 段 3 骨架(准入→取锁→建索引→逐行→changed 闸门→重算状态)在底座,本支只交「单行怎么处理」那一段。
submitShopScopedResults(taskId,
request == null ? null : request.getShops(),
ShopMatchShopPayloadDto::getShopName,
TerminalRejection.IDEMPOTENT,
null);
}
/**
* 迁移前 {@code submitResultLocked} 的全部模块语义就是下面这一个方法;骨架在
* {@link AbstractShopPayloadTaskPipeline#submitShopScopedResults}。三处步序微差登记在蓝图
* §4 Phase 3 实录(锁位、被摘掉的死局部变量 workRoot、锁内重查 taskId),
* golden {@code shopmatch/golden/submit-result-snapshot.txt} 逐字节未动是这一笔的准出证据。
*/
@Override
protected void applyShopResultRow(Long taskId,
FileTaskEntity task,
FileResultEntity result,
String shopKey,
ShopMatchShopPayloadDto incoming) {
ShopMatchShopPayloadDto merged = mergeShopPayload(taskId, shopKey, incoming);
if (incoming.getError() != null && !incoming.getError().isBlank()) {
finalizeFailedShop(result, shopKey, merged, incoming.getError().trim());
return;
}
try (TaskDistributedLockService.LockHandle ignored = acquireTaskLockOrThrow(taskId)) {
submitResultLocked(taskId, request);
if (!isShopPayloadCompleted(merged)) {
return;
}
if (countPayloadRows(merged) <= 0) {
markResultFailed(result, "未收到可用于组装的结果数据");
shopMatchTaskCacheService.removeShopMergedPayload(taskId, shopKey);
return;
}
try {
enqueueResultFileAssembly(result, shopKey, merged);
shopMatchTaskCacheService.removeShopMergedPayload(taskId, shopKey);
} catch (Exception ex) {
markResultFailed(result, ex.getMessage() == null ? "结果组装失败" : ex.getMessage());
shopMatchTaskCacheService.removeShopMergedPayload(taskId, shopKey);
}
}
private void submitResultLocked(Long taskId, ShopMatchSubmitResultRequest request) {
if (taskId == null || taskId <= 0) {
throw new BusinessException("taskId 不合法");
}
if (request == null || request.getShops() == null || request.getShops().isEmpty()) {
throw new BusinessException("shops 不能为空");
}
FileTaskEntity task = loadTaskForExecution(taskId);
if (task == null || !MODULE_TYPE.equals(task.getModuleType())) {
throw new BusinessException("任务不存在");
}
if ("SUCCESS".equals(task.getStatus()) || "FAILED".equals(task.getStatus())) {
throw new BusinessException(40901, "任务已结束,拒绝重复提交");
}
shopMatchTaskCacheService.touchTaskHeartbeat(taskId);
List<FileResultEntity> resultRows = fileResultMapper.selectList(new LambdaQueryWrapper<FileResultEntity>()
.eq(FileResultEntity::getTaskId, taskId)
.eq(FileResultEntity::getModuleType, MODULE_TYPE)
.orderByAsc(FileResultEntity::getId));
Map<String, ShopMatchShopPayloadDto> payloadByShop = new LinkedHashMap<>();
for (ShopMatchShopPayloadDto item : request.getShops()) {
if (item == null) {
continue;
}
String key = ziniaoShopSwitchService.normalizeShopName(item.getShopName());
if (!key.isBlank()) {
payloadByShop.put(key, item);
}
}
File workRoot = FileUtil.mkdir(FileUtil.file(System.getProperty("java.io.tmpdir"), "shop-match-result", String.valueOf(taskId)));
boolean changed = false;
for (FileResultEntity result : resultRows) {
String shopKey = result.getSourceFilename();
ShopMatchShopPayloadDto incoming = payloadByShop.get(shopKey);
if (incoming == null) {
continue;
}
changed = true;
ShopMatchShopPayloadDto merged = mergeShopPayload(taskId, shopKey, incoming);
if (incoming.getError() != null && !incoming.getError().isBlank()) {
finalizeFailedShop(result, shopKey, merged, incoming.getError().trim());
continue;
}
if (!isShopPayloadCompleted(merged)) {
continue;
}
if (countPayloadRows(merged) <= 0) {
markResultFailed(result, "未收到可用于组装的结果数据");
shopMatchTaskCacheService.removeShopMergedPayload(taskId, shopKey);
continue;
}
try {
enqueueResultFileAssembly(result, shopKey, merged);
shopMatchTaskCacheService.removeShopMergedPayload(taskId, shopKey);
} catch (Exception ex) {
markResultFailed(result, ex.getMessage() == null ? "结果组装失败" : ex.getMessage());
shopMatchTaskCacheService.removeShopMergedPayload(taskId, shopKey);
}
}
if (!changed) {
return;
}
List<FileResultEntity> latest = fileResultMapper.selectList(new LambdaQueryWrapper<FileResultEntity>()
.eq(FileResultEntity::getTaskId, taskId)
.eq(FileResultEntity::getModuleType, MODULE_TYPE)
.orderByAsc(FileResultEntity::getId));
updateTaskStatusFromLatestRows(task, latest);
@Override
protected void onShopRowsRecomputed(FileTaskEntity task, List<FileResultEntity> latestRows) {
updateTaskStatusFromLatestRows(task, latestRows);
}
public boolean tryFinalizeTask(Long taskId, boolean fromCompensation) {
@@ -825,19 +835,10 @@ public class ShopMatchTaskService {
boolean partialFailure = result.getErrorMessage() != null && !result.getErrorMessage().isBlank();
try {
excelAssemblyService.writeWorkbook(xlsx, countries);
String objectKey = ossStorageService.uploadResultFile(xlsx, MODULE_TYPE);
result.setResultFilename(stem + ".xlsx");
result.setResultFileUrl(objectKey);
result.setResultFileSize(xlsx.length());
result.setResultContentType(CONTENT_TYPE_XLSX);
result.setRowCount(excelAssemblyService.countRows(countries));
if (partialFailure) {
result.setSuccess(0);
} else {
result.setSuccess(1);
result.setErrorMessage(null);
}
fileResultMapper.updateById(result);
// 上传+结果行 6 列落库是六支逐字相同的那一截,沉在底座(publishResultFile);
// 临时文件的清理点各支不同,仍留在本层 finally 里。
publishResultFile(result, xlsx, stem + ".xlsx", CONTENT_TYPE_XLSX,
excelAssemblyService.countRows(countries), partialFailure);
} finally {
FileUtil.del(xlsx);
}
@@ -880,7 +881,8 @@ public class ShopMatchTaskService {
ShopMatchShopPayloadDto payload, boolean preserveFailure) {
taskResultPayloadService.saveLatest(result.getTaskId(), MODULE_TYPE, shopKey, payload);
markResultFilePending(result, shopKey, payload, preserveFailure);
taskFileJobService.enqueueAssembleResult(result.getTaskId(), MODULE_TYPE, result.getId(), shopKey);
// 挂 Job 那一转在底座(scopeKey 本支是店铺名,四形之一,底座只转发不解释)
enqueueResultAssembly(result.getTaskId(), result.getId(), shopKey);
}
/**
@@ -1045,7 +1047,7 @@ public class ShopMatchTaskService {
* 显式短事务:锁已在事务外获取,这里只包住 DB 段,
* 保证原来同属一个 @Transactional 的多条写仍在同一事务内。
*/
private <T> T inNewTransaction(java.util.function.Supplier<T> action) {
private <R> R inNewTransaction(java.util.function.Supplier<R> action) {
if (transactionManager == null) {
return action.get();
}
@@ -1296,11 +1298,8 @@ public class ShopMatchTaskService {
}
private void persistTaskRequest(FileTaskEntity task, ShopMatchCreateTaskRequest request) {
try {
task.setRequestJson(objectMapper.writeValueAsString(request));
} catch (Exception ex) {
throw new BusinessException("序列化任务数据失败");
}
// 序列化失败即拒、不带 cause——七支同形的那一截,沉在底座 writeJson(文案是本支自己的那句,逐字保留)
task.setRequestJson(writeJson(objectMapper, request, "序列化任务数据失败"));
}
private void updateTaskAndRefreshCache(FileTaskEntity task) {
@@ -2,6 +2,7 @@ package com.nanri.aiimage.modules.task.service.pipeline;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.common.exception.BusinessCodes;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.common.security.AdminAuthSupport;
@@ -38,16 +39,17 @@ import java.util.List;
* (AppearancePatent 与 DeleteBrand 全程无事务、Publish/CollectData 用
* {@code TransactionTemplate}、ShopDataCrawl/ShopMatch 用 {@code @Transactional}),
* 返回 VO 字段互不相交。所以 parse 是钩子留在各支,底座只提供落库件。</li>
* <li><b>序列化那半件本轮不沉</b>:七支建单时都写「{@code writeValueAsString} 失败即
* {@code BusinessException}(不带 cause)」,形状一致、只有文案不同,本来是最干净的公共件。
* 但 {@code config/CatchSilenceRatchetTest} 的自述规则是「新增捕获块要么别加,
* 要么同批减掉一处同形状」,而 Phase 2 禁止改七支 ⇒ 减不掉,只能不加。
* 该件随 Phase 3 首支迁移同笔落:那笔删掉被迁支的本地同名件,净增 ≤ 0。
* 现成模板已存在——{@code PublishTaskService:2118-2124} 的私有 {@code writeJson(Object, String)}
* 就是带文案参数的形状,task 模块内部另有三份逐字同名件
* ({@code TaskProgressSnapshotService:186}/{@code TaskResultItemService:405}/
* {@code TaskResultPayloadService:113}),一并归并才是正解(注意 RETHROW 那格是下限,
* 归并必须逐处记名,别悄悄搬)。</li>
* <li><b>序列化那半件已随 Phase 3 首迁落下</b>({@link #writeJson}):七支建单时都写
* 「{@code writeValueAsString} 失败即 {@code BusinessException}(不带 cause)」,形状一致、
* 只有文案不同,是最干净的公共件。Phase 2 当时落不了——{@code config/CatchSilenceRatchetTest}
* 的自述规则是「新增捕获块要么别加,要么同批减掉一处同形状」,而 Phase 2 禁止改七支
* ⇒ 减不掉只能不加(登记见蓝图 §5 Phase 2 那条);首迁这一笔删掉被迁支的本地同名件,
* 净增 ≤ 0 才成立。现成模板 {@code PublishTaskService:2118-2124} 的私有
* {@code writeJson(Object, String)} 就是带文案参数的形状。
* ⚠ task 模块内部那三份逐字同名件({@code TaskProgressSnapshotService:186}/
* {@code TaskResultItemService:405}/{@code TaskResultPayloadService:113})<b>本轮仍未归并</b>:
* 归并会把 RETHROW 那一格跌破下限(它是下限不是上限,实测首迁后正好落在 386),
* 那属于「调整门禁判据」动作,与 Phase 0 那格「三格守卫」同批做,不在迁移笔里顺手干。</li>
* <li><b>跑批</b>:服务端<b>没有属于这七支的可沉件</b>——机制早已收在
* {@link TaskFileJobService}(enqueue/claim/markSuccess/markFailed)+
* {@code TaskResultFileJobWorker}(取锁、重排、重试)+
@@ -176,8 +178,27 @@ public abstract class AbstractFileTaskPipeline<T> {
Long userId,
String deviceId,
int sourceFileCount) {
return newTaskHeader(status, taskMode, userId, deviceId, sourceFileCount, LocalDateTime.now());
}
/**
* 带<b>显式建单时刻</b>的重载——Phase 3 首迁实测出来的缺件,不是锦上添花。
*
* <p>五参版写死 {@code LocalDateTime.now()}(JVM 默认时区),那是 PriceTrack{@code :512-513}
* 的形状;ShopMatch{@code :462 → :484-485} 用的是 {@code now()}=
* {@code LocalDateTime.now(Asia/Shanghai)},并且<b>同一个时刻</b>还要喂给每一行结果行
* ({@code :503})。默认时区一旦不是业务时区(容器换 TZ、换主机、跑在 CI 里),
* 用五参版迁 ShopMatch 就把建单时间整体挪了几个小时——而这条<b>快照量不出来</b>
* (时钟刻意不进快照,否则逐字节对撞无从谈起)。所以时刻必须由被迁支自己交进来,
* 底座不替它选时区。
*/
protected final FileTaskEntity newTaskHeader(String status,
String taskMode,
Long userId,
String deviceId,
int sourceFileCount,
LocalDateTime createdAt) {
requireUserId(userId);
LocalDateTime now = LocalDateTime.now();
FileTaskEntity task = new FileTaskEntity();
task.setTaskNo(moduleType() + "-" + IdUtil.getSnowflakeNextIdStr());
task.setModuleType(moduleType());
@@ -187,8 +208,8 @@ public abstract class AbstractFileTaskPipeline<T> {
task.setCreatedBy("user:" + userId);
task.setUserId(userId);
task.setDeviceId(AdminAuthSupport.normalizeDeviceId(deviceId));
task.setCreatedAt(now);
task.setUpdatedAt(now);
task.setCreatedAt(createdAt);
task.setUpdatedAt(createdAt);
return task;
}
@@ -216,6 +237,22 @@ public abstract class AbstractFileTaskPipeline<T> {
String sourceFileUrl,
Integer rowCount,
Integer success) {
return newScopeRow(taskId, userId, sourceFilename, sourceFileUrl, rowCount, success, LocalDateTime.now());
}
/**
* 带<b>显式时刻</b>的结果行重载。理由与 {@link #newTaskHeader(String, String, Long, String, int, LocalDateTime)}
* 同一处缺件:ShopMatch{@code :503} 把建单那一枚 {@code now()} 复用给<b>每一行</b>,
* 所以同一任务的所有结果行 {@code createdAt} 逐字相同;PriceTrack{@code :538} 则逐行各取一次
* 默认时区的 {@code now()}。底座只有一种写法,就必然有一支被悄悄改掉。
*/
protected final FileResultEntity newScopeRow(Long taskId,
Long userId,
String sourceFilename,
String sourceFileUrl,
Integer rowCount,
Integer success,
LocalDateTime createdAt) {
FileResultEntity row = new FileResultEntity();
row.setTaskId(taskId);
row.setModuleType(moduleType());
@@ -224,7 +261,7 @@ public abstract class AbstractFileTaskPipeline<T> {
row.setRowCount(rowCount);
row.setSuccess(success);
row.setUserId(userId);
row.setCreatedAt(LocalDateTime.now());
row.setCreatedAt(createdAt);
return row;
}
@@ -241,6 +278,35 @@ public abstract class AbstractFileTaskPipeline<T> {
return saved;
}
/**
* 「序列化失败即拒」的公共件——Phase 2 登记为「随 Phase 3 首迁同笔落」的那一件,现在落了。
*
* <p>七支建单与状态回写时都要把对象转成 JSON,失败形状逐字相同:
* 抛 {@code BusinessException(<文案>)} 且<b>不带 cause</b>
* (ShopMatch{@code :510-512} 的文案是「task 不存在」、{@code :1301-1303} 是「序列化任务数据失败」,
* PriceTrack{@code :571-573} 与 Publish{@code :2118-2124} 同款)。
* 文案是各支自己的对外语义(其中「task 不存在」甚至是现网一句会说谎的文案——序列化失败跟任务存不存在
* 没关系,但 Python 侧按这句决定要不要重试,改它=改线协议),所以底座只收形状、文案由调用方给。
*
* <p><b>为什么 Phase 2 不能落、首迁这一笔必须落。</b>{@code CatchSilenceRatchetTest} 的自述规则是
* 「新增捕获块要么别加,要么同批减掉一处同形状」,而 Phase 2 禁止改七支 ⇒ 当时只能不加。
* 首迁这一笔把被迁支的本地同名件删掉,净增才是 ≤ 0(实测:本件 +1 处重抛,
* ShopMatch 两处 −2 处 ⇒ 整树 total 由 1003 降到 1002、RETHROW 由 387 落到下限 386,
* 两个判据一条都没放宽)。
*
* <p>捕获面是宽的一类({@code Exception})而不是只收 {@code JsonProcessingException}:被迁支原样就是宽捕获,
* 收窄会让「序列化器自己抛的 RuntimeException」从「拒单」变成「冒出去」——那是行为变更,不归本件。
*
* @param failureMessage 失败时对外的文案,逐字取被迁支现网那句
*/
protected final String writeJson(ObjectMapper objectMapper, Object value, String failureMessage) {
try {
return objectMapper.writeValueAsString(value);
} catch (Exception ex) {
throw new BusinessException(failureMessage);
}
}
// ------------------------------------------------------------------
// 段 3:回传落库——准入序列
// ------------------------------------------------------------------
@@ -84,6 +84,20 @@ public abstract class AbstractShopPayloadTaskPipeline<T> extends AbstractFileTas
return moduleType();
}
/**
* 索引建好、行循环开始之前的观察点。默认空实现。
*
* <p><b>为什么要有这一格——它是首迁实测出来的缺件,不是预留扩展点。</b>底座刻意不打日志
* (见 {@link AbstractFileTaskPipeline#admitSubmission} 那句「日志不是公共形状」),
* 而 PriceTrack{@code :617} 在现网有一句 {@code [price-track] submitResult received
* payloadShopCount=.. payloadKeys=..},位置正好卡在「准入已通过+载荷已建索引+行循环还没开始」。
* 那句话巡检按它数过回传批次,删掉就是改现网可观测行为;但除了这一格,模板里没有别的点
* 能同时满足「在拒绝路径之后、在逐行处理之前、每次回传恰好一次」。
* 所以给一个默认空的观察点:不需要它的支(ShopMatch)一个字都不用改。
*/
protected void onPayloadIndexed(FileTaskEntity task, Map<String, T> payloadByShopKey) {
}
/**
* 段 3「回传落库」整段骨架。入参 {@code incoming} 即各支请求里的 {@code shops} 列表。
*
@@ -112,6 +126,7 @@ public abstract class AbstractShopPayloadTaskPipeline<T> extends AbstractFileTas
acquireSubmissionLockOrThrow(taskId, submissionLockLogTag())) {
FileTaskEntity task = admitSubmission(taskId, terminalRejection, ownershipOperation);
Map<String, T> payloadByShopKey = indexByShopKey(incoming, rawShopNameOf);
onPayloadIndexed(task, payloadByShopKey);
boolean changed = false;
for (FileResultEntity row : listScopeRows(taskId)) {
String shopKey = row.getSourceFilename();
@@ -130,6 +130,36 @@ class AbstractFileTaskPipelineTest {
verify(fileTaskMapper, times(1)).insert(task);
}
@Test
void explicitCreatedAtIsStampedVerbatimOnBothColumns() {
// ShopMatch:462→:484-485 用的是 now()=LocalDateTime.now(Asia/Shanghai),
// 底座默认那套是 JVM 时区——首迁实测出来的缺件,时刻只能由被迁支交进来。
java.time.LocalDateTime businessNow = java.time.LocalDateTime.of(2026, 10, 18, 9, 5, 3);
FileTaskEntity task = pipeline.newTaskHeader("SCHEDULED", "PYTHON_QUEUE", 7L, "dev", 2, businessNow);
assertEquals(businessNow, task.getCreatedAt());
assertEquals(businessNow, task.getUpdatedAt(), "两列同一个时刻(现网就复用同一枚 now)");
}
@Test
void fiveArgHeaderStillFallsBackToTheWallClock() {
java.time.LocalDateTime before = java.time.LocalDateTime.now();
FileTaskEntity task = pipeline.newTaskHeader("RUNNING", "PYTHON_QUEUE", 7L, "dev", 1);
assertTrue(!task.getCreatedAt().isBefore(before), "五参版仍是默认时区的 now()(PriceTrack:512-513 那一派)");
assertTrue(!task.getCreatedAt().isAfter(before.plusMinutes(1)));
}
@Test
void scopeRowsShareOneInstantWhenTheBranchPassesIt() {
java.time.LocalDateTime businessNow = java.time.LocalDateTime.of(2026, 10, 18, 9, 5, 3);
FileResultEntity first = pipeline.newScopeRow(1L, 9L, "a", "ua", null, 0, businessNow);
FileResultEntity second = pipeline.newScopeRow(1L, 9L, "b", "ub", null, 0, businessNow);
assertEquals(businessNow, first.getCreatedAt());
assertEquals(first.getCreatedAt(), second.getCreatedAt(),
"ShopMatch:503 把建单那一枚 now 复用给每一行,逐行各取一次就不是同一件事");
}
@Test
void scopeRowKeepsNullableColumnsUntouched() {
// ShopDataCrawl/ShopMatch 建单时不写 rowCount,AppearancePatent 不写 success:
@@ -364,6 +394,44 @@ class AbstractFileTaskPipelineTest {
return Files.write(dir.resolve(name), content.getBytes(StandardCharsets.UTF_8)).toFile();
}
// ------------------------------------------------------------------
// 序列化公共件(随 Phase 3 首迁落下那一件)
// ------------------------------------------------------------------
@Test
void writeJsonHandsBackExactlyWhatTheMapperProduced() throws Exception {
com.fasterxml.jackson.databind.ObjectMapper mapper =
mock(com.fasterxml.jackson.databind.ObjectMapper.class);
when(mapper.writeValueAsString(any())).thenReturn("{\"k\":1}");
assertEquals("{\"k\":1}", pipeline.writeJson(mapper, java.util.Map.of("k", 1), "序列化任务数据失败"));
verify(mapper, times(1)).writeValueAsString(any());
}
@Test
void writeJsonReplacesFailureWithTheBranchOwnMessageAndKeepsNoCause() throws Exception {
com.fasterxml.jackson.databind.ObjectMapper mapper =
mock(com.fasterxml.jackson.databind.ObjectMapper.class);
when(mapper.writeValueAsString(any())).thenThrow(new IllegalStateException("serializer blew up"));
BusinessException ex = assertThrows(BusinessException.class,
() -> pipeline.writeJson(mapper, new Object(), "task 不存在"));
assertEquals("task 不存在", ex.getMessage(), "文案逐字取被迁支现网那句(Python 按它决定要不要重试)");
assertNull(ex.getCause(), "七支的形状都是「不带 cause」——带上就等于悄悄改日志体积");
}
@Test
void writeJsonCatchesSerializerRuntimeExceptionsNotOnlyJsonOnes() throws Exception {
// 被迁支原样是宽捕获(catch (Exception));收窄成 JsonProcessingException 会让
// 序列化器自己抛的 RuntimeException 从「拒单」变成「冒出去」,那是行为变更,不归本件。
com.fasterxml.jackson.databind.ObjectMapper mapper =
mock(com.fasterxml.jackson.databind.ObjectMapper.class);
when(mapper.writeValueAsString(any())).thenThrow(new NullPointerException("getter threw"));
assertThrows(BusinessException.class, () -> pipeline.writeJson(mapper, new Object(), "序列化任务数据失败"));
}
/** 被测底座的具体子类:只补三个钩子,公共件一律不重写(重写就等于没沉)。 */
private static class TestPipeline extends AbstractFileTaskPipeline<Map<String, Object>> {
@@ -199,6 +199,85 @@ class AbstractShopPayloadTaskPipelineTest {
assertEquals("shop-match", tagged.submissionLockLogTag());
}
// ------------------------------------------------------------------
// 载荷观察点(首迁实测缺件:PriceTrack:617 那句 received 日志的位置)
// ------------------------------------------------------------------
@Test
void payloadIsObservedOnceAfterAdmissionAndBeforeTheRowLoop() {
when(fileResultMapper.selectList(any())).thenReturn(List.of(row("shop-a"), row("shop-b")));
pipeline.submitShopScopedResults(1L, List.of("Shop-A"), String::valueOf,
AbstractFileTaskPipeline.TerminalRejection.IDEMPOTENT, null);
assertEquals(List.of("observed", "handle:shop-a", "recomputed"), pipeline.order,
"观察点必须卡在「准入已过、索引已建、行还没开始处理」——早一秒晚一秒都拿不到 PriceTrack 那句日志的语义");
assertEquals(1, pipeline.observedTasks.size(), "每次回传恰好观察一次,不是每行一次");
assertSame(pipeline.loadedTask, pipeline.observedTasks.get(0), "拿到的是准入后的那一枚任务实体");
assertEquals(List.of("shop-a"), List.copyOf(pipeline.observedIndexes.get(0).keySet()),
"拿到的是归一化之后的索引(payloadKeys 就是它)");
}
@Test
void observationIsSkippedOnEveryRejectionPath() {
FileTaskEntity finished = runningTask();
finished.setStatus("SUCCESS");
pipeline.loadedTask = finished;
assertThrows(BusinessException.class, () -> pipeline.submitShopScopedResults(1L, List.of("Shop-A"),
String::valueOf, AbstractFileTaskPipeline.TerminalRejection.IDEMPOTENT, null));
pipeline.loadedTask = runningTask();
assertThrows(BusinessException.class, () -> pipeline.submitShopScopedResults(1L, List.of(),
String::valueOf, AbstractFileTaskPipeline.TerminalRejection.IDEMPOTENT, null));
assertThrows(BusinessException.class, () -> pipeline.submitShopScopedResults(0L, List.of("Shop-A"),
String::valueOf, AbstractFileTaskPipeline.TerminalRejection.IDEMPOTENT, null));
assertTrue(pipeline.order.isEmpty(), "被拒的回传一条都不该被观察到,否则 received 日志会在拒绝路径上冒出来");
}
@Test
void branchesThatDoNotOverrideTheObserverStillRunUnchanged() {
// ShopMatch 一个字都不改就走这条路径 ⇒ 默认实现必须是真空着:既不吞调用,也不替别人打日志。
// 这里刻意造一支**没有覆写观察点**的子类:它只需要能跑完,跑不完就是默认实现写坏了。
List<String> handled = new ArrayList<>();
AbstractShopPayloadTaskPipeline<String> untouched =
new AbstractShopPayloadTaskPipeline<String>(mock(FileTaskMapper.class), fileResultMapper,
lockService, mock(TaskFileJobService.class), mock(OssStorageService.class)) {
@Override
protected String moduleType() {
return MODULE_TYPE;
}
@Override
protected FileTaskEntity loadTaskForSubmission(Long taskId) {
return runningTask();
}
@Override
protected String normalizeShopKey(String rawShopName) {
return rawShopName == null ? null : rawShopName.trim().toLowerCase();
}
@Override
protected void applyShopResultRow(Long taskId, FileTaskEntity task, FileResultEntity row,
String shopKey, String incoming) {
handled.add(shopKey);
}
@Override
protected void onShopRowsRecomputed(FileTaskEntity task, List<FileResultEntity> latestRows) {
handled.add("recomputed:" + latestRows.size());
}
};
when(fileResultMapper.selectList(any())).thenReturn(List.of(row("shop-a")));
untouched.submitShopScopedResults(1L, List.of("Shop-A"), String::valueOf,
AbstractFileTaskPipeline.TerminalRejection.IDEMPOTENT, null);
assertEquals(List.of("shop-a", "recomputed:1"), handled,
"没覆写观察点的子类,段 3 骨架的产出与覆写前逐字相同(默认实现不改变任何形状)");
}
// ------------------------------------------------------------------
// 夹具
// ------------------------------------------------------------------
@@ -227,6 +306,10 @@ class AbstractShopPayloadTaskPipelineTest {
final List<Handled> handled = new ArrayList<>();
final List<List<FileResultEntity>> recomputed = new ArrayList<>();
final List<String> normalized = new ArrayList<>();
// 观察点契约用:副作用的**先后**才是这一格的內容,光数次数证明不了它卡在准入之后、行循环之前
final List<String> order = new ArrayList<>();
final List<FileTaskEntity> observedTasks = new ArrayList<>();
final List<java.util.Map<String, String>> observedIndexes = new ArrayList<>();
RuntimeException failOnHandle;
ShopPipeline(FileTaskMapper fileTaskMapper,
@@ -253,12 +336,20 @@ class AbstractShopPayloadTaskPipelineTest {
return rawShopName == null ? null : rawShopName.trim().toLowerCase();
}
@Override
protected void onPayloadIndexed(FileTaskEntity task, java.util.Map<String, String> payloadByShopKey) {
observedTasks.add(task);
observedIndexes.add(new java.util.LinkedHashMap<>(payloadByShopKey));
order.add("observed");
}
@Override
protected void applyShopResultRow(Long taskId,
FileTaskEntity task,
FileResultEntity row,
String shopKey,
String incoming) {
order.add("handle:" + shopKey);
if (failOnHandle != null) {
throw failOnHandle;
}
@@ -267,6 +358,7 @@ class AbstractShopPayloadTaskPipelineTest {
@Override
protected void onShopRowsRecomputed(FileTaskEntity task, List<FileResultEntity> latestRows) {
order.add("recomputed");
recomputed.add(latestRows);
}
}