- /api/perf/conversation/{sid}/calls: per-call view cache on (updated, len(data)),
?since= delta with a cursor (client polls since = cursor - 30), marks memo,
(sid, updated) index. 260 calls: ~1 s → 3.7 ms cached, 0.7 ms per empty poll.
- _Memo: max_stale (soft key = summaries_version) + slots; project costs and
the related index use it (151 cost rebuilds x ~10 s in 40 min on prod).
- PerfStore: per-thread read-only connections, so a slow ingest no longer
parks pool threads (14 seen in query) and starves conversation reads.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
187 lines
6.9 KiB
Python
187 lines
6.9 KiB
Python
"""core/memo._Memo: single-flight on the key, and the stale-while-revalidate
|
|
option the slow rollups use (a soft part of the key that only ages a value,
|
|
``max_stale`` seconds) — plus its wiring into the /api/projects cost rollup."""
|
|
|
|
import threading
|
|
import time
|
|
import types
|
|
import unittest
|
|
from unittest import mock
|
|
|
|
from core import memo as memo_mod
|
|
from core.memo import _Memo
|
|
from telemetry import metrics as tm
|
|
|
|
|
|
class Clock:
|
|
def __init__(self):
|
|
self.now = 1000.0
|
|
|
|
def monotonic(self):
|
|
return self.now
|
|
|
|
|
|
class Builds:
|
|
"""A builder that counts its builds and can be held mid-build."""
|
|
|
|
__name__ = "test_memo_builds"
|
|
|
|
def __init__(self):
|
|
self.n = 0
|
|
self.hold: threading.Event | None = None
|
|
self.started = threading.Event()
|
|
|
|
def __call__(self, *args):
|
|
self.n += 1
|
|
self.started.set()
|
|
if self.hold is not None:
|
|
self.hold.wait(5)
|
|
return ("v", self.n, args)
|
|
|
|
|
|
def in_thread(fn):
|
|
out = {}
|
|
th = threading.Thread(target=lambda: out.setdefault("v", fn()), daemon=True)
|
|
th.start()
|
|
return th, out
|
|
|
|
|
|
class MemoTests(unittest.TestCase):
|
|
def setUp(self):
|
|
self.clock = Clock()
|
|
p = mock.patch.object(memo_mod, "time", types.SimpleNamespace(
|
|
monotonic=self.clock.monotonic, perf_counter=time.perf_counter))
|
|
p.start()
|
|
self.addCleanup(p.stop)
|
|
self.build = Builds()
|
|
|
|
def test_without_max_stale_the_whole_key_is_exact(self):
|
|
m = _Memo(self.build)
|
|
self.assertEqual(m.get("k", 1), ("v", 1, (1,)))
|
|
self.assertEqual(m.get("k", 1)[1], 1)
|
|
self.assertEqual(m.get("k", 1, soft=2)[1], 2) # a soft change, no max_stale
|
|
self.clock.now += 10_000
|
|
self.assertEqual(m.get("k", 1, soft=2)[1], 2) # never expires on its own
|
|
self.assertEqual(m.get("k2", 1, soft=2)[1], 3)
|
|
|
|
def test_a_soft_change_serves_the_cached_value_until_max_stale(self):
|
|
m = _Memo(self.build, max_stale=60)
|
|
self.assertEqual(m.get("h", soft=1)[1], 1)
|
|
self.clock.now += 30
|
|
self.assertEqual(m.get("h", soft=2)[1], 1) # stale, within 60 s
|
|
self.clock.now += 31 # 61 s after the build began
|
|
self.assertEqual(m.get("h", soft=3)[1], 2) # rebuilt
|
|
self.assertEqual(m.get("h", soft=3)[1], 2)
|
|
self.clock.now += 10_000
|
|
self.assertEqual(m.get("h", soft=3)[1], 2) # up to date: no expiry
|
|
self.assertEqual(self.build.n, 2)
|
|
|
|
def test_a_hard_change_rebuilds_at_once(self):
|
|
m = _Memo(self.build, max_stale=60)
|
|
m.get("h1", soft=1)
|
|
self.clock.now += 1
|
|
self.assertEqual(m.get("h2", soft=1)[1], 2)
|
|
self.assertEqual(m.get("h1", soft=1)[1], 3) # one slot: h1 was evicted
|
|
|
|
def test_past_max_stale_the_first_caller_rebuilds_and_the_rest_get_the_stale_value(self):
|
|
m = _Memo(self.build, max_stale=60)
|
|
m.get("h", soft=1)
|
|
self.clock.now += 61
|
|
self.build.hold, self.build.started = threading.Event(), threading.Event()
|
|
first, first_out = in_thread(lambda: m.get("h", soft=2))
|
|
self.assertTrue(self.build.started.wait(2)) # the first caller is building
|
|
other, other_out = in_thread(lambda: m.get("h", soft=3))
|
|
other.join(2)
|
|
self.assertFalse(other.is_alive(), "a stale caller waited on the rebuild")
|
|
self.assertEqual(other_out["v"][1], 1) # the stale value, at once
|
|
self.build.hold.set()
|
|
first.join(2)
|
|
self.assertEqual(first_out["v"][1], 2)
|
|
self.assertEqual(m.get("h", soft=2)[1], 2)
|
|
self.assertEqual(self.build.n, 2)
|
|
|
|
def test_a_hard_change_stays_single_flight(self):
|
|
m = _Memo(self.build, max_stale=60)
|
|
m.get("h1", soft=1)
|
|
self.build.hold, self.build.started = threading.Event(), threading.Event()
|
|
a, a_out = in_thread(lambda: m.get("h2", soft=1))
|
|
self.assertTrue(self.build.started.wait(2))
|
|
b, b_out = in_thread(lambda: m.get("h2", soft=1))
|
|
b.join(0.2)
|
|
self.assertTrue(b.is_alive()) # waits for the build…
|
|
self.build.hold.set()
|
|
a.join(2)
|
|
b.join(2)
|
|
self.assertEqual(a_out["v"], b_out["v"]) # …and takes its result
|
|
self.assertEqual(self.build.n, 2)
|
|
|
|
def test_slots_keep_a_few_hard_keys(self):
|
|
m = _Memo(self.build, slots=2)
|
|
m.get("a")
|
|
m.get("b")
|
|
m.get("a")
|
|
self.assertEqual(self.build.n, 2)
|
|
m.get("c") # evicts the oldest build: a
|
|
m.get("b")
|
|
self.assertEqual(self.build.n, 3)
|
|
m.get("a")
|
|
self.assertEqual(self.build.n, 4)
|
|
|
|
def test_a_build_is_timed_under_its_builders_name(self):
|
|
def count():
|
|
return tm.REGISTRY.get_sample_value("aiagent_memo_build_seconds_count",
|
|
{"memo": "test_memo_builds"}) or 0
|
|
before = count()
|
|
m = _Memo(self.build, max_stale=60)
|
|
m.get("h", soft=1)
|
|
m.get("h", soft=2) # stale: no build
|
|
self.assertEqual(count(), before + 1)
|
|
|
|
|
|
class ProjectCostsMemoTests(unittest.TestCase):
|
|
"""/api/projects: a transcript write only ages the cost rollup (≤ 60 s);
|
|
who a conversation belongs to (meta, profiles, workers) rebuilds it."""
|
|
|
|
def setUp(self):
|
|
from projects import routes as projects_routes # noqa: PLC0415
|
|
self.routes = projects_routes
|
|
self.clock = Clock()
|
|
p = mock.patch.object(memo_mod, "time", types.SimpleNamespace(
|
|
monotonic=self.clock.monotonic, perf_counter=time.perf_counter))
|
|
p.start()
|
|
self.addCleanup(p.stop)
|
|
self.build = Builds()
|
|
memo = _Memo(self.build, max_stale=projects_routes.COSTS_MAX_STALE_S, slots=4)
|
|
p = mock.patch.object(projects_routes, "_costs_memo", memo)
|
|
p.start()
|
|
self.addCleanup(p.stop)
|
|
p = mock.patch.object(projects_routes.merges_mod, "version", lambda: 0.0)
|
|
p.start()
|
|
self.addCleanup(p.stop)
|
|
v = types.SimpleNamespace
|
|
self.deps = v(store=v(summaries_version=1), meta_store=v(version=1),
|
|
profile_store=v(version=1), worker_store=v(version=1))
|
|
|
|
def test_summaries_are_soft_the_rest_is_hard(self):
|
|
costs = self.routes._project_costs
|
|
costs(self.deps)
|
|
self.deps.store.summaries_version = 2
|
|
self.clock.now += 59
|
|
costs(self.deps)
|
|
self.assertEqual(self.build.n, 1)
|
|
self.clock.now += 2
|
|
costs(self.deps)
|
|
self.assertEqual(self.build.n, 2)
|
|
self.deps.meta_store.version = 2
|
|
costs(self.deps)
|
|
self.assertEqual(self.build.n, 3)
|
|
# another ?account= view keeps its own slot
|
|
costs(self.deps, "profile:x")
|
|
costs(self.deps)
|
|
costs(self.deps, "profile:x")
|
|
self.assertEqual(self.build.n, 4)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|