You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@rocketmq.apache.org by dongeforever <gi...@git.apache.org> on 2017/09/21 02:28:02 UTC

[GitHub] incubator-rocketmq pull request #149: [ROCKETMQ-136]Provide a handy message ...

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.


---