提交 f225fd05 编写于 作者: D dongeforever

Polish the code for command

上级 165e133a
...@@ -116,13 +116,12 @@ public class TopicQueueMappingUtils { ...@@ -116,13 +116,12 @@ public class TopicQueueMappingUtils {
return detailList; return detailList;
} }
public static Map.Entry<Long, Integer> validConsistenceOfTopicConfigAndQueueMapping(Map<String, TopicConfigAndQueueMapping> brokerConfigMap) { public static Map.Entry<Long, Integer> checkConsistenceOfTopicConfigAndQueueMapping(String topic, Map<String, TopicConfigAndQueueMapping> brokerConfigMap) {
if (brokerConfigMap == null if (brokerConfigMap == null
|| brokerConfigMap.isEmpty()) { || brokerConfigMap.isEmpty()) {
return null; return null;
} }
//make sure it it not null //make sure it it not null
String topic = null;
long maxEpoch = -1; long maxEpoch = -1;
int maxNum = -1; int maxNum = -1;
for (Map.Entry<String, TopicConfigAndQueueMapping> entry : brokerConfigMap.entrySet()) { for (Map.Entry<String, TopicConfigAndQueueMapping> entry : brokerConfigMap.entrySet()) {
...@@ -143,9 +142,7 @@ public class TopicQueueMappingUtils { ...@@ -143,9 +142,7 @@ public class TopicQueueMappingUtils {
} }
if (topic != null if (topic != null
&& !topic.equals(mappingDetail.getTopic())) { && !topic.equals(mappingDetail.getTopic())) {
throw new RuntimeException("The topic name is inconsistent in broker " + broker); throw new RuntimeException("The topic name is not match for broker " + broker);
} else {
topic = mappingDetail.getTopic();
} }
if (maxEpoch != -1 if (maxEpoch != -1
...@@ -165,7 +162,7 @@ public class TopicQueueMappingUtils { ...@@ -165,7 +162,7 @@ public class TopicQueueMappingUtils {
return new AbstractMap.SimpleEntry<Long, Integer>(maxEpoch, maxNum); return new AbstractMap.SimpleEntry<Long, Integer>(maxEpoch, maxNum);
} }
public static Map<Integer, TopicQueueMappingOne> buildMappingItems(List<TopicQueueMappingDetail> mappingDetailList, boolean replace, boolean checkConsistence) { public static Map<Integer, TopicQueueMappingOne> checkAndBuildMappingItems(List<TopicQueueMappingDetail> mappingDetailList, boolean replace, boolean checkConsistence) {
Collections.sort(mappingDetailList, new Comparator<TopicQueueMappingDetail>() { Collections.sort(mappingDetailList, new Comparator<TopicQueueMappingDetail>() {
@Override @Override
public int compare(TopicQueueMappingDetail o1, TopicQueueMappingDetail o2) { public int compare(TopicQueueMappingDetail o1, TopicQueueMappingDetail o2) {
...@@ -216,10 +213,14 @@ public class TopicQueueMappingUtils { ...@@ -216,10 +213,14 @@ public class TopicQueueMappingUtils {
return items.get(items.size() - 1); return items.get(items.size() - 1);
} }
public static String writeToTemp(TopicRemappingDetailWrapper wrapper, String suffix) { public static String writeToTemp(TopicRemappingDetailWrapper wrapper, boolean after) {
String topic = wrapper.getTopic(); String topic = wrapper.getTopic();
String data = wrapper.toJson(); String data = wrapper.toJson();
String fileName = System.getProperty("java.io.tmpdir") + File.separator + topic + "-" + wrapper.getEpoch() + "-" + suffix; String suffix = TopicRemappingDetailWrapper.SUFFIX_BEFORE;
if (after) {
suffix = TopicRemappingDetailWrapper.SUFFIX_AFTER;
}
String fileName = System.getProperty("java.io.tmpdir") + File.separator + topic + "-" + wrapper.getEpoch() + suffix;
try { try {
MixAll.string2File(data, fileName); MixAll.string2File(data, fileName);
return fileName; return fileName;
......
...@@ -3,12 +3,17 @@ package org.apache.rocketmq.common.statictopic; ...@@ -3,12 +3,17 @@ package org.apache.rocketmq.common.statictopic;
import org.apache.rocketmq.remoting.protocol.RemotingSerializable; import org.apache.rocketmq.remoting.protocol.RemotingSerializable;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set;
public class TopicRemappingDetailWrapper extends RemotingSerializable { public class TopicRemappingDetailWrapper extends RemotingSerializable {
public static final String TYPE_CREATE_OR_UPDATE = "CREATE_OR_UPDATE"; public static final String TYPE_CREATE_OR_UPDATE = "CREATE_OR_UPDATE";
public static final String TYPE_REMAPPING = "REMAPPING"; public static final String TYPE_REMAPPING = "REMAPPING";
public static final String SUFFIX_BEFORE = ".before";
public static final String SUFFIX_AFTER = ".after";
private final String topic; private final String topic;
private final String type; private final String type;
...@@ -17,6 +22,10 @@ public class TopicRemappingDetailWrapper extends RemotingSerializable { ...@@ -17,6 +22,10 @@ public class TopicRemappingDetailWrapper extends RemotingSerializable {
private Map<String, TopicConfigAndQueueMapping> brokerConfigMap = new HashMap<String, TopicConfigAndQueueMapping>(); private Map<String, TopicConfigAndQueueMapping> brokerConfigMap = new HashMap<String, TopicConfigAndQueueMapping>();
private Set<String> brokerToMapIn = new HashSet<String>();
private Set<String> brokerToMapOut = new HashSet<String>();
public TopicRemappingDetailWrapper(String topic, String type, long epoch, Map<Integer, String> expectedIdToBroker, Map<String, TopicConfigAndQueueMapping> brokerConfigMap) { public TopicRemappingDetailWrapper(String topic, String type, long epoch, Map<Integer, String> expectedIdToBroker, Map<String, TopicConfigAndQueueMapping> brokerConfigMap) {
this.topic = topic; this.topic = topic;
this.type = type; this.type = type;
...@@ -44,4 +53,20 @@ public class TopicRemappingDetailWrapper extends RemotingSerializable { ...@@ -44,4 +53,20 @@ public class TopicRemappingDetailWrapper extends RemotingSerializable {
public Map<String, TopicConfigAndQueueMapping> getBrokerConfigMap() { public Map<String, TopicConfigAndQueueMapping> getBrokerConfigMap() {
return brokerConfigMap; return brokerConfigMap;
} }
public Set<String> getBrokerToMapIn() {
return brokerToMapIn;
}
public void setBrokerToMapIn(Set<String> brokerToMapIn) {
this.brokerToMapIn = brokerToMapIn;
}
public Set<String> getBrokerToMapOut() {
return brokerToMapOut;
}
public void setBrokerToMapOut(Set<String> brokerToMapOut) {
this.brokerToMapOut = brokerToMapOut;
}
} }
...@@ -39,7 +39,6 @@ import org.apache.rocketmq.tools.admin.DefaultMQAdminExt; ...@@ -39,7 +39,6 @@ import org.apache.rocketmq.tools.admin.DefaultMQAdminExt;
import org.apache.rocketmq.tools.command.SubCommand; import org.apache.rocketmq.tools.command.SubCommand;
import org.apache.rocketmq.tools.command.SubCommandException; import org.apache.rocketmq.tools.command.SubCommandException;
import java.nio.charset.Charset;
import java.util.AbstractMap; import java.util.AbstractMap;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
...@@ -84,7 +83,7 @@ public class UpdateStaticTopicSubCommand implements SubCommand { ...@@ -84,7 +83,7 @@ public class UpdateStaticTopicSubCommand implements SubCommand {
options.addOption(opt); options.addOption(opt);
opt = new Option("f", "mapFile", true, "The map file name"); opt = new Option("f", "mapFile", true, "The map file name");
opt.setRequired(true); opt.setRequired(false);
options.addOption(opt); options.addOption(opt);
return options; return options;
...@@ -99,12 +98,13 @@ public class UpdateStaticTopicSubCommand implements SubCommand { ...@@ -99,12 +98,13 @@ public class UpdateStaticTopicSubCommand implements SubCommand {
ClientMetadata clientMetadata = new ClientMetadata(); ClientMetadata clientMetadata = new ClientMetadata();
try { try {
String topic = commandLine.getOptionValue('t').trim();
String mapFileName = commandLine.getOptionValue('f').trim(); String mapFileName = commandLine.getOptionValue('f').trim();
String mapData = MixAll.file2String(mapFileName); String mapData = MixAll.file2String(mapFileName);
TopicRemappingDetailWrapper wrapper = TopicRemappingDetailWrapper.decode(mapData.getBytes(), TopicRemappingDetailWrapper.class); TopicRemappingDetailWrapper wrapper = TopicRemappingDetailWrapper.decode(mapData.getBytes(), TopicRemappingDetailWrapper.class);
//double check the config //double check the config
TopicQueueMappingUtils.validConsistenceOfTopicConfigAndQueueMapping(wrapper.getBrokerConfigMap()); TopicQueueMappingUtils.checkConsistenceOfTopicConfigAndQueueMapping(topic, wrapper.getBrokerConfigMap());
TopicQueueMappingUtils.buildMappingItems(new ArrayList<>(TopicQueueMappingUtils.getMappingDetailFromConfig(wrapper.getBrokerConfigMap().values())), false, true); TopicQueueMappingUtils.checkAndBuildMappingItems(new ArrayList<>(TopicQueueMappingUtils.getMappingDetailFromConfig(wrapper.getBrokerConfigMap().values())), false, true);
ClusterInfo clusterInfo = defaultMQAdminExt.examineBrokerClusterInfo(); ClusterInfo clusterInfo = defaultMQAdminExt.examineBrokerClusterInfo();
if (clusterInfo == null if (clusterInfo == null
...@@ -131,6 +131,11 @@ public class UpdateStaticTopicSubCommand implements SubCommand { ...@@ -131,6 +131,11 @@ public class UpdateStaticTopicSubCommand implements SubCommand {
@Override @Override
public void execute(final CommandLine commandLine, final Options options, public void execute(final CommandLine commandLine, final Options options,
RPCHook rpcHook) throws SubCommandException { RPCHook rpcHook) throws SubCommandException {
if (!commandLine.hasOption('t')) {
ServerUtil.printCommandLineHelp("mqadmin " + this.commandName(), options);
return;
}
if (commandLine.hasOption("f")) { if (commandLine.hasOption("f")) {
executeFromFile(commandLine, options, rpcHook); executeFromFile(commandLine, options, rpcHook);
return; return;
...@@ -159,7 +164,6 @@ public class UpdateStaticTopicSubCommand implements SubCommand { ...@@ -159,7 +164,6 @@ public class UpdateStaticTopicSubCommand implements SubCommand {
} }
clientMetadata.refreshClusterInfo(clusterInfo); clientMetadata.refreshClusterInfo(clusterInfo);
String clusters = commandLine.getOptionValue('c').trim(); String clusters = commandLine.getOptionValue('c').trim();
for (String cluster : clusters.split(",")) { for (String cluster : clusters.split(",")) {
cluster = cluster.trim(); cluster = cluster.trim();
...@@ -200,8 +204,8 @@ public class UpdateStaticTopicSubCommand implements SubCommand { ...@@ -200,8 +204,8 @@ public class UpdateStaticTopicSubCommand implements SubCommand {
Map.Entry<Long, Integer> maxEpochAndNum = new AbstractMap.SimpleImmutableEntry<>(System.currentTimeMillis(), queueNum); Map.Entry<Long, Integer> maxEpochAndNum = new AbstractMap.SimpleImmutableEntry<>(System.currentTimeMillis(), queueNum);
if (!brokerConfigMap.isEmpty()) { if (!brokerConfigMap.isEmpty()) {
maxEpochAndNum = TopicQueueMappingUtils.validConsistenceOfTopicConfigAndQueueMapping(brokerConfigMap); maxEpochAndNum = TopicQueueMappingUtils.checkConsistenceOfTopicConfigAndQueueMapping(topic, brokerConfigMap);
globalIdMap = TopicQueueMappingUtils.buildMappingItems(new ArrayList<>(TopicQueueMappingUtils.getMappingDetailFromConfig(brokerConfigMap.values())), false, true); globalIdMap = TopicQueueMappingUtils.checkAndBuildMappingItems(new ArrayList<>(TopicQueueMappingUtils.getMappingDetailFromConfig(brokerConfigMap.values())), false, true);
} }
if (queueNum < globalIdMap.size()) { if (queueNum < globalIdMap.size()) {
throw new RuntimeException(String.format("Cannot decrease the queue num for static topic %d < %d", queueNum, globalIdMap.size())); throw new RuntimeException(String.format("Cannot decrease the queue num for static topic %d < %d", queueNum, globalIdMap.size()));
...@@ -213,7 +217,7 @@ public class UpdateStaticTopicSubCommand implements SubCommand { ...@@ -213,7 +217,7 @@ public class UpdateStaticTopicSubCommand implements SubCommand {
{ {
TopicRemappingDetailWrapper oldWrapper = new TopicRemappingDetailWrapper(topic, TopicRemappingDetailWrapper.TYPE_CREATE_OR_UPDATE, maxEpochAndNum.getKey(), new HashMap<>(), brokerConfigMap); TopicRemappingDetailWrapper oldWrapper = new TopicRemappingDetailWrapper(topic, TopicRemappingDetailWrapper.TYPE_CREATE_OR_UPDATE, maxEpochAndNum.getKey(), new HashMap<>(), brokerConfigMap);
String oldMappingDataFile = TopicQueueMappingUtils.writeToTemp(oldWrapper, "before"); String oldMappingDataFile = TopicQueueMappingUtils.writeToTemp(oldWrapper, false);
System.out.println("The old mapping data is written to file " + oldMappingDataFile); System.out.println("The old mapping data is written to file " + oldMappingDataFile);
} }
...@@ -264,12 +268,12 @@ public class UpdateStaticTopicSubCommand implements SubCommand { ...@@ -264,12 +268,12 @@ public class UpdateStaticTopicSubCommand implements SubCommand {
configMapping.getMappingDetail().setTotalQueues(queueNum); configMapping.getMappingDetail().setTotalQueues(queueNum);
}); });
//double check the config //double check the config
TopicQueueMappingUtils.validConsistenceOfTopicConfigAndQueueMapping(brokerConfigMap); TopicQueueMappingUtils.checkConsistenceOfTopicConfigAndQueueMapping(topic, brokerConfigMap);
TopicQueueMappingUtils.buildMappingItems(new ArrayList<>(TopicQueueMappingUtils.getMappingDetailFromConfig(brokerConfigMap.values())), false, true); TopicQueueMappingUtils.checkAndBuildMappingItems(new ArrayList<>(TopicQueueMappingUtils.getMappingDetailFromConfig(brokerConfigMap.values())), false, true);
{ {
TopicRemappingDetailWrapper newWrapper = new TopicRemappingDetailWrapper(topic, TopicRemappingDetailWrapper.TYPE_CREATE_OR_UPDATE, newEpoch, newIdToBroker, brokerConfigMap); TopicRemappingDetailWrapper newWrapper = new TopicRemappingDetailWrapper(topic, TopicRemappingDetailWrapper.TYPE_CREATE_OR_UPDATE, newEpoch, newIdToBroker, brokerConfigMap);
String newMappingDataFile = TopicQueueMappingUtils.writeToTemp(newWrapper, "after"); String newMappingDataFile = TopicQueueMappingUtils.writeToTemp(newWrapper, true);
System.out.println("The new mapping data is written to file " + newMappingDataFile); System.out.println("The new mapping data is written to file " + newMappingDataFile);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册