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 {
58            topic: "test_topic".to_string(),
59        });
60        let encoded = serde_json::to_string(&wal_options).unwrap();
61        let expected = r#"{"wal.provider":"kafka","wal.kafka.topic":"test_topic"}"#;
62        assert_eq!(&encoded, expected);
63
64        let decoded: WalOptions = serde_json::from_str(&encoded).unwrap();
65        assert_eq!(decoded, wal_options);
66
67        // Test serde noop wal options.
68        let wal_options = WalOptions::Noop;
69        let encoded = serde_json::to_string(&wal_options).unwrap();
70        let expected = r#"{"wal.provider":"noop"}"#;
71        assert_eq!(&encoded, expected);
72
73        let decoded: WalOptions = serde_json::from_str(&encoded).unwrap();
74        assert_eq!(decoded, wal_options);
75    }
76}