metric_engine/
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
15use std::time::Duration;
16
17use common_telemetry::warn;
18use serde::{Deserialize, Serialize};
19
20/// The default flush interval of the metadata region.
21pub(crate) const DEFAULT_FLUSH_METADATA_REGION_INTERVAL: Duration = Duration::from_secs(30);
22
23/// Configuration for the metric engine.
24#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
25pub struct EngineConfig {
26    /// Whether to use sparse primary key encoding.
27    #[serde(default = "EngineConfig::default_sparse_primary_key_encoding")]
28    pub sparse_primary_key_encoding: bool,
29    /// The flush interval of the metadata region.
30    #[serde(
31        with = "humantime_serde",
32        default = "EngineConfig::default_flush_metadata_region_interval"
33    )]
34    pub flush_metadata_region_interval: Duration,
35}
36
37impl Default for EngineConfig {
38    fn default() -> Self {
39        Self {
40            flush_metadata_region_interval: DEFAULT_FLUSH_METADATA_REGION_INTERVAL,
41            sparse_primary_key_encoding: Self::default_sparse_primary_key_encoding(),
42        }
43    }
44}
45
46impl EngineConfig {
47    fn default_flush_metadata_region_interval() -> Duration {
48        DEFAULT_FLUSH_METADATA_REGION_INTERVAL
49    }
50
51    fn default_sparse_primary_key_encoding() -> bool {
52        true
53    }
54
55    /// Sanitizes the configuration.
56    pub fn sanitize(&mut self) {
57        if self.flush_metadata_region_interval.is_zero() {
58            warn!(
59                "Flush metadata region interval is zero, override with default value: {:?}. Disable metadata region flush is forbidden.",
60                DEFAULT_FLUSH_METADATA_REGION_INTERVAL
61            );
62            self.flush_metadata_region_interval = DEFAULT_FLUSH_METADATA_REGION_INTERVAL;
63        }
64    }
65}