causal_utils 0.4.0

Utility patterns for causal-based applications — batching, dataloader, and more
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
use std::collections::HashMap;
use std::future::Future;
use std::hash::Hash;
use std::sync::Arc;
use std::time::{Duration, Instant};

use anyhow::Result;
use dashmap::DashMap;
use parking_lot::Mutex;
use tokio::sync::oneshot;

/// A shared batcher that accumulates work items across concurrent reactor
/// invocations, then flushes them together when a threshold is met.
///
/// This is the dataloader pattern applied to event reactors: each reactor
/// thinks it's doing one unit of work, but the batcher transparently groups
/// items and processes them in a single batch call.
///
/// ```ignore
/// let sources = ctx.deps().batcher
///     .submit("source_expansion", concern.id, concern.clone())
///     .flush_when(|batch| batch.len() >= 8 || batch.age() >= Duration::from_secs(5))
///     .then(discover_response_sources_batch)
///     .await?;
/// ```
#[derive(Clone)]
pub struct Batcher {
    groups: Arc<DashMap<String, Arc<dyn std::any::Any + Send + Sync>>>,
}

impl Batcher {
    pub fn new() -> Self {
        Self {
            groups: Arc::new(DashMap::new()),
        }
    }

    /// Submit an item to a named batch group.
    ///
    /// Items with the same `group` name are collected together. The returned
    /// `Submission` lets you configure flush policy and the batch function.
    pub fn submit<K, I>(&self, group: impl Into<String>, key: K, item: I) -> Submission<K, I>
    where
        K: Eq + Hash + Clone + Send + Sync + 'static,
        I: Send + Sync + 'static,
    {
        Submission {
            batcher: self.clone(),
            group: group.into(),
            key,
            item: Some(item),
        }
    }
}

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

/// A pending submission to a batch group. Configure flush policy with
/// `flush_when`, then provide the batch function with `then`.
pub struct Submission<K, I> {
    batcher: Batcher,
    group: String,
    key: K,
    item: Option<I>,
}

impl<K, I> Submission<K, I>
where
    K: Eq + Hash + Clone + Send + Sync + 'static,
    I: Send + Sync + 'static,
{
    /// Set the flush policy for this batch group.
    ///
    /// The predicate receives a `BatchInfo` with the current batch size
    /// and age, and returns `true` when the batch should flush.
    ///
    /// ```ignore
    /// .flush_when(|batch| batch.len() >= 8 || batch.age() >= Duration::from_secs(5))
    /// ```
    pub fn flush_when<P>(self, predicate: P) -> FlushConfigured<K, I, P>
    where
        P: Fn(&BatchInfo) -> bool + Send + Sync + 'static,
    {
        FlushConfigured {
            batcher: self.batcher,
            group: self.group,
            key: self.key,
            item: self.item,
            predicate,
        }
    }
}

/// Batch metadata passed to the `flush_when` predicate.
pub struct BatchInfo {
    len: usize,
    created_at: Instant,
}

impl BatchInfo {
    /// Number of items currently in the batch.
    pub fn len(&self) -> usize {
        self.len
    }

    /// Whether the batch is empty.
    pub fn is_empty(&self) -> bool {
        self.len == 0
    }

    /// How long since the first item was added to this batch.
    pub fn age(&self) -> Duration {
        self.created_at.elapsed()
    }
}

/// A submission with flush policy configured. Call `then` to provide the
/// batch processing function.
pub struct FlushConfigured<K, I, P> {
    batcher: Batcher,
    group: String,
    key: K,
    item: Option<I>,
    predicate: P,
}

impl<K, I, P> FlushConfigured<K, I, P>
where
    K: Eq + Hash + Clone + Send + Sync + 'static,
    I: Send + Sync + 'static,
    P: Fn(&BatchInfo) -> bool + Send + Sync + 'static,
{
    /// Provide the batch processing function and await the result.
    ///
    /// The function receives all accumulated items and must return a
    /// `HashMap<K, O>` mapping each item's key to its result.
    ///
    /// ```ignore
    /// .then(|items: Vec<Concern>| async move {
    ///     let results = process_batch(&items).await?;
    ///     Ok(results.into_iter().map(|r| (r.id, r)).collect())
    /// })
    /// .await?
    /// ```
    pub async fn then<F, Fut, O>(mut self, f: F) -> Result<O>
    where
        F: FnOnce(Vec<I>) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<HashMap<K, O>>> + Send + 'static,
        O: Send + Sync + 'static,
    {
        let item = self.item.take().expect("item already consumed");
        let key = self.key.clone();

        type GroupState<K, I, O> = Arc<Mutex<BatchGroupInner<K, I, O>>>;

        let group_state: GroupState<K, I, O> = {
            let groups = &self.batcher.groups;
            let entry = groups
                .entry(self.group.clone())
                .or_insert_with(|| {
                    Arc::new(Mutex::new(BatchGroupInner::<K, I, O> {
                        items: Vec::new(),
                        keys: Vec::new(),
                        waiters: Vec::new(),
                        created_at: Instant::now(),
                        timer_active: false,
                    })) as Arc<dyn std::any::Any + Send + Sync>
                });
            entry
                .value()
                .clone()
                .downcast::<Mutex<BatchGroupInner<K, I, O>>>()
                .expect("batch group type mismatch")
        };

        let (tx, rx) = oneshot::channel::<Result<O>>();

        let (should_flush, should_spawn_timer) = {
            let mut inner = group_state.lock();
            inner.keys.push(key);
            inner.items.push(item);
            inner.waiters.push(tx);

            let info = BatchInfo {
                len: inner.items.len(),
                created_at: inner.created_at,
            };
            let flush_now = (self.predicate)(&info);
            let spawn_timer = !flush_now && !inner.timer_active;
            if spawn_timer {
                inner.timer_active = true;
            }
            (flush_now, spawn_timer)
        };

        if should_flush {
            do_flush(&self.batcher.groups, &self.group, &group_state, f).await;
        } else if should_spawn_timer {
            let groups = self.batcher.groups.clone();
            let group_name = self.group.clone();
            let predicate = self.predicate;
            let group_state = group_state.clone();
            tokio::spawn(async move {
                loop {
                    tokio::time::sleep(Duration::from_millis(10)).await;
                    let should_flush = {
                        let inner = group_state.lock();
                        if inner.items.is_empty() {
                            // Already flushed by a count-triggered submitter
                            break;
                        }
                        let info = BatchInfo {
                            len: inner.items.len(),
                            created_at: inner.created_at,
                        };
                        predicate(&info)
                    };
                    if should_flush {
                        do_flush(&groups, &group_name, &group_state, f).await;
                        break;
                    }
                }
            });
        }

        rx.await.map_err(|_| anyhow::anyhow!("batch group dropped without flushing"))?
    }
}

async fn do_flush<K, I, F, Fut, O>(
    groups: &DashMap<String, Arc<dyn std::any::Any + Send + Sync>>,
    group_name: &str,
    group_state: &Mutex<BatchGroupInner<K, I, O>>,
    f: F,
)
where
    K: Eq + Hash + Clone,
    F: FnOnce(Vec<I>) -> Fut,
    Fut: Future<Output = Result<HashMap<K, O>>> + Send,
    O: Send,
{
    let (items, keys, waiters) = {
        let mut inner = group_state.lock();
        let items = std::mem::take(&mut inner.items);
        let keys = std::mem::take(&mut inner.keys);
        let waiters = std::mem::take(&mut inner.waiters);
        inner.created_at = Instant::now();
        inner.timer_active = false;
        (items, keys, waiters)
    };

    groups.remove(group_name);

    match f(items).await {
        Ok(mut results) => {
            for (key, tx) in keys.into_iter().zip(waiters) {
                match results.remove(&key) {
                    Some(val) => { let _ = tx.send(Ok(val)); }
                    None => { let _ = tx.send(Err(anyhow::anyhow!("batch function did not return result for key"))); }
                }
            }
        }
        Err(e) => {
            let error_msg = e.to_string();
            for tx in waiters {
                let _ = tx.send(Err(anyhow::anyhow!("{}", error_msg)));
            }
        }
    }
}

struct BatchGroupInner<K, I, O> {
    items: Vec<I>,
    keys: Vec<K>,
    waiters: Vec<oneshot::Sender<Result<O>>>,
    created_at: Instant,
    timer_active: bool,
}

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

    #[tokio::test]
    async fn single_item_flushes_immediately() {
        let batcher = Batcher::new();

        let result = batcher
            .submit("test", "key1", 42)
            .flush_when(|batch| batch.len() >= 1)
            .then(|items| async move {
                Ok(items.into_iter().map(|i| ("key1", i * 2)).collect())
            })
            .await
            .unwrap();

        assert_eq!(result, 84);
    }

    #[tokio::test]
    async fn batch_collects_multiple_items() {
        let batcher = Batcher::new();
        let batcher2 = batcher.clone();

        let (a, b) = tokio::join!(
            async {
                batcher
                    .submit("add", 1u32, 10)
                    .flush_when(|batch| batch.len() >= 2)
                    .then(|items| async move {
                        Ok(items.into_iter().enumerate().map(|(i, v)| ((i + 1) as u32, v * 10)).collect())
                    })
                    .await
            },
            async {
                // Small delay so first submit registers the group
                tokio::time::sleep(Duration::from_millis(5)).await;
                batcher2
                    .submit("add", 2u32, 20)
                    .flush_when(|batch| batch.len() >= 2)
                    .then(|items| async move {
                        Ok(items.into_iter().enumerate().map(|(i, v)| ((i + 1) as u32, v * 10)).collect())
                    })
                    .await
            },
        );

        assert_eq!(a.unwrap(), 100);
        assert_eq!(b.unwrap(), 200);
    }

    #[tokio::test]
    async fn batch_error_propagates_to_all_waiters() {
        let batcher = Batcher::new();
        let batcher2 = batcher.clone();

        let (a, b) = tokio::join!(
            async {
                batcher
                    .submit("fail", 1u32, 10)
                    .flush_when(|batch| batch.len() >= 2)
                    .then(|_items: Vec<i32>| async move {
                        Err::<HashMap<u32, i32>, _>(anyhow::anyhow!("batch failed"))
                    })
                    .await
            },
            async {
                tokio::time::sleep(Duration::from_millis(5)).await;
                batcher2
                    .submit("fail", 2u32, 20)
                    .flush_when(|batch| batch.len() >= 2)
                    .then(|_items: Vec<i32>| async move {
                        Err::<HashMap<u32, i32>, _>(anyhow::anyhow!("batch failed"))
                    })
                    .await
            },
        );

        assert!(a.is_err());
        assert!(b.is_err());
    }

    #[tokio::test]
    async fn single_item_flushes_on_timeout() {
        let batcher = Batcher::new();

        // Only age-based flush — no count threshold met.
        // This single item must still flush after the timeout.
        let result = tokio::time::timeout(
            Duration::from_secs(2),
            batcher
                .submit("timeout_test", "k1", 99)
                .flush_when(|batch| batch.len() >= 100 || batch.age() >= Duration::from_millis(50))
                .then(|items| async move {
                    Ok(items.into_iter().map(|v| ("k1", v + 1)).collect())
                }),
        )
        .await
        .expect("should not hang — timeout flush must fire")
        .unwrap();

        assert_eq!(result, 100);
    }

    #[tokio::test]
    async fn different_groups_are_independent() {
        let batcher = Batcher::new();
        let batcher2 = batcher.clone();

        let (a, b) = tokio::join!(
            async {
                batcher
                    .submit("group_a", "k", 100)
                    .flush_when(|batch| batch.len() >= 1)
                    .then(|items| async move {
                        Ok(items.into_iter().map(|v| ("k", v + 1)).collect())
                    })
                    .await
            },
            async {
                batcher2
                    .submit("group_b", "k", 200)
                    .flush_when(|batch| batch.len() >= 1)
                    .then(|items| async move {
                        Ok(items.into_iter().map(|v| ("k", v + 2)).collect())
                    })
                    .await
            },
        );

        assert_eq!(a.unwrap(), 101);
        assert_eq!(b.unwrap(), 202);
    }
}