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
32pub type CleanControllerBrokerDataRequestHeader = CleanBrokerDataRequestHeader;
33
34impl Default for CleanBrokerDataRequestHeader {
35    fn default() -> Self {
36        Self {
37            cluster_name: None,
38            broker_name: CheetahString::new(),
39            broker_controller_ids_to_clean: None,
40            clean_living_broker: false,
41            invoke_time: current_millis(),
42        }
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use std::collections::HashMap;
49
50    use super::*;
51    use crate::protocol::command_custom_header::CommandCustomHeader;
52    use crate::protocol::command_custom_header::FromMap;
53
54    #[test]
55    fn clean_broker_data_request_header_serializes_correctly() {
56        let header = CleanBrokerDataRequestHeader {
57            cluster_name: Some(CheetahString::from_static_str("test_cluster")),
58            broker_name: CheetahString::from_static_str("test_broker"),
59            broker_controller_ids_to_clean: Some(CheetahString::from_static_str("1;2")),
60            clean_living_broker: true,
61            invoke_time: 1234567890,
62        };
63
64        let map = header.to_map().unwrap();
65        assert_eq!(
66            map.get(&CheetahString::from_static_str("clusterName")).unwrap(),
67            "test_cluster"
68        );
69        assert_eq!(
70            map.get(&CheetahString::from_static_str("brokerName")).unwrap(),
71            "test_broker"
72        );
73        assert_eq!(
74            map.get(&CheetahString::from_static_str("brokerControllerIdsToClean"))
75                .unwrap(),
76            "1;2"
77        );
78        assert_eq!(
79            map.get(&CheetahString::from_static_str("cleanLivingBroker")).unwrap(),
80            "true"
81        );
82    }
83
84    #[test]
85    fn clean_broker_data_request_header_deserializes_correctly() {
86        let mut map = HashMap::new();
87        map.insert(
88            CheetahString::from_static_str("clusterName"),
89            CheetahString::from_static_str("test_cluster"),
90        );
91        map.insert(
92            CheetahString::from_static_str("brokerName"),
93            CheetahString::from_static_str("test_broker"),
94        );
95        map.insert(
96            CheetahString::from_static_str("brokerControllerIdsToClean"),
97            CheetahString::from_static_str("1;2"),
98        );
99        map.insert(
100            CheetahString::from_static_str("cleanLivingBroker"),
101            CheetahString::from_static_str("false"),
102        );
103        map.insert(
104            CheetahString::from_static_str("invokeTime"),
105            CheetahString::from_static_str("1234567890"),
106        );
107
108        let header = <CleanBrokerDataRequestHeader as FromMap>::from(&map).unwrap();
109        assert_eq!(
110            header.cluster_name,
111            Some(CheetahString::from_static_str("test_cluster"))
112        );
113        assert_eq!(header.broker_name, CheetahString::from_static_str("test_broker"));
114        assert_eq!(
115            header.broker_controller_ids_to_clean,
116            Some(CheetahString::from_static_str("1;2"))
117        );
118        assert!(!header.clean_living_broker);
119        assert_eq!(header.invoke_time, 1234567890);
120    }
121
122    #[test]
123    fn clean_controller_broker_data_request_header_alias_matches_java_name() {
124        let header = CleanControllerBrokerDataRequestHeader {
125            broker_name: CheetahString::from_static_str("broker-a"),
126            ..Default::default()
127        };
128
129        assert_eq!(header.broker_name, "broker-a");
130    }
131}