RocketMQ——NameServer部分源码流程分析

对于一个消息队列集群来说,需要一个协调者对集群中的角色进行管理,NameServer的作用就是维护这些配置信息,状态信息,其他角色都通过NameServer来协同执行

  • NameServer简介

    NameServer是整个MQ集群的状态服务器,集群中的各个角色需要通过它来获得全局配置信息。同时,各个角色需要定期向NameServer上报自己的状态,超时会让NameServer认为该机器故障不可用了,那么会将故障的机器从可用列表中移除。
    NameServer本身也可以集群化部署,达到高可用,NameServer之间相互独立,NameServer本身是无状态的,也就是说NameServer中的Broker、Topic信息不会持久化,是一个动态信息,由各个角色定时上报存储到内存中。
    NameServer 之间相互独立,不需要相互选举,因此不需要用到Zookeeper,只需要一个轻量级的元数据服务器就可以了。

  • 目录结构

在这里插入图片描述
其中,NamesrvStartUp是整个模块的启动入口,NamesrvController用来协调各个模块功能,有很多成员变量,包括各种配置Manager。

  • NameServer的启动
	public static void main(String[] args) {
        main0(args);
    }
    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;
    }
    
    public static NamesrvController createNamesrvController(String[] args) throws IOException, JoranException {
        System.setProperty(RemotingCommand.REMOTING_VERSION_KEY, Integer.toString(MQVersion.CURRENT_VERSION));
        //PackageConflictDetect.detectFastjson();

        Options options = ServerUtil.buildCommandlineOptions(new Options());
        commandLine = ServerUtil.parseCmdLine("mqnamesrv", args, buildCommandlineOptions(options), new PosixParser());
        if (null == commandLine) {
            System.exit(-1);
            return null;
        }

        final NamesrvConfig namesrvConfig = new NamesrvConfig();
        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);

        if (null == namesrvConfig.getRocketmqHome()) {
            System.out.printf("Please set the %s variable in your environment to match the location of the RocketMQ installation%n", MixAll.ROCKETMQ_HOME_ENV);
            System.exit(-2);
        }

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

        final NamesrvController controller = new NamesrvController(namesrvConfig, nettyServerConfig);

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

        return controller;
    }
    
	public static NamesrvController start(final NamesrvController controller) throws Exception {
        if (null == controller) {
            throw new IllegalArgumentException("NamesrvController is null");
        }

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

        // 当程序关闭的时候执行 ShutdownHookThread 的 run方法,通过把callback作为成员变量传给ShutdownHookThread,作为回调
        Runtime.getRuntime().addShutdownHook(new ShutdownHookThread(log, new Callable<Void>() {
            @Override
            public Void call() throws Exception {
                controller.shutdown();
                return null;
            }
        }));

        controller.start();

        return controller;
    }

可以看出NameServer的启动过程主要完成两个功能:第一个功能是解析命令行的参数,得到NameServerController对象;第二个功能是初始化并启动NameServerController,添加钩子addShutdownHook,当程序退出时调用shutdown方法,做最后的清理工作。

// 第一个功能
commandLine = ServerUtil.parseCmdLine("mqnamesrv", args, buildCommandlineOptions(options), new PosixParser());`
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();
}

// 第二个功能
// 当程序关闭的时候执行 ShutdownHookThread 的 run方法,通过把callback作为成员变量传给ShutdownHookThread,作为回调
Runtime.getRuntime().addShutdownHook(new ShutdownHookThread(log, new Callable<Void>() {
            @Override
     public Void call() throws Exception {
         controller.shutdown();
         return null;
     }
 }));
  • NameServer的协调

前面已经提到第二个功能是对NameController进行初始化,那么下面将详细介绍初始化的过程:

		// NameController initialize 方法
   this.kvConfigManager.load();

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

	/**
     * 使用了自定义的线程工厂,没产生一个线程都会去调用线程工厂的newThread()方法
     * 为了保证线程名称有共同特点(前缀)并且名字不重复,用到了原子类的递增
     * **/
    this.remotingExecutor =  Executors.newFixedThreadPool(nettyServerConfig.getServerWorkerThreads(), new ThreadFactoryImpl("RemotingExecutorThread_"));// 启动一个默认是8线程的工作线程池

    this.registerProcessor();//remotingServer 是基于Netty封装的通信层,在这里remotingServer上注册processor,处理client broker等远程的请求,具体逻辑在DefaultRequestProcessor的processorRequest中
	 /**
      * 启动了两个定时任务 
      * 1: scanNotActiveBroker 扫描失效的broker
      * 2:printAllPeriodically 周期性的打印日志信息
      * **/
    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);


/* routeInfoManager scanNotActiveBroker */
	public void scanNotActiveBroker() {
        Iterator<Entry<String, BrokerLiveInfo>> it = this.brokerLiveTable.entrySet().iterator();
        while (it.hasNext()) {
            Entry<String, BrokerLiveInfo> next = it.next();
            //做迭代,若某个broker的更新时间太久没有更新,将关闭channel并且从brokerLiveTable中移除
            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());
            }
        }
    }
    
	// DefaultRequestProcessor的processorRequest
    public RemotingCommand processRequest(ChannelHandlerContext ctx,
        RemotingCommand request) throws RemotingCommandException {

        if (ctx != null) {
            log.debug("receive request, {} {} {}",
                request.getCode(),
                RemotingHelper.parseChannelRemoteAddr(ctx.channel()),
                request);
        }

        switch (request.getCode()) {
            case RequestCode.PUT_KV_CONFIG:
                return this.putKVConfig(ctx, request);
            case RequestCode.GET_KV_CONFIG:
                return this.getKVConfig(ctx, request);
            case RequestCode.DELETE_KV_CONFIG:
                return this.deleteKVConfig(ctx, request);
            case RequestCode.QUERY_DATA_VERSION:
                return queryBrokerTopicConfig(ctx, request);
            case RequestCode.REGISTER_BROKER:
                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:
                return this.unregisterBroker(ctx, request);
            case RequestCode.GET_ROUTEINTO_BY_TOPIC:
                return this.getRouteInfoByTopic(ctx, request);
            case RequestCode.GET_BROKER_CLUSTER_INFO:
                return this.getBrokerClusterInfo(ctx, request);
            case RequestCode.WIPE_WRITE_PERM_OF_BROKER:
                return this.wipeWritePermOfBroker(ctx, request);
            case RequestCode.GET_ALL_TOPIC_LIST_FROM_NAMESERVER:
                return getAllTopicListFromNameserver(ctx, request);
            case RequestCode.DELETE_TOPIC_IN_NAMESRV:
                return deleteTopicInNamesrv(ctx, request);
            case RequestCode.GET_KVLIST_BY_NAMESPACE:
                return this.getKVListByNamespace(ctx, request);
            case RequestCode.GET_TOPICS_BY_CLUSTER:
                return this.getTopicsByCluster(ctx, request);
            case RequestCode.GET_SYSTEM_TOPIC_LIST_FROM_NS:
                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:
                return this.updateConfig(ctx, request);
            case RequestCode.GET_NAMESRV_CONFIG:
                return this.getConfig(ctx, request);
            default:
                break;
        }
        return null;
    }

可以看出,处理的逻辑是一个switch语句,根据请求的code来处理,比如,REGISTER_BROKER就是在集群中注册一个新的broker,其他的逻辑类似,不再赘述。

	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 {
           		 /**
                 * 读写集群信息都是通过从clusterAddrTable 和 brokerAddrTable 中获取
                 * 而由于定时任务的执行和动态增改信息,写请求不算少,因此采用读写锁来保证线程安全
                 * **/
                this.lock.writeLock().lockInterruptibly();

                Set<String> brokerNames = this.clusterAddrTable.get(clusterName);
                if (null == brokerNames) {
                	// 还没有集群的broker信息,比如初次建立集群或者没有broker注册
                    brokerNames = new HashSet<String>();
                    this.clusterAddrTable.put(clusterName, brokerNames);
                }
                brokerNames.add(brokerName);
				// 加入<集群,broker>的信息和该集群的所有brokers的集合信息,不会重复
				
                boolean registerFirst = false;
				 /**
                 * 从brokerAddrTable 中读取broker的具体信息(
                 *  String cluster; 所属集群名
                 *  String brokerName; broker名称
                 *  HashMap<Long brokerId ,String/* broker address > brokerAddrs) broker对应的ip地址
                 * */
                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
                // 遍历,当brokerId不同但是IP相同的时候,要将信息更新把第一次的删掉,保证在brokerAddrTable中一个brokerId只有一个ip,一个ip也只能对应一个brokerId
                // 应用场景就是switch slave to master 的时候(broker从1——0,但是ip没变)
                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();
                    }
                }
				// 重新添加brokerid brokerAddr的信息
                String oldAddr = brokerData.getBrokerAddrs().put(brokerId, brokerAddr);
                registerFirst = registerFirst || (null == oldAddr);
				// 是一个master,更新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());
                            }
                        }
                    }
                }
			// 更新broker的时间戳,因为这个要经常进行远程操作,包括关闭通道等等,因此用的是brokerAddr做key
                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);
                }
				// 针对brokerAddr 添加一些filterServer 做过滤
                if (filterServerList != null) {
                    if (filterServerList.isEmpty()) {
                        this.filterServerTable.remove(brokerAddr);
                    } else {
                        this.filterServerTable.put(brokerAddr, filterServerList);
                    }
                }
				// 是一个slave
                if (MixAll.MASTER_ID != brokerId) {
                	// 拿到这个brokerName对应的master对应的信息
                    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;
    }
  • 总结

NameServer模块的代码综合性比较强,涉及到很多其他层的设计,例如通信层,读懂这一部分可以大致熟悉RocketMQ的整体设计,对以后分析客户端、消费端、Netty通信层都会有一定的帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值