rocketmq源码解析之管理请求clone消费组的offset

说在前面

管理请求之clone消费组的offset,更多源码解析请关注“天河聊架构”微信公众号

源码解析
进入这个方法org.apache.rocketmq.broker.processor.AdminBrokerProcessor#cloneGroupOffset

private RemotingCommand cloneGroupOffset(ChannelHandlerContext ctx,
        RemotingCommand request) throws RemotingCommandException {
        final RemotingCommand response = RemotingCommand.createResponseCommand(null);
        CloneGroupOffsetRequestHeader requestHeader =
            (CloneGroupOffsetRequestHeader) request.decodeCommandCustomHeader(CloneGroupOffsetRequestHeader.class);

        Set<String> topics;
        if (UtilAll.isBlank(requestHeader.getTopic())) {
//            查询那些topic被这个消费组在消费=》
            topics = this.brokerController.getConsumerOffsetManager().whichTopicByConsumer(requestHeader.getSrcGroup());
        } else {
            topics = new HashSet<String>();
            topics.add(requestHeader.getTopic());
        }

        for (String topic : topics) {
//            按topic查询到topic的配置信息
            TopicConfig topicConfig = this.brokerController.getTopicConfigManager().selectTopicConfig(topic);
            if (null == topicConfig) {
                log.warn("[cloneGroupOffset], topic config not exist, {}", topic);
                continue;
            }

            if (!requestHeader.isOffline()) {

//                查询topic、消费组的订阅信息
                SubscriptionData findSubscriptionData =
                    this.brokerController.getConsumerManager().findSubscriptionData(requestHeader.getSrcGroup(), topic);
                if (this.brokerController.getConsumerManager().findSubscriptionDataCount(requestHeader.getSrcGroup()) > 0
                    && findSubscriptionData == null) {
                    log.warn("[cloneGroupOffset], the consumer group[{}], topic[{}] not exist", requestHeader.getSrcGroup(), topic);
                    continue;
                }
            }

//            clone offset=》
            this.brokerController.getConsumerOffsetManager().cloneOffset(requestHeader.getSrcGroup(), requestHeader.getDestGroup(),
                requestHeader.getTopic());
        }

        response.setCode(ResponseCode.SUCCESS);
        response.setRemark(null);
        return response;
    }

进入这个方法org.apache.rocketmq.broker.offset.ConsumerOffsetManager#whichTopicByConsumer,查询那些topic被这个消费组在消费

public Set<String> whichTopicByConsumer(final String group) {
        Set<String> topics = new HashSet<String>();

//        遍历消费者组、topic缓存信息
        Iterator<Entry<String, ConcurrentMap<Integer, Long>>> it = this.offsetTable.entrySet().iterator();
        while (it.hasNext()) {
            Entry<String, ConcurrentMap<Integer, Long>> next = it.next();
            String topicAtGroup = next.getKey();
            String[] arrays = topicAtGroup.split(TOPIC_GROUP_SEPARATOR);
            if (arrays.length == 2) {
                if (group.equals(arrays[1])) {
                    topics.add(arrays[0]);
                }
            }
        }

        return topics;
    }

进入这个方法org.apache.rocketmq.broker.topic.TopicConfigManager#selectTopicConfig 按topic查询到topic的配置信息

public TopicConfig selectTopicConfig(final String topic) {
//        从topic配置缓存信息中查询当前topic的配置
        return this.topicConfigTable.get(topic);
    }

进入这个方法,查询topic、消费组的订阅信息,org.apache.rocketmq.broker.client.ConsumerManager#findSubscriptionData

public SubscriptionData findSubscriptionData(final String group, final String topic) {
//        按组从缓存中获取消费组信息
        ConsumerGroupInfo consumerGroupInfo = this.getConsumerGroupInfo(group);
        if (consumerGroupInfo != null) {
//            按topic从缓存中获取订阅数据
            return consumerGroupInfo.findSubscriptionData(topic);
        }

        return null;
    }

进入这个方法,clone offset,org.apache.rocketmq.broker.offset.ConsumerOffsetManager#cloneOffset

public void cloneOffset(final String srcGroup, final String destGroup, final String topic) {
        ConcurrentMap<Integer, Long> offsets = this.offsetTable.get(topic + TOPIC_GROUP_SEPARATOR + srcGroup);
        if (offsets != null) {
            this.offsetTable.put(topic + TOPIC_GROUP_SEPARATOR + destGroup, new ConcurrentHashMap<Integer, Long>(offsets));
        }
    }

往上返回到这个方法,org.apache.rocketmq.broker.processor.AdminBrokerProcessor#cloneGroupOffset结束。

说在最后
本次解析仅代表个人观点,仅供参考。

加入技术微信群
在这里插入图片描述

钉钉技术群
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值