test_gms_shadow_failover.py 20.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

from __future__ import annotations

import logging
import os
import signal
import time
from concurrent.futures import ThreadPoolExecutor
from contextlib import ExitStack
from typing import Callable

import pytest
15
from gpu_memory_service.server.fsm import ServerState
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
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501

from tests.utils.constants import FAULT_TOLERANCE_MODEL_NAME
from tests.utils.managed_process import DynamoFrontendProcess, ManagedProcess

from ..harness.gms import ThreadedGMSServer
from ..harness.runtime import (
    MIN_EXPECTED_MEMORY_RETURN_FRACTION,
    get_gpu_memory_used,
    send_completion,
)
from ..harness.sglang import SGLangWithGMSProcess
from ..harness.vllm import VLLMWithGMSProcess

pytestmark = [pytest.mark.nightly]

# Event flow under test:
# 1. Shadow A starts with committed weights and a live RW KV layout, then sleeps.
# 2. Shadow B starts from the same committed weights layout, then sleeps as well.
# 3. Primary wakes and owns the next RW KV layout.
# 4. Shadow A wakes after a forced primary disconnect and enters a new RW layout.
# 5. Shadow A blocks on allocation_oom until the still-alive primary is killed.
# 6. After primary death, the old KV layout clears and Shadow A finishes wake.

logger = logging.getLogger(__name__)


def _kill_process_group(process: ManagedProcess) -> None:
    pid = process.get_pid()
    if pid is None:
        logger.warning("kill process group: no PID available")
        return

    try:
        os.killpg(os.getpgid(pid), signal.SIGKILL)
    except ProcessLookupError:
        logger.warning("kill process group: process %d already dead", pid)
        return

    try:
        os.waitpid(pid, 0)
    except ChildProcessError:
        pass


def _is_process_alive(process: ManagedProcess) -> bool:
    pid = process.get_pid()
    if pid is None:
        return False
    try:
        os.kill(pid, 0)
    except ProcessLookupError:
        return False
    return True


def _assert_weights_published_once(events) -> None:
    assert [event.kind for event in events] == ["rw_connected", "committed"]


def _assert_cleared_rw_layout_prefix(events, cleared_layouts: int) -> None:
    expected_prefix = [
        "rw_connected",
        "rw_aborted",
        "allocations_cleared",
    ] * cleared_layouts
    assert [event.kind for event in events[: len(expected_prefix)]] == expected_prefix
    clear_counts = [
        event.allocation_count
        for event in events
        if event.kind == "allocations_cleared"
    ]
    assert len(clear_counts) >= cleared_layouts
    assert all(count > 0 for count in clear_counts[:cleared_layouts])


def _sleep_shadow(
    frontend_port: int,
    weights_gms: ThreadedGMSServer,
    kv_cache_gms: ThreadedGMSServer,
    shadow: ManagedProcess,
    expected_weights_hash: str | None = None,
) -> tuple[str, int, int]:
    result = send_completion(frontend_port)
    assert result["choices"], "Shadow inference failed"
    logger.info("Shadow inference OK: %s", result)

    deadline = time.monotonic() + 30.0
    while True:
        weights_state = weights_gms.get_runtime_state()
        kv_state = kv_cache_gms.get_runtime_state()
        if (
            weights_state.state == ServerState.RO
            and weights_state.allocation_count > 0
            and weights_state.memory_layout_hash
            and kv_state.state == ServerState.RW
            and kv_state.allocation_count > 0
        ):
            break
        if time.monotonic() > deadline:
            raise TimeoutError("shadow startup did not stabilize GMS state")
        time.sleep(0.1)

    if expected_weights_hash is not None:
        assert weights_state.memory_layout_hash == expected_weights_hash

    shadow_memory_before_sleep = get_gpu_memory_used()
    assert shadow.sleep()["status"] == "ok"
    shadow_memory_after_sleep = get_gpu_memory_used()
    shadow_released_bytes = shadow_memory_before_sleep - shadow_memory_after_sleep
    logger.info(
        "Shadow sleep: %.2f -> %.2f GiB (freed %.0f MB)",
        shadow_memory_before_sleep / (1 << 30),
        shadow_memory_after_sleep / (1 << 30),
        shadow_released_bytes / (1 << 20),
    )
    assert shadow_memory_after_sleep < shadow_memory_before_sleep
    assert shadow_released_bytes > 0

    deadline = time.monotonic() + 30.0
    while True:
        weights_after_sleep = weights_gms.get_runtime_state()
        kv_after_sleep = kv_cache_gms.get_runtime_state()
        if (
            weights_after_sleep.state == ServerState.COMMITTED
            and weights_after_sleep.allocation_count == weights_state.allocation_count
            and weights_after_sleep.memory_layout_hash
            == weights_state.memory_layout_hash
            and kv_after_sleep.state == ServerState.EMPTY
            and kv_after_sleep.allocation_count == 0
        ):
            break
        if time.monotonic() > deadline:
            raise TimeoutError("shadow sleep did not clear GMS state")
        time.sleep(0.1)

    return (
        weights_state.memory_layout_hash,
        shadow_released_bytes,
        shadow_memory_after_sleep,
    )


def _run_shadow_failover_test(
    request,
    ports: dict,
    make_shadow_a: Callable[[], ManagedProcess],
    make_shadow_b: Callable[[], ManagedProcess],
    make_primary: Callable[[], ManagedProcess],
) -> None:
    frontend_port = ports["frontend"]

    with ExitStack() as stack:
        weights_gms = stack.enter_context(ThreadedGMSServer(device=0, tag="weights"))
        kv_cache_gms = stack.enter_context(ThreadedGMSServer(device=0, tag="kv_cache"))
        stack.enter_context(
            DynamoFrontendProcess(
                request,
                frontend_port=frontend_port,
                display_name="frontend",
            )
        )
        with make_shadow_a() as shadow_a:
            (
                weights_hash,
                shadow_a_released_bytes,
                _shadow_a_memory_after_sleep,
            ) = _sleep_shadow(frontend_port, weights_gms, kv_cache_gms, shadow_a)
            with make_shadow_b() as shadow_b:
                (
                    sleeping_weights_hash,
                    _shadow_b_released_bytes,
                    sleeping_memory_after_sleep,
                ) = _sleep_shadow(
                    frontend_port,
                    weights_gms,
                    kv_cache_gms,
                    shadow_b,
                    expected_weights_hash=weights_hash,
                )
                assert sleeping_weights_hash == weights_hash

                weights_events_after_shadow_sleep = (
                    weights_gms.get_event_history().events
                )
                _assert_weights_published_once(weights_events_after_shadow_sleep)

                kv_events_after_shadow_sleep = kv_cache_gms.get_event_history().events
                _assert_cleared_rw_layout_prefix(kv_events_after_shadow_sleep, 2)

                with make_primary() as primary:
                    result = send_completion(frontend_port, "Primary test")
                    assert result["choices"], "Primary inference failed"
                    logger.info("Primary inference OK: %s", result)

                    primary_memory_in_use = get_gpu_memory_used()
                    logger.info(
                        "Primary active memory: %.2f GiB",
                        primary_memory_in_use / (1 << 30),
                    )
                    assert primary_memory_in_use > sleeping_memory_after_sleep
                    assert (
                        (primary_memory_in_use - sleeping_memory_after_sleep)
                        >= shadow_a_released_bytes * MIN_EXPECTED_MEMORY_RETURN_FRACTION
                    )

                    deadline = time.monotonic() + 30.0
                    while True:
                        weights_with_primary = weights_gms.get_runtime_state()
                        kv_with_primary = kv_cache_gms.get_runtime_state()
                        if (
                            weights_with_primary.state == ServerState.RO
                            and weights_with_primary.ro_session_count >= 1
                            and weights_with_primary.allocation_count > 0
                            and weights_with_primary.memory_layout_hash == weights_hash
                            and kv_with_primary.state == ServerState.RW
                            and kv_with_primary.allocation_count > 0
                        ):
                            break
                        if time.monotonic() > deadline:
                            raise TimeoutError(
                                "primary did not acquire KV cache GMS state"
                            )
                        time.sleep(0.1)
                    expected_kv_kinds_before_disconnect = [
                        "rw_connected",
                        "rw_aborted",
                        "allocations_cleared",
                        "rw_connected",
                        "rw_aborted",
                        "allocations_cleared",
                        "rw_connected",
                    ]
                    assert [
                        event.kind for event in kv_cache_gms.get_event_history().events
                    ] == expected_kv_kinds_before_disconnect

                    with ThreadPoolExecutor(max_workers=1) as executor:
                        # Shadow A wakes while Shadow B remains asleep. After we
                        # force-disconnect the primary from GMS, Shadow A should enter
                        # a new RW layout but block on real CUDA OOM until the primary dies.
                        wake_future = executor.submit(shadow_a.wake, 180)
                        deadline = time.monotonic() + 10.0
                        while time.monotonic() < deadline:
                            if wake_future.done():
                                break
                            time.sleep(0.2)
                        assert not wake_future.done(), (
                            "Shadow wake completed before the primary died; "
                            "KV cache RW handoff did not block as expected"
                        )
                        kv_while_blocked = kv_cache_gms.get_runtime_state()
                        assert kv_while_blocked.state == ServerState.RW
                        assert kv_while_blocked.allocation_count > 0

                        kv_cache_gms.disconnect_rw_session()

                        expected_kv_kinds_while_blocked = (
                            expected_kv_kinds_before_disconnect
                            + [
                                "rw_aborted",
                                "allocations_cleared",
                                "rw_connected",
                                "allocation_oom",
                            ]
                        )
                        blocked_allocation_count: int | None = None
                        deadline = time.monotonic() + 30.0
                        while time.monotonic() < deadline:
                            kv_after_forced_disconnect = (
                                kv_cache_gms.get_runtime_state()
                            )
                            kv_events_after_forced_disconnect = (
                                kv_cache_gms.get_event_history().events
                            )
                            if (
                                kv_after_forced_disconnect.state == ServerState.RW
                                and [
                                    event.kind
                                    for event in kv_events_after_forced_disconnect
                                ]
                                == expected_kv_kinds_while_blocked
                                and not wake_future.done()
                            ):
                                blocked_allocation_count = (
                                    kv_after_forced_disconnect.allocation_count
                                )
                                if (
                                    blocked_allocation_count
                                    < kv_while_blocked.allocation_count
                                    and blocked_allocation_count
                                    == kv_events_after_forced_disconnect[
                                        -1
                                    ].allocation_count
                                ):
                                    break
                            time.sleep(0.2)
                        else:
                            raise TimeoutError(
                                "shadow never entered a new KV-cache layout blocked on allocation"
                            )

                        assert blocked_allocation_count is not None
                        linger_deadline = time.monotonic() + 3.0
                        while time.monotonic() < linger_deadline:
                            kv_while_lingering = kv_cache_gms.get_runtime_state()
                            kv_events_while_lingering = (
                                kv_cache_gms.get_event_history().events
                            )
                            assert kv_while_lingering.state == ServerState.RW
                            assert (
                                kv_while_lingering.allocation_count
                                == blocked_allocation_count
                            )
                            assert [
                                event.kind for event in kv_events_while_lingering
                            ] == expected_kv_kinds_while_blocked
                            assert _is_process_alive(
                                primary
                            ), "primary died before the linger window completed"
                            assert (
                                not wake_future.done()
                            ), "shadow wake completed while the primary was still alive"
                            time.sleep(0.2)

                        primary_memory_before_kill = get_gpu_memory_used()
                        _kill_process_group(primary)
                        primary_memory_after_kill = get_gpu_memory_used()
                        logger.info(
                            "Primary kill snapshot: %.2f -> %.2f GiB",
                            primary_memory_before_kill / (1 << 30),
                            primary_memory_after_kill / (1 << 30),
                        )

                        deadline = time.monotonic() + 30.0
                        while time.monotonic() < deadline:
                            kv_after_primary_kill = kv_cache_gms.get_runtime_state()
                            if (
                                kv_after_primary_kill.state == ServerState.RW
                                and kv_after_primary_kill.allocation_count > 0
                            ):
                                break
                            time.sleep(0.2)
                        else:
                            raise TimeoutError(
                                "shadow did not reacquire KV cache after failover"
                            )

                        wake_result = wake_future.result(timeout=180)

                assert wake_result["status"] == "ok"
                shadow_memory_after_wake = get_gpu_memory_used()
                shadow_reacquired_bytes = (
                    shadow_memory_after_wake - sleeping_memory_after_sleep
                )
                logger.info(
                    "Shadow wake memory: %.2f GiB (reacquired %.0f MB)",
                    shadow_memory_after_wake / (1 << 30),
                    shadow_reacquired_bytes / (1 << 20),
                )
                assert shadow_memory_after_wake > sleeping_memory_after_sleep
                assert (
                    shadow_reacquired_bytes
                ) >= shadow_a_released_bytes * MIN_EXPECTED_MEMORY_RETURN_FRACTION

                # Once the primary is gone, the failover shadow should finish wake
                # with the same committed weights layout and a new live RW KV-cache layout.
                deadline = time.monotonic() + 30.0
                while True:
                    weights_after_wake = weights_gms.get_runtime_state()
                    kv_after_wake = kv_cache_gms.get_runtime_state()
                    if (
                        weights_after_wake.state == ServerState.RO
                        and weights_after_wake.ro_session_count >= 1
                        and weights_after_wake.allocation_count > 0
                        and weights_after_wake.memory_layout_hash
                        == weights_with_primary.memory_layout_hash
                        and kv_after_wake.state == ServerState.RW
                        and kv_after_wake.allocation_count > 0
                    ):
                        break
                    if time.monotonic() > deadline:
                        raise TimeoutError(
                            "shadow wake did not restore the expected GMS state"
                        )
                    time.sleep(0.1)

                # The final KV history should show the full handoff:
                # shadow A slept -> shadow B slept -> primary layout ->
                # primary abort/clear -> shadow A reconnects -> shadow A sees OOM.
                weights_events_after_wake = weights_gms.get_event_history().events
                _assert_weights_published_once(weights_events_after_wake)

                kv_events_after_wake = kv_cache_gms.get_event_history().events
                _assert_cleared_rw_layout_prefix(kv_events_after_wake, 3)
                assert [event.kind for event in kv_events_after_wake] == [
                    "rw_connected",
                    "rw_aborted",
                    "allocations_cleared",
                    "rw_connected",
                    "rw_aborted",
                    "allocations_cleared",
                    "rw_connected",
                    "rw_aborted",
                    "allocations_cleared",
                    "rw_connected",
                    "allocation_oom",
                ]

                result = send_completion(frontend_port, "Post failover")
                assert result["choices"], "Shadow inference after failover failed"
                logger.info("Shadow inference after failover OK: %s", result)


@pytest.mark.vllm
@pytest.mark.e2e
@pytest.mark.gpu_1
@pytest.mark.model(FAULT_TOLERANCE_MODEL_NAME)
@pytest.mark.timeout(600)
def test_gms_shadow_engine_failover_vllm(
    request, runtime_services_dynamic_ports, gms_ports, predownload_models
):
    ports = gms_ports
    _run_shadow_failover_test(
        request,
        ports,
        make_shadow_a=lambda: VLLMWithGMSProcess(
            request,
            "shadow-a",
            ports["shadow_system"],
            ports["shadow_kv_event"],
            ports["shadow_nixl"],
            ports["frontend"],
        ),
        make_shadow_b=lambda: VLLMWithGMSProcess(
            request,
            "shadow-b",
            ports["shadow2_system"],
            ports["shadow2_kv_event"],
            ports["shadow2_nixl"],
            ports["frontend"],
        ),
        make_primary=lambda: VLLMWithGMSProcess(
            request,
            "primary",
            ports["primary_system"],
            ports["primary_kv_event"],
            ports["primary_nixl"],
            ports["frontend"],
        ),
    )


@pytest.mark.sglang
@pytest.mark.e2e
@pytest.mark.gpu_1
@pytest.mark.model(FAULT_TOLERANCE_MODEL_NAME)
@pytest.mark.timeout(600)
def test_gms_shadow_engine_failover_sglang(
    request, runtime_services_dynamic_ports, gms_ports, predownload_models
):
    ports = gms_ports
    _run_shadow_failover_test(
        request,
        ports,
        make_shadow_a=lambda: SGLangWithGMSProcess(
            request,
            "shadow-a",
            ports["shadow_system"],
            ports["shadow_sglang"],
            ports["frontend"],
        ),
        make_shadow_b=lambda: SGLangWithGMSProcess(
            request,
            "shadow-b",
            ports["shadow2_system"],
            ports["shadow2_sglang"],
            ports["frontend"],
        ),
        make_primary=lambda: SGLangWithGMSProcess(
            request,
            "primary",
            ports["primary_system"],
            ports["primary_sglang"],
            ports["frontend"],
        ),
    )