Files
romm/backend/handler/redis_handler.py
Georges-Antoine Assi ec1684d2af perf(metadata): serve the metadata dumps and fixtures from SQLite files (#5844)
* perf(metadata): serve the metadata dumps and fixtures from SQLite files

The LaunchBox and Switch TitleDB stores sat in valkey, which keeps every
byte resident: about 300MB on an instance with both imported, against
roughly 1MB for an empty server. Every read is an exact-key lookup and
nothing writes between imports, so they now live in read-only SQLite
files under RESOURCES_BASE_PATH/metadata, where the OS page cache holds
only the pages lookups touch.

A build writes a temporary file and renames it over the previous one,
so readers see the old store or the new one and never a partial import.
That replaces the initial-import flag and the schema stamp: the store
revision is part of the file name. A failed import keeps serving the
previous file.

The startup fixture indexes (MAME, ScummVM, Sony serials, known BIOS)
no longer get copied into valkey on every boot. They build on first use
into a file under the cache path, named after a hash of the JSON.

Startup deletes the old valkey keys and any file of an older revision,
and queues a rebuild when a store had data, so an upgrade refills
LaunchBox and TitleDB on its own. nginx returns 404 for the metadata
folder, since /assets/romm/resources is otherwise served without auth.

Measured against the real dumps: LaunchBox builds in 22s into 263MB on
disk, TitleDB in 1.2s into 54MB, the fixtures in 0.2s into 8MB. A hit
costs 10-23us including decompression, a miss about 7us.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: code-review

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: simplify

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: review-polish

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: prose

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: prose (code-review fixes)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Keep the previous metadata store when a dump comes out empty

A LaunchBox or TitleDB build that writes no rows to its main table now
fails with EmptyDumpError instead of swapping an empty store over a
populated one.

Each store keeps one connection per process behind a lock instead of one
per thread. A build closes it once it swaps the file in, and any other
process closes its own on its next lookup, so a replaced file's disk
space is released instead of held by idle threads.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Address Greptile review: trim DumpStore comments

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Restore valkey's own snapshot policy by default

The hourly default (48259b3a9, #3980) kept valkey from rewriting the
hundreds of MB of LaunchBox and TitleDB data it held. With those moved to
SQLite files, a snapshot holds only sessions, queues and query caches, so
an unset REDIS_SAVE_POLICY now leaves valkey's stock policy in place and
loses less on a crash. An empty value still disables snapshots.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 08:49:15 -05:00

289 lines
8.8 KiB
Python

import asyncio
import os
import sys
import threading
from collections.abc import AsyncIterator, Iterable
from contextlib import asynccontextmanager
from enum import Enum
from typing import Any, Final, cast
from uuid import uuid4
from redis import Redis
from redis.asyncio import Redis as AsyncRedis
from redis.maint_notifications import MaintNotificationsConfig
from rq import Queue, Worker
from rq.exceptions import DeserializationError, InvalidJobOperation, NoSuchJobError
from rq.job import Job, JobStatus
from rq.worker import BaseWorker, WorkerStatus
from config import IS_PYTEST_RUN, REDIS_URL
from logger.logger import log
class QueuePrio(Enum):
HIGH = "high"
DEFAULT = "default"
LOW = "low"
# Scans have a queue and a worker of their own: a library scan runs for hours,
# and one worker on one queue keeps two of them from ever running at once.
SCAN_QUEUE_NAME: Final = "scans"
# Streaming teardowns and exit save pulls get one too: a sick broker can hold
# either for minutes.
STREAMING_QUEUE_NAME: Final = "streaming"
# Maintenance notifications are a Redis Enterprise/Cloud feature. Left on
# "auto", every new connection probes for it and fails on Redis < 8.2 and Valkey.
REDIS_CLIENT_OPTIONS: Final[dict[str, Any]] = {
"maint_notifications_config": MaintNotificationsConfig(enabled=False),
}
class RomMRedis(Redis):
"""`Redis` with `REDIS_CLIENT_OPTIONS`, for the RQ CLI's `--connection-class`."""
@classmethod
def from_url(cls, url: str, **kwargs: Any) -> Redis:
return super().from_url(url, **{**REDIS_CLIENT_OPTIONS, **kwargs})
redis_client = Redis.from_url(REDIS_URL, **REDIS_CLIENT_OPTIONS)
high_prio_queue = Queue(name=QueuePrio.HIGH.value, connection=redis_client)
default_queue = Queue(name=QueuePrio.DEFAULT.value, connection=redis_client)
low_prio_queue = Queue(name=QueuePrio.LOW.value, connection=redis_client)
scan_queue = Queue(name=SCAN_QUEUE_NAME, connection=redis_client)
streaming_queue = Queue(name=STREAMING_QUEUE_NAME, connection=redis_client)
ALL_QUEUES: Final = (
scan_queue,
streaming_queue,
high_prio_queue,
default_queue,
low_prio_queue,
)
QUEUES_BY_NAME: Final = {queue.name: queue for queue in ALL_QUEUES}
def __get_fake_server() -> Any:
# Only import fakeredis when running tests, as it is a test dependency.
from fakeredis import FakeServer
# One keyspace for both caches, as one Redis serves both outside tests, so
# a flush between tests clears what either of them wrote.
return FakeServer(version=(7,))
_fake_server = __get_fake_server() if IS_PYTEST_RUN else None
def __get_sync_cache() -> Redis:
if IS_PYTEST_RUN:
from fakeredis import FakeRedis
return FakeRedis(server=_fake_server)
# A separate client that auto-decodes responses is needed
client = Redis.from_url(REDIS_URL, decode_responses=True, **REDIS_CLIENT_OPTIONS)
log.debug(
f"Sync redis/valkey connection established in {os.path.splitext(os.path.basename(sys.argv[0]))[0]}"
)
return client
_LOOP_CLIENT_ATTR: Final = "_romm_fake_async_redis"
class _PerLoopFakeAsyncRedis:
"""A fake async client per event loop, or per thread outside one, over one fake server."""
# The TestClient's loop and a test's asyncio.run loop use the cache at once,
# and an asyncio.Lock in a pool they share binds to just one of them.
def __init__(self, server: Any) -> None:
self._server = server
self._by_thread = threading.local()
def _new_client(self) -> Any:
from fakeredis import FakeAsyncRedis
return FakeAsyncRedis(server=self._server)
def _client(self) -> Any:
try:
loop = asyncio.get_running_loop()
except RuntimeError:
client = getattr(self._by_thread, "client", None)
if client is None:
client = self._by_thread.client = self._new_client()
return client
# Held on the loop, not in a map keyed by it: the client's asyncio
# objects reference the loop, so such a map would never let one go.
client = getattr(loop, _LOOP_CLIENT_ATTR, None)
if client is None:
client = self._new_client()
setattr(loop, _LOOP_CLIENT_ATTR, client)
return client
def __getattr__(self, name: str) -> Any:
return getattr(self._client(), name)
def __get_async_cache() -> AsyncRedis:
if IS_PYTEST_RUN:
return cast(AsyncRedis, _PerLoopFakeAsyncRedis(_fake_server))
# A separate client that auto-decodes responses is needed
client = AsyncRedis.from_url(
REDIS_URL, decode_responses=True, **REDIS_CLIENT_OPTIONS
)
log.debug(
f"Async redis/valkey connection established in {os.path.splitext(os.path.basename(sys.argv[0]))[0]}"
)
return client
sync_cache = __get_sync_cache()
async_cache = __get_async_cache()
@asynccontextmanager
async def redis_lock(
key: str,
*,
timeout_seconds: int,
poll_seconds: float = 0.1,
lease_seconds: int | None = None,
) -> AsyncIterator[None]:
"""Hold `key` as a mutex across gunicorn workers, via SET NX (no Lua needed).
Args:
lease_seconds: How long the key outlives a holder that never releases it,
`timeout_seconds` by default
Raises:
TimeoutError: The key stayed held for `timeout_seconds`.
"""
token = uuid4().hex
for _ in range(int(timeout_seconds / poll_seconds)):
if await async_cache.set(
key, token, nx=True, ex=lease_seconds or timeout_seconds
):
break
await asyncio.sleep(poll_seconds)
else:
raise TimeoutError(f"Timed out waiting for lock {key}")
try:
yield
finally:
# Only the owner releases; an expired lock may belong to someone else.
held = await async_cache.get(key)
if held in (token, token.encode()):
await async_cache.delete(key)
def as_text(value: bytes | str) -> str:
"""A cached value as text, since the fake caches return bytes where Redis decodes."""
return value.decode() if isinstance(value, bytes) else value
def get_job_func_name(job: Job, fallback: str = "") -> str:
"""Safely get the function name from an RQ job, handling DeserializationError.
Args:
job: The RQ Job object to get the function name from
fallback: The value to return if deserialization fails
Returns:
The function name if available, otherwise the fallback value
"""
try:
return job.func_name or fallback
except DeserializationError:
# Job data cannot be deserialized (e.g., function no longer exists)
return fallback
def get_job_status(job: Job, refresh: bool = True) -> JobStatus | None:
"""Safely get the status of an RQ job, which is gone once its hash expires.
Args:
job: The RQ Job object to get the status of
refresh: Whether to re-read the status, rather than trust the one the
job was fetched with
Returns:
The job status, or None if the job no longer has one
"""
try:
return job.get_status(refresh=refresh)
except InvalidJobOperation:
return None
def get_job_kwargs(job: Job) -> dict[str, Any] | None:
"""Safely get the keyword arguments an RQ job was enqueued with.
Args:
job: The RQ Job object to read
Returns:
The keyword arguments, or None if the payload cannot be deserialized
"""
try:
return job.kwargs
except DeserializationError:
return None
def cancel_job(job: Job) -> bool:
"""Cancel an RQ job, tolerating one that is already cancelled.
Args:
job: The RQ Job object to cancel
Returns:
Whether this call was the one that cancelled it
"""
try:
job.cancel()
except InvalidJobOperation:
return False
return True
def get_worker_current_job(worker: BaseWorker) -> Job | None:
"""Safely get the job a worker is holding, which can be gone before the
worker's own registration expires.
Args:
worker: The RQ Worker to read
Returns:
The job the worker is running, or None if it has none or it is gone
"""
try:
return worker.get_current_job()
except NoSuchJobError:
return None
def has_live_worker(queue: Queue, workers: Iterable[BaseWorker] | None = None) -> bool:
"""Whether a job enqueued on ``queue`` would be picked up.
Args:
workers: Every registered worker, when the caller has already listed them
"""
listening = (
Worker.all(queue=queue)
if workers is None
else [worker for worker in workers if queue.name in worker.queue_names()]
)
# A worker that crashed without announcing it stays registered until its
# key TTL lapses, so this can still say yes for a few minutes after a kill.
return any(
worker.death_date is None and worker.get_state() != WorkerStatus.SUSPENDED
for worker in listening
)