Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[ISSUE #1378] Add BrokerStatsData #1636

Merged

Conversation

sainad2222
Copy link
Contributor

@sainad2222 sainad2222 commented Dec 7, 2024

Fixes #1378

Summary by CodeRabbit

  • New Features
    • Introduced a new module for broker statistics, enhancing the RocketMQ protocol.
    • Added BrokerStatsItem struct to encapsulate key broker performance metrics.
    • Added BrokerStatsData struct to manage statistics over different time intervals (minute, hour, day).
  • Tests
    • Implemented unit tests for the new BrokerStatsData struct to ensure functionality and data integrity.

Copy link
Contributor

coderabbitai bot commented Dec 7, 2024

Walkthrough

The changes introduce new modules and data structures in the RocketMQ remoting protocol. Specifically, a module broker_item is added, which includes a struct BrokerStatsItem that encapsulates various statistics. Additionally, a new module broker_stats_data is created, containing the BrokerStatsData struct that holds instances of BrokerStatsItem for different time intervals. Both modules include methods for constructing and manipulating their respective data structures.

Changes

File Path Change Summary
rocketmq-remoting/src/protocol/body.rs Added module: pub mod broker_item;
rocketmq-remoting/src/protocol/body/broker_item.rs Added struct: pub struct BrokerStatsItem with fields and methods for statistics management.
rocketmq-remoting/src/protocol/subscription.rs Added module: pub mod broker_stats_data;
rocketmq-remoting/src/protocol/subscription/broker_stats_data.rs Added struct: pub struct BrokerStatsData with fields and methods for managing BrokerStatsItem.

Assessment against linked issues

Objective Addressed Explanation
Convert BrokerStatsData to Rust struct (#1378)

Possibly related PRs

Suggested reviewers

  • TeslaRustor
  • SpaceXCN

🐇 In the land of code, where structs do play,
A BrokerStatsItem joins the fray.
With stats to gather, both minute and hour,
Our Rusty rabbit builds with power!
So hop along, let’s code and cheer,
For every new module brings us near! 🌟


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 01d247a and bc01677.

📒 Files selected for processing (1)
  • rocketmq-remoting/src/protocol/subscription/broker_stats_data.rs (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • rocketmq-remoting/src/protocol/subscription/broker_stats_data.rs

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@rocketmq-rust-robot rocketmq-rust-robot added the feature🚀 Suggest an idea for this project. label Dec 7, 2024
@rocketmq-rust-robot rocketmq-rust-robot added this to the v0.4.0 milestone Dec 7, 2024
@rocketmq-rust-bot
Copy link
Collaborator

🔊@sainad2222 🚀Thanks for your contribution 🎉. CodeRabbit(AI) will review your code first 🔥

Copy link

codecov bot commented Dec 7, 2024

Codecov Report

Attention: Patch coverage is 90.74074% with 10 lines in your changes missing coverage. Please review.

Project coverage is 26.01%. Comparing base (dc52c0a) to head (bc01677).
Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
rocketmq-remoting/src/protocol/body/broker_item.rs 54.54% 10 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1636      +/-   ##
==========================================
+ Coverage   25.92%   26.01%   +0.08%     
==========================================
  Files         460      462       +2     
  Lines       60879    61060     +181     
==========================================
+ Hits        15784    15882      +98     
- Misses      45095    45178      +83     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Outside diff range and nitpick comments (2)
rocketmq-remoting/src/protocol/body/broker_item.rs (1)

21-26: Add documentation for the BrokerStatsItem struct and its fields

The struct lacks documentation explaining its purpose and the meaning of each field. Consider adding rustdoc comments.

 #[derive(Debug, Clone, Serialize, Deserialize, Default)]
+/// Represents statistical data for a broker over a specific time period
 pub struct BrokerStatsItem {
+    /// Total count of operations
     sum: u64,
+    /// Transactions per second
     tps: f64,
+    /// Average processing time
     avgpt: f64,
 }
rocketmq-remoting/src/protocol/subscription/broker_stats_data.rs (1)

47-116: Add edge cases to test suite

While the test coverage is good, consider adding edge cases:

  • Zero values for all fields
  • Maximum possible values for sum
  • Very small/large values for tps/avgpt

Also consider implementing Default trait:

impl Default for BrokerStatsData {
    fn default() -> Self {
        Self {
            stats_minute: BrokerStatsItem::default(),
            stats_hour: BrokerStatsItem::default(),
            stats_day: BrokerStatsItem::default(),
        }
    }
}
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between ed6ae9c and 01d247a.

📒 Files selected for processing (4)
  • rocketmq-remoting/src/protocol/body.rs (1 hunks)
  • rocketmq-remoting/src/protocol/body/broker_item.rs (1 hunks)
  • rocketmq-remoting/src/protocol/subscription.rs (1 hunks)
  • rocketmq-remoting/src/protocol/subscription/broker_stats_data.rs (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • rocketmq-remoting/src/protocol/body.rs
🔇 Additional comments (1)
rocketmq-remoting/src/protocol/subscription.rs (1)

18-18: LGTM!

The module declaration is correctly placed and follows the existing pattern of module organization.

Comment on lines +30 to +32
pub fn new(sum: u64, tps: f64, avgpt: f64) -> Self {
Self { sum, tps, avgpt }
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add validation for non-negative values in setters

The tps and avgpt fields represent rates and times which should not be negative. Consider adding validation in the setters.

     pub fn set_tps(&mut self, tps: f64) {
+        assert!(tps >= 0.0, "TPS cannot be negative");
         self.tps = tps;
     }

     pub fn set_avgpt(&mut self, avgpt: f64) {
+        assert!(avgpt >= 0.0, "Average processing time cannot be negative");
         self.avgpt = avgpt;
     }

Also applies to: 48-50, 57-59

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Outside diff range and nitpick comments (1)
rocketmq-remoting/src/protocol/body/broker_item.rs (1)

21-26: Add documentation for the struct and its fields

The struct lacks documentation explaining its purpose and the meaning of each field. Consider adding doc comments to improve code maintainability.

Add documentation like this:

+/// Represents statistical data for a broker over a specific time period
+///
+/// # Fields
+/// * `sum` - Total number of operations
+/// * `tps` - Transactions per second
+/// * `avgpt` - Average processing time in milliseconds
 #[derive(Debug, Clone, Serialize, Deserialize, Default)]
 pub struct BrokerStatsItem {
     sum: u64,
     tps: f64,
     avgpt: f64,
 }
🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 21-21: rocketmq-remoting/src/protocol/body/broker_item.rs#L21
Added line #L21 was not covered by tests

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between ed6ae9c and 01d247a.

📒 Files selected for processing (4)
  • rocketmq-remoting/src/protocol/body.rs (1 hunks)
  • rocketmq-remoting/src/protocol/body/broker_item.rs (1 hunks)
  • rocketmq-remoting/src/protocol/subscription.rs (1 hunks)
  • rocketmq-remoting/src/protocol/subscription/broker_stats_data.rs (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • rocketmq-remoting/src/protocol/body.rs
🧰 Additional context used
🪛 GitHub Check: codecov/patch
rocketmq-remoting/src/protocol/body/broker_item.rs

[warning] 21-21: rocketmq-remoting/src/protocol/body/broker_item.rs#L21
Added line #L21 was not covered by tests


[warning] 39-41: rocketmq-remoting/src/protocol/body/broker_item.rs#L39-L41
Added lines #L39 - L41 were not covered by tests


[warning] 48-50: rocketmq-remoting/src/protocol/body/broker_item.rs#L48-L50
Added lines #L48 - L50 were not covered by tests


[warning] 57-59: rocketmq-remoting/src/protocol/body/broker_item.rs#L57-L59
Added lines #L57 - L59 were not covered by tests

🔇 Additional comments (2)
rocketmq-remoting/src/protocol/subscription.rs (1)

18-18: LGTM!

The module declaration follows the existing pattern and is correctly placed among other subscription-related modules.

rocketmq-remoting/src/protocol/subscription/broker_stats_data.rs (1)

47-115: LGTM! Comprehensive test coverage

The test module provides thorough coverage of all functionality, including:

  • Initial values verification
  • Setter method verification for all time periods
  • Proper cloning and value comparison

Comment on lines +48 to +50
pub fn set_tps(&mut self, tps: f64) {
self.tps = tps;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add validation for non-negative values

The setter methods for tps and avgpt should validate that the values are non-negative, as negative values don't make sense for these metrics.

Consider adding validation:

 pub fn set_tps(&mut self, tps: f64) {
+    assert!(tps >= 0.0, "TPS cannot be negative");
     self.tps = tps;
 }

 pub fn set_avgpt(&mut self, avgpt: f64) {
+    assert!(avgpt >= 0.0, "Average processing time cannot be negative");
     self.avgpt = avgpt;
 }

Also applies to: 57-59

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 48-50: rocketmq-remoting/src/protocol/body/broker_item.rs#L48-L50
Added lines #L48 - L50 were not covered by tests

Comment on lines +39 to +41
pub fn set_sum(&mut self, sum: u64) {
self.sum = sum;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add tests for setter methods

The static analysis indicates that the setter methods lack test coverage.

Add tests to verify the behavior of setters, including validation of non-negative values:

#[test]
fn test_setters() {
    let mut item = BrokerStatsItem::default();
    
    item.set_sum(100);
    assert_eq!(item.get_sum(), 100);
    
    item.set_tps(15.5);
    assert_eq!(item.get_tps(), 15.5);
    
    item.set_avgpt(8.3);
    assert_eq!(item.get_avgpt(), 8.3);
}

#[test]
#[should_panic(expected = "TPS cannot be negative")]
fn test_negative_tps_validation() {
    let mut item = BrokerStatsItem::default();
    item.set_tps(-1.0);
}

#[test]
#[should_panic(expected = "Average processing time cannot be negative")]
fn test_negative_avgpt_validation() {
    let mut item = BrokerStatsItem::default();
    item.set_avgpt(-1.0);
}

Also applies to: 48-50, 57-59

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 39-41: rocketmq-remoting/src/protocol/body/broker_item.rs#L39-L41
Added lines #L39 - L41 were not covered by tests

Comment on lines 3 to 7
pub struct BrokerStatsData {
stats_minute: BrokerStatsItem,
stats_hour: BrokerStatsItem,
stats_day: BrokerStatsItem,
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add derive traits and documentation

The struct should derive common traits and include documentation explaining its purpose and fields.

Add derives and documentation:

+/// Represents broker statistics data over different time periods
+///
+/// Holds statistical data for minute, hour, and day intervals using
+/// BrokerStatsItem instances for each period.
+#[derive(Debug, Clone, Default)]
 pub struct BrokerStatsData {
     stats_minute: BrokerStatsItem,
     stats_hour: BrokerStatsItem,
     stats_day: BrokerStatsItem,
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
pub struct BrokerStatsData {
stats_minute: BrokerStatsItem,
stats_hour: BrokerStatsItem,
stats_day: BrokerStatsItem,
}
/// Represents broker statistics data over different time periods
///
/// Holds statistical data for minute, hour, and day intervals using
/// BrokerStatsItem instances for each period.
#[derive(Debug, Clone, Default)]
pub struct BrokerStatsData {
stats_minute: BrokerStatsItem,
stats_hour: BrokerStatsItem,
stats_day: BrokerStatsItem,
}

Copy link
Owner

@mxsm mxsm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sainad2222 Thanks for your contribution. LGTM

@rocketmq-rust-bot
Copy link
Collaborator

🔊@sainad2222 🚀Thanks for your contribution 🎉. CodeRabbit(AI) will review your code first 🔥

@mxsm mxsm added the approved PR has approved label Dec 7, 2024
@rocketmq-rust-bot rocketmq-rust-bot merged commit 8801bad into mxsm:main Dec 7, 2024
16 of 18 checks passed
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
AI review first Ai review pr first approved PR has approved auto merge feature🚀 Suggest an idea for this project.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature🚀] Add BrokerStatsData
4 participants