void RebalanceImpl::truncateMessageQueueNotMyTopic() { std::map<std::string, SubscriptionData> subTable = getSubscriptionInner(); std::map<MessageQueue, ProcessQueue*>::iterator it = m_processQueueTable.begin(); for ( ; it != m_processQueueTable.end(); it++) { MessageQueue mq = it->first; std::map<std::string, SubscriptionData>::iterator itt = subTable.find(mq.getTopic()); if (itt == subTable.end()) { ProcessQueue* pq = it->second; if (pq != NULL) { pq->setDroped(true); //TODO log.info("doRebalance, {}, truncateMessageQueueNotMyTopic remove unnecessary mq, {}", // consumerGroup, mq); } std::map<MessageQueue, ProcessQueue*>::iterator ittmp = it; it++; m_processQueueTable.erase(ittmp); } else { it++; } } }
bool RebalanceImpl::updateProcessQueueTableInRebalance(const std::string& topic, std::set<MessageQueue>& mqSet) { bool changed = false; // 将多余的队列删除 std::map<MessageQueue, ProcessQueue*>::iterator it = m_processQueueTable.begin(); for ( ; it != m_processQueueTable.end();) { MessageQueue mq = it->first; if (mq.getTopic() == topic) { std::set<MessageQueue>::iterator its = mqSet.find(mq); if (its == mqSet.end()) { changed = true; ProcessQueue* pq = it->second; if (pq != NULL) { pq->setDroped(true); removeUnnecessaryMessageQueue(mq, *pq); //TODO log.info("doRebalance, {}, remove unnecessary mq, {}", // consumerGroup, mq); } std::map<MessageQueue, ProcessQueue*>::iterator ittmp = it; it++; m_processQueueTable.erase(ittmp); } else { it++; } } else { it++; } } // 增加新增的队列 std::list<PullRequest> pullRequestList; std::set<MessageQueue>::iterator its = mqSet.begin(); for (; its != mqSet.end(); its++) { MessageQueue mq = *its; std::map<MessageQueue, ProcessQueue*>::iterator itm = m_processQueueTable.find(mq); if (itm == m_processQueueTable.end()) { PullRequest pullRequest; pullRequest.setConsumerGroup(m_consumerGroup); pullRequest.setMessageQueue(new MessageQueue(mq.getTopic(),mq.getBrokerName(),mq.getQueueId())); pullRequest.setProcessQueue(new ProcessQueue()); // 这个需要根据策略来设置 long long nextOffset = computePullFromWhere(mq); if (nextOffset >= 0) { pullRequest.setNextOffset(nextOffset); pullRequestList.push_back(pullRequest); changed = true; m_processQueueTable[mq] = pullRequest.getProcessQueue(); //TODO log.info("doRebalance, {}, add a new mq, {}", consumerGroup, mq); } else { // 等待此次Rebalance做重试 //TODO log.warn("doRebalance, {}, add new mq failed, {}", consumerGroup, mq); } } } dispatchPullRequest(pullRequestList); return changed; }