Skip to content

Commit

Permalink
[ROCKETMQ-243] Avoid picking the 1st element in the list of addresses…
Browse files Browse the repository at this point in the history
… for BrokerData#selectBrokerAddr().

Signed-off-by: shroman <rshtykh@yahoo.com>
  • Loading branch information
shroman committed Jul 12, 2017
1 parent 246be9e commit 3672f70
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,21 @@
* limitations under the License.
*/


package org.apache.rocketmq.common.protocol.route;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.Random;
import org.apache.rocketmq.common.MixAll;

public class BrokerData implements Comparable<BrokerData> {
private String cluster;
private String brokerName;
private HashMap<Long/* brokerId */, String/* broker address */> brokerAddrs;

private final Random random = new Random();

public BrokerData() {

}
Expand All @@ -37,15 +40,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<String> addrs = new ArrayList<String>(brokerAddrs.values());
return addrs.get(random.nextInt(addrs.size()));
}

return value;
return addr;
}

public HashMap<Long, String> getBrokerAddrs() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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.common.protocol.route;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

/**
* BrokerData tests.
*/
public class BrokerDataTest {
private static BrokerData brokerData;

@BeforeClass
public static void prepare() {
brokerData = new BrokerData("testCluster", "testBroker",
new HashMap<Long, String>() {{
put(1L, "addr1");
put(2L, "addr2");
put(3L, "addr3");
}});
}

@Test
public void selectBrokerAddr() throws Exception {
List<String> selectedAddr = new ArrayList<String>();

for (int i = 0; i < 5; i++)
selectedAddr.add(brokerData.selectBrokerAddr());

List<String> firstElemList = new ArrayList<String>();

for (int i = 0; i < 5; i++)
firstElemList.add(selectedAddr.get(0));

Assert.assertFalse("Contains same addresses", selectedAddr.equals(firstElemList));
}
}

0 comments on commit 3672f70

Please sign in to comment.