-
Notifications
You must be signed in to change notification settings - Fork 122
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
[ISSUE #1500]♻️Refactor SendMessageRequestHeaderV2🍻 #1501
Conversation
WalkthroughThe pull request introduces significant modifications to several components within the RocketMQ remoting protocol. Key changes include updating method signatures for error handling in the Changes
Assessment against linked issues
Possibly related issues
Possibly related PRs
Suggested labels
Suggested reviewers
Poem
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? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
Documentation and Community
|
🔊@mxsm 🚀Thanks for your contribution 🎉. CodeRabbit(AI) will review your code first 🔥 |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1501 +/- ##
==========================================
+ Coverage 23.28% 23.79% +0.50%
==========================================
Files 450 450
Lines 58674 58917 +243
==========================================
+ Hits 13663 14018 +355
+ Misses 45011 44899 -112 ☔ View full report in Codecov by Sentry. |
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.
Actionable comments posted: 0
🧹 Outside diff range and nitpick comments (6)
rocketmq-remoting/src/protocol/command_custom_header.rs (2)
74-79
: Document the purpose of the defaultdecode_fast
implementationThe default implementation of
decode_fast
in theCommandCustomHeader
trait returnsOk(())
without performing any actions. Consider adding documentation to clarify that this default method should be overridden by implementing types that require custom decoding logic.
90-120
: Improve error handling inget_and_check_not_none
methodThe method
get_and_check_not_none
can be simplified using thecloned
andok_or_else
methods for clearer error handling.Apply this diff to enhance readability and efficiency:
- fn get_and_check_not_none( - &self, - map: &HashMap<CheetahString, CheetahString>, - field: &CheetahString, - ) -> crate::Result<CheetahString> { - match map.get(field) { - Some(value) => Ok(value.clone()), - None => Err(crate::remoting_error::RemotingError::RemotingCommandError( - format!("The field {} is required.", field), - )), - } - } + fn get_and_check_not_none( + &self, + map: &HashMap<CheetahString, CheetahString>, + field: &CheetahString, + ) -> crate::Result<CheetahString> { + map.get(field) + .cloned() + .ok_or_else(|| crate::remoting_error::RemotingError::RemotingCommandError( + format!("The field {} is required.", field), + )) + }rocketmq-remoting/src/protocol/header/message_operation_header/send_message_request_header_v2.rs (2)
28-28
: Remove unused import if unnecessaryThe import of
RemotingError::RemotingCommandError
is added but may not be required in this file. Ensure that this import is needed; otherwise, consider removing it to clean up the code.
Line range hint
149-206
: Handle parsing errors to prevent panics indecode_fast
The
decode_fast
method usesparse().unwrap()
when parsing fields, which can cause the application to panic if parsing fails. Replaceunwrap()
with proper error handling to ensure robustness.Apply this diff to handle parsing errors safely:
- self.j = Some(v.parse().unwrap()); + self.j = Some(v.parse().map_err(|_| RemotingCommandError("Parse field j error".to_string()))?); - self.k = Some( - v.parse() - .map_err(|_| RemotingCommandError("Parse field k error".to_string()))?, - ); - self.l = Some( - v.parse() - .map_err(|_| RemotingCommandError("Parse field l error".to_string()))?, - ); - self.m = Some( - v.parse() - .map_err(|_| RemotingCommandError("Parse field m error".to_string()))?, - );This change ensures that any parsing errors are properly propagated as
Err
values.rocketmq-remoting/src/protocol/header/pull_message_response_header.rs (1)
Line range hint
137-174
: Safely handle parsing indecode_fast
to avoid panicsIn the
decode_fast
method, usingparse().unwrap()
can lead to panics if the data cannot be parsed. Replaceunwrap()
withmap_err
to return meaningful errors and prevent the application from crashing.Apply this diff to improve error handling:
if let Some(offset_delta) = fields.get(&CheetahString::from_static_str(Self::SUGGEST_WHICH_BROKER_ID)) { - self.suggest_which_broker_id = Some(offset_delta.parse().unwrap()); + self.suggest_which_broker_id = Some(offset_delta.parse().map_err(|_| RemotingCommandError("Parse field suggestWhichBrokerId error".to_string()))?); }Repeat similar changes for the other fields (
next_begin_offset
,min_offset
, etc.) to handle parsing errors appropriately.rocketmq-remoting/src/protocol/header/pull_message_request_header.rs (1)
Line range hint
214-319
: Prevent application panics by handling errors indecode_fast
The use of
str.parse::<i64>().unwrap()
and similar statements can cause panics if parsing fails. Modify the code to handle parsing errors usingmap_err
and return them as part of theResult
.Apply this diff to handle parsing errors safely:
- self.queue_offset = str.parse::<i64>().unwrap(); + self.queue_offset = str.parse::<i64>().map_err(|_| RemotingCommandError("Parse field queueOffset error".to_string()))?; - self.max_msg_nums = str.parse::<i32>().unwrap(); + self.max_msg_nums = str.parse::<i32>().map_err(|_| RemotingCommandError("Parse field maxMsgNums error".to_string()))?; - self.sys_flag = str.parse::<i32>().unwrap(); + self.sys_flag = str.parse::<i32>().map_err(|_| RemotingCommandError("Parse field sysFlag error".to_string()))?; - self.commit_offset = str.parse::<i64>().unwrap(); + self.commit_offset = str.parse::<i64>().map_err(|_| RemotingCommandError("Parse field commitOffset error".to_string()))?; - self.suspend_timeout_millis = str.parse::<u64>().unwrap(); + self.suspend_timeout_millis = str.parse::<u64>().map_err(|_| RemotingCommandError("Parse field suspendTimeoutMillis error".to_string()))?; - self.sub_version = str.parse::<i64>().unwrap(); + self.sub_version = str.parse::<i64>().map_err(|_| RemotingCommandError("Parse field subVersion error".to_string()))?;By handling errors, you ensure that the application remains stable even when encountering invalid input.
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
📒 Files selected for processing (5)
rocketmq-remoting/src/protocol/command_custom_header.rs
(2 hunks)rocketmq-remoting/src/protocol/header/message_operation_header/send_message_request_header_v2.rs
(4 hunks)rocketmq-remoting/src/protocol/header/pull_message_request_header.rs
(2 hunks)rocketmq-remoting/src/protocol/header/pull_message_response_header.rs
(2 hunks)rocketmq-remoting/src/protocol/remoting_command.rs
(1 hunks)
🔇 Additional comments (2)
rocketmq-remoting/src/protocol/header/message_operation_header/send_message_request_header_v2.rs (1)
449-687
: Add unit tests for error cases and edge conditions
The current unit tests cover standard serialization and deserialization scenarios. Consider adding tests that handle parsing failures, missing required fields, and invalid data to ensure the SendMessageRequestHeaderV2
struct behaves correctly under all conditions.
[unit_tests_coverage]
rocketmq-remoting/src/protocol/remoting_command.rs (1)
607-607
: Improve error propagation in decode_command_custom_header_fast
The addition of the ?
operator after target.decode_fast(header)
ensures that errors from decode_fast
are properly returned to the caller. This change enhances error handling by propagating errors instead of silently ignoring them.
Which Issue(s) This PR Fixes(Closes)
Fixes #1500
Brief Description
How Did You Test This Change?
Summary by CodeRabbit
New Features
get_and_check_not_none
for improved field validation and retrieval.Bug Fixes
Tests