-
Notifications
You must be signed in to change notification settings - Fork 144
[ISSUE #1742]⚡️Enhancement OrderInfo struct add some methods #1744
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
Conversation
🔊@mxsm 🚀Thanks for your contribution 🎉. CodeRabbit(AI) will review your code first 🔥 |
WalkthroughThe changes in this pull request focus on enhancing the Changes
Assessment against linked issues
Possibly related PRs
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
|
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: 3
🧹 Outside diff range and nitpick comments (3)
rocketmq-broker/src/offset/manager/consumer_order_info_manager.rs (3)
222-232
: Simplify loop by removing unnecessaryenumerate
inneed_block
The use of
enumerate
infor (i, _) in (0..num).enumerate()
is unnecessary because0..num
already provides the indices. You can simplify the loop by iterating directly over the range.Apply this diff to simplify the loop:
-for (i, _) in (0..num).enumerate() { +for i in 0..num {
246-260
: Simplify loop by removing unnecessaryenumerate
inget_lock_free_timestamp
Similarly, in the
get_lock_free_timestamp
method, the loop can be simplified by removing the unnecessaryenumerate
.Apply this diff to simplify the loop:
-for (i, _) in (0..self.offset_list.len()).enumerate() { +for i in 0..self.offset_list.len() {
399-407
: Improve test coverage by checking more scenarios inneed_block
testThe test
need_block_returns_false_for_empty_offset_list
checks only for an emptyoffset_list
. Consider adding tests for other scenarios, such as whenattempt_id
matches, whencurrent_time
is beforenext_visible_time
, and when offsets are acknowledged or not.Adding more comprehensive tests will ensure that
need_block
behaves correctly under various conditions.
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
rocketmq-broker/src/offset/manager/consumer_order_info_manager.rs
(3 hunks)
🔇 Additional comments (1)
rocketmq-broker/src/offset/manager/consumer_order_info_manager.rs (1)
126-126
: Good use of encapsulation with update_offset_next_visible_time
The call to order_info.update_offset_next_visible_time(queue_offset, next_visible_time);
enhances encapsulation by moving the update logic into the OrderInfo
struct. This improves code maintainability and readability.
pub fn is_not_ack(&self, offset_index: usize) -> bool { | ||
(self.commit_offset_bit & (1 << offset_index)) == 0 | ||
} |
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.
Potential undefined behavior when shifting bits in is_not_ack
In the is_not_ack
method, shifting 1 << offset_index
can cause undefined behavior if offset_index
is 64 or greater, since u64
has 64 bits. Shifting by an amount equal to or greater than the type's size is not allowed and will panic in Rust.
Ensure that offset_index
is less than 64 to prevent overflow:
+if offset_index >= 64 {
+ // Handle the error, e.g., return false or panic with a meaningful message
+ return false;
+}
(self.commit_offset_bit & (1 << offset_index)) == 0
Alternatively, use checked shifting:
if let Some(bit) = (1u64).checked_shl(offset_index as u32) {
(self.commit_offset_bit & bit) == 0
} else {
// Handle the error
false
}
for &offset in &pre_offset_list { | ||
pre_queue_offset_set.insert(Self::get_queue_offset_from_list( | ||
&pre_offset_list, | ||
offset as usize, | ||
)); | ||
} |
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.
Possible misuse of offset value as index in merge_offset_consumed_count
In the merge_offset_consumed_count
method, when iterating over pre_offset_list
, offset
(a queue offset value) is being cast to usize
and used as an index in get_queue_offset_from_list
. This can cause an out-of-bounds access if offset as usize
exceeds the length of pre_offset_list
.
Consider iterating over the indices instead of the values to ensure valid indexing.
Apply this diff to fix the issue:
-for &offset in &pre_offset_list {
- pre_queue_offset_set.insert(Self::get_queue_offset_from_list(
- &pre_offset_list,
- offset as usize,
- ));
+for (index, _) in pre_offset_list.iter().enumerate() {
+ pre_queue_offset_set.insert(Self::get_queue_offset_from_list(
+ &pre_offset_list,
+ index,
+ ));
}
📝 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.
for &offset in &pre_offset_list { | |
pre_queue_offset_set.insert(Self::get_queue_offset_from_list( | |
&pre_offset_list, | |
offset as usize, | |
)); | |
} | |
for (index, _) in pre_offset_list.iter().enumerate() { | |
pre_queue_offset_set.insert(Self::get_queue_offset_from_list( | |
&pre_offset_list, | |
index, | |
)); | |
} |
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.
I fix it
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.
pub fn build_offset_list(queue_offset_list: Vec<u64>) -> Vec<u64> { | ||
let mut simple = Vec::new(); | ||
if queue_offset_list.len() == 1 { | ||
simple.extend(queue_offset_list); | ||
return simple; | ||
} | ||
let first = queue_offset_list[0]; | ||
simple.push(first); | ||
for item in queue_offset_list.iter().skip(1) { | ||
simple.push(*item - first); | ||
} | ||
simple | ||
} |
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.
Potential integer underflow in build_offset_list
In the build_offset_list
method, subtracting first
from *item
where both are u64
may result in integer underflow if *item
is less than first
. Since u64
is an unsigned integer, this underflow will cause a panic in debug builds or wrap around in release builds, leading to incorrect results.
Consider validating that each *item
is greater than or equal to first
before performing the subtraction, or handle the case where *item
is less than first
.
for item in queue_offset_list.iter().skip(1) {
+ if *item < first {
+ // Handle the error, e.g., return an error or skip the value
+ }
simple.push(*item - first);
}
Committable suggestion skipped: line range outside the PR's diff.
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1744 +/- ##
==========================================
+ Coverage 28.18% 28.24% +0.06%
==========================================
Files 466 466
Lines 64493 64671 +178
==========================================
+ Hits 18175 18265 +90
- Misses 46318 46406 +88 ☔ View full report in Codecov by Sentry. |
🔊@mxsm 🚀Thanks for your contribution 🎉. CodeRabbit(AI) will review your code first 🔥 |
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 (2)
rocketmq-broker/src/offset/manager/consumer_order_info_manager.rs (2)
210-234
: Consider refactoring time calculation logicThe time calculation logic is duplicated between
need_block
andget_lock_free_timestamp
. Consider extracting the common logic into a private helper method.+ fn calculate_next_visible_time(&self, offset_index: usize) -> u64 { + let mut next_visible_time = self.pop_time + self.invisible_time.unwrap_or(0); + if let Some(time) = self.offset_next_visible_time.get(&self.get_queue_offset(offset_index)) { + next_visible_time = *time; + } + next_visible_time + }Also applies to: 241-261
383-459
: Consider adding more test casesWhile the current test coverage is good, consider adding these test cases:
- Test
build_offset_list
with potentially underflowing offsets- Test
need_block
andget_lock_free_timestamp
with edge cases around visibility times#[test] fn test_build_offset_list_with_potential_underflow() { let queue_offset_list = vec![5, 3]; // Second offset less than first // Verify the handling of potential underflow } #[test] fn test_need_block_visibility_edge_cases() { let mut order_info = OrderInfo { pop_time: get_current_millis(), invisible_time: Some(1000), // ... other fields }; // Test cases around visibility boundaries }
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
rocketmq-broker/src/offset/manager/consumer_order_info_manager.rs
(3 hunks)
🔇 Additional comments (5)
rocketmq-broker/src/offset/manager/consumer_order_info_manager.rs (5)
19-19
: LGTM: New imports are appropriate
The added imports for HashSet
and TimeUtils
are necessary for the new functionality and follow Rust's conventions.
Also applies to: 26-26
126-126
: LGTM: Improved encapsulation
Moving the offset update logic to OrderInfo::update_offset_next_visible_time
improves encapsulation and maintainability.
323-328
: LGTM: Proper bounds checking for bit shifting
The implementation correctly checks for the bounds before performing bit shifting operations.
185-197
:
Fix potential integer underflow in build_offset_list
The subtraction operation *item - first
could underflow if *item < first
.
171-381
: Verify usage of OrderInfo methods across the codebase
Let's verify that all callers of these methods handle the new behavior correctly.
✅ Verification successful
Based on the search results, I can now generate the final response since I have enough information to verify the changes:
OrderInfo implementation is safe and properly integrated with the codebase
The code changes in OrderInfo
are well-integrated with the rest of the codebase:
- The time-related fields (
pop_time
,invisible_time
,next_visible_time
) are consistently used across the codebase, particularly inChangeInvisibleTimeProcessor
and consumer implementations - The offset management methods are properly used by the broker components, especially in
consumer_order_info_manager.rs
- The blocking and visibility time calculations align with the broker's message processing flow
- All method signatures and data structures are consistent with their usage in other components
The implementation shows proper handling of:
- Time-based message visibility
- Offset tracking and management
- Order preservation
- Consumer group coordination
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Search for usages of OrderInfo methods
echo "Searching for OrderInfo method calls..."
rg -A 2 "build_offset_list|need_block|get_lock_free_timestamp|update_offset_next_visible_time|get_next_offset|get_queue_offset|is_not_ack|merge_offset_consumed_count" --type rust
# Search for potential time-related issues
echo "Checking for time-related calculations..."
rg -A 2 "pop_time|invisible_time|next_visible_time" --type rust
Length of output: 74973
Which Issue(s) This PR Fixes(Closes)
Fixes #1742
Brief Description
How Did You Test This Change?
Summary by CodeRabbit