RocketMQ - NameServer源码分析

RocketMQ架构的四个核心

rocketmq_architecture_1.png

Producer Cluster

消息的发送者,需要去NameServer去取到对应Topic的信息去建立连接并发送消息到指定Broker

Consumer Cluster

消息的消费者,需要去NameServer去取到对应Topic的信息去建立连接到指定的Broker并取得消息去消费

Broker

实际消息的接收站,消息的存储地,过滤功能等。启动会向NameServer注册自己。

NameServer

RocketMQ的注册中心,支持Broker的动态注册与发现,保存元数据,干啥都要先找他去做路由。

NameSrv -

在真实环境中,如果有生产者,消费者加入或者掉线,Broker扩容或掉线等各种异常场景,NameSrv的协调管理能力正是用于解决此类场景。但相较之下,NameSrv比大多数分布式协调服务或注册中心又轻量的多。比如ZK、Eureka都会在Node之间做同步,而NameSrv被设计成无状态的,每个NameSrv节点互相不通信。

NameSrv 启动流程

入口

public static NamesrvController main0(String[] args) {

        try {
            NamesrvController controller = createNamesrvController(args);
            start(controller);
            String tip = "The Name Server boot success. serializeType=" + RemotingCommand.getSerializeTypeConfigInThisServer();
            log.info(tip);
            System.out.printf("%s%n", tip);
            return controller;
        } catch (Throwable e) {
            e.printStackTrace();
            System.exit(-1);
        }

        return null;
    }

createNamesrvController

public static NamesrvController createNamesrvController(String[] args) throws IOException, JoranException {
        // 省略部分
        final NamesrvConfig namesrvConfig = new NamesrvConfig();
        final NettyServerConfig nettyServerConfig = new NettyServerConfig();
        nettyServerConfig.setListenPort(9876);
        // 如果启动命令含有 c 代表指定了配置文件 如 -c /home/rocketmq/conf/namesrv.properties
        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);
                // 把配置写入namesrvConfig和nettyServerConfig中
                MixAll.properties2Object(properties, namesrvConfig);
                MixAll.properties2Object(properties, nettyServerConfig);

                namesrvConfig.setConfigStorePath(file);

                System.out.printf("load config properties file OK, %s%n", file);
                in.close();
            }
        }

        // 省略部分
        // 日志配置
        LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
        JoranConfigurator configurator = new JoranConfigurator();
        configurator.setContext(lc);
        lc.reset();
        configurator.doConfigure(namesrvConfig.getRocketmqHome() + "/conf/logback_namesrv.xml");

        log = InternalLoggerFactory.getLogger(LoggerName.NAMESRV_LOGGER_NAME);

        MixAll.printObjectProperties(log, namesrvConfig);
        MixAll.printObjectProperties(log, nettyServerConfig);

        // 把namesrvConfig和nettyServerConfig构建一个NamesrvController
        final NamesrvController controller = new NamesrvController(namesrvConfig, nettyServerConfig);

        // remember all configs to prevent discard
        controller.getConfiguration().registerConfig(properties);

        return controller;
    }

NamesrvController的参数和他的构造函数

NamesrvController的参数

    // namesrv配置信息
    private final NamesrvConfig namesrvConfig;

    // nettyServer配置信息
    private final NettyServerConfig nettyServerConfig;

    // 单个线程的定时调度线程池
    private final ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryImpl(
        "NSScheduledThread"));
    // 键值对管理
    private final KVConfigManager kvConfigManager;
    // 路由信息管理
    private final RouteInfoManager routeInfoManager;

    // 实际启动的netty server
    private RemotingServer remotingServer;

    // broker管理
    private BrokerHousekeepingService brokerHousekeepingService;

    // 固定大小的线程池
    private ExecutorService remotingExecutor;

    // 配置类
    private Configuration configuration;

    // 暂不知道
    private FileWatchService fileWatchService;

可以看到NamesrvController类包含了很多信息,如有一个NettyServer和相关的配置,有键值对管理,有路由信息管理,有broker管理,有自己的配置。

NamesrvController的构造器

public NamesrvController(NamesrvConfig namesrvConfig, NettyServerConfig nettyServerConfig) {
        this.namesrvConfig = namesrvConfig;
        this.nettyServerConfig = nettyServerConfig;
        this.kvConfigManager = new KVConfigManager(this);
        this.routeInfoManager = new RouteInfoManager();
        this.brokerHousekeepingService = new BrokerHousekeepingService(this);
        this.configuration = new Configuration(
            log,
            this.namesrvConfig, this.nettyServerConfig
        );
        this.configuration.setStorePathFromConfig(this.namesrvConfig, "configStorePath");
    }

至此,NamesrvController已实例化完毕,返回入口再执行start。

启动 controller

public static NamesrvController start(final NamesrvController controller) throws Exception {

        if (null == controller) {
            throw new IllegalArgumentException("NamesrvController is null");
        }
        // 1. 初始化controller
        boolean initResult = controller.initialize();
        if (!initResult) {
            controller.shutdown();
            System.exit(-3);
        }

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

        // 3. 启用controller,其实就是启动netty server
        controller.start();

        return controller;
    }

本质上NameServer是一个tcp server,启动后用于接收来自broker,C,P的请求并作出处理。

初始化

public boolean initialize() {

        this.kvConfigManager.load();

        // 创建netty server
        this.remotingServer = new NettyRemotingServer(this.nettyServerConfig, this.brokerHousekeepingService);

        // 创建线程池,默认8个线程,最后丢给netty server使用
        this.remotingExecutor =
            Executors.newFixedThreadPool(nettyServerConfig.getServerWorkerThreads(), new ThreadFactoryImpl("RemotingExecutorThread_"));

        // 为remotingServer绑定processor, 实际就是用来处理NettyServer接收到的请求
        this.registerProcessor();

        // 扫描不活跃的broker
        this.scheduledExecutorService.scheduleAtFixedRate(new Runnable() {

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

        // 打配置
        this.scheduledExecutorService.scheduleAtFixedRate(new Runnable() {

            @Override
            public void run() {
                NamesrvController.this.kvConfigManager.printAllPeriodically();
            }
        }, 1, 10, TimeUnit.MINUTES);

        // 非主线省略

        return true;
    }

NameServer启动的流程可被简单归类于以下几个步骤 -

  • 读配置,分发在NameServer和Netty的配置中
  • 用配置构建NamesrvController实例
  • 初始化NamesrvController
  • 启动NamesrvController中的netty server用于接收请求,响应请求

至此,NameSrv已经成功启动了NettyServer,现在NameSrv是一个可以被生产者,Broker,消费者连接的一个状态了,并可以源源不断的处理他们发过来的请求。在this.registerProcessor();这一行代码中,也为NettyServer配置了具体的业务处理器。我们后面将在DefaultRequestProcessor中去探索其可以实现哪些功能。

NameSrv的核心数据结构

从上文的扫描不活跃的broker来初探NameSrv的核心数据结构

public void scanNotActiveBroker() {
        Iterator> it = this.brokerLiveTable.entrySet().iterator();
        // 遍历存活broker map
        while (it.hasNext()) {
            Entry next = it.next();
            // 得到上一次更新时间
            long last = next.getValue().getLastUpdateTimestamp();
            // 2分钟没发心跳
            if ((last + BROKER_CHANNEL_EXPIRED_TIME) < System.currentTimeMillis()) {
                // 关闭channel
                RemotingUtil.closeChannel(next.getValue().getChannel());
                // 移出map
                it.remove();
                log.warn("The broker channel expired, {} {}ms", next.getKey(), BROKER_CHANNEL_EXPIRED_TIME);
                this.onChannelDestroy(next.getKey(), next.getValue().getChannel());
            }
        }
    }

brokerLiveTable是什么?从上文中看是一个维护每个Broker与NameSrv存活关系集合。NameSrv靠着一次次的接收心跳请求来判断这些Broker有没有失活。

private final HashMap> topicQueueTable;
private final HashMap brokerAddrTable;
private final HashMap> clusterAddrTable;
private final HashMap brokerLiveTable;
private final HashMap/* Filter Server */> filterServerTable;

这些就是NameSrv的核心存储结构
可以从两方面来理解,其最终都是为了拿到某个broker的地址去建立连接或将broker安置到哪个Map中。

  • 第一种是由topic去topicQueueTable中拿到brokerName再去brokerAddrTable去拿到broker的地址信息。这是能猜测到的生产者去NameSrv拿broker地址的一种场景。
  • 第二种是由集群名去clusterAddrTable去拿到此集群下的brokerName。经常发生在注册broker。
  • brokerLiveTable用于保持brokerAddr的心跳状态,以定期移除不存活的broker。
  • filterServerTable这里暂不讨论
    我们下面也将根据以上2种较为常用的场景对这些功能进行分析,也可以看到NameSrv是如何基于以上几个Map做出各种功能。
    namesrv_maps.jpg

NameSrv的功能

NameSrv所支持的功能都在org.apache.rocketmq.namesrv.processor.DefaultRequestProcessor#processRequest方法的switch中。其主要功能是负责处理netty接收到的请求的,根据请求的类型分别执行不同的操作。我们挑RequestCode.REGISTER_BROKERRequestCode.GET_ROUTEINFO_BY_TOPIC来看看。

路由注册

public RegisterBrokerResult registerBroker(
        final String clusterName,
        final String brokerAddr,
        final String brokerName,
        final long brokerId,
        final String haServerAddr,
        final TopicConfigSerializeWrapper topicConfigWrapper,
        final List filterServerList,
        final Channel channel) {
        RegisterBrokerResult result = new RegisterBrokerResult();
        try {
            try {
                this.lock.writeLock().lockInterruptibly();

                // 根据集群名拿到集群下的所有brokerName
                Set brokerNames = this.clusterAddrTable.get(clusterName);
                // 如果不存在,则创建,把集群名,brokerName进行映射
                if (null == brokerNames) {
                    brokerNames = new HashSet();
                    this.clusterAddrTable.put(clusterName, brokerNames);
                }
                // 把新注册的brokerName丢到集群table里
                brokerNames.add(brokerName);

                boolean registerFirst = false;

                // 再根据brokerName去拿到broker的详细信息里查
                BrokerData brokerData = this.brokerAddrTable.get(brokerName);
                // 如果找不到对应的brokerData数据,则证明是第一次注册
                if (null == brokerData) {
                    registerFirst = true;
                    // 新建brokerData并放入brokerAddrTable
                    brokerData = new BrokerData(clusterName, brokerName, new HashMap());
                    this.brokerAddrTable.put(brokerName, brokerData);
                }
                // 一个brokerName对应的BrokerData可能对应多个broker地址
                Map 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> it = brokerAddrsMap.entrySet().iterator();
                while (it.hasNext()) {
                    Entry item = it.next();
                    // 此broker的地址之前就在里面了,但此次的brokerId如发生变化,则代表主从发生了变化,移除
                    if (null != brokerAddr && brokerAddr.equals(item.getValue()) && brokerId != item.getKey()) {
                        it.remove();
                    }
                }

                // 放入新的并返回旧的,如果不存在会返回null
                String oldAddr = brokerData.getBrokerAddrs().put(brokerId, brokerAddr);
                // oldAddr的值就能确定是不是第一次注册
                registerFirst = registerFirst || (null == oldAddr);

                // 如果是master节点
                if (null != topicConfigWrapper
                    && MixAll.MASTER_ID == brokerId) {
                    // 如果version不一样或是第一次注册
                    if (this.isBrokerTopicConfigChanged(brokerAddr, topicConfigWrapper.getDataVersion())
                        || registerFirst) {
                        ConcurrentMap tcTable =
                            topicConfigWrapper.getTopicConfigTable();
                        if (tcTable != null) {
                            // 创建或更新queueData
                            for (Map.Entry entry : tcTable.entrySet()) {
                                this.createAndUpdateQueueData(brokerName, entry.getValue());
                            }
                        }
                    }
                }

                // 填充broker状态表,以便心跳检测
                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);
                    }
                }

                // 如果不是master节点,则把master节点的地址放到haServer和masterAddr中
                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;
    }

我们将注册broker概括为以下几步

  • 先把本次注册的broker信息加到clusterAddrTable中,意为本集群下多了一个broker
  • 然后把broker信息注册到brokerAddrTable中,这一步可能发生主从变化等
  • topicQueueTable中创建或更新队列数据
  • brokerLiveTable中维护本次broker的心跳信息
  • 如果不是Master节点,则返回Master节点地址

路由发现

其实看过路由注册之后,八九不离十就能猜到路由发现可能是从topicQueueTable取到broker的信息,然后再去brokerAddrTable去获取每个broker的详细信息。源码也证实了这一点:

public TopicRouteData pickupTopicRouteData(final String topic) {
        TopicRouteData topicRouteData = new TopicRouteData();
        boolean foundQueueData = false;
        boolean foundBrokerData = false;
        Set brokerNameSet = new HashSet();
        List brokerDataList = new LinkedList();
        topicRouteData.setBrokerDatas(brokerDataList);

        HashMap> filterServerMap = new HashMap>();
        topicRouteData.setFilterServerTable(filterServerMap);

        try {
            try {
                this.lock.readLock().lockInterruptibly();
                List queueDataList = this.topicQueueTable.get(topic);
                if (queueDataList != null) {
                    // topic队列元信息集合
                    topicRouteData.setQueueDatas(queueDataList);
                    foundQueueData = true;

                    Iterator it = queueDataList.iterator();
                    while (it.hasNext()) {
                        QueueData qd = it.next();
                        brokerNameSet.add(qd.getBrokerName());
                    }

                    for (String brokerName : brokerNameSet) {
                        BrokerData brokerData = this.brokerAddrTable.get(brokerName);
                        if (null != brokerData) {
                            BrokerData brokerDataClone = new BrokerData(brokerData.getCluster(), brokerData.getBrokerName(), (HashMap) brokerData
                                .getBrokerAddrs().clone());
                            // broker的元数据集合
                            brokerDataList.add(brokerDataClone);
                            foundBrokerData = true;
                            for (final String brokerAddr : brokerDataClone.getBrokerAddrs().values()) {
                                List filterServerList = this.filterServerTable.get(brokerAddr);
                                filterServerMap.put(brokerAddr, filterServerList);
                            }
                        }
                    }
                }
            } finally {
                this.lock.readLock().unlock();
            }
        } catch (Exception e) {
            log.error("pickupTopicRouteData Exception", e);
        }

        log.debug("pickupTopicRouteData {} {}", topic, topicRouteData);

        if (foundBrokerData && foundQueueData) {
            return topicRouteData;
        }

        return null;
    }

总结

理解了NameSrv的核心数据结构的用处和他们的层级关系,就变相的理解了NameSrv的作用场景。从Processor上看,每个请求对应的处理皆是对这几个Map进行 一些信息的提取和维护。

你可能感兴趣的:(RocketMQ - NameServer源码分析)