Bound semantic map publication and define experiment storage closeout

This commit is contained in:
DCCONSTRUCTIONS
2026-11-07 17:31:06 +03:00
parent 09c13a5c68
commit 43148a8769
6 changed files with 215 additions and 23 deletions
+14 -4
View File
@@ -11,6 +11,7 @@ import os
import shutil
import stat
import threading
from collections.abc import Callable
from contextlib import contextmanager
from pathlib import Path
@@ -26,6 +27,7 @@ LIMITS = {
"laboratory-rerun-overlays": 4 * GiB,
"observatory-portable-replay-cache": 4 * GiB,
"editor-cache": 4 * GiB,
"urban-maps": GiB,
}
FREE_RESERVE = 2 * GiB
_LOCK = threading.RLock()
@@ -58,7 +60,7 @@ def usage(root: Path) -> tuple[int, int]:
@contextmanager
def cache_publication(
root: Path,
additional_bytes: int,
additional_bytes: int | Callable[[], int],
*,
kind=None,
limit=None,
@@ -70,11 +72,16 @@ def cache_publication(
additional_bytes includes only bytes not yet in this root; a file renamed
within it contributes zero additional bytes. Callers include sidecar overhead.
A callable computes the reservation under the publication lock, for writers
that must recheck deduplication after waiting for another publisher.
Unknown legacy contents stay in place even if already above the new limit.
"""
root = root.expanduser().absolute()
maximum = LIMITS[kind or root.name] if limit is None else limit
if type(additional_bytes) is not int or additional_bytes < 0 or maximum < 1:
if maximum < 1 or (
not callable(additional_bytes)
and (type(additional_bytes) is not int or additional_bytes < 0)
):
raise ValueError("Invalid cache publication budget")
ancestor = root
while not ancestor.exists():
@@ -94,10 +101,13 @@ def cache_publication(
import fcntl
fcntl.flock(fd, fcntl.LOCK_EX)
reserved = additional_bytes() if callable(additional_bytes) else additional_bytes
if type(reserved) is not int or reserved < 0:
raise ValueError("Invalid cache publication budget")
current, count = usage(root)
if current + additional_bytes > maximum or count >= max_files:
if current + reserved > maximum or count >= max_files:
raise CacheCapacityError("Достигнут лимит кэша. Сохранённые данные не удалены.")
if shutil.disk_usage(root).free < additional_bytes + reserve:
if shutil.disk_usage(root).free < reserved + reserve:
raise CacheCapacityError("Недостаточно свободного места для новой копии кэша.")
yield
except CacheCapacityError as exc: