[ https://issues.apache.org/jira/browse/ROCKETMQ-136?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16174136#comment-16174136 ]
ASF GitHub Bot commented on ROCKETMQ-136: ----------------------------------------- Github user dongeforever commented on a diff in the pull request: https://github.com/apache/incubator-rocketmq/pull/149#discussion_r140136764 --- Diff: client/src/main/java/org/apache/rocketmq/client/producer/selector/SelectMessageQueueByConsistentHash.java --- @@ -0,0 +1,94 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.rocketmq.client.producer.selector; + +import org.apache.rocketmq.client.producer.MessageQueueSelector; +import org.apache.rocketmq.common.consistenthash.ConsistentHashRouter; +import org.apache.rocketmq.common.consistenthash.Node; +import org.apache.rocketmq.common.message.Message; +import org.apache.rocketmq.common.message.MessageQueue; + +import java.util.ArrayList; +import java.util.List; + +public class SelectMessageQueueByConsistentHash implements MessageQueueSelector { + + private final ConsistentHashRouter<MQNode> consistentHashRouter = new ConsistentHashRouter<MQNode>(null, 0); + private volatile List<MessageQueue> lastMqs = new ArrayList<MessageQueue>(); + private final int virtualNodeNum; + + public SelectMessageQueueByConsistentHash() { + this.virtualNodeNum = 10; + } + + public SelectMessageQueueByConsistentHash(int virtualNodeNum) { + this.virtualNodeNum = virtualNodeNum; + } + + @Override + public MessageQueue select(List<MessageQueue> mqs, Message msg, Object arg) { + if (arg == null) { + throw new NullPointerException(); + } + synchronized (consistentHashRouter) { + rehash(mqs); + return consistentHashRouter.routeNode(arg.toString()).mq; + } + } + + private void rehash(List<MessageQueue> mqs) { --- End diff -- How about do a check before allocating two array lists? Check if mqs is the same as lastMqs. For in production environment, the mqs is rarely changed. > Provide a handy message queue producer for order message sharding > ----------------------------------------------------------------- > > Key: ROCKETMQ-136 > URL: https://issues.apache.org/jira/browse/ROCKETMQ-136 > Project: Apache RocketMQ > Issue Type: Improvement > Components: rocketmq-client > Affects Versions: 4.1.0-incubating > Reporter: Jaskey Lam > Assignee: Jaskey Lam > Priority: Minor > Fix For: 4.2.0-incubating > > > When order message is needed, users need to provide a message queue selector > to make sure that the messages which has the same shading key should be sent > to the same message queue. > Actually this is a very common scenario with a common solutions, say > consistent hashing. > We should provide a handy selector for them to easily do that, what they only > need to provide is a sharding key. > A consistent hash selector will meet most of the user's need. -- This message was sent by Atlassian JIRA (v6.4.14#64029)