"lib/protocols/src/project_service_accounts.rs" did not exist on "199b9a30f4ecfb896f70e30ae085d99dcd162f7f"
gms.py 13.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

"""Top-level server-side GMS service."""

from __future__ import annotations

import hashlib
import logging
from collections import deque
from dataclasses import dataclass
from typing import Callable, Optional

14
from gpu_memory_service.common.locks import GrantedLockType, RequestedLockType
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from gpu_memory_service.common.protocol.messages import (
    AllocateRequest,
    AllocateResponse,
    CommitRequest,
    CommitResponse,
    ExportAllocationRequest,
    ExportAllocationResponse,
    FreeAllocationRequest,
    FreeAllocationResponse,
    GetAllocationRequest,
    GetAllocationResponse,
    GetAllocationStateRequest,
    GetAllocationStateResponse,
    GetEventHistoryResponse,
    GetLockStateRequest,
    GetLockStateResponse,
    GetRuntimeStateResponse,
    GetStateHashRequest,
    GetStateHashResponse,
    GMSRuntimeEvent,
    ListAllocationsRequest,
    ListAllocationsResponse,
    MetadataDeleteRequest,
    MetadataDeleteResponse,
    MetadataGetRequest,
    MetadataGetResponse,
    MetadataListRequest,
    MetadataListResponse,
    MetadataPutRequest,
    MetadataPutResponse,
)

from .allocations import AllocationInfo, GMSAllocationManager
48
49
from .fsm import Connection, ServerState, StateEvent
from .session import GMSSessionManager
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409

logger = logging.getLogger(__name__)


@dataclass(frozen=True)
class MetadataEntry:
    allocation_id: str
    offset_bytes: int
    value: bytes


class GMS:
    """Owns all non-transport server state."""

    _MAX_EVENTS = 256

    def __init__(
        self,
        device: int = 0,
        *,
        allocation_retry_interval: float = 0.5,
        allocation_retry_timeout: Optional[float] = None,
    ):
        self._allocations = GMSAllocationManager(
            device,
            allocation_retry_interval=allocation_retry_interval,
            allocation_retry_timeout=allocation_retry_timeout,
        )
        self._sessions = GMSSessionManager()
        self._events: deque[GMSRuntimeEvent] = deque(maxlen=self._MAX_EVENTS)
        self._metadata: dict[str, MetadataEntry] = {}
        self._memory_layout_hash = ""
        logger.info("GMS initialized: device=%d", device)

    @property
    def state(self) -> ServerState:
        return self._sessions.state

    @property
    def committed(self) -> bool:
        return self._sessions.snapshot().committed

    @property
    def allocation_count(self) -> int:
        return self._allocations.allocation_count

    def is_ready(self) -> bool:
        return self._sessions.snapshot().is_ready

    def get_runtime_state(self) -> GetRuntimeStateResponse:
        session = self._sessions.snapshot()
        return GetRuntimeStateResponse(
            state=session.state.name,
            has_rw_session=session.has_rw_session,
            ro_session_count=session.ro_session_count,
            waiting_writers=session.waiting_writers,
            committed=session.committed,
            is_ready=session.is_ready,
            allocation_count=self._allocations.allocation_count,
            memory_layout_hash=self._memory_layout_hash,
        )

    def get_event_history(self) -> GetEventHistoryResponse:
        return GetEventHistoryResponse(events=list(self._events))

    def next_session_id(self) -> str:
        return self._sessions.next_session_id()

    async def acquire_lock(
        self,
        mode: RequestedLockType,
        timeout_ms: int | None,
        session_id: str,
    ) -> GrantedLockType | None:
        return await self._sessions.acquire_lock(mode, timeout_ms, session_id)

    async def cancel_connect(
        self,
        session_id: str,
        mode: GrantedLockType | None,
    ) -> None:
        await self._sessions.cancel_connect(session_id, mode)

    def _validate_metadata_target(
        self,
        allocation: AllocationInfo,
        offset_bytes: int,
    ) -> None:
        if offset_bytes < 0:
            raise ValueError(f"offset_bytes must be >= 0, got {offset_bytes}")
        if offset_bytes >= allocation.aligned_size:
            raise ValueError(
                f"offset_bytes {offset_bytes} out of range for allocation {allocation.allocation_id} "
                f"(aligned_size={allocation.aligned_size})"
            )

    def _drop_metadata_for_allocation(self, allocation_id: str) -> int:
        keys_to_remove = [
            key
            for key, entry in self._metadata.items()
            if entry.allocation_id == allocation_id
        ]
        for key in keys_to_remove:
            self._metadata.pop(key, None)
        return len(keys_to_remove)

    def _validate_metadata_integrity(
        self,
        allocations_by_id: dict[str, AllocationInfo],
    ) -> None:
        for key, entry in self._metadata.items():
            info = allocations_by_id.get(entry.allocation_id)
            if info is None:
                raise AssertionError(
                    f"Metadata key {key!r} references missing allocation "
                    f"{entry.allocation_id!r}"
                )

            if entry.offset_bytes < 0 or entry.offset_bytes >= info.aligned_size:
                raise AssertionError(
                    f"Metadata key {key!r} has invalid offset {entry.offset_bytes} "
                    f"for allocation {entry.allocation_id!r} "
                    f"(aligned_size={info.aligned_size})"
                )

    def _compute_memory_layout_hash(self, allocations: list[AllocationInfo]) -> str:
        h = hashlib.sha256()
        allocation_slots_by_id: dict[str, int] = {}
        for info in sorted(allocations, key=lambda info: info.layout_slot):
            allocation_slots_by_id[info.allocation_id] = info.layout_slot
            h.update(
                f"{info.layout_slot}:{info.size}:{info.aligned_size}:{info.tag}".encode()
            )

        for key in sorted(self._metadata):
            entry = self._metadata[key]
            layout_slot = allocation_slots_by_id[entry.allocation_id]
            h.update(f"{key}:{layout_slot}:{entry.offset_bytes}:".encode())
            h.update(entry.value)
        return h.hexdigest()

    def _clear_layout_state(self) -> int:
        self._metadata.clear()
        self._memory_layout_hash = ""
        return self._allocations.clear_all()

    def on_connect(self, conn: Connection) -> None:
        if conn.mode == GrantedLockType.RW:
            had_committed_layout = self._sessions.snapshot().committed
            cleared = self._clear_layout_state()
            if had_committed_layout:
                self._events.append(
                    GMSRuntimeEvent(
                        kind="allocations_cleared",
                        allocation_count=cleared,
                    )
                )

        self._sessions.on_connect(conn)
        if conn.mode == GrantedLockType.RW:
            self._events.append(GMSRuntimeEvent(kind="rw_connected"))

    async def cleanup_connection(self, conn: Connection | None) -> None:
        event = self._sessions.begin_cleanup(conn)
        if event == StateEvent.RW_ABORT:
            logger.warning("RW aborted; clearing active layout")
            cleared = self._clear_layout_state()
            self._events.append(GMSRuntimeEvent(kind="rw_aborted"))
            self._events.append(
                GMSRuntimeEvent(
                    kind="allocations_cleared",
                    allocation_count=cleared,
                )
            )
        await self._sessions.finish_cleanup(conn)

    async def handle_request(
        self,
        conn: Connection,
        msg,
        is_connected: Callable[[], bool],
    ) -> tuple[object, int, bool]:
        msg_type = type(msg)
        self._sessions.check_operation(msg_type, conn)

        if msg_type is CommitRequest:
            if self.state != ServerState.RW:
                raise AssertionError("RW state is not active")

            allocations = self._allocations.list_allocations()
            allocations_by_id = {info.allocation_id: info for info in allocations}
            self._validate_metadata_integrity(allocations_by_id)
            self._memory_layout_hash = self._compute_memory_layout_hash(allocations)

            logger.info(
                "Committed layout with state hash: %s...",
                self._memory_layout_hash[:16],
            )
            self._sessions.on_commit(conn)
            self._events.append(GMSRuntimeEvent(kind="committed"))
            return CommitResponse(success=True), -1, True

        if msg_type is AllocateRequest:
            if self.state != ServerState.RW:
                raise AssertionError("RW state is not active")

            info = await self._allocations.allocate(
                size=msg.size,
                tag=msg.tag,
                is_connected=is_connected,
                on_oom=lambda: self._events.append(
                    GMSRuntimeEvent(
                        kind="allocation_oom",
                        allocation_count=self._allocations.allocation_count,
                    )
                ),
            )
            return (
                AllocateResponse(
                    allocation_id=info.allocation_id,
                    size=info.size,
                    aligned_size=info.aligned_size,
                    layout_slot=info.layout_slot,
                ),
                -1,
                False,
            )

        if msg_type is GetLockStateRequest:
            snapshot = self._sessions.snapshot()
            return (
                GetLockStateResponse(
                    state=snapshot.state.name,
                    has_rw_session=snapshot.has_rw_session,
                    ro_session_count=snapshot.ro_session_count,
                    waiting_writers=snapshot.waiting_writers,
                    committed=snapshot.committed,
                    is_ready=snapshot.is_ready,
                ),
                -1,
                False,
            )

        if msg_type is GetAllocationStateRequest:
            return (
                GetAllocationStateResponse(
                    allocation_count=self._allocations.allocation_count
                ),
                -1,
                False,
            )

        if msg_type is ExportAllocationRequest:
            info = self._allocations.get_allocation(msg.allocation_id)
            fd = self._allocations.export_allocation(info.allocation_id)
            return (
                ExportAllocationResponse(
                    allocation_id=info.allocation_id,
                    size=info.size,
                    aligned_size=info.aligned_size,
                    tag=info.tag,
                    layout_slot=info.layout_slot,
                ),
                fd,
                False,
            )

        if msg_type is GetStateHashRequest:
            return (
                GetStateHashResponse(memory_layout_hash=self._memory_layout_hash),
                -1,
                False,
            )

        if msg_type is GetAllocationRequest:
            info = self._allocations.get_allocation(msg.allocation_id)
            return (
                GetAllocationResponse(
                    allocation_id=info.allocation_id,
                    size=info.size,
                    aligned_size=info.aligned_size,
                    tag=info.tag,
                    layout_slot=info.layout_slot,
                ),
                -1,
                False,
            )

        if msg_type is ListAllocationsRequest:
            return (
                ListAllocationsResponse(
                    allocations=[
                        GetAllocationResponse(
                            allocation_id=info.allocation_id,
                            size=info.size,
                            aligned_size=info.aligned_size,
                            tag=info.tag,
                            layout_slot=info.layout_slot,
                        )
                        for info in self._allocations.list_allocations(msg.tag)
                    ]
                ),
                -1,
                False,
            )

        if msg_type is FreeAllocationRequest:
            success = self._allocations.free_allocation(msg.allocation_id)
            if success:
                self._drop_metadata_for_allocation(msg.allocation_id)
            return (
                FreeAllocationResponse(success=success),
                -1,
                False,
            )

        if msg_type is MetadataPutRequest:
            allocation = self._allocations.get_allocation(msg.allocation_id)
            self._validate_metadata_target(allocation, msg.offset_bytes)
            self._metadata[msg.key] = MetadataEntry(
                allocation_id=allocation.allocation_id,
                offset_bytes=msg.offset_bytes,
                value=msg.value,
            )
            return MetadataPutResponse(success=True), -1, False

        if msg_type is MetadataGetRequest:
            entry = self._metadata.get(msg.key)
            if entry is None:
                return MetadataGetResponse(found=False), -1, False
            return (
                MetadataGetResponse(
                    found=True,
                    allocation_id=entry.allocation_id,
                    offset_bytes=entry.offset_bytes,
                    value=entry.value,
                ),
                -1,
                False,
            )

        if msg_type is MetadataDeleteRequest:
            return (
                MetadataDeleteResponse(
                    deleted=self._metadata.pop(msg.key, None) is not None
                ),
                -1,
                False,
            )

        if msg_type is MetadataListRequest:
            if not msg.prefix:
                keys = sorted(self._metadata)
            else:
                keys = sorted(
                    key for key in self._metadata if key.startswith(msg.prefix)
                )
            return MetadataListResponse(keys=keys), -1, False

        raise ValueError(f"Unknown request: {msg_type.__name__}")