bool RebalanceImpl::lock(MessageQueue& mq)
{
    FindBrokerResult findBrokerResult =
        m_pMQClientFactory->findBrokerAddressInSubscribe(mq.getBrokerName(), MixAll::MASTER_ID, true);
    if (!findBrokerResult.brokerAddr.empty())
    {
        LockBatchRequestBody* requestBody = new LockBatchRequestBody();
        requestBody->setConsumerGroup(m_consumerGroup);
        requestBody->setClientId(m_pMQClientFactory->getClientId());
        requestBody->getMqSet().insert(mq);

        try
        {
            std::set<MessageQueue> lockedMq =
                m_pMQClientFactory->getMQClientAPIImpl()->lockBatchMQ(
                    findBrokerResult.brokerAddr, requestBody, 1000);

            std::set<MessageQueue>::iterator it = lockedMq.begin();
            for (; it != lockedMq.end(); it++)
            {
                kpr::ScopedLock<kpr::Mutex> lock(m_processQueueTableLock);
                MessageQueue mmqq = *it;
                std::map<MessageQueue, ProcessQueue*>::iterator itt = m_processQueueTable.find(mmqq);
                if (itt != m_processQueueTable.end())
                {
                    itt->second->setLocked(true);
                    itt->second->setLastLockTimestamp(GetCurrentTimeMillis());
                }
            }

            it = lockedMq.find(mq);
            if (it != lockedMq.end())
            {
                return true;
            }

            //TODO log.info("the message queue lock {}, {} {}",//
            //	(lockOK ? "OK" : "Failed"), //
            //	this.consumerGroup, //
            //	mq);
            return false;
        }
        catch (...)
        {
            //TODO log.error("lockBatchMQ exception, " + mq, e);
        }
    }

    return false;
}
void RebalanceImpl::lockAll()
{
	std::map<std::string, std::set<MessageQueue> > brokerMqs = buildProcessQueueTableByBrokerName();

	std::map<std::string, std::set<MessageQueue> >::iterator it = brokerMqs.begin();
	for (;it != brokerMqs.end();it++)
	{
		std::string brokerName = it->first;
		std::set<MessageQueue> mqs = it->second;

		if (mqs.empty())
		{
			continue;
		}

		FindBrokerResult findBrokerResult =
			m_pMQClientFactory->findBrokerAddressInSubscribe(brokerName, MixAll::MASTER_ID, true);
		if (!findBrokerResult.brokerAddr.empty())
		{
			LockBatchRequestBody* requestBody = new LockBatchRequestBody();
			requestBody->setConsumerGroup(m_consumerGroup);
			requestBody->setClientId(m_pMQClientFactory->getClientId());
			requestBody->setMqSet(mqs);

			try
			{
				std::set<MessageQueue> lockOKMQSet =
					m_pMQClientFactory->getMQClientAPIImpl()->lockBatchMQ(
					findBrokerResult.brokerAddr, requestBody, 1000);

				// 锁定成功的队列
				std::set<MessageQueue>::iterator its = lockOKMQSet.begin();
				for (;its != lockOKMQSet.end();its++)
				{
					MessageQueue mq = *its;
					std::map<MessageQueue, ProcessQueue*>::iterator itt = m_processQueueTable.find(mq);
					if (itt != m_processQueueTable.end())
					{
						ProcessQueue* processQueue = itt->second;
						if (!processQueue->isLocked())
						{
							//TODO log.info("the message queue locked OK, Group: {} {}", this.consumerGroup, mq);
						}

						processQueue->setLocked(true);
						processQueue->setLastLockTimestamp(GetCurrentTimeMillis());
					}
				}

				// 锁定失败的队列
				its = mqs.begin();
				for (;its != mqs.end();its++)
				{
					MessageQueue mq = *its;
					std::set<MessageQueue>::iterator itf = lockOKMQSet.find(mq);
					if (itf == lockOKMQSet.end())
					{
						std::map<MessageQueue, ProcessQueue*>::iterator itt = m_processQueueTable.find(mq);
						if (itt != m_processQueueTable.end())
						{
							itt->second->setLocked(false);
							//TODO log.warn("the message queue locked Failed, Group: {} {}", this.consumerGroup,
							//	mq);
						}
					}
				}
			}
			catch (...)
			{
				//TODO log.error("lockBatchMQ exception, " + mqs, e);
			}
		}
	}
}