Search in sources :

Example 36 with SubCommandException

use of org.apache.rocketmq.tools.command.SubCommandException in project rocketmq by apache.

the class CLusterSendMsgRTCommand 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()));
    DefaultMQProducer producer = new DefaultMQProducer(rpcHook);
    producer.setProducerGroup(Long.toString(System.currentTimeMillis()));
    try {
        defaultMQAdminExt.start();
        producer.start();
        ClusterInfo clusterInfoSerializeWrapper = defaultMQAdminExt.examineBrokerClusterInfo();
        HashMap<String, Set<String>> clusterAddr = clusterInfoSerializeWrapper.getClusterAddrTable();
        Set<String> clusterNames = null;
        long amount = !commandLine.hasOption('a') ? 50 : Long.parseLong(commandLine.getOptionValue('a').trim());
        long size = !commandLine.hasOption('s') ? 128 : Long.parseLong(commandLine.getOptionValue('s').trim());
        long interval = !commandLine.hasOption('i') ? 10 : Long.parseLong(commandLine.getOptionValue('i').trim());
        boolean printAsTlog = commandLine.hasOption('p') && Boolean.parseBoolean(commandLine.getOptionValue('p').trim());
        String machineRoom = !commandLine.hasOption('m') ? "noname" : commandLine.getOptionValue('m').trim();
        if (commandLine.hasOption('c')) {
            clusterNames = new TreeSet<String>();
            clusterNames.add(commandLine.getOptionValue('c').trim());
        } else {
            clusterNames = clusterAddr.keySet();
        }
        if (!printAsTlog) {
            System.out.printf("%-24s  %-24s  %-4s  %-8s  %-8s%n", "#Cluster Name", "#Broker Name", "#RT", "#successCount", "#failCount");
        }
        while (true) {
            for (String clusterName : clusterNames) {
                Set<String> brokerNames = clusterAddr.get(clusterName);
                if (brokerNames == null) {
                    System.out.printf("cluster [%s] not exist", clusterName);
                    break;
                }
                for (String brokerName : brokerNames) {
                    Message msg = new Message(brokerName, getStringBySize(size).getBytes(MixAll.DEFAULT_CHARSET));
                    long start = 0;
                    long end = 0;
                    long elapsed = 0;
                    int successCount = 0;
                    int failCount = 0;
                    for (int i = 0; i < amount; i++) {
                        start = System.currentTimeMillis();
                        try {
                            producer.send(msg);
                            successCount++;
                            end = System.currentTimeMillis();
                        } catch (Exception e) {
                            failCount++;
                            end = System.currentTimeMillis();
                        }
                        if (i != 0) {
                            elapsed += end - start;
                        }
                    }
                    double rt = (double) elapsed / (amount - 1);
                    if (!printAsTlog) {
                        System.out.printf("%-24s  %-24s  %-8s  %-16s  %-16s%n", clusterName, brokerName, String.format("%.2f", rt), successCount, failCount);
                    } else {
                        System.out.printf(String.format("%s|%s|%s|%s|%s%n", getCurTime(), machineRoom, clusterName, brokerName, new BigDecimal(rt).setScale(0, BigDecimal.ROUND_HALF_UP)));
                    }
                }
            }
            Thread.sleep(interval * 1000);
        }
    } catch (Exception e) {
        throw new SubCommandException(this.getClass().getSimpleName() + " command failed", e);
    } finally {
        defaultMQAdminExt.shutdown();
        producer.shutdown();
    }
}
Also used : Set(java.util.Set) TreeSet(java.util.TreeSet) Message(org.apache.rocketmq.common.message.Message) SubCommandException(org.apache.rocketmq.tools.command.SubCommandException) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) SubCommandException(org.apache.rocketmq.tools.command.SubCommandException) BigDecimal(java.math.BigDecimal) ClusterInfo(org.apache.rocketmq.common.protocol.body.ClusterInfo) DefaultMQAdminExt(org.apache.rocketmq.tools.admin.DefaultMQAdminExt)

Example 37 with SubCommandException

use of org.apache.rocketmq.tools.command.SubCommandException in project rocketmq by apache.

the class ProducerConnectionSubCommand 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 group = commandLine.getOptionValue('g').trim();
        String topic = commandLine.getOptionValue('t').trim();
        ProducerConnection pc = defaultMQAdminExt.examineProducerConnectionInfo(group, topic);
        int i = 1;
        for (Connection conn : pc.getConnectionSet()) {
            System.out.printf("%04d  %-32s %-22s %-8s %s%n", i++, conn.getClientId(), conn.getClientAddr(), conn.getLanguage(), MQVersion.getVersionDesc(conn.getVersion()));
        }
    } catch (Exception e) {
        throw new SubCommandException(this.getClass().getSimpleName() + " command failed", e);
    } finally {
        defaultMQAdminExt.shutdown();
    }
}
Also used : SubCommandException(org.apache.rocketmq.tools.command.SubCommandException) ProducerConnection(org.apache.rocketmq.common.protocol.body.ProducerConnection) Connection(org.apache.rocketmq.common.protocol.body.Connection) DefaultMQAdminExt(org.apache.rocketmq.tools.admin.DefaultMQAdminExt) ProducerConnection(org.apache.rocketmq.common.protocol.body.ProducerConnection) SubCommandException(org.apache.rocketmq.tools.command.SubCommandException)

Example 38 with SubCommandException

use of org.apache.rocketmq.tools.command.SubCommandException in project rocketmq by apache.

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();
        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 39 with SubCommandException

use of org.apache.rocketmq.tools.command.SubCommandException in project rocketmq by apache.

the class GetConsumerStatusCommand 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 {
        String group = commandLine.getOptionValue("g").trim();
        String topic = commandLine.getOptionValue("t").trim();
        String originClientId = "";
        if (commandLine.hasOption("i")) {
            originClientId = commandLine.getOptionValue("i").trim();
        }
        defaultMQAdminExt.start();
        Map<String, Map<MessageQueue, Long>> consumerStatusTable = defaultMQAdminExt.getConsumeStatus(topic, group, originClientId);
        System.out.printf("get consumer status from client. group=%s, topic=%s, originClientId=%s%n", group, topic, originClientId);
        System.out.printf("%-50s  %-15s  %-15s  %-20s%n", "#clientId", "#brokerName", "#queueId", "#offset");
        for (Map.Entry<String, Map<MessageQueue, Long>> entry : consumerStatusTable.entrySet()) {
            String clientId = entry.getKey();
            Map<MessageQueue, Long> mqTable = entry.getValue();
            for (Map.Entry<MessageQueue, Long> entry1 : mqTable.entrySet()) {
                MessageQueue mq = entry1.getKey();
                System.out.printf("%-50s  %-15s  %-15d  %-20d%n", UtilAll.frontStringAtLeast(clientId, 50), mq.getBrokerName(), mq.getQueueId(), mqTable.get(mq));
            }
        }
    } catch (Exception e) {
        throw new SubCommandException(this.getClass().getSimpleName() + " command failed", e);
    } finally {
        defaultMQAdminExt.shutdown();
    }
}
Also used : MessageQueue(org.apache.rocketmq.common.message.MessageQueue) SubCommandException(org.apache.rocketmq.tools.command.SubCommandException) DefaultMQAdminExt(org.apache.rocketmq.tools.admin.DefaultMQAdminExt) Map(java.util.Map) SubCommandException(org.apache.rocketmq.tools.command.SubCommandException)

Example 40 with SubCommandException

use of org.apache.rocketmq.tools.command.SubCommandException in project rocketmq by apache.

the class ResetOffsetByTimeCommand 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 {
        String group = commandLine.getOptionValue("g").trim();
        String topic = commandLine.getOptionValue("t").trim();
        String timeStampStr = commandLine.getOptionValue("s").trim();
        long timestamp = timeStampStr.equals("now") ? System.currentTimeMillis() : 0;
        try {
            if (timestamp == 0) {
                timestamp = Long.parseLong(timeStampStr);
            }
        } catch (NumberFormatException e) {
            timestamp = UtilAll.parseDate(timeStampStr, UtilAll.YYYY_MM_DD_HH_MM_SS_SSS).getTime();
        }
        boolean force = true;
        if (commandLine.hasOption('f')) {
            force = Boolean.valueOf(commandLine.getOptionValue("f").trim());
        }
        boolean isC = false;
        if (commandLine.hasOption('c')) {
            isC = true;
        }
        defaultMQAdminExt.start();
        Map<MessageQueue, Long> offsetTable;
        try {
            offsetTable = defaultMQAdminExt.resetOffsetByTimestamp(topic, group, timestamp, force, isC);
        } catch (MQClientException e) {
            if (ResponseCode.CONSUMER_NOT_ONLINE == e.getResponseCode()) {
                ResetOffsetByTimeOldCommand.resetOffset(defaultMQAdminExt, group, topic, timestamp, force, timeStampStr);
                return;
            }
            throw e;
        }
        System.out.printf("rollback consumer offset by specified group[%s], topic[%s], force[%s], timestamp(string)[%s], timestamp(long)[%s]%n", group, topic, force, timeStampStr, timestamp);
        System.out.printf("%-40s  %-40s  %-40s%n", "#brokerName", "#queueId", "#offset");
        Iterator<Map.Entry<MessageQueue, Long>> iterator = offsetTable.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<MessageQueue, Long> entry = iterator.next();
            System.out.printf("%-40s  %-40d  %-40d%n", UtilAll.frontStringAtLeast(entry.getKey().getBrokerName(), 32), entry.getKey().getQueueId(), entry.getValue());
        }
    } catch (Exception e) {
        throw new SubCommandException(this.getClass().getSimpleName() + " command failed", e);
    } finally {
        defaultMQAdminExt.shutdown();
    }
}
Also used : SubCommandException(org.apache.rocketmq.tools.command.SubCommandException) SubCommandException(org.apache.rocketmq.tools.command.SubCommandException) MQClientException(org.apache.rocketmq.client.exception.MQClientException) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) DefaultMQAdminExt(org.apache.rocketmq.tools.admin.DefaultMQAdminExt) Map(java.util.Map) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Aggregations

SubCommandException (org.apache.rocketmq.tools.command.SubCommandException)88 DefaultMQAdminExt (org.apache.rocketmq.tools.admin.DefaultMQAdminExt)76 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)22 MQBrokerException (org.apache.rocketmq.client.exception.MQBrokerException)16 MQClientException (org.apache.rocketmq.client.exception.MQClientException)16 Set (java.util.Set)14 RemotingException (org.apache.rocketmq.remoting.exception.RemotingException)12 UnsupportedEncodingException (java.io.UnsupportedEncodingException)8 LinkedList (java.util.LinkedList)8 Map (java.util.Map)8 DefaultMQProducer (org.apache.rocketmq.client.producer.DefaultMQProducer)8 Connection (org.apache.rocketmq.common.protocol.body.Connection)8 ConsumerConnection (org.apache.rocketmq.common.protocol.body.ConsumerConnection)8 TopicRouteData (org.apache.rocketmq.common.protocol.route.TopicRouteData)8 Date (java.util.Date)6 Entry (java.util.Map.Entry)6 Properties (java.util.Properties)6 DefaultMQPullConsumer (org.apache.rocketmq.client.consumer.DefaultMQPullConsumer)6 PullResult (org.apache.rocketmq.client.consumer.PullResult)6 ConsumeStats (org.apache.rocketmq.common.admin.ConsumeStats)6