Skip to main content

burncloud_service_models/
runtime.rs

1use serde::{Deserialize, Serialize};
2use uuid::Uuid;
3use chrono::{DateTime, Utc};
4use std::collections::HashMap;
5
6/// 模型运行时配置
7#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct RuntimeConfig {
9    /// 最大上下文长度
10    pub max_context_length: Option<u32>,
11    /// 温度参数
12    pub temperature: Option<f32>,
13    /// Top-p 参数
14    pub top_p: Option<f32>,
15    /// Top-k 参数
16    pub top_k: Option<u32>,
17    /// 最大生成长度
18    pub max_tokens: Option<u32>,
19    /// 停止词
20    pub stop_sequences: Vec<String>,
21    /// 批处理大小
22    pub batch_size: Option<u32>,
23    /// 并发请求数
24    pub max_concurrent_requests: Option<u32>,
25    /// GPU 设备ID
26    pub gpu_device_ids: Vec<u32>,
27    /// 内存限制 (MB)
28    pub memory_limit_mb: Option<u64>,
29    /// 是否启用流式输出
30    pub enable_streaming: bool,
31    /// 自定义参数
32    pub custom_params: HashMap<String, serde_json::Value>,
33}
34
35/// 模型运行时实例
36#[derive(Debug, Clone, Serialize, Deserialize)]
37pub struct ModelRuntime {
38    /// 运行时ID
39    pub id: Uuid,
40    /// 模型ID
41    pub model_id: Uuid,
42    /// 运行时名称
43    pub name: String,
44    /// 绑定端口
45    pub port: u16,
46    /// 进程ID
47    pub process_id: Option<u32>,
48    /// 运行时配置
49    pub config: RuntimeConfig,
50    /// 启动时间
51    pub started_at: Option<DateTime<Utc>>,
52    /// 停止时间
53    pub stopped_at: Option<DateTime<Utc>>,
54    /// 运行状态
55    pub status: crate::ModelStatus,
56    /// 健康检查端点
57    pub health_endpoint: String,
58    /// API 端点
59    pub api_endpoint: String,
60    /// 日志文件路径
61    pub log_file: Option<String>,
62    /// 环境变量
63    pub environment: HashMap<String, String>,
64}
65
66/// 运行时性能指标
67#[derive(Debug, Clone, Serialize, Deserialize)]
68pub struct RuntimeMetrics {
69    /// 运行时ID
70    pub runtime_id: Uuid,
71    /// 采样时间
72    pub timestamp: DateTime<Utc>,
73    /// CPU 使用率 (百分比)
74    pub cpu_usage_percent: f32,
75    /// 内存使用量 (MB)
76    pub memory_usage_mb: u64,
77    /// GPU 使用率 (百分比)
78    pub gpu_usage_percent: Option<f32>,
79    /// GPU 内存使用量 (MB)
80    pub gpu_memory_usage_mb: Option<u64>,
81    /// 活跃连接数
82    pub active_connections: u32,
83    /// 总请求数
84    pub total_requests: u64,
85    /// 成功请求数
86    pub successful_requests: u64,
87    /// 失败请求数
88    pub failed_requests: u64,
89    /// 平均响应时间 (毫秒)
90    pub avg_response_time_ms: f32,
91    /// 吞吐量 (请求/秒)
92    pub throughput_rps: f32,
93    /// 队列长度
94    pub queue_length: u32,
95}
96
97/// 运行时事件
98#[derive(Debug, Clone, Serialize, Deserialize)]
99pub struct RuntimeEvent {
100    /// 事件ID
101    pub id: Uuid,
102    /// 运行时ID
103    pub runtime_id: Uuid,
104    /// 事件类型
105    pub event_type: RuntimeEventType,
106    /// 事件时间
107    pub timestamp: DateTime<Utc>,
108    /// 事件描述
109    pub message: String,
110    /// 事件详情
111    pub details: Option<serde_json::Value>,
112    /// 严重程度
113    pub severity: EventSeverity,
114}
115
116/// 运行时事件类型
117#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
118pub enum RuntimeEventType {
119    /// 启动
120    Started,
121    /// 停止
122    Stopped,
123    /// 重启
124    Restarted,
125    /// 配置更新
126    ConfigUpdated,
127    /// 健康检查失败
128    HealthCheckFailed,
129    /// 内存警告
130    MemoryWarning,
131    /// 错误
132    Error,
133    /// 请求处理
134    RequestProcessed,
135    /// 性能警告
136    PerformanceWarning,
137}
138
139/// 事件严重程度
140#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
141pub enum EventSeverity {
142    /// 信息
143    Info,
144    /// 警告
145    Warning,
146    /// 错误
147    Error,
148    /// 严重错误
149    Critical,
150}
151
152impl Default for RuntimeConfig {
153    fn default() -> Self {
154        Self {
155            max_context_length: Some(4096),
156            temperature: Some(0.7),
157            top_p: Some(0.9),
158            top_k: Some(50),
159            max_tokens: Some(2048),
160            stop_sequences: vec!["</s>".to_string(), "<|endoftext|>".to_string()],
161            batch_size: Some(1),
162            max_concurrent_requests: Some(10),
163            gpu_device_ids: vec![0],
164            memory_limit_mb: None,
165            enable_streaming: true,
166            custom_params: HashMap::new(),
167        }
168    }
169}
170
171impl ModelRuntime {
172    /// 创建新的运行时实例
173    pub fn new(model_id: Uuid, name: String, port: u16) -> Self {
174        Self {
175            id: Uuid::new_v4(),
176            model_id,
177            name,
178            port,
179            process_id: None,
180            config: RuntimeConfig::default(),
181            started_at: None,
182            stopped_at: None,
183            status: crate::ModelStatus::Stopped,
184            health_endpoint: format!("http://localhost:{}/health", port),
185            api_endpoint: format!("http://localhost:{}/v1", port),
186            log_file: None,
187            environment: HashMap::new(),
188        }
189    }
190
191    /// 标记为启动
192    pub fn mark_started(&mut self, process_id: u32) {
193        self.process_id = Some(process_id);
194        self.started_at = Some(Utc::now());
195        self.status = crate::ModelStatus::Running;
196        self.stopped_at = None;
197    }
198
199    /// 标记为停止
200    pub fn mark_stopped(&mut self) {
201        self.process_id = None;
202        self.stopped_at = Some(Utc::now());
203        self.status = crate::ModelStatus::Stopped;
204    }
205
206    /// 获取运行时长 (秒)
207    pub fn uptime_seconds(&self) -> Option<i64> {
208        self.started_at.map(|start| {
209            if self.status == crate::ModelStatus::Running {
210                Utc::now().timestamp() - start.timestamp()
211            } else {
212                self.stopped_at.unwrap_or(Utc::now()).timestamp() - start.timestamp()
213            }
214        })
215    }
216
217    /// 检查是否健康
218    pub fn is_healthy(&self) -> bool {
219        self.status == crate::ModelStatus::Running && self.process_id.is_some()
220    }
221}
222
223impl RuntimeEvent {
224    /// 创建新事件
225    pub fn new(
226        runtime_id: Uuid,
227        event_type: RuntimeEventType,
228        message: String,
229        severity: EventSeverity,
230    ) -> Self {
231        Self {
232            id: Uuid::new_v4(),
233            runtime_id,
234            event_type,
235            timestamp: Utc::now(),
236            message,
237            details: None,
238            severity,
239        }
240    }
241
242    /// 带详情创建事件
243    pub fn with_details(
244        runtime_id: Uuid,
245        event_type: RuntimeEventType,
246        message: String,
247        severity: EventSeverity,
248        details: serde_json::Value,
249    ) -> Self {
250        Self {
251            id: Uuid::new_v4(),
252            runtime_id,
253            event_type,
254            timestamp: Utc::now(),
255            message,
256            details: Some(details),
257            severity,
258        }
259    }
260}