rust-data-processing 0.1.6

Schema-first ingestion (CSV, JSON, Parquet, Excel) into an in-memory DataSet, plus Polars-backed pipelines, SQL, profiling, validation, and map/reduce-style processing.
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
//! Execution engine for running processing pipelines with configurable parallelism.
//!
//! This module sits "above" [`crate::processing`] and provides:
//!
//! - Parallel (chunked) execution for filter/map
//! - Resource limits / throttling (e.g., in-flight chunks)
//! - Real-time metrics + observer hooks for monitoring

mod observer;
mod semaphore;

use std::sync::Arc;
use std::time::{Duration, Instant};

use rayon::ThreadPool;
use rayon::ThreadPoolBuilder;
use rayon::prelude::*;

use crate::processing::{ReduceOp, reduce};
use crate::types::{DataSet, Value};

pub use observer::{
    ExecutionEvent, ExecutionMetrics, ExecutionMetricsSnapshot, ExecutionObserver,
    StdErrExecutionObserver,
};

use semaphore::Semaphore;

/// Configuration for the [`ExecutionEngine`].
#[derive(Debug, Clone)]
pub struct ExecutionOptions {
    /// Number of worker threads used by the engine.
    ///
    /// If `None`, uses the platform's available parallelism.
    pub num_threads: Option<usize>,
    /// Number of rows per chunk.
    ///
    /// Chunking lets the engine bound working-set size and implement throttling.
    pub chunk_size: usize,
    /// Upper bound on concurrently executing chunks.
    ///
    /// This is an additional throttle on top of `num_threads`.
    pub max_in_flight_chunks: usize,
}

impl Default for ExecutionOptions {
    fn default() -> Self {
        let n = std::thread::available_parallelism()
            .map(|n| n.get())
            .unwrap_or(1);
        Self {
            num_threads: Some(n),
            chunk_size: 4_096,
            max_in_flight_chunks: n.max(1),
        }
    }
}

/// A configurable execution engine for in-memory [`DataSet`] pipelines.
pub struct ExecutionEngine {
    pool: ThreadPool,
    opts: ExecutionOptions,
    observer: Option<Arc<dyn ExecutionObserver>>,
    metrics: Arc<ExecutionMetrics>,
}

impl ExecutionEngine {
    /// Create a new engine with the given options.
    ///
    /// # Panics
    ///
    /// Panics if `chunk_size == 0`, `max_in_flight_chunks == 0`, or `num_threads == Some(0)`.
    pub fn new(opts: ExecutionOptions) -> Self {
        assert!(opts.chunk_size > 0, "chunk_size must be > 0");
        assert!(
            opts.max_in_flight_chunks > 0,
            "max_in_flight_chunks must be > 0"
        );
        if let Some(n) = opts.num_threads {
            assert!(n > 0, "num_threads must be > 0 when set");
        }

        let n_threads = opts
            .num_threads
            .unwrap_or_else(|| {
                std::thread::available_parallelism()
                    .map(|n| n.get())
                    .unwrap_or(1)
            })
            .max(1);

        let pool = ThreadPoolBuilder::new()
            .num_threads(n_threads)
            .build()
            .expect("failed to build rayon thread pool");

        Self {
            pool,
            opts: opts.clone(),
            observer: None,
            metrics: Arc::new(ExecutionMetrics::new()),
        }
    }

    /// Attach an observer for execution events (metrics/logging).
    pub fn with_observer(mut self, observer: Arc<dyn ExecutionObserver>) -> Self {
        self.observer = Some(observer);
        self
    }

    /// Get a handle to real-time execution metrics.
    pub fn metrics(&self) -> Arc<ExecutionMetrics> {
        Arc::clone(&self.metrics)
    }

    /// Execute a parallel filter over the dataset.
    pub fn filter_parallel<F>(&self, dataset: &DataSet, predicate: F) -> DataSet
    where
        F: Fn(&[Value]) -> bool + Send + Sync,
    {
        self.pool
            .install(|| self.filter_parallel_impl(dataset, &predicate))
    }

    fn filter_parallel_impl(
        &self,
        dataset: &DataSet,
        predicate: &(dyn Fn(&[Value]) -> bool + Send + Sync),
    ) -> DataSet {
        let start = Instant::now();
        self.metrics.begin_run();
        self.emit(ExecutionEvent::RunStarted);

        let sem = Semaphore::new(self.opts.max_in_flight_chunks);
        let chunk_ranges = chunk_ranges(dataset.row_count(), self.opts.chunk_size);

        let per_chunk: Vec<Vec<Vec<Value>>> = chunk_ranges
            .into_par_iter()
            .map(|range| {
                let waited = sem.acquire();
                if waited > Duration::ZERO {
                    self.metrics.on_throttle_wait(waited);
                    self.emit(ExecutionEvent::ThrottleWaited { duration: waited });
                }

                self.metrics.on_chunk_start();
                self.emit(ExecutionEvent::ChunkStarted {
                    start_row: range.start,
                    row_count: range.end - range.start,
                });

                let mut out = Vec::new();
                for row in &dataset.rows[range] {
                    self.metrics.on_row_processed();
                    if predicate(row.as_slice()) {
                        out.push(row.clone());
                    }
                }

                self.emit(ExecutionEvent::ChunkFinished {
                    output_rows: out.len(),
                });
                self.metrics.on_chunk_end();
                sem.release();
                out
            })
            .collect();

        let rows = per_chunk.into_iter().flatten().collect::<Vec<_>>();
        let out = DataSet::new(dataset.schema.clone(), rows);

        self.metrics.end_run(start.elapsed());
        self.emit(ExecutionEvent::RunFinished {
            elapsed: start.elapsed(),
            metrics: self.metrics.snapshot(),
        });

        out
    }

    /// Execute a parallel map over the dataset.
    ///
    /// # Panics
    ///
    /// Panics if `mapper` returns rows with a different length than the schema field count.
    pub fn map_parallel<F>(&self, dataset: &DataSet, mapper: F) -> DataSet
    where
        F: Fn(&[Value]) -> Vec<Value> + Send + Sync,
    {
        self.pool
            .install(|| self.map_parallel_impl(dataset, &mapper))
    }

    fn map_parallel_impl(
        &self,
        dataset: &DataSet,
        mapper: &(dyn Fn(&[Value]) -> Vec<Value> + Send + Sync),
    ) -> DataSet {
        let start = Instant::now();
        self.metrics.begin_run();
        self.emit(ExecutionEvent::RunStarted);

        let expected_len = dataset.schema.fields.len();
        let sem = Semaphore::new(self.opts.max_in_flight_chunks);
        let chunk_ranges = chunk_ranges(dataset.row_count(), self.opts.chunk_size);

        let per_chunk: Vec<Vec<Vec<Value>>> = chunk_ranges
            .into_par_iter()
            .map(|range| {
                let waited = sem.acquire();
                if waited > Duration::ZERO {
                    self.metrics.on_throttle_wait(waited);
                    self.emit(ExecutionEvent::ThrottleWaited { duration: waited });
                }

                self.metrics.on_chunk_start();
                self.emit(ExecutionEvent::ChunkStarted {
                    start_row: range.start,
                    row_count: range.end - range.start,
                });

                let mut out = Vec::with_capacity(range.end - range.start);
                for row in &dataset.rows[range] {
                    self.metrics.on_row_processed();
                    let mapped = mapper(row.as_slice());
                    assert!(
                        mapped.len() == expected_len,
                        "mapped row length {} does not match schema length {}",
                        mapped.len(),
                        expected_len
                    );
                    out.push(mapped);
                }

                self.emit(ExecutionEvent::ChunkFinished {
                    output_rows: out.len(),
                });
                self.metrics.on_chunk_end();
                sem.release();
                out
            })
            .collect();

        let rows = per_chunk.into_iter().flatten().collect::<Vec<_>>();
        let out = DataSet::new(dataset.schema.clone(), rows);

        self.metrics.end_run(start.elapsed());
        self.emit(ExecutionEvent::RunFinished {
            elapsed: start.elapsed(),
            metrics: self.metrics.snapshot(),
        });

        out
    }

    /// Reduce a column using the existing built-in reduce operation.
    ///
    /// This is currently sequential, but is tracked via the observer/metrics hooks.
    pub fn reduce(&self, dataset: &DataSet, column: &str, op: ReduceOp) -> Option<Value> {
        let start = Instant::now();
        self.metrics.begin_run();
        self.emit(ExecutionEvent::RunStarted);
        self.emit(ExecutionEvent::ReduceStarted {
            column: column.to_string(),
            op,
        });

        let out = reduce(dataset, column, op);

        self.emit(ExecutionEvent::ReduceFinished {
            result: out.clone(),
        });
        self.metrics.end_run(start.elapsed());
        self.emit(ExecutionEvent::RunFinished {
            elapsed: start.elapsed(),
            metrics: self.metrics.snapshot(),
        });
        out
    }

    fn emit(&self, event: ExecutionEvent) {
        if let Some(obs) = &self.observer {
            obs.on_event(&event);
        }
    }
}

fn chunk_ranges(row_count: usize, chunk_size: usize) -> Vec<std::ops::Range<usize>> {
    if row_count == 0 {
        return Vec::new();
    }
    let mut out = Vec::with_capacity((row_count + chunk_size - 1) / chunk_size);
    let mut start = 0usize;
    while start < row_count {
        let end = (start + chunk_size).min(row_count);
        out.push(start..end);
        start = end;
    }
    out
}

#[cfg(test)]
mod tests {
    use super::{ExecutionEngine, ExecutionOptions};
    use std::sync::Arc;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::time::Duration;

    use crate::execution::{ExecutionEvent, ExecutionObserver};
    use crate::types::{DataSet, DataType, Field, Schema, Value};

    fn dataset_of_n(n: usize) -> DataSet {
        let schema = Schema::new(vec![Field::new("id", DataType::Int64)]);
        let mut rows = Vec::with_capacity(n);
        for i in 0..n as i64 {
            rows.push(vec![Value::Int64(i)]);
        }
        DataSet::new(schema, rows)
    }

    #[test]
    fn map_parallel_runs_with_concurrency() {
        let ds = dataset_of_n(400);
        let engine = ExecutionEngine::new(ExecutionOptions {
            num_threads: Some(4),
            chunk_size: 1,
            max_in_flight_chunks: 4,
        });

        let active = Arc::new(AtomicUsize::new(0));
        let max_active = Arc::new(AtomicUsize::new(0));

        let active2 = Arc::clone(&active);
        let max_active2 = Arc::clone(&max_active);

        let out = engine.map_parallel(&ds, move |row| {
            let now = active2.fetch_add(1, Ordering::SeqCst) + 1;
            // max = max(max, now)
            loop {
                let cur = max_active2.load(Ordering::SeqCst);
                if now <= cur {
                    break;
                }
                if max_active2
                    .compare_exchange(cur, now, Ordering::SeqCst, Ordering::SeqCst)
                    .is_ok()
                {
                    break;
                }
            }

            std::thread::sleep(Duration::from_millis(2));
            let _ = active2.fetch_sub(1, Ordering::SeqCst);

            let v = match row[0] {
                Value::Int64(x) => x + 1,
                _ => 0,
            };
            vec![Value::Int64(v)]
        });

        assert_eq!(out.row_count(), ds.row_count());
        assert!(max_active.load(Ordering::SeqCst) > 1);
    }

    struct ConcurrencyObserver {
        active_chunks: AtomicUsize,
        max_active_chunks: AtomicUsize,
    }

    impl ConcurrencyObserver {
        fn new() -> Self {
            Self {
                active_chunks: AtomicUsize::new(0),
                max_active_chunks: AtomicUsize::new(0),
            }
        }
        fn max(&self) -> usize {
            self.max_active_chunks.load(Ordering::SeqCst)
        }
        fn bump_max(&self, now: usize) {
            loop {
                let cur = self.max_active_chunks.load(Ordering::SeqCst);
                if now <= cur {
                    break;
                }
                if self
                    .max_active_chunks
                    .compare_exchange(cur, now, Ordering::SeqCst, Ordering::SeqCst)
                    .is_ok()
                {
                    break;
                }
            }
        }
    }

    impl ExecutionObserver for ConcurrencyObserver {
        fn on_event(&self, event: &ExecutionEvent) {
            match event {
                ExecutionEvent::ChunkStarted { .. } => {
                    let now = self.active_chunks.fetch_add(1, Ordering::SeqCst) + 1;
                    self.bump_max(now);
                }
                ExecutionEvent::ChunkFinished { .. } => {
                    let _ = self.active_chunks.fetch_sub(1, Ordering::SeqCst);
                }
                _ => {}
            }
        }
    }

    #[test]
    fn max_in_flight_chunks_throttles_chunk_concurrency() {
        let ds = dataset_of_n(100);
        let observer = Arc::new(ConcurrencyObserver::new());
        let obs_trait: Arc<dyn ExecutionObserver> = observer.clone();
        let engine = ExecutionEngine::new(ExecutionOptions {
            num_threads: Some(4),
            chunk_size: 1,
            max_in_flight_chunks: 1,
        })
        .with_observer(obs_trait);

        let out = engine.map_parallel(&ds, |_row| {
            // Make each chunk/row take long enough to overlap if not throttled.
            std::thread::sleep(Duration::from_millis(1));
            vec![Value::Int64(1)]
        });

        assert_eq!(out.row_count(), ds.row_count());
        assert_eq!(observer.max(), 1);
    }

    #[test]
    fn metrics_are_available_after_run() {
        let ds = dataset_of_n(60);
        let engine = ExecutionEngine::new(ExecutionOptions {
            num_threads: Some(4),
            chunk_size: 1,
            max_in_flight_chunks: 1,
        });
        let metrics = engine.metrics();

        let out = engine.map_parallel(&ds, |_row| {
            std::thread::sleep(Duration::from_millis(2));
            vec![Value::Int64(1)]
        });

        assert_eq!(out.row_count(), ds.row_count());

        let snap = metrics.snapshot();
        assert_eq!(snap.rows_processed, ds.row_count() as u64);
        assert_eq!(snap.chunks_started, ds.row_count() as u64);
        assert_eq!(snap.chunks_finished, ds.row_count() as u64);
        assert_eq!(snap.max_active_chunks, 1);
        assert!(snap.throttle_wait > Duration::ZERO);
        assert!(snap.elapsed.is_some());
    }
}