RocketMQ源码阅读(4)-Broker启动向NameServer注册、发送心跳

1、Broker启动向NameServer注册

在BrokerStartup类的main方法运行的时候,创建了BrokerController,然后调用了BrokerController的start方法,在该方法中有如下代码

// 启动的时候向每个NameServer发起注册
this.registerBrokerAll(true, false, true);


this.scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                try {
                    // Broker会每隔30s向NameSrv注册并更新自身topic信息,完成心跳功能
                    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);

首先在Broker启动的时候就会向每个NameServer进行注册。

// true false true
    public synchronized void registerBrokerAll(final boolean checkOrderConfig, boolean oneway, boolean forceRegister) {
        // ConcurrentMap<String, TopicConfig> topicConfigTable
        // 将topicConfigTable封装到TopicConfigSerializeWrapper中
        TopicConfigSerializeWrapper topicConfigWrapper = this.getTopicConfigManager().buildTopicConfigSerializeWrapper();

        // 判断权限如果为不可读或不可写那么久在拼装一下topicConfigTable到TopicConfigSerializeWrapper中
        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);
        }

        // forceRegister是否强制注册
        // needRegister方法是与配置的每个NameServer进行通信,判断topicConfigTable是否改变了,只要其中一个改变了那么久需要发起注册
        // 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) {
                // 根据updateMasterHAServerAddrPeriodically标注位(在初始化时若Broker的配置文件中没有haMasterAddress参数配置,则标记为true,表示注册之后需要更新主用Broker地址)
                // 以及NameServer返回的HaServerAddr地址是否为空,若标记位是true且返回的HaServerAddr不为空,则用HaServerAddr地址更新HAService.HAClient.masterAddress的值;
                // 该HAClient.masterAddress值用于主备Broker之间的commitlog数据同步之用
                if (this.updateMasterHAServerAddrPeriodically && registerBrokerResult.getHaServerAddr() != null) {
                    this.messageStore.updateHaMasterAddress(registerBrokerResult.getHaServerAddr());
                }

                // 用NameServer返回的MasterAddr值更新SlaveSynchronize.masterAddr值,用于主备Broker同步Config文件使用;
                this.slaveSynchronize.setMasterAddr(registerBrokerResult.getMasterAddr());

                if (checkOrderConfig) {
                    this.getTopicConfigManager().updateOrderTopicConfig(registerBrokerResult.getKvTable());
                }
            }
        }
    }

在NameServer中DefaultRequestProcessor类负责处理请求,在processRequest中根据不同的请求code调用不同的方法。

// 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 {
                // 加锁,防止并发修改RouteInfoManager中的路由表
                this.lock.writeLock().lockInterruptibly();

                // 判断broker所属集群是否存在,如果不存在则创建,然后将改broker加入到集群中去
                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;

                // 维护brokerData信息;如果不存在则创建并放入到brokerAddrTable集合中
                // HashMap<String/* brokerName */, BrokerData> brokerAddrTable
                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);
                }
                Map<Long, String> brokerAddrsMap = brokerData.getBrokerAddrs();
                //Switch slave to master: first remove <1, IP:PORT> in namesrv, then add <0, IP:PORT>
                //The same IP:PORT must only have one record in brokerAddrTable
                Iterator<Entry<Long, String>> it = brokerAddrsMap.entrySet().iterator();
                while (it.hasNext()) {
                    Entry<Long, String> item = it.next();
                    if (null != brokerAddr && brokerAddr.equals(item.getValue()) && brokerId != item.getKey()) {
                        it.remove();
                    }
                }

                String oldAddr = brokerData.getBrokerAddrs().put(brokerId, brokerAddr);
                registerFirst = registerFirst || (null == oldAddr);

                // 如果broker为master,并且broker topic配置信息发生变化或者初次注册则需要创建或者更新Topic路由元数据,填充topicQueueTable
                // 就是为默认主题自动注册路由信息,其中包含MixAll.DEFAULT_TOPIC的路由信息。当消息生产者发送主题时,
                // 如果该主题未创建并且BrokerConfig的autoCreateTopicEnable为true时,将返回MixALL.DEFAULT_TOPIC的路由信息
                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());
                            }
                        }
                    }
                }

                // 更新prevBrokerLiveInfo
                BrokerLiveInfo prevBrokerLiveInfo = this.brokerLiveTable.put(brokerAddr,
                    new BrokerLiveInfo(
                        System.currentTimeMillis(),
                        topicConfigWrapper.getDataVersion(),
                        channel,
                        haServerAddr));
                if (null == prevBrokerLiveInfo) {
                    log.info("new broker registered, {} HAServer: {}", brokerAddr, haServerAddr);
                }

                if (filterServerList != null) {
                    if (filterServerList.isEmpty()) {
                        this.filterServerTable.remove(brokerAddr);
                    } else {
                        this.filterServerTable.put(brokerAddr, filterServerList);
                    }
                }

                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;
    }

 

RocketMQ是一个开源的分布式消息中间件,由阿里巴巴开,主要用于大规模的消息传输和处理。阅读RocketMQ源码可以帮助你深入了解其内部工作机制、设计模式以及优化策略。以下是阅读RocketMQ源码的一些步骤和关键点: 1. **入门准备**: - 安装必要的依赖:首先需要对Java和相关开环境(如Maven或Gradle)有一定了解。 - 了解项目结构:熟悉RocketMQ的代码仓库结构,主要包括`mq-client`, `mq-common`, `mq-server`, `mq-externalservice`等主要模块。 2. **理解设计思想**: - 分布式系统设计:研究分区、集群、消息路由、故障恢复机制。 - 消息模型:Consumer Group, Topic, Queue, Orderly Consumers等概念。 - 事务一致性:检查如何处理消息的事务性和持久化。 3. **核心组件**: - **Broker**:查看消息的存储、消费分、事务管理和高可用性实现。 - **Producer**:了解消息发送流程,包括序列化、路由选择、发送确认机制。 - **Consumer**:研究拉取消费模式和顺序消费的实现。 4. **关键技术实现**: - **Message Queueing(MQ)**:学习如何使用队列来存储和分消息。 - **Reentrant Locks**:查看并控制的使用。 - **高可用和容错**:关注心跳检测、主备切换、网络重试策略等。 5. **查阅文档和注释**: - 阅读官方文档、API文档和开者指南,有助于理解源码中各种类和方法的作用。 - 源码中的JavaDoc是非常重要的参考资料。 6. **实践与问题跟踪**: - 尝试在本地搭建并运行RocketMQ,通过实际操作现问题。 - 查阅GitHub上的issue和PR,了解社区对源码的讨论和改进。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值