1. Broker starts registering with NameServer
When the main method of the BrokerStartup class runs, the BrokerController is created, and then the start method of the BrokerController is invoked, which has the following code
// Initiate registration for each Name Server at startup this.registerBrokerAll(true, false, true); this.scheduledExecutorService.scheduleAtFixedRate(new Runnable() { @Override public void run() { try { // Broker registers with NameSrv every 30 seconds and updates its topic information BrokerController.this.registerBrokerAll(true, false, brokerConfig.isForceRegister()); } catch (Throwable e) { log.error("registerBrokerAll Exception", e); } } }, 1000 * 10, Math.max(10000, Math.min(brokerConfig.getRegisterNameServerPeriod(), 60000)), TimeUnit.MILLISECONDS);
First, each Name Server is registered at Broker startup.
// true false true public synchronized void registerBrokerAll(final boolean checkOrderConfig, boolean oneway, boolean forceRegister) { // ConcurrentMap<String, TopicConfig> topicConfigTable // Encapsulate topicConfigTable into TopicConfigSerialize Wrapper TopicConfigSerializeWrapper topicConfigWrapper = this.getTopicConfigManager().buildTopicConfigSerializeWrapper(); // Determine permissions if they are unreadable or unwritable for so long. Assemble topicConfigTable into TopicConfigSerialize Wrapper if (!PermName.isWriteable(this.getBrokerConfig().getBrokerPermission()) || !PermName.isReadable(this.getBrokerConfig().getBrokerPermission())) { ConcurrentHashMap<String, TopicConfig> topicConfigTable = new ConcurrentHashMap<String, TopicConfig>(); for (TopicConfig topicConfig : topicConfigWrapper.getTopicConfigTable().values()) { TopicConfig tmp = new TopicConfig(topicConfig.getTopicName(), topicConfig.getReadQueueNums(), topicConfig.getWriteQueueNums(), this.brokerConfig.getBrokerPermission()); topicConfigTable.put(topicConfig.getTopicName(), tmp); } topicConfigWrapper.setTopicConfigTable(topicConfigTable); } // Is forceRegister mandatory for registration? // The needRegister method communicates with each Name Server configured to determine whether topicConfigTable has changed, as long as one of the changes takes so long to initiate registration. // QUERY_DATA_VERSION = 322; if (forceRegister || needRegister(this.brokerConfig.getBrokerClusterName(), this.getBrokerAddr(), this.brokerConfig.getBrokerName(), this.brokerConfig.getBrokerId(), this.brokerConfig.getRegisterBrokerTimeoutMills())) { // REGISTER_BROKER = 103; doRegisterBrokerAll(checkOrderConfig, oneway, topicConfigWrapper); } } private void doRegisterBrokerAll(boolean checkOrderConfig, boolean oneway, TopicConfigSerializeWrapper topicConfigWrapper) { List<RegisterBrokerResult> registerBrokerResultList = this.brokerOuterAPI.registerBrokerAll( this.brokerConfig.getBrokerClusterName(), this.getBrokerAddr(), this.brokerConfig.getBrokerName(), this.brokerConfig.getBrokerId(), this.getHAServerAddr(), topicConfigWrapper, this.filterServerManager.buildNewFilterServerList(), oneway, this.brokerConfig.getRegisterBrokerTimeoutMills(), this.brokerConfig.isCompressedRegister()); if (registerBrokerResultList.size() > 0) { RegisterBrokerResult registerBrokerResult = registerBrokerResultList.get(0); if (registerBrokerResult != null) { // According to the updateMasterHAServerAddrPeriodically annotation bit (if there is no haMasterAddress parameter configuration in the Broker configuration file at initialization, it is marked as true, indicating that the main Broker address needs to be updated after registration) // And whether the HaServer Addr address returned by NameServer is empty, if the tag bit is true and the returned HaServer Addr is not empty, the value of HAService. HAClient. master Address is updated with the HaServer Addr address; // This HAClient. master Address value is used for commitlog data synchronization between primary and standby Broker s if (this.updateMasterHAServerAddrPeriodically && registerBrokerResult.getHaServerAddr() != null) { this.messageStore.updateHaMasterAddress(registerBrokerResult.getHaServerAddr()); } // Update the SlaveSynchronize. master Addr value with MasterAddr value returned by NameServer for the use of master and standby Broker synchronization Config files; this.slaveSynchronize.setMasterAddr(registerBrokerResult.getMasterAddr()); if (checkOrderConfig) { this.getTopicConfigManager().updateOrderTopicConfig(registerBrokerResult.getKvTable()); } } } }
In NameServer, the DefaultRequestProcessor class handles requests, and in processRequest, different methods are invoked according to different request code s.
2. Send Heart Rate
The heartbeat Executor is initialize d in the initialization method of BrokeController; then the thread pool task is filled in by registerProcessor.
// Heart Rate Management Thread Pool this.heartbeatExecutor = new BrokerFixedThreadPoolExecutor( this.brokerConfig.getHeartbeatThreadPoolNums(), this.brokerConfig.getHeartbeatThreadPoolNums(), 1000 * 60, TimeUnit.MILLISECONDS, this.heartbeatThreadPoolQueue, new ThreadFactoryImpl("HeartbeatThread_", true)); this.registerProcessor();
public void registerProcessor() { ...... this.remotingServer.registerProcessor(RequestCode.HEART_BEAT, clientProcessor, this.heartbeatExecutor); this.fastRemotingServer.registerProcessor(RequestCode.HEART_BEAT, clientProcessor, this.heartbeatExecutor); ...... }