[ISSUE #10582] Fix POP revive offset comparator overflow#10583
Open
Aias00 wants to merge 1 commit into
Open
Conversation
POP revive checkpoints are ordered by reviveOffset before mergeAndRevive advances through the pending checkpoints. The previous comparator subtracted two long offsets and cast the result to int, so large offset gaps could reverse the intended ordering. Use Comparator.comparingLong to preserve the full long ordering and add a regression test covering offsets separated by more than Integer.MAX_VALUE. Constraint: reviveOffset is a long queue offset and can exceed int comparison range Rejected: Keep subtraction with a wider cast | Comparator APIs express the ordering directly and avoid overflow Confidence: high Scope-risk: narrow Directive: Do not compare long queue offsets by subtraction in POP revive ordering Tested: mvn -pl broker -Dtest=PopReviveServiceTest test -Dspotbugs.skip=true -Dcheckstyle.skip=true Tested: git diff --check
There was a problem hiding this comment.
Pull request overview
Fixes incorrect POP revive checkpoint ordering caused by an overflow-prone comparator in PopReviveService.ConsumeReviveObj#genSortList(), ensuring revive offsets are always sorted by their full long value.
Changes:
- Replaced
(int) (o1.getReviveOffset() - o2.getReviveOffset())comparator withComparator.comparingLong(PopCheckPoint::getReviveOffset)to prevent overflow and wrong ordering. - Added a regression test covering revive offsets that differ by more than
Integer.MAX_VALUE.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| broker/src/main/java/org/apache/rocketmq/broker/processor/PopReviveService.java | Uses a safe long comparator to sort revive checkpoints deterministically without overflow. |
| broker/src/test/java/org/apache/rocketmq/broker/processor/PopReviveServiceTest.java | Adds a regression test that would fail with the old overflow-prone comparator. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #10583 +/- ##
=============================================
- Coverage 48.26% 48.17% -0.10%
+ Complexity 13433 13411 -22
=============================================
Files 1378 1378
Lines 100817 100817
Branches 13040 13040
=============================================
- Hits 48660 48564 -96
- Misses 46211 46292 +81
- Partials 5946 5961 +15 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which Issue(s) This PR Fixes
Brief Description
This PR fixes the POP revive checkpoint ordering comparator in
PopReviveService.ConsumeReviveObj#genSortList().The previous implementation subtracted two long
reviveOffsetvalues and cast the result toint:When the offset gap is larger than
Integer.MAX_VALUE, the cast can overflow and return the wrong ordering. Since the sorted checkpoint list is consumed bymergeAndRevive(), this can cause POP revive checkpoints to be processed out of order.The comparator now uses
Comparator.comparingLong(PopCheckPoint::getReviveOffset)so the full long value is compared safely. A regression test covers two checkpoints whose revive offsets differ by more thanInteger.MAX_VALUE.How Did You Test This Change?
mvn -pl broker -Dtest=PopReviveServiceTest test -Dspotbugs.skip=true -Dcheckstyle.skip=truegit diff --check