From e3c27f29b346f28be5e0ee932c26b706d3c575f8 Mon Sep 17 00:00:00 2001 From: mxsm Date: Sat, 4 Jan 2025 15:11:54 +0800 Subject: [PATCH] =?UTF-8?q?[ISSUE=20#2079]=F0=9F=92=ABAdd=20HAConnectionRu?= =?UTF-8?q?ntimeInfo=20for=20rust=F0=9F=9A=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rocketmq-remoting/src/protocol/body.rs | 1 + .../body/ha_connection_runtime_info.rs | 98 +++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 rocketmq-remoting/src/protocol/body/ha_connection_runtime_info.rs diff --git a/rocketmq-remoting/src/protocol/body.rs b/rocketmq-remoting/src/protocol/body.rs index 938c7dfa..c5d995bc 100644 --- a/rocketmq-remoting/src/protocol/body.rs +++ b/rocketmq-remoting/src/protocol/body.rs @@ -33,6 +33,7 @@ pub mod cm_result; pub mod connection; pub mod consume_message_directly_result; pub mod group_list; +pub mod ha_connection_runtime_info; pub mod kv_table; pub mod pop_process_queue_info; pub mod process_queue_info; diff --git a/rocketmq-remoting/src/protocol/body/ha_connection_runtime_info.rs b/rocketmq-remoting/src/protocol/body/ha_connection_runtime_info.rs new file mode 100644 index 00000000..e5e40610 --- /dev/null +++ b/rocketmq-remoting/src/protocol/body/ha_connection_runtime_info.rs @@ -0,0 +1,98 @@ +/* + * 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 serde::Deserialize; +use serde::Serialize; + +#[derive(Debug, Default, Clone, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct HAConnectionRuntimeInfo { + pub addr: String, + pub slave_ack_offset: u64, + pub diff: i64, + pub in_sync: bool, + pub transferred_byte_in_second: u64, + pub transfer_from_where: u64, +} + +impl Display for HAConnectionRuntimeInfo { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "HAConnectionRuntimeInfo [addr={}, slaveAckOffset={}, diff={}, inSync={}, \ + transferredBytesInSecond={}, transferFromWhere={}]", + self.addr, + self.slave_ack_offset, + self.diff, + self.in_sync, + self.transferred_byte_in_second, + self.transfer_from_where + ) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn ha_connection_runtime_info_initializes_correctly() { + let info = HAConnectionRuntimeInfo { + addr: "127.0.0.1:10911".to_string(), + slave_ack_offset: 100, + diff: 50, + in_sync: true, + transferred_byte_in_second: 1024, + transfer_from_where: 200, + }; + assert_eq!(info.addr, "127.0.0.1:10911"); + assert_eq!(info.slave_ack_offset, 100); + assert_eq!(info.diff, 50); + assert!(info.in_sync); + assert_eq!(info.transferred_byte_in_second, 1024); + assert_eq!(info.transfer_from_where, 200); + } + + #[test] + fn ha_connection_runtime_info_default_values() { + let info = HAConnectionRuntimeInfo::default(); + assert_eq!(info.addr, ""); + assert_eq!(info.slave_ack_offset, 0); + assert_eq!(info.diff, 0); + assert!(!info.in_sync); + assert_eq!(info.transferred_byte_in_second, 0); + assert_eq!(info.transfer_from_where, 0); + } + + #[test] + fn ha_connection_runtime_info_display_formats_correctly() { + let info = HAConnectionRuntimeInfo { + addr: "127.0.0.1:10911".to_string(), + slave_ack_offset: 100, + diff: 50, + in_sync: true, + transferred_byte_in_second: 1024, + transfer_from_where: 200, + }; + let display = format!("{}", info); + assert!(display.contains( + "HAConnectionRuntimeInfo [addr=127.0.0.1:10911, slaveAckOffset=100, diff=50, \ + inSync=true, transferredBytesInSecond=1024, transferFromWhere=200]" + )); + } +}