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 #1829]🌽Add BatchAckMessageRequestBody struct for rust🚀 #1832

Merged
merged 1 commit into from
Dec 17, 2024

Conversation

mxsm
Copy link
Owner

@mxsm mxsm commented Dec 17, 2024

Which Issue(s) This PR Fixes(Closes)

Fixes #1829

Brief Description

How Did You Test This Change?

Summary by CodeRabbit

  • New Features

    • Introduced a new public module for batch acknowledgment messages.
    • Added a BatchAckMessageRequestBody struct for handling batch acknowledgment requests.
  • Bug Fixes

    • Implemented unit tests for serialization, default values, and edge cases of the new struct to ensure data integrity.
  • Documentation

    • Enhanced module visibility and structure for better accessibility and clarity.

Copy link
Contributor

coderabbitai bot commented Dec 17, 2024

Walkthrough

The pull request introduces a new public module batch_ack_message_request_body in the RocketMQ remoting protocol implementation. The changes modify the module visibility in the body.rs file, making the batch_ack module public and adding a new module for batch acknowledgment message request body. A new Rust file is created to define the BatchAckMessageRequestBody struct with serialization support and accompanying unit tests.

Changes

File Change Summary
rocketmq-remoting/src/protocol/body.rs - Changed batch_ack module from private to public
- Added new public module batch_ack_message_request_body
rocketmq-remoting/src/protocol/body/batch_ack_message_request_body.rs - Created new file with BatchAckMessageRequestBody struct
- Added serialization support with Serde
- Implemented unit tests for serialization and edge cases

Assessment against linked issues

Objective Addressed Explanation
Add BatchAckMessageRequestBody struct [#1829]

Possibly related PRs

Suggested reviewers

  • TeslaRustor
  • SpaceXCN
  • RocketmqRustBot

Poem

🚀 Batch acks dancing light,
Rust modules taking flight,
Serialization's embrace,
Protocols finding their place,
RocketMQ's code shines bright! 🐰


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 🔥

@rocketmq-rust-robot rocketmq-rust-robot added feature🚀 Suggest an idea for this project. rust Pull requests that update Rust code labels Dec 17, 2024
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

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 20892bc and 9a589d7.

📒 Files selected for processing (2)
  • rocketmq-remoting/src/protocol/body.rs (1 hunks)
  • rocketmq-remoting/src/protocol/body/batch_ack_message_request_body.rs (1 hunks)
🔇 Additional comments (4)
rocketmq-remoting/src/protocol/body.rs (1)

26-27: LGTM! Module declarations are well-organized.

The new module declarations maintain consistent visibility and alphabetical ordering with existing modules.

rocketmq-remoting/src/protocol/body/batch_ack_message_request_body.rs (3)

17-28: LGTM! Well-structured struct definition with appropriate serialization.

The struct follows Rust best practices with:

  • Proper use of derive macros for Debug and serialization
  • Consistent field naming and types
  • Appropriate JSON serialization configuration

37-58: LGTM! Comprehensive serialization test coverage.

The test effectively verifies serialization/deserialization and field value preservation.


60-68: LGTM! Good coverage of default values.

The test properly verifies initialization with empty values.

Comment on lines +70 to +89
#[test]
fn batch_ack_message_request_body_edge_case_empty_strings() {
let body = BatchAckMessageRequestBody {
broker_name: CheetahString::from(""),
acks: vec![BatchAck {
consumer_group: String::from(""),
topic: String::from(""),
retry: String::from(""),
start_offset: -1,
queue_id: -1,
revive_queue_id: -1,
pop_time: -1,
invisible_time: -1,
bit_set: BitVec::new(),
}],
};
assert_eq!(body.broker_name, CheetahString::from(""));
assert_eq!(body.acks.len(), 1);
assert_eq!(body.acks[0].consumer_group, "");
}
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

🛠️ Refactor suggestion

Based on the complete code review, I can now generate the final response:

Validation is needed for negative values in BatchAck struct

The BatchAck struct currently accepts negative values for numeric fields without any validation, as evidenced by both the struct definition and test cases. While the test batch_ack_edge_case_empty_strings shows that negative values are technically accepted, these fields represent queue positions, timestamps, and offsets which should logically be non-negative:

  • start_offset: Queue message offset should be non-negative
  • queue_id: Queue identifier should be non-negative
  • revive_queue_id: Queue identifier should be non-negative
  • pop_time: Timestamp should be non-negative
  • invisible_time: Duration should be non-negative

Add validation by implementing:

impl BatchAck {
    pub fn validate(&self) -> Result<(), String> {
        if self.start_offset < 0 {
            return Err("start_offset cannot be negative".to_string());
        }
        if self.queue_id < 0 {
            return Err("queue_id cannot be negative".to_string());
        }
        if self.revive_queue_id < 0 {
            return Err("revive_queue_id cannot be negative".to_string());
        }
        if self.pop_time < 0 {
            return Err("pop_time cannot be negative".to_string());
        }
        if self.invisible_time < 0 {
            return Err("invisible_time cannot be negative".to_string());
        }
        Ok(())
    }
}
🔗 Analysis chain

Consider validating negative values in BatchAck struct.

While testing edge cases is good, the use of negative values for fields like queue_id, start_offset, etc., might indicate missing validation in the BatchAck struct.

Let's check if BatchAck implements validation for these fields:

Consider adding validation in BatchAck to ensure these fields don't accept negative values:

impl BatchAck {
    pub fn validate(&self) -> Result<(), String> {
        if self.start_offset < 0 {
            return Err("start_offset cannot be negative".to_string());
        }
        if self.queue_id < 0 {
            return Err("queue_id cannot be negative".to_string());
        }
        // ... similar validation for other numeric fields
        Ok(())
    }
}

Let me search for more specific information about the BatchAck struct and its usage.


Let me search for the BatchAck struct definition and any validation logic in the codebase.


Let me search for the complete BatchAck struct definition to see all its fields and any related implementations.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for BatchAck struct definition and any validation logic
ast-grep --pattern 'struct BatchAck {
  $$$
}'

# Search for any validation methods or implementations for BatchAck
rg -A 5 'impl.*BatchAck'

Length of output: 564


Script:

#!/bin/bash
# Search for BatchAck struct definition
rg -A 10 "struct BatchAck"

# Search for any validation or error handling related to these fields
rg -B 2 -A 5 "queue_id|start_offset|revive_queue_id|pop_time|invisible_time" "rocketmq-remoting/src"

# Look for any error handling or validation patterns in the codebase
rg "validate|check.*valid" "rocketmq-remoting/src/protocol/body"

Length of output: 121509


Script:

#!/bin/bash
# Search for BatchAck struct definition and its implementation
rg -A 10 "pub struct BatchAck" "rocketmq-remoting/src"

# Search for any validation or error handling related to BatchAck fields
rg -B 2 -A 5 "impl.*BatchAck" "rocketmq-remoting/src"

# Look for any validation patterns in the protocol/body directory
rg "validate|check.*valid" "rocketmq-remoting/src/protocol/body"

Length of output: 2022


Script:

#!/bin/bash
# Get the complete BatchAck struct definition
cat rocketmq-remoting/src/protocol/body/batch_ack.rs

# Look for any validation patterns in the same file
rg -B 2 -A 5 "impl" "rocketmq-remoting/src/protocol/body/batch_ack.rs"

Length of output: 5881

Copy link

codecov bot commented Dec 17, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 28.40%. Comparing base (20892bc) to head (9a589d7).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1832      +/-   ##
==========================================
+ Coverage   28.35%   28.40%   +0.05%     
==========================================
  Files         474      475       +1     
  Lines       65957    66005      +48     
==========================================
+ Hits        18703    18751      +48     
  Misses      47254    47254              

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

# 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. rust Pull requests that update Rust code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature🚀] Add BatchAckMessageRequestBody struct for rust
3 participants