Skip to main content

rocketmq_protocol/protocol/admin/
rollback_stats.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 std::fmt::Display;
16
17use cheetah_string::CheetahString;
18use serde::Deserialize;
19use serde::Serialize;
20
21#[derive(Debug, Clone, Serialize, Deserialize, Default)]
22#[serde(rename_all = "camelCase")]
23pub struct RollbackStats {
24    pub broker_name: CheetahString,
25    pub queue_id: i64,
26    pub broker_offset: i64,
27    pub consumer_offset: i64,
28    pub timestamp_offset: i64,
29    pub rollback_offset: i64,
30}
31
32impl Display for RollbackStats {
33    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34        write!(
35            f,
36            "RollbackStats [brokerName={}, queueId={}, brokerOffset={}, consumerOffset={}, timestampOffset={}, \
37             rollbackOffset={}]",
38            self.broker_name,
39            self.queue_id,
40            self.broker_offset,
41            self.consumer_offset,
42            self.timestamp_offset,
43            self.rollback_offset
44        )
45    }
46}
47
48#[cfg(test)]
49mod tests {
50    use cheetah_string::CheetahString;
51
52    use super::*;
53
54    #[test]
55    fn serde_and_display_preserve_rollback_stats() {
56        let stats = RollbackStats {
57            broker_name: CheetahString::from("broker1"),
58            queue_id: 1,
59            broker_offset: 100,
60            consumer_offset: 200,
61            timestamp_offset: 300,
62            rollback_offset: 400,
63        };
64        let serialized = serde_json::to_string(&stats).unwrap();
65        assert_eq!(
66            serialized,
67            r#"{"brokerName":"broker1","queueId":1,"brokerOffset":100,"consumerOffset":200,"timestampOffset":300,"rollbackOffset":400}"#
68        );
69
70        let deserialized: RollbackStats = serde_json::from_str(&serialized).unwrap();
71        assert_eq!(
72            (
73                deserialized.broker_name.as_str(),
74                deserialized.queue_id,
75                deserialized.broker_offset,
76                deserialized.consumer_offset,
77                deserialized.timestamp_offset,
78                deserialized.rollback_offset,
79            ),
80            ("broker1", 1, 100, 200, 300, 400)
81        );
82        assert_eq!(
83            deserialized.to_string(),
84            "RollbackStats [brokerName=broker1, queueId=1, brokerOffset=100, consumerOffset=200, timestampOffset=300, \
85             rollbackOffset=400]"
86        );
87    }
88}