datanode/
config.rs

1// Copyright 2023 Greptime Team
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
15//! Datanode configurations
16
17use common_base::readable_size::ReadableSize;
18use common_config::{Configurable, DEFAULT_DATA_HOME};
19use common_options::memory::MemoryOptions;
20pub use common_procedure::options::ProcedureConfig;
21use common_telemetry::logging::{LoggingOptions, TracingOptions};
22use common_wal::config::DatanodeWalConfig;
23use common_workload::{DatanodeWorkloadType, sanitize_workload_types};
24use file_engine::config::EngineConfig as FileEngineConfig;
25use meta_client::MetaClientOptions;
26use metric_engine::config::EngineConfig as MetricEngineConfig;
27use mito2::config::MitoConfig;
28pub(crate) use object_store::config::ObjectStoreConfig;
29use query::options::QueryOptions;
30use serde::{Deserialize, Serialize};
31use servers::export_metrics::ExportMetricsOption;
32use servers::grpc::GrpcOptions;
33use servers::heartbeat_options::HeartbeatOptions;
34use servers::http::HttpOptions;
35
36/// Storage engine config
37#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
38#[serde(default)]
39pub struct StorageConfig {
40    /// The working directory of database
41    pub data_home: String,
42    #[serde(flatten)]
43    pub store: ObjectStoreConfig,
44    /// Object storage providers
45    pub providers: Vec<ObjectStoreConfig>,
46}
47
48impl StorageConfig {
49    /// Returns true when the default storage config is a remote object storage service such as AWS S3, etc.
50    pub fn is_object_storage(&self) -> bool {
51        self.store.is_object_storage()
52    }
53}
54
55impl Default for StorageConfig {
56    fn default() -> Self {
57        Self {
58            data_home: DEFAULT_DATA_HOME.to_string(),
59            store: ObjectStoreConfig::default(),
60            providers: vec![],
61        }
62    }
63}
64
65#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
66#[serde(default)]
67pub struct DatanodeOptions {
68    pub node_id: Option<u64>,
69    pub workload_types: Vec<DatanodeWorkloadType>,
70    pub require_lease_before_startup: bool,
71    pub init_regions_in_background: bool,
72    pub init_regions_parallelism: usize,
73    pub grpc: GrpcOptions,
74    pub heartbeat: HeartbeatOptions,
75    pub http: HttpOptions,
76    pub meta_client: Option<MetaClientOptions>,
77    pub wal: DatanodeWalConfig,
78    pub storage: StorageConfig,
79    pub max_concurrent_queries: usize,
80    /// Options for different store engines.
81    pub region_engine: Vec<RegionEngineConfig>,
82    pub logging: LoggingOptions,
83    pub enable_telemetry: bool,
84    pub export_metrics: ExportMetricsOption,
85    pub tracing: TracingOptions,
86    pub query: QueryOptions,
87    pub memory: MemoryOptions,
88
89    /// Deprecated options, please use the new options instead.
90    #[deprecated(note = "Please use `grpc.addr` instead.")]
91    pub rpc_addr: Option<String>,
92    #[deprecated(note = "Please use `grpc.hostname` instead.")]
93    pub rpc_hostname: Option<String>,
94    #[deprecated(note = "Please use `grpc.runtime_size` instead.")]
95    pub rpc_runtime_size: Option<usize>,
96    #[deprecated(note = "Please use `grpc.max_recv_message_size` instead.")]
97    pub rpc_max_recv_message_size: Option<ReadableSize>,
98    #[deprecated(note = "Please use `grpc.max_send_message_size` instead.")]
99    pub rpc_max_send_message_size: Option<ReadableSize>,
100}
101
102impl DatanodeOptions {
103    /// Sanitize the `DatanodeOptions` to ensure the config is valid.
104    pub fn sanitize(&mut self) {
105        sanitize_workload_types(&mut self.workload_types);
106
107        if self.storage.is_object_storage() {
108            self.storage
109                .store
110                .cache_config_mut()
111                .unwrap()
112                .sanitize(&self.storage.data_home);
113        }
114    }
115}
116
117impl Default for DatanodeOptions {
118    #[allow(deprecated)]
119    fn default() -> Self {
120        Self {
121            node_id: None,
122            workload_types: vec![DatanodeWorkloadType::Hybrid],
123            require_lease_before_startup: false,
124            init_regions_in_background: false,
125            init_regions_parallelism: 16,
126            grpc: GrpcOptions::default().with_bind_addr("127.0.0.1:3001"),
127            http: HttpOptions::default(),
128            meta_client: None,
129            wal: DatanodeWalConfig::default(),
130            storage: StorageConfig::default(),
131            max_concurrent_queries: 0,
132            region_engine: vec![
133                RegionEngineConfig::Mito(MitoConfig::default()),
134                RegionEngineConfig::File(FileEngineConfig::default()),
135            ],
136            logging: LoggingOptions::default(),
137            heartbeat: HeartbeatOptions::datanode_default(),
138            enable_telemetry: true,
139            export_metrics: ExportMetricsOption::default(),
140            tracing: TracingOptions::default(),
141            query: QueryOptions::default(),
142            memory: MemoryOptions::default(),
143
144            // Deprecated options
145            rpc_addr: None,
146            rpc_hostname: None,
147            rpc_runtime_size: None,
148            rpc_max_recv_message_size: None,
149            rpc_max_send_message_size: None,
150        }
151    }
152}
153
154impl Configurable for DatanodeOptions {
155    fn env_list_keys() -> Option<&'static [&'static str]> {
156        Some(&["meta_client.metasrv_addrs", "wal.broker_endpoints"])
157    }
158}
159
160#[allow(clippy::large_enum_variant)]
161#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
162pub enum RegionEngineConfig {
163    #[serde(rename = "mito")]
164    Mito(MitoConfig),
165    #[serde(rename = "file")]
166    File(FileEngineConfig),
167    #[serde(rename = "metric")]
168    Metric(MetricEngineConfig),
169}
170
171#[cfg(test)]
172mod tests {
173    use common_base::secrets::ExposeSecret;
174
175    use super::*;
176
177    #[test]
178    fn test_toml() {
179        let opts = DatanodeOptions::default();
180        let toml_string = toml::to_string(&opts).unwrap();
181        let _parsed: DatanodeOptions = toml::from_str(&toml_string).unwrap();
182    }
183
184    #[test]
185    fn test_secstr() {
186        let toml_str = r#"
187            [storage]
188            type = "S3"
189            access_key_id = "access_key_id"
190            secret_access_key = "secret_access_key"
191        "#;
192        let opts: DatanodeOptions = toml::from_str(toml_str).unwrap();
193        match &opts.storage.store {
194            ObjectStoreConfig::S3(cfg) => {
195                assert_eq!(
196                    "SecretBox<alloc::string::String>([REDACTED])".to_string(),
197                    format!("{:?}", cfg.connection.access_key_id)
198                );
199                assert_eq!(
200                    "access_key_id",
201                    cfg.connection.access_key_id.expose_secret()
202                );
203            }
204            _ => unreachable!(),
205        }
206    }
207    #[test]
208    fn test_skip_ssl_validation_config() {
209        // Test with skip_ssl_validation = true
210        let toml_str_true = r#"
211            [storage]
212            type = "S3"
213            [storage.http_client]
214            skip_ssl_validation = true
215        "#;
216        let opts: DatanodeOptions = toml::from_str(toml_str_true).unwrap();
217        match &opts.storage.store {
218            ObjectStoreConfig::S3(cfg) => {
219                assert!(cfg.http_client.skip_ssl_validation);
220            }
221            _ => panic!("Expected S3 config"),
222        }
223
224        // Test with skip_ssl_validation = false
225        let toml_str_false = r#"
226            [storage]
227            type = "S3"
228            [storage.http_client]
229            skip_ssl_validation = false
230        "#;
231        let opts: DatanodeOptions = toml::from_str(toml_str_false).unwrap();
232        match &opts.storage.store {
233            ObjectStoreConfig::S3(cfg) => {
234                assert!(!cfg.http_client.skip_ssl_validation);
235            }
236            _ => panic!("Expected S3 config"),
237        }
238        // Test default value (should be false)
239        let toml_str_default = r#"
240            [storage]
241            type = "S3"
242        "#;
243        let opts: DatanodeOptions = toml::from_str(toml_str_default).unwrap();
244        match &opts.storage.store {
245            ObjectStoreConfig::S3(cfg) => {
246                assert!(!cfg.http_client.skip_ssl_validation);
247            }
248            _ => panic!("Expected S3 config"),
249        }
250    }
251
252    #[test]
253    fn test_cache_config() {
254        let toml_str = r#"
255            [storage]
256            data_home = "test_data_home"
257            type = "S3"
258            [storage.cache_config]
259            enable_read_cache = true
260        "#;
261        let mut opts: DatanodeOptions = toml::from_str(toml_str).unwrap();
262        opts.sanitize();
263        assert!(opts.storage.store.cache_config().unwrap().enable_read_cache);
264        assert_eq!(
265            opts.storage.store.cache_config().unwrap().cache_path,
266            "test_data_home"
267        );
268    }
269}