subx_cli/core/parallel/mod.rs
1//! High-performance parallel processing system for subtitle operations.
2//!
3//! This module provides a sophisticated task scheduling and execution framework
4//! designed specifically for subtitle processing workloads. It offers intelligent
5//! load balancing, resource management, and fault tolerance for CPU-intensive
6//! operations like format conversion, AI analysis, and audio synchronization.
7//!
8//! # Core Features
9//!
10//! ## Intelligent Task Scheduling
11//! - **Priority-Based Queuing**: Tasks are prioritized based on complexity and user preferences
12//! - **Resource-Aware Scheduling**: Considers CPU, memory, and I/O constraints
13//! - **Adaptive Load Balancing**: Dynamically adjusts worker allocation based on system load
14//! - **Dependency Management**: Handles task dependencies and execution ordering
15//!
16//! ## Worker Pool Management
17//! - **Dynamic Scaling**: Automatically adjusts worker count based on system resources
18//! - **Specialized Workers**: Different worker types for different operation categories
19//! - **Health Monitoring**: Tracks worker performance and handles failures gracefully
20//! - **Resource Isolation**: Prevents resource contention between concurrent tasks
21//!
22//! ## Performance Optimization
23//! - **Batch Processing**: Groups similar tasks for efficient execution
24//! - **Memory Pool**: Reuses memory allocations to reduce garbage collection
25//! - **Cache-Aware Scheduling**: Optimizes for CPU cache locality
26//! - **NUMA Awareness**: Considers system topology for optimal performance
27//!
28//! # Architecture Overview
29//!
30//! The parallel processing system follows a producer-consumer pattern with
31//! multiple specialized components:
32//!
33//! ```text
34//! ┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐
35//! │ Task Queue │────│ Scheduler │────│ Load Balancer │
36//! │ - Priority │ │ - Dispatch │ │ - Resource │
37//! │ - Dependencies│ │ - Monitoring │ │ - Scaling │
38//! │ - Batching │ │ - Recovery │ │ - Affinity │
39//! └─────────────────┘ └──────────────────┘ └─────────────────┘
40//! │ │ │
41//! └────────────────────────┼────────────────────────┘
42//! │
43//! ┌─────────────────────────┐
44//! │ Worker Pool │
45//! │ ┌───────────────────┐ │
46//! │ │ Format Converter │ │
47//! │ │ AI Analyzer │ │
48//! │ │ Audio Processor │ │
49//! │ │ File Manager │ │
50//! │ └───────────────────┘ │
51//! └─────────────────────────┘
52//! ```
53//!
54//! # Usage Examples
55//!
56//! ## Basic Task Execution
57//!
58//! ```rust,ignore
59//! use subx_cli::core::parallel::{TaskScheduler, Task, ProcessingOperation};
60//! use std::path::PathBuf;
61//!
62//! // Create a task scheduler with default configuration
63//! let scheduler = TaskScheduler::new().await?;
64//!
65//! // Define a format conversion task
66//! let task = Task::new(
67//! ProcessingOperation::FormatConversion {
68//! input_path: PathBuf::from("input.srt"),
69//! output_path: PathBuf::from("output.ass"),
70//! target_format: "ass".to_string(),
71//! },
72//! 1, // Normal priority
73//! );
74//!
75//! // Submit task and wait for completion
76//! let result = scheduler.submit_and_wait(task).await?;
77//! println!("Task completed: {:?}", result);
78//! ```
79//!
80//! ## Batch Processing
81//!
82//! ```rust,ignore
83//! use subx_cli::core::parallel::{FileProcessingTask, TaskScheduler};
84//!
85//! let scheduler = TaskScheduler::new().await?;
86//! let mut tasks = Vec::new();
87//!
88//! // Create multiple tasks for batch processing
89//! for file_path in subtitle_files {
90//! let task = FileProcessingTask::new(
91//! file_path,
92//! ProcessingOperation::EncodingDetection,
93//! 2, // Higher priority for encoding detection
94//! );
95//! tasks.push(task);
96//! }
97//!
98//! // Submit all tasks for parallel execution
99//! let results = scheduler.submit_batch(tasks).await?;
100//!
101//! for result in results {
102//! match result {
103//! Ok(output) => println!("Success: {:?}", output),
104//! Err(error) => eprintln!("Failed: {}", error),
105//! }
106//! }
107//! ```
108//!
109//! ## Custom Worker Configuration
110//!
111//! ```rust,ignore
112//! use subx_cli::core::parallel::{TaskScheduler, WorkerConfig};
113//!
114//! let config = WorkerConfig {
115//! max_workers: 8,
116//! min_workers: 2,
117//! cpu_intensive_workers: 4,
118//! io_intensive_workers: 2,
119//! memory_limit_mb: 1024,
120//! task_timeout_secs: 300,
121//! };
122//!
123//! let scheduler = TaskScheduler::with_config(config).await?;
124//! ```
125//!
126//! # Task Types
127//!
128//! ## Format Conversion Tasks
129//! - Convert between different subtitle formats (SRT, ASS, VTT, SUB)
130//! - Preserve styling information where possible
131//! - Handle encoding conversion automatically
132//!
133//! ## AI Analysis Tasks
134//! - Semantic content analysis for matching
135//! - Language detection and verification
136//! - Quality assessment and scoring
137//! - Content similarity comparison
138//!
139//! ## Audio Processing Tasks
140//! - Speech detection and timing extraction
141//! - Audio-subtitle synchronization
142//! - Voice activity detection
143//! - Audio quality analysis
144//!
145//! ## File Management Tasks
146//! - Batch file operations (copy, move, rename)
147//! - Encoding detection and conversion
148//! - Metadata extraction and validation
149//! - Directory structure analysis
150//!
151//! # Performance Characteristics
152//!
153//! ## CPU Optimization
154//! - **Work Stealing**: Idle workers steal tasks from busy workers
155//! - **Cache Affinity**: Tasks are scheduled to maintain CPU cache locality
156//! - **Thread Pinning**: Critical workers can be pinned to specific CPU cores
157//! - **SIMD Utilization**: Vectorized operations where applicable
158//!
159//! ## Memory Management
160//! - **Pool Allocation**: Reuses memory buffers to reduce allocation overhead
161//! - **Memory Pressure Handling**: Automatically reduces concurrency under memory pressure
162//! - **Garbage Collection Optimization**: Minimizes allocations in hot paths
163//! - **Memory-Mapped I/O**: Uses memory mapping for large file operations
164//!
165//! ## I/O Optimization
166//! - **Asynchronous I/O**: Non-blocking file operations where possible
167//! - **Read-Ahead**: Predictive file reading based on task patterns
168//! - **Write Coalescing**: Batches small writes for better performance
169//! - **Network Optimization**: Optimized handling of AI service requests
170//!
171//! # Error Handling and Recovery
172//!
173//! ## Task-Level Recovery
174//! - **Automatic Retry**: Failed tasks are retried with exponential backoff
175//! - **Fallback Strategies**: Alternative approaches for failed operations
176//! - **Partial Results**: Returns partial results when possible
177//! - **Progress Preservation**: Saves intermediate results for long-running tasks
178//!
179//! ## System-Level Recovery
180//! - **Worker Restart**: Automatically restarts failed workers
181//! - **Resource Cleanup**: Ensures proper cleanup after failures
182//! - **State Persistence**: Maintains scheduler state across restarts
183//! - **Graceful Degradation**: Continues operation with reduced capacity
184//!
185//! # Monitoring and Observability
186//!
187//! The parallel system provides comprehensive monitoring:
188//! - **Task Metrics**: Execution time, success rate, resource usage
189//! - **Worker Health**: CPU usage, memory consumption, error rates
190//! - **System Performance**: Overall throughput, queue depth, response times
191//! - **Resource Utilization**: CPU, memory, disk, and network usage
192//!
193//! # Thread Safety
194//!
195//! All components are designed for concurrent access:
196//! - Lock-free data structures where possible
197//! - Minimal contention in critical paths
198//! - Safe sharing of resources between threads
199//! - Proper synchronization for shared state
200
201pub mod config;
202pub mod load_balancer;
203pub mod pool;
204pub mod scheduler;
205pub mod task;
206pub mod worker;
207
208pub use scheduler::TaskScheduler;
209pub use task::{FileProcessingTask, ProcessingOperation, Task, TaskResult, TaskStatus};