Github user Jaskey commented on a diff in the pull request: https://github.com/apache/incubator-rocketmq/pull/130#discussion_r126865859 --- Diff: common/src/main/java/org/apache/rocketmq/common/protocol/route/BrokerData.java --- @@ -37,15 +41,21 @@ public BrokerData(String cluster, String brokerName, HashMap<Long, String> broke this.brokerAddrs = brokerAddrs; } + /** + * Selects a (preferably master) broker address from the registered list. + * If the master's address cannot be found, a slave broker address is selected in a random manner. + * + * @return Broker address. + */ public String selectBrokerAddr() { - String value = this.brokerAddrs.get(MixAll.MASTER_ID); - if (null == value) { - for (Map.Entry<Long, String> entry : this.brokerAddrs.entrySet()) { - return entry.getValue(); - } + String addr = this.brokerAddrs.get(MixAll.MASTER_ID); + + if (addr == null) { + List<Long> keys = new ArrayList<Long>(brokerAddrs.keySet()); + return brokerAddrs.get(keys.get(random.nextInt(keys.size()))); --- End diff -- @shroman use `values()` will benefit from one less method call of `map.get(i)`
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---