utils.py 21.4 KB
Newer Older
1
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2
3
4
# SPDX-License-Identifier: Apache-2.0

import logging
5
import re
6
7
8
9
10
import threading
import time

import pytest
import requests
11
from openai import APIError, OpenAI
12
13

from tests.utils.constants import FAULT_TOLERANCE_MODEL_NAME
14
15
16
from tests.utils.managed_process import (
    DynamoFrontendProcess as BaseDynamoFrontendProcess,
)
17
from tests.utils.managed_process import ManagedProcess, terminate_process_tree
18
19
20
21

logger = logging.getLogger(__name__)


22
23
class DynamoFrontendProcess(BaseDynamoFrontendProcess):
    """Fault-tolerance frontend wrapper (keeps env settings from the historical helper)."""
24

25
26
27
28
29
30
    def __init__(
        self,
        request,
        migration_limit: int,
        migration_max_seq_len: int | None,
    ):
31
32
33
34
35
36
        extra_env = {
            "DYN_REQUEST_PLANE": request.getfixturevalue("request_plane"),
            # These tests expect full control over requests sent to workers. The canary
            # health check can inject extra requests and cause intermittent failures.
            "DYN_HEALTH_CHECK_ENABLED": "false",
        }
37
        super().__init__(
38
39
40
            request,
            frontend_port=0,  # allocate a free port (xdist-safe)
            router_mode="round-robin",
41
            migration_limit=migration_limit,
42
            migration_max_seq_len=migration_max_seq_len,
43
            extra_env=extra_env,
44
            terminate_all_matching_process_names=False,
45
            display_name="frontend",
46
47
        )

48

49
50
def _make_client(frontend_port: int) -> OpenAI:
    """Build an OpenAI client pointed at the test frontend.
51

52
53
    max_retries=0 so fault-tolerance tests see the first error instead of
    silent retries; api_key is a placeholder since the frontend doesn't auth.
54
    """
55
56
57
58
59
60
    return OpenAI(
        base_url=f"http://localhost:{frontend_port}/v1",
        api_key="not-needed",
        max_retries=0,
        timeout=240,
    )
61
62
63
64
65


def start_completion_request(
    frontend_port: int, stream: bool, use_long_prompt: bool = False
) -> tuple:
66
67
68
    """
    Start a long-running completion request in a separate thread.

69
70
71
    Responses are processed internally to extract content. First entry is (None, start_time)
    to mark when request was sent. Subsequent entries contain extracted content or exceptions.

72
73
    Args:
        frontend_port: Port where the frontend is running
74
75
        stream: Whether to use streaming responses
        use_long_prompt: Whether to use a long prompt (~8000 tokens)
76

77
    Returns:
78
79
80
81
        tuple: (request_thread, response_list) where response_list contains
               (str | None | Exception, float) tuples.
               - For streaming: each entry is (content_word, timestamp)
               - For non-streaming: single entry is (full_content, timestamp)
82
    """
83
    response_list: list[tuple[str | None | Exception, float]] = []
84
85
86

    def send_request():
        prompt = "Tell me a long long long story about yourself?"
87
88
        if use_long_prompt:
            prompt += " Make sure it is" + " long" * 8000 + "!"
89
90

        logger.info(
91
            f"Sending completion request (stream={stream}) with prompt: '{prompt[:50]}...'"
92
93
        )

94
95
        response_list.append((None, time.time()))  # start timestamp

96
        try:
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
            client = _make_client(frontend_port)
            if stream:
                for chunk in client.completions.create(
                    model=FAULT_TOLERANCE_MODEL_NAME,
                    prompt=prompt,
                    stream=True,
                ):
                    text = chunk.choices[0].text if chunk.choices else None
                    # Match the original hand-rolled parser: keep empty strings,
                    # drop only None. Empty chunks (e.g. the first stream frame)
                    # still count as a response arrival for delay measurement.
                    if text is not None:
                        response_list.append((text, time.time()))
            else:
                resp = client.completions.create(
                    model=FAULT_TOLERANCE_MODEL_NAME,
                    prompt=prompt,
                    stream=False,
115
                )
116
                response_list.append((resp.choices[0].text, time.time()))
117
        except Exception as e:
118
119
120
            # openai.APIError subclasses cover HTTP non-200, mid-stream
            # structured `data: {"error": {...}}` frames, connection failures,
            # and timeouts. Non-openai exceptions (network, etc.) also bubble.
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
            logger.error(f"Request failed with error: {e}")
            response_list.append((e, time.time()))

    request_thread = threading.Thread(target=send_request, daemon=True)
    request_thread.start()

    return request_thread, response_list


def start_chat_completion_request(
    frontend_port: int, stream: bool, use_long_prompt: bool = False
) -> tuple:
    """
    Start a long-running chat completion request in a separate thread.

    Responses are processed internally to extract content. First entry is (None, start_time)
    to mark when request was sent. Subsequent entries contain extracted content or exceptions.

    Args:
        frontend_port: Port where the frontend is running
        stream: Whether to use streaming responses
        use_long_prompt: Whether to use a long prompt (~8000 tokens)

    Returns:
        tuple: (request_thread, response_list) where response_list contains
               (str | None | Exception, float) tuples.
               - For streaming: each entry is (content_word, timestamp)
               - For non-streaming: single entry is (full_content, timestamp)
    """
    response_list: list[tuple[str | None | Exception, float]] = []

    def send_request():
        prompt = "Tell me a long long long story about yourself?"
        if use_long_prompt:
            prompt += " Make sure it is" + " long" * 8000 + "!"

        logger.info(
            f"Sending chat completion request (stream={stream}) with prompt: '{prompt[:50]}...'"
        )

        response_list.append((None, time.time()))  # start timestamp

        try:
164
165
166
167
168
169
170
171
172
173
174
175
176
            client = _make_client(frontend_port)
            if stream:
                for chunk in client.chat.completions.create(
                    model=FAULT_TOLERANCE_MODEL_NAME,
                    messages=[{"role": "user", "content": prompt}],
                    stream=True,
                ):
                    content = chunk.choices[0].delta.content if chunk.choices else None
                    # Match the original hand-rolled parser: keep empty strings,
                    # drop only None. Empty chunks (e.g. the first `role`-only
                    # stream frame) still count as a response arrival for delay
                    # measurement.
                    if content is not None:
177
                        response_list.append((content, time.time()))
178
179
180
181
182
183
184
            else:
                resp = client.chat.completions.create(
                    model=FAULT_TOLERANCE_MODEL_NAME,
                    messages=[{"role": "user", "content": prompt}],
                    stream=False,
                )
                response_list.append((resp.choices[0].message.content, time.time()))
185
        except Exception as e:
186
187
188
            # openai.APIError subclasses cover HTTP non-200, mid-stream
            # structured `data: {"error": {...}}` frames, connection failures,
            # and timeouts. Non-openai exceptions also bubble for visibility.
189
            logger.error(f"Request failed with error: {e}")
190
            response_list.append((e, time.time()))
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213

    request_thread = threading.Thread(target=send_request, daemon=True)
    request_thread.start()

    return request_thread, response_list


def determine_request_receiving_worker(
    worker1: ManagedProcess, worker2: ManagedProcess, receiving_pattern: str
) -> tuple:
    """
    Determine which worker received the request using parallel polling.

    Args:
        worker1: First worker process
        worker2: Second worker process
        receiving_pattern: Log pattern indicating request receipt

    Returns:
        Tuple of (worker_with_request, name_of_worker_with_request)
    """
    worker1_results: list[bool] = []
    worker2_results: list[bool] = []
214
215
    # Event to signal all threads to exit when one finds the pattern
    found_event = threading.Event()
216
217
218
219
220
221
222
223

    # Poll both workers in parallel
    def poll_worker(worker: ManagedProcess, result_list: list[bool]):
        max_wait_ms = 500
        poll_interval_ms = 5
        max_iterations = max_wait_ms // poll_interval_ms
        iteration = 0

224
225
        while iteration < max_iterations and not found_event.is_set():
            # Check if the worker logs contain the pattern
226
227
228
229
230
            try:
                with open(worker.log_path, "r") as f:
                    log_content = f.read()
                    if receiving_pattern in log_content:
                        result_list.append(True)
231
                        found_event.set()  # Signal other thread to exit
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
                        return
            except Exception as e:
                logger.error(f"Could not read log file {worker.log_path}: {e}")
                return

            time.sleep(poll_interval_ms / 1000.0)
            iteration += 1

    # Look for which worker received the request
    thread1 = threading.Thread(
        target=poll_worker, args=(worker1, worker1_results), daemon=True
    )
    thread2 = threading.Thread(
        target=poll_worker, args=(worker2, worker2_results), daemon=True
    )
    thread1.start()
    thread2.start()
    thread1.join(timeout=1)
    thread2.join(timeout=1)

    # Get results from lists
    worker1_received = worker1_results[0] if worker1_results else False
    worker2_received = worker2_results[0] if worker2_results else False

    if worker1_received and not worker2_received:
        logger.info("Request was received by Worker 1")
        return worker1, "Worker 1"
    elif worker2_received and not worker1_received:
        logger.info("Request was received by Worker 2")
        return worker2, "Worker 2"
    elif worker1_received and worker2_received:
        pytest.fail("Both workers received the request")
    else:
        pytest.fail("Neither worker received the request")


268
269
270
271
def wait_for_response(
    response_list: list[tuple[str | None | Exception, float]],
    num_responses: int = 5,
    max_wait_time: float = 10.0,
272
273
) -> None:
    """
274
    Block until num_responses new responses are received or max_wait_time is reached.
275
276

    Args:
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
        response_list: List being populated by background thread
        num_responses: Number of new responses to wait for (default 5)
        max_wait_time: Maximum time to wait in seconds (default 10s)
    """
    initial_len = len(response_list)
    target_len = initial_len + num_responses
    poll_interval = 0.001  # 1ms
    elapsed = 0.0

    while elapsed < max_wait_time:
        if len(response_list) >= target_len:
            return
        time.sleep(poll_interval)
        elapsed += poll_interval

    logger.warning(
        f"Only received {len(response_list) - initial_len}/{num_responses} new responses within {max_wait_time}s"
    )


def validate_response(
    request_thread: threading.Thread,
    response_list: list[tuple[str | None | Exception, float]],
    validate_delay: bool = True,
) -> None:
    """
    Wait for and validate the response after migration.
    Checks that delay before each response is reasonable (covers both TTFT and TPOT).

    Args:
        request_thread: The thread running the request
        response_list: List of (content_string | None | Exception, timestamp) tuples.
                       Content is already parsed - no SSE format parsing needed.
        validate_delay: Whether to validate delay before each response.
311
312
    """
    request_thread.join(timeout=240)
313
    assert not request_thread.is_alive(), "Request did not complete within 240 seconds"
314

315
316
317
    assert len(response_list) > 0, "Missing first entry with start timestamp"
    assert response_list[0][0] is None, "First entry should be start timestamp only"
    prev_timestamp = response_list[0][1]
318

319
320
321
322
323
324
325
326
327
    response_words: list[str] = []
    for res, timestamp in response_list[1:]:
        delay = timestamp - prev_timestamp
        if delay > 2.0 and validate_delay:
            # Cold workers can take longer on first token - only warn but don't fail
            logger.warning(f"Delay before response: {delay:.3f} secs")
            # Capture cases like migration is blocked by engine graceful shutdown
            assert delay <= 6.0, f"Delay before response > 6 secs, got {delay:.3f} secs"
        prev_timestamp = timestamp
328

329
330
331
        assert res is not None, "Response entry should not be None"
        if isinstance(res, Exception):
            raise res
332

333
334
        # Content is already parsed - just collect it
        response_words.append(res)
335
336

    logger.info(
337
        f"Received {len(response_words)} response(s): {''.join(response_words)[:100]}..."
338
339
340
341
342
343
344
345
346
347
348
    )


def verify_migration_occurred(frontend_process: DynamoFrontendProcess) -> None:
    """
    Verify that migration occurred by checking frontend logs for stream disconnection message.

    Args:
        frontend_process: The frontend process to check logs for
    """
    log_path = frontend_process.log_path
349
350
351
352
353
354
355
356
357
358
359
360
    log_content = ""
    for i in range(10):
        try:
            with open(log_path, "r") as f:
                log_content = f.read()
        except Exception as e:
            pytest.fail(f"Could not read frontend log file {log_path}: {e}")
        # Make sure this message is captured if any with the polling
        if "Cannot recreate stream: " in log_content:
            break
        time.sleep(0.005)

361
362
363
364
365
366
    assert (
        "Stream disconnected... recreating stream..." in log_content
    ), "'Stream disconnected... recreating stream...' message not found in logs"
    assert (
        "Cannot recreate stream: " not in log_content
    ), "'Cannot recreate stream: ...' error found in logs"
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


def _parse_migration_metric(
    metrics_text: str, model_name: str, migration_type: str
) -> int:
    """
    Parse the migration metric value from Prometheus metrics text.

    Args:
        metrics_text: Raw Prometheus metrics text
        model_name: The model name label value
        migration_type: The migration_type label value ("ongoing_request" or "new_request")

    Returns:
        The metric count, or 0 if not found
    """
    # Match pattern like:
    # dynamo_frontend_model_migration_total{migration_type="ongoing_request",model="Qwen/Qwen3-0.6B"} 1
    # Labels can be in any order
    pattern = rf'dynamo_frontend_model_migration_total\{{[^}}]*migration_type="{migration_type}"[^}}]*model="{re.escape(model_name)}"[^}}]*\}}\s+(\d+)'
    match = re.search(pattern, metrics_text)

    if match:
        return int(match.group(1))

    # Try with labels in reverse order
    pattern = rf'dynamo_frontend_model_migration_total\{{[^}}]*model="{re.escape(model_name)}"[^}}]*migration_type="{migration_type}"[^}}]*\}}\s+(\d+)'
    match = re.search(pattern, metrics_text)

    if match:
        return int(match.group(1))

    return 0


402
403
404
405
406
407
408
409
410
411
412
413
414
415
def _parse_migration_max_seq_len_exceeded_metric(
    metrics_text: str, model_name: str
) -> int:
    """
    Parse the migration max_seq_len exceeded counter from Prometheus metrics text.

    Returns:
        The metric count, or 0 if not found
    """
    pattern = rf'dynamo_frontend_model_migration_max_seq_len_exceeded_total\{{[^}}]*model="{re.escape(model_name)}"[^}}]*\}}\s+(\d+)'
    match = re.search(pattern, metrics_text)
    return int(match.group(1)) if match else 0


416
417
418
419
def verify_migration_metrics(
    frontend_port: int,
    expected_ongoing_request_count: int = 0,
    expected_new_request_count: int = 0,
420
    expected_max_seq_len_exceeded_count: int = 0,
421
422
423
424
425
426
427
428
) -> None:
    """
    Verify migration metrics by querying the frontend's /metrics endpoint.

    Args:
        frontend_port: Port where the frontend is running
        expected_ongoing_request_count: Expected count of ongoing_request migrations
        expected_new_request_count: Expected count of new_request migrations
429
        expected_max_seq_len_exceeded_count: Expected count of max_seq_len exceeded events
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
    """
    metrics_url = f"http://localhost:{frontend_port}/metrics"

    try:
        response = requests.get(metrics_url, timeout=1)
        response.raise_for_status()
    except requests.RequestException as e:
        pytest.fail(f"Failed to fetch metrics from {metrics_url}: {e}")

    metrics_text = response.text
    logger.info(f"Fetched metrics from {metrics_url}")

    # Parse metrics to find migration counts
    ongoing_count = _parse_migration_metric(
        metrics_text, FAULT_TOLERANCE_MODEL_NAME, "ongoing_request"
    )
    new_request_count = _parse_migration_metric(
        metrics_text, FAULT_TOLERANCE_MODEL_NAME, "new_request"
    )
449
450
451
    max_seq_len_exceeded_count = _parse_migration_max_seq_len_exceeded_metric(
        metrics_text, FAULT_TOLERANCE_MODEL_NAME
    )
452
453

    logger.info(
454
455
456
        f"Migration metrics - ongoing_request: {ongoing_count}, "
        f"new_request: {new_request_count}, "
        f"max_seq_len_exceeded: {max_seq_len_exceeded_count}"
457
458
459
460
461
462
463
464
465
466
467
468
469
    )

    if expected_ongoing_request_count > 0:
        assert ongoing_count >= expected_ongoing_request_count, (
            f"Expected at least {expected_ongoing_request_count} ongoing_request migrations, "
            f"but got {ongoing_count}"
        )

    if expected_new_request_count > 0:
        assert new_request_count >= expected_new_request_count, (
            f"Expected at least {expected_new_request_count} new_request migrations, "
            f"but got {new_request_count}"
        )
470

471
472
473
474
475
    assert max_seq_len_exceeded_count == expected_max_seq_len_exceeded_count, (
        f"Expected {expected_max_seq_len_exceeded_count} "
        f"max_seq_len_exceeded events, but got {max_seq_len_exceeded_count}"
    )

476
477
478
479
480
481
482

def run_migration_test(
    frontend: DynamoFrontendProcess,
    worker1: ManagedProcess,
    worker2: ManagedProcess,
    receiving_pattern: str,
    migration_limit: int,
483
    migration_max_seq_len: int | None,
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
    immediate_kill: bool,
    use_chat_completion: bool,
    stream: bool,
    use_long_prompt: bool = False,
    wait_for_new_response_before_stop: bool = False,
) -> None:
    """
    Run the common migration test flow after frontend and workers are started.

    Args:
        frontend: The frontend process
        worker1: First worker process
        worker2: Second worker process
        receiving_pattern: Log pattern to identify which worker received the request
        migration_limit: Migration limit setting (0 = disabled)
499
        migration_max_seq_len: Max sequence length for migration (None = no limit)
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
        immediate_kill: True for immediate kill, False for graceful shutdown
        use_chat_completion: Whether to use chat completion API (True) or completion API (False)
        stream: Whether to use streaming responses
        use_long_prompt: Whether to use long prompt (for prefill tests)
        wait_for_new_response_before_stop: Whether to wait for response before stopping (for decode tests)
    """
    # Step 1: Send the request
    if use_chat_completion:
        request_thread, response_list = start_chat_completion_request(
            frontend.frontend_port, stream=stream, use_long_prompt=use_long_prompt
        )
    else:
        request_thread, response_list = start_completion_request(
            frontend.frontend_port, stream=stream, use_long_prompt=use_long_prompt
        )

    # Step 2: Determine which worker received the request
    worker, worker_name = determine_request_receiving_worker(
        worker1, worker2, receiving_pattern=receiving_pattern
    )

    # Step 3: Optionally wait for new response before stop (for decode tests)
    if wait_for_new_response_before_stop:
        wait_for_response(response_list)

    # Step 4: Stop the worker (kill or graceful shutdown)
    if immediate_kill:
        logger.info(f"Killing {worker_name} with PID {worker.get_pid()}")
        terminate_process_tree(worker.get_pid(), immediate_kill=True, timeout=0)
    else:
        logger.info(
            f"Gracefully shutting down {worker_name} with PID {worker.get_pid()}"
        )
        terminate_process_tree(worker.get_pid(), immediate_kill=False, timeout=10)

535
536
537
    # Step 5: Validate response and verify migration occurred.
    # migration_enabled and not max_seq_len_exceeded -> migration should succeed
    if migration_limit > 0 and migration_max_seq_len != 1:
538
539
540
541
542
        validate_response(request_thread, response_list, validate_delay=stream)
        verify_migration_occurred(frontend)
    else:
        try:
            validate_response(request_thread, response_list, validate_delay=stream)
543
544
545
            pytest.fail(
                "Request succeeded unexpectedly when migration should have failed"
            )
546
547
548
549
550
551
        except APIError as e:
            # Expected: openai.APIError covers mid-stream structured error
            # frames (DIS-1768 contract) and HTTP non-200 responses. A typed
            # check is more robust than matching the exception's stringified
            # message against a specific wire-format prefix.
            logger.info(f"Got expected APIError: {e}")
552
553
554

        try:
            verify_migration_occurred(frontend)
555
            pytest.fail("Migration unexpectedly succeeded")
556
557
        except AssertionError as e:
            assert "'Cannot recreate stream: ...' error found in logs" in str(e)
558
559
560
561
562
563
564

    # Step 6: Verify migration metrics
    verify_migration_metrics(
        frontend.frontend_port,
        expected_ongoing_request_count=1 if migration_limit > 0 else 0,
        expected_max_seq_len_exceeded_count=1 if migration_max_seq_len == 1 else 0,
    )