通过源码追溯,我们发现Store包里的DefaultMessageStore是Consumer的构建入口(DefaultMessageStore做了很多工作并不仅限于构建ConsumerQueue)。DefaultMessageStore提供ConcurrentMap(private final ConcurrentMap<String/* topic */, ConcurrentMap<Integer/* queueId */, ConsumeQueue>> consumeQueueTable;)来对Consumerqueue进行管理,使用ReputMessageService作为构建ConsumerQueue的触发器,最终通过调用ConsumerQueue.putMessagePositionInfoWrapper进行ConsumerQueue的构建。
1.ReputMessageService继承ServiceThread是一个线程服务,服务启动后每间隔1毫秒调用一次doReput方法,doReput方法会调用CommitLogDispatcher进行消息分发。
class ReputMessageService extends ServiceThread {
/**省略非关键代码**/
private void doReput() {
for (boolean doNext = true; this.isCommitLogAvailable() && doNext; ) {
if (DefaultMessageStore.this.getMessageStoreConfig().isDuplicationEnable()
&& this.reputFromOffset >= DefaultMessageStore.this.getConfirmOffset()) {
break;
}
SelectMappedBufferResult result = DefaultMessageStore.this.commitLog.getData(reputFromOffset);
if (result != null) {
try {
this.reputFromOffset = result.getStartOffset();
for (int readSize = 0; readSize < result.getSize() && doNext; ) {
DispatchRequest dispatchRequest =
DefaultMessageStore.this.commitLog.checkMessageAndReturnSize(result.getByteBuffer(), false, false);
int size = dispatchRequest.getMsgSize();
if (dispatchRequest.isSuccess()) {
if (size > 0) {
/**DefaultMessageStore在初始化时会维护一个dispatcherList,默认加载两个类;
*CommitLogDispatcherBuildConsumeQueue用于构建ConsumerQueue
*CommitLogDispatcherBuildIndex用于构建Index索引
**/
DefaultMessageStore.this.doDispatch(dispatchRequest);
if (BrokerRole.SLAVE != DefaultMessageStore.this.getMessageStoreConfig().getBrokerRole()

本文深入探讨RocketMQ中ConsumerQueue的构建流程,从ReputMessageService触发到CommitLogDispatcher分发,再到DefaultMessageStore的putMessagePositionInfo方法,最后在ConsumeQueue中完成构建。解析了关键代码路径,揭示ConsumerQueue的构建细节。
最低0.47元/天 解锁文章
1508





