zjncs opened a new pull request, #11064:
URL: https://github.com/apache/rocketmq/pull/11064

   ### Motivation
   
   `MessageQueueAssignment.hashCode()` combines all three fields 
(`messageQueue`, `mode`, `attachments`), but `equals()` compares only 
`messageQueue`, with no null guard:
   
   ```java
   MessageQueueAssignment other = (MessageQueueAssignment) obj;
   return messageQueue.equals(other.messageQueue);
   ```
   
   Two consequences:
   
   1. `new MessageQueueAssignment().equals(new MessageQueueAssignment())` 
throws `NullPointerException` because the default `messageQueue` is null.
   2. Two assignments with the same `MessageQueue` but different `mode` (`PULL` 
vs `POP`) compare equal while having different hashCodes — a direct `Object` 
equals/hashCode contract violation that corrupts any hash-based collection.
   
   The class is the element type of the `Set<MessageQueueAssignment>` in 
`QueryAssignmentResponseBody`, produced by `QueryAssignmentProcessor` into a 
`HashSet` and re-deserialized into a `HashSet` on the client in 
`MQClientInstance.queryAssignment()`, so it is routinely stored in hash 
collections. Sibling classes in the same package (`MessageQueue`, 
`MessageQueueForC`) implement equals/hashCode consistently.
   
   ### Modifications
   
   Compare all three fields with `Objects.equals`, mirroring `hashCode`.
   
   No broker behavior change: in `QueryAssignmentProcessor` the assignments are 
built from a `Set<MessageQueue>`, so each queue appears once and the wider 
equality never merges distinct entries.
   
   ### Verification
   
   Fail-before (new test class `MessageQueueAssignmentTest`, run against the 
unpatched code):
   
   ```
   Tests run: 1, Failures: 0, Errors: 1 -- 
MessageQueueAssignmentTest#testEqualsHandlesNullMessageQueue
   java.lang.NullPointerException: Cannot invoke 
"...MessageQueue.equals(Object)" because "this.messageQueue" is null
   
   Tests run: 2, Failures: 1, Errors: 0 -- 
MessageQueueAssignmentTest#testEqualsAndHashCodeUseAllFields
   Expecting actual not to be equal to: MessageQueueAssignment [..., Mode=POP]
   ```
   
   Pass-after:
   
   ```
   Tests run: 2, Failures: 0, Errors: 0, Skipped: 0 -- 
MessageQueueAssignmentTest
   ```
   


-- 
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