Skip to main content

rocketmq_protocol/protocol/header/
update_broker_config_request_header.rs

1// Copyright 2026 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 rocketmq_macros::RequestHeaderCodec;
16use serde::Deserialize;
17use serde::Serialize;
18
19/// Compare-and-set precondition for `UpdateBrokerConfigCas`.
20///
21/// Legacy `UpdateBrokerConfig` requests do not use this header. Keeping CAS on
22/// a distinct request code ensures an older broker rejects the operation
23/// instead of ignoring an unknown header and applying it unconditionally.
24#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Serialize, Deserialize, RequestHeaderCodec)]
25#[header(
26    type_id = "rocketmq_protocol::protocol::header::update_broker_config_request_header::UpdateBrokerConfigRequestHeader"
27)]
28#[serde(rename_all = "camelCase")]
29pub struct UpdateBrokerConfigRequestHeader {
30    #[header(default, default_semantic = "literal:0")]
31    pub expected_generation: u64,
32}
33
34#[cfg(test)]
35mod tests {
36    use std::collections::HashMap;
37
38    use cheetah_string::CheetahString;
39
40    use super::UpdateBrokerConfigRequestHeader;
41    use crate::protocol::command_custom_header::CommandCustomHeader;
42    use crate::protocol::command_custom_header::FromMap;
43
44    #[test]
45    fn expected_generation_round_trips_through_wire_map() {
46        let header = UpdateBrokerConfigRequestHeader {
47            expected_generation: 42,
48        };
49        let map = header.to_map().expect("header should encode");
50        assert_eq!(map.get("expectedGeneration").map(CheetahString::as_str), Some("42"));
51
52        let decoded = <UpdateBrokerConfigRequestHeader as FromMap>::from(&map).expect("header should decode");
53        assert_eq!(decoded, header);
54    }
55
56    #[test]
57    fn missing_expected_generation_decodes_to_invalid_zero() {
58        let map = HashMap::new();
59        let decoded = <UpdateBrokerConfigRequestHeader as FromMap>::from(&map).expect("wire decoder uses the default");
60        assert_eq!(decoded.expected_generation, 0);
61    }
62}