Skip to main content

llm_test_bench_core/distributed/
protocol.rs

1// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
4// option. This file may not be copied, modified, or distributed
5// except according to those terms.
6
7//! Protocol definitions for coordinator-worker communication.
8
9use chrono::{DateTime, Utc};
10use serde::{Deserialize, Serialize};
11use std::collections::HashMap;
12
13use crate::distributed::types::{JobId, NodeId, TaskId, WorkerCapabilities};
14
15/// Register worker request
16#[derive(Debug, Clone, Serialize, Deserialize)]
17pub struct RegisterRequest {
18    /// Worker ID
19    pub worker_id: NodeId,
20    /// Worker address
21    pub address: String,
22    /// Worker capacity
23    pub capacity: usize,
24    /// Worker capabilities
25    pub capabilities: WorkerCapabilities,
26    /// Worker tags
27    pub tags: Vec<String>,
28    /// Worker metadata
29    pub metadata: HashMap<String, String>,
30}
31
32/// Register worker response
33#[derive(Debug, Clone, Serialize, Deserialize)]
34pub struct RegisterResponse {
35    /// Registration success
36    pub success: bool,
37    /// Coordinator version
38    pub coordinator_version: String,
39    /// Assigned worker ID (may differ from requested)
40    pub assigned_worker_id: NodeId,
41    /// Heartbeat interval (seconds)
42    pub heartbeat_interval: u64,
43    /// Message
44    pub message: String,
45}
46
47/// Deregister worker request
48#[derive(Debug, Clone, Serialize, Deserialize)]
49pub struct DeregisterRequest {
50    /// Worker ID
51    pub worker_id: NodeId,
52    /// Reason for deregistration
53    pub reason: String,
54}
55
56/// Deregister worker response
57#[derive(Debug, Clone, Serialize, Deserialize)]
58pub struct DeregisterResponse {
59    /// Deregistration success
60    pub success: bool,
61    /// Message
62    pub message: String,
63}
64
65/// Health check request
66#[derive(Debug, Clone, Serialize, Deserialize)]
67pub struct HealthCheckRequest {
68    /// Worker ID
69    pub worker_id: NodeId,
70    /// Timestamp
71    pub timestamp: DateTime<Utc>,
72}
73
74/// Health check response
75#[derive(Debug, Clone, Serialize, Deserialize)]
76pub struct HealthCheckResponse {
77    /// Health status
78    pub healthy: bool,
79    /// Worker status
80    pub status: String,
81    /// Current tasks
82    pub current_tasks: usize,
83    /// Available capacity
84    pub available_capacity: usize,
85    /// CPU usage (0.0-1.0)
86    pub cpu_usage: f64,
87    /// Memory usage (0.0-1.0)
88    pub memory_usage: f64,
89    /// Timestamp
90    pub timestamp: DateTime<Utc>,
91}
92
93/// Heartbeat request
94#[derive(Debug, Clone, Serialize, Deserialize)]
95pub struct HeartbeatRequest {
96    /// Worker ID
97    pub worker_id: NodeId,
98    /// Worker status
99    pub status: String,
100    /// Current tasks
101    pub current_tasks: usize,
102    /// Completed tasks since last heartbeat
103    pub completed_tasks_delta: u64,
104    /// Failed tasks since last heartbeat
105    pub failed_tasks_delta: u64,
106    /// Timestamp
107    pub timestamp: DateTime<Utc>,
108}
109
110/// Heartbeat response
111#[derive(Debug, Clone, Serialize, Deserialize)]
112pub struct HeartbeatResponse {
113    /// Acknowledgment
114    pub acknowledged: bool,
115    /// Coordinator has pending tasks for this worker
116    pub has_pending_tasks: bool,
117    /// Timestamp
118    pub timestamp: DateTime<Utc>,
119}
120
121/// Job submission request
122#[derive(Debug, Clone, Serialize, Deserialize)]
123pub struct JobRequest {
124    /// Job type
125    pub job_type: String,
126    /// Job payload
127    pub payload: serde_json::Value,
128    /// Job priority (higher = more important)
129    pub priority: i32,
130    /// Required worker tags
131    pub required_tags: Vec<String>,
132    /// Job metadata
133    pub metadata: HashMap<String, String>,
134    /// Job timeout (seconds)
135    pub timeout_seconds: u64,
136    /// Max retries
137    pub max_retries: u32,
138}
139
140impl JobRequest {
141    /// Create a new job request builder
142    pub fn builder() -> JobRequestBuilder {
143        JobRequestBuilder::default()
144    }
145}
146
147/// Job request builder
148#[derive(Default)]
149pub struct JobRequestBuilder {
150    job_type: Option<String>,
151    payload: Option<serde_json::Value>,
152    priority: i32,
153    required_tags: Vec<String>,
154    metadata: HashMap<String, String>,
155    timeout_seconds: u64,
156    max_retries: u32,
157}
158
159impl JobRequestBuilder {
160    pub fn job_type(mut self, job_type: impl Into<String>) -> Self {
161        self.job_type = Some(job_type.into());
162        self
163    }
164
165    pub fn payload(mut self, payload: serde_json::Value) -> Self {
166        self.payload = Some(payload);
167        self
168    }
169
170    pub fn priority(mut self, priority: i32) -> Self {
171        self.priority = priority;
172        self
173    }
174
175    pub fn required_tags(mut self, tags: Vec<String>) -> Self {
176        self.required_tags = tags;
177        self
178    }
179
180    pub fn metadata(mut self, metadata: HashMap<String, String>) -> Self {
181        self.metadata = metadata;
182        self
183    }
184
185    pub fn timeout_seconds(mut self, seconds: u64) -> Self {
186        self.timeout_seconds = seconds;
187        self
188    }
189
190    pub fn max_retries(mut self, retries: u32) -> Self {
191        self.max_retries = retries;
192        self
193    }
194
195    pub fn build(self) -> JobRequest {
196        JobRequest {
197            job_type: self.job_type.unwrap_or_else(|| "default".to_string()),
198            payload: self.payload.unwrap_or(serde_json::json!({})),
199            priority: self.priority,
200            required_tags: self.required_tags,
201            metadata: self.metadata,
202            timeout_seconds: self.timeout_seconds,
203            max_retries: self.max_retries,
204        }
205    }
206}
207
208/// Job submission response
209#[derive(Debug, Clone, Serialize, Deserialize)]
210pub struct JobResponse {
211    /// Job ID
212    pub job_id: JobId,
213    /// Submission success
214    pub success: bool,
215    /// Message
216    pub message: String,
217    /// Estimated completion time
218    pub estimated_completion: Option<DateTime<Utc>>,
219}
220
221/// Task assignment request
222#[derive(Debug, Clone, Serialize, Deserialize)]
223pub struct TaskRequest {
224    /// Task ID
225    pub task_id: TaskId,
226    /// Job ID
227    pub job_id: JobId,
228    /// Task type
229    pub task_type: String,
230    /// Task payload
231    pub payload: serde_json::Value,
232    /// Task metadata
233    pub metadata: HashMap<String, String>,
234    /// Timeout (seconds)
235    pub timeout_seconds: u64,
236    /// Retry count
237    pub retry_count: u32,
238}
239
240/// Task response
241#[derive(Debug, Clone, Serialize, Deserialize)]
242pub struct TaskResponse {
243    /// Task ID
244    pub task_id: TaskId,
245    /// Success flag
246    pub success: bool,
247    /// Result data
248    pub result: Option<serde_json::Value>,
249    /// Error message
250    pub error: Option<String>,
251    /// Execution time (milliseconds)
252    pub execution_time_ms: u64,
253    /// Completed at
254    pub completed_at: DateTime<Utc>,
255}
256
257/// Request task assignment from coordinator
258#[derive(Debug, Clone, Serialize, Deserialize)]
259pub struct PullTaskRequest {
260    /// Worker ID
261    pub worker_id: NodeId,
262    /// Number of tasks requested
263    pub count: usize,
264    /// Worker capabilities
265    pub capabilities: Vec<String>,
266}
267
268/// Task assignment response
269#[derive(Debug, Clone, Serialize, Deserialize)]
270pub struct PullTaskResponse {
271    /// Assigned tasks
272    pub tasks: Vec<TaskRequest>,
273    /// Message
274    pub message: String,
275}
276
277/// Job status query request
278#[derive(Debug, Clone, Serialize, Deserialize)]
279pub struct JobStatusRequest {
280    /// Job ID
281    pub job_id: JobId,
282}
283
284/// Job status response
285#[derive(Debug, Clone, Serialize, Deserialize)]
286pub struct JobStatusResponse {
287    /// Job ID
288    pub job_id: JobId,
289    /// Job status
290    pub status: String,
291    /// Progress (0.0-1.0)
292    pub progress: f64,
293    /// Result (if completed)
294    pub result: Option<serde_json::Value>,
295    /// Error (if failed)
296    pub error: Option<String>,
297    /// Created at
298    pub created_at: DateTime<Utc>,
299    /// Started at
300    pub started_at: Option<DateTime<Utc>>,
301    /// Completed at
302    pub completed_at: Option<DateTime<Utc>>,
303}
304
305/// Cancel job request
306#[derive(Debug, Clone, Serialize, Deserialize)]
307pub struct CancelJobRequest {
308    /// Job ID
309    pub job_id: JobId,
310    /// Reason for cancellation
311    pub reason: String,
312}
313
314/// Cancel job response
315#[derive(Debug, Clone, Serialize, Deserialize)]
316pub struct CancelJobResponse {
317    /// Cancellation success
318    pub success: bool,
319    /// Message
320    pub message: String,
321}
322
323/// List workers request
324#[derive(Debug, Clone, Serialize, Deserialize)]
325pub struct ListWorkersRequest {
326    /// Filter by status
327    pub status_filter: Option<String>,
328    /// Filter by tags
329    pub tag_filter: Vec<String>,
330}
331
332/// List workers response
333#[derive(Debug, Clone, Serialize, Deserialize)]
334pub struct ListWorkersResponse {
335    /// Workers
336    pub workers: Vec<WorkerSummary>,
337    /// Total count
338    pub total: usize,
339}
340
341/// Worker summary
342#[derive(Debug, Clone, Serialize, Deserialize)]
343pub struct WorkerSummary {
344    /// Worker ID
345    pub worker_id: NodeId,
346    /// Worker address
347    pub address: String,
348    /// Worker status
349    pub status: String,
350    /// Current tasks
351    pub current_tasks: usize,
352    /// Capacity
353    pub capacity: usize,
354    /// Load (0.0-1.0)
355    pub load: f64,
356    /// Tags
357    pub tags: Vec<String>,
358    /// Last heartbeat
359    pub last_heartbeat: DateTime<Utc>,
360}
361
362/// Cluster statistics request
363#[derive(Debug, Clone, Serialize, Deserialize)]
364pub struct ClusterStatsRequest {}
365
366/// Cluster statistics response
367#[derive(Debug, Clone, Serialize, Deserialize)]
368pub struct ClusterStatsResponse {
369    /// Total workers
370    pub total_workers: usize,
371    /// Active workers
372    pub active_workers: usize,
373    /// Total jobs
374    pub total_jobs: u64,
375    /// Pending jobs
376    pub pending_jobs: usize,
377    /// Running jobs
378    pub running_jobs: usize,
379    /// Completed jobs
380    pub completed_jobs: u64,
381    /// Failed jobs
382    pub failed_jobs: u64,
383    /// Average job duration (seconds)
384    pub avg_job_duration: f64,
385    /// Cluster uptime (seconds)
386    pub uptime_seconds: u64,
387}
388
389#[cfg(test)]
390mod tests {
391    use super::*;
392
393    #[test]
394    fn test_job_request_builder() {
395        let job = JobRequest::builder()
396            .job_type("benchmark")
397            .payload(serde_json::json!({"model": "gpt-4"}))
398            .priority(10)
399            .timeout_seconds(300)
400            .max_retries(3)
401            .build();
402
403        assert_eq!(job.job_type, "benchmark");
404        assert_eq!(job.priority, 10);
405        assert_eq!(job.timeout_seconds, 300);
406        assert_eq!(job.max_retries, 3);
407    }
408
409    #[test]
410    fn test_heartbeat_request() {
411        let heartbeat = HeartbeatRequest {
412            worker_id: "worker-1".to_string(),
413            status: "idle".to_string(),
414            current_tasks: 0,
415            completed_tasks_delta: 5,
416            failed_tasks_delta: 0,
417            timestamp: Utc::now(),
418        };
419
420        assert_eq!(heartbeat.worker_id, "worker-1");
421        assert_eq!(heartbeat.current_tasks, 0);
422    }
423}