提交 daf47490 编写于 作者: D dongeforever

Polish the use of route data

上级 40184357
...@@ -10,8 +10,11 @@ import org.apache.rocketmq.common.protocol.route.TopicRouteData; ...@@ -10,8 +10,11 @@ import org.apache.rocketmq.common.protocol.route.TopicRouteData;
import org.apache.rocketmq.logging.InternalLogger; import org.apache.rocketmq.logging.InternalLogger;
import org.apache.rocketmq.logging.InternalLoggerFactory; import org.apache.rocketmq.logging.InternalLoggerFactory;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ConcurrentMap;
...@@ -93,33 +96,46 @@ public class ClientMetadata { ...@@ -93,33 +96,46 @@ public class ClientMetadata {
|| route.getTopicQueueMappingByBroker().isEmpty()) { || route.getTopicQueueMappingByBroker().isEmpty()) {
return new ConcurrentHashMap<MessageQueue, String>(); return new ConcurrentHashMap<MessageQueue, String>();
} }
ConcurrentMap<MessageQueue, String> mqEndPoints = new ConcurrentHashMap<MessageQueue, String>(); ConcurrentMap<MessageQueue, TopicQueueMappingInfo> mqEndPoints = new ConcurrentHashMap<MessageQueue, TopicQueueMappingInfo>();
int totalNums = 0;
for (Map.Entry<String, TopicQueueMappingInfo> entry : route.getTopicQueueMappingByBroker().entrySet()) { List<Map.Entry<String, TopicQueueMappingInfo>> mappingInfos = new ArrayList<Map.Entry<String, TopicQueueMappingInfo>>(route.getTopicQueueMappingByBroker().entrySet());
String brokerName = entry.getKey(); Collections.sort(mappingInfos, new Comparator<Map.Entry<String, TopicQueueMappingInfo>>() {
//TODO check the epoch of @Override
if (entry.getValue().getTotalQueues() > totalNums) { public int compare(Map.Entry<String, TopicQueueMappingInfo> o1, Map.Entry<String, TopicQueueMappingInfo> o2) {
if (totalNums != 0) { return (int) (o2.getValue().getEpoch() - o1.getValue().getEpoch());
log.warn("The static logic queue totalNums dose not match before {} {} != {}", topic, totalNums, entry.getValue().getTotalQueues()); }
} });
totalNums = entry.getValue().getTotalQueues();
int maxTotalNums = 0;
long maxTotalNumOfEpoch = -1;
for (Map.Entry<String, TopicQueueMappingInfo> entry : mappingInfos) {
TopicQueueMappingInfo info = entry.getValue();
if (info.getEpoch() >= maxTotalNumOfEpoch && info.getTotalQueues() > maxTotalNums) {
maxTotalNums = entry.getValue().getTotalQueues();
} }
for (Map.Entry<Integer, Integer> idEntry : entry.getValue().getCurrIdMap().entrySet()) { for (Map.Entry<Integer, Integer> idEntry : entry.getValue().getCurrIdMap().entrySet()) {
int globalId = idEntry.getKey(); int globalId = idEntry.getKey();
MessageQueue mq = new MessageQueue(topic, MixAll.LOGICAL_QUEUE_MOCK_BROKER_NAME, globalId); MessageQueue mq = new MessageQueue(topic, MixAll.LOGICAL_QUEUE_MOCK_BROKER_NAME, globalId);
String oldBrokerName = mqEndPoints.put(mq, brokerName); TopicQueueMappingInfo oldInfo = mqEndPoints.get(mq);
log.warn("The static logic queue is duplicated {} {} {} ", mq, oldBrokerName, brokerName); if (oldInfo == null || oldInfo.getEpoch() <= info.getEpoch()) {
mqEndPoints.put(mq, info);
}
} }
} }
ConcurrentMap<MessageQueue, String> mqEndPointsOfBroker = new ConcurrentHashMap<MessageQueue, String>();
//accomplish the static logic queues //accomplish the static logic queues
for (int i = 0; i < totalNums; i++) { for (int i = 0; i < maxTotalNums; i++) {
MessageQueue mq = new MessageQueue(topic, MixAll.LOGICAL_QUEUE_MOCK_BROKER_NAME, i); MessageQueue mq = new MessageQueue(topic, MixAll.LOGICAL_QUEUE_MOCK_BROKER_NAME, i);
if (!mqEndPoints.containsKey(mq)) { if (!mqEndPoints.containsKey(mq)) {
mqEndPoints.put(mq, MixAll.LOGICAL_QUEUE_MOCK_BROKER_NAME_NOT_EXIST); mqEndPointsOfBroker.put(mq, MixAll.LOGICAL_QUEUE_MOCK_BROKER_NAME_NOT_EXIST);
} else {
mqEndPointsOfBroker.put(mq, mqEndPoints.get(mq).getBname());
} }
} }
return mqEndPoints; return mqEndPointsOfBroker;
} }
} }
...@@ -2,14 +2,14 @@ package org.apache.rocketmq.common.statictopic; ...@@ -2,14 +2,14 @@ package org.apache.rocketmq.common.statictopic;
public class LogicQueueMappingItem { public class LogicQueueMappingItem {
private int gen; //generation, mutable private final int gen; // immutable
private int queueId; private final int queueId; //, immutable
private String bname; private final String bname; //important, immutable
private long logicOffset; // the start of the logic offset private long logicOffset; // the start of the logic offset, important, can be changed by command only once
private long startOffset; // the start of the physical offset, included private final long startOffset; // the start of the physical offset, should always be 0, immutable
private long endOffset = -1; // the end of the physical offset, excluded private long endOffset = -1; // the end of the physical offset, excluded, revered -1, mutable
private long timeOfStart = -1; // mutable private long timeOfStart = -1; // mutable, reserved
private long timeOfEnd = -1; // mutable private long timeOfEnd = -1; // mutable, reserved
public LogicQueueMappingItem(int gen, int queueId, String bname, long logicOffset, long startOffset, long endOffset, long timeOfStart, long timeOfEnd) { public LogicQueueMappingItem(int gen, int queueId, String bname, long logicOffset, long startOffset, long endOffset, long timeOfStart, long timeOfEnd) {
this.gen = gen; this.gen = gen;
......
...@@ -45,14 +45,13 @@ public class TopicQueueMappingDetail extends TopicQueueMappingInfo { ...@@ -45,14 +45,13 @@ public class TopicQueueMappingDetail extends TopicQueueMappingInfo {
public void buildIdMap() { public void buildIdMap() {
this.currIdMap = buildIdMap(LEVEL_0); this.currIdMap = buildIdMap(LEVEL_0);
this.prevIdMap = buildIdMap(LEVEL_1);
} }
public ConcurrentMap<Integer, Integer> buildIdMap(int level) { public ConcurrentMap<Integer, Integer> buildIdMap(int level) {
//level 0 means current leader in this broker //level 0 means current leader in this broker
//level 1 means previous leader in this broker //level 1 means previous leader in this broker, reserved for
assert level == LEVEL_0 || level == LEVEL_1; assert level == LEVEL_0 ;
if (hostedQueues == null || hostedQueues.isEmpty()) { if (hostedQueues == null || hostedQueues.isEmpty()) {
return new ConcurrentHashMap<Integer, Integer>(); return new ConcurrentHashMap<Integer, Integer>();
...@@ -67,12 +66,6 @@ public class TopicQueueMappingDetail extends TopicQueueMappingInfo { ...@@ -67,12 +66,6 @@ public class TopicQueueMappingDetail extends TopicQueueMappingInfo {
if (bname.equals(curr.getBname())) { if (bname.equals(curr.getBname())) {
tmpIdMap.put(globalId, curr.getQueueId()); tmpIdMap.put(globalId, curr.getQueueId());
} }
} else if (level == LEVEL_1
&& items.size() >= 2) {
LogicQueueMappingItem prev = items.get(items.size() - 1);
if (bname.equals(prev.getBname())) {
tmpIdMap.put(globalId, prev.getQueueId());
}
} }
} }
return tmpIdMap; return tmpIdMap;
...@@ -120,8 +113,6 @@ public class TopicQueueMappingDetail extends TopicQueueMappingInfo { ...@@ -120,8 +113,6 @@ public class TopicQueueMappingDetail extends TopicQueueMappingInfo {
public TopicQueueMappingInfo cloneAsMappingInfo() { public TopicQueueMappingInfo cloneAsMappingInfo() {
TopicQueueMappingInfo topicQueueMappingInfo = new TopicQueueMappingInfo(this.topic, this.totalQueues, this.bname, this.epoch); TopicQueueMappingInfo topicQueueMappingInfo = new TopicQueueMappingInfo(this.topic, this.totalQueues, this.bname, this.epoch);
topicQueueMappingInfo.currIdMap = this.buildIdMap(LEVEL_0); topicQueueMappingInfo.currIdMap = this.buildIdMap(LEVEL_0);
topicQueueMappingInfo.prevIdMap = this.buildIdMap(LEVEL_1);
return topicQueueMappingInfo; return topicQueueMappingInfo;
} }
......
...@@ -23,7 +23,6 @@ import java.util.concurrent.ConcurrentMap; ...@@ -23,7 +23,6 @@ import java.util.concurrent.ConcurrentMap;
public class TopicQueueMappingInfo extends RemotingSerializable { public class TopicQueueMappingInfo extends RemotingSerializable {
public static final int LEVEL_0 = 0; public static final int LEVEL_0 = 0;
public static final int LEVEL_1 = 1;
String topic; // redundant field String topic; // redundant field
int totalQueues; int totalQueues;
...@@ -32,8 +31,6 @@ public class TopicQueueMappingInfo extends RemotingSerializable { ...@@ -32,8 +31,6 @@ public class TopicQueueMappingInfo extends RemotingSerializable {
boolean dirty; //indicate if the data is dirty boolean dirty; //indicate if the data is dirty
//register to broker to construct the route //register to broker to construct the route
transient ConcurrentMap<Integer/*logicId*/, Integer/*physicalId*/> currIdMap = new ConcurrentHashMap<Integer, Integer>(); transient ConcurrentMap<Integer/*logicId*/, Integer/*physicalId*/> currIdMap = new ConcurrentHashMap<Integer, Integer>();
//register to broker to help detect remapping failure
transient ConcurrentMap<Integer/*logicId*/, Integer/*physicalId*/> prevIdMap = new ConcurrentHashMap<Integer, Integer>();
public TopicQueueMappingInfo(String topic, int totalQueues, String bname, long epoch) { public TopicQueueMappingInfo(String topic, int totalQueues, String bname, long epoch) {
this.topic = topic; this.topic = topic;
...@@ -79,8 +76,4 @@ public class TopicQueueMappingInfo extends RemotingSerializable { ...@@ -79,8 +76,4 @@ public class TopicQueueMappingInfo extends RemotingSerializable {
public ConcurrentMap<Integer, Integer> getCurrIdMap() { public ConcurrentMap<Integer, Integer> getCurrIdMap() {
return currIdMap; return currIdMap;
} }
public ConcurrentMap<Integer, Integer> getPrevIdMap() {
return prevIdMap;
}
} }
...@@ -168,6 +168,7 @@ public class RouteInfoManager { ...@@ -168,6 +168,7 @@ public class RouteInfoManager {
if (!topicQueueMappingInfoTable.containsKey(entry.getKey())) { if (!topicQueueMappingInfoTable.containsKey(entry.getKey())) {
topicQueueMappingInfoTable.put(entry.getKey(), new HashMap<String, TopicQueueMappingInfo>()); topicQueueMappingInfoTable.put(entry.getKey(), new HashMap<String, TopicQueueMappingInfo>());
} }
//Note asset brokerName equal entry.getValue().getBname()
topicQueueMappingInfoTable.get(entry.getKey()).put(entry.getValue().getBname(), entry.getValue()); topicQueueMappingInfoTable.get(entry.getKey()).put(entry.getValue().getBname(), entry.getValue());
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册