"components/router/vscode:/vscode.git/clone" did not exist on "5166a3dd442f58866b12b535c7a9900caaabedcf"
http.rs 11 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::{EngineConfig, EngineFactoryCallback, RouterConfig, input::common},
11
    http::service::service_v2::{self, HttpService},
12
    namespace::is_global_namespace,
13
14
15
    types::openai::{
        chat_completions::{NvCreateChatCompletionRequest, NvCreateChatCompletionStreamResponse},
        completions::{NvCreateCompletionRequest, NvCreateCompletionResponse},
16
17
    },
};
18
use dynamo_runtime::DistributedRuntime;
19
20

/// Build and run an HTTP service
21
22
23
24
pub async fn run(
    distributed_runtime: DistributedRuntime,
    engine_config: EngineConfig,
) -> anyhow::Result<()> {
Graham King's avatar
Graham King committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
    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"
            );
        }
    };
48
49
50
    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
51
52
    http_service_builder =
        http_service_builder.with_request_template(engine_config.local_model().request_template());
53

54
55
56
57
58
59
60
61
62
    // 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(),
    );

63
    let http_service = match engine_config {
64
65
66
67
        EngineConfig::Dynamic {
            ref model,
            ref engine_factory,
        } => {
68
            // This allows the /health endpoint to query store for active instances
69
            http_service_builder = http_service_builder.store(distributed_runtime.store().clone());
70
            let http_service = http_service_builder.build()?;
71

72
            let router_config = model.router_config();
73
74
75
            // Listen for models registering themselves, add them to HTTP service
            // 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
76
            let namespace = model.namespace().unwrap_or("");
77
78
79
80
81
82
            let target_namespace = if is_global_namespace(namespace) {
                None
            } else {
                Some(namespace.to_string())
            };
            run_watcher(
83
                distributed_runtime.clone(),
84
                http_service.state().manager_clone(),
85
                router_config.clone(),
86
87
88
                target_namespace,
                Arc::new(http_service.clone()),
                http_service.state().metrics_clone(),
89
                engine_factory.clone(),
90
91
            )
            .await?;
92
            http_service
93
        }
94
        EngineConfig::InProcessText { engine, model, .. } => {
95
            let http_service = http_service_builder.build()?;
96
97
            let engine = Arc::new(StreamingEngineAdapter::new(engine));
            let manager = http_service.model_manager();
98
            let checksum = model.card().mdcsum();
99
100
            manager.add_completions_model(model.display_name(), checksum, engine.clone())?;
            manager.add_chat_completions_model(model.display_name(), checksum, engine)?;
101
102
103

            // Enable all endpoints
            for endpoint_type in EndpointType::all() {
104
                http_service.enable_model_endpoint(endpoint_type, true);
105
            }
106
            http_service
107
        }
108
        EngineConfig::InProcessTokens {
109
            engine: inner_engine,
110
            model,
111
            ..
112
        } => {
113
            let http_service = http_service_builder.build()?;
114
            let manager = http_service.model_manager();
115
            let checksum = model.card().mdcsum();
116

117
118
119
120
121
122
123
            let tokenizer_hf = model.card().tokenizer_hf()?;
            let chat_pipeline =
                common::build_pipeline::<
                    NvCreateChatCompletionRequest,
                    NvCreateChatCompletionStreamResponse,
                >(model.card(), inner_engine.clone(), tokenizer_hf.clone())
                .await?;
124
            manager.add_chat_completions_model(model.display_name(), checksum, chat_pipeline)?;
125

126
127
            let cmpl_pipeline = common::build_pipeline::<
                NvCreateCompletionRequest,
128
                NvCreateCompletionResponse,
129
            >(model.card(), inner_engine, tokenizer_hf)
130
            .await?;
131
            manager.add_completions_model(model.display_name(), checksum, cmpl_pipeline)?;
132
133
            // Enable all endpoints
            for endpoint_type in EndpointType::all() {
134
                http_service.enable_model_endpoint(endpoint_type, true);
135
            }
136
            http_service
137
        }
138
    };
139
140
141
142
143
144
145
146
    tracing::debug!(
        "Supported routes: {:?}",
        http_service
            .route_docs()
            .iter()
            .map(|rd| rd.to_string())
            .collect::<Vec<String>>()
    );
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168

    // 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(),
        ) {
            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(
169
                    distributed_runtime.clone(),
170
171
172
173
174
175
176
177
178
                    namespace_component_endpoint.clone(),
                    polling_interval,
                    registry.clone(),
                ),
            )
        } else {
            None
        };

179
180
181
    http_service
        .run(distributed_runtime.primary_token())
        .await?;
182
183
184
185
186
187

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

188
    distributed_runtime.shutdown(); // Cancel primary token
189
    Ok(())
190
}
191

192
/// Spawns a task that watches for new models in store,
193
194
/// and registers them with the ModelManager so that the HTTP service can use them.
async fn run_watcher(
195
    runtime: DistributedRuntime,
196
    model_manager: Arc<ModelManager>,
197
    router_config: RouterConfig,
198
    target_namespace: Option<String>,
199
    http_service: Arc<HttpService>,
200
    metrics: Arc<crate::http::service::metrics::Metrics>,
201
    engine_factory: Option<EngineFactoryCallback>,
202
) -> anyhow::Result<()> {
203
204
205
206
207
    let mut watch_obj = ModelWatcher::new(
        runtime.clone(),
        model_manager,
        router_config,
        engine_factory,
208
        metrics.clone(),
209
    );
210
    tracing::debug!("Waiting for remote model");
211
212
213
214
215
216
217
    let discovery = runtime.discovery();
    let discovery_stream = discovery
        .list_and_watch(
            dynamo_runtime::discovery::DiscoveryQuery::AllModels,
            Some(runtime.primary_token()),
        )
        .await?;
218
219
220
221
222

    // 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);

223
    // Spawn a task to watch for model type changes and update HTTP service endpoints and metrics
224
    let _endpoint_enabler_task = tokio::spawn(async move {
225
226
227
        while let Some(model_update) = rx.recv().await {
            update_http_endpoints(http_service.clone(), model_update.clone());
            update_model_metrics(model_update, metrics.clone());
228
229
230
        }
    });

231
    // Pass the discovery stream to the watcher
232
    let _watcher_task = tokio::spawn(async move {
233
234
235
        watch_obj
            .watch(discovery_stream, target_namespace.as_deref())
            .await;
236
    });
237

238
239
    Ok(())
}
240
241

/// Updates HTTP service endpoints based on available model types
242
fn update_http_endpoints(service: Arc<HttpService>, model_type: ModelUpdate) {
243
244
245
246
247
    tracing::debug!(
        "Updating HTTP service endpoints for model type: {:?}",
        model_type
    );
    match model_type {
248
        ModelUpdate::Added(card) => {
249
            // Handle all supported endpoint types, not just the first one
250
            for endpoint_type in card.model_type.as_endpoint_types() {
251
                service.enable_model_endpoint(endpoint_type, true);
252
            }
253
        }
254
        ModelUpdate::Removed(card) => {
255
            // Handle all supported endpoint types, not just the first one
256
            for endpoint_type in card.model_type.as_endpoint_types() {
257
                service.enable_model_endpoint(endpoint_type, false);
258
            }
259
        }
260
261
    }
}
262
263

/// Updates metrics for model type changes
264
fn update_model_metrics(
265
266
267
268
    model_type: ModelUpdate,
    metrics: Arc<crate::http::service::metrics::Metrics>,
) {
    match model_type {
269
270
271
272
        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");
273
274
            }
        }
275
276
        ModelUpdate::Removed(card) => {
            tracing::debug!(model_name = card.display_name, "Model removed");
277
278
279
280
281
            // Note: Metrics are typically not removed to preserve historical data
            // This matches the behavior in the polling task
        }
    }
}