fix: 分块大小减至5MB + Edge端chunk_size变更自动清理 + 断点续传防护

NAS dispatcher:
- CHUNK_SIZE 10MB→5MB(~1Mbps上行带宽下可靠传输)
- chunk上传timeout (30,120)→(60,180)(增加连接和读取余量)
- _query_uploaded_chunks 返回 (set, edge_total) 元组
- expected_total != edge_total 时跳过断点续传(防止chunk_size变更导致文件损坏)

Edge api_gateway:
- upload_chunk 检测 total_chunks 变更,自动清理旧分块
- 防止不同chunk_size的旧分块与新分块混合导致assemble后文件损坏
This commit is contained in:
ericwyuan
2026-08-20 14:18:29 +08:00
parent d6054de060
commit 5915cf4be3
2 changed files with 37 additions and 10 deletions

View File

@@ -27,7 +27,7 @@ from .. import db_layer
logger = setup_logger('fam-core.dispatcher')
CHUNK_SIZE = 10 * 1024 * 1024 # 10MB per chunk (smaller = fewer timeouts)
CHUNK_SIZE = 5 * 1024 * 1024 # 5MB per chunk (reliable at ~1Mbps upload)
CHUNK_THRESHOLD = 20 * 1024 * 1024 # files > 20MB use chunked upload
MAX_CHUNK_RETRIES = 3
@@ -145,8 +145,13 @@ class Dispatcher:
logger.error(f"[task_id={task_id}] Edge 返回异常状态码: {resp.status_code}")
self._schedule_retry(task)
def _query_uploaded_chunks(self, task_id):
"""查询 Edge 端已上传分块列表"""
def _query_uploaded_chunks(self, task_id, expected_total=None):
"""查询 Edge 端已上传分块列表
返回 (uploaded_set, edge_total_chunks)。
如果 expected_total 与 edge_total 不匹配chunk_size 变更),
返回空集让 Edge 自动清理旧分块。
"""
try:
resp = requests.get(
self.chunks_query_url,
@@ -154,10 +159,17 @@ class Dispatcher:
timeout=(30, 15)
)
if resp.status_code == 200:
return set(resp.json().get('uploaded_chunks', []))
data = resp.json()
uploaded = set(data.get('uploaded_chunks', []))
edge_total = data.get('total_chunks', 0)
if expected_total and edge_total and edge_total != expected_total:
logger.warning(f"[task_id={task_id}] Edge total_chunks={edge_total} "
f"≠ expected={expected_total}chunk_size 已变更),从头上传")
return set(), edge_total
return uploaded, edge_total
except requests.RequestException:
pass
return set()
return set(), 0
def _dispatch_chunked(self, task, payload, video_path, file_size):
"""大文件分块上传 + 断点续传
@@ -171,7 +183,7 @@ class Dispatcher:
filename = os.path.basename(video_path)
# 1. 查询已上传分块(断点续传)
uploaded_set = self._query_uploaded_chunks(task_id)
uploaded_set, edge_total = self._query_uploaded_chunks(task_id, expected_total=total_chunks)
if uploaded_set:
logger.info(f"[task_id={task_id}] 断点续传: 已有 {len(uploaded_set)}/{total_chunks}")
@@ -198,7 +210,7 @@ class Dispatcher:
'filename': filename,
},
files={'chunk': (f'chunk_{idx}', io.BytesIO(chunk_data))},
timeout=(30, 120)
timeout=(60, 180)
)
if cresp.status_code == 200:
success = True
@@ -211,7 +223,7 @@ class Dispatcher:
time.sleep(5 * (attempt + 1))
if not success:
uploaded_now = self._query_uploaded_chunks(task_id)
uploaded_now, _ = self._query_uploaded_chunks(task_id)
if idx in uploaded_now:
logger.info(f"[task_id={task_id}] 分块 {idx} 虽超时但 Edge 已收到,继续下一块")
uploaded_set.add(idx)