2025-08-28 12:07:06 -07:00
|
|
|
// PD (Prefill-Decode) gRPC Router Implementation
|
|
|
|
|
|
2025-09-04 00:35:51 -04:00
|
|
|
use crate::config::types::RetryConfig;
|
2025-09-01 20:06:15 -07:00
|
|
|
use crate::core::{
|
2025-09-19 09:19:57 -04:00
|
|
|
BasicWorkerBuilder, CircuitBreakerConfig, HealthConfig, WorkerRegistry, WorkerType,
|
2025-09-01 20:06:15 -07:00
|
|
|
};
|
|
|
|
|
use crate::metrics::RouterMetrics;
|
2025-09-19 09:19:57 -04:00
|
|
|
use crate::policies::{LoadBalancingPolicy, PolicyRegistry};
|
2025-09-01 20:06:15 -07:00
|
|
|
use crate::reasoning_parser::ParserFactory;
|
2025-09-22 15:17:50 -04:00
|
|
|
use crate::routers::RouterTrait;
|
2025-09-03 22:35:13 -04:00
|
|
|
use crate::tokenizer::traits::Tokenizer;
|
2025-09-01 20:06:15 -07:00
|
|
|
use crate::tool_parser::ParserRegistry;
|
2025-08-28 12:07:06 -07:00
|
|
|
use async_trait::async_trait;
|
|
|
|
|
use axum::{
|
|
|
|
|
body::Body,
|
|
|
|
|
extract::Request,
|
|
|
|
|
http::{HeaderMap, StatusCode},
|
|
|
|
|
response::{IntoResponse, Response},
|
|
|
|
|
};
|
2025-09-19 09:19:57 -04:00
|
|
|
use std::sync::Arc;
|
2025-09-01 20:06:15 -07:00
|
|
|
use std::time::Duration;
|
2025-09-26 06:13:47 -04:00
|
|
|
use tracing::info;
|
2025-08-28 12:07:06 -07:00
|
|
|
|
2025-09-01 20:06:15 -07:00
|
|
|
/// gRPC PD (Prefill-Decode) router implementation for SGLang
|
|
|
|
|
#[allow(dead_code)] // Fields will be used once implementation is complete
|
|
|
|
|
pub struct GrpcPDRouter {
|
2025-09-19 09:19:57 -04:00
|
|
|
/// Centralized worker registry
|
|
|
|
|
worker_registry: Arc<WorkerRegistry>,
|
|
|
|
|
/// Centralized policy registry
|
|
|
|
|
policy_registry: Arc<PolicyRegistry>,
|
2025-09-01 20:06:15 -07:00
|
|
|
/// Load balancing policy for prefill
|
|
|
|
|
prefill_policy: Arc<dyn LoadBalancingPolicy>,
|
|
|
|
|
/// Load balancing policy for decode
|
|
|
|
|
decode_policy: Arc<dyn LoadBalancingPolicy>,
|
|
|
|
|
/// Tokenizer for handling text encoding/decoding
|
|
|
|
|
tokenizer: Arc<dyn Tokenizer>,
|
|
|
|
|
/// Reasoning parser factory for structured reasoning outputs
|
|
|
|
|
reasoning_parser_factory: ParserFactory,
|
|
|
|
|
/// Tool parser registry for function/tool calls
|
|
|
|
|
tool_parser_registry: &'static ParserRegistry,
|
|
|
|
|
/// Configuration
|
|
|
|
|
timeout_secs: u64,
|
|
|
|
|
interval_secs: u64,
|
|
|
|
|
dp_aware: bool,
|
|
|
|
|
api_key: Option<String>,
|
|
|
|
|
retry_config: RetryConfig,
|
|
|
|
|
circuit_breaker_config: CircuitBreakerConfig,
|
|
|
|
|
}
|
2025-08-28 12:07:06 -07:00
|
|
|
|
|
|
|
|
impl GrpcPDRouter {
|
2025-09-01 20:06:15 -07:00
|
|
|
/// Create a new gRPC PD router
|
|
|
|
|
pub async fn new(
|
|
|
|
|
prefill_urls: Vec<(String, Option<u16>)>,
|
|
|
|
|
decode_urls: Vec<String>,
|
|
|
|
|
prefill_policy: Arc<dyn LoadBalancingPolicy>,
|
|
|
|
|
decode_policy: Arc<dyn LoadBalancingPolicy>,
|
2025-09-04 00:35:51 -04:00
|
|
|
ctx: &Arc<crate::server::AppContext>,
|
2025-09-01 20:06:15 -07:00
|
|
|
) -> Result<Self, String> {
|
2025-09-19 09:19:57 -04:00
|
|
|
// Get registries from context
|
|
|
|
|
let worker_registry = ctx.worker_registry.clone();
|
|
|
|
|
let policy_registry = ctx.policy_registry.clone();
|
|
|
|
|
|
2025-09-01 20:06:15 -07:00
|
|
|
// Update metrics
|
|
|
|
|
RouterMetrics::set_active_workers(prefill_urls.len() + decode_urls.len());
|
|
|
|
|
|
2025-09-04 00:35:51 -04:00
|
|
|
// Extract necessary components from context
|
|
|
|
|
let tokenizer = ctx
|
|
|
|
|
.tokenizer
|
|
|
|
|
.as_ref()
|
|
|
|
|
.ok_or_else(|| "gRPC PD router requires tokenizer".to_string())?
|
|
|
|
|
.clone();
|
|
|
|
|
let reasoning_parser_factory = ctx
|
|
|
|
|
.reasoning_parser_factory
|
|
|
|
|
.as_ref()
|
|
|
|
|
.ok_or_else(|| "gRPC PD router requires reasoning parser factory".to_string())?
|
|
|
|
|
.clone();
|
|
|
|
|
let tool_parser_registry = ctx
|
|
|
|
|
.tool_parser_registry
|
|
|
|
|
.ok_or_else(|| "gRPC PD router requires tool parser registry".to_string())?;
|
|
|
|
|
|
2025-09-01 20:06:15 -07:00
|
|
|
// Convert config CircuitBreakerConfig to core CircuitBreakerConfig
|
2025-09-04 00:35:51 -04:00
|
|
|
let circuit_breaker_config = ctx.router_config.effective_circuit_breaker_config();
|
2025-09-01 20:06:15 -07:00
|
|
|
let core_cb_config = CircuitBreakerConfig {
|
|
|
|
|
failure_threshold: circuit_breaker_config.failure_threshold,
|
|
|
|
|
success_threshold: circuit_breaker_config.success_threshold,
|
|
|
|
|
timeout_duration: Duration::from_secs(circuit_breaker_config.timeout_duration_secs),
|
|
|
|
|
window_duration: Duration::from_secs(circuit_breaker_config.window_duration_secs),
|
|
|
|
|
};
|
|
|
|
|
|
2025-09-19 09:19:57 -04:00
|
|
|
for (url, bootstrap_port) in &prefill_urls {
|
2025-09-26 06:13:47 -04:00
|
|
|
let worker = BasicWorkerBuilder::new(url.clone())
|
|
|
|
|
.worker_type(WorkerType::Prefill {
|
|
|
|
|
bootstrap_port: *bootstrap_port,
|
|
|
|
|
})
|
|
|
|
|
.connection_mode(crate::core::ConnectionMode::Grpc {
|
|
|
|
|
port: *bootstrap_port,
|
|
|
|
|
})
|
|
|
|
|
.circuit_breaker_config(core_cb_config.clone())
|
|
|
|
|
.health_config(HealthConfig {
|
|
|
|
|
timeout_secs: ctx.router_config.health_check.timeout_secs,
|
|
|
|
|
check_interval_secs: ctx.router_config.health_check.check_interval_secs,
|
|
|
|
|
endpoint: ctx.router_config.health_check.endpoint.clone(),
|
|
|
|
|
failure_threshold: ctx.router_config.health_check.failure_threshold,
|
|
|
|
|
success_threshold: ctx.router_config.health_check.success_threshold,
|
|
|
|
|
})
|
|
|
|
|
// No longer passing pre-initialized client - will be created lazily
|
|
|
|
|
.build();
|
2025-09-19 09:19:57 -04:00
|
|
|
|
2025-09-26 06:13:47 -04:00
|
|
|
worker_registry.register(Arc::new(worker));
|
|
|
|
|
info!(
|
|
|
|
|
"Registered gRPC prefill worker at {} (will connect on first use)",
|
|
|
|
|
url
|
|
|
|
|
);
|
2025-09-19 09:19:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for url in &decode_urls {
|
2025-09-26 06:13:47 -04:00
|
|
|
let worker = BasicWorkerBuilder::new(url.clone())
|
|
|
|
|
.worker_type(WorkerType::Decode)
|
|
|
|
|
.connection_mode(crate::core::ConnectionMode::Grpc { port: None })
|
|
|
|
|
.circuit_breaker_config(core_cb_config.clone())
|
|
|
|
|
.health_config(HealthConfig {
|
|
|
|
|
timeout_secs: ctx.router_config.health_check.timeout_secs,
|
|
|
|
|
check_interval_secs: ctx.router_config.health_check.check_interval_secs,
|
|
|
|
|
endpoint: ctx.router_config.health_check.endpoint.clone(),
|
|
|
|
|
failure_threshold: ctx.router_config.health_check.failure_threshold,
|
|
|
|
|
success_threshold: ctx.router_config.health_check.success_threshold,
|
|
|
|
|
})
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
worker_registry.register(Arc::new(worker));
|
|
|
|
|
info!(
|
|
|
|
|
"Registered gRPC decode worker at {} (will connect on first use)",
|
|
|
|
|
url
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-09-01 20:06:15 -07:00
|
|
|
|
2025-09-26 06:13:47 -04:00
|
|
|
if prefill_urls.is_empty() && decode_urls.is_empty() {
|
|
|
|
|
return Err("No gRPC workers configured".to_string());
|
2025-09-19 09:19:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Initialize policies with workers if needed - filter for gRPC workers only
|
|
|
|
|
let prefill_workers = worker_registry.get_workers_filtered(
|
|
|
|
|
None, // any model
|
|
|
|
|
Some(WorkerType::Prefill {
|
|
|
|
|
bootstrap_port: None,
|
|
|
|
|
}),
|
|
|
|
|
Some(crate::core::ConnectionMode::Grpc { port: None }),
|
|
|
|
|
false, // include unhealthy workers during initialization
|
|
|
|
|
);
|
2025-09-01 20:06:15 -07:00
|
|
|
if let Some(cache_aware) = prefill_policy
|
|
|
|
|
.as_any()
|
|
|
|
|
.downcast_ref::<crate::policies::CacheAwarePolicy>()
|
|
|
|
|
{
|
|
|
|
|
cache_aware.init_workers(&prefill_workers);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-19 09:19:57 -04:00
|
|
|
let decode_workers = worker_registry.get_workers_filtered(
|
|
|
|
|
None, // any model
|
|
|
|
|
Some(WorkerType::Decode),
|
|
|
|
|
Some(crate::core::ConnectionMode::Grpc { port: None }),
|
|
|
|
|
false, // include unhealthy workers during initialization
|
|
|
|
|
);
|
2025-09-01 20:06:15 -07:00
|
|
|
if let Some(cache_aware) = decode_policy
|
|
|
|
|
.as_any()
|
|
|
|
|
.downcast_ref::<crate::policies::CacheAwarePolicy>()
|
|
|
|
|
{
|
|
|
|
|
cache_aware.init_workers(&decode_workers);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-19 09:19:57 -04:00
|
|
|
// No need for local health checkers - WorkerRegistry handles health checking
|
2025-09-01 20:06:15 -07:00
|
|
|
|
|
|
|
|
Ok(GrpcPDRouter {
|
2025-09-19 09:19:57 -04:00
|
|
|
worker_registry,
|
|
|
|
|
policy_registry,
|
2025-09-01 20:06:15 -07:00
|
|
|
prefill_policy,
|
|
|
|
|
decode_policy,
|
|
|
|
|
tokenizer,
|
|
|
|
|
reasoning_parser_factory,
|
|
|
|
|
tool_parser_registry,
|
2025-09-04 00:35:51 -04:00
|
|
|
timeout_secs: ctx.router_config.worker_startup_timeout_secs,
|
|
|
|
|
interval_secs: ctx.router_config.worker_startup_check_interval_secs,
|
|
|
|
|
dp_aware: ctx.router_config.dp_aware,
|
|
|
|
|
api_key: ctx.router_config.api_key.clone(),
|
|
|
|
|
retry_config: ctx.router_config.effective_retry_config(),
|
2025-09-01 20:06:15 -07:00
|
|
|
circuit_breaker_config: core_cb_config,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl std::fmt::Debug for GrpcPDRouter {
|
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2025-09-19 09:19:57 -04:00
|
|
|
let prefill_workers = self.worker_registry.get_workers_filtered(
|
|
|
|
|
None,
|
|
|
|
|
Some(WorkerType::Prefill {
|
|
|
|
|
bootstrap_port: None,
|
|
|
|
|
}),
|
|
|
|
|
Some(crate::core::ConnectionMode::Grpc { port: None }),
|
|
|
|
|
false,
|
|
|
|
|
);
|
|
|
|
|
let decode_workers = self.worker_registry.get_workers_filtered(
|
|
|
|
|
None,
|
|
|
|
|
Some(WorkerType::Decode),
|
|
|
|
|
Some(crate::core::ConnectionMode::Grpc { port: None }),
|
|
|
|
|
false,
|
|
|
|
|
);
|
2025-09-01 20:06:15 -07:00
|
|
|
f.debug_struct("GrpcPDRouter")
|
2025-09-19 09:19:57 -04:00
|
|
|
.field("prefill_workers_count", &prefill_workers.len())
|
|
|
|
|
.field("decode_workers_count", &decode_workers.len())
|
2025-09-01 20:06:15 -07:00
|
|
|
.field("timeout_secs", &self.timeout_secs)
|
|
|
|
|
.field("interval_secs", &self.interval_secs)
|
|
|
|
|
.field("dp_aware", &self.dp_aware)
|
|
|
|
|
.finish()
|
2025-08-28 12:07:06 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
|
impl RouterTrait for GrpcPDRouter {
|
|
|
|
|
fn as_any(&self) -> &dyn std::any::Any {
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn health_generate(&self, _req: Request<Body>) -> Response {
|
2025-09-24 18:23:21 -04:00
|
|
|
// TODO: Implement actual generation test for gRPC PD mode
|
|
|
|
|
(
|
|
|
|
|
StatusCode::NOT_IMPLEMENTED,
|
|
|
|
|
"Health generate not yet implemented for gRPC PD",
|
|
|
|
|
)
|
|
|
|
|
.into_response()
|
2025-08-28 12:07:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn get_server_info(&self, _req: Request<Body>) -> Response {
|
|
|
|
|
(StatusCode::NOT_IMPLEMENTED).into_response()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn get_models(&self, _req: Request<Body>) -> Response {
|
|
|
|
|
(StatusCode::NOT_IMPLEMENTED).into_response()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn get_model_info(&self, _req: Request<Body>) -> Response {
|
|
|
|
|
(StatusCode::NOT_IMPLEMENTED).into_response()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn route_generate(
|
|
|
|
|
&self,
|
|
|
|
|
_headers: Option<&HeaderMap>,
|
|
|
|
|
_body: &crate::protocols::spec::GenerateRequest,
|
2025-09-12 19:18:27 -04:00
|
|
|
_model_id: Option<&str>,
|
2025-08-28 12:07:06 -07:00
|
|
|
) -> Response {
|
|
|
|
|
(StatusCode::NOT_IMPLEMENTED).into_response()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn route_chat(
|
|
|
|
|
&self,
|
|
|
|
|
_headers: Option<&HeaderMap>,
|
|
|
|
|
_body: &crate::protocols::spec::ChatCompletionRequest,
|
2025-09-12 19:18:27 -04:00
|
|
|
_model_id: Option<&str>,
|
2025-08-28 12:07:06 -07:00
|
|
|
) -> Response {
|
|
|
|
|
(StatusCode::NOT_IMPLEMENTED).into_response()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn route_completion(
|
|
|
|
|
&self,
|
|
|
|
|
_headers: Option<&HeaderMap>,
|
|
|
|
|
_body: &crate::protocols::spec::CompletionRequest,
|
2025-09-12 19:18:27 -04:00
|
|
|
_model_id: Option<&str>,
|
2025-08-28 12:07:06 -07:00
|
|
|
) -> Response {
|
|
|
|
|
(StatusCode::NOT_IMPLEMENTED).into_response()
|
2025-09-11 20:56:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn route_responses(
|
|
|
|
|
&self,
|
|
|
|
|
_headers: Option<&HeaderMap>,
|
|
|
|
|
_body: &crate::protocols::spec::ResponsesRequest,
|
2025-09-12 19:18:27 -04:00
|
|
|
_model_id: Option<&str>,
|
2025-09-11 20:56:17 -07:00
|
|
|
) -> Response {
|
|
|
|
|
(StatusCode::NOT_IMPLEMENTED).into_response()
|
2025-08-28 12:07:06 -07:00
|
|
|
}
|
|
|
|
|
|
2025-09-23 12:12:02 -04:00
|
|
|
async fn get_response(
|
|
|
|
|
&self,
|
|
|
|
|
_headers: Option<&HeaderMap>,
|
|
|
|
|
_response_id: &str,
|
|
|
|
|
_params: &crate::protocols::spec::ResponsesGetParams,
|
|
|
|
|
) -> Response {
|
2025-09-12 16:19:38 -07:00
|
|
|
(StatusCode::NOT_IMPLEMENTED).into_response()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn cancel_response(&self, _headers: Option<&HeaderMap>, _response_id: &str) -> Response {
|
|
|
|
|
(StatusCode::NOT_IMPLEMENTED).into_response()
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-15 09:44:35 +08:00
|
|
|
async fn route_embeddings(
|
|
|
|
|
&self,
|
|
|
|
|
_headers: Option<&HeaderMap>,
|
|
|
|
|
_body: &crate::protocols::spec::EmbeddingRequest,
|
|
|
|
|
_model_id: Option<&str>,
|
|
|
|
|
) -> Response {
|
2025-08-28 12:07:06 -07:00
|
|
|
(StatusCode::NOT_IMPLEMENTED).into_response()
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-13 00:10:18 +08:00
|
|
|
async fn route_rerank(
|
|
|
|
|
&self,
|
|
|
|
|
_headers: Option<&HeaderMap>,
|
|
|
|
|
_body: &crate::protocols::spec::RerankRequest,
|
2025-09-12 19:18:27 -04:00
|
|
|
_model_id: Option<&str>,
|
2025-09-13 00:10:18 +08:00
|
|
|
) -> Response {
|
2025-08-28 12:07:06 -07:00
|
|
|
(StatusCode::NOT_IMPLEMENTED).into_response()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn router_type(&self) -> &'static str {
|
|
|
|
|
"grpc_pd"
|
|
|
|
|
}
|
|
|
|
|
}
|