Search in sources :

Example 86 with BrokerData

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

the class BrokerLiveInfo method getSystemTopicList.

public byte[] getSystemTopicList() {
    TopicList topicList = new TopicList();
    try {
        try {
            this.lock.readLock().lockInterruptibly();
            for (Map.Entry<String, Set<String>> entry : clusterAddrTable.entrySet()) {
                topicList.getTopicList().add(entry.getKey());
                topicList.getTopicList().addAll(entry.getValue());
            }
            if (brokerAddrTable != null && !brokerAddrTable.isEmpty()) {
                Iterator<String> it = brokerAddrTable.keySet().iterator();
                while (it.hasNext()) {
                    BrokerData bd = brokerAddrTable.get(it.next());
                    HashMap<Long, String> brokerAddrs = bd.getBrokerAddrs();
                    if (brokerAddrs != null && !brokerAddrs.isEmpty()) {
                        Iterator<Long> it2 = brokerAddrs.keySet().iterator();
                        topicList.setBrokerAddr(brokerAddrs.get(it2.next()));
                        break;
                    }
                }
            }
        } finally {
            this.lock.readLock().unlock();
        }
    } catch (Exception e) {
        log.error("getAllTopicList Exception", e);
    }
    return topicList.encode();
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) BrokerData(org.apache.rocketmq.common.protocol.route.BrokerData) TopicList(org.apache.rocketmq.common.protocol.body.TopicList) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map)

Example 87 with BrokerData

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

the class BrokerLiveInfo method registerBroker.

public RegisterBrokerResult registerBroker(final String clusterName, final String brokerAddr, final String brokerName, final long brokerId, final String haServerAddr, final TopicConfigSerializeWrapper topicConfigWrapper, final List<String> filterServerList, final Channel channel) {
    RegisterBrokerResult result = new RegisterBrokerResult();
    try {
        try {
            this.lock.writeLock().lockInterruptibly();
            Set<String> brokerNames = this.clusterAddrTable.get(clusterName);
            if (null == brokerNames) {
                brokerNames = new HashSet<String>();
                this.clusterAddrTable.put(clusterName, brokerNames);
            }
            brokerNames.add(brokerName);
            boolean registerFirst = false;
            BrokerData brokerData = this.brokerAddrTable.get(brokerName);
            if (null == brokerData) {
                registerFirst = true;
                brokerData = new BrokerData(clusterName, brokerName, new HashMap<Long, String>());
                this.brokerAddrTable.put(brokerName, brokerData);
            }
            String oldAddr = brokerData.getBrokerAddrs().put(brokerId, brokerAddr);
            registerFirst = registerFirst || (null == oldAddr);
            if (null != topicConfigWrapper && MixAll.MASTER_ID == brokerId) {
                if (this.isBrokerTopicConfigChanged(brokerAddr, topicConfigWrapper.getDataVersion()) || registerFirst) {
                    ConcurrentMap<String, TopicConfig> tcTable = topicConfigWrapper.getTopicConfigTable();
                    if (tcTable != null) {
                        for (Map.Entry<String, TopicConfig> entry : tcTable.entrySet()) {
                            this.createAndUpdateQueueData(brokerName, entry.getValue());
                        }
                    }
                }
            }
            BrokerLiveInfo prevBrokerLiveInfo = this.brokerLiveTable.put(brokerAddr, new BrokerLiveInfo(System.currentTimeMillis(), topicConfigWrapper.getDataVersion(), channel, haServerAddr));
            if (null == prevBrokerLiveInfo) {
                log.info("new broker registered, {} HAServer: {}", brokerAddr, haServerAddr);
            }
            if (filterServerList != null) {
                if (filterServerList.isEmpty()) {
                    this.filterServerTable.remove(brokerAddr);
                } else {
                    this.filterServerTable.put(brokerAddr, filterServerList);
                }
            }
            if (MixAll.MASTER_ID != brokerId) {
                String masterAddr = brokerData.getBrokerAddrs().get(MixAll.MASTER_ID);
                if (masterAddr != null) {
                    BrokerLiveInfo brokerLiveInfo = this.brokerLiveTable.get(masterAddr);
                    if (brokerLiveInfo != null) {
                        result.setHaServerAddr(brokerLiveInfo.getHaServerAddr());
                        result.setMasterAddr(masterAddr);
                    }
                }
            }
        } finally {
            this.lock.writeLock().unlock();
        }
    } catch (Exception e) {
        log.error("registerBroker Exception", e);
    }
    return result;
}
Also used : BrokerData(org.apache.rocketmq.common.protocol.route.BrokerData) HashMap(java.util.HashMap) RegisterBrokerResult(org.apache.rocketmq.common.namesrv.RegisterBrokerResult) TopicConfig(org.apache.rocketmq.common.TopicConfig) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map)

Example 88 with BrokerData

use of org.apache.rocketmq.common.protocol.route.BrokerData 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)

Example 89 with BrokerData

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

the class DefaultRequestProcessorTest method testProcessRequest_RegisterBrokerWithFilterServer.

@Test
public void testProcessRequest_RegisterBrokerWithFilterServer() throws RemotingCommandException, NoSuchFieldException, IllegalAccessException {
    RemotingCommand request = genSampleRegisterCmd(true);
    // version >= MQVersion.Version.V3_0_11.ordinal() to register with filter server
    request.setVersion(100);
    ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
    when(ctx.channel()).thenReturn(null);
    RemotingCommand response = defaultRequestProcessor.processRequest(ctx, request);
    assertThat(response.getCode()).isEqualTo(ResponseCode.SUCCESS);
    assertThat(response.getRemark()).isNull();
    RouteInfoManager routes = namesrvController.getRouteInfoManager();
    Field brokerAddrTable = RouteInfoManager.class.getDeclaredField("brokerAddrTable");
    brokerAddrTable.setAccessible(true);
    BrokerData broker = new BrokerData();
    broker.setBrokerName("broker");
    broker.setBrokerAddrs((HashMap) Maps.newHashMap(new Long(2333), "10.10.1.1"));
    assertThat((Map) brokerAddrTable.get(routes)).contains(new HashMap.SimpleEntry("broker", broker));
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) Field(java.lang.reflect.Field) BrokerData(org.apache.rocketmq.common.protocol.route.BrokerData) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) RouteInfoManager(org.apache.rocketmq.namesrv.routeinfo.RouteInfoManager) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.Test)

Example 90 with BrokerData

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

the class DefaultRequestProcessorTest method testProcessRequest_RegisterBroker.

@Test
public void testProcessRequest_RegisterBroker() throws RemotingCommandException, NoSuchFieldException, IllegalAccessException {
    RemotingCommand request = genSampleRegisterCmd(true);
    ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
    when(ctx.channel()).thenReturn(null);
    RemotingCommand response = defaultRequestProcessor.processRequest(ctx, request);
    assertThat(response.getCode()).isEqualTo(ResponseCode.SUCCESS);
    assertThat(response.getRemark()).isNull();
    RouteInfoManager routes = namesrvController.getRouteInfoManager();
    Field brokerAddrTable = RouteInfoManager.class.getDeclaredField("brokerAddrTable");
    brokerAddrTable.setAccessible(true);
    BrokerData broker = new BrokerData();
    broker.setBrokerName("broker");
    broker.setBrokerAddrs((HashMap) Maps.newHashMap(new Long(2333), "10.10.1.1"));
    assertThat((Map) brokerAddrTable.get(routes)).contains(new HashMap.SimpleEntry("broker", broker));
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) Field(java.lang.reflect.Field) BrokerData(org.apache.rocketmq.common.protocol.route.BrokerData) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) RouteInfoManager(org.apache.rocketmq.namesrv.routeinfo.RouteInfoManager) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.Test)

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