fix: fix equal bug in some files#10574
Conversation
fix equal bug in some files. equals/hashCode 基于当前字段值。若对象在集合中时字段被修改,可能导致定位失败——这是这些类本就存在的可变性问题,本次修复仅保证契约一致性,未改变可变性。
RockteMQ-AI
left a comment
There was a problem hiding this comment.
Code Review
This PR adds equals() and hashCode() implementations to 7 classes that implement Comparable but were missing proper equality methods.
Overall Assessment: ✅ Generally good, with minor suggestions
Changes Reviewed:
TraceContext.java- UsestimeStampfor equalityGrpcClientChannel.java- UsesclientIdfor equalityRemoteChannel.java- Usesidfor equalityPopCheckPoint.java- UsesstartOffsetfor equalityFileSegment.java- UsesbaseOffsetfor equalityGroupConsumeInfo- Usescount+diffTotalfor equalityTagCountBean- Usestagfor equality
Observations:
-
Consistency with
compareTo(): Most implementations align equality fields withcompareTo(), which is good practice. However, note thatTraceContextuses onlytimeStampwhilecompareTo()also usestimeStamp— this is consistent. -
Null handling:
GrpcClientChannelandTagCountBeanproperly handle null values inequals()andhashCode(). Good. -
Potential issue in
GroupConsumeInfo: ThecompareTo()usesdiffTotal - diffTotal(subtraction), butequals()uses field comparison. This is acceptable, but ensure the class is not used in sorted collections wherecompareTo()returning 0 should implyequals()returning true. -
Missing
Objects.equals(): Some implementations use manual null checks instead ofObjects.equals(). Consider using:return Objects.equals(this.tag, that.tag);
Suggestion: Consider adding unit tests to verify the equals()/hashCode() contracts (reflexive, symmetric, transitive, consistent).
Review by @RockteMQ-AI
fix by rocketmq ai suggestion
RockteMQ-AI
left a comment
There was a problem hiding this comment.
Review by github-manager-bot (Re-review after new commit 72b8f81)
Summary
New commit pushed addressing previous review suggestions. Two equals() implementations changed and unit tests added for GroupConsumeInfo and TagCountBean.
Changes in This Commit
| File | Change | Assessment |
|---|---|---|
GroupConsumeInfo |
equals() now uses group instead of count+diffTotal |
✅ Semantically correct — two entries with the same group name should be equal |
TagCountBean |
equals() now uses count.get() instead of tag |
✅ Consistent with compareTo() which also orders by count |
GroupConsumeInfoTest (new) |
Tests for equals contract (reflexive, symmetric, transitive) and hashCode | ✅ Good coverage |
TagCountBeanTest (new) |
Tests for equals contract and hashCode | ✅ Good coverage |
Remaining Observations
-
GroupConsumeInfo—compareTovsequalsfield mismatch:compareTo()usesdiffTotalwhileequals()usesgroup. This means two objects with the samegroupbut differentdiffTotalwould beequals() == truebutcompareTo() != 0. This is technically aComparablecontract violation, but it's a pre-existing design issue in the class (the originalcompareTowas already questionable). Not a blocker for this PR. -
TagCountBean— descending order:compareTo()returnso.count - this.count(descending), whileequals()usescount. This is actually consistent (same field, just different ordering direction), so no issue here.
Verdict
✅ Approved — The new commit properly addresses the previous suggestions. Adding unit tests is a nice improvement. The remaining compareTo/equals field mismatch in GroupConsumeInfo is a pre-existing design concern, not introduced by this PR.
Automated review by github-manager-bot
Which Issue(s) This PR Fixes
Brief Description
compareTo 与 Object.equals 共存.
在 TreeSet/TreeMap 中,compareTo 返回 0 的两个对象会被视为"相等"但 equals() 返回 false,违反 Comparable 契约,可能导致集合中元素丢失或行为不一致。
修复:重写 equals/hashCode 与 compareTo 一致。
How Did You Test This Change?