common.py 112 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 asyncio
5
import contextlib
6
7
import json
import logging
8
import os
9
import random
10
from typing import TYPE_CHECKING, Any, Optional
11
12
13

import aiohttp
import nats
14
import requests
15

16
from dynamo.llm import AicPerfConfig, KvRouter, KvRouterConfig
17
from dynamo.prometheus_names import frontend_service, name_prefix
18
19
20
21
22
23
24
25
26
from tests.router.helper import (
    _nats_server,
    assert_event_dumps_equal,
    get_runtime,
    send_inflight_requests,
    send_request_via_python_kv_router,
    send_request_with_retry,
    verify_response_timing,
    wait_for_frontend_ready,
27
    wait_for_indexer_workers_active,
28
29
    wait_for_workers_ready,
)
30
31
32
33
34
from tests.router.router_process import (
    DirectRouterProcess,
    FrontendRouterProcess,
    KVRouterProcess,
)
35

36
37
38
if TYPE_CHECKING:
    from tests.conftest import NatsServer

39
40
logger = logging.getLogger(__name__)

41
42
NUM_REQUESTS = 100
BLOCK_SIZE = 16
43
44
45
46
47
48
49
50
51
52
53
54
55
56
MIN_INITIAL_WORKERS_ENV = "DYN_ROUTER_MIN_INITIAL_WORKERS"


@contextlib.contextmanager
def min_initial_workers_env(min_initial_workers: int):
    previous = os.environ.get(MIN_INITIAL_WORKERS_ENV)
    os.environ[MIN_INITIAL_WORKERS_ENV] = str(min_initial_workers)
    try:
        yield
    finally:
        if previous is None:
            os.environ.pop(MIN_INITIAL_WORKERS_ENV, None)
        else:
            os.environ[MIN_INITIAL_WORKERS_ENV] = previous
57

58
59
60
61
62
63
64
65
66
67
68
69
70

########################################################
# Test templates
########################################################


def _test_router_basic(
    engine_workers,
    block_size: int,
    request,
    frontend_port: int,
    test_payload: dict,
    num_requests: int,
71
    frontend_timeout: int = 120,
72
    store_backend: str = "etcd",
73
    request_plane: str = "nats",
74
75
    router_mode: str = "kv",
    enforce_disagg: bool = False,
76
    min_initial_workers: int | None = None,
77
):
78
    """Basic router test: start router, wait for workers and send concurrent requests via HTTP frontend.
79
80
81
82

    Assumes engine_workers are already initialized. This function manages router lifecycle.

    This is a shared test implementation for both mocker and vLLM workers.
83
    Always waits for workers to be properly registered before sending requests to avoid flakiness.
84

85
86
87
    Supports any router_mode (defaults to "kv" for existing callers).
    block_size is only sent to the frontend CLI when router_mode is "kv".

88
    Args:
89
        engine_workers: Backend worker instance ({MockerProcess, VLLMProcess, TRTLLMProcess}) (already initialized with __enter__())
90
91
92
93
94
        block_size: Block size for KV cache
        request: Pytest request fixture for managing resources
        frontend_port: Port to start the frontend HTTP server on
        test_payload: Test payload to send to /v1/chat/completions
        num_requests: Number of concurrent requests to send
95
        frontend_timeout: Timeout for frontend readiness check (default: 120s)
96
        store_backend: Storage backend to use ("etcd" or "file"). Defaults to "etcd".
97
        request_plane: Request plane to use ("nats", "tcp", or "http"). Defaults to "nats".
98
        router_mode: Router mode ("kv", "round-robin", "random", "power-of-two", "direct"). Defaults to "kv".
99
        enforce_disagg: Whether to pass --enforce-disagg to the frontend. Defaults to False.
100
        min_initial_workers: Optional frontend startup worker gate. Defaults to None.
101
102
103
104
105

    Raises:
        AssertionError: If requests fail or frontend doesn't become ready
        TimeoutError: If frontend doesn't become ready within timeout
    """
106
    with FrontendRouterProcess(
107
108
109
110
111
        request,
        block_size,
        frontend_port,
        engine_workers.namespace,
        store_backend,
112
        enforce_disagg=enforce_disagg,
113
        request_plane=request_plane,
114
        router_mode=router_mode,
115
        min_initial_workers=min_initial_workers,
116
    ):
117
118
119
120
        # Start router frontend
        logger.info(
            f"Starting frontend --router-mode {router_mode} on port {frontend_port}"
        )
121
122
123

        frontend_url = f"http://localhost:{frontend_port}"

124
125
126
127
128
129
130
        # Always wait for workers to register with frontend to avoid flakiness
        logger.info("Waiting for workers to register with frontend...")
        asyncio.run(
            wait_for_frontend_ready(
                frontend_url=frontend_url,
                expected_num_workers=engine_workers.num_workers,
                timeout=frontend_timeout,
131
            )
132
        )
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154

        # Send concurrent requests to the frontend
        logger.info(f"Sending {num_requests} concurrent requests to frontend...")
        asyncio.run(
            send_inflight_requests(
                [f"{frontend_url}/v1/chat/completions"],
                test_payload,
                num_requests,
            )
        )

        logger.info(f"Successfully completed {num_requests} requests")


def _test_router_two_routers(
    engine_workers,
    block_size: int,
    request,
    router_ports: list[int],
    test_payload: dict,
    num_requests: int,
    store_backend: str = "etcd",
155
    skip_consumer_verification: bool = False,
156
157
158
159
160
161
162
163
):
    """Test two KV routers with alternating requests and consumer lifecycle verification.

    Assumes engine_workers are already initialized. This function manages router lifecycle.

    This test:
    1. Starts two KV routers on different ports
    2. Sends requests alternating between the two routers
164
165
    3. Verifies that both routers create durable consumers (unless skipped)
    4. Verifies consumers are cleaned up when routers exit (unless skipped)
166
167
168
169
170
171
172
173
174

    Args:
        engine_workers: Backend workers (mocker/vllm) already initialized with __enter__()
        block_size: Block size for KV cache
        request: Pytest request fixture for managing resources
        router_ports: List of two port numbers for the routers (e.g., [8091, 8092])
        test_payload: Test payload to send to /v1/chat/completions
        num_requests: Number of concurrent requests to send
        store_backend: Storage backend to use ("etcd" or "file"). Defaults to "etcd".
175
        skip_consumer_verification: Skip JetStream consumer verification (for NATS Core mode).
176
177
178
179
180
181
182
183

    Raises:
        AssertionError: If consumer lifecycle verification fails
    """
    kv_routers = []

    try:
        # Start two KV routers on different ports
184
        for i, port in enumerate(router_ports):
185
            logger.info(f"Starting KV router frontend on port {port}")
186
            kv_router = KVRouterProcess(
187
188
189
190
191
192
                request,
                block_size,
                port,
                engine_workers.namespace,
                store_backend,
                min_initial_workers=engine_workers.num_workers,
193
            )
194
195
196
            kv_router.__enter__()
            kv_routers.append(kv_router)

197
198
199
200
201
202
203
204
205
206
207
208
209
210
        # Wait for workers to be ready on both routers
        logger.info("Waiting for workers to register with both routers...")
        for i, port in enumerate(router_ports):
            frontend_url = f"http://localhost:{port}"
            logger.info(f"Waiting for router {i} on port {port} to discover workers...")
            asyncio.run(
                wait_for_frontend_ready(
                    frontend_url=frontend_url,
                    expected_num_workers=engine_workers.num_workers,
                    timeout=120,
                )
            )
        logger.info("Both routers have discovered workers")

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
        # Build URLs for both routers
        router_urls = [
            f"http://localhost:{port}/v1/chat/completions" for port in router_ports
        ]

        # Send requests concurrently, alternating between routers
        asyncio.run(
            send_inflight_requests(
                router_urls,
                test_payload,
                num_requests,
            )
        )

        logger.info(
            f"Successfully completed {num_requests} requests across {len(router_ports)} routers"
        )

        # Verify durable consumers lifecycle
        async def verify_consumer_lifecycle():
            logger.info("Verifying durable consumers lifecycle")

            # Construct the stream name from the workers namespace
            component_subject = f"namespace.{engine_workers.namespace}.component.{engine_workers.component_name}"
            slugified = component_subject.lower().replace(".", "-").replace("_", "-")
            stream_name = f"{slugified}-kv-events"

            logger.info(f"Checking consumers for stream: {stream_name}")

            # Connect to NATS and list consumers
241
            nc = await nats.connect(servers=_nats_server())
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
            try:
                js = nc.jetstream()

                # List consumers - should have 2 (one for each router process)
                consumer_infos = await js.consumers_info(stream_name)
                consumer_names = [info.name for info in consumer_infos]
                logger.info(f"Found {len(consumer_names)} consumers: {consumer_names}")

                assert (
                    len(consumer_names) == 2
                ), f"Expected 2 durable consumers (one per router), found {len(consumer_names)}: {consumer_names}"
                logger.info("✓ Verified 2 durable consumers exist (one per router)")

                # Kill the first router process
                logger.info(f"Killing first router on port {router_ports[0]}")
                kv_routers[0].__exit__(None, None, None)

                # Poll until one consumer remains (up to 5s)
                for _ in range(25):
                    consumer_infos = await js.consumers_info(stream_name)
                    if len(list(consumer_infos)) == 1:
                        break
                    await asyncio.sleep(0.2)

                # Verify only 1 consumer remains
                consumer_names = [info.name for info in consumer_infos]
                logger.info(
                    f"After killing router1, found {len(consumer_names)} consumers: {consumer_names}"
                )

                assert (
                    len(consumer_names) == 1
                ), f"Expected 1 durable consumer after killing router1, found {len(consumer_names)}: {consumer_names}"
                logger.info(
                    "✓ Verified 1 durable consumer remains after killing first router"
                )

                # Kill the second router process
                logger.info(f"Killing second router on port {router_ports[1]}")
                kv_routers[1].__exit__(None, None, None)

                # Poll until no consumers remain (up to 5s)
                for _ in range(25):
                    consumer_infos = await js.consumers_info(stream_name)
                    if len(list(consumer_infos)) == 0:
                        break
                    await asyncio.sleep(0.2)

                consumer_names = [info.name for info in consumer_infos]
                logger.info(
                    f"After killing router2, found {len(consumer_names)} consumers: {consumer_names}"
                )

                assert (
                    len(consumer_names) == 0
                ), f"Expected 0 durable consumers after killing both routers, found {len(consumer_names)}: {consumer_names}"
                logger.info(
                    "✓ Verified 0 durable consumers remain after killing both routers"
                )

            finally:
                await nc.close()

305
306
307
308
309
310
311
312
        # Run consumer lifecycle verification (skip for NATS Core mode)
        if skip_consumer_verification:
            logger.info("Skipping JetStream consumer verification (NATS Core mode)")
            # Clean up routers manually since we're not doing consumer verification
            for kv_router in kv_routers:
                kv_router.__exit__(None, None, None)
        else:
            asyncio.run(verify_consumer_lifecycle())
313
314
315
316
317
318
319
320
321
322

        # Clear the kv_routers list since we've already cleaned them up
        kv_routers = []

    finally:
        # Clean up any remaining routers (in case of error before consumer verification)
        for kv_router in kv_routers:
            kv_router.__exit__(None, None, None)


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
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
def _test_remote_indexer_decisions(
    engine_workers,
    model_name: str,
    block_size: int = 8,
    use_kv_events: bool = True,
    test_dp_rank: bool = True,
    request_plane: str = "nats",
    store_backend: str = "etcd",
):
    """Validate remote-indexer-backed routing decisions using direct KvRouter instances."""

    async def wait_for_worker_ids(endpoint, expected_num_workers: int) -> list[int]:
        client = await endpoint.client()

        for _ in range(120):
            worker_ids = sorted(set(client.instance_ids()))
            if len(worker_ids) >= expected_num_workers:
                return worker_ids
            await asyncio.sleep(1)

        raise TimeoutError("Timed out waiting for backend worker IDs")

    async def wait_for_served_indexer(
        runtime,
        expected_query_instances: int,
        expected_record_instances: int,
    ) -> None:
        query_endpoint = runtime.endpoint(
            f"{engine_workers.namespace}.{engine_workers.component_name}.kv_indexer_query"
        )
        query_client = await query_endpoint.client()
        record_endpoint = runtime.endpoint(
            f"{engine_workers.namespace}.{engine_workers.component_name}.kv_indexer_record_routing_decision"
        )
        record_client = await record_endpoint.client()

        for _ in range(120):
            query_ids = set(query_client.instance_ids())
            record_ids = set(record_client.instance_ids())

            if use_kv_events:
                if len(query_ids) >= expected_query_instances and len(record_ids) == 0:
                    return
            elif (
                len(query_ids) == expected_query_instances
                and len(record_ids) == expected_record_instances
                and query_ids == record_ids
            ):
                return

            await asyncio.sleep(0.5)

        raise TimeoutError("Timed out waiting for served indexer endpoints to register")

    async def test_sync():
        endpoint_path = (
            f"{engine_workers.namespace}.{engine_workers.component_name}.generate"
        )
        expected_num_instances = engine_workers.num_workers

        async def make_router(*, serve_indexer: bool, use_remote_indexer: bool):
            kv_router_config = KvRouterConfig(
                router_snapshot_threshold=20,
                use_kv_events=use_kv_events,
                router_track_prefill_tokens=True,
                serve_indexer=serve_indexer,
                use_remote_indexer=use_remote_indexer,
            )
            last_error: Exception | None = None
            for _ in range(60):
                runtime = get_runtime(
                    store_backend=store_backend, request_plane=request_plane
                )
                endpoint = runtime.endpoint(endpoint_path)
                try:
                    with min_initial_workers_env(expected_num_instances):
                        kv_router = KvRouter(
                            endpoint=endpoint,
                            block_size=block_size,
                            kv_router_config=kv_router_config,
                        )
                    return runtime, endpoint, kv_router
                except Exception as error:
                    last_error = error
                    if not (serve_indexer or use_remote_indexer):
                        raise
                    del endpoint
                    del runtime
                    await asyncio.sleep(1.0)

            raise AssertionError(
                "Timed out waiting for model discovery before creating remote-indexer router"
            ) from last_error

        serving_runtimes = []
        serving_endpoints = []
        serving_routers = []

        runtime_a, endpoint_a, router_a = await make_router(
            serve_indexer=True, use_remote_indexer=False
        )
        serving_runtimes.append(runtime_a)
        serving_endpoints.append(endpoint_a)
        serving_routers.append(router_a)

        if use_kv_events:
            runtime_b, endpoint_b, router_b = await make_router(
                serve_indexer=True, use_remote_indexer=False
            )
            serving_runtimes.append(runtime_b)
            serving_endpoints.append(endpoint_b)
            serving_routers.append(router_b)

        await wait_for_served_indexer(
            serving_runtimes[0],
            expected_query_instances=len(serving_routers),
            expected_record_instances=0 if use_kv_events else 1,
        )

        _, consumer_endpoint, consumer_router = await make_router(
            serve_indexer=False, use_remote_indexer=True
        )

        worker_ids = await wait_for_worker_ids(
            serving_endpoints[0], expected_num_instances
        )
        if len(worker_ids) >= 2:
            worker_a_id = worker_ids[0]
            worker_b_id = worker_ids[1]
        elif len(worker_ids) == 1 and test_dp_rank:
            worker_a_id = worker_ids[0]
            worker_b_id = worker_ids[0]
        else:
            raise AssertionError(
                f"Need at least 2 routing targets but got {len(worker_ids)} worker(s) "
                f"with test_dp_rank={test_dp_rank}"
            )

        dp_rank_a = 0 if test_dp_rank else None
        dp_rank_b = 1 if test_dp_rank else None
        logger.info(
            "Remote-indexer routing targets: worker_a=%s/%s worker_b=%s/%s",
            worker_a_id,
            dp_rank_a,
            worker_b_id,
            dp_rank_b,
        )

        blocks = [
            [random.randint(1, 10000) for _ in range(block_size)] for _ in range(7)
        ]
        A, B, C, D, E, F, G = blocks
        request_specs = [
            (serving_routers[0], A + B, worker_a_id, dp_rank_a, 0.1),
            (serving_routers[0], A + C + D, worker_a_id, dp_rank_a, 0.1),
            (serving_routers[-1], A + C + E, worker_b_id, dp_rank_b, 2.0),
            (consumer_router, A + C + D + F, None, None, 2.0),
            (consumer_router, A + C + G, None, None, 2.0),
        ]

        responses: list[dict[str, Optional[int]]] = []
        for i, (
            kv_router,
            token_ids,
            forced_worker_id,
            forced_dp_rank,
            sleep_after,
        ) in enumerate(request_specs, start=1):
            logger.info(
                "Sending remote-indexer request %s/5%s%s",
                i,
                (
                    f" forced_worker_id={forced_worker_id}"
                    if forced_worker_id is not None
                    else ""
                ),
                (
                    f" forced_dp_rank={forced_dp_rank}"
                    if forced_dp_rank is not None
                    else ""
                ),
            )
            result = await send_request_via_python_kv_router(
                kv_python_router=kv_router,
                model_name=model_name,
                token_ids=token_ids,
                initial_wait=1.0,
                max_retries=8,
                stop_conditions={
                    "ignore_eos": True,
                    "max_tokens": 2,
                },
                worker_id=forced_worker_id,
                dp_rank=forced_dp_rank,
                return_worker_ids=True,
            )
            assert isinstance(result, dict), f"Expected dict result, got {type(result)}"
            responses.append(result)
            if sleep_after > 0:
                await asyncio.sleep(sleep_after)

        req4 = responses[3]
        assert req4["prefill_worker_id"] == worker_a_id, (
            f"Request 4: expected prefill_worker_id={worker_a_id} (longest prefix match), "
            f"got {req4['prefill_worker_id']}"
        )
        if test_dp_rank:
            assert req4["prefill_dp_rank"] == dp_rank_a, (
                f"Request 4: expected prefill_dp_rank={dp_rank_a} "
                f"(longest prefix match), got {req4['prefill_dp_rank']}"
            )

        req5 = responses[4]
        assert req5["prefill_worker_id"] == worker_b_id, (
            f"Request 5: expected prefill_worker_id={worker_b_id} (tiebreak by smaller tree), "
            f"got {req5['prefill_worker_id']}"
        )
        if test_dp_rank:
            assert req5["prefill_dp_rank"] == dp_rank_b, (
                f"Request 5: expected prefill_dp_rank={dp_rank_b} "
                f"(tiebreak by smaller tree), got {req5['prefill_dp_rank']}"
            )

        await wait_for_worker_ids(consumer_endpoint, expected_num_instances)

    asyncio.run(test_sync())


551
552
553
554
555
556
557
def _test_python_router_bindings(
    engine_workers,
    endpoint,
    block_size: int,
    model_name: str,
    num_workers: int,
):
558
    """Test KvRouter Python bindings with token streaming and config overrides.
559

560
    Assumes engine_workers are already initialized. This test creates a KvRouter
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
    Python object and sends three test requests to verify:
    1. Token streaming with full router config overrides (overlap_score_weight, router_temperature)
    2. Token streaming without any overrides (uses default config)
    3. Token streaming with partial override (only router_temperature)

    All requests use ignore_eos=True with varying max_tokens to test token generation control.

    Args:
        engine_workers: Backend workers (mocker/vllm) already initialized with __enter__()
        endpoint: Dynamo endpoint for the workers
        block_size: Block size for KV cache
        model_name: Model name to use for requests
        num_workers: Expected number of workers

    Raises:
        AssertionError: If requests fail or router doesn't work correctly
    """
    # Create KvRouterConfig with default settings
579
    kv_router_config = KvRouterConfig()
580

581
    # Create KvRouter Python object
582
583
584
585
586
587
    with min_initial_workers_env(num_workers):
        kv_router = KvRouter(
            endpoint=endpoint,
            block_size=block_size,
            kv_router_config=kv_router_config,
        )
588

589
    logger.info("Created KvRouter Python object")
590
591

    # Wait for workers to be ready
592
    asyncio.run(wait_for_workers_ready(endpoint, kv_router, num_workers, model_name))
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609

    # Generate random token IDs (100 to 200 tokens)
    num_input_tokens = random.randint(100, 200)
    token_ids = [random.randint(1, 10000) for _ in range(num_input_tokens)]

    # Set up override parameters
    router_config_override = {
        "overlap_score_weight": 0.5,  # Override the default weight
        "router_temperature": 0.5,  # Override the default temperature
    }

    logger.info(f"Generated {num_input_tokens} random token IDs")

    # Test with full overrides
    logger.info(f"Testing with full router config overrides: {router_config_override}")
    asyncio.run(
        send_request_via_python_kv_router(
610
            kv_python_router=kv_router,
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
            model_name=model_name,
            token_ids=token_ids,
            initial_wait=1.0,
            max_retries=8,
            stop_conditions={
                "ignore_eos": True,  # Don't stop on EOS token
                "max_tokens": 20,  # Generate exactly 20 tokens
            },
            sampling_options={"temperature": 0.7, "top_p": 0.9},
            output_options={
                "include_input_tokens": False,
                "return_full_text": False,
            },
            router_config_override=router_config_override,
        )
    )

    # Test without overrides
    logger.info("Testing without router config overrides")
    asyncio.run(
        send_request_via_python_kv_router(
632
            kv_python_router=kv_router,
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
            model_name=model_name,
            token_ids=token_ids[:50],  # Use fewer tokens for second test,
            initial_wait=1.0,
            max_retries=8,
            stop_conditions={
                "ignore_eos": True,  # Don't stop on EOS token
                "max_tokens": 10,  # Generate exactly 10 tokens for the second test
            },
            sampling_options={"temperature": 0.7, "top_p": 0.9},
            output_options={
                "include_input_tokens": False,
                "return_full_text": False,
            },
            # No router_config_override this time
        )
    )

    # Test with partial override (only temperature)
    partial_override = {"router_temperature": 0.1}
    logger.info(f"Testing with partial router config overrides: {partial_override}")
    asyncio.run(
        send_request_via_python_kv_router(
655
            kv_python_router=kv_router,
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
            model_name=model_name,
            token_ids=token_ids[:30],  # Use fewer tokens for third test,
            initial_wait=1.0,
            max_retries=8,
            stop_conditions={
                "ignore_eos": True,  # Don't stop on EOS token
                "max_tokens": 5,  # Generate exactly 5 tokens for the third test
            },
            sampling_options={"temperature": 0.7, "top_p": 0.9},
            output_options={
                "include_input_tokens": False,
                "return_full_text": False,
            },
            router_config_override=partial_override,
        )
    )

673
    logger.info("KvRouter bindings test completed successfully")
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709


def _test_router_query_instance_id(
    engine_workers,
    block_size: int,
    request,
    frontend_port: int,
    test_payload: dict,
    store_backend: str = "etcd",
):
    """Test query_instance_id annotation returns worker_instance_id and token_data without routing.

    Assumes engine_workers are already initialized. This function manages router lifecycle.

    This tests the early return optimization where a request with 'nvext.annotations': ['query_instance_id']
    receives metadata without waiting for model generation. The router should:
    1. NOT route the request to a worker for generation
    2. Return worker_instance_id as an SSE event (which worker would handle it)
    3. Return token_data as an SSE event (the tokenized input)
    4. Terminate the stream with [DONE]

    This is useful for clients that want to know which worker will handle a request before
    committing to the full generation (e.g., for request routing decisions).

    Args:
        engine_workers: Backend workers (mocker/vllm) already initialized with __enter__()
        block_size: Block size for KV cache
        request: Pytest request fixture for managing resources
        frontend_port: Port for the frontend HTTP server
        test_payload: Base test payload to send to /v1/chat/completions
        store_backend: Storage backend to use ("etcd" or "file"). Defaults to "etcd".

    Raises:
        AssertionError: If annotation response structure is incorrect or contains generation content
    """

710
711
712
    with KVRouterProcess(
        request, block_size, frontend_port, engine_workers.namespace, store_backend
    ):
713
714
715
716
717
718
719
720
721
722
        # Start KV router (frontend)
        logger.info(f"Starting KV router frontend on port {frontend_port}")

        url = f"http://localhost:{frontend_port}/v1/chat/completions"

        # Send a warming request first to ensure system is ready
        logger.info("Sending warming request without annotations...")
        asyncio.run(send_request_with_retry(url, test_payload))

        # Test payload with query_instance_id annotation
723
        # Format: "query_instance_id:" (colon with empty value) for GAIE aggregated mode
724
725
        annotated_payload = {
            **test_payload,
726
            "nvext": {"annotations": ["query_instance_id:"]},
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
        }

        async def test_annotation_response():
            """Send request with query_instance_id and validate response structure"""
            async with aiohttp.ClientSession() as session:
                logger.info("Sending request with query_instance_id annotation...")

                async with session.post(url, json=annotated_payload) as response:
                    assert (
                        response.status == 200
                    ), f"Expected 200 but got {response.status}"

                    # Collect all response chunks
                    response_chunks = []
                    async for chunk in response.content:
                        if chunk:
                            chunk_str = chunk.decode("utf-8", errors="replace")
                            response_chunks.append(chunk_str)

                    full_response = "".join(response_chunks)
                    logger.info(
                        f"Full SSE response ({len(full_response)} bytes):\n{full_response}"
                    )

751
752
                    # Parse the SSE response to extract the first chunk with nvext data
                    # New format: nvext contains worker_id and token_ids
753
                    sse_parts = full_response.split("\n\n")
754
755
                    worker_id_info = None
                    token_list = None
756
757
758

                    for part in sse_parts:
                        part = part.strip()
759
                        if not part or not part.startswith("data:"):
760
761
                            continue

762
763
764
                        data_str = part.split("data:", 1)[1].strip()
                        if data_str == "[DONE]":
                            continue
765

766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
                        try:
                            chunk = json.loads(data_str)
                            logger.info(f"Parsed chunk: {json.dumps(chunk, indent=2)}")

                            # Extract nvext data containing worker_id and token_ids
                            nvext = chunk.get("nvext", {})
                            if nvext:
                                if "worker_id" in nvext:
                                    worker_id_info = nvext["worker_id"]
                                    logger.info(
                                        f"Found worker_id info: {worker_id_info}"
                                    )
                                if "token_ids" in nvext:
                                    token_list = nvext["token_ids"]
                                    logger.info(
                                        f"Found token_ids: {len(token_list)} tokens"
                                    )
                        except json.JSONDecodeError:
                            continue

                    # Validate worker_id info
787
                    assert (
788
789
                        worker_id_info is not None
                    ), f"Missing worker_id in nvext. Response: {full_response}"
790

791
792
793
                    # For aggregated mode, both prefill and decode should be the same
                    prefill_worker_id = worker_id_info.get("prefill_worker_id")
                    decode_worker_id = worker_id_info.get("decode_worker_id")
794
                    assert (
795
796
797
798
799
800
801
802
                        prefill_worker_id is not None
                    ), f"Missing prefill_worker_id in worker_id: {worker_id_info}"
                    assert (
                        decode_worker_id is not None
                    ), f"Missing decode_worker_id in worker_id: {worker_id_info}"
                    assert (
                        prefill_worker_id == decode_worker_id
                    ), f"For aggregated mode, prefill and decode worker should be same: {worker_id_info}"
803

804
805
806
807
                    # Validate token_ids
                    assert (
                        token_list is not None
                    ), f"Missing token_ids in nvext. Response: {full_response}"
808
809
                    assert isinstance(
                        token_list, list
810
                    ), f"token_ids should be a list, got: {type(token_list)}"
811
812
                    assert (
                        len(token_list) > 0
813
                    ), f"token_ids should not be empty: {token_list}"
814
815
816
817
818
                    assert all(
                        isinstance(token, int) for token in token_list
                    ), f"All tokens should be integers: {token_list}"

                    logger.info(
819
                        f"Valid token_ids with {len(token_list)} tokens: {token_list[:10]}{'...' if len(token_list) > 10 else ''}"
820
821
822
                    )

                    return {
823
824
                        "prefill_worker_id": prefill_worker_id,
                        "decode_worker_id": decode_worker_id,
825
826
827
828
829
830
831
                        "token_count": len(token_list),
                        "tokens": token_list,
                    }

        result = asyncio.run(test_annotation_response())

        logger.info("Successfully validated query_instance_id annotation response:")
832
833
        logger.info(f"Prefill Worker ID: {result['prefill_worker_id']}")
        logger.info(f"Decode Worker ID: {result['decode_worker_id']}")
834
835
836
        logger.info(f"Token count: {result['token_count']}")


837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
def _parse_frontend_rejection_metric(
    metrics_text: str, model_name: str, endpoint: str
) -> int:
    """Parse frontend model_rejection_total from Prometheus metrics text.

    Args:
        metrics_text: Raw Prometheus metrics text
        model_name: The model name label value
        endpoint: The endpoint label value (e.g. "chat_completions")

    Returns:
        The metric count, or 0 if not found
    """
    metric_name = f"{name_prefix.FRONTEND}_{frontend_service.MODEL_REJECTION_TOTAL}"
    for line in metrics_text.splitlines():
        if not line.startswith(f"{metric_name}{{"):
            continue
        if f'model="{model_name}"' in line and f'endpoint="{endpoint}"' in line:
            parts = line.rsplit(None, 1)
            if len(parts) == 2:
                try:
                    return int(float(parts[1]))
                except ValueError:
                    pass
    return 0


def _verify_frontend_rejection_metrics(
    frontend_port: int,
    model_name: str,
    endpoint: str,
    expected_count: int,
) -> None:
    """Verify frontend rejection metrics by scraping the /metrics endpoint.

    Args:
        frontend_port: Port where the frontend /metrics is served
        model_name: The model name label value
        endpoint: The endpoint label value (e.g. "chat_completions")
        expected_count: Expected rejection count to match exactly
    """
    metrics_url = f"http://localhost:{frontend_port}/metrics"
    try:
        metrics_response = requests.get(metrics_url, timeout=5)
        metrics_response.raise_for_status()
    except requests.RequestException as e:
        raise AssertionError(
            f"Failed to fetch frontend metrics from {metrics_url}: {e}"
        ) from e

    metric_count = _parse_frontend_rejection_metric(
        metrics_response.text, model_name, endpoint
    )
    logger.info(f"Frontend rejection metric: model_rejection_total={metric_count}")
    assert metric_count == expected_count, (
        f"Frontend model_rejection_total ({metric_count}) does not match "
        f"expected count ({expected_count})"
    )


897
898
899
900
901
902
def _test_router_overload_503(
    engine_workers,
    block_size: int,
    request,
    frontend_port: int,
    test_payload: dict,
903
    blocks_threshold: float = 0.2,
904
):
905
    """Test that 503 is returned when all workers are busy, and verify rejection metrics.
906
907
908
909

    Assumes engine_workers are already initialized. This function manages router lifecycle.
    Uses limited resources to intentionally trigger the overload condition.

910
911
912
913
914
    Sends staggered requests (0.1s apart) to exhaust worker resources, then verifies:
    1. At least one request succeeds (routed before busy state propagates)
    2. At least one request is rejected with 503 (worker busy)
    3. The frontend model_rejection_total metric matches the observed 503 count

915
916
917
918
919
920
    Args:
        engine_workers: Backend workers (mocker/vllm) already initialized with __enter__()
        block_size: Block size for KV cache (should be small to exhaust quickly, e.g. 4)
        request: Pytest request fixture for managing resources
        frontend_port: Port for the frontend HTTP server
        test_payload: Base test payload to send to /v1/chat/completions
921
        blocks_threshold: Active decode blocks threshold for the router (default 0.2)
922
923

    Raises:
924
        AssertionError: If success/rejection counts or metrics don't meet expectations
925
    """
926
927
928
    logger.info(
        f"Starting KV router frontend on port {frontend_port} with limited resources"
    )
929

930
931
932
933
934
935
936
    with KVRouterProcess(
        request=request,
        block_size=block_size,
        frontend_port=frontend_port,
        namespace=engine_workers.namespace,
        blocks_threshold=blocks_threshold,
    ):
937
        frontend_url = f"http://localhost:{frontend_port}"
938
939
940
941
942
943
944
945
        url = f"http://localhost:{frontend_port}/v1/chat/completions"

        # Custom payload for 503 test with more tokens to consume resources
        test_payload_503 = {
            **test_payload,
            "max_tokens": 50,  # Longer output to consume more blocks
        }

946
947
948
949
950
951
952
953
        logger.info("Waiting for frontend readiness before overload test...")
        asyncio.run(
            wait_for_frontend_ready(
                frontend_url=frontend_url,
                expected_num_workers=1,
                timeout=60,
            )
        )
954

955
        logger.info("Launching streaming requests until the router returns 503...")
956
957

        async def exhaust_resources_and_verify_503():
958
959
            stop_event = asyncio.Event()

960
961
962
            async with aiohttp.ClientSession() as session:
                tasks = []

963
964
965
966
967
968
969
970
971
972
973
974
975
976
                async def send_request(req_id, payload):
                    try:
                        async with session.post(url, json=payload) as response:
                            if response.status == 200:
                                logger.info(f"Request {req_id} accepted")
                                await stop_event.wait()
                                return response.status

                            if response.status == 503:
                                body = await response.json()
                                logger.info(
                                    f"Request {req_id} got expected 503: {body}"
                                )
                                stop_event.set()
977
978
979
980
981
                                error_msg = body.get("message", "")
                                assert (
                                    "Service temporarily unavailable" in error_msg
                                    or "All workers are busy" in error_msg
                                ), f"Expected service overload error message, got: {body}"
982
983
984
985
986
987
988
989
990
991
992
                                return response.status

                            body = await response.text()
                            logger.info(
                                f"Request {req_id} got unexpected status {response.status}: {body}"
                            )
                            return response.status
                    except asyncio.CancelledError:
                        raise
                    except Exception as e:
                        logger.info(f"Request {req_id} failed: {e}")
993
                        raise
994
995

                try:
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
                    for i in range(50):
                        if stop_event.is_set():
                            break

                        content_words = test_payload["messages"][0]["content"].split()
                        random.shuffle(content_words)
                        shuffled_content = " ".join(content_words)
                        unique_payload = {
                            **test_payload_503,
                            "messages": [
                                {
                                    **test_payload["messages"][0],
                                    "content": shuffled_content,
                                }
                            ],
                        }
                        tasks.append(
                            asyncio.create_task(send_request(i, unique_payload))
                        )
                        await asyncio.sleep(0.1)

                    if not stop_event.is_set():
                        try:
                            await asyncio.wait_for(stop_event.wait(), timeout=10)
                        except asyncio.TimeoutError:
                            logger.error("Timed out waiting for overload 503")
1022
                finally:
1023
1024
1025
                    stop_event.set()
                    done, pending = await asyncio.wait(tasks, timeout=3)
                    for task in pending:
1026
                        task.cancel()
1027
1028
                    await asyncio.gather(*pending, return_exceptions=True)

1029
                return [t.result() for t in done]
1030

1031
        results = asyncio.run(exhaust_resources_and_verify_503())
1032

1033
1034
1035
1036
        # Count outcomes
        num_succeeded = sum(1 for s in results if s == 200)
        num_rejected = sum(1 for s in results if s == 503)
        num_other = sum(1 for s in results if s not in (200, 503))
1037

1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
        logger.info(
            f"Results: {num_succeeded} succeeded, {num_rejected} rejected (503), "
            f"{num_other} other"
        )

        # Assert minimum thresholds
        assert (
            num_other == 0
        ), f"Expected only 200 or 503 responses, but got {num_other} other"
        assert (
            num_rejected > 0
        ), f"Expected at least 1 rejection, but got {num_rejected}"
        assert (
            num_succeeded > 0
        ), f"Expected at least 1 success, but got {num_succeeded}"

        # Verify rejection metrics from frontend /metrics endpoint
        model_name = test_payload.get("model", "")
        _verify_frontend_rejection_metrics(
            frontend_port, model_name, "chat_completions", num_rejected
        )

        logger.info(
            f"Successfully verified overload 503: {num_rejected} rejected, "
            f"{num_succeeded} succeeded, metrics match"
        )
1064
1065


1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
async def _zmq_replay_cycle(
    phase: int,
    router,
    router_name: str,
    endpoint,
    indexer_url: str,
    engine_workers,
    send_requests_to_router,
):
    """Pause indexer listeners → send gap requests → resume → send to trigger replay."""
    await asyncio.sleep(1)
    worker_ids = list(engine_workers.worker_id_to_zmq_ports.keys())
    dp_size = getattr(engine_workers, "dp_size", None) or 1

    logger.info(f"=== ZMQ REPLAY TEST: Phase {phase} ({router_name}) ===")
    async with aiohttp.ClientSession() as session:
        for wid in worker_ids:
            for dp_rank in range(dp_size):
                async with session.post(
                    f"{indexer_url}/test/pause_listener",
                    json={"instance_id": wid, "dp_rank": dp_rank},
                ) as resp:
                    assert (
                        resp.status == 200
                    ), f"Pause {wid}:{dp_rank} failed: {await resp.text()}"

    logger.info("Sending 10 requests while indexer listeners are paused")
    successful_gap = await send_requests_to_router(
        router, 10, f"{router_name} (indexer paused)", endpoint
    )
    assert (
        successful_gap == 10
    ), f"Expected 10 requests while paused, got {successful_gap}"

    async with aiohttp.ClientSession() as session:
        for wid in worker_ids:
            for dp_rank in range(dp_size):
                async with session.post(
                    f"{indexer_url}/test/resume_listener",
                    json={"instance_id": wid, "dp_rank": dp_rank},
                ) as resp:
                    assert (
                        resp.status == 200
                    ), f"Resume {wid}:{dp_rank} failed: {await resp.text()}"

    logger.info("Sending 5 requests after resume (triggers gap detection + replay)")
    successful_post = await send_requests_to_router(
        router, 5, f"{router_name} (post-resume)", endpoint
    )
    assert (
        successful_post == 5
    ), f"Expected 5 requests post-resume, got {successful_post}"
    await asyncio.sleep(2)


1121
1122
1123
1124
1125
1126
def _test_router_indexers_sync(
    engine_workers,
    block_size: int,
    model_name: str,
    num_workers: int,
    store_backend: str = "etcd",
1127
1128
1129
    request_plane: str = "nats",
    test_nats_interruption: bool = False,
    nats_server: Optional["NatsServer"] = None,
1130
    durable_kv_events: bool = False,
1131
    router_event_threads: int = 4,
1132
    standalone_indexer_url: Optional[str] = None,
1133
    standalone_indexer_b_url: Optional[str] = None,
1134
    test_zmq_replay: bool = False,
1135
1136
1137
1138
):
    """Test that two KV routers have synchronized indexer states after processing requests.

    Assumes engine_workers are already initialized. This test:
1139
1140
    1. Creates first KvRouter (with its own runtime) and sends 25 requests (triggers snapshot at threshold=20)
    2. Creates second KvRouter (with its own runtime, should sync from NATS snapshot)
1141
1142
1143
1144
1145
1146
    3. Sends 25 requests to second router
    4. Verifies NATS object store contains the snapshot
    5. Dumps states from both routers and compares them (should be identical)

    This validates that the snapshot mechanism works and routers can sync state from NATS.

1147
1148
1149
1150
1151
1152
1153
1154
1155
    When test_nats_interruption=True (requires nats_server and request_plane="tcp"):
    - After first router sends 25 requests, NATS is stopped
    - 10 more requests sent while NATS is down (stored locally by local indexer)
    - NATS restarted (fresh state), recovery mechanism re-syncs
    - Second router starts and sends 25 requests
    - NATS stopped again, 10 more requests sent
    - NATS restarted, 5 more requests sent
    - Verify both routers converge to same state

1156
    Args:
1157
        engine_workers: Backend worker instance ({MockerProcess, VLLMProcess, TRTLLMProcess}) (already initialized with __enter__())
1158
1159
1160
1161
        block_size: Block size for KV cache
        model_name: Model name to use for requests
        num_workers: Expected number of workers
        store_backend: Storage backend to use ("etcd" or "file"). Defaults to "etcd".
1162
1163
1164
        request_plane: Request plane to use ("nats" or "tcp"). Defaults to "nats".
        test_nats_interruption: If True, test NATS interruption recovery. Defaults to False.
        nats_server: NatsServer instance for stop/start (required if test_nats_interruption=True).
1165
        durable_kv_events: If True, use durable KV events (JetStream). Defaults to False.
1166
1167
1168
1169

    Raises:
        AssertionError: If router states don't synchronize correctly or snapshot is missing
    """
1170
1171
    if test_nats_interruption and nats_server is None:
        raise ValueError("nats_server is required when test_nats_interruption=True")
1172
1173
1174
1175

    # Use async to manage the test flow
    async def test_sync():
        # Create KvRouterConfig with lower snapshot threshold for testing
1176
1177
1178
        kv_router_config = KvRouterConfig(
            router_snapshot_threshold=20,
            durable_kv_events=durable_kv_events,
Yan Ru Pei's avatar
Yan Ru Pei committed
1179
            router_event_threads=router_event_threads,
1180
        )
1181

1182
        # If standalone indexer mode, launch workers one-by-one and register.
1183
1184
1185
1186
1187
1188
        # We need to create a temporary endpoint just to discover worker IDs.
        if standalone_indexer_url:
            tmp_runtime = get_runtime(store_backend, request_plane)
            tmp_endpoint = tmp_runtime.endpoint(
                f"{engine_workers.namespace}.{engine_workers.component_name}.generate"
            )
1189
            await engine_workers.launch_workers_with_indexer(tmp_endpoint)
1190

1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
        async def send_requests_to_router(router, num_requests, router_name, endpoint):
            # Now send the actual requests
            tasks = []
            for i in range(num_requests):
                # Generate random token IDs for each request
                logger.debug(f"Sending request {i + 1}/{num_requests} to {router_name}")

                # Generate 30 random tokens
                request_tokens = [random.randint(1, 10000) for _ in range(30)]

                # Send request to mocker via the router
                tasks.append(
                    asyncio.create_task(
                        send_request_via_python_kv_router(
                            kv_python_router=router,
                            model_name=model_name,
                            token_ids=request_tokens,
                            initial_wait=1.0,
                            max_retries=8,
                            stop_conditions={
                                "ignore_eos": True,  # Don't stop on EOS token
                                "max_tokens": 10,  # Generate exactly 10 tokens
                            },
                        )
                    )
                )

            # Wait for all requests to complete
            results = await asyncio.gather(*tasks)
            successful = sum(1 for r in results if r)
            logger.info(
                f"Completed {successful}/{num_requests} requests for {router_name}"
            )
            return successful

        # Create first runtime and endpoint for router 1
        logger.info("Creating first KV router with its own runtime")
1228
        runtime1 = get_runtime(store_backend, request_plane)
1229
1230
1231
        endpoint1 = runtime1.endpoint(
            f"{engine_workers.namespace}.{engine_workers.component_name}.generate"
        )
1232

1233
1234
1235
1236
1237
1238
        with min_initial_workers_env(num_workers):
            kv_router1 = KvRouter(
                endpoint=endpoint1,
                block_size=block_size,
                kv_router_config=kv_router_config,
            )
1239
1240

        # Wait for workers to be ready
1241
        await wait_for_workers_ready(endpoint1, kv_router1, num_workers, model_name)
1242
1243
1244
1245
1246
1247

        # Send 25 requests to first router
        logger.info("Sending 25 requests to first router")

        # Send requests to first router
        successful1 = await send_requests_to_router(
1248
            kv_router1, 25, "Router 1", endpoint1
1249
1250
1251
1252
1253
        )
        assert (
            successful1 == 25
        ), f"Expected 25 successful requests to router 1, got {successful1}"

1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
        # NATS interruption test: stop NATS, send requests, restart
        if test_nats_interruption:
            await asyncio.sleep(1)

            assert nats_server is not None  # Validated at function entry
            logger.info("=== NATS INTERRUPTION TEST: Phase 1 ===")
            logger.info("Stopping NATS server")
            nats_server.stop()

            logger.info("Sending 10 requests while NATS is down (via TCP)")
            successful_offline1 = await send_requests_to_router(
1265
                kv_router1, 10, "Router 1 (NATS down)", endpoint1
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
            )
            assert (
                successful_offline1 == 10
            ), f"Expected 10 successful requests while NATS down, got {successful_offline1}"

            logger.info("Restarting NATS server (fresh state)")
            nats_server.start()

            await asyncio.sleep(5)

1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
        if test_zmq_replay and standalone_indexer_url:
            await _zmq_replay_cycle(
                1,
                kv_router1,
                "Router 1",
                endpoint1,
                standalone_indexer_url,
                engine_workers,
                send_requests_to_router,
            )

1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
        # Wait for snapshot to be available before creating second router.
        # In JetStream mode, the background task may purge acknowledged messages
        # from the stream before the snapshot upload completes. Poll the object
        # store so Router 2 can reliably download the snapshot on startup.
        if durable_kv_events:
            component_subject = f"namespace.{engine_workers.namespace}.component.{engine_workers.component_name}"
            slugified = component_subject.lower().replace(".", "-").replace("_", "-")
            bucket_name = f"{slugified}-radix-bucket"
            nc = await nats.connect(servers=_nats_server())
            try:
                js = nc.jetstream()
                for attempt in range(50):
                    try:
                        obj_store = await js.object_store(bucket_name)
                        await obj_store.get("radix-state")
                        logger.info(
                            f"Snapshot available in object store (attempt {attempt + 1})"
                        )
                        break
                    except Exception:
                        await asyncio.sleep(0.1)
                else:
                    assert False, (
                        f"Snapshot not found in bucket '{bucket_name}' after 50 attempts (5s). "
                        f"Router 1 sent 25 requests with snapshot_threshold=20, snapshot should exist."
                    )
            finally:
                await nc.close()
        else:
            await asyncio.sleep(1)
1317
1318
1319

        # Create second runtime and endpoint for router 2
        logger.info("Creating second KV router with its own runtime")
1320
        runtime2 = get_runtime(store_backend, request_plane)
1321
1322
1323
        endpoint2 = runtime2.endpoint(
            f"{engine_workers.namespace}.{engine_workers.component_name}.generate"
        )
1324

1325
1326
1327
1328
1329
1330
        with min_initial_workers_env(num_workers):
            kv_router2 = KvRouter(
                endpoint=endpoint2,
                block_size=block_size,
                kv_router_config=kv_router_config,
            )
1331

1332
1333
1334
1335
        # Launch Indexer B alongside Router 2. Workers are passed via --workers
        # so ZMQ sockets connect before recovery, avoiding the slow-joiner problem.
        if standalone_indexer_b_url:
            engine_workers.launch_indexer()
1336
1337
1338
            await wait_for_indexer_workers_active(
                standalone_indexer_b_url, engine_workers.worker_id_to_zmq_ports
            )
1339
1340
1341
1342
1343
            logger.info(
                f"Launched Indexer B at {standalone_indexer_b_url} "
                f"(P2P recovery from Indexer A)"
            )

1344
1345
1346
        # Send 25 requests to second router with initial retry loop
        logger.info("Sending 25 requests to second router")
        successful2 = await send_requests_to_router(
1347
            kv_router2, 25, "Router 2", endpoint2
1348
1349
1350
1351
1352
        )
        assert (
            successful2 == 25
        ), f"Expected 25 successful requests to router 2, got {successful2}"

1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
        # NATS interruption test: stop NATS again, send requests, restart, send more
        if test_nats_interruption:
            await asyncio.sleep(1)

            assert nats_server is not None  # Validated at function entry
            logger.info("=== NATS INTERRUPTION TEST: Phase 2 ===")
            logger.info("Stopping NATS server")
            nats_server.stop()

            logger.info("Sending 10 requests while NATS is down (via TCP)")
            successful_offline2 = await send_requests_to_router(
1364
                kv_router2, 10, "Router 2 (NATS down)", endpoint2
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
            )
            assert (
                successful_offline2 == 10
            ), f"Expected 10 successful requests while NATS down, got {successful_offline2}"

            logger.info("Restarting NATS server (fresh state)")
            nats_server.start()
            await asyncio.sleep(5)

            logger.info("Sending 5 more requests after NATS recovery")
            successful_recovery = await send_requests_to_router(
1376
                kv_router1, 5, "Router 1 (post-recovery)", endpoint1
1377
1378
1379
1380
1381
            )
            assert (
                successful_recovery == 5
            ), f"Expected 5 successful requests post-recovery, got {successful_recovery}"

1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
        if test_zmq_replay and standalone_indexer_url:
            await _zmq_replay_cycle(
                2,
                kv_router2,
                "Router 2",
                endpoint2,
                standalone_indexer_url,
                engine_workers,
                send_requests_to_router,
            )

1393
        # Wait for internal synchronization and ZMQ event propagation
1394
        logger.info("Waiting for final synchronization")
1395
        await asyncio.sleep(2)
1396
1397

        # Verify NATS object store bucket was created with snapshot
1398
1399
        # Skip for NATS interruption test (restarts fresh) and non-durable modes
        if not test_nats_interruption and durable_kv_events:
1400
1401
1402
1403
1404
1405
1406
            # Mirror the Rust bucket naming logic from subscriber.rs:
            # component.subject() -> "namespace.{ns}.component.{comp}"
            # then slugify (convert dots to dashes, lowercase, etc) and append "-radix-bucket"
            component_subject = f"namespace.{engine_workers.namespace}.component.{engine_workers.component_name}"
            slugified = component_subject.lower().replace(".", "-").replace("_", "-")
            expected_bucket = f"{slugified}-radix-bucket"
            expected_file = "radix-state"
1407

1408
1409
            logger.info(f"Verifying NATS object store bucket exists: {expected_bucket}")
            snapshot_verified = False
1410
1411
1412
1413

            # Connect to NATS and check object store. This honors per-test NATS instances
            # started by fixtures (xdist-safe) instead of assuming localhost:4222.
            nc = await nats.connect(servers=_nats_server())
1414
            try:
1415
1416
                js = nc.jetstream()
                obj_store = await js.object_store(expected_bucket)
1417

1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
                # Try to get the expected file
                try:
                    result = await obj_store.get(expected_file)
                    logger.info(
                        f"✓ Snapshot file '{expected_file}' found in bucket '{expected_bucket}' "
                        f"(size: {len(result.data) if result.data else 0} bytes)"
                    )
                    snapshot_verified = True
                except Exception as e:
                    logger.error(
                        f"Snapshot file '{expected_file}' not found in bucket '{expected_bucket}': {e}"
                    )
1430
1431
            except Exception as e:
                logger.error(f"Error checking NATS object store: {e}")
1432
1433
            finally:
                await nc.close()
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443

            # Assert that snapshot was created (threshold=20, sent 25 requests)
            if not snapshot_verified:
                assert False, (
                    f"Expected snapshot to be created in bucket '{expected_bucket}' with file '{expected_file}'. "
                    f"Router sent 25 requests with snapshot_threshold=20, so snapshot should have been triggered."
                )
        else:
            logger.info(
                "Skipping NATS object store verification (NATS was restarted fresh for interruption test)"
1444
1445
            )

1446
1447
        # Dump states from all sources
        logger.info("Dumping states from all sources")
1448
1449
        state1_json = await kv_router1.dump_events()
        state2_json = await kv_router2.dump_events()
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469

        state1 = json.loads(state1_json)
        state2 = json.loads(state2_json)

        def sort_key(event):
            data = event["event"]["data"]["stored"]
            blocks = data["blocks"]
            first_block = blocks[0]
            return (
                event["worker_id"],
                first_block["tokens_hash"],
                data["parent_hash"],
            )

        sorted_state1 = sorted(state1, key=sort_key)
        sorted_state2 = sorted(state2, key=sort_key)

        logger.info(f"Router 1 has {len(sorted_state1)} events")
        logger.info(f"Router 2 has {len(sorted_state2)} events")

1470
        assert_event_dumps_equal(sorted_state1, sorted_state2, "Router 1", "Router 2")
1471
        logger.info("Successfully verified Router 1 and Router 2 states are equal")
1472

1473
        # Verify standalone HTTP indexers build the same tree (via ZMQ)
1474
1475
1476
1477
        if standalone_indexer_url:
            async with aiohttp.ClientSession() as session:
                async with session.get(f"{standalone_indexer_url}/dump") as resp:
                    assert resp.status == 200, f"GET /dump failed: {resp.status}"
1478
                    dump_a = await resp.json()
1479

1480
            # /dump returns {model:tenant -> {"block_size": N, "events": [...]}}
1481
            expected_key = f"{model_name}:default"
1482
            assert expected_key in dump_a, (
1483
                f"Expected dump key '{expected_key}', "
1484
                f"got keys={list(dump_a.keys())}"
1485
            )
1486
1487
1488
1489
1490
1491
            for k, v in dump_a.items():
                assert (
                    isinstance(v, dict) and "events" in v
                ), f"Dump key '{k}' returned unexpected format: {v}"
            sorted_standalone_a = sorted(dump_a[expected_key]["events"], key=sort_key)
            logger.info(f"Standalone Indexer A has {len(sorted_standalone_a)} events")
1492
1493

            assert_event_dumps_equal(
1494
                sorted_state1, sorted_standalone_a, "Router 1", "Standalone A"
1495
            )
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
            logger.info("Standalone A matches Router 1")

            if standalone_indexer_b_url:
                async with aiohttp.ClientSession() as session:
                    async with session.get(f"{standalone_indexer_b_url}/dump") as resp:
                        assert (
                            resp.status == 200
                        ), f"GET /dump from Indexer B failed: {resp.status}"
                        dump_b = await resp.json()

                assert expected_key in dump_b, (
                    f"Indexer B missing dump key '{expected_key}', "
                    f"got keys={list(dump_b.keys())}"
                )
                sorted_standalone_b = sorted(
                    dump_b[expected_key]["events"], key=sort_key
                )
                logger.info(
                    f"Standalone Indexer B has {len(sorted_standalone_b)} events"
                )

                assert_event_dumps_equal(
                    sorted_standalone_a,
                    sorted_standalone_b,
                    "Standalone A",
                    "Standalone B",
                )
                logger.info(
                    "All 4 dumps match: Router 1, Router 2, "
                    "Standalone A, Standalone B"
                )
1527
1528

        # Verify NATS consumers are created (while routers are still alive)
1529
1530
        # Skip for NATS interruption test (restarts fresh) and non-durable modes
        if not test_nats_interruption and durable_kv_events:
1531
1532
1533
1534
            logger.info("Verifying NATS consumers exist for both routers")
            component_subject = f"namespace.{engine_workers.namespace}.component.{engine_workers.component_name}"
            slugified = component_subject.lower().replace(".", "-").replace("_", "-")
            stream_name = f"{slugified}-kv-events"
1535

1536
            nc = await nats.connect(servers=_nats_server())
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
            try:
                js = nc.jetstream()
                consumer_infos = await js.consumers_info(stream_name)
                consumer_names = [info.name for info in consumer_infos]
                logger.info(f"Found {len(consumer_names)} consumers: {consumer_names}")

                assert len(consumer_names) == 2, (
                    f"Expected 2 durable consumers (one per router), "
                    f"found {len(consumer_names)}: {consumer_names}"
                )
                logger.info("✓ Verified 2 durable consumers exist (one per router)")
            finally:
                await nc.close()
        else:
            logger.info(
                "Skipping NATS consumers verification (local indexer uses NATS Core, not JetStream)"
1553
1554
1555
1556
1557
1558
1559
1560
            )

    # Run the async test
    asyncio.run(test_sync())

    logger.info("Indexers sync test completed successfully")


1561
def _test_router_decisions_disagg(
1562
1563
1564
1565
1566
1567
1568
    prefill_workers,
    decode_workers,
    block_size: int,
    request,
    frontend_port: int,
    test_payload: dict,
    store_backend: str = "etcd",
1569
    request_plane: str = "nats",
1570
    durable_kv_events: bool = False,
1571
    router_aic_config: Optional[dict[str, Any]] = None,
1572
    enable_bootstrap: bool = False,
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
):
    """Validate KV cache prefix reuse in disaggregated prefill-decode setup via HTTP frontend.

    Assumes prefill_workers and decode_workers are already initialized. This function manages
    router lifecycle and sends progressive requests with overlapping prefixes.

    This test:
    1. Starts the KV router frontend with disagg support
    2. Sends 4 progressive requests where each extends the previous tokens by block_size
    3. Extracts prefill_worker_id and decode_worker_id from response nvext
    4. Verifies all prefill_worker_ids are the same (due to prefix reuse routing)
    5. Verifies prefill_worker_id is NOT in the set of decode_worker_ids (true disagg)

    Args:
        prefill_workers: Prefill workers already initialized with __enter__()
        decode_workers: Decode workers already initialized with __enter__()
        block_size: Block size for KV cache
        request: Pytest request fixture for managing resources
        frontend_port: Port for the frontend HTTP server
        test_payload: Base test payload to send to /v1/chat/completions
        store_backend: Storage backend to use ("etcd" or "file"). Defaults to "etcd".
1594
        durable_kv_events: If True, use durable KV events (JetStream). Defaults to False.
1595
        router_aic_config: Optional AIC router perf-model config for frontend KV routing.
1596
1597
1598
1599
1600

    Raises:
        AssertionError: If prefill_worker_ids differ across requests (prefix reuse failure)
        AssertionError: If prefill_worker_id is in decode_worker_ids (not true disagg)
    """
1601
1602
1603
1604
1605
1606
    with KVRouterProcess(
        request,
        block_size,
        frontend_port,
        decode_workers.namespace,
        store_backend,
1607
        enforce_disagg=True,
1608
1609
        request_plane=request_plane,
        durable_kv_events=durable_kv_events,
1610
        min_initial_workers=decode_workers.num_workers,
1611
        router_aic_config=router_aic_config,
1612
    ):
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
        # Start KV router frontend - uses decode_workers namespace for discovery
        # The frontend will auto-discover both prefill and decode workers
        logger.info(
            f"Starting KV router frontend on port {frontend_port} for disagg test"
        )

        frontend_url = f"http://localhost:{frontend_port}"
        chat_url = f"{frontend_url}/v1/chat/completions"

        # Wait for workers to register with frontend
        logger.info(
            "Waiting for prefill and decode workers to register with frontend..."
        )
        asyncio.run(
            wait_for_frontend_ready(
                frontend_url=frontend_url,
                expected_num_workers=decode_workers.num_workers,
                timeout=120,
            )
        )

        async def send_progressive_requests():
            """Send 4 progressive requests with overlapping prefixes and collect worker IDs."""
            prefill_worker_ids = []
            decode_worker_ids = []

            # Generate base tokens for progressive prefix extension
            base_content = test_payload["messages"][0]["content"]

            async with aiohttp.ClientSession() as session:
                for i in range(4):
                    # Build progressive content by repeating base content
                    # Each iteration adds more content to extend the prefix
                    progressive_content = " ".join([base_content] * (i + 1))

1648
                    # Create payload with worker_id and timing in extra_fields
1649
1650
1651
1652
1653
1654
1655
1656
                    payload = {
                        **test_payload,
                        "messages": [
                            {
                                "role": "user",
                                "content": progressive_content,
                            }
                        ],
1657
                        "nvext": {"extra_fields": ["worker_id", "timing"]},
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
                        "stream": True,
                    }

                    logger.info(
                        f"Sending request {i + 1}/4 with progressive prefix "
                        f"(~{len(progressive_content)} chars)"
                    )

                    async with session.post(chat_url, json=payload) as response:
                        assert (
                            response.status == 200
                        ), f"Request {i + 1} failed with status {response.status}"

1671
                        # Collect all chunks and look for nvext with worker_id and timing
1672
1673
                        prefill_wid = None
                        decode_wid = None
1674
                        timing_info = None
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689

                        async for line in response.content:
                            if not line:
                                continue

                            line_str = line.decode("utf-8", errors="replace").strip()
                            if not line_str.startswith("data:"):
                                continue

                            data_str = line_str[5:].strip()
                            if data_str == "[DONE]":
                                break

                            try:
                                data = json.loads(data_str)
1690
                                # Check for nvext in the response
1691
                                nvext = data.get("nvext", {})
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
                                if nvext:
                                    worker_id_info = nvext.get("worker_id", {})
                                    if worker_id_info:
                                        if "prefill_worker_id" in worker_id_info:
                                            prefill_wid = worker_id_info[
                                                "prefill_worker_id"
                                            ]
                                        if "decode_worker_id" in worker_id_info:
                                            decode_wid = worker_id_info[
                                                "decode_worker_id"
                                            ]
                                    # Timing info appears in final chunk
                                    if "timing" in nvext:
                                        timing_info = nvext["timing"]
1706
1707
1708
1709
1710
1711

                            except json.JSONDecodeError:
                                continue

                        logger.info(
                            f"Request {i + 1}: prefill_worker_id={prefill_wid}, "
1712
                            f"decode_worker_id={decode_wid}, timing={timing_info}"
1713
1714
1715
1716
1717
1718
1719
                        )

                        if prefill_wid is not None:
                            prefill_worker_ids.append(prefill_wid)
                        if decode_wid is not None:
                            decode_worker_ids.append(decode_wid)

1720
1721
1722
                        # Verify timing info is present and valid.
                        # kv_transfer_estimated_latency_ms is measured on both the original
                        # and bootstrap prefill paths (uses first_token_time as stop).
1723
1724
1725
                        assert (
                            timing_info is not None
                        ), f"Request {i + 1}: Expected timing info in final chunk, got None"
1726
                        verify_response_timing(timing_info, disagg=not enable_bootstrap)
1727

1728
                    # Small delay between requests
1729
                    await asyncio.sleep(1)
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744

            return prefill_worker_ids, decode_worker_ids

        # Run the progressive requests
        prefill_ids, decode_ids = asyncio.run(send_progressive_requests())

        logger.info(f"Collected prefill_worker_ids: {prefill_ids}")
        logger.info(f"Collected decode_worker_ids: {decode_ids}")

        # Verify we got worker IDs from all requests
        assert len(prefill_ids) == 4, (
            f"Expected 4 prefill_worker_ids, got {len(prefill_ids)}. "
            f"Make sure nvext.extra_fields=['worker_id'] is being processed."
        )

1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
        # Verify prefix reuse behavior.
        #
        # In JetStream (KV events enabled) mode, the router learns cache state from KV events.
        # With the TCP request plane, we can observe a transient on the *first* request where
        # the second request is routed before the first request's KV "stored" events have been
        # fully ingested. After ingestion, routing stabilizes.
        #
        # So for TCP we assert that requests 2-4 converge to the same prefill worker; for NATS
        # request plane we keep the stronger assertion that all 4 match.
        if request_plane == "tcp":
            unique_prefill_ids = set(prefill_ids[1:])
            assert len(unique_prefill_ids) == 1, (
                f"Expected prefill requests 2-4 to route to the same worker due to prefix reuse, "
                f"but found {len(unique_prefill_ids)} unique prefill_worker_ids: {unique_prefill_ids}. "
                f"Full list: {prefill_ids}"
            )
        else:
            unique_prefill_ids = set(prefill_ids)
            assert len(unique_prefill_ids) == 1, (
                f"Expected all prefill requests to route to the same worker due to prefix reuse, "
                f"but found {len(unique_prefill_ids)} unique prefill_worker_ids: {unique_prefill_ids}. "
                f"Full list: {prefill_ids}"
            )
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784

        # Verify prefill_worker_id is NOT in decode_worker_ids (true disagg)
        unique_decode_ids = set(decode_ids)
        prefill_id = prefill_ids[0]
        assert prefill_id not in unique_decode_ids, (
            f"Prefill worker {prefill_id} should NOT be in decode workers {unique_decode_ids}. "
            f"This suggests disaggregated mode is not working correctly - "
            f"prefill and decode should use separate worker pools."
        )

        logger.info(
            f"Successfully verified disaggregated routing:\n"
            f"  - All 4 requests routed to same prefill_worker_id={prefill_id} (prefix reuse)\n"
            f"  - Prefill worker is NOT in decode worker set {unique_decode_ids} (true disagg)"
        )


1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
def _test_router_decisions_disagg_round_robin_prefill_dp_rank(
    prefill_workers,
    decode_workers,
    block_size: int,
    request,
    frontend_port: int,
    test_payload: dict,
    expected_prefill_dp_ranks: int,
    store_backend: str = "etcd",
    request_plane: str = "nats",
):
    """Verify disaggregated round-robin requests store prefill KV blocks across DP ranks."""

    with FrontendRouterProcess(
        request,
        block_size,
        frontend_port,
        decode_workers.namespace,
        store_backend,
        enforce_disagg=True,
        request_plane=request_plane,
        router_mode="round-robin",
        min_initial_workers=decode_workers.num_workers,
    ):
        logger.info(
            "Starting round-robin frontend on port %s for disagg prefill dp-rank test",
            frontend_port,
        )

        async def test_sync():
            frontend_url = f"http://localhost:{frontend_port}"
            chat_url = f"{frontend_url}/v1/chat/completions"
            await wait_for_frontend_ready(
                frontend_url=frontend_url,
                expected_num_workers=decode_workers.num_workers,
                timeout=120,
            )

            runtime = get_runtime(
                store_backend=store_backend, request_plane=request_plane
            )
            prefill_endpoint = runtime.endpoint(
                f"{prefill_workers.namespace}.prefill.generate"
            )

            with min_initial_workers_env(prefill_workers.num_workers):
                observer_router = KvRouter(
                    endpoint=prefill_endpoint,
                    block_size=block_size,
                    kv_router_config=KvRouterConfig(
                        router_snapshot_threshold=20,
                        use_kv_events=True,
                        durable_kv_events=False,
                        router_event_threads=4,
                        router_track_prefill_tokens=True,
                        router_prefill_load_model="none",
                    ),
                )

            client = await prefill_endpoint.client()
            worker_ids: list[int] = []
            deadline = asyncio.get_running_loop().time() + 60
            while asyncio.get_running_loop().time() < deadline:
                worker_ids = sorted(set(client.instance_ids()))
                if len(worker_ids) >= prefill_workers.num_workers:
                    break
                await asyncio.sleep(1.0)

            assert len(worker_ids) == prefill_workers.num_workers, (
                f"Timed out waiting for prefill workers. "
                f"Found {worker_ids}, expected {prefill_workers.num_workers}"
            )
            prefill_worker_id = worker_ids[0]

            def stored_blocks_by_dp_rank(events_json: str) -> dict[int, int]:
                counts = {dp_rank: 0 for dp_rank in range(expected_prefill_dp_ranks)}
                for event in json.loads(events_json):
                    if event.get("worker_id") != prefill_worker_id:
                        continue
                    stored = event.get("event", {}).get("data", {}).get("stored")
                    if stored is None:
                        continue
                    dp_rank = event.get("event", {}).get("dp_rank", 0)
                    counts[dp_rank] = counts.get(dp_rank, 0) + len(
                        stored.get("blocks", [])
                    )
                return counts

            await asyncio.sleep(2.0)
            baseline_counts = stored_blocks_by_dp_rank(
                await observer_router.dump_events()
            )

            async with aiohttp.ClientSession() as session:
                for request_idx in range(expected_prefill_dp_ranks * 2):
                    prompt_tokens = " ".join(
                        f"prefill-{request_idx}-token-{token_idx}"
                        for token_idx in range(block_size * 3)
                    )
                    payload = {
                        **test_payload,
                        "stream": False,
                        "max_tokens": 1,
                        "messages": [
                            {
                                "role": "user",
                                "content": prompt_tokens,
                            }
                        ],
                    }
                    async with session.post(chat_url, json=payload) as response:
                        assert response.status == 200, (
                            f"Request {request_idx + 1} failed with status "
                            f"{response.status}: {await response.text()}"
                        )
                        await response.text()
                    await asyncio.sleep(0.5)

            await asyncio.sleep(2.0)
            final_counts = stored_blocks_by_dp_rank(await observer_router.dump_events())
            return prefill_worker_id, baseline_counts, final_counts

        prefill_worker_id, baseline_counts, final_counts = asyncio.run(test_sync())

        delta_counts = {
            dp_rank: final_counts.get(dp_rank, 0) - baseline_counts.get(dp_rank, 0)
            for dp_rank in range(expected_prefill_dp_ranks)
        }
        active_dp_ranks = sorted(
            dp_rank for dp_rank, block_count in delta_counts.items() if block_count > 0
        )

        assert active_dp_ranks == list(range(expected_prefill_dp_ranks)), (
            f"Expected round-robin prefill requests for worker {prefill_worker_id} "
            f"to store KV blocks on dp_ranks {list(range(expected_prefill_dp_ranks))}, "
            f"but saw deltas {delta_counts}"
        )


1924
1925
1926
1927
1928
1929
def _test_router_decisions(
    engine_workers,
    endpoint,
    model_name: str,
    request,
    test_dp_rank: bool = False,
1930
    block_size: int = 8,
1931
    use_kv_events: bool = True,
1932
    durable_kv_events: bool = False,
1933
    router_event_threads: int = 4,
1934
    standalone_indexer_url: Optional[str] = None,
1935
    router_aic_config: Optional[dict[str, Any]] = None,
1936
):
1937
    """Validate cross-worker routing decisions based on longest prefix match and tree-size tiebreaking.
1938

1939
    Assumes engine workers are already initialized.
1940
1941
    Seeds two routing targets (worker a and worker b) with different prefix trees,
    then verifies the router picks the correct worker for subsequent requests.
1942

1943
1944
1945
1946
1947
1948
    Test sequence (7 blocks A-G, each block_size tokens, 5 requests):
    1. [A, B]       → force worker a        (seed worker a's tree)
    2. [A, C, D]    → force worker a        (branch under A on worker a)
    3. [A, C, E]    → force worker b        (seed worker b's tree)
    4. [A, C, D, F] → router picks          (worker a wins: prefix [A,C,D]=3 vs worker b [A,C]=2)
    5. [A, C, G]    → router picks          (tie on [A,C], worker b wins by smaller tree: 3 vs 5)
1949
1950

    Args:
1951
        engine_workers: Backend worker instance ({MockerProcess, VLLMProcess, TRTLLMProcess}) (already initialized with __enter__())
1952
1953
1954
1955
        endpoint: Endpoint of the engine workers
        model_name: Name of the model
        request: Pytest request fixture
        test_dp_rank: If True, also forces and validates dp_rank routing (for data parallel setups)
1956
        block_size: KV cache block size. Defaults to 8.
1957
1958
        use_kv_events: If True (default), uses KV events from workers. If False, uses
            approximate routing with TTL-based expiration (--no-kv-events mode).
1959
        durable_kv_events: If True, use durable KV events (JetStream). Defaults to False.
1960
        router_aic_config: Optional AIC router perf-model config for direct KvRouter tests.
1961
1962

    Raises:
1963
        AssertionError: If routing decisions don't match expected prefix/tiebreak logic
1964
1965
    """

1966
    # Create KvRouterConfig with lower snapshot threshold for testing
1967
1968
    # Use async to manage the test flow
    async def test_sync():
1969
        # If standalone indexer mode, launch workers one-by-one and register.
1970
1971
        # Must happen before KvRouter creation since KvRouter blocks until workers appear.
        if standalone_indexer_url:
1972
            await engine_workers.launch_workers_with_indexer(endpoint)
1973

1974
1975
1976
        # Workers register one instance per process (not per dp_rank)
        expected_num_instances = engine_workers.num_workers

1977
1978
1979
1980
1981
        kv_router_config = KvRouterConfig(
            router_snapshot_threshold=20,
            use_kv_events=use_kv_events,
            durable_kv_events=durable_kv_events,
            router_event_threads=router_event_threads,
1982
1983
1984
1985
1986
1987
1988
1989
1990
            router_track_prefill_tokens=True,
            router_prefill_load_model=(
                "aic" if router_aic_config is not None else "none"
            ),
        )
        aic_perf_config = (
            AicPerfConfig(**router_aic_config)
            if router_aic_config is not None
            else None
1991
        )
1992
1993
1994
1995
1996
        with min_initial_workers_env(expected_num_instances):
            kv_router = KvRouter(
                endpoint=endpoint,
                block_size=block_size,
                kv_router_config=kv_router_config,
1997
                aic_perf_config=aic_perf_config,
1998
            )
1999

2000
2001
2002
        # Wait for workers to be ready and get their instance IDs
        worker_ids = await wait_for_workers_ready(
            endpoint,
2003
            kv_router,
2004
            expected_num_workers=expected_num_instances,
2005
2006
2007
2008
            model_name=model_name,
        )
        logger.info(f"Workers ready: {worker_ids}")

2009
2010
2011
2012
2013
2014
2015
        # Determine worker a / worker b routing targets
        if len(worker_ids) >= 2:
            worker_a_id = worker_ids[0]
            worker_b_id = worker_ids[1]
        elif len(worker_ids) == 1 and test_dp_rank:
            worker_a_id = worker_ids[0]
            worker_b_id = worker_ids[0]
2016
        else:
2017
2018
2019
2020
            raise AssertionError(
                f"Need at least 2 routing targets but got {len(worker_ids)} worker(s) "
                f"with test_dp_rank={test_dp_rank}"
            )
2021

2022
2023
2024
2025
2026
2027
2028
        dp_rank_a = 0 if test_dp_rank else None
        dp_rank_b = 1 if test_dp_rank else None

        logger.info(
            f"Routing targets: worker_a=(id={worker_a_id}, dp_rank={dp_rank_a}), "
            f"worker_b=(id={worker_b_id}, dp_rank={dp_rank_b})"
        )
2029

2030
2031
        # Generate 7 random blocks (A-G)
        num_blocks = 7
2032
2033
2034
2035
        blocks = [
            [random.randint(1, 10000) for _ in range(block_size)]
            for _ in range(num_blocks)
        ]
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
        A, B, C, D, E, F, G = blocks

        # 5 requests with specific prefix structure
        request_specs = [
            # (token_ids, forced_worker_id, forced_dp_rank, sleep_after)
            (A + B, worker_a_id, dp_rank_a, 0.1),  # req1: seed worker a
            (
                A + C + D,
                worker_a_id,
                dp_rank_a,
                0.1,
            ),  # req2: branch under A on worker a
            (A + C + E, worker_b_id, dp_rank_b, 2.0),  # req3: seed worker b
            (
                A + C + D + F,
                None,
                None,
                2.0,
            ),  # req4: router picks (worker a should win)
            (A + C + G, None, None, 2.0),  # req5: router picks (worker b should win)
2056
2057
        ]

2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
        response_worker_ids: list[dict[str, Optional[int]]] = []

        for i, (token_ids, wid_override, dp_override, sleep_after) in enumerate(
            request_specs
        ):
            log_msg = f"Sending request {i + 1}/5 with {len(token_ids)} tokens"
            if wid_override is not None:
                log_msg += f" - FORCING worker_id={wid_override}"
                if dp_override is not None:
                    log_msg += f", dp_rank={dp_override}"
2068
2069
            logger.info(log_msg)

2070
            result = await send_request_via_python_kv_router(
2071
                kv_python_router=kv_router,
2072
                model_name=model_name,
2073
                token_ids=token_ids,
2074
2075
2076
                initial_wait=1.0,
                max_retries=8,
                stop_conditions={
2077
2078
                    "ignore_eos": True,
                    "max_tokens": 2,
2079
                },
2080
2081
                worker_id=wid_override,
                dp_rank=dp_override,
2082
2083
2084
2085
2086
2087
                return_worker_ids=True,
            )
            assert isinstance(result, dict), f"Expected dict result, got {type(result)}"
            response_worker_ids.append(result)
            logger.info(
                f"Request {i + 1} response: prefill_worker_id={result.get('prefill_worker_id')}, "
2088
2089
2090
                f"decode_worker_id={result.get('decode_worker_id')}, "
                f"prefill_dp_rank={result.get('prefill_dp_rank')}, "
                f"decode_dp_rank={result.get('decode_dp_rank')}"
2091
2092
            )

2093
2094
            if sleep_after > 0:
                await asyncio.sleep(sleep_after)
2095

2096
        events_json = await kv_router.dump_events()
2097
2098
2099
2100
2101
2102
2103
        return (
            events_json,
            worker_a_id,
            worker_b_id,
            dp_rank_a,
            dp_rank_b,
            response_worker_ids,
2104
            A + C + D + F,  # req4 tokens for standalone indexer /score verification
2105
        )
2106
2107

    # Run the async test
2108
2109
    (
        events_json,
2110
2111
2112
2113
        worker_a_id,
        worker_b_id,
        dp_rank_a,
        dp_rank_b,
2114
        response_worker_ids,
2115
        req4_tokens,
2116
2117
    ) = asyncio.run(test_sync())

2118
2119
2120
2121
2122
    # Verify request 4 routed to worker a (longest prefix match)
    req4 = response_worker_ids[3]
    assert req4["prefill_worker_id"] == worker_a_id, (
        f"Request 4: expected prefill_worker_id={worker_a_id} (longest prefix match), "
        f"got {req4['prefill_worker_id']}"
2123
    )
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
    if test_dp_rank:
        assert (
            req4["prefill_dp_rank"] == dp_rank_a
        ), f"Request 4: expected prefill_dp_rank={dp_rank_a}, got {req4['prefill_dp_rank']}"

    # Verify request 5 routed to worker b (tiebreak by smaller tree)
    req5 = response_worker_ids[4]
    assert req5["prefill_worker_id"] == worker_b_id, (
        f"Request 5: expected prefill_worker_id={worker_b_id} (tiebreak by smaller tree), "
        f"got {req5['prefill_worker_id']}"
2134
    )
2135
    if test_dp_rank:
2136
2137
2138
        assert (
            req5["prefill_dp_rank"] == dp_rank_b
        ), f"Request 5: expected prefill_dp_rank={dp_rank_b}, got {req5['prefill_dp_rank']}"
2139

2140
2141
2142
2143
    logger.info(
        f"Response routing verified: req4 → worker_a (id={worker_a_id}, dp_rank={dp_rank_a}), "
        f"req5 → worker_b (id={worker_b_id}, dp_rank={dp_rank_b})"
    )
2144

2145
2146
    # Parse events and verify event counts per routing target
    events = json.loads(events_json)
2147

2148
2149
2150
2151
2152
2153
2154
2155
2156
    # Always group by (worker_id, dp_rank)
    events_by_key: dict[tuple[int, int], list[Any]] = {}
    for event in events:
        worker_id = event.get("worker_id")
        dp_rank = event.get("event", {}).get("dp_rank", 0)
        key = (worker_id, dp_rank)
        if key not in events_by_key:
            events_by_key[key] = []
        events_by_key[key].append(event)
2157

2158
2159
2160
2161
2162
2163
2164
2165
2166
    def count_stored_blocks(events: list[Any]) -> int:
        total = 0
        for event in events:
            stored = event.get("event", {}).get("data", {}).get("stored")
            if stored is None:
                continue
            total += len(stored.get("blocks", []))
        return total

2167
    logger.info(
2168
2169
        "Stored blocks by (worker_id, dp_rank): "
        f"{[(key, count_stored_blocks(evts)) for key, evts in events_by_key.items()]}"
2170
    )
2171

2172
    # Worker a key: 5 stored blocks (A, B from req1; C, D from req2; F from req4)
2173
    worker_a_key = (worker_a_id, dp_rank_a if dp_rank_a is not None else 0)
2174
2175
2176
2177
    worker_a_blocks = count_stored_blocks(events_by_key.get(worker_a_key, []))
    assert worker_a_blocks == 5, (
        f"Expected worker_a {worker_a_key} to have 5 stored blocks (A,B + C,D + F), "
        f"but found {worker_a_blocks}"
2178
    )
2179

2180
    # Worker b key: 4 stored blocks (A, C, E from req3; G from req5)
2181
    worker_b_key = (worker_b_id, dp_rank_b if dp_rank_b is not None else 0)
2182
2183
2184
2185
    worker_b_blocks = count_stored_blocks(events_by_key.get(worker_b_key, []))
    assert worker_b_blocks == 4, (
        f"Expected worker_b {worker_b_key} to have 4 stored blocks (A,C,E + G), "
        f"but found {worker_b_blocks}"
2186
    )
2187

2188
2189
    logger.info(
        f"Successfully verified cross-worker routing: "
2190
2191
        f"worker_a {worker_a_key} has {worker_a_blocks} stored blocks, "
        f"worker_b {worker_b_key} has {worker_b_blocks} stored blocks"
2192
    )
2193

2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
    # Verify standalone indexer scores via HTTP POST /query
    if standalone_indexer_url:
        _dp_a = dp_rank_a if dp_rank_a is not None else 0
        _dp_b = dp_rank_b if dp_rank_b is not None else 0

        async def _verify_scores():
            # Wait for ZMQ events to propagate to the indexer
            await asyncio.sleep(3)

            async with aiohttp.ClientSession() as session:
                async with session.post(
                    f"{standalone_indexer_url}/query",
2206
                    json={"token_ids": req4_tokens, "model_name": model_name},
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
                ) as resp:
                    assert resp.status == 200, f"POST /query failed: {resp.status}"
                    scores = (await resp.json())["scores"]

                    id_a = str(worker_a_id)
                    id_b = str(worker_b_id)
                    dp_a = str(_dp_a)
                    dp_b = str(_dp_b)
                    score_a = scores[id_a][dp_a]
                    score_b = scores[id_b][dp_b]

                    logger.info(
                        f"Standalone indexer /query: {id_a}[{dp_a}]={score_a}, "
                        f"{id_b}[{dp_b}]={score_b}"
                    )
                    assert score_a > score_b, (
                        f"Expected instance {id_a} dp_rank {dp_a} score {score_a} > "
                        f"instance {id_b} dp_rank {dp_b} score {score_b} for req4 tokens"
                    )

        asyncio.run(_verify_scores())

2229
2230
2231
2232
2233
2234
2235
2236

def _test_busy_threshold_endpoint(
    engine_workers,
    block_size: int,
    request,
    frontend_port: int,
    test_payload: dict,
    store_backend: str = "etcd",
2237
    request_plane: str = "nats",
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
):
    """Test that the /busy_threshold endpoint can be hit and responds correctly.

    TODO: This doesn't actually test any e2e rejection for now. A proper test would:
    1. Set a very low threshold
    2. Send enough requests to exceed the threshold
    3. Verify that subsequent requests are rejected with 503

    For now, this test only verifies the endpoint is accessible and returns valid responses.

    Args:
2249
        engine_workers: MockerProcess instance (already initialized with __enter__())
2250
2251
2252
2253
2254
        block_size: Block size for KV cache
        request: Pytest request fixture for managing resources
        frontend_port: Port for the frontend HTTP server
        test_payload: Base test payload (used to extract model name)
        store_backend: Storage backend to use ("etcd" or "file"). Defaults to "etcd".
2255
        request_plane: Request plane to use ("nats" or "tcp"). Defaults to "nats".
2256
2257
2258
2259

    Raises:
        AssertionError: If endpoint responses are incorrect
    """
2260
2261
2262
    # Initial thresholds - we need to start with these so the monitor is created
    initial_active_decode_blocks_threshold = 0.9
    initial_active_prefill_tokens_threshold = 1000  # Literal token count threshold
2263

2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
    with KVRouterProcess(
        request,
        block_size,
        frontend_port,
        engine_workers.namespace,
        store_backend,
        blocks_threshold=initial_active_decode_blocks_threshold,
        tokens_threshold=initial_active_prefill_tokens_threshold,
        request_plane=request_plane,
    ):
2274
        # Start KV router frontend with initial thresholds to create monitor
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
        logger.info(f"Starting KV router frontend on port {frontend_port}")

        frontend_url = f"http://localhost:{frontend_port}"
        busy_threshold_url = f"{frontend_url}/busy_threshold"

        # Wait for workers to register with frontend
        logger.info("Waiting for workers to register with frontend...")
        asyncio.run(
            wait_for_frontend_ready(
                frontend_url=frontend_url,
                expected_num_workers=engine_workers.num_workers,
                timeout=120,
            )
        )

        model_name = test_payload.get("model", "test-model")

        async def test_busy_threshold_api():
            async with aiohttp.ClientSession() as session:
                # Test 1: GET /busy_threshold - list all thresholds
                logger.info("Testing GET /busy_threshold (list all)")
                async with session.get(busy_threshold_url) as response:
                    assert (
                        response.status == 200
                    ), f"GET /busy_threshold failed with status {response.status}"
                    data = await response.json()
                    assert (
                        "thresholds" in data
                    ), f"Expected 'thresholds' key in response: {data}"
                    logger.info(f"GET /busy_threshold response: {data}")

2306
                # Test 2: POST /busy_threshold with model only (get thresholds)
2307
                logger.info(
2308
                    f"Testing POST /busy_threshold to get thresholds for model '{model_name}'"
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
                )
                async with session.post(
                    busy_threshold_url,
                    json={"model": model_name},
                ) as response:
                    assert (
                        response.status == 200
                    ), f"POST /busy_threshold (get) failed with status {response.status}"
                    data = await response.json()
                    assert (
2319
2320
2321
2322
2323
2324
2325
                        data.get("active_decode_blocks_threshold")
                        == initial_active_decode_blocks_threshold
                    ), f"Expected initial active_decode_blocks_threshold={initial_active_decode_blocks_threshold}: {data}"
                    assert (
                        data.get("active_prefill_tokens_threshold")
                        == initial_active_prefill_tokens_threshold
                    ), f"Expected initial active_prefill_tokens_threshold={initial_active_prefill_tokens_threshold}: {data}"
2326
2327
2328
2329
                    logger.info(
                        f"POST /busy_threshold (get) response: status={response.status}, data={data}"
                    )

2330
2331
                # Test 3: POST /busy_threshold to set active_decode_blocks_threshold only
                test_active_decode_blocks_threshold = 0.75
2332
                logger.info(
2333
                    f"Testing POST /busy_threshold to set active_decode_blocks_threshold={test_active_decode_blocks_threshold}"
2334
2335
2336
                )
                async with session.post(
                    busy_threshold_url,
2337
2338
2339
2340
                    json={
                        "model": model_name,
                        "active_decode_blocks_threshold": test_active_decode_blocks_threshold,
                    },
2341
2342
2343
                ) as response:
                    assert (
                        response.status == 200
2344
                    ), f"POST /busy_threshold (set blocks) failed with status {response.status}"
2345
2346
2347
2348
2349
                    data = await response.json()
                    assert (
                        data.get("model") == model_name
                    ), f"Expected model={model_name}: {data}"
                    assert (
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
                        data.get("active_decode_blocks_threshold")
                        == test_active_decode_blocks_threshold
                    ), f"Expected active_decode_blocks_threshold={test_active_decode_blocks_threshold}: {data}"
                    logger.info(f"POST /busy_threshold (set blocks) response: {data}")

                # Test 4: POST /busy_threshold to set active_prefill_tokens_threshold only
                test_active_prefill_tokens_threshold = (
                    2000  # Literal token count threshold
                )
                logger.info(
                    f"Testing POST /busy_threshold to set active_prefill_tokens_threshold={test_active_prefill_tokens_threshold}"
                )
2362
2363
                async with session.post(
                    busy_threshold_url,
2364
2365
2366
2367
                    json={
                        "model": model_name,
                        "active_prefill_tokens_threshold": test_active_prefill_tokens_threshold,
                    },
2368
2369
2370
                ) as response:
                    assert (
                        response.status == 200
2371
                    ), f"POST /busy_threshold (set tokens) failed with status {response.status}"
2372
2373
                    data = await response.json()
                    assert (
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
                        data.get("active_prefill_tokens_threshold")
                        == test_active_prefill_tokens_threshold
                    ), f"Expected active_prefill_tokens_threshold={test_active_prefill_tokens_threshold}: {data}"
                    logger.info(f"POST /busy_threshold (set tokens) response: {data}")

                # Test 5: POST /busy_threshold to set both thresholds
                new_active_decode_blocks_threshold = 0.5
                new_active_prefill_tokens_threshold = (
                    1200  # Literal token count threshold
                )
2384
                logger.info(
2385
2386
                    f"Testing POST /busy_threshold to set both thresholds: "
                    f"active_decode_blocks={new_active_decode_blocks_threshold}, active_prefill_tokens={new_active_prefill_tokens_threshold}"
2387
2388
2389
                )
                async with session.post(
                    busy_threshold_url,
2390
2391
2392
2393
2394
                    json={
                        "model": model_name,
                        "active_decode_blocks_threshold": new_active_decode_blocks_threshold,
                        "active_prefill_tokens_threshold": new_active_prefill_tokens_threshold,
                    },
2395
2396
2397
                ) as response:
                    assert (
                        response.status == 200
2398
                    ), f"POST /busy_threshold (set both) failed with status {response.status}"
2399
2400
                    data = await response.json()
                    assert (
2401
2402
2403
2404
2405
2406
2407
2408
                        data.get("active_decode_blocks_threshold")
                        == new_active_decode_blocks_threshold
                    ), f"Expected active_decode_blocks_threshold={new_active_decode_blocks_threshold}: {data}"
                    assert (
                        data.get("active_prefill_tokens_threshold")
                        == new_active_prefill_tokens_threshold
                    ), f"Expected active_prefill_tokens_threshold={new_active_prefill_tokens_threshold}: {data}"
                    logger.info(f"POST /busy_threshold (set both) response: {data}")
2409

2410
2411
                # Test 6: GET /busy_threshold - verify thresholds appear in list
                logger.info("Testing GET /busy_threshold to verify thresholds in list")
2412
2413
2414
2415
2416
2417
                async with session.get(busy_threshold_url) as response:
                    assert (
                        response.status == 200
                    ), f"GET /busy_threshold failed with status {response.status}"
                    data = await response.json()
                    thresholds = data.get("thresholds", [])
2418
2419
2420
                    model_entry = next(
                        (t for t in thresholds if t["model"] == model_name), None
                    )
2421
                    assert (
2422
                        model_entry is not None
2423
2424
                    ), f"Expected model '{model_name}' in thresholds: {data}"
                    assert (
2425
2426
2427
2428
2429
2430
2431
                        model_entry.get("active_decode_blocks_threshold")
                        == new_active_decode_blocks_threshold
                    ), f"Expected active_decode_blocks_threshold={new_active_decode_blocks_threshold}: {data}"
                    assert (
                        model_entry.get("active_prefill_tokens_threshold")
                        == new_active_prefill_tokens_threshold
                    ), f"Expected active_prefill_tokens_threshold={new_active_prefill_tokens_threshold}: {data}"
2432
2433
                    logger.info(f"GET /busy_threshold (after set) response: {data}")

2434
                # Test 7: Invalid active_decode_blocks_threshold value (should fail validation)
2435
                logger.info(
2436
                    "Testing POST /busy_threshold with invalid active_decode_blocks_threshold (>1.0)"
2437
2438
2439
                )
                async with session.post(
                    busy_threshold_url,
2440
                    json={"model": model_name, "active_decode_blocks_threshold": 1.5},
2441
2442
2443
                ) as response:
                    assert (
                        response.status == 400
2444
                    ), f"Expected 400 for invalid active_decode_blocks_threshold, got {response.status}"
2445
                    data = await response.json()
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
                    logger.info(
                        f"POST /busy_threshold (invalid blocks) response: {data}"
                    )

                # Test 8: active_prefill_tokens_threshold accepts large values (should be valid)
                logger.info(
                    "Testing POST /busy_threshold with large active_prefill_tokens_threshold (valid)"
                )
                async with session.post(
                    busy_threshold_url,
                    json={"model": model_name, "active_prefill_tokens_threshold": 5000},
                ) as response:
                    assert (
                        response.status == 200
                    ), f"Expected 200 for large active_prefill_tokens_threshold, got {response.status}"
                    data = await response.json()
                    assert (
                        data.get("active_prefill_tokens_threshold") == 5000
                    ), f"Expected active_prefill_tokens_threshold=5000: {data}"
                    logger.info(
                        f"POST /busy_threshold (large tokens threshold) response: {data}"
                    )

                # Test 9: Invalid active_prefill_tokens_threshold value (should fail validation for < 0)
                # Note: Returns 422 because -1.0 can't be deserialized into u64 (type validation)
                # vs Test 7 which returns 400 because 1.5 is a valid f64 but fails range validation
                logger.info(
                    "Testing POST /busy_threshold with invalid active_prefill_tokens_threshold (< 0)"
                )
                async with session.post(
                    busy_threshold_url,
                    json={"model": model_name, "active_prefill_tokens_threshold": -1.0},
                ) as response:
                    assert (
                        response.status == 422
                    ), f"Expected 422 for negative active_prefill_tokens_threshold, got {response.status}"
                    data = await response.json()
                    logger.info(
                        f"POST /busy_threshold (invalid tokens) response: {data}"
                    )
2486

2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
                # Test 10: Set active_prefill_tokens_threshold_frac (fraction of max_num_batched_tokens)
                test_frac_threshold = 0.8
                logger.info(
                    f"Testing POST /busy_threshold to set active_prefill_tokens_threshold_frac={test_frac_threshold}"
                )
                async with session.post(
                    busy_threshold_url,
                    json={
                        "model": model_name,
                        "active_prefill_tokens_threshold_frac": test_frac_threshold,
                    },
                ) as response:
                    assert (
                        response.status == 200
                    ), f"POST /busy_threshold (set frac) failed with status {response.status}"
                    data = await response.json()
                    assert (
                        data.get("active_prefill_tokens_threshold_frac")
                        == test_frac_threshold
                    ), f"Expected active_prefill_tokens_threshold_frac={test_frac_threshold}: {data}"
                    logger.info(f"POST /busy_threshold (set frac) response: {data}")

                # Test 11: Verify frac threshold appears in GET /busy_threshold list
                logger.info(
                    "Testing GET /busy_threshold to verify frac threshold in list"
                )
                async with session.get(busy_threshold_url) as response:
                    assert (
                        response.status == 200
                    ), f"GET /busy_threshold failed with status {response.status}"
                    data = await response.json()
                    thresholds = data.get("thresholds", [])
                    model_entry = next(
                        (t for t in thresholds if t["model"] == model_name), None
                    )
                    assert (
                        model_entry is not None
                    ), f"Expected model '{model_name}' in thresholds: {data}"
                    assert (
                        model_entry.get("active_prefill_tokens_threshold_frac")
                        == test_frac_threshold
                    ), f"Expected active_prefill_tokens_threshold_frac={test_frac_threshold}: {data}"
                    logger.info(
                        f"GET /busy_threshold (after set frac) response: {data}"
                    )

2533
2534
2535
                logger.info("All busy_threshold endpoint tests passed!")

        asyncio.run(test_busy_threshold_api())
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638


def _test_disagg_direct_mode(
    prefill_workers,
    decode_workers,
    request,
    frontend_port: int,
    test_payload: dict,
    request_plane: str = "nats",
):
    """E2E test for disaggregated Direct routing mode (simulating GAIE EPP).

    In Direct mode, the router does not select workers itself.
    Worker IDs must be provided via x-worker-instance-id and x-prefill-instance-id
    HTTP headers. The test verifies:
      1. Requests with explicit worker ID headers succeed and return a valid response.
      2. Requests without headers fail (Direct mode rejects unaddressed requests).

    Args:
        prefill_workers: Prefill mocker workers (already started).
        decode_workers: Decode mocker workers (already started).
        request: Pytest request fixture.
        frontend_port: Port for the Direct-mode frontend HTTP server.
        test_payload: Base test payload for /v1/chat/completions.
        request_plane: Transport for request plane ("nats" or "tcp").
    """
    with DirectRouterProcess(
        request,
        frontend_port,
        decode_workers.namespace,
        enforce_disagg=True,
        request_plane=request_plane,
    ):
        frontend_url = f"http://localhost:{frontend_port}"
        chat_url = f"{frontend_url}/v1/chat/completions"

        logger.info("Waiting for models to appear in Direct-mode frontend...")

        async def wait_for_models():
            models_url = f"{frontend_url}/v1/models"
            for _ in range(120):
                try:
                    async with aiohttp.ClientSession() as session:
                        async with session.get(models_url) as response:
                            if response.status == 200:
                                data = await response.json()
                                models = data.get("data", [])
                                if models:
                                    logger.info(
                                        f"Models registered: {[m.get('id') for m in models]}"
                                    )
                                    return
                except Exception as e:
                    logger.debug(f"Error checking models endpoint: {e}")
                await asyncio.sleep(1)
            raise TimeoutError("Timeout waiting for models in Direct-mode frontend")

        asyncio.run(wait_for_models())

        # Phase 2: Discover worker IDs via the runtime
        runtime = get_runtime(request_plane=request_plane)
        prefill_endpoint = runtime.endpoint(
            f"{decode_workers.namespace}.prefill.generate"
        )
        decode_endpoint = runtime.endpoint(
            f"{decode_workers.namespace}.backend.generate"
        )

        async def discover_workers():
            prefill_client = await prefill_endpoint.client()
            decode_client = await decode_endpoint.client()

            for _ in range(60):
                p_ids = prefill_client.instance_ids()
                d_ids = decode_client.instance_ids()
                if p_ids and d_ids:
                    return p_ids, d_ids
                await asyncio.sleep(0.5)
            raise TimeoutError(
                f"Timeout discovering workers: prefill={p_ids}, decode={d_ids}"
            )

        prefill_ids, decode_ids = asyncio.run(discover_workers())
        logger.info(f"Discovered prefill workers: {prefill_ids}")
        logger.info(f"Discovered decode workers: {decode_ids}")

        target_prefill = prefill_ids[0]
        target_decode = decode_ids[0]

        async def run_direct_mode_tests():
            # Test 1: Request WITH correct headers should succeed.
            # In direct mode the router is a passthrough — it does not have a
            # KvRouter and does not record worker IDs on the RequestTracker, so
            # the response's nvext will not contain worker_id info.  We only
            # verify that the request is routed successfully (HTTP 200) and
            # produces a valid chat completion response.
            payload = {
                **test_payload,
                "stream": False,
            }
            headers = {
                "x-worker-instance-id": str(target_decode),
                "x-prefill-instance-id": str(target_prefill),
atchernych's avatar
atchernych committed
2639
2640
                "x-dp-rank": "0",
                "x-prefill-dp-rank": "0",
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
            }

            async with aiohttp.ClientSession() as session:
                # Retry a few times to allow the pipeline to warm up
                for attempt in range(10):
                    async with session.post(
                        chat_url, json=payload, headers=headers
                    ) as response:
                        if response.status == 200:
                            data = await response.json()
                            logger.info(
                                f"Direct-mode response (attempt {attempt + 1}): "
                                f"status=200, model={data.get('model')}"
                            )
                            assert (
                                "choices" in data
                            ), "Expected 'choices' in response data"
                            assert (
                                len(data["choices"]) > 0
                            ), "Expected at least one choice in response"
                            break
                        else:
                            logger.info(
                                f"Direct-mode attempt {attempt + 1} returned "
                                f"status {response.status}, retrying..."
                            )
                            await asyncio.sleep(2)
                else:
                    raise AssertionError(
                        "Direct-mode request with headers never returned 200"
                    )

                # Test 2: Request WITHOUT headers should fail (Direct mode
                # rejects requests that have no worker ID)
                logger.info(
                    "Sending request without headers (should fail in Direct mode)..."
                )
                no_header_payload = {**test_payload, "stream": False}
                async with session.post(chat_url, json=no_header_payload) as response:
                    assert response.status != 200, (
                        f"Expected non-200 status without routing headers in Direct mode, "
                        f"got {response.status}. Direct mode must reject unaddressed requests."
                    )
                    logger.info(
                        f"Correctly rejected headerless request: status={response.status}"
                    )

        asyncio.run(run_direct_mode_tests())
        logger.info("Direct-mode disagg E2E test passed")