Search in sources :

Example 46 with SubCommandException

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

the class UpdateTopicSubCommand method execute.

@Override
public void execute(final CommandLine commandLine, final Options options, RPCHook rpcHook) throws SubCommandException {
    DefaultMQAdminExt defaultMQAdminExt = new DefaultMQAdminExt(rpcHook);
    defaultMQAdminExt.setInstanceName(Long.toString(System.currentTimeMillis()));
    try {
        TopicConfig topicConfig = new TopicConfig();
        topicConfig.setReadQueueNums(8);
        topicConfig.setWriteQueueNums(8);
        topicConfig.setTopicName(commandLine.getOptionValue('t').trim());
        // readQueueNums
        if (commandLine.hasOption('r')) {
            topicConfig.setReadQueueNums(Integer.parseInt(commandLine.getOptionValue('r').trim()));
        }
        // writeQueueNums
        if (commandLine.hasOption('w')) {
            topicConfig.setWriteQueueNums(Integer.parseInt(commandLine.getOptionValue('w').trim()));
        }
        // perm
        if (commandLine.hasOption('p')) {
            topicConfig.setPerm(Integer.parseInt(commandLine.getOptionValue('p').trim()));
        }
        boolean isUnit = false;
        if (commandLine.hasOption('u')) {
            isUnit = Boolean.parseBoolean(commandLine.getOptionValue('u').trim());
        }
        boolean isCenterSync = false;
        if (commandLine.hasOption('s')) {
            isCenterSync = Boolean.parseBoolean(commandLine.getOptionValue('s').trim());
        }
        int topicCenterSync = TopicSysFlag.buildSysFlag(isUnit, isCenterSync);
        topicConfig.setTopicSysFlag(topicCenterSync);
        boolean isOrder = false;
        if (commandLine.hasOption('o')) {
            isOrder = Boolean.parseBoolean(commandLine.getOptionValue('o').trim());
        }
        topicConfig.setOrder(isOrder);
        if (commandLine.hasOption('b')) {
            String addr = commandLine.getOptionValue('b').trim();
            defaultMQAdminExt.start();
            defaultMQAdminExt.createAndUpdateTopicConfig(addr, topicConfig);
            if (isOrder) {
                String brokerName = CommandUtil.fetchBrokerNameByAddr(defaultMQAdminExt, addr);
                String orderConf = brokerName + ":" + topicConfig.getWriteQueueNums();
                defaultMQAdminExt.createOrUpdateOrderConf(topicConfig.getTopicName(), orderConf, false);
                System.out.printf(String.format("set broker orderConf. isOrder=%s, orderConf=[%s]", isOrder, orderConf.toString()));
            }
            System.out.printf("create topic to %s success.%n", addr);
            System.out.printf("%s", topicConfig);
            return;
        } else if (commandLine.hasOption('c')) {
            String clusterName = commandLine.getOptionValue('c').trim();
            defaultMQAdminExt.start();
            Set<String> masterSet = CommandUtil.fetchMasterAddrByClusterName(defaultMQAdminExt, clusterName);
            for (String addr : masterSet) {
                defaultMQAdminExt.createAndUpdateTopicConfig(addr, topicConfig);
                System.out.printf("create topic to %s success.%n", addr);
            }
            if (isOrder) {
                Set<String> brokerNameSet = CommandUtil.fetchBrokerNameByClusterName(defaultMQAdminExt, clusterName);
                StringBuilder orderConf = new StringBuilder();
                String splitor = "";
                for (String s : brokerNameSet) {
                    orderConf.append(splitor).append(s).append(":").append(topicConfig.getWriteQueueNums());
                    splitor = ";";
                }
                defaultMQAdminExt.createOrUpdateOrderConf(topicConfig.getTopicName(), orderConf.toString(), true);
                System.out.printf("set cluster orderConf. isOrder=%s, orderConf=[%s]", isOrder, orderConf);
            }
            System.out.printf("%s", topicConfig);
            return;
        }
        ServerUtil.printCommandLineHelp("mqadmin " + this.commandName(), options);
    } catch (Exception e) {
        throw new SubCommandException(this.getClass().getSimpleName() + " command failed", e);
    } finally {
        defaultMQAdminExt.shutdown();
    }
}
Also used : Set(java.util.Set) SubCommandException(org.apache.rocketmq.tools.command.SubCommandException) DefaultMQAdminExt(org.apache.rocketmq.tools.admin.DefaultMQAdminExt) TopicConfig(org.apache.rocketmq.common.TopicConfig) SubCommandException(org.apache.rocketmq.tools.command.SubCommandException)

Example 47 with SubCommandException

use of org.apache.rocketmq.tools.command.SubCommandException 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 48 with SubCommandException

use of org.apache.rocketmq.tools.command.SubCommandException in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class BrokerStatusSubCommand 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.hasOption('b') ? commandLine.getOptionValue('b').trim() : null;
        String clusterName = commandLine.hasOption('c') ? commandLine.getOptionValue('c').trim() : null;
        if (brokerAddr != null) {
            printBrokerRuntimeStats(defaultMQAdminExt, brokerAddr, false);
        } else if (clusterName != null) {
            Set<String> masterSet = CommandUtil.fetchMasterAndSlaveAddrByClusterName(defaultMQAdminExt, clusterName);
            for (String ba : masterSet) {
                try {
                    printBrokerRuntimeStats(defaultMQAdminExt, ba, true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    } catch (Exception e) {
        throw new SubCommandException(this.getClass().getSimpleName() + " command failed", e);
    } finally {
        defaultMQAdminExt.shutdown();
    }
}
Also used : Set(java.util.Set) SubCommandException(org.apache.rocketmq.tools.command.SubCommandException) DefaultMQAdminExt(org.apache.rocketmq.tools.admin.DefaultMQAdminExt) RemotingTimeoutException(org.apache.rocketmq.remoting.exception.RemotingTimeoutException) SubCommandException(org.apache.rocketmq.tools.command.SubCommandException) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) RemotingConnectException(org.apache.rocketmq.remoting.exception.RemotingConnectException) RemotingSendRequestException(org.apache.rocketmq.remoting.exception.RemotingSendRequestException)

Example 49 with SubCommandException

use of org.apache.rocketmq.tools.command.SubCommandException in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class CleanExpiredCQSubCommand 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 {
        boolean result = false;
        defaultMQAdminExt.start();
        if (commandLine.hasOption('b')) {
            String addr = commandLine.getOptionValue('b').trim();
            result = defaultMQAdminExt.cleanExpiredConsumerQueueByAddr(addr);
        } else {
            String cluster = commandLine.getOptionValue('c');
            if (null != cluster)
                cluster = cluster.trim();
            result = defaultMQAdminExt.cleanExpiredConsumerQueue(cluster);
        }
        System.out.printf(result ? "success" : "false");
    } catch (Exception e) {
        throw new SubCommandException(this.getClass().getSimpleName() + " command failed", e);
    } finally {
        defaultMQAdminExt.shutdown();
    }
}
Also used : SubCommandException(org.apache.rocketmq.tools.command.SubCommandException) DefaultMQAdminExt(org.apache.rocketmq.tools.admin.DefaultMQAdminExt) SubCommandException(org.apache.rocketmq.tools.command.SubCommandException)

Example 50 with SubCommandException

use of org.apache.rocketmq.tools.command.SubCommandException in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class CleanUnusedTopicCommand 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 {
        boolean result = false;
        defaultMQAdminExt.start();
        if (commandLine.hasOption('b')) {
            String addr = commandLine.getOptionValue('b').trim();
            result = defaultMQAdminExt.cleanUnusedTopicByAddr(addr);
        } else {
            String cluster = commandLine.getOptionValue('c');
            if (null != cluster)
                cluster = cluster.trim();
            result = defaultMQAdminExt.cleanUnusedTopicByAddr(cluster);
        }
        System.out.printf(result ? "success" : "false");
    } catch (Exception e) {
        throw new SubCommandException(this.getClass().getSimpleName() + " command failed", e);
    } finally {
        defaultMQAdminExt.shutdown();
    }
}
Also used : SubCommandException(org.apache.rocketmq.tools.command.SubCommandException) DefaultMQAdminExt(org.apache.rocketmq.tools.admin.DefaultMQAdminExt) SubCommandException(org.apache.rocketmq.tools.command.SubCommandException)

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