standalone/
options.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
15use common_base::readable_size::ReadableSize;
16use common_config::{Configurable, KvBackendConfig};
17use common_options::memory::MemoryOptions;
18use common_telemetry::logging::{LoggingOptions, SlowQueryOptions, TracingOptions};
19use common_wal::config::DatanodeWalConfig;
20use datanode::config::{DatanodeOptions, ProcedureConfig, RegionEngineConfig, StorageConfig};
21use file_engine::config::EngineConfig as FileEngineConfig;
22use flow::FlowConfig;
23use frontend::frontend::FrontendOptions;
24use frontend::service_config::{
25    InfluxdbOptions, JaegerOptions, MysqlOptions, OpentsdbOptions, PostgresOptions,
26    PromStoreOptions,
27};
28use mito2::config::MitoConfig;
29use query::options::QueryOptions;
30use serde::{Deserialize, Serialize};
31use servers::export_metrics::ExportMetricsOption;
32use servers::grpc::GrpcOptions;
33use servers::http::HttpOptions;
34
35#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
36#[serde(default)]
37pub struct StandaloneOptions {
38    pub enable_telemetry: bool,
39    pub default_timezone: Option<String>,
40    pub http: HttpOptions,
41    pub grpc: GrpcOptions,
42    pub mysql: MysqlOptions,
43    pub postgres: PostgresOptions,
44    pub opentsdb: OpentsdbOptions,
45    pub influxdb: InfluxdbOptions,
46    pub jaeger: JaegerOptions,
47    pub prom_store: PromStoreOptions,
48    pub wal: DatanodeWalConfig,
49    pub storage: StorageConfig,
50    pub metadata_store: KvBackendConfig,
51    pub procedure: ProcedureConfig,
52    pub flow: FlowConfig,
53    pub logging: LoggingOptions,
54    pub user_provider: Option<String>,
55    /// Options for different store engines.
56    pub region_engine: Vec<RegionEngineConfig>,
57    pub export_metrics: ExportMetricsOption,
58    pub tracing: TracingOptions,
59    pub init_regions_in_background: bool,
60    pub init_regions_parallelism: usize,
61    pub max_in_flight_write_bytes: Option<ReadableSize>,
62    pub slow_query: SlowQueryOptions,
63    pub query: QueryOptions,
64    pub memory: MemoryOptions,
65}
66
67impl Default for StandaloneOptions {
68    fn default() -> Self {
69        Self {
70            enable_telemetry: true,
71            default_timezone: None,
72            http: HttpOptions::default(),
73            grpc: GrpcOptions::default(),
74            mysql: MysqlOptions::default(),
75            postgres: PostgresOptions::default(),
76            opentsdb: OpentsdbOptions::default(),
77            influxdb: InfluxdbOptions::default(),
78            jaeger: JaegerOptions::default(),
79            prom_store: PromStoreOptions::default(),
80            wal: DatanodeWalConfig::default(),
81            storage: StorageConfig::default(),
82            metadata_store: KvBackendConfig::default(),
83            procedure: ProcedureConfig::default(),
84            flow: FlowConfig::default(),
85            logging: LoggingOptions::default(),
86            export_metrics: ExportMetricsOption::default(),
87            user_provider: None,
88            region_engine: vec![
89                RegionEngineConfig::Mito(MitoConfig::default()),
90                RegionEngineConfig::File(FileEngineConfig::default()),
91            ],
92            tracing: TracingOptions::default(),
93            init_regions_in_background: false,
94            init_regions_parallelism: 16,
95            max_in_flight_write_bytes: None,
96            slow_query: SlowQueryOptions::default(),
97            query: QueryOptions::default(),
98            memory: MemoryOptions::default(),
99        }
100    }
101}
102
103impl Configurable for StandaloneOptions {
104    fn env_list_keys() -> Option<&'static [&'static str]> {
105        Some(&["wal.broker_endpoints"])
106    }
107}
108
109/// The [`StandaloneOptions`] is only defined in `standalone` crate,
110/// we don't want to make `frontend` depends on it, so impl [`Into`]
111/// rather than [`From`].
112#[allow(clippy::from_over_into)]
113impl Into<FrontendOptions> for StandaloneOptions {
114    fn into(self) -> FrontendOptions {
115        self.frontend_options()
116    }
117}
118
119impl StandaloneOptions {
120    pub fn frontend_options(&self) -> FrontendOptions {
121        let cloned_opts = self.clone();
122        FrontendOptions {
123            default_timezone: cloned_opts.default_timezone,
124            http: cloned_opts.http,
125            grpc: cloned_opts.grpc,
126            mysql: cloned_opts.mysql,
127            postgres: cloned_opts.postgres,
128            opentsdb: cloned_opts.opentsdb,
129            influxdb: cloned_opts.influxdb,
130            jaeger: cloned_opts.jaeger,
131            prom_store: cloned_opts.prom_store,
132            meta_client: None,
133            logging: cloned_opts.logging,
134            user_provider: cloned_opts.user_provider,
135            // Handle the export metrics task run by standalone to frontend for execution
136            export_metrics: cloned_opts.export_metrics,
137            max_in_flight_write_bytes: cloned_opts.max_in_flight_write_bytes,
138            slow_query: cloned_opts.slow_query,
139            ..Default::default()
140        }
141    }
142
143    pub fn datanode_options(&self) -> DatanodeOptions {
144        let cloned_opts = self.clone();
145        DatanodeOptions {
146            node_id: Some(0),
147            enable_telemetry: cloned_opts.enable_telemetry,
148            wal: cloned_opts.wal,
149            storage: cloned_opts.storage,
150            region_engine: cloned_opts.region_engine,
151            grpc: cloned_opts.grpc,
152            init_regions_in_background: cloned_opts.init_regions_in_background,
153            init_regions_parallelism: cloned_opts.init_regions_parallelism,
154            query: cloned_opts.query,
155            ..Default::default()
156        }
157    }
158
159    /// Sanitize the `StandaloneOptions` to ensure the config is valid.
160    pub fn sanitize(&mut self) {
161        if self.storage.is_object_storage() {
162            self.storage
163                .store
164                .cache_config_mut()
165                .unwrap()
166                .sanitize(&self.storage.data_home);
167        }
168    }
169}