Skip to main content

feldera_storage/
tokio.rs

1use once_cell::sync::Lazy;
2use std::{env, thread};
3use tokio::runtime::{Builder, Runtime};
4use tracing::debug;
5
6/// The process running dbsp can share a single Tokio runtime.
7///
8/// This is `pub` so it can be used in dbsp and adapters too for IO.
9pub static TOKIO: Lazy<Runtime> = Lazy::new(|| {
10    debug!(
11        "starting service dbsp io tokio runtime, workers: {}",
12        env::var("TOKIO_WORKER_THREADS")
13            .unwrap_or_else(|_| { thread::available_parallelism().unwrap().get().to_string() })
14    );
15    Builder::new_multi_thread()
16        .thread_name_fn(|| {
17            use std::sync::atomic::{AtomicUsize, Ordering};
18            static ATOMIC_ID: AtomicUsize = AtomicUsize::new(0);
19            let id = ATOMIC_ID.fetch_add(1, Ordering::SeqCst);
20            format!("feldera-tokio-{}", id)
21        })
22        .thread_stack_size(6 * 1024 * 1024)
23        .enable_all()
24        .build()
25        .unwrap()
26});
27
28/// Some connectors (currently only delta) need a dedicated runtime that only runs IO operations.
29/// This runtime can be used for this purpose. It should not be use for any CPU-intensive tasks
30/// such as parsing.
31pub static TOKIO_DEDICATED_IO: Lazy<Runtime> = Lazy::new(|| {
32    debug!(
33        "starting service dedicated io tokio runtime, workers: {}",
34        env::var("TOKIO_WORKER_THREADS")
35            .unwrap_or_else(|_| { thread::available_parallelism().unwrap().get().to_string() })
36    );
37    Builder::new_multi_thread()
38        .thread_name_fn(|| {
39            use std::sync::atomic::{AtomicUsize, Ordering};
40            static ATOMIC_ID: AtomicUsize = AtomicUsize::new(0);
41            let id = ATOMIC_ID.fetch_add(1, Ordering::SeqCst);
42            format!("feldera-io-tokio-{}", id)
43        })
44        .thread_stack_size(6 * 1024 * 1024)
45        .enable_all()
46        .build()
47        .unwrap()
48});