NamerServer

NameServer是一个非常简单的Topic路由注册中心,其角色类似Dubbo中的zookeeper,支持Broker的动态注册与发现。主要包括两个功能:Broker管理,NameServer接受Broker集群的注册信息并且保存下来作为路由信息的基本数据。然后提供心跳检测机制,检查Broker是否还存活;路由信息管理,每个NameServer将保存关于Broker集群的整个路由信息和用于客户端查询的队列信息。然后Producer和Conumser通过NameServer就可以知道整个Broker集群的路由信息,从而进行消息的投递和消费。NameServer通常也是集群的方式部署,各实例间相互不进行信息通讯。Broker是向每一台NameServer注册自己的路由信息,所以每一个NameServer实例上面都保存一份完整的路由信息。当某个NameServer因某种原因下线了,Broker仍然可以向其它NameServer同步其路由信息,Producer,Consumer仍然可以动态感知Broker的路由的信息。

源码解析

NameServer启动流程

填充启动参数

首先需要创建NameServerConfig(NameServer业务参数)、NettyServerConfig(NameServer网络参数)。

org.apache.rocketmq.namesrv.NamesrvStartup#createNamesrvController
// 首先创建NameServer业务参数
final NamesrvConfig namesrvConfig = new NamesrvConfig();
// 然后创建NameServer网络参数
final NettyServerConfig nettyServerConfig = new NettyServerConfig();
// 默认监听端口
nettyServerConfig.setListenPort(9876);
// 通过文件来配置参数
if (commandLine.hasOption('c')) {
    String file = commandLine.getOptionValue('c');
    if (file != null) {
        InputStream in = new BufferedInputStream(new FileInputStream(file));
        properties = new Properties();
        properties.load(in);
        MixAll.properties2Object(properties, namesrvConfig);
        MixAll.properties2Object(properties, nettyServerConfig);

        namesrvConfig.setConfigStorePath(file);

        System.out.printf("load config properties file OK, %s%n", file);
        in.close();
    }
}
// 通过命令行参数
if (commandLine.hasOption('p')) {
    InternalLogger console = InternalLoggerFactory.getLogger(LoggerName.NAMESRV_CONSOLE_NAME);
    MixAll.printObjectProperties(console, namesrvConfig);
    MixAll.printObjectProperties(console, nettyServerConfig);
    System.exit(0);
}

MixAll.properties2Object(ServerUtil.commandLine2Properties(commandLine), namesrvConfig);
复制代码
NamesrvConfig属性
//rocketMQ主目录
private String rocketmqHome = System.getProperty(MixAll.ROCKETMQ_HOME_PROPERTY, System.getenv(MixAll.ROCKETMQ_HOME_ENV));
//存储KV配置属性的持久化路径
private String kvConfigPath = System.getProperty("user.home") + File.separator + "namesrv" + File.separator + "kvConfig.json";
//默认配置文件路径,不生效。
private String configStorePath = System.getProperty("user.home") + File.separator + "namesrv" + File.separator + "namesrv.properties";
private String productEnvName = "center";
private boolean clusterTest = false;
// 是否支持顺序消息,默认是不支持。
private boolean orderMessageEnable = false;
复制代码
NettyServerConfig属性
// 监听端口
private int listenPort = 8888;
//Netty业务线程池线程个数
private int serverWorkerThreads = 8;
//Netty public任务线程数 Netty网络设计,根据不同的业务类型会创建不同的线程池,比如处理消息发送、消息消费、心跳检测等。
//如果该业务类型未注册线程池,则有Public线程池执行。
private int serverCallbackExecutorThreads = 0;
//IO线程池线程个数
private int serverSelectorThreads = 3;
// send oneway消息请求并发度
private int serverOnewaySemaphoreValue = 256;
// 异步消息发送并发度
private int serverAsyncSemaphoreValue = 64;
// 网络连接最大空闲时间
private int serverChannelMaxIdleTimeSeconds = 120;

//Socket发送缓冲区大小
private int serverSocketSndBufSize = NettySystemConfig.socketSndbufSize;
//Socket接受缓冲区大小
private int serverSocketRcvBufSize = NettySystemConfig.socketRcvbufSize;
//ByteBuffer是否开启缓存
private boolean serverPooledByteBufAllocatorEnable = true;
//是否启用Epoll IO模型 
private boolean useEpollNativeSelector = false;
复制代码

根据启动属性创建NamesrvController实例,并初始化该实例。

org.apache.rocketmq.namesrv.NamesrvStartup#start
public static NamesrvController start(final NamesrvController controller) throws Exception {

    if (null == controller) {
        throw new IllegalArgumentException("NamesrvController is null");
    }

    //初始化controller实例
    boolean initResult = controller.initialize();
    if (!initResult) {
        controller.shutdown();
        System.exit(-3);
    }

    //注册JVM钩子函数,在JVM关闭之前先关闭线程池。
    Runtime.getRuntime().addShutdownHook(new ShutdownHookThread(log, new Callable<Void>() {
        @Override
        public Void call() throws Exception {
            controller.shutdown();
            return null;
        }
    }));

    //启动controller
    controller.start();

    return controller;
}
复制代码
org.apache.rocketmq.namesrv.NamesrvController#initialize
public boolean initialize() {

    this.kvConfigManager.load();

    this.remotingServer = new NettyRemotingServer(this.nettyServerConfig, this.brokerHousekeepingService);

    this.remotingExecutor =
        Executors.newFixedThreadPool(nettyServerConfig.getServerWorkerThreads(), new ThreadFactoryImpl("RemotingExecutorThread_"));

    // 用于处理网络请求
    this.registerProcessor();

    // 10秒扫描一次BrokerLiveTable,移除处于不激活状态的Broker
    this.scheduledExecutorService.scheduleAtFixedRate(new Runnable() {

        @Override
        public void run() {
            NamesrvController.this.routeInfoManager.scanNotActiveBroker();
        }
    }, 5, 10, TimeUnit.SECONDS);

    // nameServer每隔10分钟打印一次KV配置
    this.scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
        @Override
        public void run() {
            NamesrvController.this.kvConfigManager.printAllPeriodically();
        }
    }, 1, 10, TimeUnit.MINUTES);
    return true;
}
复制代码

可以看到,NameServer启动主要就是加载了两个配置,然后开启了两个心跳检测线程,用于心跳检测扫描Broker以及打印KV配置。

路由元信息

NameServer主要作用是为生产者和消费者提供关于Topic的路由信息,还要能够管理Broker节点。我们先看一下路由元数据的数据结构。

org.apache.rocketmq.namesrv.routeinfo.RouteInfoManager
// Topic消息队列路由信息,消息发送时根据路由表进行负载均衡。
private final HashMap<String/* topic */, List<QueueData>> topicQueueTable;
// Broker基础信息
private final HashMap<String/* brokerName */, BrokerData> brokerAddrTable;
// Broker集群基础信息
private final HashMap<String/* clusterName */, Set<String/* brokerName */>> clusterAddrTable;
// Broker状态信息
private final HashMap<String/* brokerAddr */, BrokerLiveInfo> brokerLiveTable;
// 类消息过滤
private final HashMap<String/* brokerAddr */, List<String>/* Filter Server */> filterServerTable;
复制代码

路由注册

路由注册时通过Broker与NameServer的心跳功能实现的。Broker启动时向集群中所有的NameServer保持长连接,每隔30秒向所有的NameServer发送心跳包,NameServer收到心跳包时会更新brokerLiveTable缓存中BrokerLiveInfo的lastUpdateTimestamp,然后NameServer每隔10s扫描brokerLiveTable,如果连续120s没有收到心跳包,NameServer将移除该Broker的路由信息同时关闭Socket连接。

Broker心跳发送

Broker端心跳包发送(org.apache.rocketmq.broker.BrokerController#start)
// 每隔10s向NameServer发送心跳包
this.scheduledExecutorService.scheduleAtFixedRate(new Runnable() {

    @Override
    public void run() {
        try {
            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);

复制代码
org.apache.rocketmq.broker.BrokerController#registerBrokerAll
// 获取nameServerAddress列表
List<String> nameServerAddressList = this.remotingClient.getNameServerAddressList();
if (nameServerAddressList != null && nameServerAddressList.size() > 0) {

    final RegisterBrokerRequestHeader requestHeader = new RegisterBrokerRequestHeader();
    requestHeader.setBrokerAddr(brokerAddr);
    requestHeader.setBrokerId(brokerId);
    requestHeader.setBrokerName(brokerName);
    requestHeader.setClusterName(clusterName);
    requestHeader.setHaServerAddr(haServerAddr);
    requestHeader.setCompressed(compressed);

    RegisterBrokerBody requestBody = new RegisterBrokerBody();
    requestBody.setTopicConfigSerializeWrapper(topicConfigWrapper);
    requestBody.setFilterServerList(filterServerList);
    final byte[] body = requestBody.encode(compressed);
    final int bodyCrc32 = UtilAll.crc32(body);
    requestHeader.setBodyCrc32(bodyCrc32);
    // 使用countDownLatch阻塞当前线程,等待心跳同步线程
    final CountDownLatch countDownLatch = new CountDownLatch(nameServerAddressList.size());
    for (final String namesrvAddr : nameServerAddressList) {
        brokerOuterExecutor.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    // 发送心跳同步信息
                    RegisterBrokerResult result = registerBroker(namesrvAddr,oneway, timeoutMills,requestHeader,body);
                    if (result != null) {
                        registerBrokerResultList.add(result);
                    }

                    log.info("register broker[{}]to name server {} OK", brokerId, namesrvAddr);
                } catch (Exception e) {
                    log.warn("registerBroker Exception, {}", namesrvAddr, e);
                } finally {
                    countDownLatch.countDown();
                }
            }
            });
    }
    try {
        //等待心跳同步线程全部执行完毕才往下执行
        countDownLatch.await(timeoutMills, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
    }
}
复制代码
org.apache.rocketmq.broker.out.BrokerOuterAPI#registerBroker
    // 封装请求头
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.REGISTER_BROKER, requestHeader);
    request.setBody(body);
    //发送消息
    if (oneway) {
        try {
            this.remotingClient.invokeOneway(namesrvAddr, request, timeoutMills);
        } catch (RemotingTooMuchRequestException e) {
            // Ignore
        }
        return null;
    }
    发送消息
    RemotingCommand response = this.remotingClient.invokeSync(namesrvAddr, request, timeoutMills);
复制代码

向NameServer服务器发送类型为RequestCode.REGISTER_BROKER的请求。

NameServer处理心跳

NameServer处理网络请求在org.apache.rocketmq.namesrv.processor.DefaultRequestProcessor#processRequest,如果请求类型为RequestCode.REGISTER_BROKER,则请求最终转发到org.apache.rocketmq.namesrv.routeinfo.RouteInfoManager#registerBroker。

org.apache.rocketmq.namesrv.routeinfo.RouteInfoManager#registerBroker
clusterAddrTable维护
                // 写锁
                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);
复制代码
clusterAddrTable维护
                boolean registerFirst = false;
            
                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();
                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);
复制代码
topicQueueTable维护
//如果是master
if (null != topicConfigWrapper && MixAll.MASTER_ID == brokerId) {
    // 如果config发生了改变或者是第一次注册
    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());
            }
        }
    }
}
复制代码

如果Broker为Master,并且BrokerTopic配置信息发生变化或者是初次注册,则需要创建或更新Topic路由元数据,填充TopicQueueTable,其实就是为默认主题自动注册路由信息,其中包含MixAll.DEFAULT_TOPIC的路由信息。如果该主题为创建并且BrokerConfig的autoCreateTopicEnable为true时,将返回MixAll.DEFAULT_TOPIC的路由信息。

org.apache.rocketmq.namesrv.routeinfo.RouteInfoManager#createAndUpdateQueueData
根据TopicConfig创建QueueData数据结构,然后更新topicQueueTable
private void createAndUpdateQueueData(final String brokerName, final TopicConfig topicConfig) {
        QueueData queueData = new QueueData();
        queueData.setBrokerName(brokerName);
        queueData.setWriteQueueNums(topicConfig.getWriteQueueNums());
        queueData.setReadQueueNums(topicConfig.getReadQueueNums());
        queueData.setPerm(topicConfig.getPerm());
        queueData.setTopicSynFlag(topicConfig.getTopicSysFlag());

        List<QueueData> queueDataList = this.topicQueueTable.get(topicConfig.getTopicName());
        if (null == queueDataList) {
            queueDataList = new LinkedList<QueueData>();
            queueDataList.add(queueData);
            this.topicQueueTable.put(topicConfig.getTopicName(), queueDataList);
            log.info("new topic registered, {} {}", topicConfig.getTopicName(), queueData);
        } else {
            boolean addNewOne = true;

            Iterator<QueueData> it = queueDataList.iterator();
            while (it.hasNext()) {
                QueueData qd = it.next();
                if (qd.getBrokerName().equals(brokerName)) {
                    if (qd.equals(queueData)) {
                        addNewOne = false;
                    } else {
                        log.info("topic changed, {} OLD: {} NEW: {}", topicConfig.getTopicName(), qd,
                            queueData);
                        it.remove();
                    }
                }
            }

            if (addNewOne) {
                queueDataList.add(queueData);
            }
        }
    }
复制代码

根据TopicConfig创建QueueData数据结构,然后更新topicQueueTable。

brokerLiveTable维护
                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);
                }

复制代码

brokerLiveTable是执行路由删除的重要依据。

filterServerTable维护
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);
        }
    }
}
复制代码

路由注册总结

路由注册就是Broker每30s向所有NameServer发送一次心跳并带上自己的信息,然后NameServer处理这些信息,更新路由元数据的过程。 可以看到,这里路由表使用了HashMap数据结构实现,同时使用了锁粒度较小的读写锁来控制并发,允许多个Client并发读,但是同一时刻只允许处理一个心跳包。

路由删除

上面已经介绍过,NameServer和Broker保持长连接,Broker状态存储在BrokerLiveTable里,NameServer会每10s扫描一次NameServer,一旦发现已经有120s没有收到Broker发送过来的心跳信息,就移除该Broker并关闭与Broker的连接,同时更新路由元信息。 还有一种情况是Broker正常关闭,会执行unRegisterBroker指令。

扫描brokerLiveTable

上面说过,在NameServer启动时会开启一个线程,每10秒调用一次scanNotActiveBroker方法。

org.apache.rocketmq.namesrv.routeinfo.RouteInfoManager#scanNotActiveBroker
    public void scanNotActiveBroker() {
        Iterator<Entry<String, BrokerLiveInfo>> it = this.brokerLiveTable.entrySet().iterator();
        // 遍历brokerLiveTable的lastUpdateTimestamp
        while (it.hasNext()) {
            Entry<String, BrokerLiveInfo> next = it.next();
            long last = next.getValue().getLastUpdateTimestamp();
            //超过120秒就删除
            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);
                //然后关闭Channel
                this.onChannelDestroy(next.getKey(), next.getValue().getChannel());
            }
        }
    }
复制代码

维护路由表

org.apache.rocketmq.namesrv.routeinfo.RouteInfoManager#onChannelDestroy
                try {
                    // 加锁
                    this.lock.writeLock().lockInterruptibly();
                    this.brokerLiveTable.remove(brokerAddrFound);
                    this.filterServerTable.remove(brokerAddrFound);
                    String brokerNameFound = null;
                    boolean removeBrokerName = false;
                    Iterator<Entry<String, BrokerData>> itBrokerAddrTable =
                        this.brokerAddrTable.entrySet().iterator();
                    while (itBrokerAddrTable.hasNext() && (null == brokerNameFound)) {
                        BrokerData brokerData = itBrokerAddrTable.next().getValue();

                        Iterator<Entry<Long, String>> it = brokerData.getBrokerAddrs().entrySet().iterator();
                        while (it.hasNext()) {
                            Entry<Long, String> entry = it.next();
                            Long brokerId = entry.getKey();
                            String brokerAddr = entry.getValue();
                            if (brokerAddr.equals(brokerAddrFound)) {
                                brokerNameFound = brokerData.getBrokerName();
                                it.remove();
                                log.info("remove brokerAddr[{}, {}] from brokerAddrTable, because channel destroyed",
                                    brokerId, brokerAddr);
                                break;
                            }
                        }

                        if (brokerData.getBrokerAddrs().isEmpty()) {
                            removeBrokerName = true;
                            itBrokerAddrTable.remove();
                            log.info("remove brokerName[{}] from brokerAddrTable, because channel destroyed",
                                brokerData.getBrokerName());
                        }
                    }

                    if (brokerNameFound != null && removeBrokerName) {
                        Iterator<Entry<String, Set<String>>> it = this.clusterAddrTable.entrySet().iterator();
                        while (it.hasNext()) {
                            Entry<String, Set<String>> entry = it.next();
                            String clusterName = entry.getKey();
                            Set<String> brokerNames = entry.getValue();
                            boolean removed = brokerNames.remove(brokerNameFound);
                            if (removed) {
                                log.info("remove brokerName[{}], clusterName[{}] from clusterAddrTable, because channel destroyed",
                                    brokerNameFound, clusterName);

                                if (brokerNames.isEmpty()) {
                                    log.info("remove the clusterName[{}] from clusterAddrTable, because channel destroyed and no broker in this cluster",
                                        clusterName);
                                    it.remove();
                                }

                                break;
                            }
                        }
                    }

                    if (removeBrokerName) {
                        Iterator<Entry<String, List<QueueData>>> itTopicQueueTable =
                            this.topicQueueTable.entrySet().iterator();
                        while (itTopicQueueTable.hasNext()) {
                            Entry<String, List<QueueData>> entry = itTopicQueueTable.next();
                            String topic = entry.getKey();
                            List<QueueData> queueDataList = entry.getValue();

                            Iterator<QueueData> itQueueData = queueDataList.iterator();
                            while (itQueueData.hasNext()) {
                                QueueData queueData = itQueueData.next();
                                if (queueData.getBrokerName().equals(brokerNameFound)) {
                                    itQueueData.remove();
                                    log.info("remove topic[{} {}], from topicQueueTable, because channel destroyed",
                                        topic, queueData);
                                }
                            }

                            if (queueDataList.isEmpty()) {
                                itTopicQueueTable.remove();
                                log.info("remove topic[{}] all queue, from topicQueueTable, because channel destroyed",
                                    topic);
                            }
                        }
                    }
                } finally {
                    this.lock.writeLock().unlock();
                }
复制代码

路由删除总结

主要就是NameServer定时线程每10s扫描一次BrokerLiveTable,发现120s没动静的Broker就移除该Broker相关信息,维护路由表就不再赘述。

路由发现

路由发现并不是实时的,当Topic路由出现变化后,NameServer不主动推送给客户端。由客户端定时拉取Topic最新的路由。

org.apache.rocketmq.namesrv.processor.DefaultRequestProcessor#getRouteInfoByTopic
public RemotingCommand getRouteInfoByTopic(ChannelHandlerContext ctx,
    RemotingCommand request) throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(null);
    final GetRouteInfoRequestHeader requestHeader =
        (GetRouteInfoRequestHeader) request.decodeCommandCustomHeader(GetRouteInfoRequestHeader.class);
    // 从路由表中找到主题对应的路由信息填充topicRouteData
    TopicRouteData topicRouteData = this.namesrvController.getRouteInfoManager().pickupTopicRouteData(requestHeader.getTopic());

    if (topicRouteData != null) {
        if (this.namesrvController.getNamesrvConfig().isOrderMessageEnable()) {
            String orderTopicConf =
                this.namesrvController.getKvConfigManager().getKVConfig(NamesrvUtil.NAMESPACE_ORDER_TOPIC_CONFIG,
                    requestHeader.getTopic());
            topicRouteData.setOrderTopicConf(orderTopicConf);
        }

        byte[] content = topicRouteData.encode();
        response.setBody(content);
        response.setCode(ResponseCode.SUCCESS);
        response.setRemark(null);
        return response;
    }

    response.setCode(ResponseCode.TOPIC_NOT_EXIST);
    response.setRemark("No topic route info in name server for the topic: " + requestHeader.getTopic()
        + FAQUrl.suggestTodo(FAQUrl.APPLY_TOPIC_URL));
    return response;
}
复制代码

#总结

如上图所示,Broker每30s向NameServer集群发送心跳信息,NameServer收到心跳信息后更新brokerLiveTable中的信息,记录lastUpdateTime,NameServer每隔10s扫描brokerLiveTable,比较brokerLiveTable中每一个brokerLiveInfo里的lastUpdateTime,如果发现超过120s,则删除broker相关的所有信息。 当Topic信息发生变化时,NameServer并不通知客户端,而是客户端定时拉去Topic路由信息。