Skip to main content

common_wal/
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
15pub mod kafka;
16
17use serde::{Deserialize, Serialize};
18use serde_with::with_prefix;
19
20pub use crate::options::kafka::KafkaWalOptions;
21
22/// An encoded wal options will be wrapped into a (WAL_OPTIONS_KEY, encoded wal options) key-value pair
23/// and inserted into the options of a `RegionCreateRequest`.
24pub const WAL_OPTIONS_KEY: &str = "wal_options";
25
26/// Wal options allocated to a region.
27/// A wal options is encoded by metasrv with `serde_json::to_string`, and then decoded
28/// by datanode with `serde_json::from_str`.
29#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
30#[serde(tag = "wal.provider", rename_all = "snake_case")]
31pub enum WalOptions {
32    #[default]
33    RaftEngine,
34    #[serde(with = "kafka_prefix")]
35    Kafka(KafkaWalOptions),
36    Noop,
37}
38
39with_prefix!(kafka_prefix "wal.kafka.");
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44
45    #[test]
46    fn test_serde_wal_options() {
47        // Test serde raft-engine wal options.
48        let wal_options = WalOptions::RaftEngine;
49        let encoded = serde_json::to_string(&wal_options).unwrap();
50        let expected = r#"{"wal.provider":"raft_engine"}"#;
51        assert_eq!(&encoded, expected);
52
53        let decoded: WalOptions = serde_json::from_str(&encoded).unwrap();
54        assert_eq!(decoded, wal_options);
55
56        // Test serde kafka wal options.
57        let wal_options = WalOptions::Kafka(KafkaWalOptions::new("test_topic".to_string()));
58        let encoded = serde_json::to_string(&wal_options).unwrap();
59        let expected = r#"{"wal.provider":"kafka","wal.kafka.topic":"test_topic"}"#;
60        assert_eq!(&encoded, expected);
61
62        let decoded: WalOptions = serde_json::from_str(&encoded).unwrap();
63        assert_eq!(decoded, wal_options);
64
65        let wal_options = WalOptions::Kafka(KafkaWalOptions {
66            topic: "test_topic".to_string(),
67            initial_pruned_entry_id: Some(42),
68        });
69        let encoded = serde_json::to_string(&wal_options).unwrap();
70        let expected = r#"{"wal.provider":"kafka","wal.kafka.topic":"test_topic","wal.kafka.initial_pruned_entry_id":42}"#;
71        assert_eq!(&encoded, expected);
72
73        let decoded: WalOptions = serde_json::from_str(&encoded).unwrap();
74        assert_eq!(decoded, wal_options);
75
76        let decoded: WalOptions =
77            serde_json::from_str(r#"{"wal.provider":"kafka","wal.kafka.topic":"test_topic"}"#)
78                .unwrap();
79        assert_eq!(
80            decoded,
81            WalOptions::Kafka(KafkaWalOptions::new("test_topic".to_string()))
82        );
83
84        // Test serde noop wal options.
85        let wal_options = WalOptions::Noop;
86        let encoded = serde_json::to_string(&wal_options).unwrap();
87        let expected = r#"{"wal.provider":"noop"}"#;
88        assert_eq!(&encoded, expected);
89
90        let decoded: WalOptions = serde_json::from_str(&encoded).unwrap();
91        assert_eq!(decoded, wal_options);
92    }
93}