fix(外观专利): 完成标记只打在真正落库的行上

网关少回一行、或回来的 rowToken 在分片里匹配不到时,整批结果落库不完整,
但调用方仍把全部发送行标成已检测:没落库的那行带着"已检测"的空壳 24 小时内
不再补检,用户扣了次数却在结果里永久没有结论。合并写入改为回报真正写进去的
行键,只对这些行打完成标记,其余撤销交下一轮重发(受轮次上限约束)。
This commit is contained in:
2026-09-28 03:09:49 +08:00
parent f8e642588d
commit f4f6e414f0
3 changed files with 82 additions and 32 deletions
@@ -950,18 +950,27 @@ public class AppearancePatentTaskService {
llmRows = llmClient.markRowsFailed(batchRows, message);
batchAnswered = false;
}
boolean rowsCompleted = false;
List<String> keysToRetry = new ArrayList<>(batchRowKeys);
try {
mergeLlmRowsIntoChunks(task, allRowsByBaseId, llmRows, batchRows);
// 完成标记只能在载荷真正落库之后打——这是「发版重启会不会丢行」的分界线
Set<String> landedKeys = mergeLlmRowsIntoChunks(task, allRowsByBaseId, llmRows, batchRows);
// 完成标记只能打在真正落库的行上——这是「发版重启会不会丢行」的分界线
if (batchAnswered) {
taskCacheService.completeRowInspection(task.getId(), batchRowKeys);
rowsCompleted = true;
List<String> landed = batchRowKeys.stream().filter(landedKeys::contains).toList();
if (!landed.isEmpty()) {
taskCacheService.completeRowInspection(task.getId(), landed);
}
keysToRetry.removeAll(landed);
}
} finally {
// 本批结束时处理中名额必须让出来:要么已由完成标记接管,要么撤销给下一轮重试
if (!rowsCompleted) {
taskCacheService.releaseRowInspection(task.getId(), batchRowKeys);
// 本批结束时处理中名额必须让出来:没落库的行撤销后下一轮马上能重新申领,不等 TTL
if (!keysToRetry.isEmpty()) {
taskCacheService.releaseRowInspection(task.getId(), keysToRetry);
if (batchAnswered) {
// 结果拿到了却落不进分片(少回、rowToken 被改写、分片已清):
// 打成已完成就是"扣了次数永久没结论",宁可重发一轮。
log.warn("[appearance-patent] 本批结果未全部落库,撤销已检测标记交由下一轮重发 taskId={} jobId={} batch={}/{} sent={} retrying={}",
task.getId(), job.getId(), batchIndex, batchTotal, batchRowKeys.size(), keysToRetry);
}
}
}
log.info("[appearance-patent] llm batch done taskId={} jobId={} rows={} batch={}/{} costMs={}",
@@ -1018,12 +1027,12 @@ public class AppearancePatentTaskService {
return retryable;
}
private void mergeLlmRowsIntoChunks(FileTaskEntity task,
Map<String, List<AppearancePatentParsedRowVo>> allRowsByBaseId,
List<AppearancePatentResultRowDto> llmRows,
List<AppearancePatentResultRowDto> batchRows) {
private Set<String> mergeLlmRowsIntoChunks(FileTaskEntity task,
Map<String, List<AppearancePatentParsedRowVo>> allRowsByBaseId,
List<AppearancePatentResultRowDto> llmRows,
List<AppearancePatentResultRowDto> batchRows) {
if (llmRows == null || llmRows.isEmpty()) {
return;
return Set.of();
}
Map<String, AppearancePatentResultRowDto> deduped = new LinkedHashMap<>();
for (int i = 0; i < llmRows.size(); i++) {
@@ -1039,7 +1048,7 @@ public class AppearancePatentTaskService {
deduped.put(key, row);
}
}
mergeLlmRowsIntoSubmittedChunks(task, new ArrayList<>(deduped.values()), allRowsByBaseId);
return mergeLlmRowsIntoSubmittedChunks(task, new ArrayList<>(deduped.values()), allRowsByBaseId);
}
/**
@@ -1361,9 +1370,11 @@ public class AppearancePatentTaskService {
}
private void mergeChunkPayload(Long taskId, String scopeHash, Integer chunkIndex, List<AppearancePatentResultRowDto> rows) {
/** 返回真正写进这一片载荷的行键:一片也没写成时返回空集,调用方据此决定哪些行还能重发。 */
private List<String> mergeChunkPayload(Long taskId, String scopeHash, Integer chunkIndex,
List<AppearancePatentResultRowDto> rows) {
if (rows == null || rows.isEmpty()) {
return;
return List.of();
}
String conflictDetail = "未发生冲突";
for (int attempt = 1; attempt <= CHUNK_PAYLOAD_MERGE_RETRY_LIMIT; attempt++) {
@@ -1374,7 +1385,7 @@ public class AppearancePatentTaskService {
.eq(TaskChunkEntity::getChunkIndex, chunkIndex)
.last("limit 1"));
if (chunk == null) {
return;
return List.of();
}
Map<String, AppearancePatentResultRowDto> persistedRows = readChunkRows(chunk);
for (AppearancePatentResultRowDto row : rows) {
@@ -1405,7 +1416,7 @@ public class AppearancePatentTaskService {
log.debug("[appearance-patent] chunk payload replaced taskId={} scopeHash={} chunk={} oldPayload={} newPayload={} attempt={}",
taskId, scopeHash, chunkIndex, oldPayload, storedPayload, attempt);
transientPayloadStorageService.deleteReplacedPayloadIfNeeded(oldPayload, storedPayload);
return;
return rows.stream().map(this::rowKey).filter(key -> !key.isBlank()).toList();
}
// CAS 冲突:读回行上的当前哈希,否则只有一句 conflict 无从判断是谁改的(线上 28459 至今未能定位)
String currentHash = currentPayloadHash(chunk.getId());
@@ -1859,23 +1870,24 @@ public class AppearancePatentTaskService {
saveFileBuildProgress(task, job, totalProgressUnits, totalProgressUnits, "结果文件已生成");
}
private void mergeLlmRowsIntoSubmittedChunks(FileTaskEntity task,
List<AppearancePatentResultRowDto> llmRows,
Map<String, List<AppearancePatentParsedRowVo>> allRowsByBaseId) {
mergeLlmRowsIntoSubmittedChunks(task, llmRows, allRowsByBaseId, null, null);
private Set<String> mergeLlmRowsIntoSubmittedChunks(FileTaskEntity task,
List<AppearancePatentResultRowDto> llmRows,
Map<String, List<AppearancePatentParsedRowVo>> allRowsByBaseId) {
return mergeLlmRowsIntoSubmittedChunks(task, llmRows, allRowsByBaseId, null, null);
}
private void mergeLlmRowsIntoSubmittedChunks(FileTaskEntity task,
List<AppearancePatentResultRowDto> llmRows,
Map<String, List<AppearancePatentParsedRowVo>> allRowsByBaseId,
String fallbackScopeHash,
Integer fallbackChunkIndex) {
/** 返回真正落库的行键;没匹配到分片行的键不会出现在里面,调用方据此决定要不要撤销已检测标记。 */
private Set<String> mergeLlmRowsIntoSubmittedChunks(FileTaskEntity task,
List<AppearancePatentResultRowDto> llmRows,
Map<String, List<AppearancePatentParsedRowVo>> allRowsByBaseId,
String fallbackScopeHash,
Integer fallbackChunkIndex) {
if (task == null || llmRows == null || llmRows.isEmpty()) {
return;
return Set.of();
}
List<TaskChunkEntity> chunks = loadSubmittedChunks(task.getId());
if (chunks.isEmpty()) {
return;
return Set.of();
}
Map<String, Map<String, AppearancePatentResultRowDto>> rowsByChunk = new LinkedHashMap<>();
Map<String, TaskChunkEntity> chunkByKey = new LinkedHashMap<>();
@@ -1913,13 +1925,16 @@ public class AppearancePatentTaskService {
}
}
}
Set<String> landedKeys = new LinkedHashSet<>();
for (Map.Entry<String, Map<String, AppearancePatentResultRowDto>> entry : mergeRowsByChunk.entrySet()) {
TaskChunkEntity chunk = chunkByKey.get(entry.getKey());
if (chunk == null || entry.getValue().isEmpty()) {
continue;
}
mergeChunkPayload(task.getId(), chunk.getScopeHash(), chunk.getChunkIndex(), new ArrayList<>(entry.getValue().values()));
landedKeys.addAll(mergeChunkPayload(task.getId(), chunk.getScopeHash(), chunk.getChunkIndex(),
new ArrayList<>(entry.getValue().values())));
}
return landedKeys;
}
private String chunkStorageKey(String scopeHash, Integer chunkIndex) {