Skip to content

Commit

Permalink
[ISSUE #2124]💫Add RollbackStats for rust
Browse files Browse the repository at this point in the history
  • Loading branch information
mxsm authored Jan 6, 2025
1 parent f2d2b9d commit d219c8e
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
1 change: 1 addition & 0 deletions rocketmq-remoting/src/protocol/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@

pub mod consume_stats;
pub mod offset_wrapper;
pub mod rollback_stats;
pub mod topic_offset;
pub mod topic_stats_table;
113 changes: 113 additions & 0 deletions rocketmq-remoting/src/protocol/admin/rollback_stats.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use std::fmt::Display;

use cheetah_string::CheetahString;
use serde::Deserialize;
use serde::Serialize;

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct RollbackStats {
pub broker_name: CheetahString,
pub queue_id: i64,
pub broker_offset: i64,
pub consumer_offset: i64,
pub timestamp_offset: i64,
pub rollback_offset: i64,
}

impl Display for RollbackStats {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"RollbackStats [brokerName={}, queueId={}, brokerOffset={}, consumerOffset={}, \
timestampOffset={}, rollbackOffset={}]",
self.broker_name,
self.queue_id,
self.broker_offset,
self.consumer_offset,
self.timestamp_offset,
self.rollback_offset
)
}
}

#[cfg(test)]
mod tests {
use cheetah_string::CheetahString;

use super::*;

#[test]
fn rollback_stats_default_values() {
let stats: RollbackStats = Default::default();
assert_eq!(stats.broker_name, CheetahString::from(""));
assert_eq!(stats.queue_id, 0);
assert_eq!(stats.broker_offset, 0);
assert_eq!(stats.consumer_offset, 0);
assert_eq!(stats.timestamp_offset, 0);
assert_eq!(stats.rollback_offset, 0);
}

#[test]
fn rollback_stats_serialization() {
let stats = RollbackStats {
broker_name: CheetahString::from("broker1"),
queue_id: 1,
broker_offset: 100,
consumer_offset: 200,
timestamp_offset: 300,
rollback_offset: 400,
};
let serialized = serde_json::to_string(&stats).unwrap();
assert_eq!(
serialized,
r#"{"brokerName":"broker1","queueId":1,"brokerOffset":100,"consumerOffset":200,"timestampOffset":300,"rollbackOffset":400}"#
);
}

#[test]
fn rollback_stats_deserialization() {
let json = r#"{"brokerName":"broker1","queueId":1,"brokerOffset":100,"consumerOffset":200,"timestampOffset":300,"rollbackOffset":400}"#;
let deserialized: RollbackStats = serde_json::from_str(json).unwrap();
assert_eq!(deserialized.broker_name, CheetahString::from("broker1"));
assert_eq!(deserialized.queue_id, 1);
assert_eq!(deserialized.broker_offset, 100);
assert_eq!(deserialized.consumer_offset, 200);
assert_eq!(deserialized.timestamp_offset, 300);
assert_eq!(deserialized.rollback_offset, 400);
}

#[test]
fn rollback_stats_display_format() {
let stats = RollbackStats {
broker_name: CheetahString::from("broker1"),
queue_id: 1,
broker_offset: 100,
consumer_offset: 200,
timestamp_offset: 300,
rollback_offset: 400,
};
let display = format!("{}", stats);
assert_eq!(
display,
"RollbackStats [brokerName=broker1, queueId=1, brokerOffset=100, consumerOffset=200, \
timestampOffset=300, rollbackOffset=400]"
);
}
}

0 comments on commit d219c8e

Please sign in to comment.