Skip to main content

store_api/
mito_engine_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
15//! Option keys for the mito engine.
16//! We define them in this mod so the create parser can use it to validate table options.
17
18use std::collections::HashMap;
19
20/// Option key for all WAL options.
21pub use common_wal::options::WAL_OPTIONS_KEY;
22/// Option key for append mode.
23pub const APPEND_MODE_KEY: &str = "append_mode";
24/// Option key for the per-region write buffer size.
25pub const WRITE_BUFFER_SIZE_KEY: &str = "write_buffer_size";
26/// Option key for merge mode.
27pub const MERGE_MODE_KEY: &str = "merge_mode";
28/// Option key for TTL(time-to-live)
29pub const TTL_KEY: &str = "ttl";
30/// Option key for the per-table auto flush interval.
31pub const AUTO_FLUSH_INTERVAL_KEY: &str = "auto_flush_interval";
32/// Option key for compaction type.
33pub const COMPACTION_TYPE: &str = "compaction.type";
34/// Option key for forcing compaction options override.
35pub const COMPACTION_OVERRIDE: &str = "compaction.override";
36/// TWCS compaction strategy.
37pub const COMPACTION_TYPE_TWCS: &str = "twcs";
38/// Prefix for TWCS compaction options.
39pub const COMPACTION_TWCS_PREFIX: &str = "compaction.twcs.";
40/// Option key for twcs min file num to trigger a compaction.
41pub const TWCS_TRIGGER_FILE_NUM: &str = "compaction.twcs.trigger_file_num";
42/// Option key for twcs min file num to trigger compaction in the active window.
43pub const TWCS_ACTIVE_WINDOW_TRIGGER_FILE_NUM: &str =
44    "compaction.twcs.active_window.trigger_file_num";
45/// Option key for the active-window L1 safety compaction threshold.
46pub const TWCS_ACTIVE_WINDOW_L1_MERGE_TRIGGER: &str =
47    "compaction.twcs.active_window.l1_merge_trigger";
48/// Option key for twcs min file num to trigger compaction in an inactive window.
49pub const TWCS_INACTIVE_WINDOW_TRIGGER_FILE_NUM: &str =
50    "compaction.twcs.inactive_window.trigger_file_num";
51/// Option key for the inactive-window L1 compaction threshold.
52pub const TWCS_INACTIVE_WINDOW_L1_MERGE_TRIGGER: &str =
53    "compaction.twcs.inactive_window.l1_merge_trigger";
54/// Option key for twcs max output file size.
55pub const TWCS_MAX_OUTPUT_FILE_SIZE: &str = "compaction.twcs.max_output_file_size";
56/// Option key for twcs time window.
57pub const TWCS_TIME_WINDOW: &str = "compaction.twcs.time_window";
58/// Option key for twcs remote compaction.
59pub const TWCS_REMOTE_COMPACTION: &str = "compaction.twcs.remote_compaction";
60/// Option key for twcs fallback to local.
61pub const TWCS_FALLBACK_TO_LOCAL: &str = "compaction.twcs.fallback_to_local";
62/// Table options that configure compaction.
63pub const COMPACTION_OPTION_KEYS: [&str; 11] = [
64    COMPACTION_TYPE,
65    COMPACTION_OVERRIDE,
66    TWCS_TRIGGER_FILE_NUM,
67    TWCS_ACTIVE_WINDOW_TRIGGER_FILE_NUM,
68    TWCS_ACTIVE_WINDOW_L1_MERGE_TRIGGER,
69    TWCS_INACTIVE_WINDOW_TRIGGER_FILE_NUM,
70    TWCS_INACTIVE_WINDOW_L1_MERGE_TRIGGER,
71    TWCS_MAX_OUTPUT_FILE_SIZE,
72    TWCS_TIME_WINDOW,
73    TWCS_REMOTE_COMPACTION,
74    TWCS_FALLBACK_TO_LOCAL,
75];
76/// Option key for memtable type.
77pub const MEMTABLE_TYPE: &str = "memtable.type";
78/// Option key for bulk memtable merge threshold.
79pub const MEMTABLE_BULK_MERGE_THRESHOLD: &str = "memtable.bulk.merge_threshold";
80/// Option key for bulk memtable encode row threshold.
81pub const MEMTABLE_BULK_ENCODE_ROW_THRESHOLD: &str = "memtable.bulk.encode_row_threshold";
82/// Option key for bulk memtable encode bytes threshold.
83pub const MEMTABLE_BULK_ENCODE_BYTES_THRESHOLD: &str = "memtable.bulk.encode_bytes_threshold";
84/// Option key for bulk memtable max merge groups.
85pub const MEMTABLE_BULK_MAX_MERGE_GROUPS: &str = "memtable.bulk.max_merge_groups";
86/// Option key for memtable partition tree index max keys per shard.
87pub const MEMTABLE_PARTITION_TREE_INDEX_MAX_KEYS_PER_SHARD: &str =
88    "memtable.partition_tree.index_max_keys_per_shard";
89/// Option key for memtable partition tree data freeze threshold.
90pub const MEMTABLE_PARTITION_TREE_DATA_FREEZE_THRESHOLD: &str =
91    "memtable.partition_tree.data_freeze_threshold";
92/// Option key for memtable partition tree fork dictionary bytes.
93pub const MEMTABLE_PARTITION_TREE_FORK_DICTIONARY_BYTES: &str =
94    "memtable.partition_tree.fork_dictionary_bytes";
95/// Option key for skipping WAL.
96pub const SKIP_WAL_KEY: &str = "skip_wal";
97/// Option key for sst format.
98pub const SST_FORMAT_KEY: &str = "sst_format";
99/// Option key for the max number of rows in a parquet row group.
100pub const MAX_ROW_GROUP_ROW_COUNT: &str = "max_row_group_row_count";
101/// Upper bound for [`MAX_ROW_GROUP_ROW_COUNT`].
102pub const MAX_ROW_GROUP_ROW_COUNT_LIMIT: usize = 10 * 1024 * 1024;
103/// Option key for preserving per-row sequence numbers through flush and compaction.
104pub const PRESERVE_ROW_SEQUENCE: &str = "preserve_row_sequence";
105/// Option key for experimental Parquet float field encoding.
106pub const EXPERIMENTAL_SST_FLOAT_FIELD_ENCODING: &str = "experimental_sst_float_field_encoding";
107
108/// Encoding policy for direct floating-point field columns in Parquet SSTs.
109#[derive(
110    Debug,
111    Default,
112    Clone,
113    Copy,
114    PartialEq,
115    Eq,
116    Hash,
117    serde::Serialize,
118    serde::Deserialize,
119    strum::EnumString,
120)]
121#[serde(rename_all = "snake_case")]
122#[strum(serialize_all = "snake_case")]
123pub enum FloatFieldEncoding {
124    /// The existing Parquet writer behavior.
125    #[default]
126    Default,
127    /// Parquet byte-stream-split encoding.
128    ByteStreamSplit,
129}
130// Note: Adding new options here should also check if this option should be removed in [metric_engine::engine::create::region_options_for_metadata_region].
131
132/// Conflicting values supplied through the legacy and canonical TWCS trigger options.
133#[derive(Debug, PartialEq, Eq)]
134pub struct TwcsTriggerOptionConflict {
135    /// Value supplied under [`TWCS_TRIGGER_FILE_NUM`].
136    pub legacy_value: String,
137    /// Value supplied under [`TWCS_ACTIVE_WINDOW_TRIGGER_FILE_NUM`].
138    pub canonical_value: String,
139}
140
141/// Normalizes the active-window TWCS trigger option to its legacy storage key.
142pub fn normalize_twcs_trigger_options(
143    options: &mut HashMap<String, String>,
144) -> Result<(), TwcsTriggerOptionConflict> {
145    let Some(canonical_value) = options.get(TWCS_ACTIVE_WINDOW_TRIGGER_FILE_NUM).cloned() else {
146        return Ok(());
147    };
148    if let Some(legacy_value) = options.get(TWCS_TRIGGER_FILE_NUM)
149        && legacy_value != &canonical_value
150    {
151        return Err(TwcsTriggerOptionConflict {
152            legacy_value: legacy_value.clone(),
153            canonical_value,
154        });
155    }
156
157    options.remove(TWCS_ACTIVE_WINDOW_TRIGGER_FILE_NUM);
158    options.insert(TWCS_TRIGGER_FILE_NUM.to_string(), canonical_value);
159    Ok(())
160}
161
162/// Returns true if the `key` is a valid option key for the mito engine.
163pub fn is_mito_engine_option_key(key: &str) -> bool {
164    COMPACTION_OPTION_KEYS.contains(&key)
165        || [
166            "ttl",
167            WRITE_BUFFER_SIZE_KEY,
168            AUTO_FLUSH_INTERVAL_KEY,
169            "storage",
170            "index.inverted_index.ignore_column_ids",
171            "index.inverted_index.segment_row_count",
172            WAL_OPTIONS_KEY,
173            MEMTABLE_TYPE,
174            MEMTABLE_BULK_MERGE_THRESHOLD,
175            MEMTABLE_BULK_ENCODE_ROW_THRESHOLD,
176            MEMTABLE_BULK_ENCODE_BYTES_THRESHOLD,
177            MEMTABLE_BULK_MAX_MERGE_GROUPS,
178            MEMTABLE_PARTITION_TREE_INDEX_MAX_KEYS_PER_SHARD,
179            MEMTABLE_PARTITION_TREE_DATA_FREEZE_THRESHOLD,
180            MEMTABLE_PARTITION_TREE_FORK_DICTIONARY_BYTES,
181            // We don't allow to create a mito table with sparse primary key encoding directly.
182            APPEND_MODE_KEY,
183            MERGE_MODE_KEY,
184            SST_FORMAT_KEY,
185            MAX_ROW_GROUP_ROW_COUNT,
186            PRESERVE_ROW_SEQUENCE,
187            EXPERIMENTAL_SST_FLOAT_FIELD_ENCODING,
188        ]
189        .contains(&key)
190}
191
192#[cfg(test)]
193mod tests {
194    use std::collections::HashMap;
195
196    use super::*;
197
198    #[test]
199    fn test_is_mito_engine_option_key() {
200        assert!(is_mito_engine_option_key("ttl"));
201        assert!(is_mito_engine_option_key("write_buffer_size"));
202        assert!(is_mito_engine_option_key("auto_flush_interval"));
203        assert!(is_mito_engine_option_key("compaction.type"));
204        assert!(is_mito_engine_option_key("compaction.override"));
205        assert!(is_mito_engine_option_key(
206            "compaction.twcs.trigger_file_num"
207        ));
208        assert!(is_mito_engine_option_key(
209            "compaction.twcs.active_window.trigger_file_num"
210        ));
211        assert!(is_mito_engine_option_key(
212            "compaction.twcs.active_window.l1_merge_trigger"
213        ));
214        assert!(is_mito_engine_option_key(
215            "compaction.twcs.inactive_window.trigger_file_num"
216        ));
217        assert!(is_mito_engine_option_key(
218            "compaction.twcs.inactive_window.l1_merge_trigger"
219        ));
220        assert!(is_mito_engine_option_key("compaction.twcs.time_window"));
221        assert!(is_mito_engine_option_key("storage"));
222        assert!(is_mito_engine_option_key(
223            "index.inverted_index.ignore_column_ids"
224        ));
225        assert!(is_mito_engine_option_key(
226            "index.inverted_index.segment_row_count"
227        ));
228        assert!(is_mito_engine_option_key("wal_options"));
229        assert!(is_mito_engine_option_key("memtable.type"));
230        assert!(is_mito_engine_option_key("memtable.bulk.merge_threshold"));
231        assert!(is_mito_engine_option_key(
232            "memtable.bulk.encode_row_threshold"
233        ));
234        assert!(is_mito_engine_option_key(
235            "memtable.bulk.encode_bytes_threshold"
236        ));
237        assert!(is_mito_engine_option_key("memtable.bulk.max_merge_groups"));
238        assert!(is_mito_engine_option_key(
239            "memtable.partition_tree.index_max_keys_per_shard"
240        ));
241        assert!(is_mito_engine_option_key(
242            "memtable.partition_tree.data_freeze_threshold"
243        ));
244        assert!(is_mito_engine_option_key(
245            "memtable.partition_tree.fork_dictionary_bytes"
246        ));
247        assert!(is_mito_engine_option_key("append_mode"));
248        assert!(is_mito_engine_option_key("max_row_group_row_count"));
249        assert!(is_mito_engine_option_key("preserve_row_sequence"));
250        assert!(is_mito_engine_option_key(
251            EXPERIMENTAL_SST_FLOAT_FIELD_ENCODING
252        ));
253        assert!(!is_mito_engine_option_key("foo"));
254    }
255
256    #[test]
257    fn test_float_field_encoding_values() {
258        assert_eq!(
259            "default".parse::<FloatFieldEncoding>(),
260            Ok(FloatFieldEncoding::Default)
261        );
262        assert_eq!(
263            "byte_stream_split".parse::<FloatFieldEncoding>(),
264            Ok(FloatFieldEncoding::ByteStreamSplit)
265        );
266        assert_eq!(
267            serde_json::from_str::<FloatFieldEncoding>("\"byte_stream_split\"").unwrap(),
268            FloatFieldEncoding::ByteStreamSplit
269        );
270        assert!(serde_json::from_str::<FloatFieldEncoding>("\"unknown\"").is_err());
271    }
272
273    #[test]
274    fn test_normalize_twcs_trigger_aliases_to_legacy_key() {
275        let expected = HashMap::from([(TWCS_TRIGGER_FILE_NUM.to_string(), "4".to_string())]);
276        for mut options in [
277            HashMap::from([(TWCS_TRIGGER_FILE_NUM.to_string(), "4".to_string())]),
278            HashMap::from([(
279                TWCS_ACTIVE_WINDOW_TRIGGER_FILE_NUM.to_string(),
280                "4".to_string(),
281            )]),
282            HashMap::from([
283                (TWCS_TRIGGER_FILE_NUM.to_string(), "4".to_string()),
284                (
285                    TWCS_ACTIVE_WINDOW_TRIGGER_FILE_NUM.to_string(),
286                    "4".to_string(),
287                ),
288            ]),
289        ] {
290            normalize_twcs_trigger_options(&mut options).unwrap();
291            assert_eq!(expected, options);
292        }
293    }
294
295    #[test]
296    fn test_normalize_twcs_trigger_conflicting_aliases() {
297        let mut options = HashMap::from([
298            (TWCS_TRIGGER_FILE_NUM.to_string(), "4".to_string()),
299            (
300                TWCS_ACTIVE_WINDOW_TRIGGER_FILE_NUM.to_string(),
301                "8".to_string(),
302            ),
303        ]);
304        let original = options.clone();
305
306        let error = normalize_twcs_trigger_options(&mut options).unwrap_err();
307
308        assert_eq!("4", error.legacy_value);
309        assert_eq!("8", error.canonical_value);
310        assert_eq!(original, options);
311    }
312}