risingwave_common/system_param/
reader.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright 2024 RisingWave Labs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::borrow::Borrow;

use risingwave_license::LicenseKeyRef;
use risingwave_pb::meta::PbSystemParams;

use super::{default, ParamValue};
use crate::for_all_params;

/// Information about a system parameter.
///
/// Used to display to users through `pg_settings` system table and `SHOW PARAMETERS` command.
pub struct ParameterInfo {
    pub name: &'static str,
    pub mutable: bool,
    /// The [`ToString`] representation of the parameter value.
    ///
    /// Certain parameters, such as `license_key`, may be sensitive, and redaction is applied to them in their newtypes.
    /// As a result, we get the redacted value here for display.
    pub value: String,
    pub description: &'static str,
}

macro_rules! define_system_params_read_trait {
    ($({ $field:ident, $type:ty, $default:expr, $is_mutable:expr, $doc:literal, $($rest:tt)* },)*) => {
        /// The trait delegating reads on [`risingwave_pb::meta::SystemParams`].
        ///
        /// Purposes:
        /// - Avoid misuse of deprecated fields by hiding their getters.
        /// - Abstract fallback logic for fields that might not be provided by meta service due to backward
        ///   compatibility.
        /// - Redact sensitive fields by returning a newtype around the original value.
        pub trait SystemParamsRead {
            $(
                #[doc = $doc]
                fn $field(&self) -> <$type as ParamValue>::Borrowed<'_>;
            )*

            /// Return the information of all parameters.
            fn get_all(&self) -> Vec<ParameterInfo> {
                vec![
                    $(
                        ParameterInfo {
                            name: stringify!($field),
                            mutable: $is_mutable,
                            value: self.$field().to_string(), // use `to_string` to get displayable (maybe redacted) value
                            description: $doc,
                        },
                    )*
                ]
            }
        }
    };
}

for_all_params!(define_system_params_read_trait);

/// The wrapper delegating reads on [`risingwave_pb::meta::SystemParams`].
///
/// See [`SystemParamsRead`] for more details.
// TODO: should we manually impl `PartialEq` by comparing each field with read functions?
#[derive(Clone, PartialEq)]
pub struct SystemParamsReader<I = PbSystemParams> {
    inner: I,
}

// TODO: should ban `Debug` for inner `SystemParams`.
// https://github.com/risingwavelabs/risingwave/pull/17906#discussion_r1705056943
macro_rules! impl_system_params_reader_debug {
    ($({ $field:ident, $($rest:tt)* },)*) => {
        impl<I> std::fmt::Debug for SystemParamsReader<I>
        where
            I: Borrow<PbSystemParams>,
        {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                f.debug_struct("SystemParamsReader")
                    $(
                        .field(stringify!($field), &self.$field())
                    )*
                    .finish()
            }
        }
    };
}

for_all_params!(impl_system_params_reader_debug);

impl<I> From<I> for SystemParamsReader<I>
where
    I: Borrow<PbSystemParams>,
{
    fn from(inner: I) -> Self {
        Self { inner }
    }
}

impl<I> SystemParamsReader<I>
where
    I: Borrow<PbSystemParams>,
{
    pub fn new(inner: I) -> Self {
        Self { inner }
    }

    /// Return a new reader with the reference to the inner system params.
    pub fn as_ref(&self) -> SystemParamsReader<&PbSystemParams> {
        SystemParamsReader {
            inner: self.inner(),
        }
    }

    fn inner(&self) -> &PbSystemParams {
        self.inner.borrow()
    }
}

/// - Unwrap the field if it always exists.
///   For example, if a parameter is introduced before the initial public release.
///
/// - Otherwise, specify the fallback logic when the field is missing.
impl<I> SystemParamsRead for SystemParamsReader<I>
where
    I: Borrow<PbSystemParams>,
{
    fn barrier_interval_ms(&self) -> u32 {
        self.inner().barrier_interval_ms.unwrap()
    }

    fn checkpoint_frequency(&self) -> u64 {
        self.inner().checkpoint_frequency.unwrap()
    }

    fn parallel_compact_size_mb(&self) -> u32 {
        self.inner().parallel_compact_size_mb.unwrap()
    }

    fn sstable_size_mb(&self) -> u32 {
        self.inner().sstable_size_mb.unwrap()
    }

    fn block_size_kb(&self) -> u32 {
        self.inner().block_size_kb.unwrap()
    }

    fn bloom_false_positive(&self) -> f64 {
        self.inner().bloom_false_positive.unwrap()
    }

    fn state_store(&self) -> &str {
        self.inner().state_store.as_ref().unwrap()
    }

    fn data_directory(&self) -> &str {
        self.inner().data_directory.as_ref().unwrap()
    }

    fn use_new_object_prefix_strategy(&self) -> bool {
        *self
            .inner()
            .use_new_object_prefix_strategy
            .as_ref()
            .unwrap()
    }

    fn backup_storage_url(&self) -> &str {
        self.inner().backup_storage_url.as_ref().unwrap()
    }

    fn backup_storage_directory(&self) -> &str {
        self.inner().backup_storage_directory.as_ref().unwrap()
    }

    fn max_concurrent_creating_streaming_jobs(&self) -> u32 {
        self.inner().max_concurrent_creating_streaming_jobs.unwrap()
    }

    fn pause_on_next_bootstrap(&self) -> bool {
        self.inner()
            .pause_on_next_bootstrap
            .unwrap_or_else(default::pause_on_next_bootstrap)
    }

    fn enable_tracing(&self) -> bool {
        self.inner()
            .enable_tracing
            .unwrap_or_else(default::enable_tracing)
    }

    fn license_key(&self) -> LicenseKeyRef<'_> {
        self.inner()
            .license_key
            .as_deref()
            .unwrap_or_default()
            .into()
    }

    fn time_travel_retention_ms(&self) -> u64 {
        self.inner()
            .time_travel_retention_ms
            .unwrap_or_else(default::time_travel_retention_ms)
    }
}