[ISSUE #1479]♻️Refactor CheckTransactionStateRequestHeader with derive RequestHeaderCodec🔥#1494
[ISSUE #1479]♻️Refactor CheckTransactionStateRequestHeader with derive RequestHeaderCodec🔥#1494rocketmq-rust-bot merged 1 commit intomainfrom
Conversation
…e RequestHeaderCodec🔥
WalkthroughThe changes in this pull request involve modifications to the Changes
Assessment against linked issues
Possibly related issues
Possibly related PRs
Suggested labels
Suggested reviewers
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 🔥 |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Outside diff range and nitpick comments (2)
rocketmq-remoting/src/protocol/header/check_transaction_state_request_header.rs (2)
Line range hint
39-122: Remove obsolete commented-out codeThere is a large block of commented-out code spanning lines 39 to 122, including implementations of
CommandCustomHeaderandFromMap. Since theRequestHeaderCodecmacro now handles these implementations, consider removing this obsolete code to enhance readability and maintainability.Apply this diff to remove the commented-out code:
-/*impl CheckTransactionStateRequestHeader { - pub const TOPIC: &'static str = "topic"; - pub const TRAN_STATE_TABLE_OFFSET: &'static str = "tranStateTableOffset"; - pub const COMMIT_LOG_OFFSET: &'static str = "commitLogOffset"; - pub const MSG_ID: &'static str = "msgId"; - pub const TRANSACTION_ID: &'static str = "transactionId"; - pub const OFFSET_MSG_ID: &'static str = "offsetMsgId"; -} - -impl CommandCustomHeader for CheckTransactionStateRequestHeader { - fn to_map(&self) -> Option<std::collections::HashMap<CheetahString, CheetahString>> { - let mut map = std::collections::HashMap::new(); - if let Some(value) = self.topic.as_ref() { - map.insert(CheetahString::from_static_str(Self::TOPIC), value.clone()); - } - map.insert( - CheetahString::from_static_str(Self::TRAN_STATE_TABLE_OFFSET), - CheetahString::from_string(self.tran_state_table_offset.to_string()), - ); - - map.insert( - CheetahString::from_static_str(Self::COMMIT_LOG_OFFSET), - CheetahString::from_string(self.commit_log_offset.to_string()), - ); - if let Some(value) = self.msg_id.as_ref() { - map.insert(CheetahString::from_static_str(Self::MSG_ID), value.clone()); - } - if let Some(value) = self.transaction_id.as_ref() { - map.insert( - CheetahString::from_static_str(Self::TRANSACTION_ID), - value.clone(), - ); - } - if let Some(value) = self.offset_msg_id.as_ref() { - map.insert( - CheetahString::from_static_str(Self::OFFSET_MSG_ID), - value.clone(), - ); - } - if let Some(value) = self.rpc_request_header.as_ref() { - if let Some(value) = value.to_map() { - map.extend(value); - } - } - Some(map) - } -} - -impl FromMap for CheckTransactionStateRequestHeader { - type Error = crate::remoting_error::RemotingError; - - type Target = Self; - - fn from( - map: &std::collections::HashMap<CheetahString, CheetahString>, - ) -> Result<Self::Target, Self::Error> { - Ok(CheckTransactionStateRequestHeader { - topic: map - .get(&CheetahString::from_static_str(Self::TOPIC)) - .cloned(), - tran_state_table_offset: map - .get(&CheetahString::from_static_str( - Self::TRAN_STATE_TABLE_OFFSET, - )) - .and_then(|v| v.parse().ok()) - .unwrap_or_default(), - commit_log_offset: map - .get(&CheetahString::from_static_str(Self::COMMIT_LOG_OFFSET)) - .and_then(|v| v.parse().ok()) - .unwrap_or_default(), - msg_id: map - .get(&CheetahString::from_static_str(Self::MSG_ID)) - .cloned(), - transaction_id: map - .get(&CheetahString::from_static_str(Self::TRANSACTION_ID)) - .cloned(), - offset_msg_id: map - .get(&CheetahString::from_static_str(Self::OFFSET_MSG_ID)) - .cloned(), - rpc_request_header: Some(<RpcRequestHeader as FromMap>::from(map)?), - }) - } -} -*/
215-232: Add tests for missing required fieldsWhile there is a test for missing optional fields, consider adding a test case where required fields (
tran_state_table_offsetorcommit_log_offset) are missing to ensure that deserialization fails as expected when mandatory data is absent.Example test case to add:
#[test] fn check_transaction_state_request_header_errors_on_missing_required_fields() { let mut map = HashMap::new(); // Intentionally omit 'tranStateTableOffset' and 'commitLogOffset' let result = <CheckTransactionStateRequestHeader as FromMap>::from(&map); assert!(result.is_err()); }
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
📒 Files selected for processing (1)
rocketmq-remoting/src/protocol/header/check_transaction_state_request_header.rs(3 hunks)
🔇 Additional comments (4)
rocketmq-remoting/src/protocol/header/check_transaction_state_request_header.rs (4)
18-18: Appropriate use of RequestHeaderCodec macro
The addition of use rocketmq_macros::RequestHeaderCodec; enables the automatic implementation of the required serialization and deserialization traits, simplifying the codebase.
24-24: Updated derivation with RequestHeaderCodec
Including RequestHeaderCodec in the #[derive] macro enhances the struct by automatically implementing the necessary codecs for request headers, improving maintainability.
124-249: Comprehensive test suite added
The new test module provides thorough coverage for the CheckTransactionStateRequestHeader struct, including tests for:
- Correct serialization with all fields present.
- Correct deserialization with all fields present.
- Handling of missing optional fields.
- Handling of invalid data types for required fields.
This enhances the robustness of the code and ensures that changes in serialization and deserialization logic are validated.
28-30: Ensure all instances provide values for required fields
The fields tran_state_table_offset and commit_log_offset are now marked with #[required], making them mandatory. Please verify that all constructions of CheckTransactionStateRequestHeader in the codebase include these fields to prevent runtime errors due to missing required data.
Run the following script to locate instances where CheckTransactionStateRequestHeader is instantiated and check for required fields:
✅ Verification successful
All instances properly initialize required fields
Based on the search results, all instances where CheckTransactionStateRequestHeader is constructed in the codebase properly initialize both required fields:
- In the unit test:
tran_state_table_offset: 123, commit_log_offset: 456 - In the transaction check listener:
tran_state_table_offset: msg_ext.queue_offset, commit_log_offset: msg_ext.commit_log_offset
Additionally, the FromMap implementation handles these fields appropriately, ensuring they are properly deserialized from the map representation.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Description: Find all instantiations of CheckTransactionStateRequestHeader and check for the presence of required fields.
# Search for instantiations of the struct
rg --type rust 'CheckTransactionStateRequestHeader\s*{[^}]*}' -A 5
# Highlight instances missing the required fields
rg --type rust 'CheckTransactionStateRequestHeader\s*{[^}]*}' -A 5 | rg -v 'tran_state_table_offset|commit_log_offset'
Length of output: 519
Script:
#!/bin/bash
# Let's try a different approach to find struct instantiations
# First, find all Rust files that mention CheckTransactionStateRequestHeader
rg -l --type rust "CheckTransactionStateRequestHeader"
# Then look for actual usage patterns with proper escaping
rg --type rust "CheckTransactionStateRequestHeader\s*\{" -A 10
Length of output: 9547
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #1494 +/- ##
==========================================
+ Coverage 22.58% 22.83% +0.24%
==========================================
Files 450 450
Lines 58549 58593 +44
==========================================
+ Hits 13222 13378 +156
+ Misses 45327 45215 -112 ☔ View full report in Codecov by Sentry. |
Which Issue(s) This PR Fixes(Closes)
Fixes #1479
Brief Description
How Did You Test This Change?
Summary by CodeRabbit
New Features
Tests