modelexpress-server 0.4.0

High-performance gRPC server for model serving and management
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
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
436
// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

//! Kubernetes CRD types for ModelMetadata.
//!
//! These types define the ModelMetadata CustomResourceDefinition used as an
//! alternative to Redis for storing P2P metadata.

use kube::CustomResource;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

/// ModelMetadata spec - the desired state
#[derive(CustomResource, Clone, Debug, Deserialize, Serialize, JsonSchema)]
#[kube(
    group = "modelexpress.nvidia.com",
    version = "v1alpha1",
    kind = "ModelMetadata",
    plural = "modelmetadatas",
    shortname = "mxmeta",
    namespaced,
    status = "ModelMetadataStatus"
)]
pub struct ModelMetadataSpec {
    /// Full model name (e.g., deepseek-ai/DeepSeek-V3)
    #[serde(rename = "modelName")]
    pub model_name: String,
}

/// ModelMetadata status - the observed state
#[derive(Clone, Debug, Default, Deserialize, Serialize, JsonSchema)]
pub struct ModelMetadataStatus {
    /// Single worker NIXL metadata and readiness state (one CR per worker)
    #[serde(default)]
    pub worker: Option<WorkerStatus>,

    /// Conditions for ModelMetadata lifecycle
    #[serde(default)]
    pub conditions: Vec<Condition>,

    /// Generation observed by the controller
    #[serde(rename = "observedGeneration", default)]
    pub observed_generation: i64,

    /// Timestamp when first worker published
    #[serde(rename = "publishedAt", default)]
    pub published_at: Option<String>,
}

/// Per-worker status
#[derive(Clone, Debug, Default, Deserialize, Serialize, JsonSchema)]
pub struct WorkerStatus {
    /// Worker rank (0-indexed)
    #[serde(rename = "workerRank")]
    pub worker_rank: i32,

    /// Backend type discriminator ("nixl", "transfer_engine", "none")
    #[serde(rename = "backendType", default)]
    pub backend_type: Option<String>,

    /// Base64-encoded NIXL agent metadata blob
    #[serde(rename = "nixlMetadata", default)]
    pub nixl_metadata: String,

    /// Mooncake TransferEngine session ID
    #[serde(rename = "transferEngineSessionId", default)]
    pub transfer_engine_session_id: Option<String>,

    /// Number of tensors registered by this worker
    #[serde(rename = "tensorCount", default)]
    pub tensor_count: i32,

    /// Name of ConfigMap containing tensor descriptors
    #[serde(rename = "tensorConfigMap", default)]
    pub tensor_config_map: Option<String>,

    /// Worker lifecycle status (Initializing, Ready, Stale)
    #[serde(default)]
    pub status: String,

    /// Timestamp of last status update (RFC3339)
    #[serde(rename = "updatedAt", default)]
    pub updated_at: Option<String>,

    /// P2P: NIXL listen thread endpoint (host:port)
    #[serde(rename = "metadataEndpoint", default)]
    pub metadata_endpoint: String,

    /// P2P: NIXL agent name
    #[serde(rename = "agentName", default)]
    pub agent_name: String,

    /// P2P: Worker gRPC endpoint for tensor manifest (host:port)
    #[serde(rename = "workerGrpcEndpoint", default)]
    pub worker_grpc_endpoint: String,
}

impl WorkerStatus {
    /// Convert a `SourceStatus` proto enum value (i32) to the CRD status string.
    pub fn status_name_from_proto(status: i32) -> String {
        match status {
            0 => "Unknown",
            1 => "Initializing",
            2 => "Ready",
            3 => "Stale",
            _ => "Unknown",
        }
        .to_string()
    }

    /// Convert a CRD status string back to the `SourceStatus` proto enum value (i32).
    pub fn status_proto_from_name(name: &str) -> i32 {
        match name {
            "Initializing" => 1,
            "Ready" => 2,
            "Stale" => 3,
            _ => 0,
        }
    }
}

/// Standard Kubernetes condition
#[derive(Clone, Debug, Default, Deserialize, Serialize, JsonSchema)]
pub struct Condition {
    /// Condition type
    #[serde(rename = "type")]
    pub type_: String,

    /// Status: True, False, Unknown
    pub status: String,

    /// Machine-readable reason for condition
    #[serde(default)]
    pub reason: Option<String>,

    /// Human-readable message
    #[serde(default)]
    pub message: Option<String>,

    /// Timestamp of last transition
    #[serde(rename = "lastTransitionTime", default)]
    pub last_transition_time: Option<String>,
}

impl ModelMetadataStatus {
    /// Insert or update a condition by type. If a condition with the same type
    /// already exists, it is updated in place; `lastTransitionTime` is only
    /// changed when `status` actually transitions.
    pub fn set_condition(&mut self, type_: &str, status: &str, reason: &str, message: &str) {
        let now = chrono::Utc::now().to_rfc3339();
        if let Some(existing) = self.conditions.iter_mut().find(|c| c.type_ == type_) {
            if existing.status != status {
                existing.last_transition_time = Some(now);
            }
            existing.status = status.to_string();
            existing.reason = Some(reason.to_string());
            existing.message = Some(message.to_string());
        } else {
            self.conditions.push(Condition {
                type_: type_.to_string(),
                status: status.to_string(),
                reason: Some(reason.to_string()),
                message: Some(message.to_string()),
                last_transition_time: Some(now),
            });
        }
    }

    /// Update the `Ready` condition based on the worker's proto status value.
    /// Ready=True only when the worker status is `SOURCE_STATUS_READY` (2).
    pub fn update_ready_condition(&mut self, worker_proto_status: i32) {
        let is_ready = worker_proto_status == 2; // SOURCE_STATUS_READY
        if is_ready {
            self.set_condition("Ready", "True", "WorkerReady", "Worker is ready");
        } else {
            let status_name = WorkerStatus::status_name_from_proto(worker_proto_status);
            self.set_condition(
                "Ready",
                "False",
                &format!("Worker{}", status_name),
                "Worker is not ready",
            );
        }
    }
}

/// Tensor descriptor stored in ConfigMap
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct TensorDescriptorJson {
    pub name: String,
    /// Serialized as string to avoid precision loss
    pub addr: String,
    /// Serialized as string to avoid precision loss
    pub size: String,
    pub device_id: u32,
    pub dtype: String,
}

/// Sanitize model name to be a valid Kubernetes resource name
/// e.g., "deepseek-ai/DeepSeek-V3" -> "deepseek-ai-deepseek-v3"
pub fn sanitize_model_name(model_name: &str) -> String {
    model_name
        .to_lowercase()
        .replace(['/', '_'], "-")
        .chars()
        .filter(|c| c.is_ascii_alphanumeric() || *c == '-' || *c == '.')
        .collect::<String>()
        .trim_matches('-')
        .to_string()
}

#[cfg(test)]
#[allow(clippy::expect_used)]
mod tests {
    use super::*;

    #[test]
    fn test_status_roundtrip() {
        for (proto, name) in [
            (0, "Unknown"),
            (1, "Initializing"),
            (2, "Ready"),
            (3, "Stale"),
        ] {
            assert_eq!(WorkerStatus::status_name_from_proto(proto), name);
            assert_eq!(WorkerStatus::status_proto_from_name(name), proto);
        }
    }

    /// Regression test: proto status 0 (SOURCE_STATUS_UNKNOWN) must survive a
    /// write-to-CRD -> read-from-CRD roundtrip. Before the fix, status_proto_from_name
    /// returned None for "Unknown", causing get_metadata to hard-error on any worker
    /// that hadn't received an explicit UpdateStatus call after PublishMetadata.
    #[test]
    fn test_status_unknown_roundtrip() {
        let written = WorkerStatus::status_name_from_proto(0);
        assert_eq!(written, "Unknown");
        let read_back = WorkerStatus::status_proto_from_name(&written);
        assert_eq!(
            read_back, 0,
            "Unknown status must roundtrip to proto value 0"
        );
    }

    #[test]
    fn test_status_name_from_proto_unknown() {
        assert_eq!(WorkerStatus::status_name_from_proto(99), "Unknown");
        assert_eq!(WorkerStatus::status_name_from_proto(4), "Unknown");
    }

    #[test]
    fn test_status_proto_from_name_unknown() {
        assert_eq!(WorkerStatus::status_proto_from_name("Unknown"), 0);
        assert_eq!(WorkerStatus::status_proto_from_name(""), 0);
        assert_eq!(WorkerStatus::status_proto_from_name("ready"), 0);
    }

    #[test]
    fn test_sanitize_model_name() {
        assert_eq!(
            sanitize_model_name("deepseek-ai/DeepSeek-V3"),
            "deepseek-ai-deepseek-v3"
        );
        assert_eq!(
            sanitize_model_name("meta-llama/Llama-3.1-70B"),
            "meta-llama-llama-3.1-70b"
        );
        assert_eq!(sanitize_model_name("simple-model"), "simple-model");
    }

    #[test]
    fn test_sanitize_model_name_special_chars() {
        assert_eq!(sanitize_model_name("Llama@3.1+8B"), "llama3.18b");
        assert_eq!(sanitize_model_name("model with spaces"), "modelwithspaces");
        assert_eq!(
            sanitize_model_name("org_name/model_v2"),
            "org-name-model-v2"
        );
    }

    #[test]
    fn test_sanitize_model_name_edge_cases() {
        assert_eq!(sanitize_model_name(""), "");
        assert_eq!(sanitize_model_name("///"), "");
        assert_eq!(sanitize_model_name("---"), "");
        assert_eq!(sanitize_model_name("-model-"), "model");
    }

    #[test]
    fn test_tensor_descriptor_json_roundtrip() {
        let original = TensorDescriptorJson {
            name: "model.layers.0.weight".to_string(),
            addr: "139948187451390".to_string(),
            size: "134217728".to_string(),
            device_id: 0,
            dtype: "bfloat16".to_string(),
        };

        let json = serde_json::to_string(&original).expect("serialize");
        let parsed: TensorDescriptorJson = serde_json::from_str(&json).expect("deserialize");

        assert_eq!(parsed.name, original.name);
        assert_eq!(parsed.addr, original.addr);
        assert_eq!(parsed.size, original.size);
        assert_eq!(parsed.device_id, original.device_id);
        assert_eq!(parsed.dtype, original.dtype);

        let addr: u64 = parsed.addr.parse().expect("addr should parse as u64");
        assert_eq!(addr, 139948187451390);
        let size: u64 = parsed.size.parse().expect("size should parse as u64");
        assert_eq!(size, 134217728);
    }

    #[test]
    fn test_tensor_descriptor_json_large_values() {
        let desc = TensorDescriptorJson {
            name: "test".to_string(),
            addr: u64::MAX.to_string(),
            size: u64::MAX.to_string(),
            device_id: 7,
            dtype: "float16".to_string(),
        };

        let json = serde_json::to_string(&desc).expect("serialize");
        let parsed: TensorDescriptorJson = serde_json::from_str(&json).expect("deserialize");

        let addr: u64 = parsed.addr.parse().expect("max u64 addr should parse");
        assert_eq!(addr, u64::MAX);
    }

    #[test]
    fn test_set_condition_inserts_new() {
        let mut status = ModelMetadataStatus::default();
        assert!(status.conditions.is_empty());

        status.set_condition("Ready", "True", "WorkerPublished", "Published");

        assert_eq!(status.conditions.len(), 1);
        let cond = &status.conditions[0];
        assert_eq!(cond.type_, "Ready");
        assert_eq!(cond.status, "True");
        assert_eq!(cond.reason.as_deref(), Some("WorkerPublished"));
        assert_eq!(cond.message.as_deref(), Some("Published"));
        assert!(cond.last_transition_time.is_some());
    }

    #[test]
    fn test_set_condition_updates_existing() {
        let mut status = ModelMetadataStatus::default();
        status.set_condition("Ready", "True", "WorkerPublished", "Published");
        let original_time = status.conditions[0].last_transition_time.clone();

        status.set_condition("Ready", "False", "WorkerStale", "Worker is stale");

        assert_eq!(status.conditions.len(), 1);
        let cond = &status.conditions[0];
        assert_eq!(cond.status, "False");
        assert_eq!(cond.reason.as_deref(), Some("WorkerStale"));
        assert_ne!(
            cond.last_transition_time, original_time,
            "lastTransitionTime must change on status transition"
        );
    }

    #[test]
    fn test_set_condition_same_status_preserves_transition_time() {
        let mut status = ModelMetadataStatus::default();
        status.set_condition("Ready", "True", "WorkerPublished", "Published");
        let original_time = status.conditions[0].last_transition_time.clone();

        status.set_condition("Ready", "True", "StillReady", "Still ready");

        assert_eq!(status.conditions.len(), 1);
        assert_eq!(status.conditions[0].reason.as_deref(), Some("StillReady"));
        assert_eq!(
            status.conditions[0].last_transition_time, original_time,
            "lastTransitionTime must not change when status stays the same"
        );
    }

    #[test]
    fn test_update_ready_condition_ready() {
        let mut status = ModelMetadataStatus::default();
        status.update_ready_condition(2); // SOURCE_STATUS_READY

        assert_eq!(status.conditions.len(), 1);
        let cond = &status.conditions[0];
        assert_eq!(cond.type_, "Ready");
        assert_eq!(cond.status, "True");
        assert_eq!(cond.reason.as_deref(), Some("WorkerReady"));
    }

    #[test]
    fn test_update_ready_condition_not_ready_states() {
        for (proto, expected_reason) in [
            (0, "WorkerUnknown"),
            (1, "WorkerInitializing"),
            (3, "WorkerStale"),
        ] {
            let mut status = ModelMetadataStatus::default();
            status.update_ready_condition(proto);

            assert_eq!(status.conditions.len(), 1);
            let cond = &status.conditions[0];
            assert_eq!(cond.type_, "Ready");
            assert_eq!(cond.status, "False");
            assert_eq!(
                cond.reason.as_deref(),
                Some(expected_reason),
                "proto status {} should produce reason {}",
                proto,
                expected_reason
            );
        }
    }

    #[test]
    fn test_update_ready_condition_transition() {
        let mut status = ModelMetadataStatus::default();

        status.update_ready_condition(1); // Initializing
        assert_eq!(status.conditions[0].status, "False");
        let time_false = status.conditions[0].last_transition_time.clone();

        status.update_ready_condition(2); // Ready
        assert_eq!(status.conditions[0].status, "True");
        assert_ne!(
            status.conditions[0].last_transition_time, time_false,
            "lastTransitionTime must change on False->True transition"
        );

        status.update_ready_condition(3); // Stale
        assert_eq!(status.conditions[0].status, "False");
        assert_eq!(status.conditions[0].reason.as_deref(), Some("WorkerStale"));
    }
}