Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/548-ui-data-cache.minor.md
Original file line number Diff line number Diff line change
@@ -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.
16 changes: 16 additions & 0 deletions docs/ui.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
3 changes: 2 additions & 1 deletion pixi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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" }
Expand Down
1 change: 1 addition & 0 deletions scripts/unit-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions src/morca/_internal/views/data.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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)
Expand Down
252 changes: 252 additions & 0 deletions src/morca/_internal/web/cache.mojo
Original file line number Diff line number Diff line change
@@ -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()
10 changes: 7 additions & 3 deletions src/morca/_internal/web/limit.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -235,17 +235,21 @@ 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)[]
t.lock.lock()
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(),
Expand Down
Loading
Loading