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 =
this.brokerController.getConsumerOffsetManager().whichTopicByConsumer(
requestHeader.getConsumerGroup());
for (String topic : topics) {
TopicConfig topicConfig = this.brokerController.getTopicConfigManager().selectTopicConfig(topic);
if (null == topicConfig) {
log.warn("consumeStats, topic config not exist, {}", topic);
continue;
}
/**
* Consumer不在线的时候,也允许查询消费进度
*/
{
SubscriptionData findSubscriptionData =
this.brokerController.getConsumerManager().findSubscriptionData(
requestHeader.getConsumerGroup(), topic);
// 如果Consumer在线,而且这个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.getWriteQueueNums(); 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().getMaxOffsetInQuque(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);
}
long consumeTps =
(long) this.brokerController.getBrokerStatsManager().tpsGroupGetNums(
requestHeader.getConsumerGroup(), topic);
consumeTps += consumeStats.getConsumeTps();
consumeStats.setConsumeTps(consumeTps);
}