Search in sources :

Example 36 with QueueData

use of org.apache.rocketmq.common.protocol.route.QueueData in project rocketmq by apache.

the class MQClientInstance method topicRouteData2TopicSubscribeInfo.

public static Set<MessageQueue> topicRouteData2TopicSubscribeInfo(final String topic, final TopicRouteData route) {
    Set<MessageQueue> mqList = new HashSet<MessageQueue>();
    List<QueueData> qds = route.getQueueDatas();
    for (QueueData qd : qds) {
        if (PermName.isReadable(qd.getPerm())) {
            for (int i = 0; i < qd.getReadQueueNums(); i++) {
                MessageQueue mq = new MessageQueue(topic, qd.getBrokerName(), i);
                mqList.add(mq);
            }
        }
    }
    return mqList;
}
Also used : MessageQueue(org.apache.rocketmq.common.message.MessageQueue) QueueData(org.apache.rocketmq.common.protocol.route.QueueData) HashSet(java.util.HashSet)

Example 37 with QueueData

use of org.apache.rocketmq.common.protocol.route.QueueData in project rocketmq by apache.

the class DefaultMQProducerTest method createTopicRoute.

public static TopicRouteData createTopicRoute() {
    TopicRouteData topicRouteData = new TopicRouteData();
    topicRouteData.setFilterServerTable(new HashMap<String, List<String>>());
    List<BrokerData> brokerDataList = new ArrayList<BrokerData>();
    BrokerData brokerData = new BrokerData();
    brokerData.setBrokerName("BrokerA");
    brokerData.setCluster("DefaultCluster");
    HashMap<Long, String> brokerAddrs = new HashMap<Long, String>();
    brokerAddrs.put(0L, "127.0.0.1:10911");
    brokerData.setBrokerAddrs(brokerAddrs);
    brokerDataList.add(brokerData);
    topicRouteData.setBrokerDatas(brokerDataList);
    List<QueueData> queueDataList = new ArrayList<QueueData>();
    QueueData queueData = new QueueData();
    queueData.setBrokerName("BrokerA");
    queueData.setPerm(6);
    queueData.setReadQueueNums(3);
    queueData.setWriteQueueNums(4);
    queueData.setTopicSynFlag(0);
    queueDataList.add(queueData);
    topicRouteData.setQueueDatas(queueDataList);
    return topicRouteData;
}
Also used : BrokerData(org.apache.rocketmq.common.protocol.route.BrokerData) HashMap(java.util.HashMap) QueueData(org.apache.rocketmq.common.protocol.route.QueueData) ArrayList(java.util.ArrayList) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) ArrayList(java.util.ArrayList) List(java.util.List) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) TopicRouteData(org.apache.rocketmq.common.protocol.route.TopicRouteData)

Example 38 with QueueData

use of org.apache.rocketmq.common.protocol.route.QueueData in project rocketmq by apache.

the class BrokerLiveInfo method getTopicsByCluster.

public byte[] getTopicsByCluster(String cluster) {
    TopicList topicList = new TopicList();
    try {
        try {
            this.lock.readLock().lockInterruptibly();
            Set<String> brokerNameSet = this.clusterAddrTable.get(cluster);
            for (String brokerName : brokerNameSet) {
                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();
                    for (QueueData queueData : queueDatas) {
                        if (brokerName.equals(queueData.getBrokerName())) {
                            topicList.getTopicList().add(topic);
                            break;
                        }
                    }
                }
            }
        } 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 39 with QueueData

use of org.apache.rocketmq.common.protocol.route.QueueData in project rocketmq by apache.

the class BrokerLiveInfo method removeTopicByBrokerName.

private void removeTopicByBrokerName(final String brokerName) {
    Iterator<Entry<String, List<QueueData>>> itMap = this.topicQueueTable.entrySet().iterator();
    while (itMap.hasNext()) {
        Entry<String, List<QueueData>> entry = itMap.next();
        String topic = entry.getKey();
        List<QueueData> queueDataList = entry.getValue();
        Iterator<QueueData> it = queueDataList.iterator();
        while (it.hasNext()) {
            QueueData qd = it.next();
            if (qd.getBrokerName().equals(brokerName)) {
                log.info("removeTopicByBrokerName, remove one broker's topic {} {}", topic, qd);
                it.remove();
            }
        }
        if (queueDataList.isEmpty()) {
            log.info("removeTopicByBrokerName, remove the topic all queue {}", topic);
            itMap.remove();
        }
    }
}
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)

Example 40 with QueueData

use of org.apache.rocketmq.common.protocol.route.QueueData in project rocketmq by apache.

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)

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