org.apache.rocketmq.namesrv.NamesrvControllerjava
NameserController,NameServer的核心控制類。apache
1.1 NamesrvConfig json
NamesrvConfig,主要指定nameserver的相關配置目錄屬性緩存
1)kvConfigPath(kvConfig.json)安全
2)mqhome/namesrv/namesrv.properties併發
3)orderMessageEnable,是否開啓順序消息功能,默認爲falseapp
1.2 ScheduledExecutorService scheduledExecutorService異步
private final ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryIm pl("NSScheduledThread"));ide
NameServer 定時任務執行線程池,一個線程,默認定時執行兩個任務:this
任務一、每隔10s掃描broker,維護當前存活的Broker信息
任務二、每隔10s打印KVConfig信息。
1.3 KVConfigManager
讀取或變動NameServer的配置屬性,加載NamesrvConfig中配置的配置文件到內存,此類一個亮點就是使用輕量級的非線程安全容器,再結合讀寫鎖對資源讀寫進行保護。盡最大程度提升線程的併發度。
1.4 RouteInfoManager
NameServer數據的載體,記錄Broker,Topic等信息。
//NameServer 與 Broker 空閒時長,默認2分鐘,在2分鐘內Nameserver沒有收到Broker的心跳包,則關閉該鏈接。
private final static long BROKER_CHANNEL_EXPIRED_TIME = 1000 * 60 * 2; //讀寫鎖,用來保護非線程安全容器HashMap
private final ReadWriteLock lock = new ReentrantReadWriteLock(); //topicQueueTable,主題與隊列關係,記錄一個主題的隊列分佈在哪些Broker上,每一個Broker上存在該主題的隊列個數
private final HashMap<String/* topic */, List<QueueData>> topicQueueTable; //brokerAddrTable,全部Broker信息,使用brokerName當key,BrokerData信息描述每個broker信息。
private final HashMap<String/* brokerName */, BrokerData> brokerAddrTable; //clusterAddrTable,broker集羣信息,每一個集羣包含哪些Broker
private final HashMap<String/* clusterName */, Set<String/* brokerName */>> clusterAddrTable; //brokerLiveTable,當前存活的Broker,該信息不是實時的,NameServer每10S掃描一次全部的broker,根據心跳包的時間得知broker的狀態,該機制也是致使當一個master Down掉後,消息生產者沒法感知,可能繼續向Down掉的Master發送消息,致使失敗
private final HashMap<String/* brokerAddr */, BrokerLiveInfo> brokerLiveTable; private final HashMap<String/* brokerAddr */, List<String>/* Filter Server */> filterServerTable;
/** * Broker信息;key爲brokerName,value爲BrokerData */
private final HashMap<String/* brokerName */, BrokerData> brokerAddrTable; public class BrokerData implements Comparable<BrokerData> { /** * Cluster名稱 */
private String cluster; /** * broker名稱 */
private String brokerName; /** * 0->ip:port */
private HashMap<Long/* brokerId */, String/* broker address */> brokerAddrs; }
/** * 消息隊列路由信息;key爲topic,value爲QueueData */
private final HashMap<String/* topic */, List<QueueData>> topicQueueTable; public class QueueData implements Comparable<QueueData> { /** * Broker名稱 */
private String brokerName; /** * 讀隊列個數,默認4個 */
private int readQueueNums; /** * 寫隊列個數,默認4個 */
private int writeQueueNums; /** * 隊列權限 */
private int perm; /** * 配置的,同步複製仍是異步複製標記,對應TopicConfig.topicSysFlag * */
private int topicSynFlag; }
/** * Broker狀態信息,NameServer每次收到心跳包會替換該信息,每隔30秒更新一次 * brokerAddr: ip:port->{} */
private final HashMap<String/* brokerAddr */, BrokerLiveInfo> brokerLiveTable; class BrokerLiveInfo { /** * 存儲上次收到心跳包的時間,每隔30秒更新一次 */
private long lastUpdateTimestamp; private DataVersion dataVersion; private Channel channel; private String haServerAddr; }
nettyServerConfig.setListenPort(9876)
//初始化NameServer
boolean initResult = controller.initialize(); public boolean initialize() { //加載KV配置
this.kvConfigManager.load(); this.remotingServer = new NettyRemotingServer(this.nettyServerConfig, this.brokerHousekeepingService); this.remotingExecutor = Executors.newFixedThreadPool(nettyServerConfig.getServerWorkerThreads(), new ThreadFactoryImpl("RemotingExecutorThread_")); this.registerProcessor(); //每10秒鐘掃描一次,移除失效的broker
this.scheduledExecutorService.scheduleAtFixedRate(new Runnable() { @Override public void run() { NamesrvController.this.routeInfoManager.scanNotActiveBroker(); } }, 5, 10, TimeUnit.SECONDS); //每隔10秒鐘打印一次KV配置信息
this.scheduledExecutorService.scheduleAtFixedRate(new Runnable() { @Override public void run() { NamesrvController.this.kvConfigManager.printAllPeriodically(); } }, 1, 10, TimeUnit.MINUTES); return true; }
/** * 失效時間爲2分鐘,即:Broker在2分鐘內未上報心跳會被移除 */
public void scanNotActiveBroker() { Iterator<Entry<String, BrokerLiveInfo>> it = this.brokerLiveTable.entrySet().iterator(); while (it.hasNext()) { Entry<String, BrokerLiveInfo> next = it.next(); long last = next.getValue().getLastUpdateTimestamp(); if ((last + BROKER_CHANNEL_EXPIRED_TIME) < System.currentTimeMillis()) { RemotingUtil.closeChannel(next.getValue().getChannel()); it.remove(); log.warn("The broker channel expired, {} {}ms", next.getKey(), BROKER_CHANNEL_EXPIRED_TIME); this.onChannelDestroy(next.getKey(), next.getValue().getChannel()); } } } private final static long BROKER_CHANNEL_EXPIRED_TIME = 1000 * 60 * 2;
public RemotingCommand processRequest(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { if (log.isDebugEnabled()) { log.debug("receive request, {} {} {}", request.getCode(), RemotingHelper.parseChannelRemoteAddr(ctx.channel()), request); } switch (request.getCode()) { case RequestCode.PUT_KV_CONFIG://增長NameServer配置信息;由DefaultMQAdminExt使用
return this.putKVConfig(ctx, request); case RequestCode.GET_KV_CONFIG://根據NameSpace和key獲取NameServer配置信息;由DefaultMQAdminExt使用
return this.getKVConfig(ctx, request); case RequestCode.DELETE_KV_CONFIG: //據NameSapce和Key刪除NameServerr配置信息
return this.deleteKVConfig(ctx, request); case RequestCode.REGISTER_BROKER: //註冊Broker信息;由BrokerOuterAPI.registerBroker使用,在BrokerController啓動時調用
Version brokerVersion = MQVersion.value2Version(request.getVersion()); if (brokerVersion.ordinal() >= MQVersion.Version.V3_0_11.ordinal()) { return this.registerBrokerWithFilterServer(ctx, request); } else { return this.registerBroker(ctx, request); } case RequestCode.UNREGISTER_BROKER://移除註銷broker信息;由BrokerOuterAPI.unregisterBroker使用,在BrokerController.shutdown時調用
return this.unregisterBroker(ctx, request); case RequestCode.GET_ROUTEINTO_BY_TOPIC: //獲取Topic路由信息 TopicRouteData
return this.getRouteInfoByTopic(ctx, request); case RequestCode.GET_BROKER_CLUSTER_INFO://獲取Cluster及Broker信息
return this.getBrokerClusterInfo(ctx, request); case RequestCode.WIPE_WRITE_PERM_OF_BROKER: //去除該broker上全部topic的寫權限
return this.wipeWritePermOfBroker(ctx, request); case RequestCode.GET_ALL_TOPIC_LIST_FROM_NAMESERVER: //獲取全部的Topic列表
return getAllTopicListFromNameserver(ctx, request); case RequestCode.DELETE_TOPIC_IN_NAMESRV: //從nameServer中刪除topic
return deleteTopicInNamesrv(ctx, request); case RequestCode.GET_KVLIST_BY_NAMESPACE: //獲取配置信息 configTable
return this.getKVListByNamespace(ctx, request); case RequestCode.GET_TOPICS_BY_CLUSTER: //獲取該集羣下的全部topic list
return this.getTopicsByCluster(ctx, request); case RequestCode.GET_SYSTEM_TOPIC_LIST_FROM_NS: // 此處意思爲:系統會將集羣名稱、broker名稱做爲默認topic建立。如今獲取這類topic
return this.getSystemTopicListFromNs(ctx, request); case RequestCode.GET_UNIT_TOPIC_LIST: //暫無使用
return this.getUnitTopicList(ctx, request); case RequestCode.GET_HAS_UNIT_SUB_TOPIC_LIST: //暫無使用
return this.getHasUnitSubTopicList(ctx, request); case RequestCode.GET_HAS_UNIT_SUB_UNUNIT_TOPIC_LIST://暫無使用
return this.getHasUnitSubUnUnitTopicList(ctx, request); case RequestCode.UPDATE_NAMESRV_CONFIG: //更新properties請求
return this.updateConfig(ctx, request); case RequestCode.GET_NAMESRV_CONFIG: //獲取properties內容
return this.getConfig(ctx, request); default: break; } return null; }
註冊broker信息
BrokerController.start()->this.registerBrokerAll()->this.brokerOuterAPI.registerBrokerAll()
//每隔30秒向全部的NameServer上報Topic註冊信息
this.scheduledExecutorService.scheduleAtFixedRate(new Runnable() { @Override public void run() { try { BrokerController.this.registerBrokerAll(true, false); } catch (Throwable e) { log.error("registerBrokerAll Exception", e); } } }, 1000 * 10, 1000 * 30, TimeUnit.MILLISECONDS);
DefaultRequestProcessor->processRequest->RequestCode.REGISTER_BROKER->this.registerBroker->RouteInfoManager.registerBroker()
public RegisterBrokerResult registerBroker( final String clusterName, final String brokerAddr, final String brokerName, final long brokerId, final String haServerAddr, final TopicConfigSerializeWrapper topicConfigWrapper, final List<String> filterServerList, final Channel channel) { RegisterBrokerResult result = new RegisterBrokerResult(); try { try { //加寫鎖,防止併發修改
this.lock.writeLock().lockInterruptibly(); //註冊集羣信息
Set<String> brokerNames = this.clusterAddrTable.get(clusterName); if (null == brokerNames) { brokerNames = new HashSet<String>(); this.clusterAddrTable.put(clusterName, brokerNames); } brokerNames.add(brokerName); boolean registerFirst = false; //註冊broker信息
BrokerData brokerData = this.brokerAddrTable.get(brokerName); if (null == brokerData) { registerFirst = true; brokerData = new BrokerData(clusterName, brokerName, new HashMap<Long, String>()); this.brokerAddrTable.put(brokerName, brokerData); } String oldAddr = brokerData.getBrokerAddrs().put(brokerId, brokerAddr); registerFirst = registerFirst || (null == oldAddr); //Topic配置變化了;Master Broker第一次註冊或者Topic dataVersion不相同時更新路由信息 //有Topic新增時dataVersion會遞增
if (null != topicConfigWrapper // && MixAll.MASTER_ID == brokerId) { if (this.isBrokerTopicConfigChanged(brokerAddr, topicConfigWrapper.getDataVersion())// || registerFirst) { ConcurrentMap<String, TopicConfig> tcTable = topicConfigWrapper.getTopicConfigTable(); if (tcTable != null) { for (Map.Entry<String, TopicConfig> entry : tcTable.entrySet()) { this.createAndUpdateQueueData(brokerName, entry.getValue()); //更新topicQueueTable
} } } } //更新broker心跳信息
BrokerLiveInfo prevBrokerLiveInfo = this.brokerLiveTable.put(brokerAddr, new BrokerLiveInfo( System.currentTimeMillis(), topicConfigWrapper.getDataVersion(), channel, haServerAddr)); //新broker註冊時會有日誌輸出
if (null == prevBrokerLiveInfo) { log.info("new broker registerd, {} HAServer: {}", brokerAddr, haServerAddr); } //更新filterServer信息
if (filterServerList != null) { if (filterServerList.isEmpty()) { this.filterServerTable.remove(brokerAddr); } else { this.filterServerTable.put(brokerAddr, filterServerList); } } //Slave設置MasterAddr和HaServerAddr
if (MixAll.MASTER_ID != brokerId) { String masterAddr = brokerData.getBrokerAddrs().get(MixAll.MASTER_ID); if (masterAddr != null) { BrokerLiveInfo brokerLiveInfo = this.brokerLiveTable.get(masterAddr); if (brokerLiveInfo != null) { result.setHaServerAddr(brokerLiveInfo.getHaServerAddr()); result.setMasterAddr(masterAddr); } } } } finally { this.lock.writeLock().unlock(); } } catch (Exception e) { log.error("registerBroker Exception", e); } return result; }