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

4
5
use std::sync::Arc;

6
use crate::{
7
    discovery::{ModelManager, ModelUpdate, ModelWatcher},
8
    endpoint_type::EndpointType,
9
    engines::StreamingEngineAdapter,
10
    entrypoint::{self, EngineConfig, input::common},
11
    http::service::service_v2::{self, HttpService},
12
    kv_router::KvRouterConfig,
13
    model_card,
14
    namespace::is_global_namespace,
15
16
17
    types::openai::{
        chat_completions::{NvCreateChatCompletionRequest, NvCreateChatCompletionStreamResponse},
        completions::{NvCreateCompletionRequest, NvCreateCompletionResponse},
18
19
    },
};
20
use dynamo_runtime::transports::etcd;
21
use dynamo_runtime::{DistributedRuntime, Runtime};
22
use dynamo_runtime::{distributed::DistributedConfig, pipeline::RouterMode};
23
24

/// Build and run an HTTP service
25
pub async fn run(runtime: Runtime, engine_config: EngineConfig) -> anyhow::Result<()> {
Graham King's avatar
Graham King committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
    let local_model = engine_config.local_model();
    let mut http_service_builder = match (local_model.tls_cert_path(), local_model.tls_key_path()) {
        (Some(tls_cert_path), Some(tls_key_path)) => {
            if !tls_cert_path.exists() {
                anyhow::bail!("TLS certificate not found: {}", tls_cert_path.display());
            }
            if !tls_key_path.exists() {
                anyhow::bail!("TLS key not found: {}", tls_key_path.display());
            }
            service_v2::HttpService::builder()
                .enable_tls(true)
                .tls_cert_path(Some(tls_cert_path.to_path_buf()))
                .tls_key_path(Some(tls_key_path.to_path_buf()))
                .port(local_model.http_port())
        }
        (None, None) => service_v2::HttpService::builder().port(local_model.http_port()),
        (_, _) => {
            // CLI should prevent us ever getting here
            anyhow::bail!(
                "Both --tls-cert-path and --tls-key-path must be provided together to enable TLS"
            );
        }
    };
49
50
51
    if let Some(http_host) = local_model.http_host() {
        http_service_builder = http_service_builder.host(http_host);
    }
Graham King's avatar
Graham King committed
52
53
    http_service_builder =
        http_service_builder.with_request_template(engine_config.local_model().request_template());
54

55
56
57
58
59
60
61
62
63
    // DEPRECATED: To be removed after custom backends migrate to Dynamo backend.
    // Pass the custom backend metrics endpoint as-is (already in namespace.component.endpoint format)
    http_service_builder = http_service_builder.with_custom_backend_config(
        local_model
            .custom_backend_metrics_endpoint()
            .map(|s| s.to_string()),
        local_model.custom_backend_metrics_polling_interval(),
    );

64
    let http_service = match engine_config {
65
        EngineConfig::Dynamic(_) => {
66
67
            let distributed_runtime = DistributedRuntime::from_settings(runtime.clone()).await?;
            // This allows the /health endpoint to query etcd for active instances
68
            http_service_builder = http_service_builder.store(distributed_runtime.store().clone());
69
            let http_service = http_service_builder.build()?;
70
            let etcd_client = distributed_runtime.etcd_client();
71
72
            match etcd_client {
                Some(ref etcd_client) => {
73
                    let router_config = engine_config.local_model().router_config();
74
                    // Listen for models registering themselves in etcd, add them to HTTP service
75
76
77
78
79
80
81
82
                    // Check if we should filter by namespace (based on the local model's namespace)
                    // Get namespace from the model, fallback to endpoint_id namespace if not set
                    let namespace = engine_config.local_model().namespace().unwrap_or("");
                    let target_namespace = if is_global_namespace(namespace) {
                        None
                    } else {
                        Some(namespace.to_string())
                    };
83
                    run_watcher(
84
                        distributed_runtime,
85
                        http_service.state().manager_clone(),
86
                        etcd_client.clone(),
87
                        model_card::ROOT_PATH,
88
                        router_config.router_mode,
89
                        Some(router_config.kv_router_config),
90
                        router_config.busy_threshold,
91
                        target_namespace,
92
                        Arc::new(http_service.clone()),
93
                        http_service.state().metrics_clone(),
94
95
                    )
                    .await?;
96
97
98
99
100
                }
                None => {
                    // Static endpoints don't need discovery
                }
            }
101
            http_service
102
        }
103
104
        EngineConfig::StaticRemote(local_model) => {
            let card = local_model.card();
105
106
            let checksum = card.mdcsum();

107
108
            let router_mode = local_model.router_config().router_mode;

109
            let dst_config = DistributedConfig::from_settings(true); // true means static
110
            let distributed_runtime = DistributedRuntime::new(runtime.clone(), dst_config).await?;
111
            let http_service = http_service_builder.build()?;
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
            let manager = http_service.model_manager();

            let endpoint_id = local_model.endpoint_id();
            let component = distributed_runtime
                .namespace(&endpoint_id.namespace)?
                .component(&endpoint_id.component)?;
            let client = component.endpoint(&endpoint_id.name).client().await?;

            let kv_chooser = if router_mode == RouterMode::KV {
                Some(
                    manager
                        .kv_chooser_for(
                            &component,
                            card.kv_cache_block_size,
                            Some(local_model.router_config().kv_router_config),
                        )
                        .await?,
                )
            } else {
                None
            };

134
            let tokenizer_hf = card.tokenizer_hf()?;
135
136
137
            let chat_engine = entrypoint::build_routed_pipeline::<
                NvCreateChatCompletionRequest,
                NvCreateChatCompletionStreamResponse,
138
139
140
141
142
143
144
            >(
                card,
                &client,
                router_mode,
                None,
                kv_chooser.clone(),
                tokenizer_hf.clone(),
145
                None, // No prefill chooser in http static mode
146
            )
147
            .await?;
148
149
150
151
152
            manager.add_chat_completions_model(
                local_model.display_name(),
                checksum,
                chat_engine,
            )?;
153

154
155
156
157
158
159
160
161
162
163
164
165
166
            let completions_engine = entrypoint::build_routed_pipeline::<
                NvCreateCompletionRequest,
                NvCreateCompletionResponse,
            >(
                card,
                &client,
                router_mode,
                None,
                kv_chooser,
                tokenizer_hf,
                None, // No prefill chooser in http static mode
            )
            .await?;
167
168
169
170
171
            manager.add_completions_model(
                local_model.display_name(),
                checksum,
                completions_engine,
            )?;
172

173
            for endpoint_type in EndpointType::all() {
174
                http_service.enable_model_endpoint(endpoint_type, true);
175
176
            }

177
            http_service
178
179
        }
        EngineConfig::StaticFull { engine, model, .. } => {
180
            let http_service = http_service_builder.build()?;
181
182
            let engine = Arc::new(StreamingEngineAdapter::new(engine));
            let manager = http_service.model_manager();
183
184
185
            let checksum = model.card().mdcsum();
            manager.add_completions_model(model.service_name(), checksum, engine.clone())?;
            manager.add_chat_completions_model(model.service_name(), checksum, engine)?;
186
187
188

            // Enable all endpoints
            for endpoint_type in EndpointType::all() {
189
                http_service.enable_model_endpoint(endpoint_type, true);
190
            }
191
            http_service
192
        }
193
194
        EngineConfig::StaticCore {
            engine: inner_engine,
195
            model,
196
            ..
197
        } => {
198
            let http_service = http_service_builder.build()?;
199
            let manager = http_service.model_manager();
200
            let checksum = model.card().mdcsum();
201

202
203
204
205
206
207
208
            let tokenizer_hf = model.card().tokenizer_hf()?;
            let chat_pipeline =
                common::build_pipeline::<
                    NvCreateChatCompletionRequest,
                    NvCreateChatCompletionStreamResponse,
                >(model.card(), inner_engine.clone(), tokenizer_hf.clone())
                .await?;
209
            manager.add_chat_completions_model(model.service_name(), checksum, chat_pipeline)?;
210

211
212
            let cmpl_pipeline = common::build_pipeline::<
                NvCreateCompletionRequest,
213
                NvCreateCompletionResponse,
214
            >(model.card(), inner_engine, tokenizer_hf)
215
            .await?;
216
            manager.add_completions_model(model.service_name(), checksum, cmpl_pipeline)?;
217
218
            // Enable all endpoints
            for endpoint_type in EndpointType::all() {
219
                http_service.enable_model_endpoint(endpoint_type, true);
220
            }
221
            http_service
222
        }
223
    };
224
225
226
227
228
229
230
231
    tracing::debug!(
        "Supported routes: {:?}",
        http_service
            .route_docs()
            .iter()
            .map(|rd| rd.to_string())
            .collect::<Vec<String>>()
    );
232
233
234
235
236
237
238
239
240
241
242
243

    // DEPRECATED: To be removed after custom backends migrate to Dynamo backend.
    // Start custom backend metrics polling if configured
    let polling_task =
        if let (Some(namespace_component_endpoint), Some(polling_interval), Some(registry)) = (
            http_service
                .custom_backend_namespace_component_endpoint
                .as_ref(),
            http_service.custom_backend_metrics_polling_interval,
            http_service.custom_backend_registry.as_ref(),
        ) {
            // Create DistributedRuntime for polling, matching the engine's mode
244
            let drt = DistributedRuntime::from_settings(runtime.clone()).await?;
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
            tracing::info!(
                namespace_component_endpoint=%namespace_component_endpoint,
                polling_interval_secs=polling_interval,
                "Starting custom backend metrics polling task"
            );
            // Spawn the polling task and keep the JoinHandle alive so it can be aborted during
            // shutdown. While graceful shutdown is not strictly necessary for this non-critical
            // metrics polling, explicitly aborting it prevents the task from running during the
            // shutdown phase.
            Some(
                crate::http::service::custom_backend_metrics::spawn_custom_backend_polling_task(
                    drt,
                    namespace_component_endpoint.clone(),
                    polling_interval,
                    registry.clone(),
                ),
            )
        } else {
            None
        };

266
    http_service.run(runtime.primary_token()).await?;
267
268
269
270
271
272

    // Abort the polling task if it was started
    if let Some(task) = polling_task {
        task.abort();
    }

273
274
    runtime.shutdown(); // Cancel primary token
    Ok(())
275
}
276
277
278

/// Spawns a task that watches for new models in etcd at network_prefix,
/// and registers them with the ModelManager so that the HTTP service can use them.
279
#[allow(clippy::too_many_arguments)]
280
async fn run_watcher(
281
    runtime: DistributedRuntime,
282
    model_manager: Arc<ModelManager>,
283
284
    etcd_client: etcd::Client,
    network_prefix: &str,
285
    router_mode: RouterMode,
286
    kv_router_config: Option<KvRouterConfig>,
287
    busy_threshold: Option<f64>,
288
    target_namespace: Option<String>,
289
    http_service: Arc<HttpService>,
290
    metrics: Arc<crate::http::service::metrics::Metrics>,
291
) -> anyhow::Result<()> {
292
293
294
295
296
297
298
    let mut watch_obj = ModelWatcher::new(
        runtime,
        model_manager,
        router_mode,
        kv_router_config,
        busy_threshold,
    );
299
300
301
    tracing::info!("Watching for remote model at {network_prefix}");
    let models_watcher = etcd_client.kv_get_and_watch_prefix(network_prefix).await?;
    let (_prefix, _watcher, receiver) = models_watcher.dissolve();
302
303
304
305
306
307

    // Create a channel to receive model type updates
    let (tx, mut rx) = tokio::sync::mpsc::channel(32);

    watch_obj.set_notify_on_model_update(tx);

308
    // Spawn a task to watch for model type changes and update HTTP service endpoints and metrics
309
    let _endpoint_enabler_task = tokio::spawn(async move {
310
311
312
        while let Some(model_update) = rx.recv().await {
            update_http_endpoints(http_service.clone(), model_update.clone());
            update_model_metrics(model_update, metrics.clone());
313
314
315
316
        }
    });

    // Pass the sender to the watcher
317
    let _watcher_task = tokio::spawn(async move {
318
        watch_obj.watch(receiver, target_namespace.as_deref()).await;
319
    });
320

321
322
    Ok(())
}
323
324

/// Updates HTTP service endpoints based on available model types
325
fn update_http_endpoints(service: Arc<HttpService>, model_type: ModelUpdate) {
326
327
328
329
330
    tracing::debug!(
        "Updating HTTP service endpoints for model type: {:?}",
        model_type
    );
    match model_type {
331
        ModelUpdate::Added(card) => {
332
            // Handle all supported endpoint types, not just the first one
333
            for endpoint_type in card.model_type.as_endpoint_types() {
334
                service.enable_model_endpoint(endpoint_type, true);
335
            }
336
        }
337
        ModelUpdate::Removed(card) => {
338
            // Handle all supported endpoint types, not just the first one
339
            for endpoint_type in card.model_type.as_endpoint_types() {
340
                service.enable_model_endpoint(endpoint_type, false);
341
            }
342
        }
343
344
    }
}
345
346

/// Updates metrics for model type changes
347
fn update_model_metrics(
348
349
350
351
    model_type: ModelUpdate,
    metrics: Arc<crate::http::service::metrics::Metrics>,
) {
    match model_type {
352
353
354
355
        ModelUpdate::Added(card) => {
            tracing::debug!("Updating metrics for added model: {}", card.display_name);
            if let Err(err) = metrics.update_metrics_from_mdc(&card) {
                tracing::warn!(%err, model_name=card.display_name, "update_metrics_from_mdc failed");
356
357
            }
        }
358
359
        ModelUpdate::Removed(card) => {
            tracing::debug!(model_name = card.display_name, "Model removed");
360
361
362
363
364
            // Note: Metrics are typically not removed to preserve historical data
            // This matches the behavior in the polling task
        }
    }
}