/**
* Copyright (C) 2010-2013 Alibaba Group Holding Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.rocketmq.client.impl.consumer;
import org.slf4j.Logger;
import com.alibaba.rocketmq.client.impl.factory.MQClientFactory;
import com.alibaba.rocketmq.client.log.ClientLogger;
import com.alibaba.rocketmq.common.ServiceThread;
/**
* Rebalance服务
*
* <br/>
* chen.si 主要负责对每个consumer group,定期检查 group包含的 活动consumer 是否有变化,以及时更新 consumer处理的分区队列
*
* @author shijia.wxr<vintage.wang@gmail.com>
* @since 2013-7-24
*/
public class RebalanceService extends ServiceThread {
private final Logger log = ClientLogger.getLog();
private final MQClientFactory mqClientFactory;
public RebalanceService(MQClientFactory mqClientFactory) {
this.mqClientFactory = mqClientFactory;
}
private static long WaitInterval = 1000 * 10;
@Override
public void run() {
log.info(this.getServiceName() + " service started");
while (!this.isStoped()) {
this.waitForRunning(WaitInterval);
this.mqClientFactory.doRebalance();
}
log.info(this.getServiceName() + " service end");
}
@Override
public String getServiceName() {
return RebalanceService.class.getSimpleName();
}
}
com.alibaba.rocketmq.client.impl.consumer.RebalanceImpl.doRebalance()
public void doRebalance() {
Map<String, SubscriptionData> subTable = this.getSubscriptionInner();
if (subTable != null) {
/*
* chen.si 针对每个topic,检查当前consumer group中的 活动consumer list是否有变化,如果变化了,需要重新调整 分区
*/
for (final Map.Entry<String, SubscriptionData> entry : subTable.entrySet()) {
final String topic = entry.getKey();
try {
this.rebalanceByTopic(topic);
}
catch (Exception e) {
if (!topic.startsWith(MixAll.RETRY_GROUP_TOPIC_PREFIX)) {
log.warn("rebalanceByTopic Exception", e);
}
}
}
}
this.truncateMessageQueueNotMyTopic();
}
com.alibaba.rocketmq.client.impl.consumer.RebalanceImpl.rebalanceByTopic(String)
private void rebalanceByTopic(final String topic) {
switch (messageModel) {
case BROADCASTING: {
Set<MessageQueue> mqSet = this.topicSubscribeInfoTable.get(topic);
if (mqSet != null) {
boolean changed = this.updateProcessQueueTableInRebalance(topic, mqSet);
if (changed) {
this.messageQueueChanged(topic, mqSet, mqSet);
log.info("messageQueueChanged {} {} {} {}",//
consumerGroup,//
topic,//
mqSet,//
mqSet);
}
}
else {
log.warn("doRebalance, {}, but the topic[{}] not exist.", consumerGroup, topic);
}
break;
}
case CLUSTERING: {
/*
* chen.si topic下的所有的分区队列
*/
Set<MessageQueue> mqSet = this.topicSubscribeInfoTable.get(topic);
/*
* chen.si 一个consumer,会有一个唯一的clientId。这里通过clientId来标识 同一个consumer group下的当前活动的所有consumer。
*/
List<String> cidAll = this.mQClientFactory.findConsumerIdList(topic, consumerGroup);
if (null == mqSet) {
if (!topic.startsWith(MixAll.RETRY_GROUP_TOPIC_PREFIX)) {
log.warn("doRebalance, {}, but the topic[{}] not exist.", consumerGroup, topic);
}
}
if (null == cidAll) {
log.warn("doRebalance, {} {}, get consumer id list failed", consumerGroup, topic);
}
if (mqSet != null && cidAll != null) {
List<MessageQueue> mqAll = new ArrayList<MessageQueue>();
mqAll.addAll(mqSet);
// 排序
Collections.sort(mqAll);
Collections.sort(cidAll);
AllocateMessageQueueStrategy strategy = this.allocateMessageQueueStrategy;
// 执行分配算法
List<MessageQueue> allocateResult = null;
try {
allocateResult = strategy.allocate(this.mQClientFactory.getClientId(), mqAll, cidAll);
}
catch (Throwable e) {
log.error("AllocateMessageQueueStrategy.allocate Exception", e);
}
Set<MessageQueue> allocateResultSet = new HashSet<MessageQueue>();
if (allocateResult != null) {
allocateResultSet.addAll(allocateResult);
}
// 更新本地队列
boolean changed = this.updateProcessQueueTableInRebalance(topic, allocateResultSet);
if (changed) {
log.info("rebalanced result changed. mqSet={}, ConsumerId={}, mqSize={}, cidSize={}",
allocateResult, this.mQClientFactory.getClientId(), mqAll.size(), cidAll.size());
/*
* chen.si 当前consumer负责的分区队列有变化,需要通知consumer。 当前consumer需要重新调整fetch
*/
this.messageQueueChanged(topic, mqSet, allocateResultSet);
log.info("messageQueueChanged {} {} {} {}",//
consumerGroup,//
topic,//
mqSet,//
allocateResultSet);
log.info("messageQueueChanged consumerIdList: {}",//
cidAll);
}
}
break;
}
default:
break;
}
}
/**
* Copyright (C) 2010-2013 Alibaba Group Holding Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.rocketmq.client.consumer.rebalance;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.rocketmq.client.consumer.AllocateMessageQueueStrategy;
import com.alibaba.rocketmq.common.message.MessageQueue;
/**
* 平均分配队列算法
*
* @author fuchong<yubao.fyb@alibaba-inc.com>
* @author manhong.yqd<manhong.yqd@taobao.com>
* @since 2013-7-24
*/
public class AllocateMessageQueueAveragely implements AllocateMessageQueueStrategy {
@Override
public List<MessageQueue> allocate(String currentCID, List<MessageQueue> mqAll, List<String> cidAll) {
/*
* chen.si 参数很重要,这里把接口的方法参数 引用到这里:
* currentCID 当前ConsumerId
* mqAll 当前Topic的所有队列集合,无重复数据,且有序
* cidAll 当前订阅组的所有Consumer集合,无重复数据,且有序
* return 分配结果,无重复数据
*/
if (currentCID == null || currentCID.length() < 1) {
throw new IllegalArgumentException("currentCID is empty");
}
if (mqAll == null || mqAll.size() < 1) {
throw new IllegalArgumentException("mqAll is null or mqAll size < 1");
}
if (cidAll == null || cidAll.size() < 1) {
throw new IllegalArgumentException("cidAll is null or cidAll size < 1");
}
List<MessageQueue> result = new ArrayList<MessageQueue>();
if (!cidAll.contains(currentCID)) { // 不存在此ConsumerId ,直接返回
return result;
}
int index = cidAll.indexOf(currentCID);
int mod = mqAll.size() % cidAll.size();
int averageSize =
mqAll.size() <= cidAll.size() ? 1 : (mod > 0 && index < mod ? mqAll.size() / cidAll.size()
+ 1 : mqAll.size() / cidAll.size());
int startIndex = (mod > 0 && index < mod) ? index * averageSize : index * averageSize + mod;
int range = Math.min(averageSize, mqAll.size() - startIndex);
for (int i = 0; i < range; i++) {
result.add(mqAll.get((startIndex + i) % mqAll.size()));
}
return result;
}
}
com.alibaba.rocketmq.client.impl.consumer.ConsumeMessageConcurrentlyService.start()
public void start() {
}
com.alibaba.rocketmq.client.impl.consumer.ConsumeMessageOrderlyService.start()
public void start() {
// 启动定时lock队列服务
if (MessageModel.CLUSTERING.equals(ConsumeMessageOrderlyService.this.defaultMQPushConsumerImpl
.messageModel())) {
this.scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
ConsumeMessageOrderlyService.this.lockMQPeriodically();
}
}, 1000 * 1, ProcessQueue.RebalanceLockInterval, TimeUnit.MILLISECONDS);
}
}
com.alibaba.rocketmq.client.impl.consumer.RebalanceImpl.lockAll()
public void lockAll() {
HashMap<String, Set<MessageQueue>> brokerMqs = this.buildProcessQueueTableByBrokerName();
Iterator<Entry<String, Set<MessageQueue>>> it = brokerMqs.entrySet().iterator();
while (it.hasNext()) {
Entry<String, Set<MessageQueue>> entry = it.next();
final String brokerName = entry.getKey();
final Set<MessageQueue> mqs = entry.getValue();
if (mqs.isEmpty())
continue;
FindBrokerResult findBrokerResult =
this.mQClientFactory.findBrokerAddressInSubscribe(brokerName, MixAll.MASTER_ID, true);
if (findBrokerResult != null) {
LockBatchRequestBody requestBody = new LockBatchRequestBody();
requestBody.setConsumerGroup(this.consumerGroup);
requestBody.setClientId(this.mQClientFactory.getClientId());
requestBody.setMqSet(mqs);
try {
Set<MessageQueue> lockOKMQSet =
this.mQClientFactory.getMQClientAPIImpl().lockBatchMQ(
findBrokerResult.getBrokerAddr(), requestBody, 1000);
// 锁定成功的队列
for (MessageQueue mq : lockOKMQSet) {
ProcessQueue processQueue = this.processQueueTable.get(mq);
if (processQueue != null) {
if (!processQueue.isLocked()) {
log.info("the message queue locked OK, Group: {} {}", this.consumerGroup, mq);
}
processQueue.setLocked(true);
processQueue.setLastLockTimestamp(System.currentTimeMillis());
}
}
// 锁定失败的队列
for (MessageQueue mq : mqs) {
if (!lockOKMQSet.contains(mq)) {
ProcessQueue processQueue = this.processQueueTable.get(mq);
if (processQueue != null) {
processQueue.setLocked(false);
log.warn("the message queue locked Failed, Group: {} {}", this.consumerGroup,
mq);
}
}
}
}
catch (Exception e) {
log.error("lockBatchMQ exception, " + mqs, e);
}
}
}
}