fix(phase-5.1): 回收/确认幂等与两前端分片重试按 CR 消项加固

- 分片回收与 savePart/complete 共用会话锁并锁内复查 mtime
- 版本/教程 confirm 按 version 或 fileName+version 幂等返回既有行
- 证据登记失败补偿删除已上传对象;标签截断到列宽 32
- 用户端分片持久化 upload_id、校验包体失败、失败不再 DELETE
- 后管逐片重试 2 次、放弃时删除会话;catch 台账抬到 1057/190
This commit is contained in:
2026-10-31 04:26:58 +08:00
parent b13d795731
commit 2c2cb23943
7 changed files with 187 additions and 44 deletions
@@ -70,7 +70,20 @@ public class TaskEvidenceService {
entity.setContentType(stored.contentType());
entity.setSha256(stored.sha256());
entity.setCreatedAt(LocalDateTime.now());
mapper.insert(entity);
try {
mapper.insert(entity);
} catch (RuntimeException ex) {
// 对象已写进桶、登记行没落:不回收就是永久孤儿(保留期只扫登记行)。
log.warn("[task-evidence] 登记行写入失败,回收对象 taskId={} key={} reason={}",
normalizedTaskId, stored.objectKey(), ex.getMessage());
try {
storage.delete(stored.objectKey());
} catch (RuntimeException cleanup) {
log.warn("[task-evidence] 回收孤儿对象失败 key={} reason={}",
stored.objectKey(), cleanup.getMessage());
}
throw new BusinessException("证据件登记失败,请稍后重试");
}
deleteTempQuietly(file);
Map<String, Object> result = new LinkedHashMap<>();
result.put("id", entity.getId());
@@ -194,7 +207,8 @@ public class TaskEvidenceService {
if (text.isEmpty()) {
return fallback;
}
return text.length() > 64 ? text.substring(0, 64) : text;
// 列宽是 VARCHAR(32),超长标签在这里截断而不是让 insert 抛数据截断错。
return text.length() > 32 ? text.substring(0, 32) : text;
}
private void deleteTempQuietly(File file) {
@@ -298,13 +298,25 @@ public class ChunkedUploadService {
long cutoff = System.currentTimeMillis() - Math.max(1, staleHours) * 3600_000L;
try (Stream<Path> children = Files.list(root)) {
children.forEach(child -> {
try {
if (Files.getLastModifiedTime(child).toMillis() < cutoff) {
deleteQuietly(child);
log.info("[chunk-upload] 清理过期分片会话/残留文件 path={}", child.getFileName());
String name = child.getFileName().toString();
Runnable sweep = () -> {
try {
// 复查一次 mtime:等锁期间刚被 savePart 刷新的会话不能删。
if (Files.getLastModifiedTime(child).toMillis() < cutoff) {
deleteQuietly(child);
log.info("[chunk-upload] 清理过期分片会话/残留文件 path={}", child.getFileName());
}
} catch (IOException ex) {
log.warn("[chunk-upload] 条目时间戳读不出,本轮跳过该条 path={}", child.getFileName());
}
} catch (IOException ex) {
log.warn("[chunk-upload] 条目时间戳读不出,本轮跳过该条 path={}", child.getFileName());
};
if (UPLOAD_ID_PATTERN.matcher(name).matches()) {
// 与 savePart/complete 共用会话锁:在飞上传不会被整点回收腰斩。
synchronized (lockOf(name)) {
sweep.run();
}
} else {
sweep.run();
}
});
} catch (IOException ex) {
@@ -272,6 +272,15 @@ public class SoftwareVersionService {
if (size > MAX_UPLOAD_BYTES) {
throw new BusinessException("文件超过允许的大小限制");
}
// 同一 version 已登记(超时重试/双击):直接返回既有行,临时件删掉即可。
SoftwareVersionEntity existing = softwareVersionMapper.selectOne(
new LambdaQueryWrapper<SoftwareVersionEntity>()
.eq(SoftwareVersionEntity::getVersion, normalizedVersion)
.last("LIMIT 1"));
if (existing != null) {
deleteTempQuietly(source);
return toItemMap(existing);
}
String objectKey = softwareVersionObjectKey(normalizedVersion);
String fileUrl = ossStorageService.uploadSoftwareVersionPackage(source, objectKey);
SoftwareVersionEntity entity = new SoftwareVersionEntity();
@@ -148,6 +148,18 @@ public class TutorialPackageService {
if (size > MAX_UPLOAD_BYTES) {
throw new BusinessException("文件超过允许的大小限制");
}
// 同一 fileName+version 已登记(超时重试/双击):返回既有行,不重复搬桶。
TutorialPackageEntity existing = tutorialPackageMapper.selectOne(
new LambdaQueryWrapper<TutorialPackageEntity>()
.eq(TutorialPackageEntity::getFileName, normalizedName)
.eq(TutorialPackageEntity::getVersion, normalizedVersion)
.last("LIMIT 1"));
if (existing != null) {
if (source.exists() && !source.delete()) {
log.warn("[tutorial] 清理重复提交的临时件失败 path={}", source.getAbsolutePath());
}
return toItemMap(existing);
}
String generatedKey = OBJECT_KEY_PREFIX + OBJECT_NAME_FORMATTER.format(LocalDateTime.now())
+ "-" + safeFileName(normalizedName);
ossStorageService.uploadTutorialPackage(source, generatedKey);