Search in sources :

Example 11 with TopicRouteData

use of org.apache.rocketmq.common.protocol.route.TopicRouteData 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 12 with TopicRouteData

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

the class RouteInfoManagerTest method testPickupTopicRouteData.

@Test
public void testPickupTopicRouteData() {
    TopicRouteData result = routeInfoManager.pickupTopicRouteData("unit_test");
    assertThat(result).isNull();
}
Also used : TopicRouteData(org.apache.rocketmq.common.protocol.route.TopicRouteData) Test(org.junit.Test)

Example 13 with TopicRouteData

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

the class ClusterTestRequestProcessor method getRouteInfoByTopic.

@Override
public RemotingCommand getRouteInfoByTopic(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(null);
    final GetRouteInfoRequestHeader requestHeader = (GetRouteInfoRequestHeader) request.decodeCommandCustomHeader(GetRouteInfoRequestHeader.class);
    TopicRouteData topicRouteData = this.namesrvController.getRouteInfoManager().pickupTopicRouteData(requestHeader.getTopic());
    if (topicRouteData != null) {
        String orderTopicConf = this.namesrvController.getKvConfigManager().getKVConfig(NamesrvUtil.NAMESPACE_ORDER_TOPIC_CONFIG, requestHeader.getTopic());
        topicRouteData.setOrderTopicConf(orderTopicConf);
    } else {
        try {
            topicRouteData = adminExt.examineTopicRouteInfo(requestHeader.getTopic());
        } catch (Exception e) {
            log.info("get route info by topic from product environment failed. envName={},", productEnvName);
        }
    }
    if (topicRouteData != null) {
        byte[] content = topicRouteData.encode();
        response.setBody(content);
        response.setCode(ResponseCode.SUCCESS);
        response.setRemark(null);
        return response;
    }
    response.setCode(ResponseCode.TOPIC_NOT_EXIST);
    response.setRemark("No topic route info in name server for the topic: " + requestHeader.getTopic() + FAQUrl.suggestTodo(FAQUrl.APPLY_TOPIC_URL));
    return response;
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) GetRouteInfoRequestHeader(org.apache.rocketmq.common.protocol.header.namesrv.GetRouteInfoRequestHeader) RemotingCommandException(org.apache.rocketmq.remoting.exception.RemotingCommandException) MQClientException(org.apache.rocketmq.client.exception.MQClientException) TopicRouteData(org.apache.rocketmq.common.protocol.route.TopicRouteData)

Example 14 with TopicRouteData

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

the class PullAPIWrapper method computPullFromWhichFilterServer.

private String computPullFromWhichFilterServer(final String topic, final String brokerAddr) throws MQClientException {
    ConcurrentMap<String, TopicRouteData> topicRouteTable = this.mQClientFactory.getTopicRouteTable();
    if (topicRouteTable != null) {
        TopicRouteData topicRouteData = topicRouteTable.get(topic);
        List<String> list = topicRouteData.getFilterServerTable().get(brokerAddr);
        if (list != null && !list.isEmpty()) {
            return list.get(randomNum() % list.size());
        }
    }
    throw new MQClientException("Find Filter Server Failed, Broker Addr: " + brokerAddr + " topic: " + topic, null);
}
Also used : MQClientException(org.apache.rocketmq.client.exception.MQClientException) TopicRouteData(org.apache.rocketmq.common.protocol.route.TopicRouteData)

Example 15 with TopicRouteData

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

the class MQAdminImpl method createTopic.

public void createTopic(String key, String newTopic, int queueNum, int topicSysFlag) throws MQClientException {
    try {
        TopicRouteData topicRouteData = this.mQClientFactory.getMQClientAPIImpl().getTopicRouteInfoFromNameServer(key, timeoutMillis);
        List<BrokerData> brokerDataList = topicRouteData.getBrokerDatas();
        if (brokerDataList != null && !brokerDataList.isEmpty()) {
            Collections.sort(brokerDataList);
            boolean createOKAtLeastOnce = false;
            MQClientException exception = null;
            StringBuilder orderTopicString = new StringBuilder();
            for (BrokerData brokerData : brokerDataList) {
                String addr = brokerData.getBrokerAddrs().get(MixAll.MASTER_ID);
                if (addr != null) {
                    TopicConfig topicConfig = new TopicConfig(newTopic);
                    topicConfig.setReadQueueNums(queueNum);
                    topicConfig.setWriteQueueNums(queueNum);
                    topicConfig.setTopicSysFlag(topicSysFlag);
                    boolean createOK = false;
                    for (int i = 0; i < 5; i++) {
                        try {
                            this.mQClientFactory.getMQClientAPIImpl().createTopic(addr, key, topicConfig, timeoutMillis);
                            createOK = true;
                            createOKAtLeastOnce = true;
                            break;
                        } catch (Exception e) {
                            if (4 == i) {
                                exception = new MQClientException("create topic to broker exception", e);
                            }
                        }
                    }
                    if (createOK) {
                        orderTopicString.append(brokerData.getBrokerName());
                        orderTopicString.append(":");
                        orderTopicString.append(queueNum);
                        orderTopicString.append(";");
                    }
                }
            }
            if (exception != null && !createOKAtLeastOnce) {
                throw exception;
            }
        } else {
            throw new MQClientException("Not found broker, maybe key is wrong", null);
        }
    } catch (Exception e) {
        throw new MQClientException("create new topic failed", e);
    }
}
Also used : BrokerData(org.apache.rocketmq.common.protocol.route.BrokerData) TopicConfig(org.apache.rocketmq.common.TopicConfig) MQClientException(org.apache.rocketmq.client.exception.MQClientException) RemotingCommandException(org.apache.rocketmq.remoting.exception.RemotingCommandException) MQClientException(org.apache.rocketmq.client.exception.MQClientException) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException) TopicRouteData(org.apache.rocketmq.common.protocol.route.TopicRouteData)

Aggregations

TopicRouteData (org.apache.rocketmq.common.protocol.route.TopicRouteData)92 BrokerData (org.apache.rocketmq.common.protocol.route.BrokerData)65 ArrayList (java.util.ArrayList)25 MQClientException (org.apache.rocketmq.client.exception.MQClientException)25 QueueData (org.apache.rocketmq.common.protocol.route.QueueData)25 HashMap (java.util.HashMap)22 DefaultMQAdminExt (org.apache.rocketmq.tools.admin.DefaultMQAdminExt)20 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)18 List (java.util.List)16 HashSet (java.util.HashSet)13 Entry (java.util.Map.Entry)12 ConsumeStats (org.apache.rocketmq.common.admin.ConsumeStats)12 ArgumentMatchers.anyLong (org.mockito.ArgumentMatchers.anyLong)12 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)12 RemotingException (org.apache.rocketmq.remoting.exception.RemotingException)11 Field (java.lang.reflect.Field)10 MQBrokerException (org.apache.rocketmq.client.exception.MQBrokerException)10 MQClientAPIImpl (org.apache.rocketmq.client.impl.MQClientAPIImpl)10 MQClientInstance (org.apache.rocketmq.client.impl.factory.MQClientInstance)10 DefaultMQAdminExtImpl (org.apache.rocketmq.tools.admin.DefaultMQAdminExtImpl)10