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:
@@ -4,6 +4,9 @@ import { http, unwrap } from '@/api/http'
|
||||
export const CHUNK_UPLOAD_THRESHOLD = 8 * 1024 * 1024
|
||||
export const CHUNK_UPLOAD_PART_SIZE = 8 * 1024 * 1024
|
||||
|
||||
/** 每片重试次数(不含首次):单片 502/超时不该让 512MB 从 0 再来。 */
|
||||
const PART_RETRY_LIMIT = 2
|
||||
|
||||
function partUrl(uploadId: string, index: number): string {
|
||||
return `/api/files/chunk/${encodeURIComponent(uploadId)}/part/${index}`
|
||||
}
|
||||
@@ -37,18 +40,33 @@ export async function uploadFileInChunks(
|
||||
}
|
||||
const blob = file.slice(index * CHUNK_UPLOAD_PART_SIZE,
|
||||
Math.min(file.size, (index + 1) * CHUNK_UPLOAD_PART_SIZE))
|
||||
const form = new FormData()
|
||||
form.append('file', blob, `part-${index}`)
|
||||
await http.post(partUrl(uploadId, index), form, {
|
||||
headers: { 'Content-Type': 'multipart/form-data' },
|
||||
timeout: 120_000 + Math.ceil(file.size / (1024 * 1024)) * 15_000,
|
||||
onUploadProgress: (event) => {
|
||||
if (!onProgress) return
|
||||
const perPart = Math.round(((index + (event.loaded || 0) / Math.max(blob.size, 1)) / totalParts) * 100)
|
||||
onProgress(Math.min(perPart, 99))
|
||||
},
|
||||
})
|
||||
arrived.add(index)
|
||||
let lastError: unknown = null
|
||||
for (let attempt = 0; attempt <= PART_RETRY_LIMIT; attempt += 1) {
|
||||
const form = new FormData()
|
||||
form.append('file', blob, `part-${index}`)
|
||||
try {
|
||||
const partResponse = await http.post(partUrl(uploadId, index), form, {
|
||||
headers: { 'Content-Type': 'multipart/form-data' },
|
||||
timeout: 120_000 + Math.ceil(file.size / (1024 * 1024)) * 15_000,
|
||||
onUploadProgress: (event) => {
|
||||
if (!onProgress) return
|
||||
const perPart = Math.round(((index + (event.loaded || 0) / Math.max(blob.size, 1)) / totalParts) * 100)
|
||||
onProgress(Math.min(perPart, 99))
|
||||
},
|
||||
})
|
||||
unwrap(partResponse.data)
|
||||
arrived.add(index)
|
||||
lastError = null
|
||||
break
|
||||
} catch (error) {
|
||||
lastError = error
|
||||
}
|
||||
}
|
||||
if (lastError) {
|
||||
// 后管上传是一次性的:放弃时把会话连同临时片一起清掉,不留 24h 残盘。
|
||||
await http.delete(`/api/files/chunk/${encodeURIComponent(uploadId)}`).catch(() => undefined)
|
||||
throw lastError instanceof Error ? lastError : new Error('分片上传失败')
|
||||
}
|
||||
}
|
||||
|
||||
const completeResponse = await http.post(
|
||||
|
||||
+16
-2
@@ -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) {
|
||||
|
||||
+18
-6
@@ -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) {
|
||||
|
||||
+9
@@ -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();
|
||||
|
||||
+12
@@ -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);
|
||||
|
||||
@@ -61,7 +61,8 @@ class CatchSilenceRatchetTest {
|
||||
// 逐处记名见 STRICT_TOTAL 上方注释;EMPTY 一格没涨)。
|
||||
// 10-30 用户端代理白名单(极客/青果)抬 591 -> 598(7 处逐一记名见 STRICT_TOTAL 上方注释;
|
||||
// 实测量到 599,多出的 1 格来自已提交的 79b04afd(AdminApiGuardFilter 编码绕过 catch),见同处说明)。
|
||||
static final int GIVEN_NOT_RETHROW = 599;
|
||||
// 10-31 Phase 5.1:证据登记失败回收对象一枚(内层回收失败只 WARN),599 -> 600。
|
||||
static final int GIVEN_NOT_RETHROW = 600;
|
||||
static final int GIVEN_LOG_ONLY = 249;
|
||||
static final int GIVEN_EMPTY = 56;
|
||||
|
||||
@@ -273,8 +274,13 @@ class CatchSilenceRatchetTest {
|
||||
* </ul>
|
||||
* 同批实测 total 1055、rethrow 400、notRethrow 599(未涨,register 是重抛形状)、
|
||||
* logOnly 189、empty 56。
|
||||
*
|
||||
* <p>10-31 Phase 5.1 CR 消项再抬 1055 -> 1057,两枚逐处记名(都在
|
||||
* {@code TaskEvidenceService.register} 的登记行写入失败兜底):外层 catch 回收已上传对象
|
||||
* 后转业务错误(RETHROW+rethrow 401);内层 catch 是对象回收本身失败只 WARN
|
||||
* (LOG_ONLY+logOnly 190)。两处误差方向都是「不让孤儿对象永久占桶」。
|
||||
*/
|
||||
static final int STRICT_TOTAL = 1055;
|
||||
static final int STRICT_TOTAL = 1057;
|
||||
/**
|
||||
* 重抛型 catch 的**下限**(不是上限):本轮把两处吞异常改成重抛之后实测 388。
|
||||
* 这一格只能涨不能跌——它是"异常会往外走"的总量,把它钉成"只许减"就等于拦着别人修吞异常。
|
||||
@@ -397,7 +403,8 @@ class CatchSilenceRatchetTest {
|
||||
* 复测整树 599:多出的 1 格来自已提交的 79b04afd {@code AdminApiGuardFilter.decodePath}
|
||||
* (畸形编码保持原样交给容器 400),非本批新增,说明见 STRICT_TOTAL 上方。
|
||||
*/
|
||||
static final int STRICT_NOT_RETHROW = 599;
|
||||
// 10-31 Phase 5.1:同 GIVEN_NOT_RETHROW,证据登记补偿回收失败只 WARN,599 -> 600。
|
||||
static final int STRICT_NOT_RETHROW = 600;
|
||||
/**
|
||||
* 10-17 抬 172 -> 173,出处就是上面点名过的 {@code TaskHeartbeatLivenessService.stamp()}:
|
||||
* 块体只有「取到 key 名 + log.warn」一句,写心跳证据失败时既不重试也不抛出——
|
||||
@@ -428,8 +435,10 @@ class CatchSilenceRatchetTest {
|
||||
* {@code WorkbenchClientPackageService.abortChunkedPackage} 的 INFO 与 WARN 两枚。
|
||||
* 10-31 抬 188 -> 189:{@code TaskEvidenceService.purgeExpired} 的逐行保留期回收出口,
|
||||
* 块体只有一句带 {@code [task-evidence]} 锚点的 WARN(id+reason),坏行跳过、其余照删。
|
||||
* 同日 Phase 5.1 再抬 189 -> 190:登记行落库失败后的对象回收失败只 WARN(同上锚点),
|
||||
* 主错误仍由外层转业务错误;不吞主错误。
|
||||
*/
|
||||
static final int STRICT_LOG_ONLY = 189; // 严格口径:块内每条顶层语句都是 log 调用才算
|
||||
static final int STRICT_LOG_ONLY = 190; // 严格口径:块内每条顶层语句都是 log 调用才算
|
||||
static final int STRICT_EMPTY = 56;
|
||||
|
||||
/**
|
||||
|
||||
@@ -44,6 +44,60 @@ function bodyOf<T>(response: unknown): JavaApiResponse<T> {
|
||||
return response as JavaApiResponse<T>
|
||||
}
|
||||
|
||||
/** 业务失败默认也是 HTTP 200:只看状态码会把被拒的片当成功。 */
|
||||
function ensureOk<T>(body: JavaApiResponse<T>): JavaApiResponse<T> {
|
||||
if (body && body.success === false) {
|
||||
const message = (body as { message?: string }).message
|
||||
throw new Error(message || '上传失败,请重试')
|
||||
}
|
||||
return body
|
||||
}
|
||||
|
||||
/** 分片会话跨重试/刷新续用:同一文件身份复用 upload_id,服务端没回收就从已达片号继续。 */
|
||||
interface StoredChunkSession {
|
||||
uploadId: string;
|
||||
size: number;
|
||||
lastModified: number;
|
||||
}
|
||||
|
||||
const CHUNK_SESSION_PREFIX = 'shufu:chunk:'
|
||||
|
||||
function sessionKeyOf(file: File, options: UploadTempFileOptions): string {
|
||||
return `${CHUNK_SESSION_PREFIX}${file.name}:${file.size}:${file.lastModified}:${options.relativePath || ''}`
|
||||
}
|
||||
|
||||
function readStoredSession(key: string, file: File): string {
|
||||
if (typeof localStorage === 'undefined') return ''
|
||||
try {
|
||||
const raw = localStorage.getItem(key)
|
||||
if (!raw) return ''
|
||||
const parsed = JSON.parse(raw) as StoredChunkSession
|
||||
return parsed.size === file.size && parsed.lastModified === file.lastModified ? parsed.uploadId : ''
|
||||
} catch {
|
||||
return ''
|
||||
}
|
||||
}
|
||||
|
||||
function writeStoredSession(key: string, file: File, uploadId: string): void {
|
||||
if (typeof localStorage === 'undefined') return
|
||||
try {
|
||||
localStorage.setItem(key, JSON.stringify({
|
||||
uploadId, size: file.size, lastModified: file.lastModified,
|
||||
} satisfies StoredChunkSession))
|
||||
} catch {
|
||||
// 存储不可用(隐私模式/配额满)只影响跨刷新续传,不影响本次上传
|
||||
}
|
||||
}
|
||||
|
||||
function clearStoredSession(key: string): void {
|
||||
if (typeof localStorage === 'undefined') return
|
||||
try {
|
||||
localStorage.removeItem(key)
|
||||
} catch {
|
||||
// 同上:清不掉只会让下次多问一次 status
|
||||
}
|
||||
}
|
||||
|
||||
/** 单次 multipart 上传(小文件短链路)。超时按体积给,不再固定 120s。 */
|
||||
async function singleUpload(
|
||||
file: File,
|
||||
@@ -90,23 +144,34 @@ async function chunkedUpload(
|
||||
onProgress?: (percent: number) => void,
|
||||
): Promise<JavaApiResponse<UploadFileVo>> {
|
||||
const totalParts = Math.max(1, Math.ceil(file.size / UPLOAD_PART_SIZE))
|
||||
const init = bodyOf<{ upload_id?: string }>(await http.post(
|
||||
`${JAVA_API_PREFIX}/files/chunk/init`,
|
||||
null,
|
||||
{ params: { fileName: file.name, totalParts, totalBytes: file.size } },
|
||||
))
|
||||
const uploadId = String(init.data?.upload_id ?? '')
|
||||
if (!uploadId) {
|
||||
throw new Error('后端未返回分片会话,请重试')
|
||||
const sessionKey = sessionKeyOf(file, options)
|
||||
let uploadId = readStoredSession(sessionKey, file)
|
||||
let done = new Set<number>()
|
||||
if (uploadId) {
|
||||
try {
|
||||
const status = ensureOk(bodyOf<{ uploaded_parts?: unknown[] }>(await http.get(
|
||||
`${JAVA_API_PREFIX}/files/chunk/${encodeURIComponent(uploadId)}`,
|
||||
)))
|
||||
const arrived = status.data?.uploaded_parts
|
||||
done = new Set<number>(Array.isArray(arrived) ? arrived.map((index) => Number(index)) : [])
|
||||
} catch {
|
||||
// 会话已被服务端回收/过期:只能清掉重开,否则这个件每次都在同一个死会话上失败。
|
||||
clearStoredSession(sessionKey)
|
||||
uploadId = ''
|
||||
}
|
||||
}
|
||||
if (!uploadId) {
|
||||
const init = ensureOk(bodyOf<{ upload_id?: string }>(await http.post(
|
||||
`${JAVA_API_PREFIX}/files/chunk/init`,
|
||||
null,
|
||||
{ params: { fileName: file.name, totalParts, totalBytes: file.size } },
|
||||
)))
|
||||
uploadId = String(init.data?.upload_id ?? '')
|
||||
if (!uploadId) {
|
||||
throw new Error('后端未返回分片会话,请重试')
|
||||
}
|
||||
writeStoredSession(sessionKey, file, uploadId)
|
||||
}
|
||||
|
||||
// 开传前先问到已达片号:上一次断掉的会话可以跳过已传片(后端保留 24h)。
|
||||
// 这一问失败不再静默按"全缺"重传:断网/5xx 当场说清楚,别闷头把整包从 0 再传一遍。
|
||||
const status = bodyOf<{ uploaded_parts?: unknown[] }>(await http.get(
|
||||
`${JAVA_API_PREFIX}/files/chunk/${encodeURIComponent(uploadId)}`,
|
||||
))
|
||||
const arrived = status.data?.uploaded_parts
|
||||
const done = new Set<number>(Array.isArray(arrived) ? arrived.map((index) => Number(index)) : [])
|
||||
|
||||
const report = (partialBytes: number) => {
|
||||
if (!onProgress) return
|
||||
@@ -125,11 +190,12 @@ async function chunkedUpload(
|
||||
const form = new FormData()
|
||||
form.append('file', blob, `part-${index}`)
|
||||
try {
|
||||
await http.post(uploadPartUrl(uploadId, index), form, {
|
||||
const partBody = bodyOf(await http.post(uploadPartUrl(uploadId, index), form, {
|
||||
headers: { 'Content-Type': 'multipart/form-data' },
|
||||
timeout: 300_000,
|
||||
onUploadProgress: (event) => report(Math.min(event.loaded || 0, blob.size)),
|
||||
})
|
||||
}))
|
||||
ensureOk(partBody)
|
||||
done.add(index)
|
||||
lastError = null
|
||||
break
|
||||
@@ -138,7 +204,8 @@ async function chunkedUpload(
|
||||
}
|
||||
}
|
||||
if (lastError) {
|
||||
await http.delete(`${JAVA_API_PREFIX}/files/chunk/${encodeURIComponent(uploadId)}`).catch(() => undefined)
|
||||
// 不 DELETE:会话连同已达片号留在服务端,下一次(含刷新后)从断点续传。
|
||||
writeStoredSession(sessionKey, file, uploadId)
|
||||
throw lastError instanceof Error ? lastError : new Error('分片上传失败')
|
||||
}
|
||||
}
|
||||
@@ -152,8 +219,10 @@ async function chunkedUpload(
|
||||
null,
|
||||
{ params, timeout: 600_000 },
|
||||
)
|
||||
const completed = ensureOk(bodyOf<UploadFileVo>(response))
|
||||
clearStoredSession(sessionKey)
|
||||
if (onProgress) onProgress(100)
|
||||
return bodyOf<UploadFileVo>(response)
|
||||
return completed
|
||||
}
|
||||
|
||||
export async function uploadTempFileToJava(
|
||||
|
||||
Reference in New Issue
Block a user