Search in sources :

Example 16 with ConsumeStats

use of org.apache.rocketmq.common.admin.ConsumeStats in project rocketmq-externals by apache.

the class ConsumerServiceImpl method queryConsumeStatsList.

@Override
@MultiMQAdminCmdMethod
public List<TopicConsumerInfo> queryConsumeStatsList(final String topic, String groupName) {
    ConsumeStats consumeStats = null;
    try {
        consumeStats = mqAdminExt.examineConsumeStats(groupName, topic);
    } catch (Exception e) {
        throw propagate(e);
    }
    List<MessageQueue> mqList = Lists.newArrayList(Iterables.filter(consumeStats.getOffsetTable().keySet(), new Predicate<MessageQueue>() {

        @Override
        public boolean apply(MessageQueue o) {
            return StringUtils.isBlank(topic) || o.getTopic().equals(topic);
        }
    }));
    Collections.sort(mqList);
    List<TopicConsumerInfo> topicConsumerInfoList = Lists.newArrayList();
    TopicConsumerInfo nowTopicConsumerInfo = null;
    Map<MessageQueue, String> messageQueueClientMap = getClientConnection(groupName);
    for (MessageQueue mq : mqList) {
        if (nowTopicConsumerInfo == null || (!StringUtils.equals(mq.getTopic(), nowTopicConsumerInfo.getTopic()))) {
            nowTopicConsumerInfo = new TopicConsumerInfo(mq.getTopic());
            topicConsumerInfoList.add(nowTopicConsumerInfo);
        }
        QueueStatInfo queueStatInfo = QueueStatInfo.fromOffsetTableEntry(mq, consumeStats.getOffsetTable().get(mq));
        queueStatInfo.setClientInfo(messageQueueClientMap.get(mq));
        nowTopicConsumerInfo.appendQueueStatInfo(queueStatInfo);
    }
    return topicConsumerInfoList;
}
Also used : QueueStatInfo(org.apache.rocketmq.console.model.QueueStatInfo) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) ConsumeStats(org.apache.rocketmq.common.admin.ConsumeStats) TopicConsumerInfo(org.apache.rocketmq.console.model.TopicConsumerInfo) MQClientException(org.apache.rocketmq.client.exception.MQClientException) Predicate(com.google.common.base.Predicate) MultiMQAdminCmdMethod(org.apache.rocketmq.console.aspect.admin.annotation.MultiMQAdminCmdMethod)

Example 17 with ConsumeStats

use of org.apache.rocketmq.common.admin.ConsumeStats in project rocketmq-externals by apache.

the class ConsumerServiceImpl method queryGroup.

@Override
@MultiMQAdminCmdMethod
public GroupConsumeInfo queryGroup(String consumerGroup) {
    GroupConsumeInfo groupConsumeInfo = new GroupConsumeInfo();
    try {
        ConsumeStats consumeStats = null;
        try {
            consumeStats = mqAdminExt.examineConsumeStats(consumerGroup);
        } catch (Exception e) {
            logger.warn("examineConsumeStats exception, " + consumerGroup, e);
        }
        ConsumerConnection consumerConnection = null;
        try {
            consumerConnection = mqAdminExt.examineConsumerConnectionInfo(consumerGroup);
        } catch (Exception e) {
            logger.warn("examineConsumerConnectionInfo exception, " + consumerGroup, e);
        }
        groupConsumeInfo.setGroup(consumerGroup);
        if (consumeStats != null) {
            groupConsumeInfo.setConsumeTps((int) consumeStats.getConsumeTps());
            groupConsumeInfo.setDiffTotal(consumeStats.computeTotalDiff());
        }
        if (consumerConnection != null) {
            groupConsumeInfo.setCount(consumerConnection.getConnectionSet().size());
            groupConsumeInfo.setMessageModel(consumerConnection.getMessageModel());
            groupConsumeInfo.setConsumeType(consumerConnection.getConsumeType());
            groupConsumeInfo.setVersion(MQVersion.getVersionDesc(consumerConnection.computeMinVersion()));
        }
    } catch (Exception e) {
        logger.warn("examineConsumeStats or examineConsumerConnectionInfo exception, " + consumerGroup, e);
    }
    return groupConsumeInfo;
}
Also used : GroupConsumeInfo(org.apache.rocketmq.console.model.GroupConsumeInfo) ConsumeStats(org.apache.rocketmq.common.admin.ConsumeStats) ConsumerConnection(org.apache.rocketmq.common.protocol.body.ConsumerConnection) MQClientException(org.apache.rocketmq.client.exception.MQClientException) MultiMQAdminCmdMethod(org.apache.rocketmq.console.aspect.admin.annotation.MultiMQAdminCmdMethod)

Example 18 with ConsumeStats

use of org.apache.rocketmq.common.admin.ConsumeStats in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class BrokerConsumeStatsSubCommad method execute.

@Override
public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) throws SubCommandException {
    DefaultMQAdminExt defaultMQAdminExt = new DefaultMQAdminExt(rpcHook);
    defaultMQAdminExt.setInstanceName(Long.toString(System.currentTimeMillis()));
    try {
        defaultMQAdminExt.start();
        String brokerAddr = commandLine.getOptionValue('b').trim();
        boolean isOrder = false;
        long timeoutMillis = 50000;
        long diffLevel = 0;
        if (commandLine.hasOption('o')) {
            isOrder = Boolean.parseBoolean(commandLine.getOptionValue('o').trim());
        }
        if (commandLine.hasOption('t')) {
            timeoutMillis = Long.parseLong(commandLine.getOptionValue('t').trim());
        }
        if (commandLine.hasOption('l')) {
            diffLevel = Long.parseLong(commandLine.getOptionValue('l').trim());
        }
        ConsumeStatsList consumeStatsList = defaultMQAdminExt.fetchConsumeStatsInBroker(brokerAddr, isOrder, timeoutMillis);
        System.out.printf("%-32s  %-32s  %-32s  %-4s  %-20s  %-20s  %-20s  %s%n", "#Topic", "#Group", "#Broker Name", "#QID", "#Broker Offset", "#Consumer Offset", "#Diff", "#LastTime");
        for (Map<String, List<ConsumeStats>> map : consumeStatsList.getConsumeStatsList()) {
            for (Map.Entry<String, List<ConsumeStats>> entry : map.entrySet()) {
                String group = entry.getKey();
                List<ConsumeStats> consumeStatsArray = entry.getValue();
                for (ConsumeStats consumeStats : consumeStatsArray) {
                    List<MessageQueue> mqList = new LinkedList<MessageQueue>();
                    mqList.addAll(consumeStats.getOffsetTable().keySet());
                    Collections.sort(mqList);
                    for (MessageQueue mq : mqList) {
                        OffsetWrapper offsetWrapper = consumeStats.getOffsetTable().get(mq);
                        long diff = offsetWrapper.getBrokerOffset() - offsetWrapper.getConsumerOffset();
                        if (diff < diffLevel) {
                            continue;
                        }
                        String lastTime = "-";
                        try {
                            lastTime = UtilAll.formatDate(new Date(offsetWrapper.getLastTimestamp()), UtilAll.YYYY_MM_DD_HH_MM_SS);
                        } catch (Exception ignored) {
                        }
                        if (offsetWrapper.getLastTimestamp() > 0)
                            System.out.printf("%-32s  %-32s  %-32s  %-4d  %-20d  %-20d  %-20d  %s%n", UtilAll.frontStringAtLeast(mq.getTopic(), 32), group, UtilAll.frontStringAtLeast(mq.getBrokerName(), 32), mq.getQueueId(), offsetWrapper.getBrokerOffset(), offsetWrapper.getConsumerOffset(), diff, lastTime);
                    }
                }
            }
        }
        System.out.printf("%nDiff Total: %d%n", consumeStatsList.getTotalDiff());
    } catch (Exception e) {
        throw new SubCommandException(this.getClass().getSimpleName() + " command failed", e);
    } finally {
        defaultMQAdminExt.shutdown();
    }
}
Also used : ConsumeStatsList(org.apache.rocketmq.common.protocol.body.ConsumeStatsList) SubCommandException(org.apache.rocketmq.tools.command.SubCommandException) ConsumeStats(org.apache.rocketmq.common.admin.ConsumeStats) LinkedList(java.util.LinkedList) OffsetWrapper(org.apache.rocketmq.common.admin.OffsetWrapper) Date(java.util.Date) SubCommandException(org.apache.rocketmq.tools.command.SubCommandException) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) List(java.util.List) ConsumeStatsList(org.apache.rocketmq.common.protocol.body.ConsumeStatsList) LinkedList(java.util.LinkedList) DefaultMQAdminExt(org.apache.rocketmq.tools.admin.DefaultMQAdminExt) Map(java.util.Map)

Example 19 with ConsumeStats

use of org.apache.rocketmq.common.admin.ConsumeStats in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

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 20 with ConsumeStats

use of org.apache.rocketmq.common.admin.ConsumeStats in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class AdminBrokerProcessor method fetchAllConsumeStatsInBroker.

private RemotingCommand fetchAllConsumeStatsInBroker(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(null);
    GetConsumeStatsInBrokerHeader requestHeader = (GetConsumeStatsInBrokerHeader) request.decodeCommandCustomHeader(GetConsumeStatsInBrokerHeader.class);
    boolean isOrder = requestHeader.isOrder();
    ConcurrentMap<String, SubscriptionGroupConfig> subscriptionGroups = brokerController.getSubscriptionGroupManager().getSubscriptionGroupTable();
    List<Map<String, List<ConsumeStats>>> /* subscriptionGroupName */
    brokerConsumeStatsList = new ArrayList<Map<String, List<ConsumeStats>>>();
    long totalDiff = 0L;
    for (String group : subscriptionGroups.keySet()) {
        Map<String, List<ConsumeStats>> subscripTopicConsumeMap = new HashMap<String, List<ConsumeStats>>();
        Set<String> topics = this.brokerController.getConsumerOffsetManager().whichTopicByConsumer(group);
        List<ConsumeStats> consumeStatsList = new ArrayList<ConsumeStats>();
        for (String topic : topics) {
            ConsumeStats consumeStats = new ConsumeStats();
            TopicConfig topicConfig = this.brokerController.getTopicConfigManager().selectTopicConfig(topic);
            if (null == topicConfig) {
                log.warn("consumeStats, topic config not exist, {}", topic);
                continue;
            }
            if (isOrder && !topicConfig.isOrder()) {
                continue;
            }
            /**
             */
            {
                SubscriptionData findSubscriptionData = this.brokerController.getConsumerManager().findSubscriptionData(group, topic);
                if (// 
                null == findSubscriptionData && this.brokerController.getConsumerManager().findSubscriptionDataCount(group) > 0) {
                    log.warn("consumeStats, the consumer group[{}], topic[{}] not exist", group, topic);
                    continue;
                }
            }
            for (int i = 0; i < topicConfig.getWriteQueueNums(); i++) {
                MessageQueue mq = new MessageQueue();
                mq.setTopic(topic);
                mq.setBrokerName(this.brokerController.getBrokerConfig().getBrokerName());
                mq.setQueueId(i);
                OffsetWrapper offsetWrapper = new OffsetWrapper();
                long brokerOffset = this.brokerController.getMessageStore().getMaxOffsetInQueue(topic, i);
                if (brokerOffset < 0)
                    brokerOffset = 0;
                long consumerOffset = // 
                this.brokerController.getConsumerOffsetManager().queryOffset(// 
                group, // 
                topic, i);
                if (consumerOffset < 0)
                    consumerOffset = 0;
                offsetWrapper.setBrokerOffset(brokerOffset);
                offsetWrapper.setConsumerOffset(consumerOffset);
                long timeOffset = consumerOffset - 1;
                if (timeOffset >= 0) {
                    long lastTimestamp = this.brokerController.getMessageStore().getMessageStoreTimeStamp(topic, i, timeOffset);
                    if (lastTimestamp > 0) {
                        offsetWrapper.setLastTimestamp(lastTimestamp);
                    }
                }
                consumeStats.getOffsetTable().put(mq, offsetWrapper);
            }
            double consumeTps = this.brokerController.getBrokerStatsManager().tpsGroupGetNums(group, topic);
            consumeTps += consumeStats.getConsumeTps();
            consumeStats.setConsumeTps(consumeTps);
            totalDiff += consumeStats.computeTotalDiff();
            consumeStatsList.add(consumeStats);
        }
        subscripTopicConsumeMap.put(group, consumeStatsList);
        brokerConsumeStatsList.add(subscripTopicConsumeMap);
    }
    ConsumeStatsList consumeStats = new ConsumeStatsList();
    consumeStats.setBrokerAddr(brokerController.getBrokerAddr());
    consumeStats.setConsumeStatsList(brokerConsumeStatsList);
    consumeStats.setTotalDiff(totalDiff);
    response.setBody(consumeStats.encode());
    response.setCode(ResponseCode.SUCCESS);
    response.setRemark(null);
    return response;
}
Also used : ConsumeStatsList(org.apache.rocketmq.common.protocol.body.ConsumeStatsList) ConsumeStats(org.apache.rocketmq.common.admin.ConsumeStats) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TopicConfig(org.apache.rocketmq.common.TopicConfig) SubscriptionGroupConfig(org.apache.rocketmq.common.subscription.SubscriptionGroupConfig) OffsetWrapper(org.apache.rocketmq.common.admin.OffsetWrapper) RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) GetConsumeStatsInBrokerHeader(org.apache.rocketmq.common.protocol.header.GetConsumeStatsInBrokerHeader) SubscriptionData(org.apache.rocketmq.common.protocol.heartbeat.SubscriptionData) TopicList(org.apache.rocketmq.common.protocol.body.TopicList) GroupList(org.apache.rocketmq.common.protocol.body.GroupList) ArrayList(java.util.ArrayList) ConsumeStatsList(org.apache.rocketmq.common.protocol.body.ConsumeStatsList) List(java.util.List) Map(java.util.Map) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Aggregations

ConsumeStats (org.apache.rocketmq.common.admin.ConsumeStats)35 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)24 OffsetWrapper (org.apache.rocketmq.common.admin.OffsetWrapper)21 BrokerData (org.apache.rocketmq.common.protocol.route.BrokerData)16 HashMap (java.util.HashMap)12 TopicRouteData (org.apache.rocketmq.common.protocol.route.TopicRouteData)12 DefaultMQAdminExt (org.apache.rocketmq.tools.admin.DefaultMQAdminExt)12 ArrayList (java.util.ArrayList)11 Map (java.util.Map)11 List (java.util.List)10 MQClientException (org.apache.rocketmq.client.exception.MQClientException)9 ConsumerConnection (org.apache.rocketmq.common.protocol.body.ConsumerConnection)9 Field (java.lang.reflect.Field)8 MQClientAPIImpl (org.apache.rocketmq.client.impl.MQClientAPIImpl)8 MQClientInstance (org.apache.rocketmq.client.impl.factory.MQClientInstance)8 ConsumeStatsList (org.apache.rocketmq.common.protocol.body.ConsumeStatsList)8 TopicList (org.apache.rocketmq.common.protocol.body.TopicList)8 SubscriptionData (org.apache.rocketmq.common.protocol.heartbeat.SubscriptionData)8 QueueData (org.apache.rocketmq.common.protocol.route.QueueData)8 SubCommandException (org.apache.rocketmq.tools.command.SubCommandException)8