From 3ea08df69ef08ea3fb3bba9278132dbbf17f6bb9 Mon Sep 17 00:00:00 2001 From: Marius S <39998+winding-lines@users.noreply.github.com> Date: Thu, 19 Nov 2026 06:14:17 -0800 Subject: [PATCH 1/3] morca ui: keep data reads at a named snapshot; a missing snapshot is 404 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_017TycszSbyw92Efu4ZeCYL6 --- changes/0-ui-data-cache.minor.md | 1 + docs/ui.md | 16 ++ pixi.toml | 3 +- scripts/unit-tests.sh | 1 + src/morca/_internal/views/data.mojo | 9 +- src/morca/_internal/web/cache.mojo | 252 +++++++++++++++++++++++++ src/morca/_internal/web/limit.mojo | 10 +- src/morca/_internal/web/server.mojo | 32 +++- src/morca/_internal/web/turnstile.mojo | 5 + src/morca/_lake/dataread.mojo | 11 ++ tests/dataview.sh | 26 +++ tests/test_data_cache.mojo | 77 ++++++++ tests/ui_drain.sh | 6 +- 13 files changed, 439 insertions(+), 10 deletions(-) create mode 100644 changes/0-ui-data-cache.minor.md create mode 100644 src/morca/_internal/web/cache.mojo create mode 100644 tests/test_data_cache.mojo diff --git a/changes/0-ui-data-cache.minor.md b/changes/0-ui-data-cache.minor.md new file mode 100644 index 00000000..e1e7a84a --- /dev/null +++ b/changes/0-ui-data-cache.minor.md @@ -0,0 +1 @@ +- **UI:** data reads at a named snapshot are kept in memory for visitors not logged in (`MORCA_UI_DATA_CACHE_MB`, 64), and a snapshot the table no longer has is 404, not 400. diff --git a/docs/ui.md b/docs/ui.md index a8877069..8ff49466 100644 --- a/docs/ui.md +++ b/docs/ui.md @@ -774,6 +774,22 @@ It's off unless the site key and the secret are both set. On Hetzner, `TURNSTILE_SECRET_FILE`) and `TURNSTILE_HOSTNAMES` (UI_URL's host unless set) in `hetzner.env` set them. +## Data reads kept + +A data read at a named `?snapshot=` (a snapshot never changes) by a visitor +not logged in is kept in memory, so asking it again is answered at once +(`X-Morca-Cache: hit`): it counts toward the client's rate but takes no +data-read slot. The 200s are kept, and the 404s for a snapshot the table no +longer has (an expired one); the least recently used go first, and none is +kept past ten minutes. + +| Variable | Default | | +|---|---|---| +| `MORCA_UI_DATA_CACHE_MB` | 64 | what the kept reads may hold; 0 keeps none | + +A read at a snapshot the table hasn't is 404, not 400: a 400 is a mistake +in what was asked (a column it hasn't, a filter that doesn't parse). + ## Stopping SIGTERM (what `docker stop` sends) or SIGINT (Ctrl-C) drains `morca ui` diff --git a/pixi.toml b/pixi.toml index 0b9bdcc9..714709d7 100644 --- a/pixi.toml +++ b/pixi.toml @@ -217,6 +217,7 @@ test-weekly = { cmd = "scripts/with-postgres.sh tests/weekly.sh" } test-toml = { cmd = "scripts/unit-tests.sh build scheduler-ui && scripts/unit-tests.sh run test-toml" } test-limit = { cmd = "scripts/unit-tests.sh build scheduler-ui && scripts/unit-tests.sh run test-limit" } test-turnstile = { cmd = "scripts/unit-tests.sh build scheduler-ui && scripts/unit-tests.sh run test-turnstile" } +test-data-cache = { cmd = "scripts/unit-tests.sh build scheduler-ui && scripts/unit-tests.sh run test-data-cache" } test-upgrade = { cmd = "scripts/unit-tests.sh build scheduler-ui && scripts/unit-tests.sh run test-upgrade" } test-config = { cmd = "scripts/unit-tests.sh build scheduler-ui && scripts/unit-tests.sh run test-config && scripts/with-postgres.sh tests/config.sh" } # Fills (morca.fill): a pipeline filling its range by itself, within limits. @@ -301,7 +302,7 @@ test-executor-core = { depends-on = ["build-shared", "test-sample", "test-steps" test-executor-workers = { depends-on = ["build-shared", "test-ec2", "test-autoscale", "test-no-workers", "test-pool", "test-hcloud", "test-pool-scale", "test-pool-scale-ec2", "test-pool-image", "test-run-cluster", "test-secrets", "test-placement", "test-grants", "test-pressure", "test-lost-workers"] } test-executor = { depends-on = ["test-executor-core", "test-executor-workers"] } # The scheduler, the UI, and what a registration and its code control: -test-scheduler-ui = { depends-on = ["build-shared", "test-toml", "test-upgrade", "test-config", "test-fill", "test-fill-version", "test-fill-requests", "test-priority", "test-pool-fills", "test-run-locks", "test-metrics", "test-terms", "test-scheduler", "test-ui", "test-fleet", "test-system", "test-actions", "test-calendar", "test-ui-login", "test-ui-drain", "test-remote", "test-worker-api", "test-pause", "test-older-binary", "test-watchdog", "test-limit", "test-turnstile", "test-notify", "test-versions", "test-manual-run", "test-new-code", "test-defer", "test-dead-runs", "test-adopt", "test-overlap", "test-outdated", "test-concurrency", "test-one-run-per-key", "test-hourly", "test-weekly", "test-sense-errors", "test-stop", "test-binary-timeouts"] } +test-scheduler-ui = { depends-on = ["build-shared", "test-toml", "test-upgrade", "test-config", "test-fill", "test-fill-version", "test-fill-requests", "test-priority", "test-pool-fills", "test-run-locks", "test-metrics", "test-terms", "test-scheduler", "test-ui", "test-fleet", "test-system", "test-actions", "test-calendar", "test-ui-login", "test-ui-drain", "test-remote", "test-worker-api", "test-pause", "test-older-binary", "test-watchdog", "test-limit", "test-data-cache", "test-turnstile", "test-notify", "test-versions", "test-manual-run", "test-new-code", "test-defer", "test-dead-runs", "test-adopt", "test-overlap", "test-outdated", "test-concurrency", "test-one-run-per-key", "test-hourly", "test-weekly", "test-sense-errors", "test-stop", "test-binary-timeouts"] } test = { depends-on = ["test-executor", "test-scheduler-ui"] } toy = { cmd = "scripts/mojo-build.sh -I src $DEP_INC examples/toy.mojo -o build/toy" } fanout = { cmd = "scripts/mojo-build.sh -I src $DEP_INC examples/fanout.mojo -o build/fanout" } diff --git a/scripts/unit-tests.sh b/scripts/unit-tests.sh index 1c18e924..b550290c 100755 --- a/scripts/unit-tests.sh +++ b/scripts/unit-tests.sh @@ -87,6 +87,7 @@ scheduler-ui:tests/test_worker.mojo:test-worker-api scheduler-ui:tests/test_watchdog.mojo:test-watchdog scheduler-ui:tests/test_limit.mojo:test-limit scheduler-ui:tests/test_turnstile.mojo:test-turnstile +scheduler-ui:tests/test_data_cache.mojo:test-data-cache data-tables:tests/test_tables.mojo:test-tables data-tables:tests/test_lake_batch.mojo:test-lake-batch data-tables:tests/test_replace.mojo:test-replace diff --git a/src/morca/_internal/views/data.mojo b/src/morca/_internal/views/data.mojo index 687e9994..e2559d34 100644 --- a/src/morca/_internal/views/data.mojo +++ b/src/morca/_internal/views/data.mojo @@ -297,7 +297,8 @@ struct DataRead(Movable): def read_data(argv: List[String], limits: DataLimits) -> DataRead: """Runs `argv` (`data_argv`), killing it `GRACE_SECONDS` past its time limit. A mistake in the request (a column the table hasn't) is the - JSON's `error`, 400; no binary or a read that failed, 503.""" + JSON's `error`, 400 (404 for a snapshot the table hasn't); no binary or + a read that failed, 503.""" if len(argv) == 0 or not argv[0]: return DataRead( String(), @@ -328,7 +329,11 @@ def read_data(argv: List[String], limits: DataLimits) -> DataRead: var v = loads(text) var err = v.try_at("/error") if err and err.value().is_string(): - return DataRead(String(), err.value().string_value(), 400) + var status = 400 + var st = v.try_at("/status") + if st and st.value().int_value() == 404: + status = 404 + return DataRead(String(), err.value().string_value(), status) return DataRead(text^, String(), 200) except e: return DataRead(String(), String(e), 503) diff --git a/src/morca/_internal/web/cache.mojo b/src/morca/_internal/web/cache.mojo new file mode 100644 index 00000000..9630e58d --- /dev/null +++ b/src/morca/_internal/web/cache.mojo @@ -0,0 +1,252 @@ +"""Answers to data reads that `morca ui` keeps, so asking again costs +nothing: a Data tab, `data.json` or `data.csv` at a named `?snapshot=` +(a snapshot never changes), asked by a visitor not logged in (with no +session cookie and no `Authorization` header, so the page is the same for +everyone who asks it). + +| Variable | Default | | +|---|---|---| +| `MORCA_UI_DATA_CACHE_MB` | 64 | what the kept answers may hold, together; 0 keeps none | + +A 200 is kept, and so is a 404 (a snapshot the table no longer has: the +ones crawlers ask for again and again). The least recently used go first +when a new one doesn't fit, and none is kept past `TTL_SECONDS`, so the +page around the data (its header, the nav) isn't stale for long. A kept +answer says so (`X-Morca-Cache: hit`); it counts toward the client's rate +(`morca._internal.web.limit`) but takes none of the data-read slots. + +This is the UI's own; `morca_maintain data` keeps manifests on disk +(`MORCA_DATA_CACHE`, `MORCA_DATA_CACHE_MB`) apart from it. +""" + +from std.ffi import external_call +from std.memory.alloc import unsafe_alloc +from std.os import getenv + +from flare.http import Request, Response +from threads import Mutex + +from morca._internal.html import query_param +from morca._internal.web.limit import exempt, is_data_read + +comptime TTL_SECONDS = 600 +comptime DEFAULT_MB = 64 +comptime HIT_HEADER = "X-Morca-Cache" +comptime _KEPT_HEADERS = [ + "Content-Type", + "Content-Disposition", + "X-Morca-Rows", + "X-Morca-Stopped", + "Cache-Control", +] + + +def _now() -> Int: + return Int(external_call["time", Int64](Int(0))) + + +def cache_bytes() -> Int: + """`MORCA_UI_DATA_CACHE_MB`, in bytes; 0 is no cache.""" + var raw = getenv("MORCA_UI_DATA_CACHE_MB") + if not raw: + return DEFAULT_MB * 1024 * 1024 + try: + var n = Int(raw) + if n >= 0: + return n * 1024 * 1024 + except: + pass + print( + "morca ui: MORCA_UI_DATA_CACHE_MB isn't a count:", + raw, + "- using", + DEFAULT_MB, + ) + return DEFAULT_MB * 1024 * 1024 + + +@fieldwise_init +struct Kept(Copyable, Movable): + """One kept answer: its status, body and the headers that go with it.""" + + var status: Int + var body: List[UInt8] + var names: List[String] + var values: List[String] + var made: Int + """When it was kept (seconds).""" + var used: Int + """The cache's tick when it was last asked for.""" + + def size(self, key: String) -> Int: + var n = len(self.body) + key.byte_length() + 64 + for i in range(len(self.names)): + n += self.names[i].byte_length() + self.values[i].byte_length() + return n + + +struct DataCache(Movable): + """The answers, least recently used dropped first past `max_bytes`.""" + + var max_bytes: Int + var bytes: Int + var tick: Int + var kept: Dict[String, Kept] + + def __init__(out self, max_bytes: Int): + self.max_bytes = max_bytes + self.bytes = 0 + self.tick = 0 + self.kept = Dict[String, Kept]() + + def get(mut self, key: String, now: Int) -> Optional[Kept]: + if key not in self.kept: + return None + try: + ref k = self.kept[key] + if now - k.made > TTL_SECONDS: + self._drop(key) + return None + self.tick += 1 + k.used = self.tick + return k.copy() + except: + return None + + def put(mut self, key: String, var k: Kept): + var size = k.size(key) + if self.max_bytes <= 0 or size > self.max_bytes: + return + if key in self.kept: + self._drop(key) + while self.bytes + size > self.max_bytes and len(self.kept) > 0: + self._drop(self._oldest()) + self.tick += 1 + k.used = self.tick + self.bytes += size + self.kept[key] = k^ + + def _oldest(self) -> String: + var best = String() + var at = -1 + for e in self.kept.items(): + if at < 0 or e.value.used < at: + at = e.value.used + best = e.key + return best^ + + def _drop(mut self, key: String): + try: + var k = self.kept.pop(key) + self.bytes -= k.size(key) + except: + pass + + +struct _Shared(Movable): + var lock: Mutex + var cache: DataCache + + def __init__(out self, max_bytes: Int) raises: + self.lock = Mutex() + self.cache = DataCache(max_bytes) + + +comptime _SharedPtr = Pointer[_Shared, MutUntrackedOrigin] + + +def _reason(status: Int) -> String: + return String("OK") if status == 200 else String("Not Found") + + +def cache_key(req: Request, path: String) -> String: + """What `req` is kept under, or empty if its answer isn't kept: a GET + data read at a named snapshot by a visitor not logged in.""" + if req.method != "GET" or not is_data_read(req.url) or exempt(req, path): + return String() + var snap = query_param(req.url, "snapshot") + if not snap: + return String() + try: + _ = Int(snap) + except: + return String() + var json = "application/json" in req.headers.get("Accept") + return String(req.url, "\n", "json" if json else "html") + + +@fieldwise_init +struct CacheHandle(Copyable, ImplicitlyCopyable, Movable): + """What a handler on any worker thread uses the cache with; the + `ResponseCache` that gave it out must outlive it.""" + + var shared: Int + var on: Bool + + def get(self, key: String) -> Optional[Response]: + if not self.on or not key: + return None + ref s = _SharedPtr(unsafe_from_address=self.shared)[] + s.lock.lock() + var k = s.cache.get(key, _now()) + s.lock.unlock() + if not k: + return None + ref got = k.value() + var resp = Response( + status=got.status, reason=_reason(got.status), body=got.body.copy() + ) + try: + for i in range(len(got.names)): + resp.headers.set(got.names[i], got.values[i]) + resp.headers.set(HIT_HEADER, "hit") + except: + pass + return resp^ + + def put(self, key: String, resp: Response): + """Keeps `resp` under `key`, if it's a 200 or a 404 that sets no + cookie.""" + if not self.on or not key: + return + if resp.status != 200 and resp.status != 404: + return + var names = List[String]() + var values = List[String]() + try: + if resp.headers.get("Set-Cookie"): + return + for n in materialize[_KEPT_HEADERS](): + var v = resp.headers.get(n) + if v: + names.append(String(n)) + values.append(v) + except: + return + var k = Kept(resp.status, resp.body.copy(), names^, values^, _now(), 0) + ref s = _SharedPtr(unsafe_from_address=self.shared)[] + s.lock.lock() + s.cache.put(key, k^) + s.lock.unlock() + + +struct ResponseCache(Movable): + """The cache every `CacheHandle` it gives out shares; it must outlive + the server whose handlers use them.""" + + var _shared: Int + var max_bytes: Int + + def __init__(out self) raises: + self.max_bytes = cache_bytes() + var p = unsafe_alloc[_Shared](1) + p.unsafe_write(_Shared(self.max_bytes)) + self._shared = Int(p) + + def handle(self) -> CacheHandle: + return CacheHandle(self._shared, self.max_bytes > 0) + + def __deinit__(deinit self): + var p = _SharedPtr(unsafe_from_address=self._shared) + p.unsafe_deinit_pointee() + p.unsafe_free() diff --git a/src/morca/_internal/web/limit.mojo b/src/morca/_internal/web/limit.mojo index 0869bcde..fd647f90 100644 --- a/src/morca/_internal/web/limit.mojo +++ b/src/morca/_internal/web/limit.mojo @@ -235,9 +235,13 @@ struct Gate(Copyable, ImplicitlyCopyable, Movable): var rate: Int var max_reads: Int - def check(self, req: Request, path: String) -> Optional[Response]: + def check( + self, req: Request, path: String, data_read: Bool = True + ) -> Optional[Response]: """The refusal for `req`, or None if it may go on. One that goes on - as a data read (`is_data_read`) must be given back with `done`.""" + as a data read (`is_data_read`) must be given back with `done`; + `data_read=False` counts it toward the rate only (an answer the + UI kept, `morca._internal.web.cache`).""" if exempt(req, path): return None ref t = _TablePtr(unsafe_from_address=self.table)[] @@ -245,7 +249,7 @@ struct Gate(Copyable, ImplicitlyCopyable, Movable): var v = admit( t, client(req), - is_data_read(req.url), + data_read and is_data_read(req.url), self.rate, self.max_reads, _now(), diff --git a/src/morca/_internal/web/server.mojo b/src/morca/_internal/web/server.mojo index 8d306f59..12944725 100644 --- a/src/morca/_internal/web/server.mojo +++ b/src/morca/_internal/web/server.mojo @@ -91,6 +91,7 @@ from morca._internal.web.auth import ( token_refused, ) from morca._internal.web.drain import Drain +from morca._internal.web.cache import CacheHandle, ResponseCache, cache_key from morca._internal.web.limit import Gate, Limits from morca._internal.web.turnstile import ( VERIFY_PATH, @@ -262,6 +263,8 @@ struct Ui(Copyable, Handler): """The limits per client (`morca._internal.web.limit`).""" var turnstile: Turnstile """The check before a data read (`morca._internal.web.turnstile`).""" + var cache: CacheHandle + """The data reads kept (`morca._internal.web.cache`).""" def __init__( out self, @@ -269,21 +272,35 @@ struct Ui(Copyable, Handler): draining: AtomicFlag, gate: Gate, var turnstile: Turnstile, + cache: CacheHandle, ): self.url = url self.table = routes() self.draining = draining self.gate = gate self.turnstile = turnstile^ + self.cache = cache def serve(self, req: Request) raises -> Response: var path = _path_of(req.url) + var key = cache_key(req, path) + # A kept read is for those who passed Turnstile, if it's on. + if key and not self.turnstile.asked(req, path): + var kept = self.cache.get(key) + if kept: + # Counted toward the rate, but no data-read slot. + var no = self.gate.check(req, path, data_read=False) + if no: + return no.take() + return kept.take() var refused = self.gate.check(req, path) if refused: return refused.take() try: var resp = self._answer(req, path) self.gate.done(req, path) + if key: + self.cache.put(key, resp) return resp^ except e: self.gate.done(req, path) @@ -512,6 +529,7 @@ def serve_ui(url: String, addr: String, worker_addr: String = "") raises: _ = setenv("FLARE_REUSEPORT_WORKERS", "0") var limits = Limits(workers) var turnstile = Turnstile() + var cache = ResponseCache() print( "morca ui on http://", addr, @@ -524,15 +542,27 @@ def serve_ui(url: String, addr: String, worker_addr: String = "") raises: ", ", limits.rate, " requests a minute a client" ) if limits.rate else String(), ", Turnstile before data reads" if turnstile.on() else "", + String( + ", ", cache.max_bytes // (1024 * 1024), " MB of data reads kept" + ) if cache.max_bytes else String(), ")", sep="", ) server.serve( Logged( - drain.counting(Ui(url, drain.draining(), limits.gate(), turnstile^)) + drain.counting( + Ui( + url, + drain.draining(), + limits.gate(), + turnstile^, + cache.handle(), + ) + ) ), workers, ) _ = drain^ _ = limits^ + _ = cache^ print("morca ui: stopped") diff --git a/src/morca/_internal/web/turnstile.mojo b/src/morca/_internal/web/turnstile.mojo index 5efb810f..500e0eff 100644 --- a/src/morca/_internal/web/turnstile.mojo +++ b/src/morca/_internal/web/turnstile.mojo @@ -198,6 +198,11 @@ struct Turnstile(Copyable, Movable): return False return not valid(self.key, cookie(req, HUMAN_COOKIE), _now()) + def asked(self, req: Request, path: String) -> Bool: + """Whether `req` must pass the check before its data read (a kept + one too, `morca._internal.web.cache`).""" + return self._asked(req, path) + def needs_page(self, req: Request, path: String) -> Bool: """Whether `req` gets the widget's page (`challenge_page`).""" return self._asked(req, path) and not _wants_text(req, path) diff --git a/src/morca/_lake/dataread.mojo b/src/morca/_lake/dataread.mojo index c7f1fbb6..492dfe99 100644 --- a/src/morca/_lake/dataread.mojo +++ b/src/morca/_lake/dataread.mojo @@ -55,6 +55,10 @@ from morca._lake.dataask import DataAsk, columns_of, iso_ms, parse_ask from morca._lake.datarows import chart_json, csv_json, rows_json from morca._lake.impl import load_dataset +comptime MISSING_SNAPSHOT = "morca: the table has no snapshot " +"""What a read at a snapshot the table hasn't says; `data_text` answers it +with a status of 404.""" + comptime SNAPSHOTS_SHOWN = 100 comptime PARTITIONS_SHOWN = 1000 comptime MANIFEST_BYTES = 256 << 20 @@ -513,6 +517,10 @@ def _data_json(a: DataAsk, mut disk: DataCache) raises -> String: var out = String('{"dataset": ', json_quote(a.dataset), ", ") var scan = t.scan() if a.snapshot >= 0: + if t.metadata.snapshot_index(a.snapshot) < 0: + raise Error( + String(MISSING_SNAPSHOT, a.snapshot, " (expired, or never)") + ) scan = scan.use_snapshot(a.snapshot) if scan.has_any_snapshot() and scan.snapshot().manifest_list: _ = warm_manifests(t, scan.snapshot(), cache, disk) @@ -554,4 +562,7 @@ def data_text(args: List[String]) raises -> String: return data_json(a) except e: var msg = String(e) + # A snapshot the table hasn't: the UI's 404, not a mistake (400). + if msg.startswith(MISSING_SNAPSHOT): + return String('{"error": ', json_quote(msg), ', "status": 404}') return String('{"error": ', json_quote(msg), "}") diff --git a/tests/dataview.sh b/tests/dataview.sh index a1719195..f20d61a1 100755 --- a/tests/dataview.sh +++ b/tests/dataview.sh @@ -269,6 +269,32 @@ echo "ok the download: CSV, quoted, capped at MORCA_DATA_DOWNLOAD_ROWS with wh [ "$(code "$base/datasets/lake.obs/data.json?f=n:lt:abc")" = 400 ] || fail "a number that isn't" [ "$(code "$base/datasets/lake.obs?tab=data&f=bad")" = 400 ] && grep -q 'id="rows"' build/dataview/body || fail "a bad filter's page" [ "$(code "$base/datasets/lake.nope?tab=data")" = 404 ] || fail "no such dataset" +# A snapshot the table hasn't (an expired one a crawler asks again): 404. +[ "$(code "$base/datasets/lake.obs?tab=data&snapshot=12345")" = 404 ] && grep -qF 'the table has no snapshot 12345' build/dataview/body || fail "a missing snapshot's page: $(grep -o 'notice[^<]*<' build/dataview/body)" +[ "$(code "$base/datasets/lake.obs/data.json?snapshot=12345")" = 404 ] || fail "a missing snapshot's JSON" +[ "$(code "$base/datasets/lake.obs/data.csv?snapshot=12345")" = 404 ] || fail "a missing snapshot's CSV" +echo "ok a snapshot the table hasn't is 404" + +# A read at a named snapshot is kept: asked again, it's the same answer, at once. +hdr=build/dataview/kept.hdr +curl -fsS -D "$hdr" -o build/dataview/kept1 "$base/datasets/lake.obs/data.json?snapshot=$S1&c=city" || fail "the first read" +! grep -qi '^x-morca-cache:' "$hdr" || fail "the first read was kept already" +curl -fsS -D "$hdr" -o build/dataview/kept2 "$base/datasets/lake.obs/data.json?snapshot=$S1&c=city" || fail "the second read" +grep -qi '^x-morca-cache: hit' "$hdr" || fail "the second read wasn't kept: $(cat "$hdr")" +cmp -s build/dataview/kept1 build/dataview/kept2 || fail "the kept answer differs" +[ "$(code -D "$hdr" "$base/datasets/lake.obs/data.json?snapshot=12345")" = 404 ] && grep -qi '^x-morca-cache: hit' "$hdr" || fail "the 404 wasn't kept" +# Not without a snapshot (the current one moves), nor for someone logged in. +curl -fsS -o /dev/null "$base/datasets/lake.obs/data.json?c=city" +curl -fsS -D "$hdr" -o /dev/null "$base/datasets/lake.obs/data.json?c=city" || fail "a read at the current snapshot" +! grep -qi '^x-morca-cache:' "$hdr" || fail "a read at the current snapshot was kept" +curl -sS -D "$hdr" -o /dev/null -H "Cookie: morca_session=x" "$base/datasets/lake.obs/data.json?snapshot=$S1&c=city" +! grep -qi '^x-morca-cache:' "$hdr" || fail "a read with a session was answered from the cache" +stop_ui +start_ui MORCA_UI_DATA_CACHE_MB=0 +curl -fsS -o /dev/null "$base/datasets/lake.obs/data.json?snapshot=$S1" || fail "a read with no cache" +curl -fsS -D "$hdr" -o /dev/null "$base/datasets/lake.obs/data.json?snapshot=$S1" || fail "a read with no cache, again" +! grep -qi '^x-morca-cache:' "$hdr" || fail "MORCA_UI_DATA_CACHE_MB=0 kept a read" +echo "ok reads at a named snapshot are kept (200s and 404s), for visitors not logged in" stop_ui start_ui MORCA_DATA_MEMORY_MB=0.001 [ "$(json "sort=id" | jq -r .rows.stopped)" = "the scan stopped at its memory limit, 0.001 MB (MORCA_DATA_MEMORY_MB)" ] || fail "the memory limit: $(json sort=id | jq -c .rows.stopped)" diff --git a/tests/test_data_cache.mojo b/tests/test_data_cache.mojo new file mode 100644 index 00000000..f5f45d0d --- /dev/null +++ b/tests/test_data_cache.mojo @@ -0,0 +1,77 @@ +"""The UI's cache of data reads (`morca._internal.web.cache.DataCache`).""" + +from std.testing import TestSuite, assert_equal, assert_false, assert_true + +from morca._internal.web.cache import TTL_SECONDS, DataCache, Kept + + +def _kept(n: Int, status: Int = 200) -> Kept: + var body = List[UInt8]() + for _ in range(n): + body.append(UInt8(120)) + return Kept(status, body^, ["Content-Type"], ["text/html"], 1000, 0) + + +def test_hit() raises: + var c = DataCache(1 << 20) + assert_false(Bool(c.get("a", 1000))) + c.put("a", _kept(10)) + var k = c.get("a", 1001) + assert_true(Bool(k)) + assert_equal(k.value().status, 200) + assert_equal(len(k.value().body), 10) + assert_equal(k.value().values[0], "text/html") + + +def test_404_kept() raises: + var c = DataCache(1 << 20) + c.put("gone", _kept(5, 404)) + assert_equal(c.get("gone", 1000).value().status, 404) + + +def test_evicts_least_recently_used() raises: + # Room for two of these, not three. + var one = _kept(400).size("a") + var c = DataCache(one * 2 + one // 2) + c.put("a", _kept(400)) + c.put("b", _kept(400)) + _ = c.get("a", 1000) # b is now the least recently used + c.put("c", _kept(400)) + assert_true(Bool(c.get("a", 1000))) + assert_false(Bool(c.get("b", 1000))) + assert_true(Bool(c.get("c", 1000))) + assert_true(c.bytes <= c.max_bytes) + + +def test_replace_keeps_count() raises: + var c = DataCache(1 << 20) + c.put("a", _kept(100)) + var once = c.bytes + c.put("a", _kept(100)) + assert_equal(c.bytes, once) + assert_equal(len(c.kept), 1) + + +def test_too_big_not_kept() raises: + var c = DataCache(100) + c.put("a", _kept(1000)) + assert_false(Bool(c.get("a", 1000))) + assert_equal(c.bytes, 0) + + +def test_off_at_zero() raises: + var c = DataCache(0) + c.put("a", _kept(1)) + assert_false(Bool(c.get("a", 1000))) + + +def test_expires() raises: + var c = DataCache(1 << 20) + c.put("a", _kept(10)) + assert_true(Bool(c.get("a", 1000 + TTL_SECONDS))) + assert_false(Bool(c.get("a", 1001 + TTL_SECONDS))) + assert_equal(c.bytes, 0) + + +def main() raises: + TestSuite.discover_tests[__functions_in_module()]().run() diff --git a/tests/ui_drain.sh b/tests/ui_drain.sh index 288705f2..02bd0738 100755 --- a/tests/ui_drain.sh +++ b/tests/ui_drain.sh @@ -167,7 +167,7 @@ locker= wait "$slow_curl" 2> /dev/null || true # Two workers: a request gets its answer while the other waits on the lock. start 5 2 -grep -q "morca ui on http://127.0.0.1:$port (2 workers, 1 data reads at once)" "$log" || fail "no 2 workers message" +grep -q "morca ui on http://127.0.0.1:$port (2 workers, 1 data reads at once, 64 MB of data reads kept)" "$log" || fail "no 2 workers message" lock 3 slow fast=$(curl -s -o /dev/null -w '%{http_code} %{time_total}' --max-time 2 "$base/static/morca.css" 2> /dev/null || true) @@ -184,7 +184,7 @@ grep -Eq '^morca ui: [0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9:]{8}Z GET /runs 200 [0-9]+ # more go, the fourth in the minute is 429 with Retry-After, and one with a # session cookie isn't counted. MORCA_UI_RATE=3 start 1 -grep -q "3 requests a minute a client)" "$log" || fail "no rate in the start message" +grep -q "3 requests a minute a client, 64 MB of data reads kept)" "$log" || fail "no rate in the start message" for i in 1 2; do got=$(curl -s -o /dev/null -w '%{http_code}' "$base/runs") [ "$got" = 200 ] || fail "request $i under the rate got $got" @@ -207,7 +207,7 @@ stub=$! MORCA_UI_RATE=0 MORCA_SECRET_FETCH=echo MORCA_UI_TURNSTILE_SITEKEY=1x00000000000000000000AA \ MORCA_UI_TURNSTILE_SECRET=sec-ret MORCA_UI_TURNSTILE_HOSTNAMES=127.0.0.1 \ MORCA_UI_TURNSTILE_VERIFY_URL="http://127.0.0.1:$vport/siteverify" start 1 -grep -q "Turnstile before data reads)" "$log" || { kill "$stub"; fail "no Turnstile in the start message"; } +grep -q "Turnstile before data reads, 64 MB of data reads kept)" "$log" || { kill "$stub"; fail "no Turnstile in the start message"; } read_url="/datasets/toy.events?tab=data" got=$(curl -s -o build/ui-drain-ts.html -w '%{http_code}' "$base$read_url") [ "$got" = 403 ] || { kill "$stub"; fail "a data read without the cookie got $got"; } From a6204e588106ebda76abd2624ca3755da79eb60c Mon Sep 17 00:00:00 2001 From: Marius S <39998+winding-lines@users.noreply.github.com> Date: Thu, 19 Nov 2026 06:14:46 -0800 Subject: [PATCH 2/3] changes: 548 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_017TycszSbyw92Efu4ZeCYL6 --- changes/{0-ui-data-cache.minor.md => 548-ui-data-cache.minor.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename changes/{0-ui-data-cache.minor.md => 548-ui-data-cache.minor.md} (100%) diff --git a/changes/0-ui-data-cache.minor.md b/changes/548-ui-data-cache.minor.md similarity index 100% rename from changes/0-ui-data-cache.minor.md rename to changes/548-ui-data-cache.minor.md From 812b6396de453e1550e12e10c9a161d059ff80f9 Mon Sep 17 00:00:00 2001 From: Marius S <39998+winding-lines@users.noreply.github.com> Date: Thu, 19 Nov 2026 06:57:21 -0800 Subject: [PATCH 3/3] tests/ui_drain.sh: wait for the siteverify stub before the UI asks it Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_017TycszSbyw92Efu4ZeCYL6 --- tests/ui_drain.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/ui_drain.sh b/tests/ui_drain.sh index 02bd0738..0f3fdba1 100755 --- a/tests/ui_drain.sh +++ b/tests/ui_drain.sh @@ -204,6 +204,11 @@ verified=build/ui-drain-siteverify.jsonl : > "$verified" python3 tests/fake_siteverify.py "$vport" "$verified" >> "$log" 2>&1 & stub=$! +# Listening before the UI asks it (curl's 7: couldn't connect). +i=0 +while curl -s -o /dev/null "http://127.0.0.1:$vport/"; [ $? -eq 7 ]; do + i=$((i + 1)); [ "$i" -lt 100 ] || { kill "$stub"; fail "the siteverify stub didn't start"; }; sleep 0.1 +done MORCA_UI_RATE=0 MORCA_SECRET_FETCH=echo MORCA_UI_TURNSTILE_SITEKEY=1x00000000000000000000AA \ MORCA_UI_TURNSTILE_SECRET=sec-ret MORCA_UI_TURNSTILE_HOSTNAMES=127.0.0.1 \ MORCA_UI_TURNSTILE_VERIFY_URL="http://127.0.0.1:$vport/siteverify" start 1