throws RemotingCommandException {
final RemotingCommand response =
RemotingCommand.createResponseCommand(QueryConsumerOffsetResponseHeader.class);
final QueryConsumerOffsetResponseHeader responseHeader =
(QueryConsumerOffsetResponseHeader) response.getCustomHeader();
final QueryConsumerOffsetRequestHeader requestHeader =
(QueryConsumerOffsetRequestHeader) request
.decodeCommandCustomHeader(QueryConsumerOffsetRequestHeader.class);
long offset =
this.brokerController.getConsumerOffsetManager().queryOffset(
requestHeader.getConsumerGroup(), requestHeader.getTopic(), requestHeader.getQueueId());
// 订阅组存在
if (offset >= 0) {
responseHeader.setOffset(offset);
response.setCode(ResponseCode.SUCCESS);
response.setRemark(null);
}
// 订阅组不存在
else {
// 新版本服务器不做消费进度纠正
if (request.getVersion() >= MQVersion.Version.V3_0_6_SNAPSHOT.ordinal()) {
response.setCode(ResponseCode.QUERY_NOT_FOUND);
response.setRemark("Not found, V3_0_6_SNAPSHOT maybe this group consumer boot first");
}
// TODO:以下流程待所有客户端都升级后,可以删除此段代码
else {
long minOffset =
this.brokerController.getMessageStore().getMinOffsetInQuque(requestHeader.getTopic(),
requestHeader.getQueueId());
long maxOffset =
this.brokerController.getMessageStore().getMaxOffsetInQuque(requestHeader.getTopic(),
requestHeader.getQueueId());
boolean consumeFromMinEnable = false;
if (0 == minOffset && maxOffset > 0) {
long minCommitLogOffset =
this.brokerController.getMessageStore().getCommitLogOffsetInQueue(
requestHeader.getTopic(), requestHeader.getQueueId(), minOffset);
long maxCommitLogOffset =
this.brokerController.getMessageStore().getCommitLogOffsetInQueue(
requestHeader.getTopic(), requestHeader.getQueueId(), maxOffset - 1);
long memorySpan =
(long) (StoreUtil.TotalPhysicalMemorySize * (this.brokerController
.getMessageStoreConfig().getAccessMessageInMemoryMaxRatio() / 100.0));
long diff = maxCommitLogOffset - minCommitLogOffset;
if (diff < memorySpan) {
consumeFromMinEnable = true;
log.info(
"the consumer group[{}] first subscribed, minOffset: {} maxOffset: {}, from min.",//
requestHeader.getConsumerGroup(),//
minOffset,//
maxOffset);
}
}
else if (minOffset > 0 && maxOffset > 0) {
consumeFromMinEnable = false;
}
else {
consumeFromMinEnable = true;
log.info(
"the consumer group[{}] first subscribed, minOffset: {} maxOffset: {}, from min, and unknow offset.",//
requestHeader.getConsumerGroup(),//
minOffset,//
maxOffset);
}
// 说明这个队列在服务器存储的消息比较少或者没有消息