perf(hub): cache the names directory, share state builds, retry stations
Concurrent /api/state polls share one build through LiveState for a short TTL, and every POST and PATCH invalidates it, so a write is never followed by a stale view. The names directory is cached, the first transcript scan reads outside the hub lock, the graph timeline starts at the window begin, and unresolved radio stations are retried on a daemon thread every RETRY_SECONDS instead of failing until restart. 🎯 Fix: 85 % 💥 Break: 8 % 💸 Tokens: – ⏱️ Velocity: – 🛡️ Security: 100 % 🏆 Score: 78 🚦 Gate: ⛔ fix 85 % < 90, break 8 % > 5 Created-with: https://s.veen.world/skills Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
6c63c364bd
commit
96d4d2933c
18 files changed
+299
-32
No files matched your search
@@ -77,6 +77,7 @@ class HubCase(unittest.TestCase):
|
||||
engines=self.engines,
|
||||
approvers=self.approvers,
|
||||
redirects=self.redirects(),
|
||||
live_ttl=self.live_ttl(),
|
||||
)
|
||||
self.httpd = httpd
|
||||
self.origin = signing.origin_of(self.port)
|
||||
@@ -90,6 +91,13 @@ class HubCase(unittest.TestCase):
|
||||
def redirects(self) -> Redirects:
|
||||
return offline_redirects()
|
||||
|
||||
def live_ttl(self) -> float:
|
||||
"""Returns:
|
||||
0, so a test that writes council files directly reads them in the
|
||||
next state; a case testing the live cache overrides it.
|
||||
"""
|
||||
return 0.0
|
||||
|
||||
def call(self, method: str, path: str, body: bytes | None, headers: dict[str, str]) -> tuple[int, dict, bytes]:
|
||||
connection = http.client.HTTPConnection("127.0.0.1", self.port, timeout=30)
|
||||
try:
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import threading
|
||||
import time
|
||||
import unittest
|
||||
|
||||
import hub_support # noqa: F401
|
||||
from hub_case import HubCase
|
||||
from kvb_orchestrator.livestate import LiveState
|
||||
|
||||
|
||||
def counting(pause: float) -> tuple[list[int], object]:
|
||||
calls: list[int] = []
|
||||
|
||||
def build() -> dict:
|
||||
calls.append(1)
|
||||
time.sleep(pause)
|
||||
return {"build": len(calls)}
|
||||
|
||||
return calls, build
|
||||
|
||||
|
||||
class TestLiveState(unittest.TestCase):
|
||||
def test_callers_arriving_during_a_build_wait_for_it_and_share_it(self):
|
||||
live = LiveState(60.0)
|
||||
calls, build = counting(0.3)
|
||||
results: list[dict] = []
|
||||
threads = [threading.Thread(target=lambda: results.append(live.shared("live", build))) for _ in range(4)]
|
||||
for thread in threads:
|
||||
thread.start()
|
||||
for thread in threads:
|
||||
thread.join(5)
|
||||
self.assertEqual((len(calls), len(results)), (1, 4))
|
||||
self.assertTrue(all(result is results[0] for result in results))
|
||||
|
||||
def test_a_zero_ttl_rebuilds_and_other_keys_build_on_their_own(self):
|
||||
live = LiveState(0.0)
|
||||
calls, build = counting(0.0)
|
||||
live.shared("live", build)
|
||||
live.shared("live", build)
|
||||
live.shared("track", build)
|
||||
self.assertEqual(len(calls), 3)
|
||||
|
||||
def test_invalidate_ends_the_shared_build_at_once(self):
|
||||
live = LiveState(60.0)
|
||||
_, build = counting(0.0)
|
||||
first = live.shared("live", build)
|
||||
self.assertIs(live.shared("live", build), first)
|
||||
live.invalidate()
|
||||
self.assertEqual(live.shared("live", build), {"build": 2})
|
||||
|
||||
def test_a_build_overtaken_by_invalidate_is_not_served_again(self):
|
||||
live = LiveState(60.0)
|
||||
started = threading.Event()
|
||||
|
||||
def slow() -> dict:
|
||||
started.set()
|
||||
time.sleep(0.2)
|
||||
return {"stale": True}
|
||||
|
||||
thread = threading.Thread(target=lambda: live.shared("live", slow))
|
||||
thread.start()
|
||||
started.wait(5)
|
||||
live.invalidate()
|
||||
thread.join(5)
|
||||
self.assertEqual(live.shared("live", lambda: {"stale": False}), {"stale": False})
|
||||
|
||||
|
||||
class TestLiveStateRoute(HubCase):
|
||||
def live_ttl(self) -> float:
|
||||
return 60.0
|
||||
|
||||
def test_polls_share_one_state_until_a_post_or_a_window_query(self):
|
||||
first = self.snapshot()
|
||||
self.assertEqual(self.snapshot()["now"], first["now"])
|
||||
status, _, body = self.get(f"/api/state?to={first['now']}")
|
||||
self.assertEqual(status, 200, body)
|
||||
self.assertGreater(json.loads(body)["now"], first["now"])
|
||||
self.post_json("/api/inbox/mark", {}, self.guard(self.host))
|
||||
self.assertGreater(self.snapshot()["now"], first["now"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -27,5 +27,37 @@ class TestRenameOnce(unittest.TestCase):
|
||||
self.assertEqual([row["title"] for row in names.Directory(entries).renames(0, 10)], ["A is now called B", "B is now called A"])
|
||||
|
||||
|
||||
def flips(count: int) -> list[dict]:
|
||||
return [{"session": "s1", "name": "AB"[index % 2], "first_seen": float(index), "source": "claim"} for index in range(count)]
|
||||
|
||||
|
||||
class TestDirectoryLookups(unittest.TestCase):
|
||||
def test_at_names_the_last_entry_seen_at_or_before_the_moment_and_the_first_one_before_any(self):
|
||||
directory = names.Directory(flips(6))
|
||||
scanned = flips(6)
|
||||
for ts in (-1.0, 0.0, 0.5, 1.0, 2.9, 3.0, 5.0, 99.0):
|
||||
valid = [entry for entry in scanned if entry["first_seen"] <= ts]
|
||||
self.assertEqual(directory.at("s1", ts), (valid[-1] if valid else scanned[0])["name"], ts)
|
||||
self.assertEqual(directory.at("s9", 1.0), "")
|
||||
|
||||
def test_a_windowed_timeline_keeps_the_birth_and_every_name_from_the_last_one_before_the_window(self):
|
||||
directory = names.Directory(flips(10))
|
||||
self.assertEqual([entry["since"] for entry in directory.timeline("s1", 6.5)], [0.0, 6.0, 7.0, 8.0, 9.0])
|
||||
self.assertEqual(directory.timeline("s1", -5.0), directory.timeline("s1"))
|
||||
self.assertEqual(directory.timeline("s1", 0.0), directory.timeline("s1"))
|
||||
self.assertEqual(directory.timeline("s9", 3.0), [])
|
||||
|
||||
def test_the_directory_is_rebuilt_only_after_a_new_entry(self):
|
||||
with tempfile.TemporaryDirectory() as folder:
|
||||
book = names.Names(Path(folder) / "names.json")
|
||||
book.observe([("s1", "A", 1.0, "claim")], 10.0)
|
||||
first = book.directory()
|
||||
book.observe([("s1", "A", 2.0, "claim")], 20.0)
|
||||
self.assertIs(book.directory(), first)
|
||||
book.observe([("s1", "B", 3.0, "claim")], 30.0)
|
||||
self.assertIsNot(book.directory(), first)
|
||||
self.assertEqual(book.directory().now("s1"), "B")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -351,6 +351,24 @@ class TestRedirectSettings(HubCase):
|
||||
self.assertEqual([stream["via"] for stream in retried["settings"]["streams"]], [["https://*.cdn.test"], ["https://*.cdn.test"]])
|
||||
self.assertEqual(retried["problems"], [])
|
||||
|
||||
def test_an_unresolved_station_heals_without_a_save_once_due(self):
|
||||
saved = self.signed_and_saved([self.station(STATION, [])])["settings"]
|
||||
cdn().httpd.broken.add("station.radio.test")
|
||||
self.addCleanup(cdn().httpd.broken.discard, "station.radio.test")
|
||||
other = "https://station.radio.test/other"
|
||||
self.signed_and_saved([*saved["streams"], self.station(other, [])])
|
||||
hub = self.httpd.hub
|
||||
before = self.dials()
|
||||
self.assertEqual((hub.retry_stations(), self.dials() - before), ([], 0))
|
||||
hub.redirects.tried[other] -= RETRY_SECONDS
|
||||
self.assertEqual(hub.retry_stations(), ["DLF: answers 302 without a Location"])
|
||||
self.assertIsNone(hub.settings.get()["streams"][1]["via"])
|
||||
cdn().httpd.broken.discard("station.radio.test")
|
||||
hub.redirects.tried[other] -= RETRY_SECONDS
|
||||
self.assertEqual(hub.retry_stations(), [])
|
||||
self.assertEqual([stream["via"] for stream in hub.settings.get()["streams"]], [["https://*.cdn.test"], ["https://*.cdn.test"]])
|
||||
self.assertIn("https://*.cdn.test", self.media_src())
|
||||
|
||||
def test_a_save_that_lands_while_another_resolves_makes_that_one_fail(self):
|
||||
saved = self.signed_and_saved([self.station(STATION, [])])["settings"]
|
||||
slow = [*saved["streams"], self.station("https://drip.radio.test/", [])]
|
||||
|
||||
@@ -579,7 +579,7 @@ class TestHubView(HubCase):
|
||||
council=self.council, pages=self.pages, public_port=self.port, state=self.state, host_state=HOST_STATE, transcripts=self.projects, scripts=self.scripts,
|
||||
templates=HUB / "templates",
|
||||
places=server.Places(tree_file=self.tree_file, timers=self.timers, vendor=HUB / "vendor", cleanup=self.cleanup, offices=self.offices, kept=self.kept),
|
||||
model=server.Model(url="", model="", key=""), engines=self.engines, approvers=self.approvers, redirects=self.redirects(),
|
||||
model=server.Model(url="", model="", key=""), engines=self.engines, approvers=self.approvers, redirects=self.redirects(), live_ttl=self.live_ttl(),
|
||||
)
|
||||
|
||||
def test_a_get_and_a_patch_round_trip_and_the_file_is_private(self):
|
||||
|
||||
Reference in new issue
Block a user