Skip to main content

rocketmq_protocol/protocol/header/namesrv/
brokerid_change_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_macros::RequestHeaderCodec;
17use serde::Deserialize;
18use serde::Serialize;
19
20#[derive(Debug, Clone, Serialize, Deserialize, Default, RequestHeaderCodec)]
21#[header(
22    type_id = "rocketmq_protocol::protocol::header::namesrv::brokerid_change_request_header::NotifyMinBrokerIdChangeRequestHeader",
23    java_class = "org.apache.rocketmq.remoting.protocol.header.NotifyMinBrokerIdChangeRequestHeader"
24)]
25pub struct NotifyMinBrokerIdChangeRequestHeader {
26    #[serde(rename = "minBrokerId")]
27    #[header(range = "i64")]
28    pub min_broker_id: Option<u64>,
29
30    #[serde(rename = "brokerName")]
31    pub broker_name: Option<CheetahString>,
32
33    #[serde(rename = "minBrokerAddr")]
34    pub min_broker_addr: Option<CheetahString>,
35
36    #[serde(rename = "offlineBrokerAddr")]
37    pub offline_broker_addr: Option<CheetahString>,
38
39    #[serde(rename = "haBrokerAddr")]
40    pub ha_broker_addr: Option<CheetahString>,
41}
42
43impl NotifyMinBrokerIdChangeRequestHeader {
44    pub fn new(
45        min_broker_id: Option<u64>,
46        broker_name: Option<CheetahString>,
47        min_broker_addr: Option<CheetahString>,
48        offline_broker_addr: Option<CheetahString>,
49        ha_broker_addr: Option<CheetahString>,
50    ) -> Self {
51        NotifyMinBrokerIdChangeRequestHeader {
52            min_broker_id,
53            broker_name,
54            min_broker_addr,
55            offline_broker_addr,
56            ha_broker_addr,
57        }
58    }
59}
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64
65    #[test]
66    fn serde_preserves_the_broker_change_wire_fields() {
67        let header = NotifyMinBrokerIdChangeRequestHeader::new(
68            Some(1),
69            Some(CheetahString::from("broker-a")),
70            Some(CheetahString::from("127.0.0.1:10911")),
71            Some(CheetahString::from("127.0.0.2:10911")),
72            Some(CheetahString::from("127.0.0.1:10912")),
73        );
74
75        let json = serde_json::to_string(&header).unwrap();
76        let decoded: NotifyMinBrokerIdChangeRequestHeader = serde_json::from_str(&json).unwrap();
77
78        assert_eq!(decoded.min_broker_id, Some(1));
79        assert_eq!(decoded.broker_name.as_deref(), Some("broker-a"));
80        assert_eq!(decoded.min_broker_addr.as_deref(), Some("127.0.0.1:10911"));
81        assert_eq!(decoded.offline_broker_addr.as_deref(), Some("127.0.0.2:10911"));
82        assert_eq!(decoded.ha_broker_addr.as_deref(), Some("127.0.0.1:10912"));
83
84        let empty = NotifyMinBrokerIdChangeRequestHeader::new(None, None, None, None, None);
85        assert!(empty.min_broker_id.is_none());
86        assert!(empty.broker_name.is_none());
87        assert!(empty.min_broker_addr.is_none());
88        assert!(empty.offline_broker_addr.is_none());
89        assert!(empty.ha_broker_addr.is_none());
90    }
91}