Skip to content

[ISSUE #782]🚀PullMessageResponseHeader implement FastCodesHeader🚀#783

Merged
mxsm merged 1 commit intomainfrom
refactor-782
Jul 14, 2024
Merged

[ISSUE #782]🚀PullMessageResponseHeader implement FastCodesHeader🚀#783
mxsm merged 1 commit intomainfrom
refactor-782

Conversation

@mxsm
Copy link
Copy Markdown
Owner

@mxsm mxsm commented Jul 14, 2024

Which Issue(s) This PR Fixes(Closes)

Fixes #782

Brief Description

How Did You Test This Change?

Summary by CodeRabbit

  • New Features

    • Added constants and methods for the PullMessageResponseHeader to support mapping, encoding, and decoding functionalities.
    • Introduced unit tests to ensure the proper functioning of the new methods in PullMessageResponseHeader.
  • Bug Fixes

    • Corrected the write_if_not_null function to write the value parameter instead of the key parameter.
  • Maintenance

    • Added attribute to suppress invalid UTF-8 warnings in test functions.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Jul 14, 2024

Walkthrough

The recent changes introduce improvements and bug fixes to the rocketmq-remoting module. Specifically, they implement the FastCodesHeader for the PullMessageResponseHeader struct, correct the write_if_not_null function to handle the value parameter correctly, and suppress a UTF-8 related warning during testing.

Changes

File Change Summary
rocketmq-remoting/src/error.rs Added #[allow(invalid_from_utf8)] attribute to suppress UTF-8 warning in utf8_error_is_correctly_mapped() test function
rocketmq-remoting/src/protocol/command_custom_header.rs Corrected write_if_not_null to write the value parameter instead of key when value is not empty
rocketmq-remoting/src/protocol/header/pull_message_response_header.rs Implemented FastCodesHeader for PullMessageResponseHeader struct, added constants, and unit tests for encoding and decoding

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant PullMessageResponseHeader
    participant HashMap
    Client->>PullMessageResponseHeader: Create instance
    PullMessageResponseHeader->>HashMap: Convert to map
    HashMap-->>PullMessageResponseHeader: Return map data
    PullMessageResponseHeader->>Client: Return encoded data
    Client->>PullMessageResponseHeader: Decode data from map
    PullMessageResponseHeader->>PullMessageResponseHeader: Populate fields
    PullMessageResponseHeader-->>Client: Return populated instance
Loading

Assessment against linked issues

Objective Addressed Explanation
Implement FastCodesHeader for PullMessageResponseHeader (#782)
Ensure correct handling of value parameter in write_if_not_null function
Suppress specific UTF-8 warning during test execution

Poem

Amidst the code where bytes align,
A rabbit's touch made functions fine.
With headers swift and values true,
Encoding, decoding, a task anew.
Suppressed the warnings, none to see,
A future bright for RocketMQ! 🚀🐇


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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.

@mxsm
Copy link
Copy Markdown
Owner Author

mxsm commented Jul 14, 2024

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

@SpaceXCN SpaceXCN added approved PR has approved and removed ready to review labels Jul 14, 2024
@mxsm mxsm merged commit 7dfcc37 into main Jul 14, 2024
@mxsm mxsm deleted the refactor-782 branch July 14, 2024 07:11
Copy link
Copy Markdown
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

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 4c37350 and 444b9a7.

Files selected for processing (3)
  • rocketmq-remoting/src/error.rs (1 hunks)
  • rocketmq-remoting/src/protocol/command_custom_header.rs (1 hunks)
  • rocketmq-remoting/src/protocol/header/pull_message_response_header.rs (3 hunks)
Files skipped from review due to trivial changes (1)
  • rocketmq-remoting/src/error.rs
Additional comments not posted (4)
rocketmq-remoting/src/protocol/command_custom_header.rs (1)

55-55: LGTM! The logical error is fixed.

The function now correctly writes the value parameter instead of the key parameter.

rocketmq-remoting/src/protocol/header/pull_message_response_header.rs (3)

41-48: LGTM! The added constants improve readability.

The constants for various fields in the PullMessageResponseHeader struct enhance code readability and maintainability.


Line range hint 80-141:
LGTM! The implementation of CommandCustomHeader is correct.

The methods to_map, encode_fast, decode_fast, and support_fast_codec are implemented correctly.


170-274: LGTM! The unit tests are comprehensive.

The unit tests for from_map, to_map, encode_fast, and decode_fast methods cover various scenarios and ensure the correctness of the implementation.

Comment on lines +51 to +79
impl CommandCustomHeader for PullMessageResponseHeader {
fn to_map(&self) -> Option<HashMap<String, String>> {
let mut map = HashMap::new();
if let Some(value) = self.suggest_which_broker_id {
map.insert(Self::SUGGEST_WHICH_BROKER_ID.to_string(), value.to_string());
}
if let Some(value) = self.next_begin_offset {
map.insert(Self::NEXT_BEGIN_OFFSET.to_string(), value.to_string());
}
if let Some(value) = self.min_offset {
map.insert(Self::MIN_OFFSET.to_string(), value.to_string());
}
if let Some(value) = self.max_offset {
map.insert(Self::MAX_OFFSET.to_string(), value.to_string());
}
if let Some(value) = self.offset_delta {
map.insert(Self::OFFSET_DELTA.to_string(), value.to_string());
}
if let Some(value) = self.topic_sys_flag {
map.insert(Self::TOPIC_SYS_FLAG.to_string(), value.to_string());
}
if let Some(value) = self.group_sys_flag {
map.insert(Self::GROUP_SYS_FLAG.to_string(), value.to_string());
}
if let Some(value) = self.forbidden_type {
map.insert(Self::FORBIDDEN_TYPE.to_string(), value.to_string());
}
Some(map)
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Handle potential parsing errors in decode_fast and from methods.

The unwrap calls in decode_fast and from methods can cause the program to panic if parsing fails. Consider handling these errors more gracefully.

-  self.suggest_which_broker_id = Some(offset_delta.parse().unwrap());
+  self.suggest_which_broker_id = offset_delta.parse().ok();

-  suggest_which_broker_id: suggest_which_broker_id.map(|v| v.parse().unwrap()),
+  suggest_which_broker_id: suggest_which_broker_id.and_then(|v| v.parse().ok()),

Committable suggestion was skipped due to low confidence.

Comment on lines +144 to +167
impl FromMap for PullMessageResponseHeader {
type Target = Self;

fn from(map: &HashMap<String, String>) -> Option<Self::Target> {
let suggest_which_broker_id = map.get(PullMessageResponseHeader::SUGGEST_WHICH_BROKER_ID);
let next_begin_offset = map.get(PullMessageResponseHeader::NEXT_BEGIN_OFFSET);
let min_offset = map.get(PullMessageResponseHeader::MIN_OFFSET);
let max_offset = map.get(PullMessageResponseHeader::MAX_OFFSET);
let offset_delta = map.get(PullMessageResponseHeader::OFFSET_DELTA);
let topic_sys_flag = map.get(PullMessageResponseHeader::TOPIC_SYS_FLAG);
let group_sys_flag = map.get(PullMessageResponseHeader::GROUP_SYS_FLAG);
let forbidden_type = map.get(PullMessageResponseHeader::FORBIDDEN_TYPE);

Some(PullMessageResponseHeader {
suggest_which_broker_id: suggest_which_broker_id.map(|v| v.parse().unwrap()),
next_begin_offset: next_begin_offset.map(|v| v.parse().unwrap()),
min_offset: min_offset.map(|v| v.parse().unwrap()),
max_offset: max_offset.map(|v| v.parse().unwrap()),
offset_delta: offset_delta.map(|v| v.parse().unwrap()),
topic_sys_flag: topic_sys_flag.map(|v| v.parse().unwrap()),
group_sys_flag: group_sys_flag.map(|v| v.parse().unwrap()),
forbidden_type: forbidden_type.map(|v| v.parse().unwrap()),
})
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Handle potential parsing errors in the from method.

The unwrap calls in the from method can cause the program to panic if parsing fails. Consider handling these errors more gracefully.

-  suggest_which_broker_id: suggest_which_broker_id.map(|v| v.parse().unwrap()),
+  suggest_which_broker_id: suggest_which_broker_id.and_then(|v| v.parse().ok()),
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
impl FromMap for PullMessageResponseHeader {
type Target = Self;
fn from(map: &HashMap<String, String>) -> Option<Self::Target> {
let suggest_which_broker_id = map.get(PullMessageResponseHeader::SUGGEST_WHICH_BROKER_ID);
let next_begin_offset = map.get(PullMessageResponseHeader::NEXT_BEGIN_OFFSET);
let min_offset = map.get(PullMessageResponseHeader::MIN_OFFSET);
let max_offset = map.get(PullMessageResponseHeader::MAX_OFFSET);
let offset_delta = map.get(PullMessageResponseHeader::OFFSET_DELTA);
let topic_sys_flag = map.get(PullMessageResponseHeader::TOPIC_SYS_FLAG);
let group_sys_flag = map.get(PullMessageResponseHeader::GROUP_SYS_FLAG);
let forbidden_type = map.get(PullMessageResponseHeader::FORBIDDEN_TYPE);
Some(PullMessageResponseHeader {
suggest_which_broker_id: suggest_which_broker_id.map(|v| v.parse().unwrap()),
next_begin_offset: next_begin_offset.map(|v| v.parse().unwrap()),
min_offset: min_offset.map(|v| v.parse().unwrap()),
max_offset: max_offset.map(|v| v.parse().unwrap()),
offset_delta: offset_delta.map(|v| v.parse().unwrap()),
topic_sys_flag: topic_sys_flag.map(|v| v.parse().unwrap()),
group_sys_flag: group_sys_flag.map(|v| v.parse().unwrap()),
forbidden_type: forbidden_type.map(|v| v.parse().unwrap()),
})
}
impl FromMap for PullMessageResponseHeader {
type Target = Self;
fn from(map: &HashMap<String, String>) -> Option<Self::Target> {
let suggest_which_broker_id = map.get(PullMessageResponseHeader::SUGGEST_WHICH_BROKER_ID);
let next_begin_offset = map.get(PullMessageResponseHeader::NEXT_BEGIN_OFFSET);
let min_offset = map.get(PullMessageResponseHeader::MIN_OFFSET);
let max_offset = map.get(PullMessageResponseHeader::MAX_OFFSET);
let offset_delta = map.get(PullMessageResponseHeader::OFFSET_DELTA);
let topic_sys_flag = map.get(PullMessageResponseHeader::TOPIC_SYS_FLAG);
let group_sys_flag = map.get(PullMessageResponseHeader::GROUP_SYS_FLAG);
let forbidden_type = map.get(PullMessageResponseHeader::FORBIDDEN_TYPE);
Some(PullMessageResponseHeader {
suggest_which_broker_id: suggest_which_broker_id.and_then(|v| v.parse().ok()),
next_begin_offset: next_begin_offset.map(|v| v.parse().unwrap()),
min_offset: min_offset.map(|v| v.parse().unwrap()),
max_offset: max_offset.map(|v| v.parse().unwrap()),
offset_delta: offset_delta.map(|v| v.parse().unwrap()),
topic_sys_flag: topic_sys_flag.map(|v| v.parse().unwrap()),
group_sys_flag: group_sys_flag.map(|v| v.parse().unwrap()),
forbidden_type: forbidden_type.map(|v| v.parse().unwrap()),
})
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved PR has approved auto merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature🚀] PullMessageResponseHeader implement FastCodesHeader

2 participants