Spaces:
Running
Running
File size: 4,516 Bytes
3dfc93b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | """case ์ ๊ธ ํ์ผ (๊ณํ 2026-09-27 L6 ยง2.5 ยท D-L6-3 ยท ๊ฒํ F8).
`<hub_reports_dir>/<a>/<c>/hook.lock` ์ `fcntl.flock(LOCK_EX)` โ `LOCK_NB` ๋ก `LOCK_POLL_SECONDS` ๋ง๋ค ๋ค์ ์๋ํ๊ณ , ํ๋
`timeout`(๊ธฐ๋ณธ `HOOK_LOCK_TIMEOUT_SECONDS` 1200 s โ hub ๋ค๋ฆฌ ํ๋ 900 s + ๋ฒ๋ค ๋ค๋ฆฌ ์ฌ์ )์ ๋๊ธฐ๋ฉด ์ก์ง ๋ชปํ ์ฑ `False` ๋ฅผ ๋ด์ค๋ค
(ํธ์ถ์๊ฐ `lock_timeout` ์ผ๋ก ๋ณด๊ณ ํ๋ค). ๋๊ธฐ ์ค `LOCK_NOTICE_SECONDS` ๋ง๋ค "๊ฐ์ case ์ ๋ค๋ฅธ ํ
์ ๊ธฐ๋ค๋ฆฌ๋ ์คโฆ N ์ด์งธ" ํ ์ค์
stderr ์ ๋ธ๋ค.
- ์ ๊ธ ํ์ผ ๋ถ๋ชจ๋ `mkdir(parents=True)` ๋ก ๋ง๋ค๊ณ , ํ์ผ์ **์ง์ฐ์ง ์๋๋ค** โ ์ง์ฐ๋ฉด ์ ํ์ผ์ ์ ๊ธ์ ๊ฑฐ๋ ์ชฝ๊ณผ ์ ํ์ผ์ ๊ฑฐ๋
์ชฝ์ด ๋์์ "์ก์๋ค" ๊ณ ๋ฏฟ๋ ๊ฒฝ์์ด ์๊ธด๋ค.
- `flock` ์ ์ด๋ฆฐ ํ์ผ ๊ธฐ์ ์์ ๊ฑธ๋ฆฐ๋ค โ ํ๋ก์ธ์ค๊ฐ ์ฃฝ์ผ๋ฉด ์ปค๋์ด ํผ๋ค(์์ฌ ์ ๊ธ ์์). ๊ฐ์ ํ๋ก์ธ์ค๋ผ๋ ๋ฐ๋ก ์ฐ ๊ธฐ์ ์๋ผ๋ฆฌ๋
์๋ก ๋ง๋๋ค. ๋ฐ์ดํฐ ํ์ ๋ก์ปฌ ๋์คํฌ ์ ์ ๋ค(NFS ์์๋ `flock` ์ด ์๋ฏธ๊ฐ ์๋ค โ ๊ณํ R-L6-11).
- hub ๋์ run ์ ๋ ๋ค๋ฆฌ ์ ๋ถ์ batch ๋ hub ๋ค๋ฆฌ(`hook.finish_batch`)๋ฅผ ๊ฐ์ผ๋ค. L6b `runs eval` ์ ๊ฒฝ๋ก๋ โ ~โค ์ ๋ถ๋ฅผ ์ด
์ ๊ธ ์์์ ๋๋ค(๊ณํ ยง2.12 B4) โ ๊ทธ ๊ฒฝ๋ก๊ฐ ๋ถ๋ฅด๋ `bundle_leg.ensure_run_bundle` ์ ์ค์ค๋ก ์ ๊ทธ์ง ์๊ณ ํธ์ถ์๊ฐ ์ฅ ์ ๊ธ์
์ ์ ํ๋ค(๊ฐ์ ํ๋ก์ธ์ค์์ ๋ค์ ์ก์ผ๋ฉด ์ ๊ท์น๋๋ก ์๊ธฐ ์์ ์ ๊ธฐ๋ค๋ฆฐ๋ค). ์ ๊ธ ํ์ผ์๋ ์๋ฌด๊ฒ๋ ์ฐ์ง ์๋๋ค.
`poc_bundle` ์ ํจ์ ์์์๋ง import ํ๋ค(D-L6-14).
"""
from __future__ import annotations
import fcntl
import os
import time
from collections.abc import Iterator
from contextlib import contextmanager
from pathlib import Path
import click
from solar_eval.hub_hook.types import (
HOOK_LOCK_TIMEOUT_SECONDS,
LOCK_FILENAME,
LOCK_NOTICE_SECONDS,
LOCK_POLL_SECONDS,
)
#: ์ ๊ธ ํ์ผ ๊ถํ(๋ด์ฉ์ ๋น์ด ์๋ค โ ๊ถํ์ ๋ฐ์ดํฐ ํ์ ๋ค๋ฅธ ํ์ผ๊ณผ ๊ฐ๊ฒ).
LOCK_FILE_MODE = 0o644
def lock_path(account: str, case: str) -> Path:
"""์ ๊ธ ํ์ผ ์์น `<hub_reports_dir>/<a>/<c>/hook.lock`. ์ฌ๋ฌ๊ทธ๊ฐ ํ๋ฆฌ๋ฉด `DataConfigError`(๊ฒฝ๋ก ์กฐ๊ฐ์ผ๋ก ์ฐ๊ธฐ ์ ์ ๋ง๋๋ค)."""
from poc_bundle.data.case_ref import parse_case_ref
from poc_bundle.home import hub_reports_dir
account, case = parse_case_ref(f"{account}/{case}")
return hub_reports_dir() / account / case / LOCK_FILENAME
@contextmanager
def case_lock(
account: str, case: str, *, timeout: float = HOOK_LOCK_TIMEOUT_SECONDS
) -> Iterator[bool]:
"""case ํ๋์ ๋ฐฐํ ์ ๊ธ. ํ๋ ์์ ์ก์ผ๋ฉด `True`, ๋๊ธฐ๋ฉด `False`(ํธ์ถ์๊ฐ `lock_timeout` ์ผ๋ก ๋ณด๊ณ ).
Raises:
ValueError: `timeout` ์ด ์์์ผ ๋.
DataConfigError: case ์ฌ๋ฌ๊ทธ๊ฐ ํ๋ ธ์ ๋(`poc_bundle.data.errors`).
OSError: ์ ๊ธ ํ์ผ์ ๋ง๋ค๊ฑฐ๋ ์ด ์ ์์ ๋(hook ์ด `lock_exception:<Type>` ์ผ๋ก ๊ฐ๋ฑํ๋ค).
"""
if timeout < 0:
raise ValueError(f"lock timeout must be >= 0, got {timeout}")
path = lock_path(account, case)
path.parent.mkdir(parents=True, exist_ok=True)
fd = os.open(path, os.O_RDWR | os.O_CREAT, LOCK_FILE_MODE)
try:
held = _acquire(fd, timeout, f"{account}/{case}")
try:
yield held
finally:
if held:
fcntl.flock(fd, fcntl.LOCK_UN)
finally:
os.close(fd)
def _acquire(fd: int, timeout: float, case_ref: str) -> bool:
"""`LOCK_NB` ํด๋ง์ผ๋ก ์ ๊ทผ๋ค. ํ๋ ์์ ์ก์ผ๋ฉด True โ ํ๋ 0 ์ด๋ฉด ํ ๋ฒ๋ง ์๋ํ๋ค."""
started = time.monotonic()
next_notice = LOCK_NOTICE_SECONDS
while True:
try:
fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
except BlockingIOError:
elapsed = time.monotonic() - started
else:
return True
if elapsed >= timeout:
return False
if elapsed >= next_notice:
_echo_waiting(case_ref, elapsed)
next_notice += LOCK_NOTICE_SECONDS
time.sleep(min(LOCK_POLL_SECONDS, max(timeout - elapsed, 0.0)))
def _echo_waiting(case_ref: str, elapsed: float) -> None:
"""๋๊ธฐ ์๋ด ํ ์ค(stderr) โ case ์ด๋ฆ๊ณผ ๊ฒฝ๊ณผ ์ด๋ง."""
click.secho(
f" ๊ฐ์ case({case_ref})์ ๋ค๋ฅธ ํ
์ ๊ธฐ๋ค๋ฆฌ๋ ์คโฆ {int(elapsed)} ์ด์งธ",
fg="yellow",
err=True,
)
|