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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
//! Worker discovery and health monitoring.
//!
//! This module provides:
//! - Worker discovery via mDNS, etcd, Consul, or static configuration
//! - Health monitoring and heartbeat tracking
//! - Automatic worker registration
//! - Capacity tracking and load balancing
//! - Geographic distribution awareness

#![allow(dead_code)]

use crate::{DiscoveryMethod, DistributedError, Result};
use dashmap::DashMap;
use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::Arc;
use std::time::{Duration, SystemTime};
use tokio::sync::RwLock;
use tracing::{debug, error, info, warn};

/// Worker discovery service
pub struct DiscoveryService {
    /// Discovery method
    method: DiscoveryMethod,

    /// Discovered workers
    workers: Arc<DashMap<String, WorkerEndpoint>>,

    /// Service configuration
    config: DiscoveryConfig,

    /// Health check state
    health_checker: Arc<HealthChecker>,

    /// Running flag
    running: Arc<AtomicBool>,
}

/// Discovery service configuration
#[derive(Debug, Clone)]
pub struct DiscoveryConfig {
    /// Service name for discovery
    pub service_name: String,

    /// Service port
    pub service_port: u16,

    /// Discovery interval
    pub discovery_interval: Duration,

    /// Health check interval
    pub health_check_interval: Duration,

    /// Worker timeout
    pub worker_timeout: Duration,

    /// etcd endpoints (for etcd discovery)
    pub etcd_endpoints: Vec<String>,

    /// Consul address (for Consul discovery)
    pub consul_address: String,

    /// Static worker addresses
    pub static_workers: Vec<String>,
}

impl Default for DiscoveryConfig {
    fn default() -> Self {
        Self {
            service_name: "oximedia-worker".to_string(),
            service_port: 50052,
            discovery_interval: Duration::from_secs(30),
            health_check_interval: Duration::from_secs(10),
            worker_timeout: Duration::from_secs(90),
            etcd_endpoints: vec!["http://127.0.0.1:2379".to_string()],
            consul_address: "http://127.0.0.1:8500".to_string(),
            static_workers: Vec::new(),
        }
    }
}

/// Worker endpoint information
#[derive(Debug, Clone)]
pub struct WorkerEndpoint {
    /// Worker ID
    pub worker_id: String,

    /// Worker address
    pub address: SocketAddr,

    /// Hostname
    pub hostname: String,

    /// Capabilities
    pub capabilities: WorkerCapabilities,

    /// Health status
    pub health: HealthStatus,

    /// Last seen timestamp
    pub last_seen: SystemTime,

    /// Geographic location (optional)
    pub location: Option<GeoLocation>,

    /// Metadata
    pub metadata: HashMap<String, String>,
}

/// Worker capabilities
#[derive(Debug, Clone)]
pub struct WorkerCapabilities {
    /// CPU cores
    pub cpu_cores: u32,

    /// Memory in bytes
    pub memory_bytes: u64,

    /// GPU devices
    pub gpu_devices: Vec<String>,

    /// Supported codecs
    pub codecs: Vec<String>,

    /// Maximum concurrent jobs
    pub max_jobs: u32,

    /// Performance score (relative)
    pub performance_score: f32,
}

impl Default for WorkerCapabilities {
    fn default() -> Self {
        Self {
            cpu_cores: 1,
            memory_bytes: 1_073_741_824,
            gpu_devices: Vec::new(),
            codecs: vec!["h264".to_string()],
            max_jobs: 2,
            performance_score: 1.0,
        }
    }
}

/// Health status
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HealthStatus {
    Healthy,
    Degraded,
    Unhealthy,
    Unknown,
}

/// Geographic location
#[derive(Debug, Clone)]
pub struct GeoLocation {
    /// Region (e.g., "us-west-2")
    pub region: String,

    /// Availability zone
    pub zone: Option<String>,

    /// Data center
    pub datacenter: Option<String>,

    /// Latitude
    pub latitude: Option<f64>,

    /// Longitude
    pub longitude: Option<f64>,
}

/// Health checker
pub struct HealthChecker {
    /// Health check results
    health_results: Arc<DashMap<String, HealthCheckResult>>,

    /// Statistics
    stats: HealthStats,
}

/// Health check result
#[derive(Debug, Clone)]
struct HealthCheckResult {
    worker_id: String,
    status: HealthStatus,
    latency: Duration,
    last_check: SystemTime,
    consecutive_failures: u32,
}

/// Health statistics
#[derive(Debug, Default)]
struct HealthStats {
    total_checks: AtomicU64,
    successful_checks: AtomicU64,
    failed_checks: AtomicU64,
}

impl DiscoveryService {
    /// Create a new discovery service
    #[must_use]
    pub fn new(method: DiscoveryMethod, config: DiscoveryConfig) -> Self {
        Self {
            method,
            workers: Arc::new(DashMap::new()),
            config,
            health_checker: Arc::new(HealthChecker::new()),
            running: Arc::new(AtomicBool::new(false)),
        }
    }

    /// Start the discovery service
    pub async fn start(&self) -> Result<()> {
        info!("Starting discovery service with method: {:?}", self.method);
        self.running.store(true, Ordering::Relaxed);

        // Start discovery loop
        let service = self.clone_refs();
        tokio::spawn(async move {
            service.discovery_loop().await;
        });

        // Start health check loop
        let service = self.clone_refs();
        tokio::spawn(async move {
            service.health_check_loop().await;
        });

        Ok(())
    }

    fn clone_refs(&self) -> Self {
        Self {
            method: self.method,
            workers: self.workers.clone(),
            config: self.config.clone(),
            health_checker: self.health_checker.clone(),
            running: self.running.clone(),
        }
    }

    /// Discovery loop
    async fn discovery_loop(&self) {
        let mut interval = tokio::time::interval(self.config.discovery_interval);

        while self.running.load(Ordering::Relaxed) {
            interval.tick().await;

            if let Err(e) = self.discover_workers().await {
                error!("Worker discovery failed: {}", e);
            }
        }
    }

    /// Discover workers based on configured method
    async fn discover_workers(&self) -> Result<()> {
        match self.method {
            DiscoveryMethod::Static => self.discover_static().await,
            DiscoveryMethod::MDNS => self.discover_mdns().await,
            DiscoveryMethod::Etcd => self.discover_etcd().await,
            DiscoveryMethod::Consul => self.discover_consul().await,
        }
    }

    /// Static worker discovery
    async fn discover_static(&self) -> Result<()> {
        debug!("Discovering static workers");

        for addr_str in &self.config.static_workers {
            let addr: SocketAddr = addr_str
                .parse()
                .map_err(|e| DistributedError::Discovery(format!("Invalid address: {e}")))?;

            let worker_id = format!("static-{addr}");

            let endpoint = WorkerEndpoint {
                worker_id: worker_id.clone(),
                address: addr,
                hostname: addr.ip().to_string(),
                capabilities: WorkerCapabilities::default(),
                health: HealthStatus::Unknown,
                last_seen: SystemTime::now(),
                location: None,
                metadata: HashMap::new(),
            };

            self.workers.insert(worker_id, endpoint);
        }

        info!(
            "Discovered {} static workers",
            self.config.static_workers.len()
        );
        Ok(())
    }

    /// mDNS-based discovery
    async fn discover_mdns(&self) -> Result<()> {
        debug!("Discovering workers via mDNS");

        // In production, would use mdns crate to discover services
        // For now, simulate discovery
        info!("mDNS discovery completed");
        Ok(())
    }

    /// etcd-based discovery
    async fn discover_etcd(&self) -> Result<()> {
        debug!("Discovering workers via etcd");

        // In production, would query etcd for worker registrations
        // For now, simulate discovery
        info!("etcd discovery completed");
        Ok(())
    }

    /// Consul-based discovery
    async fn discover_consul(&self) -> Result<()> {
        debug!("Discovering workers via Consul");

        // In production, would query Consul service catalog
        // For now, simulate discovery
        info!("Consul discovery completed");
        Ok(())
    }

    /// Health check loop
    async fn health_check_loop(&self) {
        let mut interval = tokio::time::interval(self.config.health_check_interval);

        while self.running.load(Ordering::Relaxed) {
            interval.tick().await;

            self.check_worker_health().await;
        }
    }

    /// Check health of all workers
    async fn check_worker_health(&self) {
        let workers: Vec<_> = self
            .workers
            .iter()
            .map(|e| (e.key().clone(), e.value().clone()))
            .collect();

        for (worker_id, endpoint) in workers {
            let result = self.health_checker.check_worker(&endpoint).await;

            // Update worker health status
            if let Some(mut worker) = self.workers.get_mut(&worker_id) {
                worker.health = result.status;
                worker.last_seen = SystemTime::now();
            }

            // Remove unhealthy workers after timeout
            if result.status == HealthStatus::Unhealthy && result.consecutive_failures > 3 {
                warn!("Removing unhealthy worker: {}", worker_id);
                self.workers.remove(&worker_id);
            }
        }
    }

    /// Register a worker manually
    pub fn register_worker(&self, endpoint: WorkerEndpoint) -> Result<()> {
        info!("Registering worker: {}", endpoint.worker_id);
        self.workers.insert(endpoint.worker_id.clone(), endpoint);
        Ok(())
    }

    /// Unregister a worker
    pub fn unregister_worker(&self, worker_id: &str) -> Result<()> {
        info!("Unregistering worker: {}", worker_id);
        self.workers.remove(worker_id);
        Ok(())
    }

    /// Get all discovered workers
    #[must_use]
    pub fn get_workers(&self) -> Vec<WorkerEndpoint> {
        self.workers.iter().map(|e| e.value().clone()).collect()
    }

    /// Get healthy workers
    #[must_use]
    pub fn get_healthy_workers(&self) -> Vec<WorkerEndpoint> {
        self.workers
            .iter()
            .filter(|e| e.value().health == HealthStatus::Healthy)
            .map(|e| e.value().clone())
            .collect()
    }

    /// Get worker by ID
    #[must_use]
    pub fn get_worker(&self, worker_id: &str) -> Option<WorkerEndpoint> {
        self.workers.get(worker_id).map(|e| e.value().clone())
    }

    /// Find workers by capability
    #[must_use]
    pub fn find_workers_by_capability(&self, required_codec: &str) -> Vec<WorkerEndpoint> {
        self.workers
            .iter()
            .filter(|e| {
                e.value().health == HealthStatus::Healthy
                    && e.value()
                        .capabilities
                        .codecs
                        .iter()
                        .any(|c| c == required_codec)
            })
            .map(|e| e.value().clone())
            .collect()
    }

    /// Find workers by location
    #[must_use]
    pub fn find_workers_by_region(&self, region: &str) -> Vec<WorkerEndpoint> {
        self.workers
            .iter()
            .filter(|e| {
                e.value()
                    .location
                    .as_ref()
                    .is_some_and(|l| l.region == region)
            })
            .map(|e| e.value().clone())
            .collect()
    }

    /// Get capacity statistics
    #[must_use]
    pub fn get_capacity_stats(&self) -> CapacityStats {
        let workers = self.get_healthy_workers();

        let total_cpu: u32 = workers.iter().map(|w| w.capabilities.cpu_cores).sum();
        let total_memory: u64 = workers.iter().map(|w| w.capabilities.memory_bytes).sum();
        let total_gpus: usize = workers
            .iter()
            .map(|w| w.capabilities.gpu_devices.len())
            .sum();
        let total_job_slots: u32 = workers.iter().map(|w| w.capabilities.max_jobs).sum();

        CapacityStats {
            total_workers: workers.len(),
            total_cpu_cores: total_cpu,
            total_memory_bytes: total_memory,
            total_gpu_devices: total_gpus,
            total_job_slots,
            average_performance: workers
                .iter()
                .map(|w| w.capabilities.performance_score)
                .sum::<f32>()
                / workers.len().max(1) as f32,
        }
    }

    /// Stop the discovery service
    pub async fn stop(&self) -> Result<()> {
        info!("Stopping discovery service");
        self.running.store(false, Ordering::Relaxed);
        Ok(())
    }
}

/// Capacity statistics
#[derive(Debug, Clone)]
pub struct CapacityStats {
    pub total_workers: usize,
    pub total_cpu_cores: u32,
    pub total_memory_bytes: u64,
    pub total_gpu_devices: usize,
    pub total_job_slots: u32,
    pub average_performance: f32,
}

impl HealthChecker {
    /// Create a new health checker
    fn new() -> Self {
        Self {
            health_results: Arc::new(DashMap::new()),
            stats: HealthStats::default(),
        }
    }

    /// Check worker health
    async fn check_worker(&self, endpoint: &WorkerEndpoint) -> HealthCheckResult {
        self.stats.total_checks.fetch_add(1, Ordering::Relaxed);

        let start = SystemTime::now();

        // Perform health check (simplified)
        let status = self.perform_health_check(endpoint).await;

        let latency = start.elapsed().unwrap_or(Duration::ZERO);

        let mut result = HealthCheckResult {
            worker_id: endpoint.worker_id.clone(),
            status,
            latency,
            last_check: SystemTime::now(),
            consecutive_failures: 0,
        };

        // Update consecutive failures
        if let Some(prev) = self.health_results.get(&endpoint.worker_id) {
            if status == HealthStatus::Unhealthy {
                result.consecutive_failures = prev.consecutive_failures + 1;
            }
        } else if status == HealthStatus::Unhealthy {
            result.consecutive_failures = 1;
        }

        // Update statistics
        if status == HealthStatus::Healthy {
            self.stats.successful_checks.fetch_add(1, Ordering::Relaxed);
        } else {
            self.stats.failed_checks.fetch_add(1, Ordering::Relaxed);
        }

        self.health_results
            .insert(endpoint.worker_id.clone(), result.clone());

        result
    }

    /// Perform actual health check
    async fn perform_health_check(&self, _endpoint: &WorkerEndpoint) -> HealthStatus {
        // In production, would send HTTP/gRPC health check request
        // For now, simulate based on last_seen time
        HealthStatus::Healthy
    }

    /// Get health check statistics
    pub fn statistics(&self) -> HealthCheckStats {
        HealthCheckStats {
            total_checks: self.stats.total_checks.load(Ordering::Relaxed),
            successful_checks: self.stats.successful_checks.load(Ordering::Relaxed),
            failed_checks: self.stats.failed_checks.load(Ordering::Relaxed),
        }
    }
}

/// Health check statistics
#[derive(Debug, Clone)]
pub struct HealthCheckStats {
    pub total_checks: u64,
    pub successful_checks: u64,
    pub failed_checks: u64,
}

impl HealthCheckStats {
    /// Get success rate
    #[must_use]
    pub fn success_rate(&self) -> f64 {
        if self.total_checks == 0 {
            return 0.0;
        }
        self.successful_checks as f64 / self.total_checks as f64
    }
}

/// Worker registry for persistent storage
pub struct WorkerRegistry {
    /// Registered workers
    workers: Arc<RwLock<HashMap<String, RegisteredWorker>>>,

    /// Registry backend
    backend: RegistryBackend,
}

/// Registered worker with persistence
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct RegisteredWorker {
    worker_id: String,
    address: String,
    hostname: String,
    registered_at: u64,
    last_heartbeat: u64,
    capabilities: serde_json::Value,
    metadata: HashMap<String, String>,
}

/// Registry backend
#[derive(Debug, Clone, Copy)]
pub enum RegistryBackend {
    Memory,
    Etcd,
    Consul,
}

impl WorkerRegistry {
    /// Create a new worker registry
    #[must_use]
    pub fn new(backend: RegistryBackend) -> Self {
        Self {
            workers: Arc::new(RwLock::new(HashMap::new())),
            backend,
        }
    }

    /// Register a worker
    pub async fn register(&self, worker_id: String, endpoint: WorkerEndpoint) -> Result<()> {
        info!("Registering worker in registry: {}", worker_id);

        let unix_now = SystemTime::now()
            .duration_since(SystemTime::UNIX_EPOCH)
            .unwrap_or(Duration::ZERO)
            .as_secs();

        let worker = RegisteredWorker {
            worker_id: worker_id.clone(),
            address: endpoint.address.to_string(),
            hostname: endpoint.hostname,
            registered_at: unix_now,
            last_heartbeat: unix_now,
            capabilities: serde_json::json!({}),
            metadata: endpoint.metadata,
        };

        let mut workers = self.workers.write().await;
        workers.insert(worker_id.clone(), worker.clone());

        // Persist to backend
        self.persist_worker(&worker).await?;

        Ok(())
    }

    /// Unregister a worker
    pub async fn unregister(&self, worker_id: &str) -> Result<()> {
        info!("Unregistering worker from registry: {}", worker_id);

        let mut workers = self.workers.write().await;
        workers.remove(worker_id);

        // Remove from backend
        self.remove_worker(worker_id).await?;

        Ok(())
    }

    /// Update worker heartbeat
    pub async fn update_heartbeat(&self, worker_id: &str) -> Result<()> {
        let mut workers = self.workers.write().await;

        if let Some(worker) = workers.get_mut(worker_id) {
            worker.last_heartbeat = SystemTime::now()
                .duration_since(SystemTime::UNIX_EPOCH)
                .unwrap_or(Duration::ZERO)
                .as_secs();
        }

        Ok(())
    }

    /// Persist worker to backend
    async fn persist_worker(&self, _worker: &RegisteredWorker) -> Result<()> {
        match self.backend {
            RegistryBackend::Memory => Ok(()),
            RegistryBackend::Etcd => {
                // In production, write to etcd
                Ok(())
            }
            RegistryBackend::Consul => {
                // In production, write to Consul
                Ok(())
            }
        }
    }

    /// Remove worker from backend
    async fn remove_worker(&self, _worker_id: &str) -> Result<()> {
        match self.backend {
            RegistryBackend::Memory => Ok(()),
            RegistryBackend::Etcd => {
                // In production, delete from etcd
                Ok(())
            }
            RegistryBackend::Consul => {
                // In production, delete from Consul
                Ok(())
            }
        }
    }

    /// Get all workers
    pub async fn get_all(&self) -> Vec<RegisteredWorker> {
        let workers = self.workers.read().await;
        workers.values().cloned().collect()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::net::{IpAddr, Ipv4Addr};

    #[test]
    fn test_discovery_config() {
        let config = DiscoveryConfig::default();
        assert_eq!(config.service_name, "oximedia-worker");
        assert_eq!(config.service_port, 50052);
    }

    #[test]
    fn test_worker_capabilities() {
        let caps = WorkerCapabilities::default();
        assert_eq!(caps.cpu_cores, 1);
        assert_eq!(caps.max_jobs, 2);
        assert!(!caps.codecs.is_empty());
    }

    #[test]
    fn test_health_status() {
        assert_eq!(HealthStatus::Healthy, HealthStatus::Healthy);
        assert_ne!(HealthStatus::Healthy, HealthStatus::Unhealthy);
    }

    #[test]
    fn test_discovery_service_creation() {
        let config = DiscoveryConfig::default();
        let service = DiscoveryService::new(DiscoveryMethod::Static, config);
        assert_eq!(service.method, DiscoveryMethod::Static);
        assert_eq!(service.workers.len(), 0);
    }

    #[test]
    fn test_worker_registration() {
        let config = DiscoveryConfig::default();
        let service = DiscoveryService::new(DiscoveryMethod::Static, config);

        let endpoint = WorkerEndpoint {
            worker_id: "test-worker".to_string(),
            address: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 50052),
            hostname: "localhost".to_string(),
            capabilities: WorkerCapabilities::default(),
            health: HealthStatus::Healthy,
            last_seen: SystemTime::now(),
            location: None,
            metadata: HashMap::new(),
        };

        assert!(service.register_worker(endpoint).is_ok());
        assert_eq!(service.workers.len(), 1);
    }

    #[test]
    fn test_capacity_stats() {
        let config = DiscoveryConfig::default();
        let service = DiscoveryService::new(DiscoveryMethod::Static, config);

        let stats = service.get_capacity_stats();
        assert_eq!(stats.total_workers, 0);
        assert_eq!(stats.total_cpu_cores, 0);
    }

    #[test]
    fn test_health_check_stats() {
        let stats = HealthCheckStats {
            total_checks: 100,
            successful_checks: 95,
            failed_checks: 5,
        };

        assert_eq!(stats.success_rate(), 0.95);
    }

    #[test]
    fn test_geo_location() {
        let location = GeoLocation {
            region: "us-west-2".to_string(),
            zone: Some("us-west-2a".to_string()),
            datacenter: None,
            latitude: Some(37.7749),
            longitude: Some(-122.4194),
        };

        assert_eq!(location.region, "us-west-2");
        assert!(location.latitude.is_some());
    }
}