xiaobing395845388 commented on a change in pull request #1267: Disscuss about
hash algorithm in message queue selector.
URL: https://github.com/apache/rocketmq/pull/1267#discussion_r294254016
##########
File path:
client/src/main/java/org/apache/rocketmq/client/producer/selector/SelectMessageQueueByHash.java
##########
@@ -25,12 +26,12 @@
@Override
public MessageQueue select(List<MessageQueue> mqs, Message msg, Object
arg) {
- int value = arg.hashCode();
- if (value < 0) {
- value = Math.abs(value);
- }
+ return mqs.get(indexForQueue(mqs.size(), arg));
+ }
- value = value % mqs.size();
- return mqs.get(value);
+ private int indexForQueue(int length, Object key) {
+ int h = (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
+ return h & (length - 1);
Review comment:
I have accepted your suggestion, only use algorithm when queue nums is
power of 2 by code 'if ((length & (length - 1)) == 0)', otherwise algorithm
like before
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services