rocketmq源码解析请求处理通知消费者订阅信息发生了改变①

说在前面

请求处理 通知消费者订阅信息发生了改变,更多源码解析请关注“天河聊架构”微信公众号

源码解析
进入这个方法,org.apache.rocketmq.client.impl.ClientRemotingProcessor#notifyConsumerIdsChanged

 public RemotingCommand notifyConsumerIdsChanged(ChannelHandlerContext ctx,
        RemotingCommand request) throws RemotingCommandException {
        try {
            final NotifyConsumerIdsChangedRequestHeader requestHeader =
                (NotifyConsumerIdsChangedRequestHeader) request.decodeCommandCustomHeader(NotifyConsumerIdsChangedRequestHeader.class);
            log.info("receive broker's notification[{}], the consumer group: {} changed, rebalance immediately",
                RemotingHelper.parseChannelRemoteAddr(ctx.channel()),
                requestHeader.getConsumerGroup());
            this.mqClientFactory.rebalanceImmediately();
        } catch (Exception e) {
            log.error("notifyConsumerIdsChanged exception", RemotingHelper.exceptionSimpleDesc(e));
        }
        return null;
    }

进入这个方法,org.apache.rocketmq.client.impl.factory.MQClientInstance#rebalanceImmediately

 public void rebalanceImmediately() {
        this.rebalanceService.wakeup();
    }

进入这个方法,org.apache.rocketmq.common.ServiceThread#wakeup

 public void wakeup() {
        if (hasNotified.compareAndSet(false, true)) {
            waitPoint.countDown(); // notify org.apache.rocketmq.client.impl.consumer.RebalanceService.run() 在await
        }
    }

进入这个方法,org.apache.rocketmq.client.impl.consumer.RebalanceService#run

@Override
    public void run() {
        log.info(this.getServiceName() + " service started");

        while (!this.isStopped()) {
            this.waitForRunning(waitInterval);
//            负载均衡处理 =》
            this.mqClientFactory.doRebalance();
        }

        log.info(this.getServiceName() + " service end");
    }

进入这个方法,负载均衡处理,org.apache.rocketmq.client.impl.factory.MQClientInstance#doRebalance,org.apache.rocketmq.client.impl.consumer.DefaultMQPullConsumerImpl#doRebalance

 @Override
    public void doRebalance() {
        if (this.rebalanceImpl != null) {
//            =》
            this.rebalanceImpl.doRebalance(false);
        }
    }

进入这个方法,org.apache.rocketmq.client.impl.consumer.RebalanceImpl#doRebalance

 public void doRebalance(final boolean isOrder) {
//        获取topic的订阅信息=》
        Map<String, SubscriptionData> subTable = this.getSubscriptionInner();
        if (subTable != null) {
            for (final Map.Entry<String, SubscriptionData> entry : subTable.entrySet()) {
                final String topic = entry.getKey();
                try {
//                    根据topic进行负载均衡处理=》
                    this.rebalanceByTopic(topic, isOrder);
                } catch (Throwable e) {
                    if (!topic.startsWith(MixAll.RETRY_GROUP_TOPIC_PREFIX)) {
                        log.warn("rebalanceByTopic Exception", e);
                    }
                }
            }
        }

//        根据topic删除消息队列=》
        this.truncateMessageQueueNotMyTopic();
    }

进入这个方法,根据topic进行负载均衡处理,org.apache.rocketmq.client.impl.consumer.RebalanceImpl#rebalanceByTopic

  private void rebalanceByTopic(final String topic, final boolean isOrder) {
        switch (messageModel) {
            case BROADCASTING: {
//                获取topic的消息队列
                Set<MessageQueue> mqSet = this.topicSubscribeInfoTable.get(topic);
                if (mqSet != null) {
//                    更新负载均衡处理队列表 =》
                    boolean changed = this.updateProcessQueueTableInRebalance(topic, mqSet, isOrder);
                    if (changed) {
//                        消息队列发生改变=》
                        this.messageQueueChanged(topic, mqSet, mqSet);
                        log.info("messageQueueChanged {} {} {} {}",
                            consumerGroup,
                            topic,
                            mqSet,
                            mqSet);
                    }
                } else {
                    log.warn("doRebalance, {}, but the topic[{}] not exist.", consumerGroup, topic);
                }
                break;
            }
            case CLUSTERING: {
//                从topic订阅信息中获取消息队列
                Set<MessageQueue> mqSet = this.topicSubscribeInfoTable.get(topic);
//                获取消费者=》
                List<String> cidAll = this.mQClientFactory.findConsumerIdList(topic, consumerGroup);
                if (null == mqSet) {
                    if (!topic.startsWith(MixAll.RETRY_GROUP_TOPIC_PREFIX)) {
                        log.warn("doRebalance, {}, but the topic[{}] not exist.", consumerGroup, topic);
                    }
                }

                if (null == cidAll) {
                    log.warn("doRebalance, {} {}, get consumer id list failed", consumerGroup, topic);
                }

                if (mqSet != null && cidAll != null) {
                    List<MessageQueue> mqAll = new ArrayList<MessageQueue>();
                    mqAll.addAll(mqSet);

                    Collections.sort(mqAll);
                    Collections.sort(cidAll);

                    AllocateMessageQueueStrategy strategy = this.allocateMessageQueueStrategy;

                    List<MessageQueue> allocateResult = null;
                    try {
//                        39、消费队列分配策略算法=》
                        allocateResult = strategy.allocate(
                            this.consumerGroup,
                            this.mQClientFactory.getClientId(),
                            mqAll,
                            cidAll);
                    } catch (Throwable e) {
                        log.error("AllocateMessageQueueStrategy.allocate Exception. allocateMessageQueueStrategyName={}", strategy.getName(),
                            e);
                        return;
                    }

                    Set<MessageQueue> allocateResultSet = new HashSet<MessageQueue>();
                    if (allocateResult != null) {
                        allocateResultSet.addAll(allocateResult);
                    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值