Skip to main content

rocketmq_protocol/protocol/body/
controller_write_lease.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 serde::Deserialize;
16use serde::Serialize;
17
18/// Rust-native Controller response that authorizes one Broker master to write.
19///
20/// This body is intentionally not part of the Java Controller wire contract.
21/// It is returned only after the corresponding heartbeat has been committed by
22/// the Rust Controller quorum.
23#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
24#[serde(rename_all = "camelCase")]
25pub struct ControllerWriteLeaseGrant {
26    pub broker_id: i64,
27    pub master_epoch: i32,
28    pub generation: u64,
29    pub lease_duration_millis: u64,
30    pub safety_margin_millis: u64,
31}
32
33impl ControllerWriteLeaseGrant {
34    pub const DEFAULT_LEASE_DURATION_MILLIS: u64 = 10_000;
35    pub const DEFAULT_SAFETY_MARGIN_MILLIS: u64 = 2_000;
36}
37
38#[cfg(test)]
39mod tests {
40    use super::ControllerWriteLeaseGrant;
41
42    #[test]
43    fn grant_uses_stable_camel_case_wire_fields() {
44        let grant = ControllerWriteLeaseGrant {
45            broker_id: 0,
46            master_epoch: 3,
47            generation: 7,
48            lease_duration_millis: 10_000,
49            safety_margin_millis: 2_000,
50        };
51
52        let json = serde_json::to_string(&grant).expect("grant should serialize");
53        assert!(json.contains("\"brokerId\":0"));
54        assert!(json.contains("\"masterEpoch\":3"));
55        assert_eq!(serde_json::from_str::<ControllerWriteLeaseGrant>(&json).unwrap(), grant);
56    }
57}