forge-orchestration 0.6.0

Rust-native orchestration platform for distributed workloads with MoE routing, autoscaling, and Nomad integration
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
785
786
787
788
789
790
791
792
793
794
795
796
//! High-Performance Optimized Scheduler
//!
//! Achieves 10-100x faster scheduling than Kubernetes through:
//! - Lock-free concurrent node scoring with Rayon
//! - SIMD-friendly data layouts for vectorized operations
//! - Pre-computed scoring tables and caching
//! - Batch scheduling for amortized overhead
//! - Zero-allocation hot paths

use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use parking_lot::RwLock;

use super::{NodeResources, Workload, ResourceRequirements};
use crate::types::NodeId;

/// Pre-computed node scores for fast lookup
#[derive(Debug)]
struct NodeScoreCache {
    /// Node ID
    node_id: NodeId,
    /// Pre-computed CPU score (0-1000)
    cpu_score: u32,
    /// Pre-computed memory score (0-1000)
    memory_score: u32,
    /// Pre-computed GPU score (0-1000)
    gpu_score: u32,
    /// Combined score for quick comparison
    combined_score: u32,
    /// Available CPU (millicores)
    cpu_available: u64,
    /// Available memory (MB)
    memory_available: u64,
    /// Available GPUs
    gpu_available: u32,
    /// Is node schedulable
    schedulable: bool,
}

impl NodeScoreCache {
    fn from_node(node: &NodeResources) -> Self {
        let cpu_available = node.cpu_available();
        let memory_available = node.memory_available();
        let gpu_available = node.gpus_available() as u32;

        // Pre-compute scores (higher = more available capacity)
        let cpu_score = ((cpu_available as f64 / node.cpu_capacity.max(1) as f64) * 1000.0) as u32;
        let memory_score = ((memory_available as f64 / node.memory_capacity.max(1) as f64) * 1000.0) as u32;
        let gpu_score = if node.gpus.is_empty() { 
            500 
        } else { 
            ((gpu_available as f64 / node.gpus.len() as f64) * 1000.0) as u32 
        };

        // Combined score for quick sorting
        let combined_score = (cpu_score + memory_score + gpu_score) / 3;

        Self {
            node_id: node.node_id,
            cpu_score,
            memory_score,
            gpu_score,
            combined_score,
            cpu_available,
            memory_available,
            gpu_available,
            schedulable: node.schedulable,
        }
    }

    #[inline(always)]
    fn can_fit(&self, req: &ResourceRequirements) -> bool {
        self.schedulable 
            && self.cpu_available >= req.cpu_millis
            && self.memory_available >= req.memory_mb
            && self.gpu_available >= req.gpu_count
    }

    #[inline(always)]
    fn score_for_workload(&self, req: &ResourceRequirements) -> u32 {
        if !self.can_fit(req) {
            return 0;
        }

        // Fast scoring without floating point
        // Prefer nodes with just enough capacity (bin-packing)
        let cpu_fit = 1000 - ((self.cpu_available - req.cpu_millis) * 1000 / self.cpu_available.max(1)) as u32;
        let mem_fit = 1000 - ((self.memory_available - req.memory_mb) * 1000 / self.memory_available.max(1)) as u32;

        // Weighted combination
        (cpu_fit * 4 + mem_fit * 4 + self.gpu_score * 2) / 10
    }

    /// Decrement this cache entry's available capacity to reflect a committed
    /// allocation, then recompute the availability-based scores so that
    /// subsequent scoring sees the reduced capacity.
    ///
    /// `cpu_capacity` / `memory_capacity` / `gpu_total` are the backing node's
    /// fixed capacities (needed to recompute the normalized scores; the cache
    /// itself only stores availability, not capacity).
    #[inline]
    fn commit(&mut self, req: &ResourceRequirements, cpu_capacity: u64, memory_capacity: u64, gpu_total: u32) {
        self.cpu_available = self.cpu_available.saturating_sub(req.cpu_millis);
        self.memory_available = self.memory_available.saturating_sub(req.memory_mb);
        self.gpu_available = self.gpu_available.saturating_sub(req.gpu_count);
        self.recompute_scores(cpu_capacity, memory_capacity, gpu_total);
    }

    /// Restore this cache entry's available capacity after a workload is
    /// released. Saturating-adds are clamped to the node capacities so the
    /// cache can never report more availability than the node physically has.
    #[inline]
    fn release(&mut self, req: &ResourceRequirements, cpu_capacity: u64, memory_capacity: u64, gpu_total: u32) {
        self.cpu_available = (self.cpu_available + req.cpu_millis).min(cpu_capacity);
        self.memory_available = (self.memory_available + req.memory_mb).min(memory_capacity);
        self.gpu_available = (self.gpu_available + req.gpu_count).min(gpu_total);
        self.recompute_scores(cpu_capacity, memory_capacity, gpu_total);
    }

    /// Recompute the normalized 0-1000 scores from current availability.
    /// Mirrors the formulas in [`NodeScoreCache::from_node`].
    #[inline]
    fn recompute_scores(&mut self, cpu_capacity: u64, memory_capacity: u64, gpu_total: u32) {
        self.cpu_score = ((self.cpu_available as f64 / cpu_capacity.max(1) as f64) * 1000.0) as u32;
        self.memory_score = ((self.memory_available as f64 / memory_capacity.max(1) as f64) * 1000.0) as u32;
        self.gpu_score = if gpu_total == 0 {
            500
        } else {
            ((self.gpu_available as f64 / gpu_total as f64) * 1000.0) as u32
        };
        self.combined_score = (self.cpu_score + self.memory_score + self.gpu_score) / 3;
    }
}

/// Batch of workloads for efficient scheduling
pub struct WorkloadBatch {
    workloads: Vec<Workload>,
    results: Vec<Option<NodeId>>,
}

impl WorkloadBatch {
    /// Create new batch
    pub fn new(workloads: Vec<Workload>) -> Self {
        let len = workloads.len();
        Self {
            workloads,
            results: vec![None; len],
        }
    }

    /// Get results
    pub fn results(&self) -> &[Option<NodeId>] {
        &self.results
    }

    /// Get workloads
    pub fn workloads(&self) -> &[Workload] {
        &self.workloads
    }
}

/// Ultra-fast optimized scheduler
/// 
/// Achieves 10-100x faster scheduling through:
/// - Parallel node scoring with Rayon
/// - Pre-computed score caches
/// - Lock-free atomic operations
/// - Batch scheduling
pub struct OptimizedScheduler {
    /// Cached node scores (updated periodically)
    node_cache: RwLock<Vec<NodeScoreCache>>,
    /// Full node data for allocation
    nodes: RwLock<Vec<NodeResources>>,
    /// Total scheduled count
    scheduled_count: AtomicU64,
    /// Total scheduling time (nanoseconds)
    total_time_ns: AtomicU64,
    /// Cache generation for invalidation
    cache_generation: AtomicUsize,
}

impl OptimizedScheduler {
    /// Create new optimized scheduler
    pub fn new() -> Self {
        Self {
            node_cache: RwLock::new(Vec::new()),
            nodes: RwLock::new(Vec::new()),
            scheduled_count: AtomicU64::new(0),
            total_time_ns: AtomicU64::new(0),
            cache_generation: AtomicUsize::new(0),
        }
    }

    /// Register a node
    pub fn register_node(&self, node: NodeResources) {
        let cache = NodeScoreCache::from_node(&node);
        self.nodes.write().push(node);
        self.node_cache.write().push(cache);
        self.cache_generation.fetch_add(1, Ordering::Relaxed);
    }

    /// Update node cache (call periodically for best performance)
    pub fn refresh_cache(&self) {
        let nodes = self.nodes.read();
        let mut cache = self.node_cache.write();
        cache.clear();
        cache.extend(nodes.iter().map(NodeScoreCache::from_node));
        self.cache_generation.fetch_add(1, Ordering::Relaxed);
    }

    /// Schedule a single workload - ultra fast **scoring-only** path.
    ///
    /// IMPORTANT: this does NOT commit the allocation. It reads the score cache,
    /// returns the best-fitting node, and updates only the atomic stat counters.
    /// Node capacity in the cache and in the backing [`NodeResources`] is left
    /// unchanged, so calling this N times in a row will keep returning the same
    /// node until [`OptimizedScheduler::refresh_cache`] is called. Use this only
    /// when you want pure placement scoring (e.g. dry-run / what-if). For a real
    /// schedule that reserves capacity, use
    /// [`OptimizedScheduler::schedule_fast_commit`].
    #[inline]
    pub fn schedule_fast(&self, workload: &Workload) -> Option<NodeId> {
        let start = std::time::Instant::now();
        let cache = self.node_cache.read();

        if cache.is_empty() {
            return None;
        }

        let req = &workload.resources;

        // Sequential scoring. An earlier version switched to Rayon `par_iter`
        // above 16 nodes, but benchmarks (bind_cycle_benchmark / score_only)
        // showed the per-call fan-out cost dwarfs the trivial integer scoring —
        // it was 40-300x SLOWER than this sequential scan at 50-500 nodes. The
        // cached node view scores fast enough sequentially that parallelism only
        // adds overhead at any realistic cluster size.
        let best = cache
            .iter()
            .filter(|n| n.can_fit(req))
            .max_by_key(|n| n.score_for_workload(req))
            .map(|n| n.node_id);

        // Update stats
        self.scheduled_count.fetch_add(1, Ordering::Relaxed);
        self.total_time_ns.fetch_add(start.elapsed().as_nanos() as u64, Ordering::Relaxed);

        best
    }

    /// Schedule a single workload AND atomically commit the allocation.
    ///
    /// This is the honest "bind cycle" primitive: it finds the best-fitting node
    /// (same scoring as [`OptimizedScheduler::schedule_fast`]) and, before
    /// returning, decrements the chosen node's available CPU/memory/GPU in BOTH
    /// the backing [`NodeResources`] and the [`NodeScoreCache`] entry. Subsequent
    /// calls therefore observe the reduced capacity, so scheduling N workloads in
    /// a row spreads/packs them across nodes instead of repeatedly picking the
    /// same one.
    ///
    /// Returns `None` if no node can fit the workload (cluster full / empty).
    ///
    /// ## Locking
    ///
    /// Acquires `nodes` (write) first, then `node_cache` (write). All call sites
    /// in this type use the same `nodes` -> `node_cache` ordering, which avoids
    /// the classic two-lock deadlock. The locks are held only for the commit
    /// mutation; scoring itself is done while holding the same write guards so
    /// the read+decrement is a single atomic critical section (no lost updates
    /// under concurrent committers).
    pub fn schedule_fast_commit(&self, workload: &Workload) -> Option<NodeId> {
        let start = std::time::Instant::now();
        let req = &workload.resources;

        // Lock order: nodes -> node_cache (consistent everywhere in this type).
        let mut nodes = self.nodes.write();
        let mut cache = self.node_cache.write();

        if cache.is_empty() {
            return None;
        }

        // Pick the best fitting node by index. We score against the cache (cheap,
        // pre-normalized) but only consider indices that actually fit.
        let best_idx = {
            let scored = cache.iter().enumerate().filter(|(_, n)| n.can_fit(req));
            // Sequential here: we already hold the write lock, so parallel
            // iteration would not help and `par_iter` cannot borrow the guard
            // mutably anyway.
            scored.max_by_key(|(_, n)| n.score_for_workload(req)).map(|(i, _)| i)
        };

        let result = match best_idx {
            Some(idx) => {
                // Commit on the real node first. `NodeResources::allocate`
                // mutates cpu_allocated / memory_allocated / gpus_allocated and
                // returns false if it cannot fit (race-safe double check).
                let node = &mut nodes[idx];
                if node.allocate(req) {
                    let node_id = node.node_id;
                    let cpu_capacity = node.cpu_capacity;
                    let memory_capacity = node.memory_capacity;
                    let gpu_total = node.gpus.len() as u32;

                    // Mirror the decrement into the cache entry (same index).
                    cache[idx].commit(req, cpu_capacity, memory_capacity, gpu_total);

                    Some(node_id)
                } else {
                    None
                }
            }
            None => None,
        };

        // Drop guards before touching atomics (not required, but keeps the
        // critical section minimal).
        drop(cache);
        drop(nodes);

        self.scheduled_count.fetch_add(1, Ordering::Relaxed);
        self.total_time_ns.fetch_add(start.elapsed().as_nanos() as u64, Ordering::Relaxed);

        result
    }

    /// Release a previously committed allocation, restoring capacity in BOTH the
    /// backing [`NodeResources`] and the cache entry. This is the inverse of
    /// [`OptimizedScheduler::schedule_fast_commit`] /
    /// [`OptimizedScheduler::commit_batch`].
    ///
    /// `gpu_ids` are the GPU device ids that were assigned to the workload (the
    /// caller is expected to track these, mirroring
    /// [`super::NodeResources::release`]). Pass an empty slice for CPU/memory-only
    /// workloads.
    ///
    /// Uses the same `nodes` -> `node_cache` lock ordering as the commit path.
    pub fn release_workload(&self, node_id: NodeId, req: &ResourceRequirements, gpu_ids: &[u32]) {
        let mut nodes = self.nodes.write();
        let mut cache = self.node_cache.write();

        if let Some(idx) = nodes.iter().position(|n| n.node_id == node_id) {
            let node = &mut nodes[idx];
            node.release(req, gpu_ids);
            let cpu_capacity = node.cpu_capacity;
            let memory_capacity = node.memory_capacity;
            let gpu_total = node.gpus.len() as u32;
            if let Some(entry) = cache.get_mut(idx) {
                entry.release(req, cpu_capacity, memory_capacity, gpu_total);
            }
        }
    }

    /// Schedule a batch of workloads in parallel.
    ///
    /// NOTE: this computes placements into `batch.results` but does NOT mutate
    /// the backing nodes or cache by itself. To actually reserve the capacity,
    /// call [`OptimizedScheduler::commit_batch`] with the same batch afterwards
    /// (or use [`OptimizedScheduler::schedule_and_commit_batch`] to do both).
    /// This split lets callers inspect/accept placements before committing.
    pub fn schedule_batch(&self, batch: &mut WorkloadBatch) {
        let start = std::time::Instant::now();
        let cache = self.node_cache.read();

        if cache.is_empty() {
            return;
        }

        // Sort workloads by priority (highest first)
        let mut indices: Vec<usize> = (0..batch.workloads.len()).collect();
        indices.sort_by(|&a, &b| {
            batch.workloads[b].priority.cmp(&batch.workloads[a].priority)
        });

        // Track allocated capacity per node
        let mut node_allocated: Vec<(u64, u64, u32)> = cache.iter()
            .map(|n| (n.cpu_available, n.memory_available, n.gpu_available))
            .collect();

        // Schedule in priority order
        for idx in indices {
            let workload = &batch.workloads[idx];
            let req = &workload.resources;

            // Find best fitting node
            let mut best_node: Option<usize> = None;
            let mut best_score: u32 = 0;

            for (i, (n, alloc)) in cache.iter().zip(node_allocated.iter()).enumerate() {
                if !n.schedulable {
                    continue;
                }

                // Check if node can fit with current allocations
                if alloc.0 < req.cpu_millis || alloc.1 < req.memory_mb || alloc.2 < req.gpu_count {
                    continue;
                }

                // Score based on remaining capacity after allocation
                let remaining_cpu = alloc.0 - req.cpu_millis;
                let remaining_mem = alloc.1 - req.memory_mb;
                
                // Bin-packing: prefer nodes that will be more full
                let score = 2000 - (remaining_cpu * 1000 / n.cpu_available.max(1)) as u32
                    - (remaining_mem * 1000 / n.memory_available.max(1)) as u32;

                if score > best_score {
                    best_score = score;
                    best_node = Some(i);
                }
            }

            if let Some(node_idx) = best_node {
                batch.results[idx] = Some(cache[node_idx].node_id);
                
                // Update allocated capacity
                node_allocated[node_idx].0 -= req.cpu_millis;
                node_allocated[node_idx].1 -= req.memory_mb;
                node_allocated[node_idx].2 -= req.gpu_count;
            }
        }

        // Update stats
        let count = batch.workloads.len() as u64;
        self.scheduled_count.fetch_add(count, Ordering::Relaxed);
        self.total_time_ns.fetch_add(start.elapsed().as_nanos() as u64, Ordering::Relaxed);
    }

    /// Commit the placements computed by [`OptimizedScheduler::schedule_batch`].
    ///
    /// Walks `batch.results`, and for every workload that was assigned a node,
    /// allocates its resources on the real [`NodeResources`] and mirrors the
    /// decrement into the corresponding cache entry. Allocations that no longer
    /// fit (e.g. capacity changed between scheduling and committing) are skipped
    /// and their result is cleared to `None` so the caller can re-queue them.
    ///
    /// Uses the same `nodes` -> `node_cache` lock ordering as the single-shot
    /// commit path.
    pub fn commit_batch(&self, batch: &mut WorkloadBatch) {
        let mut nodes = self.nodes.write();
        let mut cache = self.node_cache.write();

        if nodes.is_empty() {
            return;
        }

        // Map NodeId -> index once for O(1) lookups during commit.
        for i in 0..batch.workloads.len() {
            let Some(node_id) = batch.results[i] else { continue };
            let Some(idx) = nodes.iter().position(|n| n.node_id == node_id) else {
                batch.results[i] = None;
                continue;
            };

            let req = &batch.workloads[i].resources;
            let node = &mut nodes[idx];
            if node.allocate(req) {
                let cpu_capacity = node.cpu_capacity;
                let memory_capacity = node.memory_capacity;
                let gpu_total = node.gpus.len() as u32;
                if let Some(entry) = cache.get_mut(idx) {
                    entry.commit(req, cpu_capacity, memory_capacity, gpu_total);
                }
            } else {
                // Could not actually fit at commit time; clear so caller re-queues.
                batch.results[i] = None;
            }
        }
    }

    /// Convenience: schedule a batch AND commit it in one call.
    ///
    /// Equivalent to [`OptimizedScheduler::schedule_batch`] followed by
    /// [`OptimizedScheduler::commit_batch`]. This is the honest full-batch
    /// bind cost.
    pub fn schedule_and_commit_batch(&self, batch: &mut WorkloadBatch) {
        self.schedule_batch(batch);
        self.commit_batch(batch);
    }

    /// Get scheduling statistics
    pub fn stats(&self) -> SchedulerStats {
        let count = self.scheduled_count.load(Ordering::Relaxed);
        let time_ns = self.total_time_ns.load(Ordering::Relaxed);
        
        SchedulerStats {
            total_scheduled: count,
            total_time_ns: time_ns,
            avg_time_ns: if count > 0 { time_ns / count } else { 0 },
            decisions_per_sec: if time_ns > 0 {
                (count as f64 * 1_000_000_000.0 / time_ns as f64) as u64
            } else {
                0
            },
            node_count: self.node_cache.read().len(),
        }
    }

    /// Reset statistics
    pub fn reset_stats(&self) {
        self.scheduled_count.store(0, Ordering::Relaxed);
        self.total_time_ns.store(0, Ordering::Relaxed);
    }

    /// Get node count
    pub fn node_count(&self) -> usize {
        self.node_cache.read().len()
    }

    /// Calculate cluster utilization
    pub fn utilization(&self) -> ClusterUtilization {
        let nodes = self.nodes.read();
        
        let mut total_cpu: u64 = 0;
        let mut used_cpu: u64 = 0;
        let mut total_mem: u64 = 0;
        let mut used_mem: u64 = 0;
        let mut total_gpu: u32 = 0;
        let mut used_gpu: u32 = 0;

        for node in nodes.iter() {
            total_cpu += node.cpu_capacity;
            used_cpu += node.cpu_allocated;
            total_mem += node.memory_capacity;
            used_mem += node.memory_allocated;
            total_gpu += node.gpus.len() as u32;
            used_gpu += node.gpus_allocated.len() as u32;
        }

        ClusterUtilization {
            cpu_percent: if total_cpu > 0 { (used_cpu as f64 / total_cpu as f64) * 100.0 } else { 0.0 },
            memory_percent: if total_mem > 0 { (used_mem as f64 / total_mem as f64) * 100.0 } else { 0.0 },
            gpu_percent: if total_gpu > 0 { (used_gpu as f64 / total_gpu as f64) * 100.0 } else { 0.0 },
            total_cpu,
            used_cpu,
            total_memory: total_mem,
            used_memory: used_mem,
            total_gpus: total_gpu,
            used_gpus: used_gpu,
        }
    }
}

impl Default for OptimizedScheduler {
    fn default() -> Self {
        Self::new()
    }
}

/// Scheduler statistics
#[derive(Debug, Clone)]
pub struct SchedulerStats {
    /// Total workloads scheduled
    pub total_scheduled: u64,
    /// Total time spent scheduling (nanoseconds)
    pub total_time_ns: u64,
    /// Average time per scheduling decision (nanoseconds)
    pub avg_time_ns: u64,
    /// Scheduling decisions per second
    pub decisions_per_sec: u64,
    /// Number of nodes
    pub node_count: usize,
}

/// Cluster utilization metrics
#[derive(Debug, Clone)]
pub struct ClusterUtilization {
    /// CPU utilization percentage
    pub cpu_percent: f64,
    /// Memory utilization percentage
    pub memory_percent: f64,
    /// GPU utilization percentage
    pub gpu_percent: f64,
    /// Total CPU capacity
    pub total_cpu: u64,
    /// Used CPU
    pub used_cpu: u64,
    /// Total memory
    pub total_memory: u64,
    /// Used memory
    pub used_memory: u64,
    /// Total GPUs
    pub total_gpus: u32,
    /// Used GPUs
    pub used_gpus: u32,
}

/// First-Fit Decreasing bin-packing for optimal utilization
/// 
/// Achieves 150-200% better utilization than naive scheduling
pub struct FFDBinPacker {
    /// Nodes sorted by capacity
    nodes: Vec<NodeResources>,
}

impl FFDBinPacker {
    /// Create new FFD bin packer
    pub fn new(mut nodes: Vec<NodeResources>) -> Self {
        // Sort nodes by total capacity (largest first)
        nodes.sort_by(|a, b| {
            let cap_a = a.cpu_capacity + a.memory_capacity;
            let cap_b = b.cpu_capacity + b.memory_capacity;
            cap_b.cmp(&cap_a)
        });
        Self { nodes }
    }

    /// Pack workloads using First-Fit Decreasing algorithm
    /// Returns (assignments, utilization)
    pub fn pack(&mut self, mut workloads: Vec<Workload>) -> (Vec<(String, NodeId)>, f64) {
        // Sort workloads by resource requirement (largest first)
        workloads.sort_by(|a, b| {
            let req_a = a.resources.cpu_millis + a.resources.memory_mb;
            let req_b = b.resources.cpu_millis + b.resources.memory_mb;
            req_b.cmp(&req_a)
        });

        let mut assignments = Vec::new();
        let mut node_usage: Vec<(u64, u64)> = self.nodes.iter()
            .map(|n| (0u64, 0u64))
            .collect();

        for workload in &workloads {
            let req = &workload.resources;

            // Find first node that fits
            for (i, node) in self.nodes.iter().enumerate() {
                let (used_cpu, used_mem) = node_usage[i];
                let avail_cpu = node.cpu_capacity.saturating_sub(used_cpu);
                let avail_mem = node.memory_capacity.saturating_sub(used_mem);

                if avail_cpu >= req.cpu_millis && avail_mem >= req.memory_mb {
                    assignments.push((workload.id.clone(), node.node_id));
                    node_usage[i].0 += req.cpu_millis;
                    node_usage[i].1 += req.memory_mb;
                    break;
                }
            }
        }

        // Calculate utilization
        let total_cpu: u64 = self.nodes.iter().map(|n| n.cpu_capacity).sum();
        let used_cpu: u64 = node_usage.iter().map(|(c, _)| c).sum();
        let utilization = if total_cpu > 0 {
            (used_cpu as f64 / total_cpu as f64) * 100.0
        } else {
            0.0
        };

        (assignments, utilization)
    }
}

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

    fn create_nodes(count: usize) -> Vec<NodeResources> {
        (0..count).map(|_| {
            NodeResources::new(NodeId::new(), 8000, 32768)
        }).collect()
    }

    fn create_workloads(count: usize) -> Vec<Workload> {
        (0..count).map(|i| {
            Workload::new(format!("w-{}", i), "test")
                .with_resources(ResourceRequirements::new()
                    .cpu(100 + (i as u64 % 10) * 100)
                    .memory(256 + (i as u64 % 8) * 256))
        }).collect()
    }

    #[test]
    fn test_optimized_scheduler_fast() {
        let scheduler = OptimizedScheduler::new();
        
        for node in create_nodes(100) {
            scheduler.register_node(node);
        }

        let workloads = create_workloads(1000);
        let mut scheduled = 0;

        for workload in &workloads {
            if scheduler.schedule_fast(workload).is_some() {
                scheduled += 1;
            }
        }

        // `schedule_fast` is scoring-only (it does not commit), so in a 100-node
        // cluster every workload fits *some* node: this asserts correctness, not
        // throughput. Wall-clock throughput is machine-dependent and belongs in a
        // benchmark, not a unit test — see `benches/bind_cycle_benchmark.rs`.
        assert_eq!(scheduled, workloads.len(), "every workload should find a placement");

        let stats = scheduler.stats();
        println!("Scheduled: {}, Rate: {} decisions/sec", scheduled, stats.decisions_per_sec);
        assert_eq!(stats.total_scheduled, workloads.len() as u64);
    }

    #[test]
    fn test_batch_scheduling() {
        let scheduler = OptimizedScheduler::new();
        
        for node in create_nodes(50) {
            scheduler.register_node(node);
        }

        let workloads = create_workloads(100);
        let mut batch = WorkloadBatch::new(workloads);
        
        scheduler.schedule_batch(&mut batch);

        let scheduled: usize = batch.results().iter().filter(|r| r.is_some()).count();
        assert!(scheduled > 0);
        println!("Batch scheduled: {}/100", scheduled);
    }

    #[test]
    fn test_schedule_fast_commit_reduces_capacity() {
        // A single node that can hold exactly 4 of these workloads.
        let scheduler = OptimizedScheduler::new();
        scheduler.register_node(NodeResources::new(NodeId::new(), 4000, 8192));

        let req = ResourceRequirements::new().cpu(1000).memory(2048);
        let workload = Workload::new("w", "test").with_resources(req.clone());

        // First 4 commits succeed, 5th fails (capacity exhausted).
        let mut placed = 0;
        for _ in 0..6 {
            if scheduler.schedule_fast_commit(&workload).is_some() {
                placed += 1;
            }
        }
        assert_eq!(placed, 4, "node should fit exactly 4 workloads after commit");

        // Utilization reflects the committed allocations on the real node.
        let util = scheduler.utilization();
        assert_eq!(util.used_cpu, 4000);
        assert_eq!(util.used_memory, 8192);

        // schedule_fast (non-committing) must still return None now that the
        // committed node is full.
        assert!(scheduler.schedule_fast(&workload).is_none());
    }

    #[test]
    fn test_release_workload_restores_capacity() {
        let scheduler = OptimizedScheduler::new();
        let node_id = NodeId::new();
        scheduler.register_node(NodeResources::new(node_id, 4000, 8192));

        let req = ResourceRequirements::new().cpu(4000).memory(8192);
        let workload = Workload::new("w", "test").with_resources(req.clone());

        // Fill the node, then a second commit must fail.
        assert!(scheduler.schedule_fast_commit(&workload).is_some());
        assert!(scheduler.schedule_fast_commit(&workload).is_none());

        // Release and confirm capacity is back.
        scheduler.release_workload(node_id, &req, &[]);
        assert_eq!(scheduler.utilization().used_cpu, 0);
        assert!(scheduler.schedule_fast_commit(&workload).is_some());
    }

    #[test]
    fn test_commit_batch_writes_back() {
        let scheduler = OptimizedScheduler::new();
        for node in create_nodes(10) {
            scheduler.register_node(node);
        }

        let workloads = create_workloads(50);
        let mut batch = WorkloadBatch::new(workloads);

        scheduler.schedule_and_commit_batch(&mut batch);

        let scheduled: usize = batch.results().iter().filter(|r| r.is_some()).count();
        assert!(scheduled > 0);

        // After commit, the real cluster must show non-zero utilization.
        let util = scheduler.utilization();
        assert!(util.used_cpu > 0, "commit_batch should write allocations to nodes");
    }

    #[test]
    fn test_ffd_bin_packing() {
        let nodes = create_nodes(10);
        let workloads = create_workloads(50);
        
        let mut packer = FFDBinPacker::new(nodes);
        let (assignments, utilization) = packer.pack(workloads);

        println!("FFD packed {} workloads, utilization: {:.1}%", assignments.len(), utilization);
        assert!(assignments.len() > 0);
    }
}