Search in sources :

Example 11 with ConsumerConnection

use of org.apache.rocketmq.common.protocol.body.ConsumerConnection in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class GroupConsumeInfo method getMessageQueueAllocationResult.

private Map<MessageQueue, String> getMessageQueueAllocationResult(DefaultMQAdminExt defaultMQAdminExt, String groupName) {
    Map<MessageQueue, String> results = new HashMap<>();
    try {
        ConsumerConnection consumerConnection = defaultMQAdminExt.examineConsumerConnectionInfo(groupName);
        for (Connection connection : consumerConnection.getConnectionSet()) {
            String clientId = connection.getClientId();
            ConsumerRunningInfo consumerRunningInfo = defaultMQAdminExt.getConsumerRunningInfo(groupName, clientId, false);
            for (MessageQueue messageQueue : consumerRunningInfo.getMqTable().keySet()) {
                results.put(messageQueue, clientId.split("@")[0]);
            }
        }
    } catch (Exception ignore) {
    }
    return results;
}
Also used : MessageQueue(org.apache.rocketmq.common.message.MessageQueue) HashMap(java.util.HashMap) Connection(org.apache.rocketmq.common.protocol.body.Connection) ConsumerConnection(org.apache.rocketmq.common.protocol.body.ConsumerConnection) ConsumerConnection(org.apache.rocketmq.common.protocol.body.ConsumerConnection) ConsumerRunningInfo(org.apache.rocketmq.common.protocol.body.ConsumerRunningInfo) SubCommandException(org.apache.rocketmq.tools.command.SubCommandException)

Example 12 with ConsumerConnection

use of org.apache.rocketmq.common.protocol.body.ConsumerConnection in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class ConsumerStatusSubCommand 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();
        ConsumerConnection cc = defaultMQAdminExt.examineConsumerConnectionInfo(group);
        boolean jstack = commandLine.hasOption('s');
        if (!commandLine.hasOption('i')) {
            int i = 1;
            long now = System.currentTimeMillis();
            final TreeMap<String, ConsumerRunningInfo> /* clientId */
            criTable = new TreeMap<String, ConsumerRunningInfo>();
            for (Connection conn : cc.getConnectionSet()) {
                try {
                    ConsumerRunningInfo consumerRunningInfo = defaultMQAdminExt.getConsumerRunningInfo(group, conn.getClientId(), jstack);
                    if (consumerRunningInfo != null) {
                        criTable.put(conn.getClientId(), consumerRunningInfo);
                        String filePath = now + "/" + conn.getClientId();
                        MixAll.string2FileNotSafe(consumerRunningInfo.formatString(), filePath);
                        System.out.printf("%03d  %-40s %-20s %s%n", i++, conn.getClientId(), MQVersion.getVersionDesc(conn.getVersion()), filePath);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (!criTable.isEmpty()) {
                boolean subSame = ConsumerRunningInfo.analyzeSubscription(criTable);
                boolean rebalanceOK = subSame && ConsumerRunningInfo.analyzeRebalance(criTable);
                if (subSame) {
                    System.out.printf("%n%nSame subscription in the same group of consumer");
                    System.out.printf("%n%nRebalance %s%n", rebalanceOK ? "OK" : "Failed");
                    Iterator<Entry<String, ConsumerRunningInfo>> it = criTable.entrySet().iterator();
                    while (it.hasNext()) {
                        Entry<String, ConsumerRunningInfo> next = it.next();
                        String result = ConsumerRunningInfo.analyzeProcessQueue(next.getKey(), next.getValue());
                        if (result.length() > 0) {
                            System.out.printf(result);
                        }
                    }
                } else {
                    System.out.printf("%n%nWARN: Different subscription in the same group of consumer!!!");
                }
            }
        } else {
            String clientId = commandLine.getOptionValue('i').trim();
            ConsumerRunningInfo consumerRunningInfo = defaultMQAdminExt.getConsumerRunningInfo(group, clientId, jstack);
            if (consumerRunningInfo != null) {
                System.out.printf("%s", consumerRunningInfo.formatString());
            }
        }
    } catch (Exception e) {
        throw new SubCommandException(this.getClass().getSimpleName() + " command failed", e);
    } finally {
        defaultMQAdminExt.shutdown();
    }
}
Also used : SubCommandException(org.apache.rocketmq.tools.command.SubCommandException) Connection(org.apache.rocketmq.common.protocol.body.Connection) ConsumerConnection(org.apache.rocketmq.common.protocol.body.ConsumerConnection) ConsumerConnection(org.apache.rocketmq.common.protocol.body.ConsumerConnection) TreeMap(java.util.TreeMap) SubCommandException(org.apache.rocketmq.tools.command.SubCommandException) Entry(java.util.Map.Entry) ConsumerRunningInfo(org.apache.rocketmq.common.protocol.body.ConsumerRunningInfo) DefaultMQAdminExt(org.apache.rocketmq.tools.admin.DefaultMQAdminExt)

Example 13 with ConsumerConnection

use of org.apache.rocketmq.common.protocol.body.ConsumerConnection in project rocketmq by apache.

the class MQClientAPIImpl method getConsumerConnectionList.

public ConsumerConnection getConsumerConnectionList(final String addr, final String consumerGroup, final long timeoutMillis) throws RemotingConnectException, RemotingSendRequestException, RemotingTimeoutException, InterruptedException, MQBrokerException {
    GetConsumerConnectionListRequestHeader requestHeader = new GetConsumerConnectionListRequestHeader();
    requestHeader.setConsumerGroup(consumerGroup);
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.GET_CONSUMER_CONNECTION_LIST, requestHeader);
    RemotingCommand response = this.remotingClient.invokeSync(MixAll.brokerVIPChannel(this.clientConfig.isVipChannelEnabled(), addr), request, timeoutMillis);
    switch(response.getCode()) {
        case ResponseCode.SUCCESS:
            {
                return ConsumerConnection.decode(response.getBody(), ConsumerConnection.class);
            }
        default:
            break;
    }
    throw new MQBrokerException(response.getCode(), response.getRemark());
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) GetConsumerConnectionListRequestHeader(org.apache.rocketmq.common.protocol.header.GetConsumerConnectionListRequestHeader) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) ConsumerConnection(org.apache.rocketmq.common.protocol.body.ConsumerConnection)

Example 14 with ConsumerConnection

use of org.apache.rocketmq.common.protocol.body.ConsumerConnection in project rocketmq by apache.

the class AdminBrokerProcessor method getConsumerConnectionList.

private RemotingCommand getConsumerConnectionList(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(null);
    final GetConsumerConnectionListRequestHeader requestHeader = (GetConsumerConnectionListRequestHeader) request.decodeCommandCustomHeader(GetConsumerConnectionListRequestHeader.class);
    ConsumerGroupInfo consumerGroupInfo = this.brokerController.getConsumerManager().getConsumerGroupInfo(requestHeader.getConsumerGroup());
    if (consumerGroupInfo != null) {
        ConsumerConnection bodydata = new ConsumerConnection();
        bodydata.setConsumeFromWhere(consumerGroupInfo.getConsumeFromWhere());
        bodydata.setConsumeType(consumerGroupInfo.getConsumeType());
        bodydata.setMessageModel(consumerGroupInfo.getMessageModel());
        bodydata.getSubscriptionTable().putAll(consumerGroupInfo.getSubscriptionTable());
        Iterator<Map.Entry<Channel, ClientChannelInfo>> it = consumerGroupInfo.getChannelInfoTable().entrySet().iterator();
        while (it.hasNext()) {
            ClientChannelInfo info = it.next().getValue();
            Connection connection = new Connection();
            connection.setClientId(info.getClientId());
            connection.setLanguage(info.getLanguage());
            connection.setVersion(info.getVersion());
            connection.setClientAddr(RemotingHelper.parseChannelRemoteAddr(info.getChannel()));
            bodydata.getConnectionSet().add(connection);
        }
        byte[] body = bodydata.encode();
        response.setBody(body);
        response.setCode(ResponseCode.SUCCESS);
        response.setRemark(null);
        return response;
    }
    response.setCode(ResponseCode.CONSUMER_NOT_ONLINE);
    response.setRemark("the consumer group[" + requestHeader.getConsumerGroup() + "] not online");
    return response;
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) GetConsumerConnectionListRequestHeader(org.apache.rocketmq.common.protocol.header.GetConsumerConnectionListRequestHeader) ClientChannelInfo(org.apache.rocketmq.broker.client.ClientChannelInfo) ConsumerConnection(org.apache.rocketmq.common.protocol.body.ConsumerConnection) Connection(org.apache.rocketmq.common.protocol.body.Connection) ProducerConnection(org.apache.rocketmq.common.protocol.body.ProducerConnection) ConsumerConnection(org.apache.rocketmq.common.protocol.body.ConsumerConnection) ConsumerGroupInfo(org.apache.rocketmq.broker.client.ConsumerGroupInfo)

Example 15 with ConsumerConnection

use of org.apache.rocketmq.common.protocol.body.ConsumerConnection 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

ConsumerConnection (org.apache.rocketmq.common.protocol.body.ConsumerConnection)36 Connection (org.apache.rocketmq.common.protocol.body.Connection)22 ConsumerRunningInfo (org.apache.rocketmq.common.protocol.body.ConsumerRunningInfo)15 DefaultMQAdminExt (org.apache.rocketmq.tools.admin.DefaultMQAdminExt)14 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)13 SubscriptionData (org.apache.rocketmq.common.protocol.heartbeat.SubscriptionData)12 HashMap (java.util.HashMap)10 Entry (java.util.Map.Entry)10 MQClientException (org.apache.rocketmq.client.exception.MQClientException)10 SubCommandException (org.apache.rocketmq.tools.command.SubCommandException)10 ConsumeStats (org.apache.rocketmq.common.admin.ConsumeStats)9 Field (java.lang.reflect.Field)8 ArrayList (java.util.ArrayList)8 HashSet (java.util.HashSet)8 TreeMap (java.util.TreeMap)8 MQBrokerException (org.apache.rocketmq.client.exception.MQBrokerException)8 MQClientAPIImpl (org.apache.rocketmq.client.impl.MQClientAPIImpl)8 MQClientInstance (org.apache.rocketmq.client.impl.factory.MQClientInstance)8 OffsetWrapper (org.apache.rocketmq.common.admin.OffsetWrapper)8 BrokerData (org.apache.rocketmq.common.protocol.route.BrokerData)8