risingwave_common/session_config/
parallelism.rs

1// Copyright 2025 RisingWave Labs
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::num::{NonZeroU64, ParseIntError};
16use std::str::FromStr;
17
18const KEYWORD_DEFAULT: &str = "default";
19const KEYWORD_ADAPTIVE: &str = "adaptive";
20const KEYWORD_AUTO: &str = "auto";
21
22#[derive(Copy, Default, Debug, Clone, PartialEq, Eq)]
23pub enum ConfigParallelism {
24    #[default]
25    Default,
26    Fixed(NonZeroU64),
27    Adaptive,
28}
29
30impl FromStr for ConfigParallelism {
31    type Err = ParseIntError;
32
33    fn from_str(s: &str) -> Result<Self, Self::Err> {
34        match s.to_lowercase().as_str() {
35            KEYWORD_DEFAULT => Ok(ConfigParallelism::Default),
36            KEYWORD_ADAPTIVE | KEYWORD_AUTO => Ok(ConfigParallelism::Adaptive),
37            s => {
38                let parsed = s.parse::<u64>()?;
39                if parsed == 0 {
40                    Ok(ConfigParallelism::Adaptive)
41                } else {
42                    Ok(ConfigParallelism::Fixed(NonZeroU64::new(parsed).unwrap()))
43                }
44            }
45        }
46    }
47}
48
49impl std::fmt::Display for ConfigParallelism {
50    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
51        match *self {
52            ConfigParallelism::Adaptive => {
53                write!(f, "{}", KEYWORD_ADAPTIVE)
54            }
55            ConfigParallelism::Default => {
56                write!(f, "{}", KEYWORD_DEFAULT)
57            }
58            ConfigParallelism::Fixed(n) => {
59                write!(f, "{}", n)
60            }
61        }
62    }
63}