Peach: Local-first video metadata scraper and media manager.
perf(previews): 生成锁按目标分片,不再全局串行
longmeidao6e8a248096aa
diff --git a/scripts/test_runner.py b/scripts/test_runner.pyindex 0c5256104..48a000e83 100644--- a/scripts/test_runner.py+++ b/scripts/test_runner.py@@ -20,8 +20,8 @@ SCOPES: dict[str, tuple[str, ...]] = {"test_taste_history.py", "test_web_ui.py", "test_web_resource_sync.py","test_web_review.py"),"media": ("test_endcard.py", "test_fastapi_api.py", "test_jobs.py",- "test_media.py", "test_providers.py", "test_segments.py",- "test_streaming.py", "test_transcodes.py"),+ "test_media.py", "test_previews.py", "test_providers.py",+ "test_segments.py", "test_streaming.py", "test_transcodes.py"),"sync": ("test_sync*.py", "test_platform.py", "test_tray.py", "test_mdns.py","test_netwatch.py", "test_certs.py", "test_review_mirror.py"),"metadata": ("test_metadata*.py", "test_fc2*.py", "test_babepedia_match.py",diff --git a/src/peach/previews.py b/src/peach/previews.pyindex 3dc21027e..29426e19f 100644--- a/src/peach/previews.py+++ b/src/peach/previews.py@@ -17,7 +17,24 @@ class PreviewUnavailable(RuntimeError):pass-_GENERATE_LOCK = threading.Lock()+#: 预览生成的分片锁。原来是一把模块级全局锁:任何一个资产在生成海报,其他资产的+#: 头像和海报全得排队,而 `avatar()` 持锁要连跑 6 次 ffmpeg(每次 20 秒上限),+#: 最坏能把所有预览堵上两分钟。+#:+#: 分片而不是 per-asset 字典:字典要么只增不减(8 万个资产各留一把锁,永远不回收),+#: 要么就得处理「删锁时还有人在等」的竞态。分片是固定内存、零清理,代价只是偶尔+#: 两个不相干的目标撞进同一把锁——那只是少一次并行,不影响正确性。+_LOCK_STRIPES = 16+_GENERATE_LOCKS = tuple(threading.Lock() for _ in range(_LOCK_STRIPES))+++def _generate_lock(destination: Path) -> threading.Lock:+ """按目标文件取锁:同一个目标一定拿同一把,不同目标大概率并行。++ 同一目标同一把锁是这里的正确性要求——两个线程同时生成同一个文件,+ `os.replace` 会互相覆盖,而其中一个的临时文件可能已经被删掉了。+ """+ return _GENERATE_LOCKS[hash(str(destination)) % _LOCK_STRIPES]class PreviewService:@@ -42,7 +59,7 @@ class PreviewService:col, row = cell % 3, cell // 3self.poster_root.mkdir(parents=True, exist_ok=True)temporary = destination.with_name(destination.stem + ".tmp.jpg")- with _GENERATE_LOCK:+ with _generate_lock(destination):if destination.is_file():return destinationself._run([@@ -61,7 +78,7 @@ class PreviewService:ffmpeg = self._ffmpeg()self.avatar_root.mkdir(parents=True, exist_ok=True)candidates: list[tuple[Path, float]] = []- with _GENERATE_LOCK:+ with _generate_lock(destination):if destination.is_file():return destinationtry:diff --git a/tests/test_previews.py b/tests/test_previews.pynew file mode 100644index 000000000..3ae95d6f0--- /dev/null+++ b/tests/test_previews.py@@ -0,0 +1,127 @@+"""预览生成的并发行为。++`previews.py` 此前没有直接对应的测试(检视报告把它列为覆盖盲区)。这里先补上+最要紧的一条:生成锁的粒度。功能正确性由 `/api/entity`、`/logo` 等端点的既有+测试间接覆盖,本文件只管并发。+"""+import threading+import unittest+from pathlib import Path+from types import SimpleNamespace+from unittest import mock++from peach import previews+from peach.previews import PreviewService+from peach.repository import MediaAsset+++class GenerateLockStripingTests(unittest.TestCase):+ def test_the_same_destination_always_takes_the_same_lock(self):+ """同一个目标必须同一把锁:两个线程同时生成同一个文件,+ `os.replace` 会互相覆盖,而其中一个的临时文件可能已经被删了。"""+ target = Path("/generated/posters/42_4.jpg")+ self.assertIs(previews._generate_lock(target), previews._generate_lock(Path(target)))++ def test_different_destinations_spread_across_stripes(self):+ """分片要真的分开——全落一片就退化成原来那把全局锁。"""+ locks = {id(previews._generate_lock(Path(f"/generated/posters/{n}_4.jpg")))+ for n in range(64)}+ self.assertGreater(len(locks), 1, "64 个不同目标不该全挤在一把锁上")+ self.assertLessEqual(len(locks), previews._LOCK_STRIPES)+++class ParallelGenerationTests(unittest.TestCase):+ """两个资产的预览生成必须能同时进行。++ 改成分片锁之前这里是一把模块级全局锁:任何一个资产在生成,其他资产全排队,+ 而 `avatar()` 持锁要连跑 6 次 ffmpeg(每次 20 秒上限)。用 Barrier 断言并行——+ 一旦退回串行,第一个线程会在 Barrier 上等到超时,测试直接失败。+ """++ def setUp(self):+ import tempfile+ self.tmp = tempfile.TemporaryDirectory()+ root = Path(self.tmp.name)+ self.snapshot_root = root / "snapshots"+ self.snapshot_root.mkdir()+ self.poster_root = root / "posters"+ for asset_id in (self.LEFT, self.RIGHT):+ (self.snapshot_root / f"{asset_id}.jpg").write_bytes(b"snapshot")++ repository = SimpleNamespace(media_asset=lambda asset_id: MediaAsset(+ id=asset_id, path=None,+ snapshot_path=str(self.snapshot_root / f"{asset_id}.jpg"),+ ))+ resolver = SimpleNamespace(+ ffmpeg=lambda: SimpleNamespace(path=Path("ffmpeg")))+ self.service = PreviewService(+ repository, resolver, self.snapshot_root, self.poster_root,+ root / "avatars", root / "logos",+ )++ def tearDown(self):+ self.tmp.cleanup()++ #: 两个落在不同锁片上的资产 id;同片的话这个测试就没有意义了。+ LEFT, RIGHT = None, None++ @classmethod+ def setUpClass(cls):+ stripe = lambda n: id(previews._generate_lock(Path(f"posters/{n}_4.jpg")))+ base = 1+ for other in range(2, 200):+ if stripe(base) != stripe(other):+ cls.LEFT, cls.RIGHT = base, other+ return+ raise AssertionError("找不到落在不同锁片上的两个资产 id")++ def test_two_assets_do_not_queue_behind_each_other(self):+ both_inside = threading.Barrier(2, timeout=5)++ def fake_run(command):+ # 两个线程必须同时到达这里;串行的话先到的会等到 Barrier 超时。+ both_inside.wait()+ Path(command[-1]).write_bytes(b"jpg")++ done = []+ with mock.patch.object(PreviewService, "_run", staticmethod(fake_run)):+ workers = [+ threading.Thread(+ target=lambda a=asset: done.append(self.service.poster(a)),+ daemon=True)+ for asset in (self.LEFT, self.RIGHT)+ ]+ for worker in workers:+ worker.start()+ for worker in workers:+ worker.join(8)++ self.assertEqual(len(done), 2, "两个生成都要完成;串行时 Barrier 会先超时")+ self.assertTrue(all(path.is_file() for path in done))++ def test_the_same_asset_is_generated_only_once(self):+ """同一个资产并发请求两次,只跑一次 ffmpeg:持锁后要再看一眼文件在不在。"""+ calls = []+ entered = threading.Event()++ def fake_run(command):+ calls.append(command[-1])+ entered.set()+ Path(command[-1]).write_bytes(b"jpg")++ with mock.patch.object(PreviewService, "_run", staticmethod(fake_run)):+ workers = [+ threading.Thread(+ target=lambda: self.service.poster(self.LEFT), daemon=True)+ for _ in range(2)+ ]+ for worker in workers:+ worker.start()+ for worker in workers:+ worker.join(8)++ self.assertEqual(len(calls), 1, f"同一目标只该生成一次,实际 {len(calls)} 次")+++if __name__ == "__main__":+ unittest.main()