| 
1 | 1 | // SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.  | 
2 | 2 | // SPDX-License-Identifier: Apache-2.0  | 
3 | 3 | 
 
  | 
4 |  | -use anyhow::Context as _;  | 
5 |  | - | 
6 |  | -use crate::discovery::{ModelEntry, MODEL_ROOT_PATH};  | 
7 |  | -use dynamo_runtime::component::{self, Instance};  | 
8 |  | -use dynamo_runtime::slug::Slug;  | 
9 |  | -use dynamo_runtime::transports::etcd;  | 
 | 4 | +use crate::discovery::MODEL_ROOT_PATH;  | 
10 | 5 | 
 
  | 
11 | 6 | #[derive(Debug, Clone)]  | 
12 | 7 | pub struct ModelNetworkName(String);  | 
13 | 8 | 
 
  | 
14 | 9 | impl ModelNetworkName {  | 
15 |  | -    /// Key to store this model entry in networked key-value store (etcd).  | 
16 |  | -    ///  | 
17 |  | -    /// It looks like this:  | 
18 |  | -    /// ns.cp.ep-694d967ca5efd804  | 
19 |  | -    fn from_parts(namespace: &str, component: &str, endpoint: &str, lease_id: i64) -> Self {  | 
20 |  | -        let model_root = MODEL_ROOT_PATH;  | 
21 |  | -        let slug = Slug::slugify(&format!("{namespace}.{component}.{endpoint}-{lease_id:x}"));  | 
22 |  | -        ModelNetworkName(format!("{model_root}/{slug}"))  | 
23 |  | -    }  | 
24 |  | - | 
25 |  | -    // We can't do From<&component::Endpoint> here because we also need the lease_id  | 
26 |  | -    pub fn from_local(endpoint: &component::Endpoint, lease_id: i64) -> Self {  | 
27 |  | -        Self::from_parts(  | 
28 |  | -            &endpoint.component().namespace().to_string(),  | 
29 |  | -            &endpoint.component().name(),  | 
30 |  | -            endpoint.name(),  | 
31 |  | -            lease_id,  | 
32 |  | -        )  | 
 | 10 | +    pub fn new() -> Self {  | 
 | 11 | +        ModelNetworkName(format!("{MODEL_ROOT_PATH}/{}", uuid::Uuid::new_v4()))  | 
33 | 12 |     }  | 
 | 13 | +}  | 
34 | 14 | 
 
  | 
35 |  | -    pub fn from_entry(entry: &ModelEntry, lease_id: i64) -> Self {  | 
36 |  | -        Self::from_parts(  | 
37 |  | -            &entry.endpoint.namespace,  | 
38 |  | -            &entry.endpoint.component,  | 
39 |  | -            &entry.endpoint.name,  | 
40 |  | -            lease_id,  | 
41 |  | -        )  | 
 | 15 | +impl Default for ModelNetworkName {  | 
 | 16 | +    fn default() -> Self {  | 
 | 17 | +        Self::new()  | 
42 | 18 |     }  | 
 | 19 | +}  | 
43 | 20 | 
 
  | 
44 |  | -    /// Fetch the ModelEntry from etcd.  | 
45 |  | -    pub async fn load_entry(&self, etcd_client: &etcd::Client) -> anyhow::Result<ModelEntry> {  | 
46 |  | -        let mut model_entries = etcd_client.kv_get(self.to_string(), None).await?;  | 
47 |  | -        if model_entries.is_empty() {  | 
48 |  | -            anyhow::bail!("No ModelEntry in etcd for key {self}");  | 
49 |  | -        }  | 
50 |  | -        let model_entry = model_entries.remove(0);  | 
51 |  | -        serde_json::from_slice(model_entry.value()).with_context(|| {  | 
52 |  | -            format!(  | 
53 |  | -                "Error deserializing JSON. Key={self}. JSON={}",  | 
54 |  | -                model_entry.value_str().unwrap_or("INVALID UTF-8")  | 
55 |  | -            )  | 
56 |  | -        })  | 
 | 21 | +impl std::fmt::Display for ModelNetworkName {  | 
 | 22 | +    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {  | 
 | 23 | +        write!(f, "{}", self.0)  | 
57 | 24 |     }  | 
58 | 25 | }  | 
59 | 26 | 
 
  | 
60 |  | -impl From<&Instance> for ModelNetworkName {  | 
61 |  | -    fn from(cei: &Instance) -> Self {  | 
62 |  | -        Self::from_parts(  | 
63 |  | -            &cei.namespace,  | 
64 |  | -            &cei.component,  | 
65 |  | -            &cei.endpoint,  | 
66 |  | -            cei.instance_id,  | 
67 |  | -        )  | 
 | 27 | +impl AsRef<str> for ModelNetworkName {  | 
 | 28 | +    fn as_ref(&self) -> &str {  | 
 | 29 | +        &self.0  | 
68 | 30 |     }  | 
69 | 31 | }  | 
70 | 32 | 
 
  | 
71 |  | -impl std::fmt::Display for ModelNetworkName {  | 
72 |  | -    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {  | 
73 |  | -        write!(f, "{}", self.0)  | 
 | 33 | +impl std::ops::Deref for ModelNetworkName {  | 
 | 34 | +    type Target = str;  | 
 | 35 | +    fn deref(&self) -> &Self::Target {  | 
 | 36 | +        &self.0  | 
74 | 37 |     }  | 
75 | 38 | }  | 
0 commit comments