service_v2.rs 15.6 KB
Newer Older
1
2
// SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
3

4
use std::collections::HashMap;
5
use std::env::var;
Graham King's avatar
Graham King committed
6
use std::path::PathBuf;
7
use std::sync::Arc;
8
9
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
10
use std::time::Duration;
11

12
use super::Metrics;
13
use super::RouteDoc;
14
use super::metrics;
15
use crate::discovery::ModelManager;
16
use crate::endpoint_type::EndpointType;
17
use crate::request_template::RequestTemplate;
18
use anyhow::Result;
Graham King's avatar
Graham King committed
19
use axum_server::tls_rustls::RustlsConfig;
20
use derive_builder::Builder;
21
use dynamo_runtime::logging::make_request_span;
22
use dynamo_runtime::transports::etcd;
Graham King's avatar
Graham King committed
23
use std::net::SocketAddr;
24
use tokio::task::JoinHandle;
25
use tokio_util::sync::CancellationToken;
26
use tower_http::trace::TraceLayer;
27

28
/// HTTP service shared state
29
#[derive(Default)]
30
31
32
pub struct State {
    metrics: Arc<Metrics>,
    manager: Arc<ModelManager>,
33
    etcd_client: Option<etcd::Client>,
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
    flags: StateFlags,
}

#[derive(Default, Debug)]
struct StateFlags {
    chat_endpoints_enabled: AtomicBool,
    cmpl_endpoints_enabled: AtomicBool,
    embeddings_endpoints_enabled: AtomicBool,
    responses_endpoints_enabled: AtomicBool,
}

impl StateFlags {
    pub fn get(&self, endpoint_type: &EndpointType) -> bool {
        match endpoint_type {
            EndpointType::Chat => self.chat_endpoints_enabled.load(Ordering::Relaxed),
            EndpointType::Completion => self.cmpl_endpoints_enabled.load(Ordering::Relaxed),
            EndpointType::Embedding => self.embeddings_endpoints_enabled.load(Ordering::Relaxed),
            EndpointType::Responses => self.responses_endpoints_enabled.load(Ordering::Relaxed),
        }
    }

    pub fn set(&self, endpoint_type: &EndpointType, enabled: bool) {
        match endpoint_type {
            EndpointType::Chat => self
                .chat_endpoints_enabled
                .store(enabled, Ordering::Relaxed),
            EndpointType::Completion => self
                .cmpl_endpoints_enabled
                .store(enabled, Ordering::Relaxed),
            EndpointType::Embedding => self
                .embeddings_endpoints_enabled
                .store(enabled, Ordering::Relaxed),
            EndpointType::Responses => self
                .responses_endpoints_enabled
                .store(enabled, Ordering::Relaxed),
        }
    }
71
72
73
74
75
76
77
}

impl State {
    pub fn new(manager: Arc<ModelManager>) -> Self {
        Self {
            manager,
            metrics: Arc::new(Metrics::default()),
78
            etcd_client: None,
79
80
81
82
83
84
            flags: StateFlags {
                chat_endpoints_enabled: AtomicBool::new(false),
                cmpl_endpoints_enabled: AtomicBool::new(false),
                embeddings_endpoints_enabled: AtomicBool::new(false),
                responses_endpoints_enabled: AtomicBool::new(false),
            },
85
86
87
88
89
90
91
92
        }
    }

    pub fn new_with_etcd(manager: Arc<ModelManager>, etcd_client: Option<etcd::Client>) -> Self {
        Self {
            manager,
            metrics: Arc::new(Metrics::default()),
            etcd_client,
93
94
95
96
97
98
            flags: StateFlags {
                chat_endpoints_enabled: AtomicBool::new(false),
                cmpl_endpoints_enabled: AtomicBool::new(false),
                embeddings_endpoints_enabled: AtomicBool::new(false),
                responses_endpoints_enabled: AtomicBool::new(false),
            },
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
        }
    }
    /// Get the Prometheus [`Metrics`] object which tracks request counts and inflight requests
    pub fn metrics_clone(&self) -> Arc<Metrics> {
        self.metrics.clone()
    }

    pub fn manager(&self) -> &ModelManager {
        Arc::as_ref(&self.manager)
    }

    pub fn manager_clone(&self) -> Arc<ModelManager> {
        self.manager.clone()
    }

114
115
116
117
    pub fn etcd_client(&self) -> Option<&etcd::Client> {
        self.etcd_client.as_ref()
    }

118
119
120
121
122
123
    // TODO
    pub fn sse_keep_alive(&self) -> Option<Duration> {
        None
    }
}

124
125
#[derive(Clone)]
pub struct HttpService {
126
127
128
    // The state we share with every request handler
    state: Arc<State>,

129
130
    router: axum::Router,
    port: u16,
131
    host: String,
Graham King's avatar
Graham King committed
132
133
134
    enable_tls: bool,
    tls_cert_path: Option<PathBuf>,
    tls_key_path: Option<PathBuf>,
135
    route_docs: Vec<RouteDoc>,
136
137
138

    // Metrics polling configuration
    etcd_client: Option<dynamo_runtime::transports::etcd::Client>,
139
140
141
}

#[derive(Clone, Builder)]
142
#[builder(pattern = "owned", build_fn(private, name = "build_internal"))]
143
144
145
146
pub struct HttpServiceConfig {
    #[builder(default = "8787")]
    port: u16,

147
148
149
    #[builder(setter(into), default = "String::from(\"0.0.0.0\")")]
    host: String,

Graham King's avatar
Graham King committed
150
151
152
153
154
155
156
157
158
    #[builder(default = "false")]
    enable_tls: bool,

    #[builder(default = "None")]
    tls_cert_path: Option<PathBuf>,

    #[builder(default = "None")]
    tls_key_path: Option<PathBuf>,

159
160
    // #[builder(default)]
    // custom: Vec<axum::Router>
161
    #[builder(default = "false")]
162
163
    enable_chat_endpoints: bool,

164
    #[builder(default = "false")]
165
    enable_cmpl_endpoints: bool,
166

167
    #[builder(default = "true")]
168
169
    enable_embeddings_endpoints: bool,

170
171
172
    #[builder(default = "true")]
    enable_responses_endpoints: bool,

173
174
    #[builder(default = "None")]
    request_template: Option<RequestTemplate>,
175
176
177

    #[builder(default = "None")]
    etcd_client: Option<etcd::Client>,
178
179
180
181
182
183
184
}

impl HttpService {
    pub fn builder() -> HttpServiceConfigBuilder {
        HttpServiceConfigBuilder::default()
    }

185
186
187
188
189
190
191
192
    pub fn state_clone(&self) -> Arc<State> {
        self.state.clone()
    }

    pub fn state(&self) -> &State {
        Arc::as_ref(&self.state)
    }

193
    pub fn model_manager(&self) -> &ModelManager {
194
        self.state().manager()
195
196
    }

197
198
199
200
201
202
    pub async fn spawn(&self, cancel_token: CancellationToken) -> JoinHandle<Result<()>> {
        let this = self.clone();
        tokio::spawn(async move { this.run(cancel_token).await })
    }

    pub async fn run(&self, cancel_token: CancellationToken) -> Result<()> {
203
        let address = format!("{}:{}", self.host, self.port);
Graham King's avatar
Graham King committed
204
205
        let protocol = if self.enable_tls { "HTTPS" } else { "HTTP" };
        tracing::info!(protocol, address, "Starting HTTP(S) service");
206

207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
        // Start background task to poll runtime config metrics with proper cancellation
        let poll_interval_secs = std::env::var("DYN_HTTP_SVC_CONFIG_METRICS_POLL_INTERVAL_SECS")
            .ok()
            .and_then(|s| s.parse::<f64>().ok())
            .filter(|&secs| secs > 0.0) // Guard against zero or negative values
            .unwrap_or(8.0);
        let poll_interval = Duration::from_secs_f64(poll_interval_secs);

        let _polling_task = super::metrics::Metrics::start_runtime_config_polling_task(
            self.state.metrics_clone(),
            self.state.manager_clone(),
            self.etcd_client.clone(),
            poll_interval,
            cancel_token.child_token(),
        );

223
224
225
        let router = self.router.clone();
        let observer = cancel_token.child_token();

Graham King's avatar
Graham King committed
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
        let addr: SocketAddr = address
            .parse()
            .map_err(|e| anyhow::anyhow!("Invalid address '{}': {}", address, e))?;

        if self.enable_tls {
            let cert_path = self
                .tls_cert_path
                .as_ref()
                .ok_or_else(|| anyhow::anyhow!("TLS certificate path not provided"))?;
            let key_path = self
                .tls_key_path
                .as_ref()
                .ok_or_else(|| anyhow::anyhow!("TLS private key path not provided"))?;

            // aws_lc_rs is the default but other crates pull in `ring` also,
            // so rustls doesn't know which one to use. Tell it.
            if let Err(e) = rustls::crypto::aws_lc_rs::default_provider().install_default() {
                tracing::debug!("TLS crypto provider already installed: {e:?}");
            }

            let config = RustlsConfig::from_pem_file(cert_path, key_path)
                .await
                .map_err(|e| anyhow::anyhow!("Failed to create TLS config: {}", e))?;

            let handle = axum_server::Handle::new();
            let server = axum_server::bind_rustls(addr, config)
                .handle(handle.clone())
                .serve(router.into_make_service());

            tokio::select! {
                result = server => {
                    result.map_err(|e| anyhow::anyhow!("HTTPS server error: {}", e))?;
                }
                _ = observer.cancelled() => {
                    tracing::info!("HTTPS server shutdown requested");
                    handle.graceful_shutdown(Some(Duration::from_secs(5)));
                    // TODO: Do we need to wait?
                }
            }
        } else {
            let listener = tokio::net::TcpListener::bind(addr)
                .await
                .unwrap_or_else(|_| panic!("could not bind to address: {address}"));

            axum::serve(listener, router)
                .with_graceful_shutdown(observer.cancelled_owned())
                .await
                .inspect_err(|_| cancel_token.cancel())?;
        }
275
276

        Ok(())
277
    }
278
279
280
281
282

    /// Documentation of exposed HTTP endpoints
    pub fn route_docs(&self) -> &[RouteDoc] {
        &self.route_docs
    }
283

284
    pub fn enable_model_endpoint(&self, endpoint_type: EndpointType, enable: bool) {
285
286
287
288
289
290
291
        self.state.flags.set(&endpoint_type, enable);
        tracing::info!(
            "{} endpoints {}",
            endpoint_type.as_str(),
            if enable { "enabled" } else { "disabled" }
        );
    }
292
293
}

294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/// Environment variable to set the metrics endpoint path (default: `/metrics`)
static HTTP_SVC_METRICS_PATH_ENV: &str = "DYN_HTTP_SVC_METRICS_PATH";
/// Environment variable to set the models endpoint path (default: `/v1/models`)
static HTTP_SVC_MODELS_PATH_ENV: &str = "DYN_HTTP_SVC_MODELS_PATH";
/// Environment variable to set the health endpoint path (default: `/health`)
static HTTP_SVC_HEALTH_PATH_ENV: &str = "DYN_HTTP_SVC_HEALTH_PATH";
/// Environment variable to set the live endpoint path (default: `/live`)
static HTTP_SVC_LIVE_PATH_ENV: &str = "DYN_HTTP_SVC_LIVE_PATH";
/// Environment variable to set the chat completions endpoint path (default: `/v1/chat/completions`)
static HTTP_SVC_CHAT_PATH_ENV: &str = "DYN_HTTP_SVC_CHAT_PATH";
/// Environment variable to set the completions endpoint path (default: `/v1/completions`)
static HTTP_SVC_CMP_PATH_ENV: &str = "DYN_HTTP_SVC_CMP_PATH";
/// Environment variable to set the embeddings endpoint path (default: `/v1/embeddings`)
static HTTP_SVC_EMB_PATH_ENV: &str = "DYN_HTTP_SVC_EMB_PATH";
/// Environment variable to set the responses endpoint path (default: `/v1/responses`)
static HTTP_SVC_RESPONSES_PATH_ENV: &str = "DYN_HTTP_SVC_RESPONSES_PATH";

311
312
impl HttpServiceConfigBuilder {
    pub fn build(self) -> Result<HttpService, anyhow::Error> {
313
        let config: HttpServiceConfig = self.build_internal()?;
314

315
        let model_manager = Arc::new(ModelManager::new());
316
        let etcd_client = config.etcd_client.clone();
317
        let state = Arc::new(State::new_with_etcd(model_manager, config.etcd_client));
318

319
320
321
322
323
324
325
326
327
328
329
330
331
        state
            .flags
            .set(&EndpointType::Chat, config.enable_chat_endpoints);
        state
            .flags
            .set(&EndpointType::Completion, config.enable_cmpl_endpoints);
        state
            .flags
            .set(&EndpointType::Embedding, config.enable_embeddings_endpoints);
        state
            .flags
            .set(&EndpointType::Responses, config.enable_responses_endpoints);

332
333
        // enable prometheus metrics
        let registry = metrics::Registry::new();
334
        state.metrics_clone().register(&registry)?;
335

336
337
        // Note: Metrics polling task will be started in run() method to have access to cancellation token

338
        let mut router = axum::Router::new();
339

340
341
342
        let mut all_docs = Vec::new();

        let mut routes = vec![
343
344
345
346
            metrics::router(registry, var(HTTP_SVC_METRICS_PATH_ENV).ok()),
            super::openai::list_models_router(state.clone(), var(HTTP_SVC_MODELS_PATH_ENV).ok()),
            super::health::health_check_router(state.clone(), var(HTTP_SVC_HEALTH_PATH_ENV).ok()),
            super::health::live_check_router(state.clone(), var(HTTP_SVC_LIVE_PATH_ENV).ok()),
347
348
        ];

349
350
351
352
        let endpoint_routes =
            HttpServiceConfigBuilder::get_endpoints_router(state.clone(), &config.request_template);
        routes.extend(endpoint_routes);
        for (route_docs, route) in routes {
353
354
355
356
            router = router.merge(route);
            all_docs.extend(route_docs);
        }

357
358
359
        // Add span for tracing
        router = router.layer(TraceLayer::new_for_http().make_span_with(make_request_span));

360
        Ok(HttpService {
361
            state,
362
363
            router,
            port: config.port,
364
            host: config.host,
Graham King's avatar
Graham King committed
365
366
367
            enable_tls: config.enable_tls,
            tls_cert_path: config.tls_cert_path,
            tls_key_path: config.tls_key_path,
368
            route_docs: all_docs,
369
            etcd_client,
370
371
        })
    }
372
373
374
375
376

    pub fn with_request_template(mut self, request_template: Option<RequestTemplate>) -> Self {
        self.request_template = Some(request_template);
        self
    }
377
378
379
380
381

    pub fn with_etcd_client(mut self, etcd_client: Option<etcd::Client>) -> Self {
        self.etcd_client = Some(etcd_client);
        self
    }
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

    fn get_endpoints_router(
        state: Arc<State>,
        request_template: &Option<RequestTemplate>,
    ) -> Vec<(Vec<RouteDoc>, axum::Router)> {
        let mut routes = Vec::new();
        // Add chat completions route with conditional middleware
        let (chat_docs, chat_route) = super::openai::chat_completions_router(
            state.clone(),
            request_template.clone(),
            var(HTTP_SVC_CHAT_PATH_ENV).ok(),
        );
        let (cmpl_docs, cmpl_route) =
            super::openai::completions_router(state.clone(), var(HTTP_SVC_CMP_PATH_ENV).ok());
        let (embed_docs, embed_route) =
            super::openai::embeddings_router(state.clone(), var(HTTP_SVC_EMB_PATH_ENV).ok());
        let (responses_docs, responses_route) = super::openai::responses_router(
            state.clone(),
            request_template.clone(),
            var(HTTP_SVC_RESPONSES_PATH_ENV).ok(),
        );

        let mut endpoint_routes = HashMap::new();
        endpoint_routes.insert(EndpointType::Chat, (chat_docs, chat_route));
        endpoint_routes.insert(EndpointType::Completion, (cmpl_docs, cmpl_route));
        endpoint_routes.insert(EndpointType::Embedding, (embed_docs, embed_route));
        endpoint_routes.insert(EndpointType::Responses, (responses_docs, responses_route));

        for endpoint_type in EndpointType::all() {
            let state_route = state.clone();
            if !endpoint_routes.contains_key(&endpoint_type) {
                tracing::debug!("{} endpoints are disabled", endpoint_type.as_str());
                continue;
            }
            let (docs, route) = endpoint_routes.get(&endpoint_type).cloned().unwrap();
            let route = route.route_layer(axum::middleware::from_fn(
                move |req: axum::http::Request<axum::body::Body>, next: axum::middleware::Next| {
                    let state: Arc<State> = state_route.clone();
                    async move {
                        // Check if the endpoint is enabled
                        let enabled = state.flags.get(&endpoint_type);
                        if enabled {
                            Ok(next.run(req).await)
                        } else {
                            tracing::debug!("{} endpoints are disabled", endpoint_type.as_str());
                            Err(axum::http::StatusCode::SERVICE_UNAVAILABLE)
                        }
                    }
                },
            ));
            routes.push((docs, route));
        }
        routes
    }
436
}