locking.py 12.9 KB
Newer Older
1
2
3
4
5
6
7
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

"""Connection and state machine for GPU Memory Service.

This module handles:
- Connection: Represents an active client connection
8
- GMSLocalFSM: Explicit state transitions with validated permissions
9
10
11
12
13
14
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
48
49
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

State Diagram:

    EMPTY ──RW_CONNECT──► RW ──RW_COMMIT──► COMMITTED
      ▲                    │                   │
      │                    │                   │
      └───RW_ABORT─────────┘                   │

    COMMITTED ◄──RO_DISCONNECT (last)── RO ◄──RO_CONNECT
                      │                  ▲
                      │                  │
                      └──RO_CONNECT──────┘
                      └──RO_DISCONNECT───┘ (not last)
"""

from __future__ import annotations

import asyncio
import logging
import socket
from dataclasses import dataclass, field
from typing import Callable, Optional, Set

from gpu_memory_service.common.types import (
    RO_ALLOWED,
    RW_ALLOWED,
    RW_REQUIRED,
    GrantedLockType,
    ServerState,
    StateEvent,
)

logger = logging.getLogger(__name__)


# =============================================================================
# Connection
# =============================================================================


@dataclass(eq=False)
class Connection:
    """Represents an active connection.

    The existence of Connection objects IS the state - we don't track
    sessions separately. When a Connection is removed, the lock is released.

    Note: eq=False disables auto-generated __eq__ so we can use default
    object identity for equality and add __hash__ for use in sets.
    """

    reader: asyncio.StreamReader
    writer: asyncio.StreamWriter
    mode: GrantedLockType
    session_id: str
    recv_buffer: bytearray = field(default_factory=bytearray)

    def __hash__(self) -> int:
        """Hash based on session_id (immutable identifier)."""
        return hash(self.session_id)

    @property
    def raw_socket(self) -> socket.socket:
        """Get underlying socket for FD passing."""
        return self.writer.get_extra_info("socket")

    async def close(self) -> None:
        """Close the connection."""
        self.writer.close()
        try:
            await self.writer.wait_closed()
        except Exception:
            pass


# =============================================================================
# State Machine
# =============================================================================


class InvalidTransition(Exception):
    """Raised when an invalid state transition is attempted."""

    pass


class OperationNotAllowed(Exception):
    """Raised when an operation is not allowed in the current state/mode."""

    pass


@dataclass(frozen=True)
class Transition:
    """A valid state transition.

    Attributes:
        from_states: Set of states this transition can originate from
        event: The event that triggers this transition
        to_state: The resulting state (or None if conditional)
        condition: Optional condition function for conditional transitions
    """

    from_states: frozenset[ServerState]
    event: StateEvent
    to_state: Optional[ServerState]
    condition: Optional[str] = None  # Name of condition method


# Transition table - the single source of truth for valid state transitions
TRANSITIONS: list[Transition] = [
    # From EMPTY or COMMITTED: RW can connect
    # Writer acquires exclusive lock
    Transition(
        from_states=frozenset({ServerState.EMPTY, ServerState.COMMITTED}),
        event=StateEvent.RW_CONNECT,
        to_state=ServerState.RW,
    ),
    # From RW: commit publishes and transitions to COMMITTED
    # Writer publishes and releases lock
    Transition(
        from_states=frozenset({ServerState.RW}),
        event=StateEvent.RW_COMMIT,
        to_state=ServerState.COMMITTED,
    ),
    # From RW: abort (disconnect without commit) transitions to EMPTY
    # Writer aborts, state invalidated
    Transition(
        from_states=frozenset({ServerState.RW}),
        event=StateEvent.RW_ABORT,
        to_state=ServerState.EMPTY,
    ),
    # From COMMITTED or RO: RO can connect
    # Reader acquires shared lock
    Transition(
        from_states=frozenset({ServerState.COMMITTED, ServerState.RO}),
        event=StateEvent.RO_CONNECT,
        to_state=ServerState.RO,
    ),
    # From RO: reader disconnect (not last) stays in RO
    # Reader leaves, others remain
    Transition(
        from_states=frozenset({ServerState.RO}),
        event=StateEvent.RO_DISCONNECT,
        to_state=ServerState.RO,
        condition="has_remaining_readers",
    ),
    # From RO: last reader disconnect transitions to COMMITTED
    # Last reader leaves
    Transition(
        from_states=frozenset({ServerState.RO}),
        event=StateEvent.RO_DISCONNECT,
        to_state=ServerState.COMMITTED,
        condition="is_last_reader",
    ),
]


@dataclass
class TransitionRecord:
    """Record of a state transition for debugging/auditing."""

    from_state: ServerState
    event: StateEvent
    to_state: ServerState
    session_id: Optional[str] = None


177
class GMSLocalFSM:
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
    """Explicit state machine for GPU Memory Service.

    State is DERIVED from actual connection objects:
    - _rw_conn: The active RW connection (or None)
    - _ro_conns: Set of active RO connections
    - _committed: Whether allocations have been committed

    All state mutations happen through explicit transitions.
    """

    def __init__(self, on_rw_abort: Optional[Callable[[], None]] = None):
        """Initialize the state machine.

        Args:
            on_rw_abort: Callback invoked when RW aborts (for cleanup)
        """
        # Connection state - THIS IS THE SOURCE OF TRUTH
        self._rw_conn: Optional[Connection] = None
        self._ro_conns: Set[Connection] = set()
        self._committed: bool = False

        # Callback for RW abort cleanup
        self._on_rw_abort = on_rw_abort

        # Transition history for debugging
        self._transition_log: list[TransitionRecord] = []

    # ==================== State Properties ====================

    @property
    def state(self) -> ServerState:
        """Derive current state from connection objects."""
        if self._rw_conn is not None:
            return ServerState.RW
        if len(self._ro_conns) > 0:
            return ServerState.RO
        if self._committed:
            return ServerState.COMMITTED
        return ServerState.EMPTY

    @property
    def rw_conn(self) -> Optional[Connection]:
        """The active RW connection, if any."""
        return self._rw_conn

    @property
    def ro_conns(self) -> Set[Connection]:
        """Set of active RO connections."""
        return self._ro_conns

    @property
    def ro_count(self) -> int:
        """Number of active RO connections."""
        return len(self._ro_conns)

    @property
    def committed(self) -> bool:
        """Whether allocations have been committed."""
        return self._committed

    @property
    def transition_log(self) -> list[TransitionRecord]:
        """History of state transitions."""
        return self._transition_log

    # ==================== Transition Conditions ====================

    def _has_remaining_readers(self, conn: Connection) -> bool:
        """Check if there are readers remaining after removing conn."""
        return len(self._ro_conns) > 1 or conn not in self._ro_conns

    def _is_last_reader(self, conn: Connection) -> bool:
        """Check if conn is the last reader."""
        return len(self._ro_conns) == 1 and conn in self._ro_conns

    def _check_condition(self, condition: Optional[str], conn: Connection) -> bool:
        """Evaluate a named condition."""
        if condition is None:
            return True
        if condition == "has_remaining_readers":
            return self._has_remaining_readers(conn)
        if condition == "is_last_reader":
            return self._is_last_reader(conn)
        raise ValueError(f"Unknown condition: {condition}")

    # ==================== State Transitions ====================

    def _find_transition(
        self, from_state: ServerState, event: StateEvent, conn: Connection
    ) -> Optional[Transition]:
        """Find the applicable transition for the given event."""
        for t in TRANSITIONS:
            if from_state not in t.from_states:
                continue
            if t.event != event:
                continue
            if not self._check_condition(t.condition, conn):
                continue
            return t
        return None

    def _apply_event(self, event: StateEvent, conn: Connection) -> None:
        """Mutate internal state based on event."""
        match event:
            case StateEvent.RW_CONNECT:
                self._rw_conn = conn
                self._committed = False  # Invalidate on RW connect
            case StateEvent.RW_COMMIT:
                self._committed = True
                self._rw_conn = None
            case StateEvent.RW_ABORT:
                self._rw_conn = None
                if self._on_rw_abort:
                    self._on_rw_abort()
            case StateEvent.RO_CONNECT:
                self._ro_conns.add(conn)
            case StateEvent.RO_DISCONNECT:
                self._ro_conns.discard(conn)

    def transition(self, event: StateEvent, conn: Connection) -> ServerState:
        """Execute a state transition.

        Args:
            event: The triggering event
            conn: The connection involved in the transition

        Returns:
            The new state after the transition

        Raises:
            InvalidTransition: If the transition is not valid from current state
        """
        from_state = self.state
        session_id = conn.session_id if conn else None

        # Find valid transition
        trans = self._find_transition(from_state, event, conn)
        if trans is None:
            raise InvalidTransition(
                f"No transition for {event.name} from state {from_state.name} "
                f"(session={session_id})"
            )

        # Apply the transition
        self._apply_event(event, conn)
        to_state = self.state

        # Validate we ended up in expected state
        if trans.to_state is not None and to_state != trans.to_state:
            raise InvalidTransition(
                f"Transition mismatch: expected {trans.to_state.name}, "
                f"got {to_state.name}"
            )

        # Record transition
333
334
335
336
337
338
        record = TransitionRecord(
            from_state,
            event,
            to_state,
            session_id=session_id,
        )
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
        self._transition_log.append(record)

        logger.info(
            f"State transition: {from_state.name} --{event.name}--> {to_state.name} "
            f"(session={session_id})"
        )

        return to_state

    # ==================== Operation Permissions ====================

    def check_operation(self, msg_type: type, conn: Connection) -> None:
        """Check if a request type is allowed for the given connection.

        Args:
            msg_type: The request message type (e.g., AllocateRequest)
            conn: The connection attempting the operation

        Raises:
            OperationNotAllowed: If the operation is not permitted
        """
        current_state = self.state

        # Determine allowed operations based on state
        if current_state == ServerState.RW:
            allowed = RW_ALLOWED
        elif current_state == ServerState.RO:
            allowed = RO_ALLOWED
        else:
            allowed = frozenset()  # EMPTY and COMMITTED have no connections

        if msg_type not in allowed:
            raise OperationNotAllowed(
                f"{msg_type.__name__} not allowed in state {current_state.name}"
            )

        # Check connection mode
        if msg_type in RW_REQUIRED and conn.mode != GrantedLockType.RW:
            raise OperationNotAllowed(
                f"{msg_type.__name__} requires RW connection, "
                f"but connection is {conn.mode.value}"
            )

    # ==================== Lock Acquisition Predicates ====================

    def can_acquire_rw(self) -> bool:
        """Check if RW lock can be acquired now.

        RW can only be acquired if:
        - No current RW holder
        - No RO holders

        Note: This allows RW from COMMITTED state (for explicit reload).
        For rw_or_ro mode, callers should also check `committed` to prefer RO.
        """
        return self._rw_conn is None and len(self._ro_conns) == 0

    def can_acquire_ro(self, waiting_writers: int) -> bool:
        """Check if RO lock can be acquired now.

        Args:
            waiting_writers: Number of writers waiting for the lock
        """
        return self._rw_conn is None and waiting_writers == 0 and self._committed