Repository navigation
Phase 1: edge cache for zarr data plane - #1199
neuromechanist merged 4 commits into
Conversation
Range-keyed cache entries (single bytes-range, synthetic 200 storage for the 206 the Cache API refuses), a canonical origin+path+v cache key, tokened Cache-Control (zarr.json/index.json get a day when ?v= is present, index.json's untokened TTL rises 60s -> 3600s), edge-cached missing-object 404s, and a deps seam (cache/fetch/s3Base) so backend/test/zarr-data-cache.test.ts can drive the real route against a real Bun.serve() upstream and an in-memory cache. Addresses #1178 (items 3, 4) and #1035. Part of epic #1181.
📦 Package PreviewYour package is ready to test! bun install -g nemar-cli@PR1199Version: 🤖 Updated on each push. |
Correctness: gate isPublicDataset() before every cache lookup, not only on miss, so a dataset flipped private stops serving immediately instead of up to a day later; a ranged 404 lookup now falls back to the canonical negative-cache entry so a ranged probe storm converges; a 206 with no Content-Range fails closed (stream through, warn, don't guess); cached entries get a size cap (8 MiB range / 32 MiB full), checked from Content-Length before any clone/put; 416 now passes through as 416 with Content-Range instead of collapsing into 502; cache.put failures are caught and logged instead of risking an unhandled rejection under waitUntil; app.onError logs and still applies CORS instead of a bare 500. Cache key and TTLs: canonicalCacheUrl now derives the key from the normalised S3 key (each segment encodeURIComponent'd) instead of the raw request pathname, so differently percent-encoded spellings of the same object collapse onto one entry; untokened index.json drops to 300s/3600s (was 3600s/86400s) with a corrected comment on why (the zone id is unset in prod today, so the purge is a no-op and the TTL is the only bound); the chunk-purge comment now says zarrPurgeTargets never targets a chunk URL. Types: NormalizedRange/CanonicalCacheUrl are branded strings; cacheControlFor takes an options object; the test InMemoryCache throws on a 206 (mirroring the real Cache API) with a comment on why only 206. Tests: 45 (was 29) in zarr-data-cache.test.ts, covering every item above plus a v-token round trip, telemetry via a real binding-shaped collector, four concurrent identical range requests, HEAD with a Range header, and an upstream that ignores a well-formed single range. Part of epic #1181 Addresses #1178 (items 3, 4) and #1035
|
Addressed all 18 items from the five-reviewer follow-up round (correctness 1-7, cache key/TTL 8-10, types/structure 11-13, new tests 14-18, plus the docstring wording fixes) in commit e83deb3. Correctness
Cache key and TTLs
Types and structure
New tests (backend/test/zarr-data-cache.test.ts, 45 tests total, up from 29)
Also added a per-key upstream quirk switch to the fake Gates
One implementation note, not a deviation: item 13's instruction text second-guessed itself mid-sentence (starts with "throw on |
c9949c0
into
feature/issue-1181-epic-zarr-serving
Summary
bytes=range gets itsown cache key (
canonicalCacheUrl+__cr=<normalised range>); theWorkers Cache API refuses to store a 206, so an accepted range is written
as a synthetic 200 (
X-Nemar-Content-Rangecarrying the realContent-Range) and rebuilt into a real 206 on the way back out. Multi-range, malformed, and non-bytes Range headers pass through uncached exactly
as before. 416 and any other non-206 answer to an accepted range is never
cached.
vquery parameter,dropping every other query parameter (including a client-supplied
__cr,which would otherwise let a request poison the range cache).
cacheControlForgrows atokenedargument: a?v=tokened request forzarr.json/index.jsongetsmax-age=86400; untokenedzarr.jsonisunchanged at
max-age=60, and untokenedindex.jsonrises from 60s to3600s (safe because the zarr-ready callback already purges it on rebuild).
Chunks are unchanged.
Cache-Control: public, max-age=60; the missing-object 404 (upstream 403/404 on a public dataset)is additionally cached at the edge under the canonical key so a probe
storm for a missing object stops there. Bad-path and private-dataset 404s
are not cached.
createZarrDataRoutes(deps)factory with aZarrDataDepsseam(
cache,fetch,s3Base) so tests can supply a real in-memory cache anda real local upstream;
zarrDataRoutes = createZarrDataRoutes()keepsindex.tsuntouched.Measured on live zarr.nemar.org (#1178): every level-0 read is a Range
request that skipped the edge cache entirely, costing ~439 KB of per-user S3
round trips per 10s window; index.json alone is 95% of Worker egress (#1035)
at a 60s TTL despite being purged on every re-conversion.
Test plan
backend/test/zarr-data-cache.test.ts(29 tests): realBun.serve()upstream with genuine single-range semantics (206/416/200/HEAD/ETag/404),
a real in-memory
CacheLike, and D1 viafreshDb()/realD1()with onepublic and one private dataset, driven through the real Hono app via
app.request(). Covers: ranged miss-then-hit, distinct ranges as separateentries, full GET after a ranged GET, a client-supplied
__crbeingignored for keying, multi-range never cached, 416 never cached, tokened vs
untokened
Cache-Controlforzarr.json/index.json, HEAD nevertouching the cache, CORS reapplied per requesting origin on a hit
(including a range hit), missing-object 404 caching vs private-dataset
404s never being cached, and
canonicalCacheUrl/parseCacheableRangeunit tests.
cd backend && bun test— full backend suite: 1357 pass, 0 fail.bun run lint(Biome, rootsrc/) andbunx biome checkon the twochanged backend files: clean.
bun run typecheck(roottsc --noEmit+tsc --noEmit -p backend):clean.
disabling 404 edge-caching, disabling CORS reapplication on a hit,
forcing
tokenedto always false, and skipping the synthetic-200->206restore each independently broke the corresponding test(s); reverted
after each check (
git status --porcelainclean).Part of epic #1181
Addresses #1178 (items 3, 4) and #1035