Skip to main content

rocketmq_remoting/protocol/header/controller/
clean_broker_data_request_header.rs

1// Copyright 2023 The RocketMQ Rust Authors
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 cheetah_string::CheetahString;
16use rocketmq_common::TimeUtils::current_millis;
17use rocketmq_macros::RequestHeaderCodecV2;
18use serde::Deserialize;
19use serde::Serialize;
20
21#[derive(Clone, Debug, Serialize, Deserialize, RequestHeaderCodecV2)]
22#[serde(rename_all = "camelCase")]
23pub struct CleanBrokerDataRequestHeader {
24    pub cluster_name: Option<CheetahString>,
25    #[required]
26    pub broker_name: CheetahString,
27    pub broker_controller_ids_to_clean: Option<CheetahString>,
28    pub clean_living_broker: bool,
29    pub invoke_time: u64,
30}
31
32impl Default for CleanBrokerDataRequestHeader {
33    fn default() -> Self {
34        Self {
35            cluster_name: None,
36            broker_name: CheetahString::new(),
37            broker_controller_ids_to_clean: None,
38            clean_living_broker: false,
39            invoke_time: current_millis(),
40        }
41    }
42}
43
44#[cfg(test)]
45mod tests {
46    use std::collections::HashMap;
47
48    use super::*;
49    use crate::protocol::command_custom_header::CommandCustomHeader;
50    use crate::protocol::command_custom_header::FromMap;
51
52    #[test]
53    fn clean_broker_data_request_header_serializes_correctly() {
54        let header = CleanBrokerDataRequestHeader {
55            cluster_name: Some(CheetahString::from_static_str("test_cluster")),
56            broker_name: CheetahString::from_static_str("test_broker"),
57            broker_controller_ids_to_clean: Some(CheetahString::from_static_str("1;2")),
58            clean_living_broker: true,
59            invoke_time: 1234567890,
60        };
61
62        let map = header.to_map().unwrap();
63        assert_eq!(
64            map.get(&CheetahString::from_static_str("clusterName")).unwrap(),
65            "test_cluster"
66        );
67        assert_eq!(
68            map.get(&CheetahString::from_static_str("brokerName")).unwrap(),
69            "test_broker"
70        );
71        assert_eq!(
72            map.get(&CheetahString::from_static_str("brokerControllerIdsToClean"))
73                .unwrap(),
74            "1;2"
75        );
76        assert_eq!(
77            map.get(&CheetahString::from_static_str("cleanLivingBroker")).unwrap(),
78            "true"
79        );
80    }
81
82    #[test]
83    fn clean_broker_data_request_header_deserializes_correctly() {
84        let mut map = HashMap::new();
85        map.insert(
86            CheetahString::from_static_str("clusterName"),
87            CheetahString::from_static_str("test_cluster"),
88        );
89        map.insert(
90            CheetahString::from_static_str("brokerName"),
91            CheetahString::from_static_str("test_broker"),
92        );
93        map.insert(
94            CheetahString::from_static_str("brokerControllerIdsToClean"),
95            CheetahString::from_static_str("1;2"),
96        );
97        map.insert(
98            CheetahString::from_static_str("cleanLivingBroker"),
99            CheetahString::from_static_str("false"),
100        );
101        map.insert(
102            CheetahString::from_static_str("invokeTime"),
103            CheetahString::from_static_str("1234567890"),
104        );
105
106        let header = <CleanBrokerDataRequestHeader as FromMap>::from(&map).unwrap();
107        assert_eq!(
108            header.cluster_name,
109            Some(CheetahString::from_static_str("test_cluster"))
110        );
111        assert_eq!(header.broker_name, CheetahString::from_static_str("test_broker"));
112        assert_eq!(
113            header.broker_controller_ids_to_clean,
114            Some(CheetahString::from_static_str("1;2"))
115        );
116        assert!(!header.clean_living_broker);
117        assert_eq!(header.invoke_time, 1234567890);
118    }
119}