mock_worker.rs 22.5 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
use actix_web::{middleware, web, App, HttpRequest, HttpResponse, HttpServer};
use futures_util::StreamExt;
use serde_json::json;
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};
use tokio::sync::RwLock;
use uuid;

/// Configuration for mock worker behavior
#[derive(Clone)]
pub struct MockWorkerConfig {
    pub port: u16,
    pub worker_type: WorkerType,
    pub health_status: HealthStatus,
    pub response_delay_ms: u64,
    pub fail_rate: f32,
}

#[derive(Clone, Debug)]
pub enum WorkerType {
    Regular,
    Prefill,
    Decode,
}

#[derive(Clone, Debug)]
pub enum HealthStatus {
    Healthy,
    Unhealthy,
    Degraded,
}

/// Mock worker server for testing
pub struct MockWorker {
    config: Arc<RwLock<MockWorkerConfig>>,
    server_handle: Option<actix_web::dev::ServerHandle>,
}

impl MockWorker {
    pub fn new(config: MockWorkerConfig) -> Self {
        Self {
            config: Arc::new(RwLock::new(config)),
            server_handle: None,
        }
    }

    /// Start the mock worker server
    pub async fn start(&mut self) -> Result<String, Box<dyn std::error::Error>> {
        let config = self.config.clone();
        let port = config.read().await.port;

        let server = HttpServer::new(move || {
            App::new()
                .app_data(web::Data::new(config.clone()))
                .wrap(middleware::Logger::default())
                .route("/health", web::get().to(health_handler))
                .route("/health_generate", web::get().to(health_generate_handler))
                .route("/get_server_info", web::get().to(server_info_handler))
                .route("/get_model_info", web::get().to(model_info_handler))
                .route("/generate", web::post().to(generate_handler))
                .route(
                    "/v1/chat/completions",
                    web::post().to(chat_completions_handler),
                )
                .route("/v1/completions", web::post().to(completions_handler))
                .route("/flush_cache", web::post().to(flush_cache_handler))
                .route("/v1/models", web::get().to(v1_models_handler))
        })
        .bind(("127.0.0.1", port))?
        .run();

        let handle = server.handle();
        self.server_handle = Some(handle);

        tokio::spawn(server);

        Ok(format!("http://127.0.0.1:{}", port))
    }

    /// Stop the mock worker server
    pub async fn stop(&mut self) {
        if let Some(handle) = self.server_handle.take() {
            // First try graceful stop with short timeout
            handle.stop(false);
            // Give it a moment to stop gracefully
            tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
        }
    }

    /// Update the mock worker configuration
    pub async fn update_config<F>(&self, updater: F)
    where
        F: FnOnce(&mut MockWorkerConfig),
    {
        let mut config = self.config.write().await;
        updater(&mut *config);
    }
}

// Handler implementations

102
103
104
105
106
/// Check if request should fail based on configured fail_rate
async fn should_fail(config: &MockWorkerConfig) -> bool {
    rand::random::<f32>() < config.fail_rate
}

107
108
109
async fn health_handler(config: web::Data<Arc<RwLock<MockWorkerConfig>>>) -> HttpResponse {
    let config = config.read().await;

110
111
112
    // Note: We don't apply fail_rate to health endpoint to allow workers to be added successfully
    // fail_rate is only applied to actual request endpoints

113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
    match config.health_status {
        HealthStatus::Healthy => HttpResponse::Ok().json(json!({
            "status": "healthy",
            "timestamp": SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs(),
            "worker_type": format!("{:?}", config.worker_type),
        })),
        HealthStatus::Unhealthy => HttpResponse::ServiceUnavailable().json(json!({
            "status": "unhealthy",
            "error": "Worker is not responding"
        })),
        HealthStatus::Degraded => HttpResponse::Ok().json(json!({
            "status": "degraded",
            "warning": "High load detected"
        })),
    }
}

async fn health_generate_handler(config: web::Data<Arc<RwLock<MockWorkerConfig>>>) -> HttpResponse {
    let config = config.read().await;

133
134
135
136
137
138
139
    // Simulate failure based on fail_rate
    if should_fail(&config).await {
        return HttpResponse::InternalServerError().json(json!({
            "error": "Random failure for testing"
        }));
    }

140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
    if matches!(config.health_status, HealthStatus::Healthy) {
        HttpResponse::Ok().json(json!({
            "status": "ok",
            "queue_length": 0,
            "processing_time_ms": config.response_delay_ms
        }))
    } else {
        HttpResponse::ServiceUnavailable().json(json!({
            "error": "Generation service unavailable"
        }))
    }
}

async fn server_info_handler(config: web::Data<Arc<RwLock<MockWorkerConfig>>>) -> HttpResponse {
    let config = config.read().await;

156
157
158
159
160
161
162
    // Simulate failure based on fail_rate
    if should_fail(&config).await {
        return HttpResponse::InternalServerError().json(json!({
            "error": "Random failure for testing"
        }));
    }

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
    // Return response matching actual sglang server implementation
    HttpResponse::Ok().json(json!({
        // Server args fields
        "model_path": "mock-model-path",
        "tokenizer_path": "mock-tokenizer-path",
        "port": config.port,
        "host": "127.0.0.1",
        "max_num_batched_tokens": 32768,
        "max_prefill_tokens": 16384,
        "mem_fraction_static": 0.88,
        "tp_size": 1,
        "dp_size": 1,
        "stream_interval": 8,
        "dtype": "float16",
        "device": "cuda",
        "enable_flashinfer": true,
        "enable_p2p_check": true,
        "context_length": 32768,
        "chat_template": null,
        "disable_radix_cache": false,
        "enable_torch_compile": false,
        "trust_remote_code": false,
        "show_time_cost": false,

        // Scheduler info fields
        "waiting_queue_size": 0,
        "running_queue_size": 0,
        "req_to_token_ratio": 1.2,
        "min_running_requests": 0,
        "max_running_requests": 2048,
        "max_req_num": 8192,
        "max_batch_tokens": 32768,
        "schedule_policy": "lpm",
        "schedule_conservativeness": 1.0,

        // Additional fields
        "version": "0.3.0",
        "internal_states": [{
            "waiting_queue_size": 0,
            "running_queue_size": 0
        }]
    }))
}

207
208
209
210
211
212
213
214
215
216
async fn model_info_handler(config: web::Data<Arc<RwLock<MockWorkerConfig>>>) -> HttpResponse {
    let config = config.read().await;

    // Simulate failure based on fail_rate
    if should_fail(&config).await {
        return HttpResponse::InternalServerError().json(json!({
            "error": "Random failure for testing"
        }));
    }

217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
    // Return response matching actual sglang server implementation
    HttpResponse::Ok().json(json!({
        "model_path": "mock-model-path",
        "tokenizer_path": "mock-tokenizer-path",
        "is_generation": true,
        "preferred_sampling_params": {
            "temperature": 0.7,
            "top_p": 0.9,
            "top_k": 40,
            "max_tokens": 2048
        }
    }))
}

async fn generate_handler(
    config: web::Data<Arc<RwLock<MockWorkerConfig>>>,
    _req: HttpRequest,
    payload: web::Json<serde_json::Value>,
) -> HttpResponse {
    let config = config.read().await;

    // Simulate failure based on fail_rate
239
    if should_fail(&config).await {
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
        return HttpResponse::InternalServerError().json(json!({
            "error": "Random failure for testing"
        }));
    }

    // Simulate processing delay
    if config.response_delay_ms > 0 {
        tokio::time::sleep(tokio::time::Duration::from_millis(config.response_delay_ms)).await;
    }

    let is_stream = payload
        .get("stream")
        .and_then(|v| v.as_bool())
        .unwrap_or(false);

    if is_stream {
        // Return streaming response matching sglang format
        let (tx, rx) = tokio::sync::mpsc::channel(10);
        let stream_delay = config.response_delay_ms;
        let request_id = format!("mock-req-{}", rand::random::<u32>());

        tokio::spawn(async move {
            let tokens = vec!["This ", "is ", "a ", "mock ", "response."];
263
264
265
266
            let timestamp_start = SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap()
                .as_secs_f64();
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284

            for (i, token) in tokens.iter().enumerate() {
                let chunk = json!({
                    "text": token,
                    "meta_info": {
                        "id": &request_id,
                        "finish_reason": if i == tokens.len() - 1 {
                            json!({"type": "stop", "matched_stop": null})
                        } else {
                            json!(null)
                        },
                        "prompt_tokens": 10,
                        "completion_tokens": i + 1,
                        "cached_tokens": 0,
                        "e2e_latency": SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs_f64() - timestamp_start
                    }
                });

285
286
287
288
289
290
291
292
                if tx
                    .send(format!(
                        "data: {}\n\n",
                        serde_json::to_string(&chunk).unwrap()
                    ))
                    .await
                    .is_err()
                {
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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
                    break;
                }

                if stream_delay > 0 {
                    tokio::time::sleep(tokio::time::Duration::from_millis(stream_delay)).await;
                }
            }

            let _ = tx.send("data: [DONE]\n\n".to_string()).await;
        });

        let stream = tokio_stream::wrappers::ReceiverStream::new(rx);

        HttpResponse::Ok()
            .content_type("text/event-stream")
            .insert_header(("Cache-Control", "no-cache"))
            .streaming(stream.map(|chunk| Ok::<_, actix_web::Error>(bytes::Bytes::from(chunk))))
    } else {
        // Return non-streaming response matching sglang format
        let request_id = format!("mock-req-{}", rand::random::<u32>());

        HttpResponse::Ok().json(json!({
            "text": "Mock generated response for the input",
            "meta_info": {
                "id": request_id,
                "finish_reason": {
                    "type": "stop",
                    "matched_stop": null
                },
                "prompt_tokens": 10,
                "completion_tokens": 7,
                "cached_tokens": 0,
                "e2e_latency": 0.042
            }
        }))
    }
}

async fn chat_completions_handler(
    config: web::Data<Arc<RwLock<MockWorkerConfig>>>,
    payload: web::Json<serde_json::Value>,
) -> HttpResponse {
    let config = config.read().await;

    // Simulate failure
    if rand::random::<f32>() < config.fail_rate {
        return HttpResponse::InternalServerError().json(json!({
            "error": "Chat completion failed"
        }));
    }

    let is_stream = payload
        .get("stream")
        .and_then(|v| v.as_bool())
        .unwrap_or(false);

    if is_stream {
        // Return proper streaming response for chat completions
        let (tx, rx) = tokio::sync::mpsc::channel(10);
        let stream_delay = config.response_delay_ms;
        let model = payload
            .get("model")
            .and_then(|m| m.as_str())
            .unwrap_or("mock-model")
            .to_string();

        tokio::spawn(async move {
            let chat_id = format!("chatcmpl-mock{}", rand::random::<u32>());
            let timestamp = SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap()
                .as_secs();

            // Send initial chunk with role
            let initial_chunk = json!({
                "id": &chat_id,
                "object": "chat.completion.chunk",
                "created": timestamp,
                "model": &model,
                "choices": [{
                    "index": 0,
                    "delta": {
                        "role": "assistant"
                    },
                    "finish_reason": null
                }]
            });

            let _ = tx
                .send(format!(
                    "data: {}\n\n",
                    serde_json::to_string(&initial_chunk).unwrap()
                ))
                .await;

            // Send content chunks
            let content_chunks = [
                "This ",
                "is ",
                "a ",
                "mock ",
                "streaming ",
                "chat ",
                "response.",
            ];
            for chunk in content_chunks.iter() {
                let data = json!({
                    "id": &chat_id,
                    "object": "chat.completion.chunk",
                    "created": timestamp,
                    "model": &model,
                    "choices": [{
                        "index": 0,
                        "delta": {
                            "content": chunk
                        },
                        "finish_reason": null
                    }]
                });

                if tx
                    .send(format!(
                        "data: {}\n\n",
                        serde_json::to_string(&data).unwrap()
                    ))
                    .await
                    .is_err()
                {
                    break;
                }

                if stream_delay > 0 {
                    tokio::time::sleep(tokio::time::Duration::from_millis(stream_delay)).await;
                }
            }

            // Send final chunk with finish_reason
            let final_chunk = json!({
                "id": &chat_id,
                "object": "chat.completion.chunk",
                "created": timestamp,
                "model": &model,
                "choices": [{
                    "index": 0,
                    "delta": {},
                    "finish_reason": "stop"
                }]
            });

            let _ = tx
                .send(format!(
                    "data: {}\n\n",
                    serde_json::to_string(&final_chunk).unwrap()
                ))
                .await;
            let _ = tx.send("data: [DONE]\n\n".to_string()).await;
        });

        let stream = tokio_stream::wrappers::ReceiverStream::new(rx);

        HttpResponse::Ok()
            .content_type("text/event-stream")
            .insert_header(("Cache-Control", "no-cache"))
            .streaming(stream.map(|chunk| Ok::<_, actix_web::Error>(bytes::Bytes::from(chunk))))
    } else {
        // Non-streaming response matching OpenAI format
        let model = payload
            .get("model")
            .and_then(|m| m.as_str())
            .unwrap_or("mock-model")
            .to_string();

        HttpResponse::Ok().json(json!({
            "id": format!("chatcmpl-{}", uuid::Uuid::new_v4()),
            "object": "chat.completion",
            "created": SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs(),
            "model": model,
            "choices": [{
                "index": 0,
                "message": {
                    "role": "assistant",
                    "content": "This is a mock chat completion response."
                },
                "logprobs": null,
                "finish_reason": "stop",
                "matched_stop": null
            }],
            "usage": {
                "prompt_tokens": 10,
                "completion_tokens": 8,
                "total_tokens": 18,
                "prompt_tokens_details": {
                    "cached_tokens": 0
                }
            }
        }))
    }
}

async fn completions_handler(
    config: web::Data<Arc<RwLock<MockWorkerConfig>>>,
    payload: web::Json<serde_json::Value>,
) -> HttpResponse {
    let config = config.read().await;

    if rand::random::<f32>() < config.fail_rate {
        return HttpResponse::InternalServerError().json(json!({
            "error": "Completion failed"
        }));
    }

    // Check if streaming is requested
    let is_stream = payload
        .get("stream")
        .and_then(|v| v.as_bool())
        .unwrap_or(false);

    let prompts = payload
        .get("prompt")
        .map(|p| {
            if p.is_array() {
                p.as_array().unwrap().len()
            } else {
                1
            }
        })
        .unwrap_or(1);

    if is_stream {
        // Return streaming response for completions
        let (tx, rx) = tokio::sync::mpsc::channel(10);
        let stream_delay = config.response_delay_ms;
        let model = payload
            .get("model")
            .and_then(|m| m.as_str())
            .unwrap_or("mock-model")
            .to_string();

        tokio::spawn(async move {
            let completion_id = format!("cmpl-mock{}", rand::random::<u32>());
            let timestamp = SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap()
                .as_secs();

            // Stream completions for each prompt
            for prompt_idx in 0..prompts {
                let prompt_suffix = format!("{} ", prompt_idx);
                let tokens = vec!["This ", "is ", "mock ", "completion ", &prompt_suffix];

                for (token_idx, token) in tokens.iter().enumerate() {
                    let data = json!({
                        "id": &completion_id,
                        "object": "text_completion",
                        "created": timestamp,
                        "model": &model,
                        "choices": [{
                            "text": token,
                            "index": prompt_idx,
                            "logprobs": null,
                            "finish_reason": if token_idx == tokens.len() - 1 { Some("stop") } else { None }
                        }]
                    });

                    if tx
                        .send(format!(
                            "data: {}\n\n",
                            serde_json::to_string(&data).unwrap()
                        ))
                        .await
                        .is_err()
                    {
                        return;
                    }

                    if stream_delay > 0 {
                        tokio::time::sleep(tokio::time::Duration::from_millis(stream_delay)).await;
                    }
                }
            }

            let _ = tx.send("data: [DONE]\n\n".to_string()).await;
        });

        let stream = tokio_stream::wrappers::ReceiverStream::new(rx);

        HttpResponse::Ok()
            .content_type("text/event-stream")
            .insert_header(("Cache-Control", "no-cache"))
            .streaming(stream.map(|chunk| Ok::<_, actix_web::Error>(bytes::Bytes::from(chunk))))
    } else {
        // Return non-streaming response
        let mut choices = vec![];
        for i in 0..prompts {
            choices.push(json!({
                "text": format!("Mock completion {}", i),
                "index": i,
                "logprobs": null,
                "finish_reason": "stop"
            }));
        }

        HttpResponse::Ok().json(json!({
            "id": format!("cmpl-mock{}", rand::random::<u32>()),
            "object": "text_completion",
            "created": SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs(),
            "model": payload.get("model").and_then(|m| m.as_str()).unwrap_or("mock-model"),
            "choices": choices,
            "usage": {
                "prompt_tokens": 5 * prompts,
                "completion_tokens": 10 * prompts,
                "total_tokens": 15 * prompts
            }
        }))
    }
}

610
611
612
613
614
615
616
617
618
619
async fn flush_cache_handler(config: web::Data<Arc<RwLock<MockWorkerConfig>>>) -> HttpResponse {
    let config = config.read().await;

    // Simulate failure based on fail_rate
    if should_fail(&config).await {
        return HttpResponse::InternalServerError().json(json!({
            "error": "Random failure for testing"
        }));
    }

620
621
622
623
624
625
626
    HttpResponse::Ok().json(json!({
        "status": "success",
        "message": "Cache flushed",
        "freed_entries": 42
    }))
}

627
628
629
630
631
632
633
634
635
636
async fn v1_models_handler(config: web::Data<Arc<RwLock<MockWorkerConfig>>>) -> HttpResponse {
    let config = config.read().await;

    // Simulate failure based on fail_rate
    if should_fail(&config).await {
        return HttpResponse::InternalServerError().json(json!({
            "error": "Random failure for testing"
        }));
    }

637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
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
    HttpResponse::Ok().json(json!({
        "object": "list",
        "data": [{
            "id": "mock-model-v1",
            "object": "model",
            "created": SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs(),
            "owned_by": "sglang",
            "permission": [{
                "id": "modelperm-mock",
                "object": "model_permission",
                "created": SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs(),
                "allow_create_engine": false,
                "allow_sampling": true,
                "allow_logprobs": true,
                "allow_search_indices": false,
                "allow_view": true,
                "allow_fine_tuning": false,
                "organization": "*",
                "group": null,
                "is_blocking": false
            }],
            "root": "mock-model-v1",
            "parent": null
        }]
    }))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_mock_worker_lifecycle() {
        let config = MockWorkerConfig {
            port: 18080,
            worker_type: WorkerType::Regular,
            health_status: HealthStatus::Healthy,
            response_delay_ms: 0,
            fail_rate: 0.0,
        };

        let mut worker = MockWorker::new(config);

        // Start the worker
        let url = worker.start().await.unwrap();
        assert_eq!(url, "http://127.0.0.1:18080");

        // Give server time to start
        tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;

        // Test health endpoint
        let client = reqwest::Client::new();
        let resp = client.get(&format!("{}/health", url)).send().await.unwrap();

        assert_eq!(resp.status(), 200);
        let body: serde_json::Value = resp.json().await.unwrap();
        assert_eq!(body["status"], "healthy");

        // Update config to unhealthy
        worker
            .update_config(|c| c.health_status = HealthStatus::Unhealthy)
            .await;

        // Test health again
        let resp = client.get(&format!("{}/health", url)).send().await.unwrap();

        assert_eq!(resp.status(), 503);

        // Stop the worker
        worker.stop().await;
    }
}