mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 13:29:19 +08:00
fix(cache): address review of the bounded compression cache
- HEADROOM_COMPRESSION_CACHE_MB=nan/inf/<=0 falls back to 64 MB instead of stopping startup (int(nan) raised outside the handler). - The disk namespace also digests every HEADROOM_* setting: the router reads some compressor settings (HEADROOM_LOSSLESS_THEN_LOSSY) from the environment into instance state, so a restart with one changed served results made under the old one. - A result with a CCR marker replacing a marker-free one deletes the old disk row, so the old bytes cannot return after eviction. - clear() clears this router's rows on disk too (clear_prefix). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
51287abf18
commit
adaef053a4
Vendored
+4
@@ -107,6 +107,10 @@ class ResultStore:
|
||||
def delete(self, key: str) -> None:
|
||||
self._run("DELETE FROM results WHERE k = ?", (key,))
|
||||
|
||||
def clear_prefix(self, prefix: str) -> None:
|
||||
"""Delete every row whose key starts with ``prefix`` (a router's namespace)."""
|
||||
self._run("DELETE FROM results WHERE substr(k, 1, ?) = ?", (len(prefix), prefix))
|
||||
|
||||
def _run(self, sql: str, args: tuple[object, ...]) -> None:
|
||||
try:
|
||||
with self._lock:
|
||||
|
||||
@@ -1582,8 +1582,10 @@ def _compression_cache_budget_bytes() -> int:
|
||||
try:
|
||||
mb = float(raw) if raw else DEFAULT_COMPRESSION_CACHE_MB
|
||||
except ValueError:
|
||||
mb = math.nan
|
||||
if not math.isfinite(mb) or mb <= 0:
|
||||
logger.warning(
|
||||
"HEADROOM_COMPRESSION_CACHE_MB=%r is not a number; using %d MB",
|
||||
"HEADROOM_COMPRESSION_CACHE_MB=%r is not a positive number of MB; using %d",
|
||||
raw,
|
||||
DEFAULT_COMPRESSION_CACHE_MB,
|
||||
)
|
||||
@@ -1621,8 +1623,13 @@ def _config_namespace(config: Any) -> str:
|
||||
return [stable(v) for v in value]
|
||||
return value
|
||||
|
||||
# The router also reads compressor settings from the environment (e.g.
|
||||
# HEADROOM_LOSSLESS_THEN_LOSSY) into instance state outside its config;
|
||||
# every HEADROOM_* setting is part of the digest, so changing any of them
|
||||
# starts a fresh namespace rather than serving output made under another.
|
||||
settings = sorted((k, v) for k, v in os.environ.items() if k.startswith("HEADROOM_"))
|
||||
h = hashlib.blake2b(digest_size=8)
|
||||
h.update(f"{__version__}|{stable(asdict(config))!r}".encode())
|
||||
h.update(f"{__version__}|{stable(asdict(config))!r}|{settings!r}".encode())
|
||||
return h.hexdigest() + ":"
|
||||
|
||||
|
||||
@@ -1762,8 +1769,13 @@ class CompressionCache:
|
||||
"""Store a compressed result (Tier 2). Thread-safe."""
|
||||
with self._lock:
|
||||
has_marker = self._admit_locked(key, compressed, ratio, strategy)
|
||||
if self._store is not None and not has_marker:
|
||||
self._store.put(self._namespace + key, compressed, ratio, strategy)
|
||||
if self._store is not None:
|
||||
if has_marker:
|
||||
# Never kept on disk; nor may an earlier marker-free result for
|
||||
# this key come back from there once this one leaves memory.
|
||||
self._store.delete(self._namespace + key)
|
||||
else:
|
||||
self._store.put(self._namespace + key, compressed, ratio, strategy)
|
||||
|
||||
def mark_skip(self, key: str) -> None:
|
||||
"""Mark content as non-compressible (Tier 1). Thread-safe."""
|
||||
@@ -1846,13 +1858,15 @@ class CompressionCache:
|
||||
self._on_clear.append(callback)
|
||||
|
||||
def clear(self) -> None:
|
||||
"""Clear this process's entries. Thread-safe. The disk tier is shared
|
||||
with other workers and routers and is left alone; its rows expire."""
|
||||
"""Clear all entries, including this router's rows on disk (other
|
||||
workers with the same config share them). Thread-safe."""
|
||||
with self._lock:
|
||||
self._results.clear()
|
||||
self._bytes = 0
|
||||
self._skip.clear()
|
||||
self._generation += 1
|
||||
if self._store is not None:
|
||||
self._store.clear_prefix(self._namespace)
|
||||
# Fire callbacks outside the lock to avoid cross-lock ordering issues.
|
||||
for callback in self._on_clear:
|
||||
callback()
|
||||
|
||||
@@ -125,3 +125,40 @@ def test_a_result_pointing_at_a_ccr_original_is_not_kept_on_disk(tmp_path):
|
||||
cache = CompressionCache(store=store)
|
||||
cache.put("k", "first lines <<ccr:0123456789abcdef>>", 0.2, "x")
|
||||
assert store.get("k") is None
|
||||
|
||||
|
||||
def test_a_nonfinite_budget_falls_back_instead_of_stopping_startup(monkeypatch):
|
||||
for raw in ("nan", "inf", "-5"):
|
||||
monkeypatch.setenv("HEADROOM_COMPRESSION_CACHE_MB", raw)
|
||||
assert CompressionCache().stats["cache_max_bytes"] == 64 * 1024 * 1024
|
||||
|
||||
|
||||
def test_a_changed_compressor_setting_does_not_reuse_old_results(tmp_path, monkeypatch):
|
||||
# Settings the router reads from the environment, outside its config.
|
||||
monkeypatch.setenv("HEADROOM_COMPRESSION_CACHE_PATH", str(tmp_path / "c.db"))
|
||||
before = ContentRouter(ContentRouterConfig(result_cache_disk=True))
|
||||
before._cache.put("k", "made under the old setting", 0.3, "kompress")
|
||||
monkeypatch.setenv("HEADROOM_LOSSLESS_THEN_LOSSY", "1")
|
||||
after = ContentRouter(ContentRouterConfig(result_cache_disk=True))
|
||||
assert after._cache.get("k") is None
|
||||
|
||||
|
||||
def test_a_marker_result_replacing_a_plain_one_removes_it_from_disk(tmp_path):
|
||||
store = ResultStore(tmp_path / "c.db", 1800)
|
||||
cache = CompressionCache(max_bytes=4_000, store=store)
|
||||
cache.put("k", "plain result", 0.5, "kompress")
|
||||
cache.put("k", "first lines <<ccr:0123456789abcdef>>", 0.2, "x")
|
||||
for i in range(10): # push "k" out of memory
|
||||
cache.put(f"filler{i}", "f" * 1000, 0.5, "kompress")
|
||||
assert cache.get("k") is None
|
||||
|
||||
|
||||
def test_clear_also_clears_the_routers_rows_on_disk(tmp_path):
|
||||
store = ResultStore(tmp_path / "c.db", 1800)
|
||||
cache = CompressionCache(store=store, namespace="ns1:")
|
||||
other = CompressionCache(store=store, namespace="ns2:")
|
||||
cache.put("k", "value", 0.5, "kompress")
|
||||
other.put("k", "theirs", 0.5, "kompress")
|
||||
cache.clear()
|
||||
assert cache.get("k") is None
|
||||
assert store.get("ns2:k") == ("theirs", 0.5, "kompress")
|
||||
|
||||
Reference in New Issue
Block a user