Search in sources :

Example 6 with QueueData

use of org.apache.rocketmq.common.protocol.route.QueueData in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class BrokerLiveInfo method getHasUnitSubUnUnitTopicList.

public byte[] getHasUnitSubUnUnitTopicList() {
    TopicList topicList = new TopicList();
    try {
        try {
            this.lock.readLock().lockInterruptibly();
            Iterator<Entry<String, List<QueueData>>> topicTableIt = this.topicQueueTable.entrySet().iterator();
            while (topicTableIt.hasNext()) {
                Entry<String, List<QueueData>> topicEntry = topicTableIt.next();
                String topic = topicEntry.getKey();
                List<QueueData> queueDatas = topicEntry.getValue();
                if (queueDatas != null && queueDatas.size() > 0 && !TopicSysFlag.hasUnitFlag(queueDatas.get(0).getTopicSynFlag()) && TopicSysFlag.hasUnitSubFlag(queueDatas.get(0).getTopicSynFlag())) {
                    topicList.getTopicList().add(topic);
                }
            }
        } finally {
            this.lock.readLock().unlock();
        }
    } catch (Exception e) {
        log.error("getAllTopicList Exception", e);
    }
    return topicList.encode();
}
Also used : Entry(java.util.Map.Entry) QueueData(org.apache.rocketmq.common.protocol.route.QueueData) TopicList(org.apache.rocketmq.common.protocol.body.TopicList) TopicList(org.apache.rocketmq.common.protocol.body.TopicList) LinkedList(java.util.LinkedList) List(java.util.List)

Example 7 with QueueData

use of org.apache.rocketmq.common.protocol.route.QueueData in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class BrokerLiveInfo method getUnitTopics.

public byte[] getUnitTopics() {
    TopicList topicList = new TopicList();
    try {
        try {
            this.lock.readLock().lockInterruptibly();
            Iterator<Entry<String, List<QueueData>>> topicTableIt = this.topicQueueTable.entrySet().iterator();
            while (topicTableIt.hasNext()) {
                Entry<String, List<QueueData>> topicEntry = topicTableIt.next();
                String topic = topicEntry.getKey();
                List<QueueData> queueDatas = topicEntry.getValue();
                if (queueDatas != null && queueDatas.size() > 0 && TopicSysFlag.hasUnitFlag(queueDatas.get(0).getTopicSynFlag())) {
                    topicList.getTopicList().add(topic);
                }
            }
        } finally {
            this.lock.readLock().unlock();
        }
    } catch (Exception e) {
        log.error("getAllTopicList Exception", e);
    }
    return topicList.encode();
}
Also used : Entry(java.util.Map.Entry) QueueData(org.apache.rocketmq.common.protocol.route.QueueData) TopicList(org.apache.rocketmq.common.protocol.body.TopicList) TopicList(org.apache.rocketmq.common.protocol.body.TopicList) LinkedList(java.util.LinkedList) List(java.util.List)

Example 8 with QueueData

use of org.apache.rocketmq.common.protocol.route.QueueData in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class BrokerLiveInfo method onChannelDestroy.

public void onChannelDestroy(String remoteAddr, Channel channel) {
    String brokerAddrFound = null;
    if (channel != null) {
        try {
            try {
                this.lock.readLock().lockInterruptibly();
                Iterator<Entry<String, BrokerLiveInfo>> itBrokerLiveTable = this.brokerLiveTable.entrySet().iterator();
                while (itBrokerLiveTable.hasNext()) {
                    Entry<String, BrokerLiveInfo> entry = itBrokerLiveTable.next();
                    if (entry.getValue().getChannel() == channel) {
                        brokerAddrFound = entry.getKey();
                        break;
                    }
                }
            } finally {
                this.lock.readLock().unlock();
            }
        } catch (Exception e) {
            log.error("onChannelDestroy Exception", e);
        }
    }
    if (null == brokerAddrFound) {
        brokerAddrFound = remoteAddr;
    } else {
        log.info("the broker's channel destroyed, {}, clean it's data structure at once", brokerAddrFound);
    }
    if (brokerAddrFound != null && brokerAddrFound.length() > 0) {
        try {
            try {
                this.lock.writeLock().lockInterruptibly();
                this.brokerLiveTable.remove(brokerAddrFound);
                this.filterServerTable.remove(brokerAddrFound);
                String brokerNameFound = null;
                boolean removeBrokerName = false;
                Iterator<Entry<String, BrokerData>> itBrokerAddrTable = this.brokerAddrTable.entrySet().iterator();
                while (itBrokerAddrTable.hasNext() && (null == brokerNameFound)) {
                    BrokerData brokerData = itBrokerAddrTable.next().getValue();
                    Iterator<Entry<Long, String>> it = brokerData.getBrokerAddrs().entrySet().iterator();
                    while (it.hasNext()) {
                        Entry<Long, String> entry = it.next();
                        Long brokerId = entry.getKey();
                        String brokerAddr = entry.getValue();
                        if (brokerAddr.equals(brokerAddrFound)) {
                            brokerNameFound = brokerData.getBrokerName();
                            it.remove();
                            log.info("remove brokerAddr[{}, {}] from brokerAddrTable, because channel destroyed", brokerId, brokerAddr);
                            break;
                        }
                    }
                    if (brokerData.getBrokerAddrs().isEmpty()) {
                        removeBrokerName = true;
                        itBrokerAddrTable.remove();
                        log.info("remove brokerName[{}] from brokerAddrTable, because channel destroyed", brokerData.getBrokerName());
                    }
                }
                if (brokerNameFound != null && removeBrokerName) {
                    Iterator<Entry<String, Set<String>>> it = this.clusterAddrTable.entrySet().iterator();
                    while (it.hasNext()) {
                        Entry<String, Set<String>> entry = it.next();
                        String clusterName = entry.getKey();
                        Set<String> brokerNames = entry.getValue();
                        boolean removed = brokerNames.remove(brokerNameFound);
                        if (removed) {
                            log.info("remove brokerName[{}], clusterName[{}] from clusterAddrTable, because channel destroyed", brokerNameFound, clusterName);
                            if (brokerNames.isEmpty()) {
                                log.info("remove the clusterName[{}] from clusterAddrTable, because channel destroyed and no broker in this cluster", clusterName);
                                it.remove();
                            }
                            break;
                        }
                    }
                }
                if (removeBrokerName) {
                    Iterator<Entry<String, List<QueueData>>> itTopicQueueTable = this.topicQueueTable.entrySet().iterator();
                    while (itTopicQueueTable.hasNext()) {
                        Entry<String, List<QueueData>> entry = itTopicQueueTable.next();
                        String topic = entry.getKey();
                        List<QueueData> queueDataList = entry.getValue();
                        Iterator<QueueData> itQueueData = queueDataList.iterator();
                        while (itQueueData.hasNext()) {
                            QueueData queueData = itQueueData.next();
                            if (queueData.getBrokerName().equals(brokerNameFound)) {
                                itQueueData.remove();
                                log.info("remove topic[{} {}], from topicQueueTable, because channel destroyed", topic, queueData);
                            }
                        }
                        if (queueDataList.isEmpty()) {
                            itTopicQueueTable.remove();
                            log.info("remove topic[{}] all queue, from topicQueueTable, because channel destroyed", topic);
                        }
                    }
                }
            } finally {
                this.lock.writeLock().unlock();
            }
        } catch (Exception e) {
            log.error("onChannelDestroy Exception", e);
        }
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) BrokerData(org.apache.rocketmq.common.protocol.route.BrokerData) QueueData(org.apache.rocketmq.common.protocol.route.QueueData) Entry(java.util.Map.Entry) TopicList(org.apache.rocketmq.common.protocol.body.TopicList) LinkedList(java.util.LinkedList) List(java.util.List)

Example 9 with QueueData

use of org.apache.rocketmq.common.protocol.route.QueueData in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class BrokerLiveInfo method pickupTopicRouteData.

public TopicRouteData pickupTopicRouteData(final String topic) {
    TopicRouteData topicRouteData = new TopicRouteData();
    boolean foundQueueData = false;
    boolean foundBrokerData = false;
    Set<String> brokerNameSet = new HashSet<String>();
    List<BrokerData> brokerDataList = new LinkedList<BrokerData>();
    topicRouteData.setBrokerDatas(brokerDataList);
    HashMap<String, List<String>> filterServerMap = new HashMap<String, List<String>>();
    topicRouteData.setFilterServerTable(filterServerMap);
    try {
        try {
            this.lock.readLock().lockInterruptibly();
            // 根据topic获取QueueData信息
            List<QueueData> queueDataList = this.topicQueueTable.get(topic);
            if (queueDataList != null) {
                topicRouteData.setQueueDatas(queueDataList);
                foundQueueData = true;
                Iterator<QueueData> it = queueDataList.iterator();
                while (it.hasNext()) {
                    QueueData qd = it.next();
                    brokerNameSet.add(qd.getBrokerName());
                }
                for (String brokerName : brokerNameSet) {
                    BrokerData brokerData = this.brokerAddrTable.get(brokerName);
                    if (null != brokerData) {
                        // 根据broker名称获取其地址信息
                        BrokerData brokerDataClone = new BrokerData(brokerData.getCluster(), brokerData.getBrokerName(), (HashMap<Long, String>) brokerData.getBrokerAddrs().clone());
                        brokerDataList.add(brokerDataClone);
                        foundBrokerData = true;
                        for (final String brokerAddr : brokerDataClone.getBrokerAddrs().values()) {
                            List<String> filterServerList = this.filterServerTable.get(brokerAddr);
                            filterServerMap.put(brokerAddr, filterServerList);
                        }
                    }
                }
            }
        } finally {
            this.lock.readLock().unlock();
        }
    } catch (Exception e) {
        log.error("pickupTopicRouteData Exception", e);
    }
    if (log.isDebugEnabled()) {
        log.debug("pickupTopicRouteData {} {}", topic, topicRouteData);
    }
    if (foundBrokerData && foundQueueData) {
        return topicRouteData;
    }
    return null;
}
Also used : BrokerData(org.apache.rocketmq.common.protocol.route.BrokerData) HashMap(java.util.HashMap) QueueData(org.apache.rocketmq.common.protocol.route.QueueData) LinkedList(java.util.LinkedList) TopicRouteData(org.apache.rocketmq.common.protocol.route.TopicRouteData) TopicList(org.apache.rocketmq.common.protocol.body.TopicList) LinkedList(java.util.LinkedList) List(java.util.List) HashSet(java.util.HashSet)

Example 10 with QueueData

use of org.apache.rocketmq.common.protocol.route.QueueData in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class BrokerLiveInfo method wipeWritePermOfBroker.

private int wipeWritePermOfBroker(final String brokerName) {
    int wipeTopicCnt = 0;
    Iterator<Entry<String, List<QueueData>>> itTopic = this.topicQueueTable.entrySet().iterator();
    while (itTopic.hasNext()) {
        Entry<String, List<QueueData>> entry = itTopic.next();
        List<QueueData> qdList = entry.getValue();
        Iterator<QueueData> it = qdList.iterator();
        while (it.hasNext()) {
            QueueData qd = it.next();
            if (qd.getBrokerName().equals(brokerName)) {
                int perm = qd.getPerm();
                perm &= ~PermName.PERM_WRITE;
                qd.setPerm(perm);
                wipeTopicCnt++;
            }
        }
    }
    return wipeTopicCnt;
}
Also used : Entry(java.util.Map.Entry) QueueData(org.apache.rocketmq.common.protocol.route.QueueData) TopicList(org.apache.rocketmq.common.protocol.body.TopicList) LinkedList(java.util.LinkedList) List(java.util.List)

Aggregations

QueueData (org.apache.rocketmq.common.protocol.route.QueueData)42 List (java.util.List)30 BrokerData (org.apache.rocketmq.common.protocol.route.BrokerData)24 TopicList (org.apache.rocketmq.common.protocol.body.TopicList)20 TopicRouteData (org.apache.rocketmq.common.protocol.route.TopicRouteData)20 HashMap (java.util.HashMap)18 ArrayList (java.util.ArrayList)16 LinkedList (java.util.LinkedList)16 Entry (java.util.Map.Entry)16 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)16 HashSet (java.util.HashSet)14 ArgumentMatchers.anyLong (org.mockito.ArgumentMatchers.anyLong)12 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)12 Field (java.lang.reflect.Field)10 MQClientAPIImpl (org.apache.rocketmq.client.impl.MQClientAPIImpl)10 MQClientInstance (org.apache.rocketmq.client.impl.factory.MQClientInstance)10 DefaultMQAdminExt (org.apache.rocketmq.tools.admin.DefaultMQAdminExt)10 BeforeClass (org.junit.BeforeClass)10 Set (java.util.Set)8 ConsumeStats (org.apache.rocketmq.common.admin.ConsumeStats)8