Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

perf(test): 全量锁按到达先后排队

longmeidao01e6181dfa88
diff --git a/docs/TESTING.md b/docs/TESTING.md
index b0d05edf9..dd5b4d12b 100644
--- a/docs/TESTING.md
+++ b/docs/TESTING.md
@@ -146,7 +146,9 @@ macOS 按影响域验证:
主检出只按 ADR-0091 只读检查依赖;缺包或检查失败时以 `3` 退出,依赖变更由重启流程执行。找不到 uv、锁文件过期或同步失败也会停止测试。前端依赖与构建仍按上文安装。完成安装和编辑后再启动最终验证;运行中发生改动时,完整批次按下述规则保留未受影响的文件结果。
本机全量验证与 `integrate` 共用 `full-suite.lock`,从取得代码和环境快照一直持有到记录写入完成。
-锁忙时打印持有者并等待,最长 30 分钟;超时退出且不执行测试或合并。集成事务本身仍由
+锁忙时按到达先后排队:等待者在锁旁的 `full-suite.lock.queue/` 放一张以纳秒时刻开头的票,
+前面没有活票时才去抢锁,拿到即撤票;提示里打印持有者与前面排队的个数。票主进程退出后,
+它的票由后来者清掉。最长等 30 分钟;超时撤票退出,不执行测试或合并。集成事务本身仍由
`integration.lock` 串行保护。部分域验证与 CI 分片不签发独立的本机全量记录。
锁旁的持有者信息用独立文件锁保护读写和清理;主锁仍负责验证与集成互斥。等锁线程关闭记录文件后,持有者才删除记录,避免 Windows 文件共享冲突。
diff --git a/scripts/test_evidence.py b/scripts/test_evidence.py
index 5cc61fd8d..ac9f35985 100644
--- a/scripts/test_evidence.py
+++ b/scripts/test_evidence.py
@@ -12,6 +12,7 @@ import stat
import subprocess
import sys
import time
+import uuid
from contextlib import contextmanager
from functools import lru_cache
from pathlib import Path
@@ -297,6 +298,56 @@ def holder_path(lock_path: Path) -> Path:
return lock_path.with_name(lock_path.name + ".holder.json")
+QUEUE_POLL_SECONDS = 0.5
+#: 没有 psutil 判不了票主是否还活着时,票的最长寿命。
+QUEUE_TICKET_SECONDS = 7200
+
+
+def queue_path(lock_path: Path) -> Path:
+ return lock_path.with_name(lock_path.name + ".queue")
+
+
+def _process_started(pid: int) -> float | None:
+ try:
+ import psutil
+ except ImportError:
+ return None
+ try:
+ return psutil.Process(pid).create_time()
+ except psutil.Error:
+ return -1.0
+
+
+def _ticket_alive(path: Path, now: float) -> bool:
+ """票主进程还在就算活票;同一 pid 被别的进程复用时创建时刻对不上,也算死票。"""
+ try:
+ ticket = json.loads(path.read_text(encoding="utf-8"))
+ pid, created = int(ticket["pid"]), float(ticket["created"])
+ except FileNotFoundError:
+ return False
+ except (OSError, ValueError, KeyError, TypeError):
+ # 写到一半或正被别的进程读写:先当它还在,下一轮再看。
+ return True
+ started = _process_started(pid)
+ if started is None:
+ return now - created < QUEUE_TICKET_SECONDS
+ recorded = ticket.get("process_started")
+ return started >= 0 and (not isinstance(recorded, (int, float)) or abs(started - recorded) < 1)
+
+
+def tickets_ahead(lock_path: Path, mine: str) -> list[Path]:
+ """排在 `mine` 前面的活票,按到达先后;死票顺手清掉。"""
+ now, ahead = time.time(), []
+ for path in sorted(queue_path(lock_path).glob("*.json")):
+ if path.name >= mine:
+ break
+ if _ticket_alive(path, now):
+ ahead.append(path)
+ else:
+ path.unlink(missing_ok=True)
+ return ahead
+
+
@contextmanager
def held(lock_path: Path, *, wait_seconds: float = 0, **note: object):
"""拿锁,并在旁边留下「谁在持有」的记录,让等锁的人知道在等什么。
@@ -304,17 +355,43 @@ def held(lock_path: Path, *, wait_seconds: float = 0, **note: object):
互斥只靠 filelock:锁是操作系统层面的句柄,只能被活着的进程持有,进程退出就释放。
记录是旁证,不参与互斥。持有者正常退出就删掉它;异常退出留下的旧记录会被下一位
持有者进门时覆盖,而等锁那一侧只在锁真的被占着时才去读,所以读到的总是当前持有者。
+
+ 等锁按到达先后排队:每个等待者在 `<锁>.queue/` 里放一张以纳秒时刻开头的票,只有前面
+ 没有活票时才去抢锁,拿到就撤票。没有排队时,锁一空出来谁轮询得快谁拿到,几个会话轮番
+ 抢的结果是有人连等几轮、满 30 分钟退出。票主进程退出后它的票由后面的人清掉。
"""
lock_path.parent.mkdir(parents=True, exist_ok=True)
lock = FileLock(lock_path)
+ queue = queue_path(lock_path)
+ queue.mkdir(parents=True, exist_ok=True)
+ ticket = queue / f"{time.time_ns():020d}-{os.getpid()}-{uuid.uuid4().hex[:8]}.json"
+ pending = ticket.with_suffix(".tmp")
+ pending.write_text(json.dumps({"pid": os.getpid(), "created": time.time(),
+ "process_started": _process_started(os.getpid()),
+ "started_at": time.strftime("%Y-%m-%d %H:%M:%S"), **note},
+ ensure_ascii=False, default=str), encoding="utf-8")
+ pending.replace(ticket)
+ deadline = time.monotonic() + max(wait_seconds, 0)
+ announced = None
try:
- lock.acquire(timeout=0)
- except Timeout:
- if wait_seconds <= 0:
- raise
- print(f"等待 {lock_path.name}({describe_holder(lock_path)}),最长 {wait_seconds:g} 秒。",
- file=sys.stderr, flush=True)
- lock.acquire(timeout=wait_seconds)
+ while True:
+ ahead = tickets_ahead(lock_path, ticket.name)
+ if not ahead:
+ try:
+ lock.acquire(timeout=0)
+ break
+ except Timeout:
+ pass
+ if wait_seconds <= 0 or time.monotonic() >= deadline:
+ raise Timeout(str(lock_path))
+ if announced != len(ahead):
+ announced = len(ahead)
+ place = f";前面还有 {announced} 个在排队" if announced else ""
+ print(f"等待 {lock_path.name}({describe_holder(lock_path)}{place}),最长 {wait_seconds:g} 秒。",
+ file=sys.stderr, flush=True)
+ time.sleep(min(QUEUE_POLL_SECONDS, max(0.0, deadline - time.monotonic())))
+ finally:
+ ticket.unlink(missing_ok=True)
try:
record = holder_path(lock_path)
record_lock = FileLock(record.with_suffix(".lock"), timeout=5)
diff --git a/tests/test_test_evidence.py b/tests/test_test_evidence.py
index 3305e7945..285b12f13 100644
--- a/tests/test_test_evidence.py
+++ b/tests/test_test_evidence.py
@@ -375,6 +375,56 @@ class VerificationTests(unittest.TestCase):
self.assertFalse(note.exists())
+ def test_waiters_get_the_lock_in_arrival_order_and_see_their_place(self):
+ lock = evidence.evidence_dir(self.repo) / "full-suite.lock"
+ queue = evidence.queue_path(lock)
+ order, errors = [], io.StringIO()
+
+ def wait(name):
+ with evidence.held(lock, wait_seconds=20, scope=name):
+ order.append(name)
+ time.sleep(0.3)
+
+ def queued(count):
+ deadline = time.monotonic() + 10
+ while time.monotonic() < deadline:
+ if len(list(queue.glob("*.json"))) == count:
+ return True
+ time.sleep(0.05)
+ return False
+
+ with redirect_stderr(errors), ThreadPoolExecutor(max_workers=2) as pool:
+ with evidence.held(lock, scope="holder"):
+ first = pool.submit(wait, "first")
+ self.assertTrue(queued(1))
+ second = pool.submit(wait, "second")
+ self.assertTrue(queued(2))
+ first.result(timeout=20)
+ second.result(timeout=20)
+ self.assertEqual(order, ["first", "second"])
+ self.assertIn("前面还有 1 个在排队", errors.getvalue())
+ self.assertEqual(list(queue.glob("*.json")), [], "拿到锁或放弃等待都要撤票")
+
+ def test_a_live_ticket_ahead_keeps_newcomers_out_and_a_dead_one_is_cleared(self):
+ lock = evidence.evidence_dir(self.repo) / "full-suite.lock"
+ queue = evidence.queue_path(lock)
+ queue.mkdir(parents=True, exist_ok=True)
+ live = queue / "00000000000000000001-live.json"
+ live.write_text(json.dumps({"pid": os.getpid(), "created": time.time(),
+ "process_started": evidence._process_started(os.getpid())}),
+ encoding="utf-8")
+ with self.assertRaises(evidence.Timeout):
+ with evidence.held(lock, wait_seconds=0):
+ pass
+ live.unlink()
+ finished = subprocess.run([sys.executable, "-c", "import os; print(os.getpid())"],
+ capture_output=True, text=True, encoding="utf-8", check=True)
+ dead = queue / "00000000000000000001-dead.json"
+ dead.write_text(json.dumps({"pid": int(finished.stdout), "created": time.time() - 60,
+ "process_started": time.time() - 60}), encoding="utf-8")
+ with evidence.held(lock, wait_seconds=0, scope="after-dead"):
+ self.assertFalse(dead.exists())
+
def test_the_waiting_runner_names_the_holder_of_the_full_suite_lock(self):
lock = evidence.evidence_dir(self.repo) / "full-suite.lock"
output = io.StringIO()