Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ROCKETMQ-278] Add clusterlist cmd by specified cluster #152

Closed
wants to merge 10 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
import org.apache.rocketmq.common.protocol.header.DeleteSubscriptionGroupRequestHeader;
import org.apache.rocketmq.common.protocol.header.DeleteTopicRequestHeader;
import org.apache.rocketmq.common.protocol.header.EndTransactionRequestHeader;
import org.apache.rocketmq.common.protocol.header.GetClusterListRequestHeader;
import org.apache.rocketmq.common.protocol.header.GetConsumeStatsInBrokerHeader;
import org.apache.rocketmq.common.protocol.header.GetConsumeStatsRequestHeader;
import org.apache.rocketmq.common.protocol.header.GetConsumerConnectionListRequestHeader;
Expand Down Expand Up @@ -1167,10 +1168,13 @@ public Properties getBrokerConfig(final String addr, final long timeoutMillis)
throw new MQBrokerException(response.getCode(), response.getRemark());
}

public ClusterInfo getBrokerClusterInfo(
public ClusterInfo getBrokerClusterInfo(String cluster,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how to insure the api compatibility

final long timeoutMillis) throws InterruptedException, RemotingTimeoutException,
RemotingSendRequestException, RemotingConnectException, MQBrokerException {
RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.GET_BROKER_CLUSTER_INFO, null);
GetClusterListRequestHeader requestHeader = new GetClusterListRequestHeader();
String pCluster = cluster != null ? cluster : "";
requestHeader.setCluster(pCluster);
RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.GET_BROKER_CLUSTER_INFO, requestHeader);

RemotingCommand response = this.remotingClient.invokeSync(null, request, timeoutMillis);
assert response != null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.header;

import org.apache.rocketmq.remoting.CommandCustomHeader;
import org.apache.rocketmq.remoting.annotation.CFNullable;
import org.apache.rocketmq.remoting.exception.RemotingCommandException;


public class GetClusterListRequestHeader implements CommandCustomHeader {

@CFNullable
private String cluster;

@Override
public void checkFields() throws RemotingCommandException {

}

public String getCluster() {
return cluster;
}

public void setCluster(String cluster) {
this.cluster = cluster;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.rocketmq.common.protocol.ResponseCode;
import org.apache.rocketmq.common.protocol.body.RegisterBrokerBody;
import org.apache.rocketmq.common.protocol.body.TopicConfigSerializeWrapper;
import org.apache.rocketmq.common.protocol.header.GetClusterListRequestHeader;
import org.apache.rocketmq.common.protocol.header.GetTopicsByClusterRequestHeader;
import org.apache.rocketmq.common.protocol.header.namesrv.DeleteKVConfigRequestHeader;
import org.apache.rocketmq.common.protocol.header.namesrv.DeleteTopicInNamesrvRequestHeader;
Expand Down Expand Up @@ -302,10 +303,10 @@ public RemotingCommand getRouteInfoByTopic(ChannelHandlerContext ctx,
return response;
}

private RemotingCommand getBrokerClusterInfo(ChannelHandlerContext ctx, RemotingCommand request) {
private RemotingCommand getBrokerClusterInfo(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException {
final RemotingCommand response = RemotingCommand.createResponseCommand(null);

byte[] content = this.namesrvController.getRouteInfoManager().getAllClusterInfo();
GetClusterListRequestHeader requestHeader = (GetClusterListRequestHeader)request.decodeCommandCustomHeader(GetClusterListRequestHeader.class);
byte[] content = this.namesrvController.getRouteInfoManager().getAllClusterInfo(requestHeader.getCluster());
response.setBody(content);

response.setCode(ResponseCode.SUCCESS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.common.DataVersion;
import org.apache.rocketmq.common.MixAll;
import org.apache.rocketmq.common.TopicConfig;
Expand Down Expand Up @@ -63,11 +65,36 @@ public RouteInfoManager() {
this.filterServerTable = new HashMap<String, List<String>>(256);
}

public byte[] getAllClusterInfo() {
ClusterInfo clusterInfoSerializeWrapper = new ClusterInfo();
clusterInfoSerializeWrapper.setBrokerAddrTable(this.brokerAddrTable);
clusterInfoSerializeWrapper.setClusterAddrTable(this.clusterAddrTable);
return clusterInfoSerializeWrapper.encode();
public byte[] getAllClusterInfo(String cluster) {
Copy link
Contributor

@shroman shroman Sep 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You change the signature of the public method here.
How about having another method -- public byte[] getAllClusterInfo()? And you won't need calling getAllClusterInfo(null) then too ;)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree @shroman here

if (StringUtils.isNotBlank(cluster)) {
return getOneClusterInfo(cluster);
} else {
ClusterInfo clusterInfoSerializeWrapper = new ClusterInfo();
clusterInfoSerializeWrapper.setBrokerAddrTable(this.brokerAddrTable);
clusterInfoSerializeWrapper.setClusterAddrTable(this.clusterAddrTable);
return clusterInfoSerializeWrapper.encode();
}
}

private byte[] getOneClusterInfo(String cluster) {
HashMap<String, Set<String>> clusterAddr = new HashMap<>();
HashMap<String, BrokerData> brokerAddr = new HashMap<>();

Set<String> brokers = clusterAddrTable.get(cluster);
if (brokers != null) {
for (String broker : brokers) {
BrokerData brokerData = brokerAddrTable.get(broker);
if (brokerData != null) {
brokerAddr.put(broker, brokerData);
}
}
clusterAddr.put(cluster, brokers);
}

ClusterInfo info = new ClusterInfo();
info.setBrokerAddrTable(brokerAddr);
info.setClusterAddrTable(clusterAddr);
return info.encode();
}

public void deleteTopic(final String topic) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
import org.apache.rocketmq.common.namesrv.RegisterBrokerResult;
import org.apache.rocketmq.common.protocol.RequestCode;
import org.apache.rocketmq.common.protocol.ResponseCode;
import org.apache.rocketmq.common.protocol.body.ClusterInfo;
import org.apache.rocketmq.common.protocol.body.TopicConfigSerializeWrapper;
import org.apache.rocketmq.common.protocol.header.GetClusterListRequestHeader;
import org.apache.rocketmq.common.protocol.header.namesrv.DeleteKVConfigRequestHeader;
import org.apache.rocketmq.common.protocol.header.namesrv.GetKVConfigRequestHeader;
import org.apache.rocketmq.common.protocol.header.namesrv.GetKVConfigResponseHeader;
Expand Down Expand Up @@ -76,7 +78,9 @@ public void init() throws Exception {
field.set(namesrvController, routeInfoManager);
defaultRequestProcessor = new DefaultRequestProcessor(namesrvController);

registerRouteInfoManager();
for (int i = 10; i < 15; i++) {
registerRouteInfoManager("a" + i, i);
}

logger = mock(Logger.class);
when(logger.isInfoEnabled()).thenReturn(false);
Expand Down Expand Up @@ -257,20 +261,45 @@ private static void setFinalStatic(Field field, Object newValue) throws Exceptio
field.set(null, newValue);
}

private void registerRouteInfoManager() {
private void registerRouteInfoManager(String prefix, int id) {

TopicConfigSerializeWrapper topicConfigSerializeWrapper = new TopicConfigSerializeWrapper();
ConcurrentHashMap<String, TopicConfig> topicConfigConcurrentHashMap = new ConcurrentHashMap<>();
TopicConfig topicConfig = new TopicConfig();
topicConfig.setWriteQueueNums(8);
topicConfig.setTopicName("unit-test");
topicConfig.setTopicName("unit-test_" + prefix);
topicConfig.setPerm(6);
topicConfig.setReadQueueNums(8);
topicConfig.setOrder(false);
topicConfigConcurrentHashMap.put("unit-test", topicConfig);
topicConfigSerializeWrapper.setTopicConfigTable(topicConfigConcurrentHashMap);

Channel channel = mock(Channel.class);
RegisterBrokerResult registerBrokerResult = routeInfoManager.registerBroker("default-cluster", "127.0.0.1:10911", "default-broker", 1234, "127.0.0.1:1001",
RegisterBrokerResult registerBrokerResult = routeInfoManager.registerBroker("default-cluster_" + prefix, "127.0.0.1:120" + id, "default-broker_" + prefix, 1236 + id, "127.0.0.1:100" + id,
topicConfigSerializeWrapper, new ArrayList<String>(), channel);

}

private static RemotingCommand genClusterListCmd(String cluster) {
GetClusterListRequestHeader header = new GetClusterListRequestHeader();
header.setCluster(cluster);
RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.GET_BROKER_CLUSTER_INFO, header);
return request;
}

@Test
public void testClusterInfo() throws RemotingCommandException {
testClusterInfo(null, 5);
testClusterInfo("default-cluster_a10", 1);
}

public void testClusterInfo(String cluster, int want) throws RemotingCommandException {
ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
when(ctx.channel()).thenReturn(null);
RemotingCommand response = defaultRequestProcessor.processRequest(ctx, genClusterListCmd(cluster));
assertThat(response.getCode()).isEqualTo(ResponseCode.SUCCESS);
assertThat(response.getRemark()).isNull();
ClusterInfo info = ClusterInfo.decode(response.getBody(), ClusterInfo.class);
assertThat(info.getClusterAddrTable().size() == want);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void terminate() {

@Test
public void testGetAllClusterInfo() {
byte[] clusterInfo = routeInfoManager.getAllClusterInfo();
byte[] clusterInfo = routeInfoManager.getAllClusterInfo(null);
assertThat(clusterInfo).isNotNull();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public static ClusterInfo getCluster(String nameSrvAddr) {
ClusterInfo clusterInfo = null;
try {
mqAdminExt.start();
clusterInfo = mqAdminExt.examineBrokerClusterInfo();
clusterInfo = mqAdminExt.examineBrokerClusterInfo(null);
} catch (Exception e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,9 @@ public ConsumeStats examineConsumeStats(String consumerGroup,
}

@Override
public ClusterInfo examineBrokerClusterInfo() throws InterruptedException, RemotingConnectException, RemotingTimeoutException,
public ClusterInfo examineBrokerClusterInfo(String cluster) throws InterruptedException, RemotingConnectException, RemotingTimeoutException,
RemotingSendRequestException, MQBrokerException {
return defaultMQAdminExtImpl.examineBrokerClusterInfo();
return defaultMQAdminExtImpl.examineBrokerClusterInfo(cluster);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,9 @@ public ConsumeStats examineConsumeStats(String consumerGroup,
}

@Override
public ClusterInfo examineBrokerClusterInfo() throws InterruptedException, MQBrokerException, RemotingTimeoutException,
public ClusterInfo examineBrokerClusterInfo(String cluster) throws InterruptedException, MQBrokerException, RemotingTimeoutException,
RemotingSendRequestException, RemotingConnectException {
return this.mqClientInstance.getMQClientAPIImpl().getBrokerClusterInfo(timeoutMillis);
return this.mqClientInstance.getMQClientAPIImpl().getBrokerClusterInfo(cluster,timeoutMillis);
}

@Override
Expand Down Expand Up @@ -626,7 +626,7 @@ public boolean cleanExpiredConsumerQueue(
RemotingTimeoutException, MQClientException, InterruptedException {
boolean result = false;
try {
ClusterInfo clusterInfo = examineBrokerClusterInfo();
ClusterInfo clusterInfo = examineBrokerClusterInfo(cluster);
if (null == cluster || "".equals(cluster)) {
for (String targetCluster : clusterInfo.retrieveAllClusterNames()) {
result = cleanExpiredConsumerQueueByCluster(clusterInfo, targetCluster);
Expand Down Expand Up @@ -666,7 +666,7 @@ public boolean cleanUnusedTopic(String cluster) throws RemotingConnectException,
RemotingTimeoutException, MQClientException, InterruptedException {
boolean result = false;
try {
ClusterInfo clusterInfo = examineBrokerClusterInfo();
ClusterInfo clusterInfo = examineBrokerClusterInfo(cluster);
if (null == cluster || "".equals(cluster)) {
for (String targetCluster : clusterInfo.retrieveAllClusterNames()) {
result = cleanUnusedTopicByCluster(clusterInfo, targetCluster);
Expand Down Expand Up @@ -832,7 +832,7 @@ public boolean consumed(final MessageExt msg,

ConsumeStats cstats = this.examineConsumeStats(group);

ClusterInfo ci = this.examineBrokerClusterInfo();
ClusterInfo ci = this.examineBrokerClusterInfo(null);

Iterator<Entry<MessageQueue, OffsetWrapper>> it = cstats.getOffsetTable().entrySet().iterator();
while (it.hasNext()) {
Expand Down Expand Up @@ -894,7 +894,7 @@ public Set<String> getTopicClusterList(
final String topic) throws InterruptedException, MQBrokerException, MQClientException,
RemotingException {
Set<String> clusterSet = new HashSet<String>();
ClusterInfo clusterInfo = examineBrokerClusterInfo();
ClusterInfo clusterInfo = examineBrokerClusterInfo(null);
TopicRouteData topicRouteData = examineTopicRouteInfo(topic);
BrokerData brokerData = topicRouteData.getBrokerDatas().get(0);
String brokerName = brokerData.getBrokerName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ ConsumeStats examineConsumeStats(final String consumerGroup,
final String topic) throws RemotingException, MQClientException,
InterruptedException, MQBrokerException;

ClusterInfo examineBrokerClusterInfo() throws InterruptedException, MQBrokerException, RemotingTimeoutException,
ClusterInfo examineBrokerClusterInfo(String cluster) throws InterruptedException, MQBrokerException, RemotingTimeoutException,
RemotingSendRequestException, RemotingConnectException;

TopicRouteData examineTopicRouteInfo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class CommandUtil {
MQBrokerException {
Map<String, List<String>> masterAndSlaveMap = new HashMap<String, List<String>>(4);

ClusterInfo clusterInfoSerializeWrapper = adminExt.examineBrokerClusterInfo();
ClusterInfo clusterInfoSerializeWrapper = adminExt.examineBrokerClusterInfo(clusterName);
Set<String> brokerNameSet = clusterInfoSerializeWrapper.getClusterAddrTable().get(clusterName);

if (brokerNameSet == null) {
Expand Down Expand Up @@ -79,7 +79,7 @@ public static Set<String> fetchMasterAddrByClusterName(final MQAdminExt adminExt
RemotingSendRequestException, MQBrokerException {
Set<String> masterSet = new HashSet<String>();

ClusterInfo clusterInfoSerializeWrapper = adminExt.examineBrokerClusterInfo();
ClusterInfo clusterInfoSerializeWrapper = adminExt.examineBrokerClusterInfo(clusterName);

Set<String> brokerNameSet = clusterInfoSerializeWrapper.getClusterAddrTable().get(clusterName);

Expand Down Expand Up @@ -107,7 +107,7 @@ public static Set<String> fetchMasterAndSlaveAddrByClusterName(final MQAdminExt
RemotingSendRequestException, MQBrokerException {
Set<String> masterSet = new HashSet<String>();

ClusterInfo clusterInfoSerializeWrapper = adminExt.examineBrokerClusterInfo();
ClusterInfo clusterInfoSerializeWrapper = adminExt.examineBrokerClusterInfo(clusterName);

Set<String> brokerNameSet = clusterInfoSerializeWrapper.getClusterAddrTable().get(clusterName);

Expand All @@ -129,7 +129,7 @@ public static Set<String> fetchMasterAndSlaveAddrByClusterName(final MQAdminExt

public static Set<String> fetchBrokerNameByClusterName(final MQAdminExt adminExt, final String clusterName)
throws Exception {
ClusterInfo clusterInfoSerializeWrapper = adminExt.examineBrokerClusterInfo();
ClusterInfo clusterInfoSerializeWrapper = adminExt.examineBrokerClusterInfo(clusterName);
Set<String> brokerNameSet = clusterInfoSerializeWrapper.getClusterAddrTable().get(clusterName);
if (brokerNameSet.isEmpty()) {
throw new Exception(
Expand All @@ -139,7 +139,7 @@ public static Set<String> fetchBrokerNameByClusterName(final MQAdminExt adminExt
}

public static String fetchBrokerNameByAddr(final MQAdminExt adminExt, final String addr) throws Exception {
ClusterInfo clusterInfoSerializeWrapper = adminExt.examineBrokerClusterInfo();
ClusterInfo clusterInfoSerializeWrapper = adminExt.examineBrokerClusterInfo(null);
HashMap<String/* brokerName */, BrokerData> brokerAddrTable =
clusterInfoSerializeWrapper.getBrokerAddrTable();
Iterator<Map.Entry<String, BrokerData>> it = brokerAddrTable.entrySet().iterator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) t
defaultMQAdminExt.start();
producer.start();

ClusterInfo clusterInfoSerializeWrapper = defaultMQAdminExt.examineBrokerClusterInfo();
ClusterInfo clusterInfoSerializeWrapper = defaultMQAdminExt.examineBrokerClusterInfo(null);
HashMap<String, Set<String>> clusterAddr = clusterInfoSerializeWrapper
.getClusterAddrTable();

Expand Down