• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

vortex-data / vortex / 16614565710

30 Jul 2025 05:50AM UTC coverage: 82.687% (-0.01%) from 82.699%
16614565710

push

github

web-flow
test: block_in_place (#4059)

Fixes #4045

---------

Signed-off-by: Nicholas Gates <nick@nickgates.com>
Signed-off-by: Alexander Droste <alexander.droste@protonmail.com>
Co-authored-by: Nicholas Gates <nick@nickgates.com>

0 of 1 new or added line in 1 file covered. (0.0%)

8 existing lines in 1 file now uncovered.

45215 of 54682 relevant lines covered (82.69%)

184556.13 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/vortex-scan/src/multi_thread.rs
1
// SPDX-License-Identifier: Apache-2.0
2
// SPDX-FileCopyrightText: Copyright the Vortex contributors
3

4
use std::iter;
5
use std::sync::{Arc, LazyLock};
6

7
use futures::{StreamExt, stream};
8
use tokio::runtime::{Builder, Runtime};
9
use vortex_array::ArrayRef;
10
use vortex_array::iter::{ArrayIterator, ArrayIteratorAdapter};
11
use vortex_error::{VortexExpect, VortexResult, vortex_err};
12

13
use crate::ScanBuilder;
14

15
/// We create an internal Tokio runtime used exclusively for orchestrating work-stealing
16
/// of CPU-bound work for multithreaded scans.
17
///
18
/// It is intentionally not exposed to the user, not configurable, and does not enable I/O or
19
/// timers.
20
static CPU_RUNTIME: LazyLock<Runtime> = LazyLock::new(|| {
×
21
    Builder::new_multi_thread()
×
22
        .thread_name("vortex-multithread-scan")
×
23
        .build()
×
24
        .vortex_expect("Failed to create a new Tokio runtime")
×
25
});
×
26

27
impl ScanBuilder<ArrayRef> {
28
    /// Execute the scan on multiple worker threads.
29
    pub fn into_array_iter_multithread(self) -> VortexResult<impl ArrayIterator + Send + 'static> {
×
30
        let dtype = self.dtype()?;
×
31
        Ok(ArrayIteratorAdapter::new(
×
32
            dtype,
×
33
            self.into_iter_multithread(|a| a)?,
×
34
        ))
35
    }
×
36

37
    /// Execute the scan on multiple worker threads.
38
    ///
39
    /// A `map_fn` can be passed to further transform the results of the scan while still running
40
    /// on the thread pool.
41
    pub fn into_iter_multithread<T, F>(
×
42
        self,
×
43
        map_fn: F,
×
44
    ) -> VortexResult<impl Iterator<Item = T> + Send + 'static>
×
45
    where
×
46
        T: 'static + Send,
×
47
        F: Fn(VortexResult<ArrayRef>) -> T + Send + Sync + 'static,
×
48
    {
49
        let concurrency = self.concurrency;
×
50
        let num_workers = CPU_RUNTIME.metrics().num_workers();
×
51

52
        let tasks = self.build()?;
×
53
        // We need to clone and send the map_fn into each task.
54
        let map_fn = Arc::new(map_fn);
×
55
        let handle = CPU_RUNTIME.handle().clone();
×
56

57
        let mut stream = stream::iter(tasks)
×
58
            .map(move |task| {
×
59
                let map_fn = map_fn.clone();
×
60
                // We don't _need_ to spawn the work here. But it allows Tokio to make progress on
61
                // the tasks in the background, even if the consumer thread is not calling
62
                // poll_next.
63

64
                handle.spawn(async move { task.await.transpose().map(|t| map_fn(t)) })
×
65
            })
×
66
            // TODO(ngates): this is very crude indeed. This buffered call essentially controls how
67
            //  many splits we have in-flight at any given time. We multiple workers by concurrency
68
            //  to configure per-thread concurrency, which essentially means each thread can make
69
            //  progress on one split while waiting for the I/O of another split to complete.
70
            //  In an ideal world, the number of in-flight tasks would be dynamically adjusted
71
            //  based on how much I/O the tasks _actually_ require. For example, all pruning tasks
72
            //  could be spawned immediately since they all use a single segment, this would allow
73
            //  head-room to run ahead and figure out the I/O demands of subsequent tasks.
74
            .buffered(num_workers * concurrency);
×
75

76
        Ok(iter::from_fn(move || {
×
NEW
77
            tokio::task::block_in_place(|| CPU_RUNTIME.handle().block_on(stream.next()))
×
78
        })
×
79
        .filter_map(|result| {
×
80
            result
×
81
                .map_err(|e| vortex_err!("Failed to join on a spawned scan task {e}"))
×
82
                .vortex_expect("Failed to join on a spawned scan task")
×
83
        }))
×
84
    }
×
85
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc