Search in sources :

Example 96 with BrokerData

use of org.apache.rocketmq.common.protocol.route.BrokerData in project rocketmq by apache.

the class DefaultMQAdminExtImpl method consumed.

public boolean consumed(final MessageExt msg, final String group) throws RemotingException, MQClientException, InterruptedException, MQBrokerException {
    ConsumeStats cstats = this.examineConsumeStats(group);
    ClusterInfo ci = this.examineBrokerClusterInfo();
    Iterator<Entry<MessageQueue, OffsetWrapper>> it = cstats.getOffsetTable().entrySet().iterator();
    while (it.hasNext()) {
        Entry<MessageQueue, OffsetWrapper> next = it.next();
        MessageQueue mq = next.getKey();
        if (mq.getTopic().equals(msg.getTopic()) && mq.getQueueId() == msg.getQueueId()) {
            BrokerData brokerData = ci.getBrokerAddrTable().get(mq.getBrokerName());
            if (brokerData != null) {
                String addr = brokerData.getBrokerAddrs().get(MixAll.MASTER_ID);
                if (addr.equals(RemotingUtil.socketAddress2String(msg.getStoreHost()))) {
                    if (next.getValue().getConsumerOffset() > msg.getQueueOffset()) {
                        return true;
                    }
                }
            }
        }
    }
    return false;
}
Also used : ClusterInfo(org.apache.rocketmq.common.protocol.body.ClusterInfo) Entry(java.util.Map.Entry) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) ConsumeStats(org.apache.rocketmq.common.admin.ConsumeStats) BrokerData(org.apache.rocketmq.common.protocol.route.BrokerData) OffsetWrapper(org.apache.rocketmq.common.admin.OffsetWrapper)

Example 97 with BrokerData

use of org.apache.rocketmq.common.protocol.route.BrokerData in project rocketmq by apache.

the class DefaultMQAdminExtImpl method examineConsumerConnectionInfo.

@Override
public ConsumerConnection examineConsumerConnectionInfo(String consumerGroup) throws InterruptedException, MQBrokerException, RemotingException, MQClientException {
    ConsumerConnection result = new ConsumerConnection();
    String topic = MixAll.getRetryTopic(consumerGroup);
    List<BrokerData> brokers = this.examineTopicRouteInfo(topic).getBrokerDatas();
    BrokerData brokerData = brokers.get(random.nextInt(brokers.size()));
    String addr = null;
    if (brokerData != null) {
        addr = brokerData.selectBrokerAddr();
        if (StringUtils.isNotBlank(addr)) {
            result = this.mqClientInstance.getMQClientAPIImpl().getConsumerConnectionList(addr, consumerGroup, timeoutMillis);
        }
    }
    if (result.getConnectionSet().isEmpty()) {
        log.warn("the consumer group not online. brokerAddr={}, group={}", addr, consumerGroup);
        throw new MQClientException(ResponseCode.CONSUMER_NOT_ONLINE, "Not found the consumer group connection");
    }
    return result;
}
Also used : BrokerData(org.apache.rocketmq.common.protocol.route.BrokerData) ConsumerConnection(org.apache.rocketmq.common.protocol.body.ConsumerConnection) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 98 with BrokerData

use of org.apache.rocketmq.common.protocol.route.BrokerData in project rocketmq by apache.

the class DefaultMQAdminExtImpl method resetOffsetByTimestampOld.

@Override
public List<RollbackStats> resetOffsetByTimestampOld(String consumerGroup, String topic, long timestamp, boolean force) throws RemotingException, MQBrokerException, InterruptedException, MQClientException {
    TopicRouteData topicRouteData = this.examineTopicRouteInfo(topic);
    List<RollbackStats> rollbackStatsList = new ArrayList<RollbackStats>();
    Map<String, Integer> topicRouteMap = new HashMap<String, Integer>();
    for (BrokerData bd : topicRouteData.getBrokerDatas()) {
        for (QueueData queueData : topicRouteData.getQueueDatas()) {
            topicRouteMap.put(bd.selectBrokerAddr(), queueData.getReadQueueNums());
        }
    }
    for (BrokerData bd : topicRouteData.getBrokerDatas()) {
        String addr = bd.selectBrokerAddr();
        if (addr != null) {
            ConsumeStats consumeStats = this.mqClientInstance.getMQClientAPIImpl().getConsumeStats(addr, consumerGroup, timeoutMillis);
            boolean hasConsumed = false;
            for (Map.Entry<MessageQueue, OffsetWrapper> entry : consumeStats.getOffsetTable().entrySet()) {
                MessageQueue queue = entry.getKey();
                OffsetWrapper offsetWrapper = entry.getValue();
                if (topic.equals(queue.getTopic())) {
                    hasConsumed = true;
                    RollbackStats rollbackStats = resetOffsetConsumeOffset(addr, consumerGroup, queue, offsetWrapper, timestamp, force);
                    rollbackStatsList.add(rollbackStats);
                }
            }
            if (!hasConsumed) {
                HashMap<MessageQueue, TopicOffset> topicStatus = this.mqClientInstance.getMQClientAPIImpl().getTopicStatsInfo(addr, topic, timeoutMillis).getOffsetTable();
                for (int i = 0; i < topicRouteMap.get(addr); i++) {
                    MessageQueue queue = new MessageQueue(topic, bd.getBrokerName(), i);
                    OffsetWrapper offsetWrapper = new OffsetWrapper();
                    offsetWrapper.setBrokerOffset(topicStatus.get(queue).getMaxOffset());
                    offsetWrapper.setConsumerOffset(topicStatus.get(queue).getMinOffset());
                    RollbackStats rollbackStats = resetOffsetConsumeOffset(addr, consumerGroup, queue, offsetWrapper, timestamp, force);
                    rollbackStatsList.add(rollbackStats);
                }
            }
        }
    }
    return rollbackStatsList;
}
Also used : HashMap(java.util.HashMap) BrokerData(org.apache.rocketmq.common.protocol.route.BrokerData) ConsumeStats(org.apache.rocketmq.common.admin.ConsumeStats) QueueData(org.apache.rocketmq.common.protocol.route.QueueData) ArrayList(java.util.ArrayList) OffsetWrapper(org.apache.rocketmq.common.admin.OffsetWrapper) TopicRouteData(org.apache.rocketmq.common.protocol.route.TopicRouteData) TopicOffset(org.apache.rocketmq.common.admin.TopicOffset) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) RollbackStats(org.apache.rocketmq.common.admin.RollbackStats) Map(java.util.Map) HashMap(java.util.HashMap)

Example 99 with BrokerData

use of org.apache.rocketmq.common.protocol.route.BrokerData in project rocketmq-externals by apache.

the class HudiSinkConnector method buildRoute.

public void buildRoute() {
    String srcCluster = this.sinkConnectConfig.getSrcCluster();
    try {
        for (String topic : this.sinkConnectConfig.getWhiteList()) {
            // different from BrokerData with cluster field, which can ensure the brokerData is from expected cluster.
            // QueueData use brokerName as unique info on cluster of rocketmq. so when we want to get QueueData of
            // expected cluster, we should get brokerNames of expected cluster, and then filter queueDatas.
            List<BrokerData> brokerList = Utils.examineBrokerData(this.srcMQAdminExt, topic, srcCluster);
            Set<String> brokerNameSet = new HashSet<String>();
            for (BrokerData b : brokerList) {
                brokerNameSet.add(b.getBrokerName());
            }
            TopicRouteData topicRouteData = srcMQAdminExt.examineTopicRouteInfo(topic);
            if (!topicRouteMap.containsKey(topic)) {
                topicRouteMap.put(topic, new HashSet<>(16));
            }
            for (QueueData qd : topicRouteData.getQueueDatas()) {
                if (brokerNameSet.contains(qd.getBrokerName())) {
                    for (int i = 0; i < qd.getReadQueueNums(); i++) {
                        MessageQueue taskTopicInfo = new MessageQueue(topic, qd.getBrokerName(), i);
                        topicRouteMap.get(topic).add(taskTopicInfo);
                    }
                }
            }
        }
    } catch (Exception e) {
        log.error("Fetch topic list error.", e);
    } finally {
        srcMQAdminExt.shutdown();
    }
}
Also used : MessageQueue(org.apache.rocketmq.common.message.MessageQueue) BrokerData(org.apache.rocketmq.common.protocol.route.BrokerData) QueueData(org.apache.rocketmq.common.protocol.route.QueueData) MQClientException(org.apache.rocketmq.client.exception.MQClientException) HashSet(java.util.HashSet) TopicRouteData(org.apache.rocketmq.common.protocol.route.TopicRouteData)

Example 100 with BrokerData

use of org.apache.rocketmq.common.protocol.route.BrokerData in project rocketmq-externals by apache.

the class Utils method examineBrokerData.

public static List<BrokerData> examineBrokerData(DefaultMQAdminExt defaultMQAdminExt, String topic, String cluster) throws RemotingException, MQClientException, InterruptedException {
    List<BrokerData> brokerList = new ArrayList<>();
    TopicRouteData topicRouteData = defaultMQAdminExt.examineTopicRouteInfo(topic);
    if (topicRouteData.getBrokerDatas() != null) {
        for (BrokerData broker : topicRouteData.getBrokerDatas()) {
            if (StringUtils.equals(broker.getCluster(), cluster)) {
                brokerList.add(broker);
            }
        }
    }
    return brokerList;
}
Also used : BrokerData(org.apache.rocketmq.common.protocol.route.BrokerData) ArrayList(java.util.ArrayList) TopicRouteData(org.apache.rocketmq.common.protocol.route.TopicRouteData)

Aggregations

BrokerData (org.apache.rocketmq.common.protocol.route.BrokerData)106 TopicRouteData (org.apache.rocketmq.common.protocol.route.TopicRouteData)65 HashMap (java.util.HashMap)41 ArrayList (java.util.ArrayList)29 QueueData (org.apache.rocketmq.common.protocol.route.QueueData)29 HashSet (java.util.HashSet)27 ClusterInfo (org.apache.rocketmq.common.protocol.body.ClusterInfo)23 Map (java.util.Map)22 MQClientException (org.apache.rocketmq.client.exception.MQClientException)21 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)21 List (java.util.List)20 Set (java.util.Set)20 Field (java.lang.reflect.Field)18 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)18 ConsumeStats (org.apache.rocketmq.common.admin.ConsumeStats)16 ArgumentMatchers.anyLong (org.mockito.ArgumentMatchers.anyLong)16 Entry (java.util.Map.Entry)14 MQBrokerException (org.apache.rocketmq.client.exception.MQBrokerException)14 MQClientAPIImpl (org.apache.rocketmq.client.impl.MQClientAPIImpl)14 MQClientInstance (org.apache.rocketmq.client.impl.factory.MQClientInstance)14