Skip to main content

iceberg/util/
mod.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use std::num::NonZeroUsize;
19
20/// Utilities for working with data, metadata, database, table ...etc. locations
21pub mod location;
22/// Utilities for working with snapshots.
23pub mod snapshot;
24
25// Use a default value of 1 as the safest option.
26// See https://doc.rust-lang.org/std/thread/fn.available_parallelism.html#limitations
27// for more details.
28const DEFAULT_PARALLELISM: usize = 1;
29
30/// Uses [`std::thread::available_parallelism`] in order to
31/// retrieve an estimate of the default amount of parallelism
32/// that should be used. Note that [`std::thread::available_parallelism`]
33/// returns a `Result` as it can fail, so here we use
34/// a default value instead.
35/// Note: we don't use a OnceCell or LazyCell here as there
36/// are circumstances where the level of available
37/// parallelism can change during the lifetime of an executing
38/// process, but this should not be called in a hot loop.
39pub(crate) fn available_parallelism() -> NonZeroUsize {
40    std::thread::available_parallelism().unwrap_or_else(|err| {
41        tracing::warn!(
42            error = %err,
43            "Failed to determine available parallelism; falling back to {DEFAULT_PARALLELISM}",
44        );
45        NonZeroUsize::new(DEFAULT_PARALLELISM).unwrap()
46    })
47}