forge-orchestration 0.5.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
//! Request batching for AI/ML inference
//!
//! Provides dynamic batching to improve throughput for inference workloads.

use std::collections::VecDeque;
use std::sync::Arc;
use std::time::{Duration, Instant};
use parking_lot::Mutex;
use tokio::sync::{oneshot, Notify};
use tracing::{debug, info};

/// Configuration for batch processing
#[derive(Debug, Clone)]
pub struct BatchConfig {
    /// Maximum batch size
    pub max_batch_size: usize,
    /// Maximum wait time before processing a partial batch
    pub max_wait_ms: u64,
    /// Minimum batch size to trigger immediate processing
    pub min_batch_size: usize,
    /// Enable dynamic batch sizing based on load
    pub dynamic_sizing: bool,
}

impl Default for BatchConfig {
    fn default() -> Self {
        Self {
            max_batch_size: 32,
            max_wait_ms: 50,
            min_batch_size: 1,
            dynamic_sizing: true,
        }
    }
}

impl BatchConfig {
    /// Create a new batch config
    pub fn new() -> Self {
        Self::default()
    }

    /// Set maximum batch size
    pub fn max_size(mut self, size: usize) -> Self {
        self.max_batch_size = size.max(1);
        self
    }

    /// Set maximum wait time in milliseconds
    pub fn max_wait(mut self, ms: u64) -> Self {
        self.max_wait_ms = ms;
        self
    }

    /// Set minimum batch size for immediate processing
    pub fn min_size(mut self, size: usize) -> Self {
        self.min_batch_size = size.max(1);
        self
    }
}

/// A request in the batch queue
pub struct BatchRequest<T> {
    /// Request payload
    pub payload: T,
    /// Response channel
    response_tx: oneshot::Sender<BatchResult<T>>,
    /// Request arrival time
    arrived_at: Instant,
}

/// Result of a batched request
#[derive(Debug)]
pub struct BatchResult<T> {
    /// Response payload
    pub payload: T,
    /// Batch size this request was processed with
    pub batch_size: usize,
    /// Time spent waiting in queue
    pub queue_time_ms: u64,
    /// Processing time
    pub process_time_ms: u64,
}

/// Batch processor for inference requests
pub struct BatchProcessor<T: Send + 'static> {
    config: BatchConfig,
    queue: Arc<Mutex<VecDeque<BatchRequest<T>>>>,
    notify: Arc<Notify>,
    stats: Arc<Mutex<BatchStats>>,
}

/// Statistics for batch processing
#[derive(Debug, Default, Clone)]
pub struct BatchStats {
    /// Total requests processed
    pub total_requests: u64,
    /// Total batches processed
    pub total_batches: u64,
    /// Average batch size
    pub avg_batch_size: f64,
    /// Average queue time in ms
    pub avg_queue_time_ms: f64,
    /// Average processing time in ms
    pub avg_process_time_ms: f64,
}

impl<T: Send + 'static> BatchProcessor<T> {
    /// Create a new batch processor
    pub fn new(config: BatchConfig) -> Self {
        Self {
            config,
            queue: Arc::new(Mutex::new(VecDeque::new())),
            notify: Arc::new(Notify::new()),
            stats: Arc::new(Mutex::new(BatchStats::default())),
        }
    }

    /// Submit a request and wait for the result
    pub async fn submit(&self, payload: T) -> Result<BatchResult<T>, BatchError> {
        let (tx, rx) = oneshot::channel();
        
        let request = BatchRequest {
            payload,
            response_tx: tx,
            arrived_at: Instant::now(),
        };

        {
            let mut queue = self.queue.lock();
            queue.push_back(request);
            
            // Notify if we've reached max batch size
            if queue.len() >= self.config.max_batch_size {
                self.notify.notify_one();
            }
        }

        // Also notify to start the timer
        self.notify.notify_one();

        rx.await.map_err(|_| BatchError::Cancelled)
    }

    /// Run the dynamic-batching serving loop: wait for a batch, collect it, run
    /// `executor` on the payloads, and deliver each request's response.
    ///
    /// This is the piece that makes [`BatchProcessor::submit`] resolve — without
    /// a running `run` (or manual `collect_batch`/`complete_batch`), submitted
    /// futures never complete. It is the serving primitive an inference/expert
    /// worker spawns as a task; it loops until the task is cancelled.
    ///
    /// `executor` receives the batched payloads in arrival order and must return
    /// exactly one response per input, in the same order. (If it returns fewer,
    /// the surplus requests are cancelled rather than answered.)
    ///
    /// ```ignore
    /// let p = processor.clone();
    /// tokio::spawn(async move {
    ///     p.run(|batch: Vec<Req>| async move { model.forward(batch).await }).await;
    /// });
    /// let result = processor.submit(req).await?; // resolved by the loop
    /// ```
    pub async fn run<F, Fut>(&self, mut executor: F)
    where
        F: FnMut(Vec<T>) -> Fut,
        Fut: std::future::Future<Output = Vec<T>>,
    {
        info!(max_batch = self.config.max_batch_size, "batch serving loop started");
        loop {
            if !self.wait_for_batch().await {
                continue;
            }
            let batch = self.collect_batch();
            if batch.is_empty() {
                continue;
            }

            let mut payloads = Vec::with_capacity(batch.len());
            let mut meta = Vec::with_capacity(batch.len());
            for (payload, tx, arrived_at) in batch {
                payloads.push(payload);
                meta.push((tx, arrived_at));
            }

            let responses = executor(payloads).await;

            let results = meta
                .into_iter()
                .zip(responses)
                .map(|((tx, arrived_at), response)| (tx, arrived_at, response))
                .collect();
            self.complete_batch(results);
        }
    }

    /// Get current queue length
    pub fn queue_len(&self) -> usize {
        self.queue.lock().len()
    }

    /// Get batch statistics
    pub fn stats(&self) -> BatchStats {
        self.stats.lock().clone()
    }

    /// Collect a batch of requests (up to max_batch_size)
    pub fn collect_batch(&self) -> Vec<(T, oneshot::Sender<BatchResult<T>>, Instant)> {
        let mut queue = self.queue.lock();
        let batch_size = queue.len().min(self.config.max_batch_size);
        
        let mut batch = Vec::with_capacity(batch_size);
        for _ in 0..batch_size {
            if let Some(req) = queue.pop_front() {
                batch.push((req.payload, req.response_tx, req.arrived_at));
            }
        }
        
        batch
    }

    /// Complete a batch with results.
    ///
    /// Each entry is `(response_sender, arrival_instant, response_payload)`.
    pub fn complete_batch(&self, results: Vec<(oneshot::Sender<BatchResult<T>>, Instant, T)>) {
        let batch_size = results.len();
        let process_start = Instant::now();

        let mut total_queue_time = 0u64;

        for (tx, arrived_at, response) in results {
            let queue_time = arrived_at.elapsed().as_millis() as u64;
            total_queue_time += queue_time;
            
            let result = BatchResult {
                payload: response,
                batch_size,
                queue_time_ms: queue_time,
                process_time_ms: process_start.elapsed().as_millis() as u64,
            };
            
            let _ = tx.send(result);
        }

        // Update stats
        let mut stats = self.stats.lock();
        stats.total_requests += batch_size as u64;
        stats.total_batches += 1;
        
        let n = stats.total_batches as f64;
        stats.avg_batch_size = stats.avg_batch_size * (n - 1.0) / n + batch_size as f64 / n;
        stats.avg_queue_time_ms = stats.avg_queue_time_ms * (n - 1.0) / n 
            + (total_queue_time as f64 / batch_size as f64) / n;
        
        debug!(batch_size = batch_size, "Batch completed");
    }

    /// Wait for a batch to be ready
    pub async fn wait_for_batch(&self) -> bool {
        let timeout = Duration::from_millis(self.config.max_wait_ms);
        
        tokio::select! {
            _ = self.notify.notified() => {
                // Check if we have enough for a batch
                self.queue.lock().len() >= self.config.min_batch_size
            }
            _ = tokio::time::sleep(timeout) => {
                // Timeout - process whatever we have
                !self.queue.lock().is_empty()
            }
        }
    }
}

impl<T: Send + 'static> Clone for BatchProcessor<T> {
    fn clone(&self) -> Self {
        Self {
            config: self.config.clone(),
            queue: self.queue.clone(),
            notify: self.notify.clone(),
            stats: self.stats.clone(),
        }
    }
}

/// Batch processing error
#[derive(Debug, thiserror::Error)]
pub enum BatchError {
    /// Request was cancelled
    #[error("Request cancelled")]
    Cancelled,
    /// Queue is full
    #[error("Queue full")]
    QueueFull,
    /// Processing failed
    #[error("Processing failed: {0}")]
    ProcessingFailed(String),
}

/// Simple batch collector for manual batch processing
pub struct BatchCollector<T> {
    items: Vec<T>,
    max_size: usize,
    created_at: Instant,
    max_wait: Duration,
}

impl<T> BatchCollector<T> {
    /// Create a new batch collector
    pub fn new(max_size: usize, max_wait_ms: u64) -> Self {
        Self {
            items: Vec::with_capacity(max_size),
            max_size,
            created_at: Instant::now(),
            max_wait: Duration::from_millis(max_wait_ms),
        }
    }

    /// Add an item to the batch
    pub fn add(&mut self, item: T) -> bool {
        if self.items.len() < self.max_size {
            self.items.push(item);
            true
        } else {
            false
        }
    }

    /// Check if batch is ready (full or timeout)
    pub fn is_ready(&self) -> bool {
        self.items.len() >= self.max_size || self.created_at.elapsed() >= self.max_wait
    }

    /// Check if batch is full
    pub fn is_full(&self) -> bool {
        self.items.len() >= self.max_size
    }

    /// Get current batch size
    pub fn len(&self) -> usize {
        self.items.len()
    }

    /// Check if batch is empty
    pub fn is_empty(&self) -> bool {
        self.items.is_empty()
    }

    /// Take the collected items
    pub fn take(self) -> Vec<T> {
        self.items
    }

    /// Time since batch was created
    pub fn age(&self) -> Duration {
        self.created_at.elapsed()
    }
}

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

    #[test]
    fn test_batch_collector() {
        let mut collector: BatchCollector<i32> = BatchCollector::new(3, 100);
        
        assert!(collector.add(1));
        assert!(collector.add(2));
        assert!(!collector.is_full());
        assert!(collector.add(3));
        assert!(collector.is_full());
        assert!(!collector.add(4)); // Should fail, batch is full
        
        let items = collector.take();
        assert_eq!(items, vec![1, 2, 3]);
    }

    #[tokio::test]
    async fn test_batch_processor_stats() {
        let processor: BatchProcessor<String> = BatchProcessor::new(BatchConfig::default());

        let stats = processor.stats();
        assert_eq!(stats.total_requests, 0);
        assert_eq!(stats.total_batches, 0);
    }

    #[tokio::test]
    async fn test_batch_processor_end_to_end() {
        // Spawn the serving loop with a trivial "model" that doubles each input,
        // then submit requests and confirm they are batched and answered.
        let processor: BatchProcessor<u32> = BatchProcessor::new(BatchConfig::default().max_wait(20));
        let server = processor.clone();
        tokio::spawn(async move {
            server
                .run(|inputs: Vec<u32>| async move {
                    inputs.into_iter().map(|x| x * 2).collect::<Vec<u32>>()
                })
                .await;
        });

        let r1 = processor.submit(21).await.unwrap();
        assert_eq!(r1.payload, 42);
        assert!(r1.batch_size >= 1);

        let r2 = processor.submit(50).await.unwrap();
        assert_eq!(r2.payload, 100);

        assert!(processor.stats().total_requests >= 2);
    }
}