router_fixtures.py 17 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
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
551
552
553
554
"""
Fixtures for launching OpenAI/XAI router for response API e2e testing.

This module provides fixtures for launching SGLang router with OpenAI or XAI backends:
    1. Launch router with --backend openai pointing to OpenAI or XAI API
    2. Configure history backend (memory or oracle)

This supports testing the Response API against real cloud providers.
"""

import os
import socket
import subprocess
import time
from typing import Optional

import requests


def wait_for_workers_ready(
    router_url: str,
    expected_workers: int,
    timeout: int = 300,
    api_key: Optional[str] = None,
) -> None:
    """
    Wait for router to have all workers connected.

    Polls the /workers endpoint until the 'total' field matches expected_workers.

    Example response from /workers endpoint:
    {"workers":[],"total":0,"stats":{"prefill_count":0,"decode_count":0,"regular_count":0}}

    Args:
        router_url: Base URL of router (e.g., "http://127.0.0.1:30000")
        expected_workers: Number of workers expected to be connected
        timeout: Max seconds to wait
        api_key: Optional API key for authentication
    """
    start_time = time.time()
    last_error = None
    attempt = 0

    headers = {}
    if api_key:
        headers["Authorization"] = f"Bearer {api_key}"

    with requests.Session() as session:
        while time.time() - start_time < timeout:
            attempt += 1
            elapsed = int(time.time() - start_time)

            # Print progress every 10 seconds
            if elapsed > 0 and elapsed % 10 == 0 and attempt % 10 == 0:
                print(f"  Still waiting for workers... ({elapsed}/{timeout}s elapsed)")

            try:
                response = session.get(
                    f"{router_url}/workers", headers=headers, timeout=5
                )
                if response.status_code == 200:
                    data = response.json()
                    total_workers = data.get("total", 0)

                    if total_workers == expected_workers:
                        print(
                            f"  All {expected_workers} workers connected after {elapsed}s"
                        )
                        return
                    else:
                        last_error = f"Workers: {total_workers}/{expected_workers}"
                else:
                    last_error = f"HTTP {response.status_code}"
            except requests.ConnectionError:
                last_error = "Connection refused (router not ready yet)"
            except requests.Timeout:
                last_error = "Timeout"
            except requests.RequestException as e:
                last_error = str(e)
            except (ValueError, KeyError) as e:
                last_error = f"Invalid response: {e}"

            time.sleep(1)

    raise TimeoutError(
        f"Router at {router_url} did not get {expected_workers} workers within {timeout}s.\n"
        f"Last status: {last_error}\n"
        f"Hint: Run with SHOW_ROUTER_LOGS=1 to see startup logs"
    )


def find_free_port() -> int:
    """Find an available port on localhost."""
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.bind(("127.0.0.1", 0))
        return s.getsockname()[1]


def wait_for_router_ready(
    router_url: str,
    timeout: int = 60,
    api_key: Optional[str] = None,
) -> None:
    """
    Wait for router to be ready.

    Polls the /health endpoint until it returns 200.

    Args:
        router_url: Base URL of router (e.g., "http://127.0.0.1:30000")
        timeout: Max seconds to wait
        api_key: Optional API key for authentication
    """
    start_time = time.time()
    last_error = None
    attempt = 0

    headers = {}
    if api_key:
        headers["Authorization"] = f"Bearer {api_key}"

    with requests.Session() as session:
        while time.time() - start_time < timeout:
            attempt += 1
            elapsed = int(time.time() - start_time)

            # Print progress every 10 seconds
            if elapsed > 0 and elapsed % 10 == 0 and attempt % 10 == 0:
                print(f"  Still waiting for router... ({elapsed}/{timeout}s elapsed)")

            try:
                response = session.get(
                    f"{router_url}/health", headers=headers, timeout=5
                )
                if response.status_code == 200:
                    print(f"  Router ready after {elapsed}s")
                    return
                else:
                    last_error = f"HTTP {response.status_code}"
            except requests.ConnectionError:
                last_error = "Connection refused (router not ready yet)"
            except requests.Timeout:
                last_error = "Timeout"
            except requests.RequestException as e:
                last_error = str(e)

            time.sleep(1)

    raise TimeoutError(
        f"Router at {router_url} did not become ready within {timeout}s.\n"
        f"Last status: {last_error}\n"
        f"Hint: Run with SHOW_ROUTER_LOGS=1 to see startup logs"
    )


def popen_launch_openai_xai_router(
    backend: str,  # "openai" or "xai"
    base_url: str,
    timeout: int = 60,
    history_backend: str = "memory",
    api_key: Optional[str] = None,
    router_args: Optional[list] = None,
    stdout=None,
    stderr=None,
    prometheus_port: Optional[int] = None,
) -> dict:
    """
    Launch SGLang router with OpenAI or XAI backend.

    This approach:
    1. Starts router with --backend openai
    2. Points to OpenAI or XAI API via --worker-urls
    3. Configures history backend (memory or oracle)
    4. Waits for router health check to pass

    Args:
        backend: "openai" or "xai"
        base_url: Base URL for router (e.g., "http://127.0.0.1:30000")
        timeout: Timeout for router startup (default: 60s)
        history_backend: "memory" or "oracle" (default: memory)
        api_key: Optional API key for router authentication
        router_args: Additional arguments for router
        stdout: Optional file handle for router stdout
        stderr: Optional file handle for router stderr

    Returns:
        dict with:
            - router: router process object
            - base_url: router URL (HTTP endpoint)

    Example:
        >>> cluster = popen_launch_openai_xai_router(
        ...     "openai", "http://127.0.0.1:30000"
        ... )
        >>> # Use cluster['base_url'] for HTTP requests
        >>> # Cleanup:
        >>> kill_process_tree(cluster['router'].pid)
    """
    show_output = os.environ.get("SHOW_ROUTER_LOGS", "0") == "1"

    # Parse router port from base_url
    if ":" in base_url.split("//")[-1]:
        router_port = int(base_url.split(":")[-1])
    else:
        router_port = find_free_port()

    print(f"\n{'='*70}")
    print(f"Launching {backend.upper()} router")
    print(f"{'='*70}")
    print(f"  Backend: {backend}")
    print(f"  Router port: {router_port}")
    print(f"  History backend: {history_backend}")

    # Determine worker URL based on backend
    if backend == "openai":
        worker_url = "https://api.openai.com"
        # Get API key from environment
        backend_api_key = os.environ.get("OPENAI_API_KEY")
        if not backend_api_key:
            raise ValueError(
                "OPENAI_API_KEY environment variable must be set for OpenAI backend"
            )
    elif backend == "xai":
        worker_url = "https://api.x.ai"
        # Get API key from environment
        backend_api_key = os.environ.get("XAI_API_KEY")
        if not backend_api_key:
            raise ValueError(
                "XAI_API_KEY environment variable must be set for XAI backend"
            )
    else:
        raise ValueError(f"Unsupported backend: {backend}")

    print(f"  Worker URL: {worker_url}")

    # Build router command
    router_cmd = [
        "python3",
        "-m",
        "sglang_router.launch_router",
        "--host",
        "127.0.0.1",
        "--port",
        str(router_port),
        "--backend",
        "openai",
        "--worker-urls",
        worker_url,
        "--history-backend",
        history_backend,
        "--log-level",
        "warn",
    ]

    # Note: Not adding --api-key to router command for local testing
    # The router will not require authentication

    # Add Prometheus port to avoid conflicts (use unique port or disable)
    if prometheus_port is None:
        # Auto-assign a unique prometheus port based on router port
        prometheus_port = router_port + 1000
    router_cmd.extend(["--prometheus-port", str(prometheus_port)])

    # Add router-specific args
    if router_args:
        router_cmd.extend(router_args)

    if show_output:
        print(f"  Command: {' '.join(router_cmd)}")

    # Set up environment with backend API key
    env = os.environ.copy()
    if backend == "openai":
        env["OPENAI_API_KEY"] = backend_api_key
    else:
        env["XAI_API_KEY"] = backend_api_key

    # Launch router
    if show_output:
        router_proc = subprocess.Popen(
            router_cmd,
            env=env,
            stdout=stdout,
            stderr=stderr,
        )
    else:
        router_proc = subprocess.Popen(
            router_cmd,
            stdout=stdout if stdout is not None else subprocess.PIPE,
            stderr=stderr if stderr is not None else subprocess.PIPE,
            env=env,
        )

    print(f"  PID: {router_proc.pid}")

    # Wait for router to be ready
    router_url = f"http://127.0.0.1:{router_port}"
    print(f"\nWaiting for router to start at {router_url}...")

    try:
        wait_for_router_ready(router_url, timeout=timeout, api_key=None)
        print(f"✓ Router ready at {router_url}")
    except TimeoutError:
        print(f"✗ Router failed to start")
        # Cleanup: kill router
        try:
            router_proc.kill()
        except:
            pass
        raise

    print(f"\n{'='*70}")
    print(f"✓ {backend.upper()} router ready!")
    print(f"  Router: {router_url}")
    print(f"{'='*70}\n")

    return {
        "router": router_proc,
        "base_url": router_url,
    }


def popen_launch_workers_and_router(
    model: str,
    base_url: str,
    timeout: int = 300,
    num_workers: int = 2,
    policy: str = "round_robin",
    api_key: Optional[str] = None,
    worker_args: Optional[list] = None,
    router_args: Optional[list] = None,
    tp_size: int = 1,
    env: Optional[dict] = None,
    stdout=None,
    stderr=None,
) -> dict:
    """
    Launch SGLang workers and gRPC router separately.

    This approach:
    1. Starts N SGLang workers with --grpc-mode flag
    2. Waits for workers to initialize (process startup)
    3. Starts a gRPC router pointing to those workers
    4. Waits for router health check to pass (router validates worker connectivity)

    This matches production deployment patterns better than the integrated approach.

    Args:
        model: Model path (e.g., /home/ubuntu/models/llama-3.1-8b-instruct)
        base_url: Base URL for router (e.g., "http://127.0.0.1:8080")
        timeout: Timeout for server startup (default: 300s)
        num_workers: Number of workers to launch
        policy: Routing policy (round_robin, random, power_of_two, cache_aware)
        api_key: Optional API key for router
        worker_args: Additional arguments for workers (e.g., ["--context-len", "8192"])
        router_args: Additional arguments for router (e.g., ["--max-total-token", "1536"])
        tp_size: Tensor parallelism size for workers (default: 1)
        env: Optional environment variables for workers (e.g., {"SGLANG_CLIP_MAX_NEW_TOKENS_ESTIMATION": "256"})
        stdout: Optional file handle for worker stdout (default: subprocess.PIPE)
        stderr: Optional file handle for worker stderr (default: subprocess.PIPE)

    Returns:
        dict with:
            - workers: list of worker process objects
            - worker_urls: list of gRPC worker URLs
            - router: router process object
            - base_url: router URL (HTTP endpoint)

    Example:
        >>> cluster = popen_launch_workers_and_router(model, base_url, num_workers=2)
        >>> # Use cluster['base_url'] for HTTP requests
        >>> # Cleanup:
        >>> for worker in cluster['workers']:
        >>>     kill_process_tree(worker.pid)
        >>> kill_process_tree(cluster['router'].pid)
    """
    show_output = os.environ.get("SHOW_ROUTER_LOGS", "0") == "1"

    # Parse router port from base_url
    if ":" in base_url.split("//")[-1]:
        router_port = int(base_url.split(":")[-1])
    else:
        router_port = find_free_port()

    print(f"\n{'='*70}")
    print(f"Launching gRPC cluster (separate workers + router)")
    print(f"{'='*70}")
    print(f"  Model: {model}")
    print(f"  Router port: {router_port}")
    print(f"  Workers: {num_workers}")
    print(f"  TP size: {tp_size}")
    print(f"  Policy: {policy}")

    # Step 1: Launch workers with gRPC enabled
    workers = []
    worker_urls = []

    for i in range(num_workers):
        worker_port = find_free_port()
        worker_url = f"grpc://127.0.0.1:{worker_port}"
        worker_urls.append(worker_url)

        print(f"\n[Worker {i+1}/{num_workers}]")
        print(f"  Port: {worker_port}")
        print(f"  URL: {worker_url}")

        # Build worker command
        worker_cmd = [
            "python3",
            "-m",
            "sglang.launch_server",
            "--model-path",
            model,
            "--host",
            "127.0.0.1",
            "--port",
            str(worker_port),
            "--grpc-mode",  # Enable gRPC for this worker
            "--mem-fraction-static",
            "0.8",
            "--attention-backend",
            "fa3",
        ]

        # Add TP size
        if tp_size > 1:
            worker_cmd.extend(["--tp-size", str(tp_size)])

        # Add worker-specific args
        if worker_args:
            worker_cmd.extend(worker_args)

        # Launch worker with optional environment variables
        if show_output:
            worker_proc = subprocess.Popen(
                worker_cmd,
                env=env,
                stdout=stdout,
                stderr=stderr,
            )
        else:
            worker_proc = subprocess.Popen(
                worker_cmd,
                stdout=stdout if stdout is not None else subprocess.PIPE,
                stderr=stderr if stderr is not None else subprocess.PIPE,
                env=env,
            )

        workers.append(worker_proc)
        print(f"  PID: {worker_proc.pid}")

    # Give workers a moment to start binding to ports
    # The router will check worker health when it starts
    print(f"\nWaiting for {num_workers} workers to initialize (20s)...")
    time.sleep(20)

    # Quick check: make sure worker processes are still alive
    for i, worker in enumerate(workers):
        if worker.poll() is not None:
            print(f"  ✗ Worker {i+1} died during startup (exit code: {worker.poll()})")
            # Cleanup: kill all workers
            for w in workers:
                try:
                    w.kill()
                except:
                    pass
            raise RuntimeError(f"Worker {i+1} failed to start")

    print(f"✓ All {num_workers} workers started (router will verify connectivity)")

    # Step 2: Launch router pointing to workers
    print(f"\n[Router]")
    print(f"  Port: {router_port}")
    print(f"  Worker URLs: {', '.join(worker_urls)}")

    # Build router command
    router_cmd = [
        "python3",
        "-m",
        "sglang_router.launch_router",
        "--host",
        "127.0.0.1",
        "--port",
        str(router_port),
        "--prometheus-port",
        "9321",
        "--policy",
        policy,
        "--model-path",
        model,
    ]

    # Add worker URLs
    router_cmd.append("--worker-urls")
    router_cmd.extend(worker_urls)

    # Add API key
    if api_key:
        router_cmd.extend(["--api-key", api_key])

    # Add router-specific args
    if router_args:
        router_cmd.extend(router_args)

    if show_output:
        print(f"  Command: {' '.join(router_cmd)}")

    # Launch router
    if show_output:
        router_proc = subprocess.Popen(router_cmd)
    else:
        router_proc = subprocess.Popen(
            router_cmd,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
        )

    print(f"  PID: {router_proc.pid}")

    # Wait for router to be ready
    router_url = f"http://127.0.0.1:{router_port}"
    print(f"\nWaiting for router to start at {router_url}...")

    try:
        wait_for_workers_ready(
            router_url, expected_workers=num_workers, timeout=180, api_key=api_key
        )
        print(f"✓ Router ready at {router_url}")
    except TimeoutError:
        print(f"✗ Router failed to start")
        # Cleanup: kill router and all workers
        try:
            router_proc.kill()
        except:
            pass
        for worker in workers:
            try:
                worker.kill()
            except:
                pass
        raise

    print(f"\n{'='*70}")
    print(f"✓ gRPC cluster ready!")
    print(f"  Router: {router_url}")
    print(f"  Workers: {len(workers)}")
    print(f"{'='*70}\n")

    return {
        "workers": workers,
        "worker_urls": worker_urls,
        "router": router_proc,
        "base_url": router_url,
    }