|
| 1 | +""" |
| 2 | +Valkey client for TUS resumable-upload sessions. |
| 3 | +
|
| 4 | +Stores per-session upload metadata (the source of truth) on the Glide-based |
| 5 | +valkey connection so that multiple Storage Proxy replicas share one view of an |
| 6 | +upload's progress without relying on shared-filesystem semantics. This client is |
| 7 | +metadata-only; the per-session distributed lock that serializes the |
| 8 | +read-modify-write window is a separate :class:`DistributedLockFactory` resource |
| 9 | +(see :mod:`ai.backend.storage.services.upload.tus_session`). The payload bytes |
| 10 | +stay on the shared filesystem; only this small metadata lives in Valkey. |
| 11 | +""" |
| 12 | + |
| 13 | +from __future__ import annotations |
| 14 | + |
| 15 | +import logging |
| 16 | +from typing import Final, Self |
| 17 | + |
| 18 | +from glide import ExpirySet, ExpiryType |
| 19 | + |
| 20 | +from ai.backend.common.clients.valkey_client.client import ( |
| 21 | + AbstractValkeyClient, |
| 22 | + create_valkey_client, |
| 23 | +) |
| 24 | +from ai.backend.common.exception import BackendAIError |
| 25 | +from ai.backend.common.metrics.metric import DomainType, LayerType |
| 26 | +from ai.backend.common.resilience import ( |
| 27 | + BackoffStrategy, |
| 28 | + MetricArgs, |
| 29 | + MetricPolicy, |
| 30 | + Resilience, |
| 31 | + RetryArgs, |
| 32 | + RetryPolicy, |
| 33 | +) |
| 34 | +from ai.backend.common.types import TusSessionId, ValkeyTarget |
| 35 | +from ai.backend.logging import BraceStyleAdapter |
| 36 | + |
| 37 | +log = BraceStyleAdapter(logging.getLogger(__spec__.name)) |
| 38 | + |
| 39 | +valkey_tus_resilience = Resilience( |
| 40 | + policies=[ |
| 41 | + MetricPolicy(MetricArgs(domain=DomainType.VALKEY, layer=LayerType.VALKEY_TUS)), |
| 42 | + RetryPolicy( |
| 43 | + RetryArgs( |
| 44 | + max_retries=3, |
| 45 | + retry_delay=0.1, |
| 46 | + backoff_strategy=BackoffStrategy.FIXED, |
| 47 | + non_retryable_exceptions=(BackendAIError,), |
| 48 | + ) |
| 49 | + ), |
| 50 | + ] |
| 51 | +) |
| 52 | + |
| 53 | +_STATE_KEY_PREFIX: Final = "tus.upload.session" # tus.upload.session:{session_id} |
| 54 | + |
| 55 | +# Stale (never-completed / abandoned) sessions are reclaimed by this TTL, so no |
| 56 | +# separate GC sweep is needed. |
| 57 | +_DEFAULT_STATE_TTL_SECONDS: Final = 24 * 60 * 60 |
| 58 | + |
| 59 | + |
| 60 | +class ValkeyTusClient: |
| 61 | + """Valkey-backed metadata store for TUS uploads.""" |
| 62 | + |
| 63 | + _client: AbstractValkeyClient |
| 64 | + |
| 65 | + def __init__(self, client: AbstractValkeyClient) -> None: |
| 66 | + self._client = client |
| 67 | + |
| 68 | + @classmethod |
| 69 | + async def create( |
| 70 | + cls, |
| 71 | + valkey_target: ValkeyTarget, |
| 72 | + *, |
| 73 | + db_id: int, |
| 74 | + human_readable_name: str, |
| 75 | + ) -> Self: |
| 76 | + client = create_valkey_client( |
| 77 | + valkey_target=valkey_target, |
| 78 | + db_id=db_id, |
| 79 | + human_readable_name=human_readable_name, |
| 80 | + ) |
| 81 | + await client.connect() |
| 82 | + return cls(client=client) |
| 83 | + |
| 84 | + @valkey_tus_resilience.apply() |
| 85 | + async def close(self) -> None: |
| 86 | + await self._client.disconnect() |
| 87 | + |
| 88 | + @staticmethod |
| 89 | + def _state_key(session_id: TusSessionId) -> str: |
| 90 | + return f"{_STATE_KEY_PREFIX}:{session_id}" |
| 91 | + |
| 92 | + @valkey_tus_resilience.apply() |
| 93 | + async def get_session_state(self, session_id: TusSessionId) -> bytes | None: |
| 94 | + """Return the raw serialized session state, or ``None`` if absent.""" |
| 95 | + async with self._client.client() as conn: |
| 96 | + return await conn.get(self._state_key(session_id)) |
| 97 | + |
| 98 | + @valkey_tus_resilience.apply() |
| 99 | + async def set_session_state( |
| 100 | + self, |
| 101 | + session_id: TusSessionId, |
| 102 | + payload: str | bytes, |
| 103 | + *, |
| 104 | + ttl_seconds: int = _DEFAULT_STATE_TTL_SECONDS, |
| 105 | + ) -> None: |
| 106 | + async with self._client.client() as conn: |
| 107 | + await conn.set( |
| 108 | + self._state_key(session_id), |
| 109 | + payload, |
| 110 | + expiry=ExpirySet(ExpiryType.SEC, ttl_seconds), |
| 111 | + ) |
0 commit comments