Repository: meilisearch/meilisearch
File: crates/meilisearch/src/lib.rs (Rust)

Code before:
    // 2. Import the `Key`s.
    let mut keys = Vec::new();
    auth.raw_delete_all_keys()?;
    for key in dump_reader.keys()? {
        let key = key?;
        auth.raw_insert_key(key.clone())?;
        keys.push(key);
    }

    // 3. Import the runtime features and network
    let features = dump_reader.features()?.unwrap_or_default();
    index_scheduler.put_runtime_features(features)?;

    let network = dump_reader.network()?.cloned().unwrap_or_default();
    index_scheduler.put_network(network)?;

    let mut indexer_config = IndexerConfig::clone_no_threadpool(index_scheduler.indexer_config());

    // 3.1 Use all cpus to index the import dump
    indexer_config.thread_pool = {
        let all_cpus = num_cpus::get();

        let temp_pool = ThreadPoolNoAbortBuilder::new()
            .thread_name(|index| format!("indexing-thread:{index}"))
            .num_threads(all_cpus)
            .build()?;

        Some(temp_pool)
    };

    // /!\ The tasks must be imported AFTER importing the indexes or else the scheduler might
    // try to process tasks while we're trying to import the indexes.

    // 4. Import the indexes.
    for index_reader in dump_reader.indexes()? {
        let mut index_reader = index_reader?;
        let metadata = index_reader.metadata();
        let uid = metadata.uid.clone();
        tracing::info!("Importing index `{}`.", metadata.uid);

        let date = Some((metadata.created_at, metadata.updated_at));
        let index = index_scheduler.create_raw_index(&metadata.uid, date)?;

        let mut wtxn = index.write_txn()?;

        let mut builder = milli::update::Settings::new(&mut wtxn, &index, &indexer_config);
        // 4.1 Import the primary key if there is one.
        if let Some(ref primary_key) = metadata.primary_key {
            builder.set_primary_key(primary_key.to_string());
        }


Review comment (suggestion):
Sorry about the misunderstanding, but when I was talking about using all the CPUs, I meant all the CPUs that were defined through the settings. This means that you'll need to use `Opt` to get the `max-index-threads` value for the `ThreadPoolNoAbortBuilder::num_threads` or don't call this method if `max-index-threads` is not set 🙏

Code after:
    auth.raw_delete_all_keys()?;
    for key in dump_reader.keys()? {
        let key = key?;
        auth.raw_insert_key(key.clone())?;
        keys.push(key);
    }

    // 3. Import the runtime features and network
    let features = dump_reader.features()?.unwrap_or_default();
    index_scheduler.put_runtime_features(features)?;

    let network = dump_reader.network()?.cloned().unwrap_or_default();
    index_scheduler.put_network(network)?;

    // 3.1 Use all cpus to process dump if `max_indexing_threads` not configured
    let backup_config;
    let base_config = index_scheduler.indexer_config();

    let indexer_config = if base_config.max_threads.is_none() {
        let (thread_pool, _) = default_thread_pool_and_threads();

        let _config = IndexerConfig { thread_pool, ..*base_config };
        backup_config = _config;
        &backup_config
    } else {
        base_config
    };

    // /!\ The tasks must be imported AFTER importing the indexes or else the scheduler might
    // try to process tasks while we're trying to import the indexes.

    // 4. Import the indexes.
    for index_reader in dump_reader.indexes()? {
        let mut index_reader = index_reader?;
        let metadata = index_reader.metadata();
        let uid = metadata.uid.clone();
        tracing::info!("Importing index `{}`.", metadata.uid);

        let date = Some((metadata.created_at, metadata.updated_at));
        let index = index_scheduler.create_raw_index(&metadata.uid, date)?;

        let mut wtxn = index.write_txn()?;

        let mut builder = milli::update::Settings::new(&mut wtxn, &index, indexer_config);
        // 4.1 Import the primary key if there is one.
        if let Some(ref primary_key) = metadata.primary_key {
            builder.set_primary_key(primary_key.to_string());
        }

        // 4.2 Import the settings.
        tracing::info!("Importing the settings.");