-
Notifications
You must be signed in to change notification settings - Fork 233
[ISSUE #6533]🚀Add ConsumerStatsManager and related statistics functionality for improved consumer metrics tracking #6534
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+237
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // Copyright 2023 The RocketMQ Rust Authors | ||
| // | ||
| // Licensed 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. | ||
|
|
||
| pub mod consumer_stats_manager; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,213 @@ | ||
| // Copyright 2023 The RocketMQ Rust Authors | ||
| // | ||
| // Licensed 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 rocketmq_common::common::stats::stats_item_set::StatsItemSet; | ||
| use rocketmq_remoting::protocol::body::consume_status::ConsumeStatus; | ||
|
|
||
| const TOPIC_AND_GROUP_CONSUME_OK_TPS: &str = "CONSUME_OK_TPS"; | ||
| const TOPIC_AND_GROUP_CONSUME_FAILED_TPS: &str = "CONSUME_FAILED_TPS"; | ||
| const TOPIC_AND_GROUP_CONSUME_RT: &str = "CONSUME_RT"; | ||
| const TOPIC_AND_GROUP_PULL_TPS: &str = "PULL_TPS"; | ||
| const TOPIC_AND_GROUP_PULL_RT: &str = "PULL_RT"; | ||
|
|
||
| /// Tracks consumer-side statistics for each topic/group pair. | ||
| /// | ||
| /// Maintains five time-windowed metric sets: consume OK TPS, consume failed | ||
| /// TPS, consume RT, pull TPS, and pull RT. All `inc_*` methods are | ||
| /// thread-safe and intended to be called on the hot consumption path. | ||
| pub struct ConsumerStatsManager { | ||
| topic_and_group_consume_ok_tps: StatsItemSet, | ||
| topic_and_group_consume_rt: StatsItemSet, | ||
| topic_and_group_consume_failed_tps: StatsItemSet, | ||
| topic_and_group_pull_tps: StatsItemSet, | ||
| topic_and_group_pull_rt: StatsItemSet, | ||
| } | ||
|
|
||
| impl Default for ConsumerStatsManager { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
|
|
||
| impl ConsumerStatsManager { | ||
| /// Creates a new `ConsumerStatsManager` with all metric sets initialised. | ||
| pub fn new() -> Self { | ||
| Self { | ||
| topic_and_group_consume_ok_tps: StatsItemSet::new(TOPIC_AND_GROUP_CONSUME_OK_TPS.to_string()), | ||
| topic_and_group_consume_rt: StatsItemSet::new(TOPIC_AND_GROUP_CONSUME_RT.to_string()), | ||
| topic_and_group_consume_failed_tps: StatsItemSet::new(TOPIC_AND_GROUP_CONSUME_FAILED_TPS.to_string()), | ||
| topic_and_group_pull_tps: StatsItemSet::new(TOPIC_AND_GROUP_PULL_TPS.to_string()), | ||
| topic_and_group_pull_rt: StatsItemSet::new(TOPIC_AND_GROUP_PULL_RT.to_string()), | ||
| } | ||
| } | ||
|
|
||
| /// Starts the stats manager. Currently a no-op. | ||
| pub fn start(&self) {} | ||
|
|
||
| /// Shuts down the stats manager. Currently a no-op. | ||
| pub fn shutdown(&self) {} | ||
|
|
||
| /// Records a single pull response-time observation in milliseconds. | ||
| pub fn inc_pull_rt(&self, group: &str, topic: &str, rt: u64) { | ||
| self.topic_and_group_pull_rt | ||
| .add_rt_value(&stats_key(topic, group), rt as i32, 1); | ||
| } | ||
|
|
||
| /// Records `msgs` messages successfully pulled in one batch. | ||
| pub fn inc_pull_tps(&self, group: &str, topic: &str, msgs: u64) { | ||
| self.topic_and_group_pull_tps | ||
| .add_value(&stats_key(topic, group), msgs as i32, 1); | ||
| } | ||
|
|
||
| /// Records a single consume response-time observation in milliseconds. | ||
| pub fn inc_consume_rt(&self, group: &str, topic: &str, rt: u64) { | ||
| self.topic_and_group_consume_rt | ||
| .add_rt_value(&stats_key(topic, group), rt as i32, 1); | ||
| } | ||
|
|
||
| /// Records `msgs` messages consumed successfully in one batch. | ||
| pub fn inc_consume_ok_tps(&self, group: &str, topic: &str, msgs: u64) { | ||
| self.topic_and_group_consume_ok_tps | ||
| .add_value(&stats_key(topic, group), msgs as i32, 1); | ||
| } | ||
|
|
||
| /// Records `msgs` messages that failed consumption in one batch. | ||
| pub fn inc_consume_failed_tps(&self, group: &str, topic: &str, msgs: u64) { | ||
| self.topic_and_group_consume_failed_tps | ||
| .add_value(&stats_key(topic, group), msgs as i32, 1); | ||
| } | ||
|
|
||
| /// Returns a point-in-time [`ConsumeStatus`] snapshot for the given | ||
| /// consumer group and topic. | ||
| /// | ||
| /// - Pull / consume RT uses the per-minute average; consume RT falls back to the per-hour | ||
| /// average when the per-minute window is empty. | ||
| /// - `consume_failed_msgs` accumulates the per-hour sum of failed messages. | ||
| pub fn consume_status(&self, group: &str, topic: &str) -> ConsumeStatus { | ||
| let key = stats_key(topic, group); | ||
|
|
||
| let pull_rt = self.topic_and_group_pull_rt.get_stats_data_in_minute(&key).get_avgpt(); | ||
|
|
||
| let pull_tps = self.topic_and_group_pull_tps.get_stats_data_in_minute(&key).get_tps(); | ||
|
|
||
| let consume_rt = { | ||
| let minute = self.topic_and_group_consume_rt.get_stats_data_in_minute(&key); | ||
| if minute.get_sum() == 0 { | ||
| self.topic_and_group_consume_rt.get_stats_data_in_hour(&key).get_avgpt() | ||
| } else { | ||
| minute.get_avgpt() | ||
| } | ||
| }; | ||
|
|
||
| let consume_ok_tps = self | ||
| .topic_and_group_consume_ok_tps | ||
| .get_stats_data_in_minute(&key) | ||
| .get_tps(); | ||
|
|
||
| let consume_failed_tps = self | ||
| .topic_and_group_consume_failed_tps | ||
| .get_stats_data_in_minute(&key) | ||
| .get_tps(); | ||
|
|
||
| let consume_failed_msgs = self | ||
| .topic_and_group_consume_failed_tps | ||
| .get_stats_data_in_hour(&key) | ||
| .get_sum() as i64; | ||
|
|
||
| ConsumeStatus { | ||
| pull_rt, | ||
| pull_tps, | ||
| consume_rt, | ||
| consume_ok_tps, | ||
| consume_failed_tps, | ||
| consume_failed_msgs, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Builds the canonical stats key `"topic@group"` used by all metric sets. | ||
| #[inline] | ||
| fn stats_key(topic: &str, group: &str) -> String { | ||
| format!("{topic}@{group}") | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| fn make_manager() -> ConsumerStatsManager { | ||
| ConsumerStatsManager::new() | ||
| } | ||
|
|
||
| #[test] | ||
| fn stats_key_format() { | ||
| assert_eq!(stats_key("TopicA", "GroupA"), "TopicA@GroupA"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn smoke_inc_consume_ok_tps() { | ||
| let mgr = make_manager(); | ||
| mgr.inc_consume_ok_tps("GroupA", "TopicA", 5); | ||
| } | ||
|
|
||
| #[test] | ||
| fn smoke_inc_consume_failed_tps() { | ||
| let mgr = make_manager(); | ||
| mgr.inc_consume_failed_tps("GroupA", "TopicA", 3); | ||
| } | ||
|
|
||
| #[test] | ||
| fn smoke_inc_consume_rt() { | ||
| let mgr = make_manager(); | ||
| mgr.inc_consume_rt("GroupA", "TopicA", 42); | ||
| } | ||
|
|
||
| #[test] | ||
| fn smoke_inc_pull_rt() { | ||
| let mgr = make_manager(); | ||
| mgr.inc_pull_rt("GroupA", "TopicA", 10); | ||
| } | ||
|
|
||
| #[test] | ||
| fn smoke_inc_pull_tps() { | ||
| let mgr = make_manager(); | ||
| mgr.inc_pull_tps("GroupA", "TopicA", 100); | ||
| } | ||
|
|
||
| #[test] | ||
| fn consume_status_returns_zero_for_empty_stats() { | ||
| let mgr = make_manager(); | ||
| let status = mgr.consume_status("GroupA", "TopicA"); | ||
| assert_eq!(status.pull_rt, 0.0); | ||
| assert_eq!(status.pull_tps, 0.0); | ||
| assert_eq!(status.consume_rt, 0.0); | ||
| assert_eq!(status.consume_ok_tps, 0.0); | ||
| assert_eq!(status.consume_failed_tps, 0.0); | ||
| assert_eq!(status.consume_failed_msgs, 0); | ||
| } | ||
|
|
||
| #[test] | ||
| fn start_and_shutdown_are_no_ops() { | ||
| let mgr = make_manager(); | ||
| mgr.start(); | ||
| mgr.shutdown(); | ||
| } | ||
|
|
||
| #[test] | ||
| fn default_creates_valid_manager() { | ||
| let mgr = ConsumerStatsManager::default(); | ||
| // Should not panic when querying uninitialised key. | ||
| let _ = mgr.consume_status("G", "T"); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potential silent truncation when casting
u64toi32.All
inc_*methods castu64parameters toi32usingas i32. For values exceedingi32::MAX(2,147,483,647), this silently truncates/wraps the value, potentially causing incorrect statistics in high-throughput scenarios.Consider using saturating conversion or validating bounds:
🛡️ Proposed fix using saturating conversion
pub fn inc_pull_rt(&self, group: &str, topic: &str, rt: u64) { + let rt_clamped = rt.min(i32::MAX as u64) as i32; self.topic_and_group_pull_rt - .add_rt_value(&stats_key(topic, group), rt as i32, 1); + .add_rt_value(&stats_key(topic, group), rt_clamped, 1); }Apply similar pattern to other
inc_*methods.📝 Committable suggestion
🤖 Prompt for AI Agents