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 #2339]🤡Add BrokerReplicasInfo for rust🧑‍💻 #2344

Merged
merged 2 commits into from
Jan 19, 2025

Conversation

mxsm
Copy link
Owner

@mxsm mxsm commented Jan 19, 2025

Which Issue(s) This PR Fixes(Closes)

Fixes #2339

Brief Description

How Did You Test This Change?

Summary by CodeRabbit

  • New Features
    • Introduced a new BrokerReplicasInfo struct to manage broker replica information.
    • Added methods to create, modify, and retrieve replica information for brokers.

Copy link
Contributor

coderabbitai bot commented Jan 19, 2025

Walkthrough

The pull request introduces a new struct BrokerReplicasInfo in the RocketMQ remoting protocol module. This struct is designed to manage broker replica information using a HashMap that maps broker names to their corresponding replica details. The implementation provides methods to initialize, add, retrieve, and set replica information, enhancing the module's capability to handle broker replica-related data structures.

Changes

File Change Summary
rocketmq-remoting/src/protocol/body/broker_replicas_info.rs - Added BrokerReplicasInfo struct
- Implemented new() method to create empty struct
- Added add_replica_info() method to insert replica information
- Added get_replicas_info_table() method to access replica info
- Added set_replicas_info_table() method to replace replica info

Assessment against linked issues

Objective Addressed Explanation
[#2339] Add BrokerReplicasInfo for Rust

Possibly related PRs

Suggested reviewers

  • TeslaRustor
  • SpaceXCN
  • rocketmq-rust-bot

Poem

🐰 Hop, hop, through the code we go,
Broker replicas in a row,
HashMap dancing, info so bright,
RocketMQ's structure taking flight! 🚀


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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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-bot
Copy link
Collaborator

🔊@mxsm 🚀Thanks for your contribution🎉!

💡CodeRabbit(AI) will review your code first🔥!

Note

🚨The code review suggestions from CodeRabbit are to be used as a reference only, and the PR submitter can decide whether to make changes based on their own judgment. Ultimately, the project management personnel will conduct the final code review💥.

@rocketmq-rust-bot rocketmq-rust-bot self-requested a review January 19, 2025 09:30
@rocketmq-rust-robot rocketmq-rust-robot added this to the v0.4.0 milestone Jan 19, 2025
@rocketmq-rust-robot rocketmq-rust-robot added the feature🚀 Suggest an idea for this project. label Jan 19, 2025
Copy link

codecov bot commented Jan 19, 2025

Codecov Report

Attention: Patch coverage is 0% with 17 lines in your changes missing coverage. Please review.

Project coverage is 28.46%. Comparing base (8ce52c8) to head (00a9dd1).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...remoting/src/protocol/body/broker_replicas_info.rs 0.00% 17 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2344      +/-   ##
==========================================
- Coverage   28.47%   28.46%   -0.01%     
==========================================
  Files         505      505              
  Lines       72844    72861      +17     
==========================================
  Hits        20741    20741              
- Misses      52103    52120      +17     

☔ 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: 3

🧹 Nitpick comments (3)
rocketmq-remoting/src/protocol/body/broker_replicas_info.rs (3)

35-37: Consider edge case handling in add_replica_info.

The method silently overwrites existing entries. Consider:

  1. Adding a return value to indicate if an entry was overwritten
  2. Adding a separate method for updating existing entries
-    pub fn add_replica_info(&mut self, broker_name: CheetahString, replicas_info: ReplicasInfo) {
-        self.replicas_info_table.insert(broker_name, replicas_info);
+    /// Adds a new replica info entry, returning true if an existing entry was replaced.
+    pub fn add_replica_info(&mut self, broker_name: CheetahString, replicas_info: ReplicasInfo) -> bool {
+        self.replicas_info_table.insert(broker_name, replicas_info).is_some()
     }

39-41: Add documentation for get_replicas_info_table.

+    /// Returns a reference to the internal replicas info table.
     pub fn get_replicas_info_table(&self) -> &HashMap<CheetahString, ReplicasInfo> {
         &self.replicas_info_table
     }

43-48: Add documentation and consider validation for set_replicas_info_table.

+    /// Replaces the entire replicas info table with the provided one.
     pub fn set_replicas_info_table(
         &mut self,
         replicas_info_table: HashMap<CheetahString, ReplicasInfo>,
     ) {
         self.replicas_info_table = replicas_info_table;
     }
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8ce52c8 and bb38795.

📒 Files selected for processing (1)
  • rocketmq-remoting/src/protocol/body/broker_replicas_info.rs (1 hunks)
🧰 Additional context used
🪛 GitHub Actions: CI
rocketmq-remoting/src/protocol/body/broker_replicas_info.rs

[error] 29-33: Missing Default implementation for BrokerReplicasInfo. The struct has a new() method but no Default implementation, which is recommended when implementing new()

⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: test
  • GitHub Check: auto-approve
  • GitHub Check: build

Comment on lines +24 to +26
pub struct BrokerReplicasInfo {
replicas_info_table: HashMap<CheetahString, ReplicasInfo>,
}
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 essential derive macros for BrokerReplicasInfo.

Consider adding the following derive macros to enhance the struct's functionality:

+#[derive(Debug, Clone, Default, Serialize, Deserialize)]
 pub struct BrokerReplicasInfo {
     replicas_info_table: HashMap<CheetahString, ReplicasInfo>,
 }

This will:

  • Enable serialization/deserialization (required for remoting protocol)
  • Add debug representation for logging
  • Implement Clone for data manipulation
  • Add Default implementation (fixes the pipeline failure)
📝 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 BrokerReplicasInfo {
replicas_info_table: HashMap<CheetahString, ReplicasInfo>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct BrokerReplicasInfo {
replicas_info_table: HashMap<CheetahString, ReplicasInfo>,
}

Comment on lines +28 to +33
impl BrokerReplicasInfo {
pub fn new() -> Self {
Self {
replicas_info_table: HashMap::new(),
}
}
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 documentation and implement Default trait.

The implementation should include documentation comments and use the Default trait:

 impl BrokerReplicasInfo {
+    /// Creates a new instance of BrokerReplicasInfo with an empty replicas table.
+    #[inline]
     pub fn new() -> Self {
-        Self {
-            replicas_info_table: HashMap::new(),
-        }
+        Default::default()
     }

Committable suggestion skipped: line range outside the PR's diff.

🧰 Tools
🪛 GitHub Actions: CI

[error] 29-33: Missing Default implementation for BrokerReplicasInfo. The struct has a new() method but no Default implementation, which is recommended when implementing new()

) {
self.replicas_info_table = replicas_info_table;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Codebase verification

Add unit tests for BrokerReplicasInfo struct

The file has comprehensive test coverage for ReplicaIdentity and ReplicasInfo structs, but lacks tests for the BrokerReplicasInfo struct. Please add test cases as suggested in the original review comment to ensure complete coverage of all functionality.

🔗 Analysis chain

Add unit tests for BrokerReplicasInfo.

Please add test cases for the new struct:

#[test]
fn test_broker_replicas_info_operations() {
    let mut info = BrokerReplicasInfo::new();
    let replicas = ReplicasInfo::new(1, "master", 100, 200, vec![], vec![]);
    
    // Test add and get
    assert!(!info.add_replica_info("broker1".into(), replicas.clone()));
    assert_eq!(info.get_replicas_info_table().len(), 1);
    
    // Test overwrite
    assert!(info.add_replica_info("broker1".into(), replicas.clone()));
    
    // Test set_replicas_info_table
    let mut new_table = HashMap::new();
    new_table.insert("broker2".into(), replicas);
    info.set_replicas_info_table(new_table);
    assert_eq!(info.get_replicas_info_table().len(), 1);
    assert!(info.get_replicas_info_table().contains_key("broker2"));
}
🏁 Scripts executed

The following scripts were executed for the analysis:

Error: No shell scripts found


Script:

#!/bin/bash
# Find test files in the protocol/body directory
fd "test" rocketmq-remoting/src/protocol/body

# Search for existing test cases for BrokerReplicasInfo
rg -l "BrokerReplicasInfo" rocketmq-remoting/src/protocol/body

Length of output: 168


Script:

#!/bin/bash
# Check content of the file for test module
cat rocketmq-remoting/src/protocol/body/broker_replicas_info.rs

Length of output: 13592

@rocketmq-rust-bot
Copy link
Collaborator

🔊@mxsm 🚀Thanks for your contribution🎉!

💡CodeRabbit(AI) will review your code first🔥!

Note

🚨The code review suggestions from CodeRabbit are to be used as a reference only, and the PR submitter can decide whether to make changes based on their own judgment. Ultimately, the project management personnel will conduct the final code review💥.

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: 1

♻️ Duplicate comments (1)
rocketmq-remoting/src/protocol/body/broker_replicas_info.rs (1)

24-28: 🛠️ Refactor suggestion

Add essential derive macros and documentation.

The struct is missing important derive macros and documentation:

-#[derive(Serialize, Deserialize, Default)]
+/// Represents information about broker replicas in the RocketMQ system.
+#[derive(Debug, Clone, Serialize, Deserialize, Default)]
 #[serde(rename_all = "camelCase")]
 pub struct BrokerReplicasInfo {
+    /// A mapping of broker names to their corresponding replica information.
+    pub replicas_info_table: HashMap<CheetahString, ReplicasInfo>,
 }
🧹 Nitpick comments (2)
rocketmq-remoting/src/protocol/body/broker_replicas_info.rs (2)

41-43: Add documentation for get_replicas_info_table.

+    /// Returns a reference to the replicas information table.
     pub fn get_replicas_info_table(&self) -> &HashMap<CheetahString, ReplicasInfo> {
         &self.replicas_info_table
     }

45-50: Add documentation for set_replicas_info_table.

+    /// Sets the replicas information table to a new value.
     pub fn set_replicas_info_table(
         &mut self,
         replicas_info_table: HashMap<CheetahString, ReplicasInfo>,
     ) {
         self.replicas_info_table = replicas_info_table;
     }
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bb38795 and 00a9dd1.

📒 Files selected for processing (1)
  • rocketmq-remoting/src/protocol/body/broker_replicas_info.rs (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (9)
  • GitHub Check: build (windows-latest, nightly)
  • GitHub Check: build (windows-latest, stable)
  • GitHub Check: build (macos-latest, nightly)
  • GitHub Check: build (macos-latest, stable)
  • GitHub Check: test
  • GitHub Check: build (ubuntu-latest, nightly)
  • GitHub Check: build
  • GitHub Check: build (ubuntu-latest, stable)
  • GitHub Check: auto-approve
🔇 Additional comments (2)
rocketmq-remoting/src/protocol/body/broker_replicas_info.rs (2)

30-35: Simplify new() implementation using Default.

+/// Implementation of BrokerReplicasInfo methods.
 impl BrokerReplicasInfo {
+    /// Creates a new instance of BrokerReplicasInfo with an empty replicas table.
+    #[inline]
     pub fn new() -> Self {
-        Self {
-            replicas_info_table: HashMap::new(),
-        }
+        Default::default()
     }

51-51: Add unit tests for BrokerReplicasInfo.

The struct lacks test coverage. Please add comprehensive test cases to verify the functionality of all methods.

#!/bin/bash
# Check for existing test cases
rg -l "test.*BrokerReplicasInfo" rocketmq-remoting/src/protocol/body/broker_replicas_info.rs

Comment on lines +37 to +39
pub fn add_replica_info(&mut self, broker_name: CheetahString, replicas_info: ReplicasInfo) {
self.replicas_info_table.insert(broker_name, replicas_info);
}
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

Return bool from add_replica_info to indicate if value was replaced.

The method should return the result of HashMap::insert() to inform callers if a value was replaced.

+    /// Adds or updates replica information for a broker.
+    ///
+    /// # Returns
+    /// Returns true if an existing value was replaced, false otherwise.
     pub fn add_replica_info(&mut self, broker_name: CheetahString, replicas_info: ReplicasInfo) {
-        self.replicas_info_table.insert(broker_name, replicas_info);
+        self.replicas_info_table.insert(broker_name, replicas_info).is_some()
     }
📝 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 fn add_replica_info(&mut self, broker_name: CheetahString, replicas_info: ReplicasInfo) {
self.replicas_info_table.insert(broker_name, replicas_info);
}
/// Adds or updates replica information for a broker.
///
/// # Returns
/// Returns true if an existing value was replaced, false otherwise.
pub fn add_replica_info(&mut self, broker_name: CheetahString, replicas_info: ReplicasInfo) -> bool {
self.replicas_info_table.insert(broker_name, replicas_info).is_some()
}

Copy link
Collaborator

@rocketmq-rust-bot rocketmq-rust-bot left a comment

Choose a reason for hiding this comment

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

LGTM

@rocketmq-rust-bot rocketmq-rust-bot merged commit 6d3bb20 into main Jan 19, 2025
18 of 20 checks passed
@rocketmq-rust-bot rocketmq-rust-bot added approved PR has approved and removed ready to review waiting-review waiting review this PR labels Jan 19, 2025
@mxsm mxsm deleted the feature-2339 branch January 19, 2025 15:00
# 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 BrokerReplicasInfo for rust
3 participants