Skip to main content

risingwave_frontend/utils/
overwrite_options.rs

1// Copyright 2023 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 crate::WithOptions;
16use crate::error::{ErrorCode, Result};
17use crate::handler::HandlerArgs;
18
19/// Some options can be configured both in `WITH` clause and session variables.
20/// The config in `WITH` clause has higher priority.
21#[derive(Debug, Clone, Default)]
22pub struct OverwriteOptions {
23    pub source_rate_limit: Option<u32>,
24    pub backfill_rate_limit: Option<u32>,
25    pub dml_rate_limit: Option<u32>,
26    pub sink_rate_limit: Option<u32>,
27}
28
29impl OverwriteOptions {
30    pub(crate) const BACKFILL_RATE_LIMIT_KEY: &'static str = "backfill_rate_limit";
31    pub(crate) const DML_RATE_LIMIT_KEY: &'static str = "dml_rate_limit";
32    pub(crate) const SINK_RATE_LIMIT_KEY: &'static str = "sink_rate_limit";
33    pub(crate) const SOURCE_RATE_LIMIT_KEY: &'static str = "source_rate_limit";
34
35    pub fn new(args: &mut HandlerArgs) -> Result<Self> {
36        let config = args.session.config();
37        let with_options = &mut args.with_options;
38        Ok(Self {
39            source_rate_limit: Self::take_rate_limit(
40                with_options,
41                Self::SOURCE_RATE_LIMIT_KEY,
42                config.source_rate_limit(),
43            )?,
44            backfill_rate_limit: Self::take_rate_limit(
45                with_options,
46                Self::BACKFILL_RATE_LIMIT_KEY,
47                config.backfill_rate_limit(),
48            )?,
49            dml_rate_limit: Self::take_rate_limit(
50                with_options,
51                Self::DML_RATE_LIMIT_KEY,
52                config.dml_rate_limit(),
53            )?,
54            sink_rate_limit: Self::take_rate_limit(
55                with_options,
56                Self::SINK_RATE_LIMIT_KEY,
57                config.sink_rate_limit(),
58            )?,
59        })
60    }
61
62    /// Takes the rate limit `key` out of the `WITH` clause, falling back to the session variable
63    /// where a negative value disables the rate limit.
64    fn take_rate_limit(
65        with_options: &mut WithOptions,
66        key: &str,
67        session_value: i32,
68    ) -> Result<Option<u32>> {
69        match with_options.remove(key) {
70            Some(value) => value.parse::<u32>().map(Some).map_err(|_| {
71                ErrorCode::InvalidParameterValue(format!(
72                    "`{key}` must be a non-negative integer, but got `{value}`"
73                ))
74                .into()
75            }),
76            None => Ok(u32::try_from(session_value).ok()),
77        }
78    }
79}