🐛 fix: 保留合并结果并重试上传收尾校验
* 校验读取中断时有限重试,保留合并文件供再次完成上传\n* 复用完整合并结果,损坏结果仍重建并核对完整内容\n* 固定有限轮询重试的前端版本并记录回归验证
This commit is contained in:
1 parent
8af45a70f6
commit
75dc3b1c43
4 files changed
+95
-3
No files matched your search
@@ -1 +1 @@
|
||||
fe6d81285744937e201d0452d6324533b58cc422
|
||||
d03c4e1315ba76f0ad0518200a756dd41493d5ff
|
||||
+14
@@ -245,3 +245,17 @@ WebDAV 保持 legacy 路径。新模式仍支持重试、续传、取消、配
|
||||
- 发布顺序:测试分支提交推送 → 构建并部署测试 → 验证 → 合并前端 main、后端 master
|
||||
→ 推送正式分支 → 从正式提交重新构建生产镜像 → 备份、部署及生产回归。
|
||||
正式构建使用 Dockerfile.release,固定前端提交;不再把测试镜像改标签作为生产发布。
|
||||
|
||||
## 上传收尾重试修复(2026-10-06)
|
||||
|
||||
- 公网 WebDAV 对 312.55 MiB 合并 PUT 返回 413;生产已备份配置并切换到
|
||||
同 Docker 网络内的 CloudDrive DAV,避免大文件绕公网代理。
|
||||
- 完整校验读取遇到传输中断时最多重试三次;持续失败保留合并对象,
|
||||
下次完成上传先验证已有对象,成功后发布,避免再次读取所有分片并 PUT。
|
||||
损坏或不完整的合并对象仍会重建;逐分片与完整文件 SHA-256 校验保持不变。
|
||||
- 前端完成轮询不再因 202 重置失败次数,避免 202/503 交替导致无限重试。
|
||||
- 前端 79 项测试、覆盖率检查、类型/架构检查与构建通过;
|
||||
后端 177 项单元及 HTTP DAV/FastAPI 集成测试通过,总覆盖率 62%,
|
||||
流式上传模块覆盖率 86%。Node 原生覆盖率不计 Vite SSR 动态加载模块。
|
||||
- 现有 BNApp64.apk 合并对象曾完整读取并核对本地 SHA-256 一致;
|
||||
发布与生产恢复结果另以部署验收记录为准。
|
||||
+25
-2
@@ -45,6 +45,17 @@ class WebDAVStreamMixin:
|
||||
async with client.put(url,data=data,headers={'If-None-Match':'*'}) as response:
|
||||
if response.status not in (200,201,204):raise HTTPException(503,'WebDAV 分片重传失败,请重试')
|
||||
|
||||
async def _verify_stream_file(self, client, path, info, records):
|
||||
# Cloud-backed reads can disconnect while the object is synchronizing.
|
||||
# Retry only transport failures, retaining the assembled object.
|
||||
for attempt in range(3):
|
||||
try:
|
||||
return await self._verify_offset_file(client, path, info, records)
|
||||
except (aiohttp.ClientError, asyncio.TimeoutError) as exc:
|
||||
if attempt == 2:
|
||||
raise HTTPException(503, 'WebDAV 校验读取中断,已保留合并文件,请重试完成上传') from exc
|
||||
await asyncio.sleep(attempt + 1)
|
||||
|
||||
async def finish_stream_upload(self, info, save_path):
|
||||
records=await UploadChunk.filter(upload_id=info.upload_id,completed=True).order_by('chunk_index')
|
||||
if [r.chunk_index for r in records]!=list(range(info.total_chunks)):raise ValueError('分片不完整')
|
||||
@@ -52,8 +63,20 @@ class WebDAVStreamMixin:
|
||||
staging=directory+'/assembled.bin'
|
||||
async with aiohttp.ClientSession(auth=self.auth,timeout=OFFSET_TIMEOUT) as client:
|
||||
# Recover a completed MOVE whose response/database acknowledgement was lost.
|
||||
digest=await self._verify_offset_file(client,save_path,info,records)
|
||||
digest=await self._verify_stream_file(client,save_path,info,records)
|
||||
if digest is not None:return save_path,digest
|
||||
try:
|
||||
digest=await self._verify_stream_file(client,staging,info,records)
|
||||
except ValueError:
|
||||
# An interrupted PUT may leave a partial/corrupt staging object.
|
||||
digest=None
|
||||
if digest is not None:
|
||||
move=asyncio.create_task(self._publish_offset(client,staging,save_path))
|
||||
try:await asyncio.shield(move)
|
||||
except asyncio.CancelledError:
|
||||
await move
|
||||
raise
|
||||
return save_path,digest
|
||||
async with client.delete(self._build_url(staging)) as response:
|
||||
if response.status not in (200,204,404):raise HTTPException(503,'WebDAV 合并临时文件清理失败')
|
||||
verified=0
|
||||
@@ -82,7 +105,7 @@ class WebDAVStreamMixin:
|
||||
await task
|
||||
raise
|
||||
if verified!=len(records):raise ValueError('分片校验未完成,请重新上传')
|
||||
digest=await self._verify_offset_file(client,staging,info,records)
|
||||
digest=await self._verify_stream_file(client,staging,info,records)
|
||||
if digest is None:raise ValueError('合并后的文件不存在')
|
||||
move=asyncio.create_task(self._publish_offset(client,staging,save_path))
|
||||
try:await asyncio.shield(move)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
"""Independent immutable DAV parts; one streaming assembly, never PATCH/tempfile."""
|
||||
import hashlib
|
||||
from unittest.mock import patch
|
||||
from urllib.parse import unquote, urlsplit
|
||||
from tests.test_webdav_offset_upload import WebDAVFixture
|
||||
|
||||
|
||||
@@ -106,3 +107,57 @@ class WebDAVStreamTests(WebDAVFixture):
|
||||
await clean_expired_presign_session('published-stale',now)
|
||||
self.assertEqual(self.files['/dav/old.txt'],b'keep')
|
||||
self.assertFalse(await PresignUploadSession.filter(upload_id='published-stale').exists())
|
||||
|
||||
async def test_transient_staging_read_failure_retries_without_reassembling(self):
|
||||
import aiohttp
|
||||
from core.storage import WebDAVFileStorage
|
||||
original=WebDAVFileStorage._verify_offset_file
|
||||
reads=0
|
||||
async def flaky(storage,client,path,info,records):
|
||||
nonlocal reads
|
||||
if path.endswith('assembled.bin') and unquote(urlsplit(storage._build_url(path)).path) in self.files:
|
||||
reads+=1
|
||||
if reads==1:raise aiohttp.ClientPayloadError('truncated response')
|
||||
return await original(storage,client,path,info,records)
|
||||
uid=await self.upload_all();before=len(self.calls)
|
||||
from apps.base.views import _complete_upload
|
||||
from apps.base.schemas import CompleteUploadModel
|
||||
with patch.object(WebDAVFileStorage,'_verify_offset_file',flaky):
|
||||
done=await _complete_upload(uid,CompleteUploadModel(expire_value=1,expire_style='day'),'127.0.0.1')
|
||||
self.assertEqual(done.code,200)
|
||||
self.assertEqual(reads,2)
|
||||
self.assertEqual(len([c for c in self.calls[before:] if c[0]=='PUT']),1)
|
||||
from apps.base.models import FileCodes
|
||||
record=await FileCodes.get(code=done.detail['code'])
|
||||
self.assertEqual(record.file_hash,hashlib.sha256(self.payload).hexdigest())
|
||||
|
||||
async def test_persistent_read_failure_preserves_staging_for_manual_retry(self):
|
||||
import aiohttp
|
||||
from core.storage import WebDAVFileStorage
|
||||
original=WebDAVFileStorage._verify_offset_file
|
||||
async def disconnected(storage,client,path,info,records):
|
||||
if path.endswith('assembled.bin') and unquote(urlsplit(storage._build_url(path)).path) in self.files:
|
||||
raise aiohttp.ClientPayloadError('truncated response')
|
||||
return await original(storage,client,path,info,records)
|
||||
uid=await self.upload_all()
|
||||
with patch.object(WebDAVFileStorage,'_verify_offset_file',disconnected):
|
||||
# Bypass fixture's one-second polling deadline for bounded I/O retries.
|
||||
from apps.base.views import _complete_upload
|
||||
from apps.base.schemas import CompleteUploadModel
|
||||
from fastapi import HTTPException
|
||||
with self.assertRaises(HTTPException) as raised:
|
||||
await _complete_upload(uid,CompleteUploadModel(expire_value=1,expire_style='day'),'127.0.0.1')
|
||||
self.assertEqual(raised.exception.status_code,503)
|
||||
self.assertTrue(any(k.endswith('assembled.bin') for k in self.files))
|
||||
before=len(self.calls);done=await self.complete(uid)
|
||||
self.assertEqual(done.status_code,200,done.text)
|
||||
self.assertFalse(any(c[0] in ('PUT','DELETE') and c[1].endswith('assembled.bin') for c in self.calls[before:]))
|
||||
self.assertEqual(list(self.files.values()),[self.payload])
|
||||
|
||||
async def test_corrupt_existing_staging_is_rebuilt_and_fully_verified(self):
|
||||
uid=await self.upload_all()
|
||||
part=next(k for k in self.files if k.endswith('/0.part'))
|
||||
self.files[part.rsplit('/',1)[0]+'/assembled.bin']=b'broken'
|
||||
done=await self.complete(uid)
|
||||
self.assertEqual(done.status_code,200,done.text)
|
||||
self.assertEqual(list(self.files.values()),[self.payload])
|
||||
Reference in new issue
Block a user