fgumi 0.2.0

High-performance tools for UMI-tagged sequencing data: extraction, grouping, and consensus calling
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
//! Allocator for distributing sort spill files across multiple temporary directories.
//!
//! `TmpDirAllocator` hands out base directory paths for spill/merged files in a
//! round-robin fashion across one or more user-supplied temp directories. It
//! also supports:
//!
//! - Dropping a directory from the rotation when it runs out of space
//!   (e.g. `ENOSPC` during chunk creation).
//! - Periodic re-checking of free space; directories whose free space falls
//!   below a minimum threshold are dropped from rotation automatically.

use anyhow::{Result, anyhow, bail};
use std::path::{Path, PathBuf};

/// Minimum free bytes a directory must have to remain in rotation.
pub const DEFAULT_MIN_FREE_BYTES: u64 = 1 << 30; // 1 GiB

/// Number of `next()` calls between periodic free-space re-checks.
pub const DEFAULT_RECHECK_INTERVAL: usize = 8;

/// Function that reports free bytes available on a filesystem for writes.
///
/// The default implementation uses [`fs4::available_space`]. Tests inject a
/// closure so they can simulate a filesystem filling up.
pub type FreeSpaceProbe = Box<dyn FnMut(&Path) -> Result<u64> + Send>;

/// Allocator that distributes spill directory assignments across one or more
/// base directories using free-space-aware round-robin.
pub struct TmpDirAllocator {
    /// Directories currently eligible for allocation. Shrinks as dirs are
    /// dropped via `mark_full` or the periodic free-space recheck.
    active: Vec<PathBuf>,
    cursor: usize,
    min_free_bytes: u64,
    recheck_interval: usize,
    calls_since_recheck: usize,
    probe: FreeSpaceProbe,
}

impl TmpDirAllocator {
    /// Create an allocator over the given base directories.
    ///
    /// At least one directory must be supplied. Directories whose initial free
    /// space is below `DEFAULT_MIN_FREE_BYTES` are dropped immediately.
    ///
    /// # Errors
    ///
    /// Returns an error if `dirs` is empty or if no directory has sufficient
    /// free space.
    pub fn new(dirs: Vec<PathBuf>) -> Result<Self> {
        Self::with_probe(dirs, Box::new(default_free_space_probe), DEFAULT_MIN_FREE_BYTES)
    }

    /// Create an allocator with a custom free-space probe and threshold
    /// (primarily for testing).
    ///
    /// `min_free_bytes` is the single threshold used both for the startup
    /// filter and the periodic recheck.
    ///
    /// # Errors
    ///
    /// Returns an error if `dirs` is empty or if every supplied directory
    /// either fails to probe or has insufficient free space.
    pub fn with_probe(
        dirs: Vec<PathBuf>,
        mut probe: FreeSpaceProbe,
        min_free_bytes: u64,
    ) -> Result<Self> {
        if dirs.is_empty() {
            bail!("TmpDirAllocator requires at least one directory");
        }

        let mut active = Vec::with_capacity(dirs.len());
        for dir in dirs {
            match probe(&dir) {
                Ok(free) if free >= min_free_bytes => active.push(dir),
                Ok(free) => log::warn!(
                    "Temp dir {} dropped: only {} free (need {})",
                    dir.display(),
                    free,
                    min_free_bytes
                ),
                Err(e) => log::warn!("Temp dir {} dropped: probe failed: {}", dir.display(), e),
            }
        }

        if active.is_empty() {
            bail!("No temp directory has sufficient free space (need {min_free_bytes} bytes)");
        }

        Ok(Self {
            active,
            cursor: 0,
            min_free_bytes,
            recheck_interval: DEFAULT_RECHECK_INTERVAL,
            calls_since_recheck: 0,
            probe,
        })
    }

    /// Override the periodic recheck interval (primarily for testing).
    #[must_use]
    pub fn with_recheck_interval(mut self, interval: usize) -> Self {
        self.recheck_interval = interval.max(1);
        self
    }

    /// Number of directories currently eligible for allocation.
    #[cfg(test)]
    #[must_use]
    pub fn active_count(&self) -> usize {
        self.active.len()
    }

    /// Return the next base directory in round-robin order.
    ///
    /// # Errors
    ///
    /// Returns an error if all directories have been marked full or dropped
    /// by free-space checks.
    #[allow(clippy::should_implement_trait)] // This is not Iterator::next — it is fallible.
    pub fn next(&mut self) -> Result<PathBuf> {
        self.maybe_recheck();

        if self.active.is_empty() {
            return Err(anyhow!(
                "All temp directories have been exhausted (marked full or below min free space)"
            ));
        }

        // `cursor` is maintained in `[0, active.len())` by every mutation of
        // `active`, so indexing is safe without a modulo.
        let dir = self.active[self.cursor].clone();
        self.cursor = (self.cursor + 1) % self.active.len();
        self.calls_since_recheck = self.calls_since_recheck.saturating_add(1);
        Ok(dir)
    }

    /// Drop a directory from rotation (e.g. after `ENOSPC` during a spill write).
    ///
    /// Matches by path equality. A no-op if the path isn't currently active.
    pub fn mark_full(&mut self, dir: &Path) {
        if let Some(pos) = self.active.iter().position(|d| d == dir) {
            self.active.remove(pos);
            self.rebase_cursor_after_remove(pos);
            log::warn!("Temp dir {} removed from rotation", dir.display());
        }
    }

    /// Shift `cursor` to compensate for an element removed at `removed_pos`,
    /// preserving round-robin order.
    ///
    /// If the removed index was strictly before the cursor, everything after
    /// it shifted down by one so the cursor must follow. If the cursor now
    /// sits past the end of `active`, wrap it back to the front. When
    /// `active` becomes empty the cursor is reset to zero so the next
    /// `next()` call fails cleanly on the length check rather than
    /// mis-indexing.
    fn rebase_cursor_after_remove(&mut self, removed_pos: usize) {
        if self.active.is_empty() {
            self.cursor = 0;
            return;
        }
        if removed_pos < self.cursor {
            self.cursor -= 1;
        }
        if self.cursor >= self.active.len() {
            self.cursor = 0;
        }
    }

    fn maybe_recheck(&mut self) {
        if self.calls_since_recheck < self.recheck_interval {
            return;
        }
        self.calls_since_recheck = 0;

        let min = self.min_free_bytes;
        let mut idx = 0;
        while idx < self.active.len() {
            let drop_dir = match (self.probe)(&self.active[idx]) {
                Ok(free) if free >= min => false,
                Ok(free) => {
                    log::warn!(
                        "Temp dir {} dropped mid-sort: {} free (need {})",
                        self.active[idx].display(),
                        free,
                        min
                    );
                    true
                }
                Err(e) => {
                    log::warn!(
                        "Temp dir {} dropped mid-sort: probe failed: {}",
                        self.active[idx].display(),
                        e
                    );
                    true
                }
            };
            if drop_dir {
                self.active.remove(idx);
                self.rebase_cursor_after_remove(idx);
                // Do not advance `idx`: what was at `idx+1` is now at `idx`.
            } else {
                idx += 1;
            }
        }
    }
}

/// Default probe: number of bytes free on the filesystem backing `path`.
fn default_free_space_probe(path: &Path) -> Result<u64> {
    fs4::available_space(path).map_err(anyhow::Error::from)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::{Arc, Mutex};

    /// Probe that always returns a fixed free-space value.
    fn always_free(bytes: u64) -> FreeSpaceProbe {
        Box::new(move |_| Ok(bytes))
    }

    /// Probe backed by a shared map of path -> free-bytes.
    ///
    /// Used to simulate individual directories filling up between calls.
    fn map_probe(state: Arc<Mutex<std::collections::HashMap<PathBuf, u64>>>) -> FreeSpaceProbe {
        Box::new(move |p: &Path| {
            let guard = state.lock().expect("probe map lock");
            Ok(*guard.get(p).unwrap_or(&u64::MAX))
        })
    }

    #[test]
    fn test_new_empty_errors() {
        let result = TmpDirAllocator::new(vec![]);
        assert!(result.is_err());
    }

    #[test]
    fn test_single_dir_always_returns_same() {
        let dir = PathBuf::from("/tmp/a");
        let mut alloc = TmpDirAllocator::with_probe(
            vec![dir.clone()],
            always_free(u64::MAX),
            DEFAULT_MIN_FREE_BYTES,
        )
        .expect("allocator should be constructable with one dir");
        for _ in 0..5 {
            assert_eq!(alloc.next().expect("next should succeed"), dir);
        }
    }

    #[test]
    fn test_two_dirs_round_robin() {
        let a = PathBuf::from("/tmp/a");
        let b = PathBuf::from("/tmp/b");
        let mut alloc = TmpDirAllocator::with_probe(
            vec![a.clone(), b.clone()],
            always_free(u64::MAX),
            DEFAULT_MIN_FREE_BYTES,
        )
        .expect("two-dir alloc should build");

        let seq: Vec<_> = (0..4).map(|_| alloc.next().expect("next")).collect();
        assert_eq!(seq, vec![a.clone(), b.clone(), a.clone(), b]);
    }

    #[test]
    fn test_mark_full_skips_dir() {
        let a = PathBuf::from("/tmp/a");
        let b = PathBuf::from("/tmp/b");
        let mut alloc = TmpDirAllocator::with_probe(
            vec![a.clone(), b.clone()],
            always_free(u64::MAX),
            DEFAULT_MIN_FREE_BYTES,
        )
        .expect("two-dir alloc should build");

        assert_eq!(alloc.next().expect("next"), a);
        alloc.mark_full(&b);
        // After dropping b, every subsequent next() must be a.
        for _ in 0..5 {
            assert_eq!(alloc.next().expect("next"), a);
        }
        assert_eq!(alloc.active_count(), 1);
    }

    #[test]
    fn test_mark_full_all_exhausts() {
        let a = PathBuf::from("/tmp/a");
        let b = PathBuf::from("/tmp/b");
        let mut alloc = TmpDirAllocator::with_probe(
            vec![a.clone(), b.clone()],
            always_free(u64::MAX),
            DEFAULT_MIN_FREE_BYTES,
        )
        .expect("two-dir alloc should build");

        alloc.mark_full(&a);
        alloc.mark_full(&b);
        assert!(alloc.next().is_err(), "exhausted allocator must error");
    }

    /// Regression: removing a dir at a position strictly before the cursor
    /// must shift the cursor down so round-robin order is preserved.
    #[test]
    fn test_mark_full_preserves_round_robin_order() {
        let a = PathBuf::from("/tmp/a");
        let b = PathBuf::from("/tmp/b");
        let c = PathBuf::from("/tmp/c");
        let mut alloc = TmpDirAllocator::with_probe(
            vec![a.clone(), b.clone(), c.clone()],
            always_free(u64::MAX),
            DEFAULT_MIN_FREE_BYTES,
        )
        .expect("three-dir alloc should build");

        // After handing out `a`, the next-in-rotation is `b`.
        assert_eq!(alloc.next().expect("1"), a);
        // Pruning `a` (pos=0, strictly before cursor=1) must leave `b` next,
        // not `c`.
        alloc.mark_full(&a);
        assert_eq!(alloc.next().expect("2"), b);
        assert_eq!(alloc.next().expect("3"), c);
        assert_eq!(alloc.next().expect("4"), b);
    }

    /// Regression: pruning the dir the cursor points at lets the cursor
    /// naturally move to what was the next element, and wrapping still
    /// works cleanly when that was the tail.
    #[test]
    fn test_mark_full_at_cursor_wraps_cleanly() {
        let a = PathBuf::from("/tmp/a");
        let b = PathBuf::from("/tmp/b");
        let c = PathBuf::from("/tmp/c");
        let mut alloc = TmpDirAllocator::with_probe(
            vec![a.clone(), b.clone(), c.clone()],
            always_free(u64::MAX),
            DEFAULT_MIN_FREE_BYTES,
        )
        .expect("three-dir alloc should build");

        // Hand out a, then b; cursor now points to c (pos=2).
        assert_eq!(alloc.next().expect("1"), a);
        assert_eq!(alloc.next().expect("2"), b);
        // Prune c at pos=2 == cursor. active=[a,b]; cursor=2 is now past the
        // end and must wrap to 0.
        alloc.mark_full(&c);
        assert_eq!(alloc.next().expect("3"), a);
        assert_eq!(alloc.next().expect("4"), b);
    }

    #[test]
    fn test_initial_free_space_filter() {
        // Dir b has insufficient free space at startup → dropped immediately.
        let a = PathBuf::from("/tmp/a");
        let b = PathBuf::from("/tmp/b");

        let mut map = std::collections::HashMap::new();
        map.insert(a.clone(), u64::MAX);
        map.insert(b.clone(), 1024); // tiny
        let state = Arc::new(Mutex::new(map));

        let alloc = TmpDirAllocator::with_probe(
            vec![a.clone(), b.clone()],
            map_probe(state),
            DEFAULT_MIN_FREE_BYTES,
        )
        .expect("should accept at least one valid dir");
        assert_eq!(alloc.active_count(), 1);
    }

    /// The startup filter must honor the `min_free_bytes` argument rather
    /// than the `DEFAULT_MIN_FREE_BYTES` constant.
    #[test]
    fn test_startup_filter_honors_threshold_argument() {
        let a = PathBuf::from("/tmp/a");
        let b = PathBuf::from("/tmp/b");

        // Both dirs report 2048 free, well below the 1 GiB default. A low
        // threshold of 1024 must still admit them.
        let alloc =
            TmpDirAllocator::with_probe(vec![a.clone(), b.clone()], always_free(2048), 1024)
                .expect("low threshold should admit both dirs");
        assert_eq!(alloc.active_count(), 2);
    }

    #[test]
    fn test_periodic_recheck_drops_dir() {
        let a = PathBuf::from("/tmp/a");
        let b = PathBuf::from("/tmp/b");

        let mut map = std::collections::HashMap::new();
        map.insert(a.clone(), u64::MAX);
        map.insert(b.clone(), u64::MAX);
        let state = Arc::new(Mutex::new(map));

        let mut alloc = TmpDirAllocator::with_probe(
            vec![a.clone(), b.clone()],
            map_probe(Arc::clone(&state)),
            DEFAULT_MIN_FREE_BYTES,
        )
        .expect("two-dir alloc should build")
        .with_recheck_interval(2);

        // First two calls round-robin between a and b.
        assert_eq!(alloc.next().expect("1"), a);
        assert_eq!(alloc.next().expect("2"), b);

        // Now b runs out of space. Recheck fires on next call.
        state.lock().expect("lock").insert(b.clone(), 1024);

        // After recheck, only a should remain.
        let next = alloc.next().expect("3");
        assert_eq!(next, a);
        // And subsequent calls stay on a.
        for _ in 0..4 {
            assert_eq!(alloc.next().expect("more"), a);
        }
        assert_eq!(alloc.active_count(), 1);
    }

    /// Regression: when `maybe_recheck` drops a dir at a position strictly
    /// before the cursor, the cursor must shift with it.
    #[test]
    fn test_periodic_recheck_preserves_round_robin_order() {
        let a = PathBuf::from("/tmp/a");
        let b = PathBuf::from("/tmp/b");
        let c = PathBuf::from("/tmp/c");

        let mut map = std::collections::HashMap::new();
        map.insert(a.clone(), u64::MAX);
        map.insert(b.clone(), u64::MAX);
        map.insert(c.clone(), u64::MAX);
        let state = Arc::new(Mutex::new(map));

        let mut alloc = TmpDirAllocator::with_probe(
            vec![a.clone(), b.clone(), c.clone()],
            map_probe(Arc::clone(&state)),
            DEFAULT_MIN_FREE_BYTES,
        )
        .expect("three-dir alloc should build")
        .with_recheck_interval(1);

        // Hand out a; cursor is now 1 pointing at b.
        assert_eq!(alloc.next().expect("1"), a);
        // a runs out of space before the next call.
        state.lock().expect("lock").insert(a.clone(), 1024);

        // Recheck drops a at pos=0 (< cursor=1). Cursor must follow so the
        // next allocation stays on b, not c.
        assert_eq!(alloc.next().expect("2"), b);
        assert_eq!(alloc.next().expect("3"), c);
        assert_eq!(alloc.next().expect("4"), b);
    }

    #[test]
    fn test_all_dirs_below_threshold_errors() {
        let a = PathBuf::from("/tmp/a");
        let result =
            TmpDirAllocator::with_probe(vec![a], always_free(1024), DEFAULT_MIN_FREE_BYTES);
        assert!(result.is_err(), "no dirs with enough free space → error");
    }
}