Search in sources :

Example 16 with FindBrokerResult

use of org.apache.rocketmq.client.impl.FindBrokerResult in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class RebalanceImpl method 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);
            }
        }
    }
}
Also used : Entry(java.util.Map.Entry) Set(java.util.Set) HashSet(java.util.HashSet) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) LockBatchRequestBody(org.apache.rocketmq.common.protocol.body.LockBatchRequestBody) FindBrokerResult(org.apache.rocketmq.client.impl.FindBrokerResult)

Example 17 with FindBrokerResult

use of org.apache.rocketmq.client.impl.FindBrokerResult in project rocketmq by apache.

the class RebalanceImpl method unlock.

public void unlock(final MessageQueue mq, final boolean oneway) {
    FindBrokerResult findBrokerResult = this.mQClientFactory.findBrokerAddressInSubscribe(mq.getBrokerName(), MixAll.MASTER_ID, true);
    if (findBrokerResult != null) {
        UnlockBatchRequestBody requestBody = new UnlockBatchRequestBody();
        requestBody.setConsumerGroup(this.consumerGroup);
        requestBody.setClientId(this.mQClientFactory.getClientId());
        requestBody.getMqSet().add(mq);
        try {
            this.mQClientFactory.getMQClientAPIImpl().unlockBatchMQ(findBrokerResult.getBrokerAddr(), requestBody, 1000, oneway);
            log.warn("unlock messageQueue. group:{}, clientId:{}, mq:{}", this.consumerGroup, this.mQClientFactory.getClientId(), mq);
        } catch (Exception e) {
            log.error("unlockBatchMQ exception, " + mq, e);
        }
    }
}
Also used : FindBrokerResult(org.apache.rocketmq.client.impl.FindBrokerResult) UnlockBatchRequestBody(org.apache.rocketmq.common.protocol.body.UnlockBatchRequestBody)

Example 18 with FindBrokerResult

use of org.apache.rocketmq.client.impl.FindBrokerResult in project rocketmq by apache.

the class RebalanceImpl method 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);
            }
        }
    }
}
Also used : Entry(java.util.Map.Entry) Set(java.util.Set) HashSet(java.util.HashSet) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) LockBatchRequestBody(org.apache.rocketmq.common.protocol.body.LockBatchRequestBody) FindBrokerResult(org.apache.rocketmq.client.impl.FindBrokerResult)

Example 19 with FindBrokerResult

use of org.apache.rocketmq.client.impl.FindBrokerResult in project rocketmq by apache.

the class RebalanceImpl method lock.

public boolean lock(final MessageQueue mq) {
    FindBrokerResult findBrokerResult = this.mQClientFactory.findBrokerAddressInSubscribe(mq.getBrokerName(), MixAll.MASTER_ID, true);
    if (findBrokerResult != null) {
        LockBatchRequestBody requestBody = new LockBatchRequestBody();
        requestBody.setConsumerGroup(this.consumerGroup);
        requestBody.setClientId(this.mQClientFactory.getClientId());
        requestBody.getMqSet().add(mq);
        try {
            Set<MessageQueue> lockedMq = this.mQClientFactory.getMQClientAPIImpl().lockBatchMQ(findBrokerResult.getBrokerAddr(), requestBody, 1000);
            for (MessageQueue mmqq : lockedMq) {
                ProcessQueue processQueue = this.processQueueTable.get(mmqq);
                if (processQueue != null) {
                    processQueue.setLocked(true);
                    processQueue.setLastLockTimestamp(System.currentTimeMillis());
                }
            }
            boolean lockOK = lockedMq.contains(mq);
            log.info("the message queue lock {}, {} {}", lockOK ? "OK" : "Failed", this.consumerGroup, mq);
            return lockOK;
        } catch (Exception e) {
            log.error("lockBatchMQ exception, " + mq, e);
        }
    }
    return false;
}
Also used : MessageQueue(org.apache.rocketmq.common.message.MessageQueue) LockBatchRequestBody(org.apache.rocketmq.common.protocol.body.LockBatchRequestBody) FindBrokerResult(org.apache.rocketmq.client.impl.FindBrokerResult)

Example 20 with FindBrokerResult

use of org.apache.rocketmq.client.impl.FindBrokerResult in project rocketmq by apache.

the class MQClientInstance method findBrokerAddressInAdmin.

public FindBrokerResult findBrokerAddressInAdmin(final String brokerName) {
    String brokerAddr = null;
    boolean slave = false;
    boolean found = false;
    HashMap<Long, String> /* address */
    map = this.brokerAddrTable.get(brokerName);
    if (map != null && !map.isEmpty()) {
        for (Map.Entry<Long, String> entry : map.entrySet()) {
            Long id = entry.getKey();
            brokerAddr = entry.getValue();
            if (brokerAddr != null) {
                found = true;
                if (MixAll.MASTER_ID == id) {
                    slave = false;
                } else {
                    slave = true;
                }
                break;
            }
        }
    // end of for
    }
    if (found) {
        return new FindBrokerResult(brokerAddr, slave, findBrokerVersion(brokerName, brokerAddr));
    }
    return null;
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) FindBrokerResult(org.apache.rocketmq.client.impl.FindBrokerResult)

Aggregations

FindBrokerResult (org.apache.rocketmq.client.impl.FindBrokerResult)21 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)8 HashSet (java.util.HashSet)6 MQClientException (org.apache.rocketmq.client.exception.MQClientException)6 Before (org.junit.Before)6 Field (java.lang.reflect.Field)4 Set (java.util.Set)4 PullAPIWrapper (org.apache.rocketmq.client.impl.consumer.PullAPIWrapper)4 MQClientInstance (org.apache.rocketmq.client.impl.factory.MQClientInstance)4 LockBatchRequestBody (org.apache.rocketmq.common.protocol.body.LockBatchRequestBody)4 UnlockBatchRequestBody (org.apache.rocketmq.common.protocol.body.UnlockBatchRequestBody)4 PullMessageRequestHeader (org.apache.rocketmq.common.protocol.header.PullMessageRequestHeader)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 ConcurrentMap (java.util.concurrent.ConcurrentMap)3 InetSocketAddress (java.net.InetSocketAddress)2 Entry (java.util.Map.Entry)2 ClientConfig (org.apache.rocketmq.client.ClientConfig)2 PullResult (org.apache.rocketmq.client.consumer.PullResult)2