diff --git a/crates/meilisearch/src/lib.rs b/crates/meilisearch/src/lib.rs index 0a5c2f1f5..441da0d7f 100644 --- a/crates/meilisearch/src/lib.rs +++ b/crates/meilisearch/src/lib.rs @@ -506,10 +506,8 @@ fn import_dump( let base_config = index_scheduler.indexer_config(); let indexer_config = if base_config.max_threads.is_none() { - let thread_pool = ThreadPoolNoAbortBuilder::new() - .thread_name(|index| format!("indexing-thread:{index}")) - .num_threads(num_cpus::get()) - .build()?; + let thread_pool = + ThreadPoolNoAbortBuilder::new_for_indexing().num_threads(num_cpus::get()).build()?; let _config = IndexerConfig { thread_pool, ..*base_config }; backup_config = _config; diff --git a/crates/meilisearch/src/option.rs b/crates/meilisearch/src/option.rs index acb4bc05e..d98b9aa8b 100644 --- a/crates/meilisearch/src/option.rs +++ b/crates/meilisearch/src/option.rs @@ -759,12 +759,8 @@ impl TryFrom<&IndexerOpts> for IndexerConfig { type Error = anyhow::Error; fn try_from(other: &IndexerOpts) -> Result { - // use 1/2 cpu threads if no value specified - let max_indexing_threads = other.max_indexing_threads.unwrap_or_else(|| num_cpus::get() / 2); - - let thread_pool = ThreadPoolNoAbortBuilder::new() - .thread_name(|index| format!("indexing-thread:{index}")) - .num_threads(max_indexing_threads) + let thread_pool = ThreadPoolNoAbortBuilder::new_for_indexing() + .num_threads(other.max_indexing_threads.unwrap_or_else(|| num_cpus::get() / 2)) .build()?; Ok(Self { @@ -841,7 +837,7 @@ impl FromStr for MaxThreads { type Err = ParseIntError; fn from_str(s: &str) -> Result { - if s.is_empty() { + if s.is_empty() || s == "unlimited" { return Ok(MaxThreads::default()); } usize::from_str(s).map(Some).map(MaxThreads) diff --git a/crates/milli/src/thread_pool_no_abort.rs b/crates/milli/src/thread_pool_no_abort.rs index b57050a63..0c2fbb30d 100644 --- a/crates/milli/src/thread_pool_no_abort.rs +++ b/crates/milli/src/thread_pool_no_abort.rs @@ -54,6 +54,10 @@ impl ThreadPoolNoAbortBuilder { ThreadPoolNoAbortBuilder::default() } + pub fn new_for_indexing() -> ThreadPoolNoAbortBuilder { + ThreadPoolNoAbortBuilder::default().thread_name(|index| format!("indexing-thread:{index}")) + } + pub fn thread_name(mut self, closure: F) -> Self where F: FnMut(usize) -> String + 'static, diff --git a/crates/milli/src/update/indexer_config.rs b/crates/milli/src/update/indexer_config.rs index 33573aef6..edca71e14 100644 --- a/crates/milli/src/update/indexer_config.rs +++ b/crates/milli/src/update/indexer_config.rs @@ -28,24 +28,29 @@ impl IndexerConfig { } } +/// By default use only 1 thread for indexing in tests +#[cfg(test)] +fn default_thread_pool_and_threads() -> (ThreadPoolNoAbort, Option) { + let pool = ThreadPoolNoAbortBuilder::new_for_indexing() + .num_threads(1) + .build() + .expect("failed to build default rayon thread pool"); + + (pool, Some(1)) +} + +#[cfg(not(test))] +fn default_thread_pool_and_threads() -> (ThreadPoolNoAbort, Option) { + let pool = ThreadPoolNoAbortBuilder::new_for_indexing() + .build() + .expect("failed to build default rayon thread pool"); + + (pool, None) +} + impl Default for IndexerConfig { fn default() -> Self { - #[allow(unused_mut)] - let mut pool_builder = ThreadPoolNoAbortBuilder::new(); - - #[allow(unused_mut, unused_assignments)] - let mut max_threads = None; - - #[cfg(test)] - { - pool_builder = pool_builder.num_threads(1); - max_threads = Some(1); - } - - let thread_pool = pool_builder - .thread_name(|index| format!("indexing-thread:{index}")) - .build() - .expect("failed to build default rayon thread pool"); + let (thread_pool, max_threads) = default_thread_pool_and_threads(); Self { max_threads,