oximedia-distributed 0.2.1

Distributed encoding coordinator for OxiMedia
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
//! Protocol buffer generated code stub
//! This is a minimal stub to allow the crate to compile
//! Full protobuf generation requires tonic build configuration

use async_trait::async_trait;
use std::collections::HashMap;
use tonic::server::NamedService;
use tonic::{Request, Response, Status};

// ============================================================
// Message Types
// ============================================================

// Worker capabilities
#[derive(Clone, PartialEq, Debug, Default)]
pub struct WorkerCapabilities {
    pub cpu_cores: u32,
    pub memory_bytes: u64,
    pub gpu_devices: Vec<String>,
    pub supported_codecs: Vec<String>,
    pub supported_hwaccels: Vec<String>,
    pub relative_speed: f32,
    pub max_concurrent_jobs: u32,
}

// Worker registration
#[derive(Clone, PartialEq, Debug, Default)]
pub struct WorkerRegistration {
    pub worker_id: String,
    pub hostname: String,
    pub ip_address: String,
    pub port: u32,
    pub capabilities: Option<Box<WorkerCapabilities>>,
    pub metadata: HashMap<String, String>,
}

#[derive(Clone, PartialEq, Debug, Default)]
pub struct WorkerRegistrationResponse {
    pub success: bool,
    pub message: String,
    pub assigned_worker_id: String,
}

// Heartbeat
#[derive(Clone, PartialEq, Debug, Default)]
pub struct WorkerHeartbeat {
    pub worker_id: String,
    pub status: Option<Box<WorkerStatus>>,
    pub active_job_ids: Vec<String>,
    pub metrics: Option<Box<WorkerMetrics>>,
}

#[derive(Clone, PartialEq, Debug, Default)]
pub struct HeartbeatResponse {
    pub acknowledged: bool,
    pub jobs_to_cancel: Vec<String>,
    pub should_drain: bool,
}

// Worker status
#[derive(Clone, PartialEq, Debug, Default)]
pub struct WorkerStatus {
    pub state: i32,
    pub active_jobs: u32,
    pub queued_jobs: u32,
}

// Worker metrics
#[derive(Clone, PartialEq, Debug, Default)]
pub struct WorkerMetrics {
    pub cpu_usage: f32,
    pub memory_usage: f32,
    pub gpu_usage: f32,
    pub bytes_processed: u64,
    pub frames_encoded: u32,
}

// Worker unregistration
#[derive(Clone, PartialEq, Debug, Default)]
pub struct WorkerUnregistration {
    pub worker_id: String,
    pub reason: String,
}

#[derive(Clone, PartialEq, Debug, Default)]
pub struct UnregistrationResponse {
    pub success: bool,
}

// Job request and assignment
#[derive(Clone, PartialEq, Debug, Default)]
pub struct JobRequest {
    pub worker_id: String,
    pub max_jobs: u32,
    pub preferred_codecs: Vec<String>,
}

pub mod job_assignment {
    use super::EncodingTask;

    #[derive(Clone, PartialEq, Debug, Default)]
    pub struct Job {
        pub job_id: String,
        pub task: Option<Box<EncodingTask>>,
        pub priority: u32,
        pub deadline_timestamp: i64,
    }
}

#[derive(Clone, PartialEq, Debug, Default)]
pub struct JobAssignment {
    pub jobs: Vec<job_assignment::Job>,
    pub has_more: bool,
}

#[derive(Clone, PartialEq, Debug, Default)]
pub struct Job {
    pub job_id: String,
    pub task: Option<Box<EncodingTask>>,
    pub priority: u32,
    pub deadline_timestamp: i64,
}

// Encoding task
#[derive(Clone, PartialEq, Debug, Default)]
pub struct EncodingTask {
    pub task_id: String,
    pub source_url: String,
    pub codec: String,
    pub strategy: i32,
    pub params: Option<Box<EncodingParams>>,
    pub output_url: String,
}

#[derive(Clone, PartialEq, Debug, Default)]
pub struct EncodingParams {
    pub bitrate: u32,
    pub width: u32,
    pub height: u32,
    pub preset: String,
    pub profile: String,
    pub crf: u32,
    pub extra_params: HashMap<String, String>,
}

// Time segment
#[derive(Clone, PartialEq, Debug, Default)]
pub struct TimeSegment {
    pub start_time: f64,
    pub end_time: f64,
    pub overlap: f64,
}

// Tile segment
#[derive(Clone, PartialEq, Debug, Default)]
pub struct TileSegment {
    pub tile_x: u32,
    pub tile_y: u32,
    pub tile_width: u32,
    pub tile_height: u32,
}

// GOP segment
#[derive(Clone, PartialEq, Debug, Default)]
pub struct GopSegment {
    pub start_frame: u64,
    pub end_frame: u64,
    pub keyframe_indices: Vec<u64>,
}

// Progress reporting
#[derive(Clone, PartialEq, Debug, Default)]
pub struct ProgressReport {
    pub job_id: String,
    pub worker_id: String,
    pub progress: f32,
    pub frames_encoded: u64,
    pub bytes_written: u64,
    pub encoding_speed: f32,
    pub estimated_completion_timestamp: i64,
}

#[derive(Clone, PartialEq, Debug, Default)]
pub struct ProgressAcknowledgment {
    pub acknowledged: bool,
}

// Result submission
#[derive(Clone, PartialEq, Debug, Default)]
pub struct JobResult {
    pub job_id: String,
    pub worker_id: String,
    pub output_url: String,
    pub output_size: u64,
    pub encoding_time: f64,
    pub metadata: Option<Box<ResultMetadata>>,
}

#[derive(Clone, PartialEq, Debug, Default)]
pub struct ResultMetadata {
    pub frames_encoded: u64,
    pub average_bitrate: f64,
    pub checksum: String,
    pub extra_metadata: HashMap<String, String>,
}

#[derive(Clone, PartialEq, Debug, Default)]
pub struct ResultAcknowledgment {
    pub acknowledged: bool,
    pub next_job_id: String,
}

// Failure reporting
#[derive(Clone, PartialEq, Debug, Default)]
pub struct JobFailure {
    pub job_id: String,
    pub worker_id: String,
    pub error_message: String,
    pub error_code: String,
    pub is_transient: bool,
}

#[derive(Clone, PartialEq, Debug, Default)]
pub struct FailureAcknowledgment {
    pub should_retry: bool,
    pub reassigned_job_id: String,
}

// Status queries with nested Query enum
pub mod worker_status_request {
    #[derive(Clone, PartialEq, Debug)]
    pub enum Query {
        WorkerId(String),
        AllWorkers(bool),
    }
}

#[derive(Clone, PartialEq, Debug, Default)]
pub struct WorkerStatusRequest {
    pub query: Option<worker_status_request::Query>,
}

#[derive(Clone, PartialEq, Debug, Default)]
pub struct WorkerStatusResponse {
    pub workers: Vec<WorkerInfo>,
}

#[derive(Clone, PartialEq, Debug, Default)]
pub struct WorkerInfo {
    pub worker_id: String,
    pub hostname: String,
    pub status: Option<Box<WorkerStatus>>,
    pub metrics: Option<Box<WorkerMetrics>>,
    pub last_heartbeat_timestamp: i64,
}

pub mod job_status_request {
    #[derive(Clone, PartialEq, Debug)]
    pub enum Query {
        JobId(String),
        TaskId(String),
    }
}

#[derive(Clone, PartialEq, Debug, Default)]
pub struct JobStatusRequest {
    pub query: Option<job_status_request::Query>,
}

#[derive(Clone, PartialEq, Debug, Default)]
pub struct JobStatusResponse {
    pub job_id: String,
    pub state: i32,
    pub assigned_worker_id: String,
    pub progress: f32,
    pub started_timestamp: i64,
    pub completed_timestamp: i64,
}

// Job cancellation with nested Target enum
pub mod job_cancellation {
    #[derive(Clone, PartialEq, Debug)]
    pub enum Target {
        JobId(String),
        TaskId(String),
    }
}

#[derive(Clone, PartialEq, Debug, Default)]
pub struct JobCancellation {
    pub target: Option<job_cancellation::Target>,
}

#[derive(Clone, PartialEq, Debug, Default)]
pub struct CancellationResponse {
    pub success: bool,
    pub cancelled_job_ids: Vec<String>,
}

// ============================================================
// Service Definitions
// ============================================================

pub mod coordinator_service_server {
    use super::{
        async_trait, CancellationResponse, FailureAcknowledgment, HeartbeatResponse, JobAssignment,
        JobCancellation, JobFailure, JobRequest, JobResult, JobStatusRequest, JobStatusResponse,
        NamedService, ProgressAcknowledgment, ProgressReport, Request, Response,
        ResultAcknowledgment, Status, UnregistrationResponse, WorkerHeartbeat, WorkerRegistration,
        WorkerRegistrationResponse, WorkerStatusRequest, WorkerStatusResponse,
        WorkerUnregistration,
    };

    #[async_trait]
    pub trait CoordinatorService: Send + Sync + 'static {
        async fn register_worker(
            &self,
            request: Request<WorkerRegistration>,
        ) -> Result<Response<WorkerRegistrationResponse>, Status>;
        async fn heartbeat(
            &self,
            request: Request<WorkerHeartbeat>,
        ) -> Result<Response<HeartbeatResponse>, Status>;
        async fn unregister_worker(
            &self,
            request: Request<WorkerUnregistration>,
        ) -> Result<Response<UnregistrationResponse>, Status>;
        async fn request_job(
            &self,
            request: Request<JobRequest>,
        ) -> Result<Response<JobAssignment>, Status>;
        async fn report_progress(
            &self,
            request: Request<ProgressReport>,
        ) -> Result<Response<ProgressAcknowledgment>, Status>;
        async fn submit_result(
            &self,
            request: Request<JobResult>,
        ) -> Result<Response<ResultAcknowledgment>, Status>;
        async fn report_failure(
            &self,
            request: Request<JobFailure>,
        ) -> Result<Response<FailureAcknowledgment>, Status>;
        async fn get_worker_status(
            &self,
            request: Request<WorkerStatusRequest>,
        ) -> Result<Response<WorkerStatusResponse>, Status>;
        async fn get_job_status(
            &self,
            request: Request<JobStatusRequest>,
        ) -> Result<Response<JobStatusResponse>, Status>;
        async fn cancel_job(
            &self,
            request: Request<JobCancellation>,
        ) -> Result<Response<CancellationResponse>, Status>;
    }

    #[derive(Clone)]
    #[allow(dead_code)]
    pub struct CoordinatorServiceServer<T> {
        inner: std::sync::Arc<T>,
    }

    impl<T: CoordinatorService> CoordinatorServiceServer<T> {
        pub fn new(inner: T) -> Self {
            Self {
                inner: std::sync::Arc::new(inner),
            }
        }

        #[must_use]
        pub fn inner_ref(&self) -> &T {
            &self.inner
        }
    }

    impl<T> NamedService for CoordinatorServiceServer<T> {
        const NAME: &'static str = "coordinator.CoordinatorService";
    }
}

pub mod coordinator_service_client {
    use super::{
        CancellationResponse, FailureAcknowledgment, HeartbeatResponse, JobAssignment,
        JobCancellation, JobFailure, JobRequest, JobResult, JobStatusRequest, JobStatusResponse,
        ProgressAcknowledgment, ProgressReport, Request, Response, ResultAcknowledgment, Status,
        UnregistrationResponse, WorkerHeartbeat, WorkerRegistration, WorkerRegistrationResponse,
        WorkerStatusRequest, WorkerStatusResponse, WorkerUnregistration,
    };
    use tonic::transport::Channel;

    #[derive(Clone)]
    pub struct CoordinatorServiceClient<T> {
        inner: T,
    }

    impl CoordinatorServiceClient<Channel> {
        pub async fn connect<D>(
            dst: D,
        ) -> std::result::Result<Self, Box<dyn std::error::Error + Send + Sync>>
        where
            D: std::convert::TryInto<tonic::transport::Endpoint> + Clone,
            D::Error: Into<Box<dyn std::error::Error + Send + Sync + 'static>>,
        {
            let endpoint: tonic::transport::Endpoint =
                dst.try_into().map_err(std::convert::Into::into)?;
            let channel = endpoint.connect().await?;
            Ok(Self::new(channel))
        }
    }

    impl<T> CoordinatorServiceClient<T> {
        pub fn new(inner: T) -> Self {
            Self { inner }
        }

        pub fn inner_ref(&self) -> &T {
            &self.inner
        }
    }

    impl CoordinatorServiceClient<Channel> {
        pub async fn register_worker(
            &mut self,
            _request: Request<WorkerRegistration>,
        ) -> Result<Response<WorkerRegistrationResponse>, Status> {
            Ok(Response::new(WorkerRegistrationResponse::default()))
        }

        pub async fn heartbeat(
            &mut self,
            _request: Request<WorkerHeartbeat>,
        ) -> Result<Response<HeartbeatResponse>, Status> {
            Ok(Response::new(HeartbeatResponse::default()))
        }

        pub async fn unregister_worker(
            &mut self,
            _request: Request<WorkerUnregistration>,
        ) -> Result<Response<UnregistrationResponse>, Status> {
            Ok(Response::new(UnregistrationResponse::default()))
        }

        pub async fn request_job(
            &mut self,
            _request: Request<JobRequest>,
        ) -> Result<Response<JobAssignment>, Status> {
            Ok(Response::new(JobAssignment::default()))
        }

        pub async fn report_progress(
            &mut self,
            _request: Request<ProgressReport>,
        ) -> Result<Response<ProgressAcknowledgment>, Status> {
            Ok(Response::new(ProgressAcknowledgment::default()))
        }

        pub async fn submit_result(
            &mut self,
            _request: Request<JobResult>,
        ) -> Result<Response<ResultAcknowledgment>, Status> {
            Ok(Response::new(ResultAcknowledgment::default()))
        }

        pub async fn report_failure(
            &mut self,
            _request: Request<JobFailure>,
        ) -> Result<Response<FailureAcknowledgment>, Status> {
            Ok(Response::new(FailureAcknowledgment::default()))
        }

        pub async fn get_worker_status(
            &mut self,
            _request: Request<WorkerStatusRequest>,
        ) -> Result<Response<WorkerStatusResponse>, Status> {
            Ok(Response::new(WorkerStatusResponse::default()))
        }

        pub async fn get_job_status(
            &mut self,
            _request: Request<JobStatusRequest>,
        ) -> Result<Response<JobStatusResponse>, Status> {
            Ok(Response::new(JobStatusResponse::default()))
        }

        pub async fn cancel_job(
            &mut self,
            _request: Request<JobCancellation>,
        ) -> Result<Response<CancellationResponse>, Status> {
            Ok(Response::new(CancellationResponse::default()))
        }
    }
}