Search in sources :

Example 1 with ClientChannelInfo

use of org.apache.rocketmq.broker.client.ClientChannelInfo in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class Broker2Client method getConsumeStatus.

public RemotingCommand getConsumeStatus(String topic, String group, String originClientId) {
    final RemotingCommand result = RemotingCommand.createResponseCommand(null);
    GetConsumerStatusRequestHeader requestHeader = new GetConsumerStatusRequestHeader();
    requestHeader.setTopic(topic);
    requestHeader.setGroup(group);
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.GET_CONSUMER_STATUS_FROM_CLIENT, requestHeader);
    Map<String, Map<MessageQueue, Long>> consumerStatusTable = new HashMap<String, Map<MessageQueue, Long>>();
    ConcurrentMap<Channel, ClientChannelInfo> channelInfoTable = this.brokerController.getConsumerManager().getConsumerGroupInfo(group).getChannelInfoTable();
    if (null == channelInfoTable || channelInfoTable.isEmpty()) {
        result.setCode(ResponseCode.SYSTEM_ERROR);
        result.setRemark(String.format("No Any Consumer online in the consumer group: [%s]", group));
        return result;
    }
    for (Map.Entry<Channel, ClientChannelInfo> entry : channelInfoTable.entrySet()) {
        int version = entry.getValue().getVersion();
        String clientId = entry.getValue().getClientId();
        if (version < MQVersion.Version.V3_0_7_SNAPSHOT.ordinal()) {
            result.setCode(ResponseCode.SYSTEM_ERROR);
            result.setRemark("the client does not support this feature. version=" + MQVersion.getVersionDesc(version));
            log.warn("[get-consumer-status] the client does not support this feature. version={}", RemotingHelper.parseChannelRemoteAddr(entry.getKey()), MQVersion.getVersionDesc(version));
            return result;
        } else if (UtilAll.isBlank(originClientId) || originClientId.equals(clientId)) {
            try {
                RemotingCommand response = this.brokerController.getRemotingServer().invokeSync(entry.getKey(), request, 5000);
                assert response != null;
                switch(response.getCode()) {
                    case ResponseCode.SUCCESS:
                        {
                            if (response.getBody() != null) {
                                GetConsumerStatusBody body = GetConsumerStatusBody.decode(response.getBody(), GetConsumerStatusBody.class);
                                consumerStatusTable.put(clientId, body.getMessageQueueTable());
                                log.info("[get-consumer-status] get consumer status success. topic={}, group={}, channelRemoteAddr={}", topic, group, clientId);
                            }
                        }
                    default:
                        break;
                }
            } catch (Exception e) {
                log.error("[get-consumer-status] get consumer status exception. topic={}, group={}, offset={}", new Object[] { topic, group }, e);
            }
            if (!UtilAll.isBlank(originClientId) && originClientId.equals(clientId)) {
                break;
            }
        }
    }
    result.setCode(ResponseCode.SUCCESS);
    GetConsumerStatusBody resBody = new GetConsumerStatusBody();
    resBody.setConsumerTable(consumerStatusTable);
    result.setBody(resBody.encode());
    return result;
}
Also used : ClientChannelInfo(org.apache.rocketmq.broker.client.ClientChannelInfo) HashMap(java.util.HashMap) Channel(io.netty.channel.Channel) RemotingSendRequestException(org.apache.rocketmq.remoting.exception.RemotingSendRequestException) RemotingTimeoutException(org.apache.rocketmq.remoting.exception.RemotingTimeoutException) RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) GetConsumerStatusBody(org.apache.rocketmq.common.protocol.body.GetConsumerStatusBody) GetConsumerStatusRequestHeader(org.apache.rocketmq.common.protocol.header.GetConsumerStatusRequestHeader) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map)

Example 2 with ClientChannelInfo

use of org.apache.rocketmq.broker.client.ClientChannelInfo in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

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 3 with ClientChannelInfo

use of org.apache.rocketmq.broker.client.ClientChannelInfo in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class AdminBrokerProcessor method callConsumer.

private // 
RemotingCommand callConsumer(// 
final int requestCode, // 
final RemotingCommand request, // 
final String consumerGroup, final String clientId) throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(null);
    ClientChannelInfo clientChannelInfo = this.brokerController.getConsumerManager().findChannel(consumerGroup, clientId);
    if (null == clientChannelInfo) {
        response.setCode(ResponseCode.SYSTEM_ERROR);
        response.setRemark(String.format("The Consumer <%s> <%s> not online", consumerGroup, clientId));
        return response;
    }
    if (clientChannelInfo.getVersion() < MQVersion.Version.V3_1_8_SNAPSHOT.ordinal()) {
        response.setCode(ResponseCode.SYSTEM_ERROR);
        response.setRemark(// 
        String.format(// 
        "The Consumer <%s> Version <%s> too low to finish, please upgrade it to V3_1_8_SNAPSHOT", // 
        clientId, MQVersion.getVersionDesc(clientChannelInfo.getVersion())));
        return response;
    }
    try {
        RemotingCommand newRequest = RemotingCommand.createRequestCommand(requestCode, null);
        newRequest.setExtFields(request.getExtFields());
        newRequest.setBody(request.getBody());
        return this.brokerController.getBroker2Client().callClient(clientChannelInfo.getChannel(), newRequest);
    } catch (RemotingTimeoutException e) {
        response.setCode(ResponseCode.CONSUME_MSG_TIMEOUT);
        response.setRemark(String.format("consumer <%s> <%s> Timeout: %s", consumerGroup, clientId, RemotingHelper.exceptionSimpleDesc(e)));
        return response;
    } catch (Exception e) {
        response.setCode(ResponseCode.SYSTEM_ERROR);
        response.setRemark(String.format("invoke consumer <%s> <%s> Exception: %s", consumerGroup, clientId, RemotingHelper.exceptionSimpleDesc(e)));
        return response;
    }
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) ClientChannelInfo(org.apache.rocketmq.broker.client.ClientChannelInfo) RemotingTimeoutException(org.apache.rocketmq.remoting.exception.RemotingTimeoutException) RemotingTimeoutException(org.apache.rocketmq.remoting.exception.RemotingTimeoutException) UnknownHostException(java.net.UnknownHostException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RemotingCommandException(org.apache.rocketmq.remoting.exception.RemotingCommandException)

Example 4 with ClientChannelInfo

use of org.apache.rocketmq.broker.client.ClientChannelInfo in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class ClientManageProcessor method unregisterClient.

public RemotingCommand unregisterClient(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(UnregisterClientResponseHeader.class);
    final UnregisterClientRequestHeader requestHeader = (UnregisterClientRequestHeader) request.decodeCommandCustomHeader(UnregisterClientRequestHeader.class);
    ClientChannelInfo clientChannelInfo = new ClientChannelInfo(ctx.channel(), requestHeader.getClientID(), request.getLanguage(), request.getVersion());
    {
        final String group = requestHeader.getProducerGroup();
        if (group != null) {
            this.brokerController.getProducerManager().unregisterProducer(group, clientChannelInfo);
        }
    }
    {
        final String group = requestHeader.getConsumerGroup();
        if (group != null) {
            SubscriptionGroupConfig subscriptionGroupConfig = this.brokerController.getSubscriptionGroupManager().findSubscriptionGroupConfig(group);
            boolean isNotifyConsumerIdsChangedEnable = true;
            if (null != subscriptionGroupConfig) {
                isNotifyConsumerIdsChangedEnable = subscriptionGroupConfig.isNotifyConsumerIdsChangedEnable();
            }
            this.brokerController.getConsumerManager().unregisterConsumer(group, clientChannelInfo, isNotifyConsumerIdsChangedEnable);
        }
    }
    response.setCode(ResponseCode.SUCCESS);
    response.setRemark(null);
    return response;
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) UnregisterClientRequestHeader(org.apache.rocketmq.common.protocol.header.UnregisterClientRequestHeader) ClientChannelInfo(org.apache.rocketmq.broker.client.ClientChannelInfo) SubscriptionGroupConfig(org.apache.rocketmq.common.subscription.SubscriptionGroupConfig)

Example 5 with ClientChannelInfo

use of org.apache.rocketmq.broker.client.ClientChannelInfo in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class ClientManageProcessor method heartBeat.

public RemotingCommand heartBeat(ChannelHandlerContext ctx, RemotingCommand request) {
    RemotingCommand response = RemotingCommand.createResponseCommand(null);
    HeartbeatData heartbeatData = HeartbeatData.decode(request.getBody(), HeartbeatData.class);
    ClientChannelInfo clientChannelInfo = new ClientChannelInfo(ctx.channel(), heartbeatData.getClientID(), request.getLanguage(), request.getVersion());
    for (ConsumerData data : heartbeatData.getConsumerDataSet()) {
        SubscriptionGroupConfig subscriptionGroupConfig = this.brokerController.getSubscriptionGroupManager().findSubscriptionGroupConfig(data.getGroupName());
        boolean isNotifyConsumerIdsChangedEnable = true;
        if (null != subscriptionGroupConfig) {
            isNotifyConsumerIdsChangedEnable = subscriptionGroupConfig.isNotifyConsumerIdsChangedEnable();
            int topicSysFlag = 0;
            if (data.isUnitMode()) {
                topicSysFlag = TopicSysFlag.buildSysFlag(false, true);
            }
            String newTopic = MixAll.getRetryTopic(data.getGroupName());
            this.brokerController.getTopicConfigManager().createTopicInSendMessageBackMethod(newTopic, subscriptionGroupConfig.getRetryQueueNums(), PermName.PERM_WRITE | PermName.PERM_READ, topicSysFlag);
        }
        boolean changed = this.brokerController.getConsumerManager().registerConsumer(data.getGroupName(), clientChannelInfo, data.getConsumeType(), data.getMessageModel(), data.getConsumeFromWhere(), data.getSubscriptionDataSet(), isNotifyConsumerIdsChangedEnable);
        if (changed) {
            log.info("registerConsumer info changed {} {}", data.toString(), RemotingHelper.parseChannelRemoteAddr(ctx.channel()));
        }
    }
    for (ProducerData data : heartbeatData.getProducerDataSet()) {
        this.brokerController.getProducerManager().registerProducer(data.getGroupName(), clientChannelInfo);
    }
    response.setCode(ResponseCode.SUCCESS);
    response.setRemark(null);
    return response;
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) ClientChannelInfo(org.apache.rocketmq.broker.client.ClientChannelInfo) ConsumerData(org.apache.rocketmq.common.protocol.heartbeat.ConsumerData) HeartbeatData(org.apache.rocketmq.common.protocol.heartbeat.HeartbeatData) ProducerData(org.apache.rocketmq.common.protocol.heartbeat.ProducerData) SubscriptionGroupConfig(org.apache.rocketmq.common.subscription.SubscriptionGroupConfig)

Aggregations

ClientChannelInfo (org.apache.rocketmq.broker.client.ClientChannelInfo)20 RemotingCommand (org.apache.rocketmq.remoting.protocol.RemotingCommand)16 Channel (io.netty.channel.Channel)10 ConsumerData (org.apache.rocketmq.common.protocol.heartbeat.ConsumerData)6 RemotingTimeoutException (org.apache.rocketmq.remoting.exception.RemotingTimeoutException)6 HashMap (java.util.HashMap)4 Map (java.util.Map)4 ConcurrentMap (java.util.concurrent.ConcurrentMap)4 ConsumerGroupInfo (org.apache.rocketmq.broker.client.ConsumerGroupInfo)4 TopicConfig (org.apache.rocketmq.common.TopicConfig)4 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)4 Connection (org.apache.rocketmq.common.protocol.body.Connection)4 ConsumerConnection (org.apache.rocketmq.common.protocol.body.ConsumerConnection)4 ProducerConnection (org.apache.rocketmq.common.protocol.body.ProducerConnection)4 SubscriptionGroupConfig (org.apache.rocketmq.common.subscription.SubscriptionGroupConfig)4 RemotingSendRequestException (org.apache.rocketmq.remoting.exception.RemotingSendRequestException)4 Before (org.junit.Before)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 InetSocketAddress (java.net.InetSocketAddress)2 UnknownHostException (java.net.UnknownHostException)2