說在前面apache
管理請求 GET_TOPIC_STATS_INFO 獲取topic的狀態信息緩存
源碼解析安全
進入這個方法org.apache.rocketmq.broker.processor.AdminBrokerProcessor#getTopicStatsInfo獲取topic的狀態信息微信
private RemotingCommand getTopicStatsInfo(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(null); final GetTopicStatsInfoRequestHeader requestHeader = (GetTopicStatsInfoRequestHeader) request.decodeCommandCustomHeader(GetTopicStatsInfoRequestHeader.class); final String topic = requestHeader.getTopic(); // 從topic配置信息緩存中獲取topic配置信息 TopicConfig topicConfig = this.brokerController.getTopicConfigManager().selectTopicConfig(topic); if (null == topicConfig) { response.setCode(ResponseCode.TOPIC_NOT_EXIST); response.setRemark("topic[" + topic + "] not exist"); return response; } // 組裝topic狀態信息 TopicStatsTable topicStatsTable = new TopicStatsTable(); for (int i = 0; i < topicConfig.getWriteQueueNums(); i++) { MessageQueue mq = new MessageQueue(); mq.setTopic(topic); mq.setBrokerName(this.brokerController.getBrokerConfig().getBrokerName()); mq.setQueueId(i); // 組裝topic的offset信息 TopicOffset topicOffset = new TopicOffset(); // 根據topic和queueId查詢消息隊列最小offset=》 long min = this.brokerController.getMessageStore().getMinOffsetInQueue(topic, i); if (min < 0) min = 0; // 根據topic和queueId查詢消息隊列的最大offset=》 long max = this.brokerController.getMessageStore().getMaxOffsetInQueue(topic, i); if (max < 0) max = 0; long timestamp = 0; if (max > 0) { // 根據topic、queueId,offset查詢offset存儲的時間=》 timestamp = this.brokerController.getMessageStore().getMessageStoreTimeStamp(topic, i, max - 1); } topicOffset.setMinOffset(min); topicOffset.setMaxOffset(max); topicOffset.setLastUpdateTimestamp(timestamp); topicStatsTable.getOffsetTable().put(mq, topicOffset); } byte[] body = topicStatsTable.encode(); response.setBody(body); response.setCode(ResponseCode.SUCCESS); response.setRemark(null); return response; }
進入這個方法org.apache.rocketmq.store.DefaultMessageStore#getMinOffsetInQueue 按topic、queueId查詢消費隊列的最小的offset數據結構
public long getMinOffsetInQueue(String topic, int queueId) { // 根據topic和queueId查詢消費者隊列 =》 ConsumeQueue logic = this.findConsumeQueue(topic, queueId); if (logic != null) { // 獲取隊列中的最小offset return logic.getMinOffsetInQueue(); } return -1; }
進入這個方法org.apache.rocketmq.store.DefaultMessageStore#findConsumeQueue 按topic、queueId查詢到消費隊列併發
public ConsumeQueue findConsumeQueue(String topic, int queueId) { // 找到topic的全部消息隊列 ConcurrentMap<Integer, ConsumeQueue> map = consumeQueueTable.get(topic); if (null == map) { ConcurrentMap<Integer, ConsumeQueue> newMap = new ConcurrentHashMap<Integer, ConsumeQueue>(128); ConcurrentMap<Integer, ConsumeQueue> oldMap = consumeQueueTable.putIfAbsent(topic, newMap); if (oldMap != null) { map = oldMap; } else { map = newMap; } } // 按queue id查找消費者隊列 ConsumeQueue logic = map.get(queueId); if (null == logic) { ConsumeQueue newLogic = new ConsumeQueue( topic, queueId, // 消費者隊列存儲地址 user.home/store/consumequeue StorePathConfigHelper.getStorePathConsumeQueue(this.messageStoreConfig.getStorePathRootDir()), // 每一個文件存儲默認30W this.getMessageStoreConfig().getMapedFileSizeConsumeQueue(), this); ConsumeQueue oldLogic = map.putIfAbsent(queueId, newLogic); if (oldLogic != null) { logic = oldLogic; } else { logic = newLogic; } } return logic; }
往上返回到這個方法app
org.apache.rocketmq.store.DefaultMessageStore#getMaxOffsetInQueue 按topic、queueId查詢消費隊列的最大offsetide
public long getMaxOffsetInQueue(String topic, int queueId) { // 根據topic和queueId找到消費者隊列=》 ConsumeQueue logic = this.findConsumeQueue(topic, queueId); if (logic != null) { // 獲取最大的offset =》 long offset = logic.getMaxOffsetInQueue(); return offset; } // 若是不存在指定topic和queueId的消費隊列直接返回0 return 0; }
進入這個方法org.apache.rocketmq.store.DefaultMessageStore#findConsumeQueue 按topic、queueId查詢消費隊列,前面介紹過this
public ConsumeQueue findConsumeQueue(String topic, int queueId) { // 找到topic的全部消息隊列 ConcurrentMap<Integer, ConsumeQueue> map = consumeQueueTable.get(topic); if (null == map) { ConcurrentMap<Integer, ConsumeQueue> newMap = new ConcurrentHashMap<Integer, ConsumeQueue>(128); ConcurrentMap<Integer, ConsumeQueue> oldMap = consumeQueueTable.putIfAbsent(topic, newMap); if (oldMap != null) { map = oldMap; } else { map = newMap; } } // 按queue id查找消費者隊列 ConsumeQueue logic = map.get(queueId); if (null == logic) { ConsumeQueue newLogic = new ConsumeQueue( topic, queueId, // 消費者隊列存儲地址 user.home/store/consumequeue StorePathConfigHelper.getStorePathConsumeQueue(this.messageStoreConfig.getStorePathRootDir()), // 每一個文件存儲默認30W this.getMessageStoreConfig().getMapedFileSizeConsumeQueue(), this); ConsumeQueue oldLogic = map.putIfAbsent(queueId, newLogic); if (oldLogic != null) { logic = oldLogic; } else { logic = newLogic; } } return logic; }
往上返回到這個方法org.apache.rocketmq.store.ConsumeQueue#getMaxOffsetInQueue查詢最大的offset線程
public long getMaxOffsetInQueue() { // =》 return this.mappedFileQueue.getMaxOffset() / CQ_STORE_UNIT_SIZE; }
進入到這個方法
org.apache.rocketmq.store.MappedFileQueue#getMaxOffset
public long getMaxOffset() { // 獲取存儲映射文件隊列中索引位置最大的映射文件=》 MappedFile mappedFile = getLastMappedFile(); if (mappedFile != null) { // 映射文件的起始offset+映射文件的可讀取的索引位置 return mappedFile.getFileFromOffset() + mappedFile.getReadPosition(); } // 若是隊列中沒有存儲映射文件直接返回0 return 0; }
進入到這個方法
org.apache.rocketmq.store.MappedFileQueue#getLastMappedFile()查詢映射文件隊列中的最後一個映射文件
public MappedFile getLastMappedFile() { MappedFile mappedFileLast = null; while (!this.mappedFiles.isEmpty()) { try { mappedFileLast = this.mappedFiles.get(this.mappedFiles.size() - 1); break; } catch (IndexOutOfBoundsException e) { //continue; } catch (Exception e) { log.error("getLastMappedFile has exception.", e); break; } } return mappedFileLast; }
能夠看到這裏線程安全的隊列實現是CopyOnWriteArrayList,這個數據結構是不可變模式無鎖實現,讀操做大於寫操做時候效率很高
// 併發線程安全隊列存儲映射文件 private final CopyOnWriteArrayList<MappedFile> mappedFiles = new CopyOnWriteArrayList<MappedFile>();
往上返回到這個方法
org.apache.rocketmq.store.DefaultMessageStore#getMessageStoreTimeStamp 按topic、queueId、offset查詢offset的存儲時間
@Override public long getMessageStoreTimeStamp(String topic, int queueId, long consumeQueueOffset) { // 按topic和queueId查詢到消費隊列=》 ConsumeQueue logicQueue = this.findConsumeQueue(topic, queueId); if (logicQueue != null) { // 按消費者的offset查詢存儲時間所在的buffer=》 SelectMappedBufferResult result = logicQueue.getIndexBuffer(consumeQueueOffset); // =》 return getStoreTime(result); } return -1; }
進入這個方法
org.apache.rocketmq.store.DefaultMessageStore#findConsumeQueue 按topic、queueId查詢消費隊列,前面介紹過
public ConsumeQueue findConsumeQueue(String topic, int queueId) { // 找到topic的全部消息隊列 ConcurrentMap<Integer, ConsumeQueue> map = consumeQueueTable.get(topic); if (null == map) { ConcurrentMap<Integer, ConsumeQueue> newMap = new ConcurrentHashMap<Integer, ConsumeQueue>(128); ConcurrentMap<Integer, ConsumeQueue> oldMap = consumeQueueTable.putIfAbsent(topic, newMap); if (oldMap != null) { map = oldMap; } else { map = newMap; } } // 按queue id查找消費者隊列 ConsumeQueue logic = map.get(queueId); if (null == logic) { ConsumeQueue newLogic = new ConsumeQueue( topic, queueId, // 消費者隊列存儲地址 user.home/store/consumequeue StorePathConfigHelper.getStorePathConsumeQueue(this.messageStoreConfig.getStorePathRootDir()), // 每一個文件存儲默認30W this.getMessageStoreConfig().getMapedFileSizeConsumeQueue(), this); ConsumeQueue oldLogic = map.putIfAbsent(queueId, newLogic); if (oldLogic != null) { logic = oldLogic; } else { logic = newLogic; } } return logic; }
往上返回到這個方法
org.apache.rocketmq.store.ConsumeQueue#getIndexBuffer 按索引查詢映射buffer
public SelectMappedBufferResult getIndexBuffer(final long startIndex) { int mappedFileSize = this.mappedFileSize; // 獲取最小的物理offset long offset = startIndex * CQ_STORE_UNIT_SIZE; if (offset >= this.getMinLogicOffset()) { // 根據offset查詢映射文件 =》 MappedFile mappedFile = this.mappedFileQueue.findMappedFileByOffset(offset); if (mappedFile != null) { SelectMappedBufferResult result = mappedFile.selectMappedBuffer((int) (offset % mappedFileSize)); return result; } } return null; }
進入這個方法
org.apache.rocketmq.store.MappedFileQueue#findMappedFileByOffset(long) 按offset查詢映射文件
public MappedFile findMappedFileByOffset(final long offset) { // =》 return findMappedFileByOffset(offset, false); }
進入這個方法org.apache.rocketmq.store.MappedFileQueue#findMappedFileByOffset(long, boolean)
public MappedFile findMappedFileByOffset(final long offset, final boolean returnFirstOnNotFound) { try { // 獲取隊列中第一個映射文件 MappedFile firstMappedFile = this.getFirstMappedFile(); // 獲取隊列中最後一個映射文件 MappedFile lastMappedFile = this.getLastMappedFile(); if (firstMappedFile != null && lastMappedFile != null) { // 若是offset不在索引文件的offset範圍內 if (offset < firstMappedFile.getFileFromOffset() || offset >= lastMappedFile.getFileFromOffset() + this.mappedFileSize) { LOG_ERROR.warn("Offset not matched. Request offset: {}, firstOffset: {}, lastOffset: {}, mappedFileSize: {}, mappedFiles count: {}", offset, firstMappedFile.getFileFromOffset(), lastMappedFile.getFileFromOffset() + this.mappedFileSize, this.mappedFileSize, this.mappedFiles.size()); } else { // 找到映射文件在隊列中的索引位置 int index = (int) ((offset / this.mappedFileSize) - (firstMappedFile.getFileFromOffset() / this.mappedFileSize)); MappedFile targetFile = null; try { // 獲取索引文件 targetFile = this.mappedFiles.get(index); } catch (Exception ignored) { } // offset在目標文件的起始offset和結束offset範圍內 if (targetFile != null && offset >= targetFile.getFileFromOffset() && offset < targetFile.getFileFromOffset() + this.mappedFileSize) { return targetFile; } // 若是按索引在隊列中找不到映射文件就遍歷隊列查找映射文件 for (MappedFile tmpMappedFile : this.mappedFiles) { if (offset >= tmpMappedFile.getFileFromOffset() && offset < tmpMappedFile.getFileFromOffset() + this.mappedFileSize) { return tmpMappedFile; } } } // 若是offset=0獲取隊列中第一個映射文件,我的感受這個邏輯是否放在前面判斷更爲合理,仍是放在這裏另有深意 if (returnFirstOnNotFound) { return firstMappedFile; } } } catch (Exception e) { log.error("findMappedFileByOffset Exception", e); } return null; }
往上返回到這個方法org.apache.rocketmq.store.DefaultMessageStore#getStoreTime 根據SelectMappedBufferResult查詢存儲時間
private long getStoreTime(SelectMappedBufferResult result) { if (result != null) { try { final long phyOffset = result.getByteBuffer().getLong(); final int size = result.getByteBuffer().getInt(); // 根據SelectMappedBufferResult的offset和大小查找存儲時間=》 long storeTime = this.getCommitLog().pickupStoreTimestamp(phyOffset, size); return storeTime; } catch (Exception e) { } finally { result.release(); } } return -1; }
進入這個方法org.apache.rocketmq.store.CommitLog#pickupStoreTimestamp 根據offset和大小查詢存儲時間
public long pickupStoreTimestamp(final long offset, final int size) { if (offset >= this.getMinOffset()) { // =》 SelectMappedBufferResult result = this.getMessage(offset, size); if (null != result) { try { // 獲取消息存儲時間 return result.getByteBuffer().getLong(MessageDecoder.MESSAGE_STORE_TIMESTAMP_POSTION); } finally { result.release(); } } } return -1; }
進入這個方法org.apache.rocketmq.store.CommitLog#getMessage 按offset、大小查詢SelectMappedBufferResult
public SelectMappedBufferResult getMessage(final long offset, final int size) { int mappedFileSize = this.defaultMessageStore.getMessageStoreConfig().getMapedFileSizeCommitLog(); // 根據offset找到映射文件 =》 MappedFile mappedFile = this.mappedFileQueue.findMappedFileByOffset(offset, offset == 0); if (mappedFile != null) { int pos = (int) (offset % mappedFileSize); return mappedFile.selectMappedBuffer(pos, size); } return null; }
進入這個方法org.apache.rocketmq.store.MappedFileQueue#findMappedFileByOffset(long, boolean) 按offset查詢映射文件
public MappedFile findMappedFileByOffset(final long offset, final boolean returnFirstOnNotFound) { try { // 獲取隊列中第一個映射文件 MappedFile firstMappedFile = this.getFirstMappedFile(); // 獲取隊列中最後一個映射文件 MappedFile lastMappedFile = this.getLastMappedFile(); if (firstMappedFile != null && lastMappedFile != null) { // 若是offset不在索引文件的offset範圍內 if (offset < firstMappedFile.getFileFromOffset() || offset >= lastMappedFile.getFileFromOffset() + this.mappedFileSize) { LOG_ERROR.warn("Offset not matched. Request offset: {}, firstOffset: {}, lastOffset: {}, mappedFileSize: {}, mappedFiles count: {}", offset, firstMappedFile.getFileFromOffset(), lastMappedFile.getFileFromOffset() + this.mappedFileSize, this.mappedFileSize, this.mappedFiles.size()); } else { // 找到映射文件在隊列中的索引位置 int index = (int) ((offset / this.mappedFileSize) - (firstMappedFile.getFileFromOffset() / this.mappedFileSize)); MappedFile targetFile = null; try { // 獲取索引文件 targetFile = this.mappedFiles.get(index); } catch (Exception ignored) { } // offset在目標文件的起始offset和結束offset範圍內 if (targetFile != null && offset >= targetFile.getFileFromOffset() && offset < targetFile.getFileFromOffset() + this.mappedFileSize) { return targetFile; } // 若是按索引在隊列中找不到映射文件就遍歷隊列查找映射文件 for (MappedFile tmpMappedFile : this.mappedFiles) { if (offset >= tmpMappedFile.getFileFromOffset() && offset < tmpMappedFile.getFileFromOffset() + this.mappedFileSize) { return tmpMappedFile; } } } // 若是offset=0獲取隊列中第一個映射文件,我的感受這個邏輯是否放在前面判斷更爲合理,仍是放在這裏另有深意 if (returnFirstOnNotFound) { return firstMappedFile; } } } catch (Exception e) { log.error("findMappedFileByOffset Exception", e); } return null; }
往上返回到這個方法org.apache.rocketmq.broker.processor.AdminBrokerProcessor#getTopicStatsInfo結束
說在最後
本次解析僅表明我的觀點,僅供參考。
加入技術微信羣
釘釘技術羣