Metastarx opened a new issue, #11306:
URL: https://github.com/apache/rocketmq/issues/11306

   Three comparators in rocketmq-tools subtract two `long` values and cast the 
difference to `int`:
   
   - `GroupConsumeInfo.compareTo` 
(tools/src/main/java/org/apache/rocketmq/tools/command/consumer/ConsumerProgressSubCommand.java:352):
 `(int) (o.diffTotal - diffTotal)`, while `diffTotal` is a `long` (field at 
line 283, assigned from `consumeStats.computeTotalDiff()`).
   - `TagCountBean.compareTo` 
(tools/src/main/java/org/apache/rocketmq/tools/command/message/PrintMessageByQueueCommand.java:253):
 `(int) (o.getCount().get() - this.count.get())`.
   - `QueryMsgByUniqueKeySubCommand.queryById` 
(tools/src/main/java/org/apache/rocketmq/tools/command/message/QueryMsgByUniqueKeySubCommand.java:76):
 `list.sort((o1, o2) -> (int) (o1.getStoreTimestamp() - 
o2.getStoreTimestamp()))`.
   
   When the real difference is larger than `Integer.MAX_VALUE` the cast wraps: 
a positive difference becomes negative and vice versa. The printout order is 
then wrong, and the comparator contract is broken (`sign(compare(a, b)) != 
-sign(compare(b, a))`), which the JDK `TimSort` used by 
`Collections.sort`/`List.sort` can detect and fail with `Comparison method 
violates its general contract!`.
   
   The values are reachable in practice: a group's total consume lag can exceed 
2^31 messages, a `queryMsgByUniqueKey` `-s`/`-e` window can span more than 
about 24.8 days, and a `printMsgByQueue` tag count can exceed 2^31.
   
   ### How to reproduce
   
   Build two `GroupConsumeInfo` with `setCount(1)` and `diffTotal` of `0` and 
`(long) Integer.MAX_VALUE + 1`. Both `left.compareTo(right)` and 
`right.compareTo(left)` return a negative value, so the comparator is not 
antisymmetric; sorting a list that contains those entries prints them in the 
wrong order.
   
   ### Expected
   
   Comparisons should use `Long.compare(...)` / `Comparator.comparingLong(...)` 
instead of subtracting.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to