Search in sources :

Example 26 with BrokerData

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

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();
            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) {
                        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 27 with BrokerData

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

the class ClusterTestRequestProcessorTest method init.

@Before
public void init() throws NoSuchFieldException, IllegalAccessException, RemotingException, MQClientException, InterruptedException {
    NamesrvController namesrvController = new NamesrvController(new NamesrvConfig(), new NettyServerConfig());
    clusterTestProcessor = new ClusterTestRequestProcessor(namesrvController, "default-producer");
    mQClientAPIImpl = mock(MQClientAPIImpl.class);
    DefaultMQAdminExt defaultMQAdminExt = new DefaultMQAdminExt();
    defaultMQAdminExtImpl = new DefaultMQAdminExtImpl(defaultMQAdminExt, 1000);
    ctx = mock(ChannelHandlerContext.class);
    Field field = DefaultMQAdminExtImpl.class.getDeclaredField("mqClientInstance");
    field.setAccessible(true);
    field.set(defaultMQAdminExtImpl, mqClientInstance);
    field = MQClientInstance.class.getDeclaredField("mQClientAPIImpl");
    field.setAccessible(true);
    field.set(mqClientInstance, mQClientAPIImpl);
    field = ClusterTestRequestProcessor.class.getDeclaredField("adminExt");
    field.setAccessible(true);
    field.set(clusterTestProcessor, defaultMQAdminExt);
    TopicRouteData topicRouteData = new TopicRouteData();
    List<BrokerData> brokerDatas = new ArrayList<>();
    HashMap<Long, String> brokerAddrs = new HashMap<>();
    brokerAddrs.put(1234l, "127.0.0.1:10911");
    BrokerData brokerData = new BrokerData();
    brokerData.setCluster("default-cluster");
    brokerData.setBrokerName("default-broker");
    brokerData.setBrokerAddrs(brokerAddrs);
    brokerDatas.add(brokerData);
    topicRouteData.setBrokerDatas(brokerDatas);
    when(mQClientAPIImpl.getTopicRouteInfoFromNameServer(anyString(), anyLong())).thenReturn(topicRouteData);
}
Also used : BrokerData(org.apache.rocketmq.common.protocol.route.BrokerData) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) NamesrvController(org.apache.rocketmq.namesrv.NamesrvController) TopicRouteData(org.apache.rocketmq.common.protocol.route.TopicRouteData) Field(java.lang.reflect.Field) NamesrvConfig(org.apache.rocketmq.common.namesrv.NamesrvConfig) MQClientAPIImpl(org.apache.rocketmq.client.impl.MQClientAPIImpl) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) NettyServerConfig(org.apache.rocketmq.remoting.netty.NettyServerConfig) MQClientInstance(org.apache.rocketmq.client.impl.factory.MQClientInstance) DefaultMQAdminExt(org.apache.rocketmq.tools.admin.DefaultMQAdminExt) DefaultMQAdminExtImpl(org.apache.rocketmq.tools.admin.DefaultMQAdminExtImpl) Before(org.junit.Before)

Example 28 with BrokerData

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

the class MQClientInstance method topicRouteData2TopicPublishInfo.

public static TopicPublishInfo topicRouteData2TopicPublishInfo(final String topic, final TopicRouteData route) {
    TopicPublishInfo info = new TopicPublishInfo();
    info.setTopicRouteData(route);
    if (route.getOrderTopicConf() != null && route.getOrderTopicConf().length() > 0) {
        String[] brokers = route.getOrderTopicConf().split(";");
        for (String broker : brokers) {
            String[] item = broker.split(":");
            int nums = Integer.parseInt(item[1]);
            for (int i = 0; i < nums; i++) {
                MessageQueue mq = new MessageQueue(topic, item[0], i);
                info.getMessageQueueList().add(mq);
            }
        }
        info.setOrderTopic(true);
    } else {
        List<QueueData> qds = route.getQueueDatas();
        Collections.sort(qds);
        for (QueueData qd : qds) {
            if (PermName.isWriteable(qd.getPerm())) {
                BrokerData brokerData = null;
                for (BrokerData bd : route.getBrokerDatas()) {
                    if (bd.getBrokerName().equals(qd.getBrokerName())) {
                        brokerData = bd;
                        break;
                    }
                }
                if (null == brokerData) {
                    continue;
                }
                if (!brokerData.getBrokerAddrs().containsKey(MixAll.MASTER_ID)) {
                    continue;
                }
                for (int i = 0; i < qd.getWriteQueueNums(); i++) {
                    MessageQueue mq = new MessageQueue(topic, qd.getBrokerName(), i);
                    info.getMessageQueueList().add(mq);
                }
            }
        }
        info.setOrderTopic(false);
    }
    return info;
}
Also used : TopicPublishInfo(org.apache.rocketmq.client.impl.producer.TopicPublishInfo) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) BrokerData(org.apache.rocketmq.common.protocol.route.BrokerData) QueueData(org.apache.rocketmq.common.protocol.route.QueueData)

Example 29 with BrokerData

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

the class MQClientInstance method isBrokerInNameServer.

private boolean isBrokerInNameServer(final String brokerAddr) {
    Iterator<Entry<String, TopicRouteData>> it = this.topicRouteTable.entrySet().iterator();
    while (it.hasNext()) {
        Entry<String, TopicRouteData> itNext = it.next();
        List<BrokerData> brokerDatas = itNext.getValue().getBrokerDatas();
        for (BrokerData bd : brokerDatas) {
            boolean contain = bd.getBrokerAddrs().containsValue(brokerAddr);
            if (contain)
                return true;
        }
    }
    return false;
}
Also used : Entry(java.util.Map.Entry) BrokerData(org.apache.rocketmq.common.protocol.route.BrokerData) TopicRouteData(org.apache.rocketmq.common.protocol.route.TopicRouteData)

Example 30 with BrokerData

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

the class MQClientInstance method updateTopicRouteInfoFromNameServer.

public boolean updateTopicRouteInfoFromNameServer(final String topic, boolean isDefault, DefaultMQProducer defaultMQProducer) {
    try {
        if (this.lockNamesrv.tryLock(LOCK_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)) {
            try {
                TopicRouteData topicRouteData;
                if (isDefault && defaultMQProducer != null) {
                    topicRouteData = this.mQClientAPIImpl.getDefaultTopicRouteInfoFromNameServer(defaultMQProducer.getCreateTopicKey(), 1000 * 3);
                    if (topicRouteData != null) {
                        for (QueueData data : topicRouteData.getQueueDatas()) {
                            int queueNums = Math.min(defaultMQProducer.getDefaultTopicQueueNums(), data.getReadQueueNums());
                            data.setReadQueueNums(queueNums);
                            data.setWriteQueueNums(queueNums);
                        }
                    }
                } else {
                    topicRouteData = this.mQClientAPIImpl.getTopicRouteInfoFromNameServer(topic, 1000 * 3);
                }
                if (topicRouteData != null) {
                    TopicRouteData old = this.topicRouteTable.get(topic);
                    boolean changed = topicRouteDataIsChange(old, topicRouteData);
                    if (!changed) {
                        changed = this.isNeedUpdateTopicRouteInfo(topic);
                    } else {
                        log.info("the topic[{}] route info changed, old[{}] ,new[{}]", topic, old, topicRouteData);
                    }
                    if (changed) {
                        TopicRouteData cloneTopicRouteData = topicRouteData.cloneTopicRouteData();
                        for (BrokerData bd : topicRouteData.getBrokerDatas()) {
                            this.brokerAddrTable.put(bd.getBrokerName(), bd.getBrokerAddrs());
                        }
                        // Update Pub info
                        {
                            TopicPublishInfo publishInfo = topicRouteData2TopicPublishInfo(topic, topicRouteData);
                            publishInfo.setHaveTopicRouterInfo(true);
                            Iterator<Entry<String, MQProducerInner>> it = this.producerTable.entrySet().iterator();
                            while (it.hasNext()) {
                                Entry<String, MQProducerInner> entry = it.next();
                                MQProducerInner impl = entry.getValue();
                                if (impl != null) {
                                    impl.updateTopicPublishInfo(topic, publishInfo);
                                }
                            }
                        }
                        // Update sub info
                        {
                            Set<MessageQueue> subscribeInfo = topicRouteData2TopicSubscribeInfo(topic, topicRouteData);
                            Iterator<Entry<String, MQConsumerInner>> it = this.consumerTable.entrySet().iterator();
                            while (it.hasNext()) {
                                Entry<String, MQConsumerInner> entry = it.next();
                                MQConsumerInner impl = entry.getValue();
                                if (impl != null) {
                                    impl.updateTopicSubscribeInfo(topic, subscribeInfo);
                                }
                            }
                        }
                        log.info("topicRouteTable.put. Topic = {}, TopicRouteData[{}]", topic, cloneTopicRouteData);
                        this.topicRouteTable.put(topic, cloneTopicRouteData);
                        return true;
                    }
                } else {
                    log.warn("updateTopicRouteInfoFromNameServer, getTopicRouteInfoFromNameServer return null, Topic: {}", topic);
                }
            } catch (Exception e) {
                if (!topic.startsWith(MixAll.RETRY_GROUP_TOPIC_PREFIX) && !topic.equals(MixAll.DEFAULT_TOPIC)) {
                    log.warn("updateTopicRouteInfoFromNameServer Exception", e);
                }
            } finally {
                this.lockNamesrv.unlock();
            }
        } else {
            log.warn("updateTopicRouteInfoFromNameServer tryLock timeout {}ms", LOCK_TIMEOUT_MILLIS);
        }
    } catch (InterruptedException e) {
        log.warn("updateTopicRouteInfoFromNameServer Exception", e);
    }
    return false;
}
Also used : MQConsumerInner(org.apache.rocketmq.client.impl.consumer.MQConsumerInner) Set(java.util.Set) HashSet(java.util.HashSet) MQProducerInner(org.apache.rocketmq.client.impl.producer.MQProducerInner) BrokerData(org.apache.rocketmq.common.protocol.route.BrokerData) QueueData(org.apache.rocketmq.common.protocol.route.QueueData) MQClientException(org.apache.rocketmq.client.exception.MQClientException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) TopicRouteData(org.apache.rocketmq.common.protocol.route.TopicRouteData) Entry(java.util.Map.Entry) TopicPublishInfo(org.apache.rocketmq.client.impl.producer.TopicPublishInfo) Iterator(java.util.Iterator)

Aggregations

BrokerData (org.apache.rocketmq.common.protocol.route.BrokerData)106 TopicRouteData (org.apache.rocketmq.common.protocol.route.TopicRouteData)65 HashMap (java.util.HashMap)41 ArrayList (java.util.ArrayList)29 QueueData (org.apache.rocketmq.common.protocol.route.QueueData)29 HashSet (java.util.HashSet)27 ClusterInfo (org.apache.rocketmq.common.protocol.body.ClusterInfo)23 Map (java.util.Map)22 MQClientException (org.apache.rocketmq.client.exception.MQClientException)21 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)21 List (java.util.List)20 Set (java.util.Set)20 Field (java.lang.reflect.Field)18 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)18 ConsumeStats (org.apache.rocketmq.common.admin.ConsumeStats)16 ArgumentMatchers.anyLong (org.mockito.ArgumentMatchers.anyLong)16 Entry (java.util.Map.Entry)14 MQBrokerException (org.apache.rocketmq.client.exception.MQBrokerException)14 MQClientAPIImpl (org.apache.rocketmq.client.impl.MQClientAPIImpl)14 MQClientInstance (org.apache.rocketmq.client.impl.factory.MQClientInstance)14