pepita 0.1.0

Tiny First-Principles Rust Kernel for Sovereign AI - io_uring/ublk/blk-mq interfaces
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
//! High-level execution pool for distributed task execution.
//!
//! This module provides the main user-facing API for submitting and
//! executing tasks across CPU, GPU, and remote backends.
//!
//! ## Example
//!
//! ```rust,ignore
//! use pepita::pool::Pool;
//! use pepita::task::Task;
//!
//! // Create a pool with 4 CPU workers
//! let pool = Pool::builder()
//!     .cpu_workers(4)
//!     .build()?;
//!
//! // Submit a task
//! let task = Task::binary("./worker")
//!     .args(vec!["--input", "data.bin"])
//!     .build();
//!
//! let result = pool.submit(task)?;
//! ```

use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::Arc;

use crate::error::{KernelError, Result};
use crate::executor::{CpuExecutor, ExecutorRegistry, GpuExecutor, RemoteExecutor};
use crate::fault::{FailureDetector, RetryPolicy, RetryState};
use crate::scheduler::{Scheduler, TaskId};
use crate::task::{ExecutionResult, Task};

// ============================================================================
// POOL CONFIGURATION
// ============================================================================

/// Configuration for the execution pool.
#[derive(Debug, Clone)]
pub struct PoolConfig {
    /// Number of CPU workers (0 = auto-detect)
    pub cpu_workers: usize,
    /// Enable GPU backend
    pub enable_gpu: bool,
    /// Remote worker addresses
    pub remote_workers: Vec<String>,
    /// Queue capacity per worker
    pub queue_capacity: usize,
    /// Default retry policy
    pub retry_policy: RetryPolicy,
    /// Task timeout (None = no timeout)
    pub default_timeout: Option<std::time::Duration>,
}

impl Default for PoolConfig {
    fn default() -> Self {
        Self {
            cpu_workers: 0, // Auto-detect
            enable_gpu: false,
            remote_workers: Vec::new(),
            queue_capacity: 1024,
            retry_policy: RetryPolicy::default(),
            default_timeout: None,
        }
    }
}

// ============================================================================
// POOL BUILDER
// ============================================================================

/// Builder for creating execution pools.
#[derive(Debug, Default)]
pub struct PoolBuilder {
    config: PoolConfig,
}

impl PoolBuilder {
    /// Create a new pool builder.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Set number of CPU workers.
    #[must_use]
    pub const fn cpu_workers(mut self, count: usize) -> Self {
        self.config.cpu_workers = count;
        self
    }

    /// Enable GPU backend.
    #[must_use]
    pub const fn enable_gpu(mut self, enable: bool) -> Self {
        self.config.enable_gpu = enable;
        self
    }

    /// Add remote workers.
    #[must_use]
    pub fn remote_workers(mut self, addresses: Vec<impl Into<String>>) -> Self {
        self.config.remote_workers = addresses.into_iter().map(Into::into).collect();
        self
    }

    /// Set queue capacity per worker.
    #[must_use]
    pub const fn queue_capacity(mut self, capacity: usize) -> Self {
        self.config.queue_capacity = capacity;
        self
    }

    /// Set retry policy.
    #[must_use]
    pub fn retry_policy(mut self, policy: RetryPolicy) -> Self {
        self.config.retry_policy = policy;
        self
    }

    /// Set default timeout.
    #[must_use]
    pub fn default_timeout(mut self, timeout: std::time::Duration) -> Self {
        self.config.default_timeout = Some(timeout);
        self
    }

    /// Build the pool.
    pub fn build(self) -> Result<Pool> {
        Pool::from_config(self.config)
    }
}

// ============================================================================
// POOL STATISTICS
// ============================================================================

/// Statistics for the execution pool.
#[derive(Debug, Clone, Default)]
pub struct PoolStats {
    /// Total tasks submitted
    pub tasks_submitted: u64,
    /// Tasks completed successfully
    pub tasks_completed: u64,
    /// Tasks failed
    pub tasks_failed: u64,
    /// Tasks currently pending
    pub tasks_pending: u64,
    /// Total retries
    pub total_retries: u64,
}

impl PoolStats {
    /// Get success rate.
    #[must_use]
    pub fn success_rate(&self) -> f64 {
        let total = self.tasks_completed + self.tasks_failed;
        if total == 0 {
            return 1.0;
        }
        self.tasks_completed as f64 / total as f64
    }
}

// ============================================================================
// POOL
// ============================================================================

/// Execution pool for distributed task execution.
///
/// The pool manages a set of executors (CPU, GPU, remote) and a
/// work-stealing scheduler for efficient task distribution.
pub struct Pool {
    /// Configuration
    config: PoolConfig,
    /// Work-stealing scheduler
    scheduler: Arc<Scheduler<Task>>,
    /// Executor registry
    executors: Arc<ExecutorRegistry>,
    /// Failure detector
    failure_detector: Arc<FailureDetector>,
    /// Tasks submitted counter
    tasks_submitted: AtomicU64,
    /// Tasks completed counter
    tasks_completed: AtomicU64,
    /// Tasks failed counter
    tasks_failed: AtomicU64,
    /// Retries counter
    total_retries: AtomicU64,
    /// Whether pool is running
    running: AtomicBool,
}

impl Pool {
    /// Create a new pool builder.
    #[must_use]
    pub fn builder() -> PoolBuilder {
        PoolBuilder::new()
    }

    /// Create a pool from configuration.
    fn from_config(config: PoolConfig) -> Result<Self> {
        let cpu_workers = if config.cpu_workers == 0 {
            std::thread::available_parallelism()
                .map(|p| p.get())
                .unwrap_or(4)
        } else {
            config.cpu_workers
        };

        // Create scheduler
        let scheduler = Arc::new(Scheduler::with_capacity(
            cpu_workers,
            config.queue_capacity,
        ));

        // Create executor registry
        let mut executors = ExecutorRegistry::new();

        // Add CPU executor
        executors.register(Arc::new(CpuExecutor::new(cpu_workers)));

        // Add GPU executor if enabled
        if config.enable_gpu {
            executors.register(Arc::new(GpuExecutor::new()));
        }

        // Add remote executors
        if !config.remote_workers.is_empty() {
            let mut remote = RemoteExecutor::new();
            for addr in &config.remote_workers {
                remote.add_worker(addr.clone());
            }
            executors.register(Arc::new(remote));
        }

        // Create failure detector
        let failure_detector = Arc::new(FailureDetector::new());

        Ok(Self {
            config,
            scheduler,
            executors: Arc::new(executors),
            failure_detector,
            tasks_submitted: AtomicU64::new(0),
            tasks_completed: AtomicU64::new(0),
            tasks_failed: AtomicU64::new(0),
            total_retries: AtomicU64::new(0),
            running: AtomicBool::new(true),
        })
    }

    /// Submit a task for execution.
    ///
    /// The task is queued in the scheduler and executed by the
    /// appropriate backend based on task requirements.
    pub fn submit(&self, task: Task) -> Result<ExecutionResult> {
        if !self.running.load(Ordering::Acquire) {
            return Err(KernelError::DeviceNotReady);
        }

        self.tasks_submitted.fetch_add(1, Ordering::Relaxed);

        // Execute with retry
        let mut retry_state = RetryState::new();
        let mut current_task = task;

        loop {
            // Try to execute
            let result = self.executors.execute(&current_task);

            match result {
                Ok(exec_result) => {
                    if exec_result.is_success() {
                        self.tasks_completed.fetch_add(1, Ordering::Relaxed);
                        return Ok(exec_result);
                    }

                    // Check if we should retry
                    if retry_state.should_retry(&self.config.retry_policy) {
                        retry_state.record_failure(
                            exec_result.error.clone().unwrap_or_default(),
                        );
                        self.total_retries.fetch_add(1, Ordering::Relaxed);
                        current_task.increment_retry();

                        // Wait before retry
                        let delay = retry_state.next_delay(&self.config.retry_policy);
                        if delay > std::time::Duration::ZERO {
                            std::thread::sleep(delay);
                        }
                        continue;
                    }

                    self.tasks_failed.fetch_add(1, Ordering::Relaxed);
                    return Ok(exec_result);
                }
                Err(e) => {
                    // Check if retriable error
                    if e.is_retriable() && retry_state.should_retry(&self.config.retry_policy) {
                        retry_state.record_failure(format!("{}", e));
                        self.total_retries.fetch_add(1, Ordering::Relaxed);
                        current_task.increment_retry();

                        let delay = retry_state.next_delay(&self.config.retry_policy);
                        if delay > std::time::Duration::ZERO {
                            std::thread::sleep(delay);
                        }
                        continue;
                    }

                    self.tasks_failed.fetch_add(1, Ordering::Relaxed);
                    return Err(e);
                }
            }
        }
    }

    /// Submit a task and get back a task ID (non-blocking).
    pub fn submit_async(&self, task: Task) -> Result<TaskId> {
        if !self.running.load(Ordering::Acquire) {
            return Err(KernelError::DeviceNotReady);
        }

        self.tasks_submitted.fetch_add(1, Ordering::Relaxed);

        self.scheduler
            .submit(task)
            .ok_or(KernelError::UblkQueueFull)
    }

    /// Get pool statistics.
    #[must_use]
    pub fn stats(&self) -> PoolStats {
        PoolStats {
            tasks_submitted: self.tasks_submitted.load(Ordering::Relaxed),
            tasks_completed: self.tasks_completed.load(Ordering::Relaxed),
            tasks_failed: self.tasks_failed.load(Ordering::Relaxed),
            tasks_pending: self.scheduler.pending_tasks() as u64,
            total_retries: self.total_retries.load(Ordering::Relaxed),
        }
    }

    /// Get the number of workers.
    #[must_use]
    pub fn num_workers(&self) -> usize {
        self.executors.total_workers()
    }

    /// Get the number of pending tasks.
    #[must_use]
    pub fn pending_tasks(&self) -> usize {
        self.scheduler.pending_tasks()
    }

    /// Check if pool is running.
    #[must_use]
    pub fn is_running(&self) -> bool {
        self.running.load(Ordering::Acquire)
    }

    /// Shutdown the pool.
    pub fn shutdown(&self) {
        self.running.store(false, Ordering::Release);
        self.scheduler.stop();
        self.executors.shutdown();
        self.failure_detector.stop();
    }

    /// Get worker loads.
    #[must_use]
    pub fn worker_loads(&self) -> Vec<usize> {
        self.scheduler.worker_loads()
    }
}

impl std::fmt::Debug for Pool {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Pool")
            .field("num_workers", &self.num_workers())
            .field("pending_tasks", &self.pending_tasks())
            .field("running", &self.is_running())
            .finish()
    }
}

// ============================================================================
// TESTS (EXTREME TDD)
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::task::TaskPriority;

    // ------------------------------------------------------------------------
    // PoolConfig Tests
    // ------------------------------------------------------------------------

    #[test]
    fn test_pool_config_default() {
        let config = PoolConfig::default();
        assert_eq!(config.cpu_workers, 0);
        assert!(!config.enable_gpu);
        assert!(config.remote_workers.is_empty());
    }

    // ------------------------------------------------------------------------
    // PoolBuilder Tests
    // ------------------------------------------------------------------------

    #[test]
    fn test_pool_builder_cpu_workers() {
        let pool = Pool::builder().cpu_workers(4).build().unwrap();
        assert_eq!(pool.num_workers(), 4);
    }

    #[test]
    fn test_pool_builder_auto_workers() {
        let pool = Pool::builder().cpu_workers(0).build().unwrap();
        assert!(pool.num_workers() >= 1);
    }

    #[test]
    fn test_pool_builder_queue_capacity() {
        let pool = Pool::builder()
            .cpu_workers(2)
            .queue_capacity(100)
            .build()
            .unwrap();
        assert!(pool.is_running());
    }

    #[test]
    fn test_pool_builder_retry_policy() {
        let policy = RetryPolicy::no_retry();
        let pool = Pool::builder()
            .cpu_workers(2)
            .retry_policy(policy)
            .build()
            .unwrap();
        assert!(pool.is_running());
    }

    // ------------------------------------------------------------------------
    // Pool Tests
    // ------------------------------------------------------------------------

    #[test]
    fn test_pool_builder() {
        let pool = Pool::builder().cpu_workers(4).build();
        assert!(pool.is_ok());
    }

    #[test]
    fn test_pool_submit_echo() {
        let pool = Pool::builder().cpu_workers(2).build().unwrap();

        let task = Task::binary("echo")
            .args(vec!["Hello, Pool!"])
            .build();

        let result = pool.submit(task).unwrap();
        assert!(result.is_success());
        assert!(result.stdout_string().contains("Hello"));
    }

    #[test]
    fn test_pool_submit_multiple() {
        let pool = Pool::builder().cpu_workers(4).build().unwrap();

        for i in 0..10 {
            let task = Task::binary("echo").args(vec![format!("Task {}", i)]).build();
            let result = pool.submit(task).unwrap();
            assert!(result.is_success());
        }

        let stats = pool.stats();
        assert_eq!(stats.tasks_submitted, 10);
        assert_eq!(stats.tasks_completed, 10);
    }

    #[test]
    fn test_pool_stats() {
        let pool = Pool::builder().cpu_workers(2).build().unwrap();

        // Submit successful task
        let task = Task::binary("true").build();
        pool.submit(task).unwrap();

        // Submit failing task
        let task = Task::binary("false").build();
        let _ = pool.submit(task);

        let stats = pool.stats();
        assert_eq!(stats.tasks_submitted, 2);
        assert_eq!(stats.tasks_completed, 1);
        assert_eq!(stats.tasks_failed, 1);
    }

    #[test]
    fn test_pool_stats_success_rate() {
        let mut stats = PoolStats::default();
        assert_eq!(stats.success_rate(), 1.0);

        stats.tasks_completed = 8;
        stats.tasks_failed = 2;
        assert!((stats.success_rate() - 0.8).abs() < 0.001);
    }

    #[test]
    fn test_pool_shutdown() {
        let pool = Pool::builder().cpu_workers(2).build().unwrap();
        assert!(pool.is_running());

        pool.shutdown();
        assert!(!pool.is_running());

        // Submit should fail after shutdown
        let task = Task::binary("echo").args(vec!["test"]).build();
        let result = pool.submit(task);
        assert!(result.is_err());
    }

    #[test]
    fn test_pool_worker_loads() {
        let pool = Pool::builder().cpu_workers(4).build().unwrap();
        let loads = pool.worker_loads();
        assert_eq!(loads.len(), 4);
    }

    #[test]
    fn test_pool_pending_tasks() {
        let pool = Pool::builder().cpu_workers(2).build().unwrap();
        assert_eq!(pool.pending_tasks(), 0);
    }

    #[test]
    fn test_pool_retry() {
        let pool = Pool::builder()
            .cpu_workers(2)
            .retry_policy(RetryPolicy::new().with_max_retries(2))
            .build()
            .unwrap();

        // This will fail and retry
        let task = Task::binary("false").build();
        let result = pool.submit(task);
        assert!(result.is_ok()); // Returns result, but failed

        let stats = pool.stats();
        // Should have retried
        assert!(stats.total_retries > 0 || stats.tasks_failed == 1);
    }

    #[test]
    fn test_pool_submit_async() {
        let pool = Pool::builder().cpu_workers(2).build().unwrap();

        let task = Task::binary("echo").args(vec!["async"]).build();
        let task_id = pool.submit_async(task);
        assert!(task_id.is_ok());
    }

    #[test]
    fn test_pool_with_priority() {
        let pool = Pool::builder().cpu_workers(2).build().unwrap();

        let task = Task::binary("echo")
            .args(vec!["high priority"])
            .priority(TaskPriority::High)
            .build();

        let result = pool.submit(task).unwrap();
        assert!(result.is_success());
    }

    #[test]
    fn test_pool_debug() {
        let pool = Pool::builder().cpu_workers(2).build().unwrap();
        let debug = format!("{:?}", pool);
        assert!(debug.contains("Pool"));
        assert!(debug.contains("num_workers"));
    }
}