Search in sources :

Example 1 with OffsetWrapper

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

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 2 with OffsetWrapper

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

the class GroupConsumeInfo 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();
        // 查询特定consumer
        if (commandLine.hasOption('g')) {
            String consumerGroup = commandLine.getOptionValue('g').trim();
            ConsumeStats consumeStats = defaultMQAdminExt.examineConsumeStats(consumerGroup);
            List<MessageQueue> mqList = new LinkedList<MessageQueue>();
            mqList.addAll(consumeStats.getOffsetTable().keySet());
            Collections.sort(mqList);
            Map<MessageQueue, String> messageQueueAllocationResult = getMessageQueueAllocationResult(defaultMQAdminExt, consumerGroup);
            System.out.printf("%-32s  %-32s  %-4s  %-20s  %-20s  %-20s %-20s  %s%n", "#Topic", "#Broker Name", "#QID", "#Broker Offset", "#Consumer Offset", "#Client IP", "#Diff", "#LastTime");
            long diffTotal = 0L;
            for (MessageQueue mq : mqList) {
                OffsetWrapper offsetWrapper = consumeStats.getOffsetTable().get(mq);
                long diff = offsetWrapper.getBrokerOffset() - offsetWrapper.getConsumerOffset();
                diffTotal += diff;
                String lastTime = "";
                try {
                    lastTime = UtilAll.formatDate(new Date(offsetWrapper.getLastTimestamp()), UtilAll.YYYY_MM_DD_HH_MM_SS);
                } catch (Exception e) {
                }
                String clientIP = messageQueueAllocationResult.get(mq);
                System.out.printf("%-32s  %-32s  %-4d  %-20d  %-20d  %-20s %-20d  %s%n", UtilAll.frontStringAtLeast(mq.getTopic(), 32), UtilAll.frontStringAtLeast(mq.getBrokerName(), 32), mq.getQueueId(), offsetWrapper.getBrokerOffset(), offsetWrapper.getConsumerOffset(), null != clientIP ? clientIP : "NA", diff, lastTime);
            }
            System.out.printf("%n");
            System.out.printf("Consume TPS: %s%n", consumeStats.getConsumeTps());
            System.out.printf("Diff Total: %d%n", diffTotal);
        } else // 查询全部
        {
            System.out.printf("%-32s  %-6s  %-24s %-5s  %-14s  %-7s  %s%n", "#Group", "#Count", "#Version", "#Type", "#Model", "#TPS", "#Diff Total");
            TopicList topicList = defaultMQAdminExt.fetchAllTopicList();
            for (String topic : topicList.getTopicList()) {
                if (topic.startsWith(MixAll.RETRY_GROUP_TOPIC_PREFIX)) {
                    String consumerGroup = topic.substring(MixAll.RETRY_GROUP_TOPIC_PREFIX.length());
                    try {
                        ConsumeStats consumeStats = null;
                        try {
                            consumeStats = defaultMQAdminExt.examineConsumeStats(consumerGroup);
                        } catch (Exception e) {
                            log.warn("examineConsumeStats exception, " + consumerGroup, e);
                        }
                        ConsumerConnection cc = null;
                        try {
                            cc = defaultMQAdminExt.examineConsumerConnectionInfo(consumerGroup);
                        } catch (Exception e) {
                            log.warn("examineConsumerConnectionInfo exception, " + consumerGroup, e);
                        }
                        GroupConsumeInfo groupConsumeInfo = new GroupConsumeInfo();
                        groupConsumeInfo.setGroup(consumerGroup);
                        if (consumeStats != null) {
                            groupConsumeInfo.setConsumeTps((int) consumeStats.getConsumeTps());
                            groupConsumeInfo.setDiffTotal(consumeStats.computeTotalDiff());
                        }
                        if (cc != null) {
                            groupConsumeInfo.setCount(cc.getConnectionSet().size());
                            groupConsumeInfo.setMessageModel(cc.getMessageModel());
                            groupConsumeInfo.setConsumeType(cc.getConsumeType());
                            groupConsumeInfo.setVersion(cc.computeMinVersion());
                        }
                        System.out.printf("%-32s  %-6d  %-24s %-5s  %-14s  %-7d  %d%n", UtilAll.frontStringAtLeast(groupConsumeInfo.getGroup(), 32), groupConsumeInfo.getCount(), groupConsumeInfo.getCount() > 0 ? groupConsumeInfo.versionDesc() : "OFFLINE", groupConsumeInfo.consumeTypeDesc(), groupConsumeInfo.messageModelDesc(), groupConsumeInfo.getConsumeTps(), groupConsumeInfo.getDiffTotal());
                    } catch (Exception e) {
                        log.warn("examineConsumeStats or examineConsumerConnectionInfo exception, " + consumerGroup, e);
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new SubCommandException(this.getClass().getSimpleName() + " command failed", e);
    } finally {
        defaultMQAdminExt.shutdown();
    }
}
Also used : SubCommandException(org.apache.rocketmq.tools.command.SubCommandException) ConsumeStats(org.apache.rocketmq.common.admin.ConsumeStats) ConsumerConnection(org.apache.rocketmq.common.protocol.body.ConsumerConnection) 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) TopicList(org.apache.rocketmq.common.protocol.body.TopicList) DefaultMQAdminExt(org.apache.rocketmq.tools.admin.DefaultMQAdminExt)

Example 3 with OffsetWrapper

use of org.apache.rocketmq.common.admin.OffsetWrapper in project rocketmq by apache.

the class AdminBrokerProcessor method getConsumeStats.

private RemotingCommand getConsumeStats(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(null);
    final GetConsumeStatsRequestHeader requestHeader = (GetConsumeStatsRequestHeader) request.decodeCommandCustomHeader(GetConsumeStatsRequestHeader.class);
    ConsumeStats consumeStats = new ConsumeStats();
    Set<String> topics = new HashSet<String>();
    if (UtilAll.isBlank(requestHeader.getTopic())) {
        topics = this.brokerController.getConsumerOffsetManager().whichTopicByConsumer(requestHeader.getConsumerGroup());
    } else {
        topics.add(requestHeader.getTopic());
    }
    for (String topic : topics) {
        TopicConfig topicConfig = this.brokerController.getTopicConfigManager().selectTopicConfig(topic);
        if (null == topicConfig) {
            log.warn("consumeStats, topic config not exist, {}", topic);
            continue;
        }
        {
            SubscriptionData findSubscriptionData = this.brokerController.getConsumerManager().findSubscriptionData(requestHeader.getConsumerGroup(), topic);
            if (null == findSubscriptionData && this.brokerController.getConsumerManager().findSubscriptionDataCount(requestHeader.getConsumerGroup()) > 0) {
                log.warn("consumeStats, the consumer group[{}], topic[{}] not exist", requestHeader.getConsumerGroup(), topic);
                continue;
            }
        }
        for (int i = 0; i < topicConfig.getReadQueueNums(); 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(requestHeader.getConsumerGroup(), 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(requestHeader.getConsumerGroup(), topic);
        consumeTps += consumeStats.getConsumeTps();
        consumeStats.setConsumeTps(consumeTps);
    }
    byte[] body = consumeStats.encode();
    response.setBody(body);
    response.setCode(ResponseCode.SUCCESS);
    response.setRemark(null);
    return response;
}
Also used : ConsumeStats(org.apache.rocketmq.common.admin.ConsumeStats) GetConsumeStatsRequestHeader(org.apache.rocketmq.common.protocol.header.GetConsumeStatsRequestHeader) TopicConfig(org.apache.rocketmq.common.TopicConfig) OffsetWrapper(org.apache.rocketmq.common.admin.OffsetWrapper) RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) SubscriptionData(org.apache.rocketmq.common.protocol.heartbeat.SubscriptionData) HashSet(java.util.HashSet)

Example 4 with OffsetWrapper

use of org.apache.rocketmq.common.admin.OffsetWrapper in project rocketmq by apache.

the class MonitorService method computeUndoneMsgs.

private void computeUndoneMsgs(final UndoneMsgs undoneMsgs, final ConsumeStats consumeStats) {
    long total = 0;
    long singleMax = 0;
    long delayMax = 0;
    Iterator<Entry<MessageQueue, OffsetWrapper>> it = consumeStats.getOffsetTable().entrySet().iterator();
    while (it.hasNext()) {
        Entry<MessageQueue, OffsetWrapper> next = it.next();
        MessageQueue mq = next.getKey();
        OffsetWrapper ow = next.getValue();
        long diff = ow.getBrokerOffset() - ow.getConsumerOffset();
        if (diff > singleMax) {
            singleMax = diff;
        }
        if (diff > 0) {
            total += diff;
        }
        // Delay
        if (ow.getLastTimestamp() > 0) {
            try {
                long maxOffset = this.defaultMQPullConsumer.maxOffset(mq);
                if (maxOffset > 0) {
                    PullResult pull = this.defaultMQPullConsumer.pull(mq, "*", maxOffset - 1, 1);
                    switch(pull.getPullStatus()) {
                        case FOUND:
                            long delay = pull.getMsgFoundList().get(0).getStoreTimestamp() - ow.getLastTimestamp();
                            if (delay > delayMax) {
                                delayMax = delay;
                            }
                            break;
                        case NO_MATCHED_MSG:
                        case NO_NEW_MSG:
                        case OFFSET_ILLEGAL:
                            break;
                        default:
                            break;
                    }
                }
            } catch (Exception e) {
            }
        }
    }
    undoneMsgs.setUndoneMsgsTotal(total);
    undoneMsgs.setUndoneMsgsSingleMQ(singleMax);
    undoneMsgs.setUndoneMsgsDelayTimeMills(delayMax);
}
Also used : Entry(java.util.Map.Entry) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) OffsetWrapper(org.apache.rocketmq.common.admin.OffsetWrapper) PullResult(org.apache.rocketmq.client.consumer.PullResult) MQClientException(org.apache.rocketmq.client.exception.MQClientException) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException)

Example 5 with OffsetWrapper

use of org.apache.rocketmq.common.admin.OffsetWrapper in project rocketmq by apache.

the class MonitorService method reportUndoneMsgs.

private void reportUndoneMsgs(final String consumerGroup) {
    ConsumeStats cs = null;
    try {
        cs = defaultMQAdminExt.examineConsumeStats(consumerGroup);
    } catch (Exception e) {
        return;
    }
    ConsumerConnection cc = null;
    try {
        cc = defaultMQAdminExt.examineConsumerConnectionInfo(consumerGroup);
    } catch (Exception e) {
        return;
    }
    if (cs != null) {
        HashMap<String, ConsumeStats> /* Topic */
        csByTopic = new HashMap<String, ConsumeStats>();
        {
            Iterator<Entry<MessageQueue, OffsetWrapper>> it = cs.getOffsetTable().entrySet().iterator();
            while (it.hasNext()) {
                Entry<MessageQueue, OffsetWrapper> next = it.next();
                MessageQueue mq = next.getKey();
                OffsetWrapper ow = next.getValue();
                ConsumeStats csTmp = csByTopic.get(mq.getTopic());
                if (null == csTmp) {
                    csTmp = new ConsumeStats();
                    csByTopic.put(mq.getTopic(), csTmp);
                }
                csTmp.getOffsetTable().put(mq, ow);
            }
        }
        {
            Iterator<Entry<String, ConsumeStats>> it = csByTopic.entrySet().iterator();
            while (it.hasNext()) {
                Entry<String, ConsumeStats> next = it.next();
                UndoneMsgs undoneMsgs = new UndoneMsgs();
                undoneMsgs.setConsumerGroup(consumerGroup);
                undoneMsgs.setTopic(next.getKey());
                this.computeUndoneMsgs(undoneMsgs, next.getValue());
                this.monitorListener.reportUndoneMsgs(undoneMsgs);
                this.reportFailedMsgs(consumerGroup, next.getKey());
            }
        }
    }
}
Also used : Entry(java.util.Map.Entry) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) ConsumeStats(org.apache.rocketmq.common.admin.ConsumeStats) HashMap(java.util.HashMap) Iterator(java.util.Iterator) ConsumerConnection(org.apache.rocketmq.common.protocol.body.ConsumerConnection) MQClientException(org.apache.rocketmq.client.exception.MQClientException) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException) OffsetWrapper(org.apache.rocketmq.common.admin.OffsetWrapper)

Aggregations

OffsetWrapper (org.apache.rocketmq.common.admin.OffsetWrapper)22 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)22 ConsumeStats (org.apache.rocketmq.common.admin.ConsumeStats)20 HashMap (java.util.HashMap)12 ArrayList (java.util.ArrayList)10 List (java.util.List)10 BrokerData (org.apache.rocketmq.common.protocol.route.BrokerData)10 Map (java.util.Map)8 ConsumerConnection (org.apache.rocketmq.common.protocol.body.ConsumerConnection)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 DefaultMQAdminExt (org.apache.rocketmq.tools.admin.DefaultMQAdminExt)8 Field (java.lang.reflect.Field)6 HashSet (java.util.HashSet)6 Entry (java.util.Map.Entry)6 MQClientAPIImpl (org.apache.rocketmq.client.impl.MQClientAPIImpl)6 MQClientInstance (org.apache.rocketmq.client.impl.factory.MQClientInstance)6 ConsumeStatsList (org.apache.rocketmq.common.protocol.body.ConsumeStatsList)6 TopicRouteData (org.apache.rocketmq.common.protocol.route.TopicRouteData)6