RocketMQ源码解析-NameServer篇

在这一篇我们主要来看下NameServer是怎样保存topicbrokercluster这些信息的。

一、rocketmq-namesrv模块基本介绍

RocketMQ源码解析-NameServer篇_第1张图片

​ 可以看到在它的源码中,namesrv其只有这些类(当然也有使用rocketMQ源码的其他的模块)。NamesrvController完成netty连接的初始化,然后DefaultRequestProcessor负责完成客户端具体要做的操作的派发,而关于注册信息相关的保存以及获取就是RouteInfoManager

二、重要类介绍

1、DefaultRequestProcessor

​ 这个类就是对客户端例如broker注册、producer获取对应broker信息等具体操作的派发

@Override
public RemotingCommand processRequest(ChannelHandlerContext ctx,
    RemotingCommand request) throws RemotingCommandException {
		...........
    switch (request.getCode()) {
        ..........
        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_ROUTEINFO_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;
}

​ 这里就有如RequestCode.REGISTER_BROKER其就是处理broker的注册请求的、RequestCode.GET_TOPICS_BY_CLUSTER客户端获取namesrv保存的topic信息、客户端获取以及注册的broker信息GET_BROKER_CLUSTER_INFOGET_ROUTEINFO_BY_TOPIC获取Topic路由信息等。

1)、registerBroker

​ 这个方法就是处理broker注册请求的

public RemotingCommand registerBroker(ChannelHandlerContext ctx,
    RemotingCommand request) throws RemotingCommandException {
 		.........
    RegisterBrokerResult result = this.namesrvController.getRouteInfoManager().registerBroker(
        requestHeader.getClusterName(),
        requestHeader.getBrokerAddr(),
        requestHeader.getBrokerName(),
        requestHeader.getBrokerId(),
        requestHeader.getHaServerAddr(),
        topicConfigWrapper,
        null,
        ctx.channel()
    );

    responseHeader.setHaServerAddr(result.getHaServerAddr());
    responseHeader.setMasterAddr(result.getMasterAddr());

    byte[] jsonValue = this.namesrvController.getKvConfigManager().getKVListByNamespace(NamesrvUtil.NAMESPACE_ORDER_TOPIC_CONFIG);
    response.setBody(jsonValue);
    response.setCode(ResponseCode.SUCCESS);
    response.setRemark(null);
    return response;
}

​ 这里就会交给RouteInfoManager来请求。

2)、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 = 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;
}

​ 获取TopicRouteData路由信息也是交给RouteInfoManager。下面我们就来具体看下RouteInfoManager

2、RouteInfoManager

public class RouteInfoManager {
    private static final InternalLogger log = InternalLoggerFactory.getLogger(LoggerName.NAMESRV_LOGGER_NAME);
    private final static long BROKER_CHANNEL_EXPIRED_TIME = 1000 * 60 * 2;
    private final ReadWriteLock lock = new ReentrantReadWriteLock();
    private final HashMap<String/* topic */, List<QueueData>> topicQueueTable;
    private final HashMap<String/* brokerName */, BrokerData> brokerAddrTable;
    private final HashMap<String/* clusterName */, Set<String/* brokerName */>> clusterAddrTable;
    private final HashMap<String/* brokerAddr */, BrokerLiveInfo> brokerLiveTable;
    private final HashMap<String/* brokerAddr */, List<String>/* Filter Server */> filterServerTable;

这个类我们主要是要知道这几个成员变量保存的值就可以了,因为关于broker注册、Topic路由信息都是获取这些变量中的值。

1)、前置介绍

​ 在了解这些变量的时候,我有配置3台机的集群关系,建立了两个broker集群:DefaultClusterDefaultCluster-2

DefaultCluster由双主双从构成,其主要构成DefaultCluster集群,broker-xx.properties配置的部分内容

192.168.127.128:broker-a

brokerClusterName=DefaultCluster
brokerName=broker-a
brokerId=0

192.168.127.128:broker-b-s

brokerClusterName=DefaultCluster
brokerName=broker-b
brokerId=1

192.168.127.129:broker-a-s

brokerClusterName=DefaultCluster
brokerName=broker-a
brokerId=1

192.168.127.129:broker-b

brokerClusterName=DefaultCluster
brokerName=broker-b
brokerId=0

另一个集群我用的一台机192.168.127.130正常不会怎样做(而且我发送消息的时候,发送的消息会在两个broker集群上都会有,要实现集群发消息隔离,应该需要不同集群分别注册到不同的nameServer上)

192.168.127.129:broker-c

brokerClusterName=DefaultCluster-2
brokerName=broker-c
brokerId=0

192.168.127.129:broker-a-s

brokerClusterName=DefaultCluster-2
brokerName=broker-c-s
brokerId=1

​ 下面我们就来看下其具体保存的信息

2)、topicQueueTable(HashMap>)

RocketMQ源码解析-NameServer篇_第2张图片

public class QueueData implements Comparable<QueueData> {
    private String brokerName;
    private int readQueueNums;
    private int writeQueueNums;
    private int perm;
    private int topicSysFlag;

topicQueueTable就是保存topicbroker的对应关系,例如cluster_queue_topic,这个topic在我们搭建的集群中,其每台broker默认是4个队列,然后发送的消息会分布在3台broker的节点上面–broker-abroker-bbroker-c,也就是说一个topic会有3*4个MessageQueue,例如在producer发送消息的时候,就会在12个MessageQueue中选一个:

RocketMQ源码解析-NameServer篇_第3张图片

RocketMQ源码解析-NameServer篇_第4张图片

​ 同时这个topic的路由信息:

RocketMQ源码解析-NameServer篇_第5张图片

​ 所以我们就可以通过topicQueueTable来知道topic发布在哪些broker上,以及整个topicqueue分布情况。

3)、brokerAddrTable(HashMap )

public class BrokerData implements Comparable<BrokerData> {
    private String cluster;
    private String brokerName;
    private HashMap<Long/* brokerId */, String/* broker address */> brokerAddrs;

​ 这个就是保存brokerName对应的broker启动的实例的主从实例的分布情况:

RocketMQ源码解析-NameServer篇_第6张图片

例如broker-b所属的集群是DefaultCluster,其有两个实例其中一个主节点(breokerId=0)在192.168.127.129,有一个从节点,这个从节点(brokerId>0)在192.168.127.128

4)、clusterAddrTable(HashMap>)

RocketMQ源码解析-NameServer篇_第7张图片

​ 这个就是保存集群有哪些brokerName

5)、brokerLiveTable(HashMap)

​ 这个主要是用来维护上次心跳的维持时间

private final HashMap brokerLiveTable;
class BrokerLiveInfo {
    private long lastUpdateTimestamp;
    private DataVersion dataVersion;
    private Channel channel;
    private String haServerAddr;

6)、registerBroker(REGISTER_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 {
            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);

            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);
            }
        		..........
            String oldAddr = brokerData.getBrokerAddrs().put(brokerId, brokerAddr);
            registerFirst = registerFirst || (null == oldAddr);

            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());
                        }
                    }
                }
            }
            BrokerLiveInfo prevBrokerLiveInfo = this.brokerLiveTable.put(brokerAddr,
                new BrokerLiveInfo(
                    System.currentTimeMillis(),
                    topicConfigWrapper.getDataVersion(),
                    channel,
                    haServerAddr));
			.........
        } finally {
            this.lock.writeLock().unlock();
        }
    } catch (Exception e) {
        log.error("registerBroker Exception", e);
    }
    return result;
}
private final ReadWriteLock lock = new ReentrantReadWriteLock();
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.setTopicSysFlag(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 {
      ..........
    }
}

​ 这里更新的时候首先是上锁,然后就是将客户端实例broker注册的信息构建保存到上面的那些变量中。

7)、pickupTopicRouteData(GET_ROUTEINFO_BY_TOPIC)

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

    HashMap<String, List<String>> filterServerMap = new HashMap<String, List<String>>();
    topicRouteData.setFilterServerTable(filterServerMap);
    try {
        try {
            this.lock.readLock().lockInterruptibly();
            List<QueueData> queueDataList = this.topicQueueTable.get(topic);
            if (queueDataList != null) {
                topicRouteData.setQueueDatas(queueDataList);
                foundQueueData = true;
                Iterator<QueueData> 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<Long, String>) brokerData
                            .getBrokerAddrs().clone());
                        brokerDataList.add(brokerDataClone);
                        ...........
                    }
                }
            }
      				.......
    if (foundBrokerData && foundQueueData) {
        return topicRouteData;
    }

    return null;
}
public class TopicRouteData extends RemotingSerializable {
    private String orderTopicConf;
    private List<QueueData> queueDatas;
    private List<BrokerData> brokerDatas;
    private HashMap<String/* brokerAddr */, List<String>/* Filter Server */> filterServerTable;

​ 这个就是获取topic对应在哪些broker(brokerDatas)上面、以及在对应broker(queueDatas)上面的队列分布情况。

你可能感兴趣的:(Java框架,java,rocketMQ)