Java Code Examples for org.apache.rocketmq.common.protocol.route.BrokerData#getBrokerAddrs()

The following examples show how to use org.apache.rocketmq.common.protocol.route.BrokerData#getBrokerAddrs() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MQClientInstance.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
private boolean isBrokerAddrExistInTopicRouteTable(final String addr) {
    Iterator<Entry<String, TopicRouteData>> it = this.topicRouteTable.entrySet().iterator();
    while (it.hasNext()) {
        Entry<String, TopicRouteData> entry = it.next();
        TopicRouteData topicRouteData = entry.getValue();
        List<BrokerData> bds = topicRouteData.getBrokerDatas();
        for (BrokerData bd : bds) {
            if (bd.getBrokerAddrs() != null) {
                boolean exist = bd.getBrokerAddrs().containsValue(addr);
                if (exist)
                    return true;
            }
        }
    }

    return false;
}
 
Example 2
Source File: MQClientInstance.java    From rocketmq-4.3.0 with Apache License 2.0 6 votes vote down vote up
private boolean isBrokerAddrExistInTopicRouteTable(final String addr) {
//        遍历topic路由
        Iterator<Entry<String, TopicRouteData>> it = this.topicRouteTable.entrySet().iterator();
        while (it.hasNext()) {
            Entry<String, TopicRouteData> entry = it.next();
            TopicRouteData topicRouteData = entry.getValue();
            List<BrokerData> bds = topicRouteData.getBrokerDatas();
            for (BrokerData bd : bds) {
                if (bd.getBrokerAddrs() != null) {
                    boolean exist = bd.getBrokerAddrs().containsValue(addr);
                    if (exist)
                        return true;
                }
            }
        }

        return false;
    }
 
Example 3
Source File: MQClientInstance.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
private boolean isBrokerAddrExistInTopicRouteTable(final String addr) {
    Iterator<Entry<String, TopicRouteData>> it = this.topicRouteTable.entrySet().iterator();
    while (it.hasNext()) {
        Entry<String, TopicRouteData> entry = it.next();
        TopicRouteData topicRouteData = entry.getValue();
        List<BrokerData> bds = topicRouteData.getBrokerDatas();
        for (BrokerData bd : bds) {
            if (bd.getBrokerAddrs() != null) {
                boolean exist = bd.getBrokerAddrs().containsValue(addr);
                if (exist)
                    return true;
            }
        }
    }

    return false;
}
 
Example 4
Source File: MQClientInstance.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
private boolean isBrokerAddrExistInTopicRouteTable(final String addr) {
    Iterator<Entry<String, TopicRouteData>> it = this.topicRouteTable.entrySet().iterator();
    while (it.hasNext()) {
        Entry<String, TopicRouteData> entry = it.next();
        TopicRouteData topicRouteData = entry.getValue();
        List<BrokerData> bds = topicRouteData.getBrokerDatas();
        for (BrokerData bd : bds) {
            if (bd.getBrokerAddrs() != null) {
                boolean exist = bd.getBrokerAddrs().containsValue(addr);
                if (exist)
                    return true;
            }
        }
    }

    return false;
}
 
Example 5
Source File: RouteInfoManager.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 6
Source File: RouteInfoManager.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 7
Source File: RouteInfoManager.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
private boolean isBrokerIdExist(final String clusterName, final String brokerAddr, final String brokerName,
    final long brokerId) {
    boolean brokerIdExist = false;
    BrokerData brokerData = brokerAddrTable.get(brokerName);
    if (brokerData != null && brokerData.getCluster().equals(clusterName) && brokerData.getBrokerAddrs() != null) {
        if (StringUtils.isNotEmpty(brokerData.getBrokerAddrs().get(brokerId)) &&
            !brokerAddr.equals(brokerData.getBrokerAddrs().get(brokerId))) {
            brokerIdExist = true;
            log.error("brokers:{}, {} have the same id:{} , cluster name:{}, broker name:{}", brokerAddr, brokerData.getBrokerAddrs().get(brokerId),
                brokerId, clusterName, brokerName);
        }
    }
    return brokerIdExist;
}
 
Example 8
Source File: CommandUtil.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public static Map<String/*master addr*/, List<String>/*slave addr*/> fetchMasterAndSlaveDistinguish(
    final MQAdminExt adminExt, final String clusterName)
    throws InterruptedException, RemotingConnectException,
    RemotingTimeoutException, RemotingSendRequestException,
    MQBrokerException {
    Map<String, List<String>> masterAndSlaveMap = new HashMap<String, List<String>>(4);

    ClusterInfo clusterInfoSerializeWrapper = adminExt.examineBrokerClusterInfo();
    Set<String> brokerNameSet = clusterInfoSerializeWrapper.getClusterAddrTable().get(clusterName);

    if (brokerNameSet == null) {
        System.out.printf("[error] %s", ERROR_MESSAGE);
        return masterAndSlaveMap;
    }

    for (String brokerName : brokerNameSet) {
        BrokerData brokerData = clusterInfoSerializeWrapper.getBrokerAddrTable().get(brokerName);

        if (brokerData == null || brokerData.getBrokerAddrs() == null) {
            continue;
        }

        String masterAddr = brokerData.getBrokerAddrs().get(MixAll.MASTER_ID);
        masterAndSlaveMap.put(masterAddr, new ArrayList<String>());

        for (Long id : brokerData.getBrokerAddrs().keySet()) {
            if (brokerData.getBrokerAddrs().get(id) == null || id == MixAll.MASTER_ID) {
                continue;
            }

            masterAndSlaveMap.get(masterAddr).add(brokerData.getBrokerAddrs().get(id));
        }
    }

    return masterAndSlaveMap;
}
 
Example 9
Source File: CommandUtil.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public static Map<String/*master addr*/, List<String>/*slave addr*/> fetchMasterAndSlaveDistinguish(
    final MQAdminExt adminExt, final String clusterName)
    throws InterruptedException, RemotingConnectException,
    RemotingTimeoutException, RemotingSendRequestException,
    MQBrokerException {
    Map<String, List<String>> masterAndSlaveMap = new HashMap<String, List<String>>(4);

    ClusterInfo clusterInfoSerializeWrapper = adminExt.examineBrokerClusterInfo();
    Set<String> brokerNameSet = clusterInfoSerializeWrapper.getClusterAddrTable().get(clusterName);

    if (brokerNameSet == null) {
        System.out
            .printf("[error] Make sure the specified clusterName exists or the nameserver which connected is correct.");
        return masterAndSlaveMap;
    }

    for (String brokerName : brokerNameSet) {
        BrokerData brokerData = clusterInfoSerializeWrapper.getBrokerAddrTable().get(brokerName);

        if (brokerData == null || brokerData.getBrokerAddrs() == null) {
            continue;
        }

        String masterAddr = brokerData.getBrokerAddrs().get(MixAll.MASTER_ID);
        masterAndSlaveMap.put(masterAddr, new ArrayList<String>());

        for (Long id : brokerData.getBrokerAddrs().keySet()) {
            if (brokerData.getBrokerAddrs().get(id) == null
                || id.longValue() == MixAll.MASTER_ID) {
                continue;
            }

            masterAndSlaveMap.get(masterAddr).add(brokerData.getBrokerAddrs().get(id));
        }
    }

    return masterAndSlaveMap;
}
 
Example 10
Source File: CommandUtil.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
public static Map<String/*master addr*/, List<String>/*slave addr*/> fetchMasterAndSlaveDistinguish(
    final MQAdminExt adminExt, final String clusterName)
    throws InterruptedException, RemotingConnectException,
    RemotingTimeoutException, RemotingSendRequestException,
    MQBrokerException {
    Map<String, List<String>> masterAndSlaveMap = new HashMap<String, List<String>>(4);

    ClusterInfo clusterInfoSerializeWrapper = adminExt.examineBrokerClusterInfo();
    Set<String> brokerNameSet = clusterInfoSerializeWrapper.getClusterAddrTable().get(clusterName);

    if (brokerNameSet == null) {
        System.out
            .printf("[error] Make sure the specified clusterName exists or the nameserver which connected is correct.");
        return masterAndSlaveMap;
    }

    for (String brokerName : brokerNameSet) {
        BrokerData brokerData = clusterInfoSerializeWrapper.getBrokerAddrTable().get(brokerName);

        if (brokerData == null || brokerData.getBrokerAddrs() == null) {
            continue;
        }

        String masterAddr = brokerData.getBrokerAddrs().get(MixAll.MASTER_ID);
        masterAndSlaveMap.put(masterAddr, new ArrayList<String>());

        for (Long id : brokerData.getBrokerAddrs().keySet()) {
            if (brokerData.getBrokerAddrs().get(id) == null
                || id.longValue() == MixAll.MASTER_ID) {
                continue;
            }

            masterAndSlaveMap.get(masterAddr).add(brokerData.getBrokerAddrs().get(id));
        }
    }

    return masterAndSlaveMap;
}
 
Example 11
Source File: CommandUtil.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
public static Map<String/*master addr*/, List<String>/*slave addr*/> fetchMasterAndSlaveDistinguish(
    final MQAdminExt adminExt, final String clusterName)
    throws InterruptedException, RemotingConnectException,
    RemotingTimeoutException, RemotingSendRequestException,
    MQBrokerException {
    Map<String, List<String>> masterAndSlaveMap = new HashMap<String, List<String>>(4);

    ClusterInfo clusterInfoSerializeWrapper = adminExt.examineBrokerClusterInfo();
    Set<String> brokerNameSet = clusterInfoSerializeWrapper.getClusterAddrTable().get(clusterName);

    if (brokerNameSet == null) {
        System.out
            .printf("[error] Make sure the specified clusterName exists or the nameserver which connected is correct.");
        return masterAndSlaveMap;
    }

    for (String brokerName : brokerNameSet) {
        BrokerData brokerData = clusterInfoSerializeWrapper.getBrokerAddrTable().get(brokerName);

        if (brokerData == null || brokerData.getBrokerAddrs() == null) {
            continue;
        }

        String masterAddr = brokerData.getBrokerAddrs().get(MixAll.MASTER_ID);
        masterAndSlaveMap.put(masterAddr, new ArrayList<String>());

        for (Long id : brokerData.getBrokerAddrs().keySet()) {
            if (brokerData.getBrokerAddrs().get(id) == null
                || id.longValue() == MixAll.MASTER_ID) {
                continue;
            }

            masterAndSlaveMap.get(masterAddr).add(brokerData.getBrokerAddrs().get(id));
        }
    }

    return masterAndSlaveMap;
}
 
Example 12
Source File: CommandUtil.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
public static Map<String/*master addr*/, List<String>/*slave addr*/> fetchMasterAndSlaveDistinguish(
    final MQAdminExt adminExt, final String clusterName)
    throws InterruptedException, RemotingConnectException,
    RemotingTimeoutException, RemotingSendRequestException,
    MQBrokerException {
    Map<String, List<String>> masterAndSlaveMap = new HashMap<String, List<String>>(4);

    ClusterInfo clusterInfoSerializeWrapper = adminExt.examineBrokerClusterInfo();
    Set<String> brokerNameSet = clusterInfoSerializeWrapper.getClusterAddrTable().get(clusterName);

    if (brokerNameSet == null) {
        System.out.printf("[error] %s", ERROR_MESSAGE);
        return masterAndSlaveMap;
    }

    for (String brokerName : brokerNameSet) {
        BrokerData brokerData = clusterInfoSerializeWrapper.getBrokerAddrTable().get(brokerName);

        if (brokerData == null || brokerData.getBrokerAddrs() == null) {
            continue;
        }

        String masterAddr = brokerData.getBrokerAddrs().get(MixAll.MASTER_ID);
        masterAndSlaveMap.put(masterAddr, new ArrayList<String>());

        for (Long id : brokerData.getBrokerAddrs().keySet()) {
            if (brokerData.getBrokerAddrs().get(id) == null || id == MixAll.MASTER_ID) {
                continue;
            }

            masterAndSlaveMap.get(masterAddr).add(brokerData.getBrokerAddrs().get(id));
        }
    }

    return masterAndSlaveMap;
}
 
Example 13
Source File: CommandUtil.java    From rocketmq_trans_message with Apache License 2.0 5 votes vote down vote up
public static Map<String/*master addr*/, List<String>/*slave addr*/> fetchMasterAndSlaveDistinguish(
    final MQAdminExt adminExt, final String clusterName)
    throws InterruptedException, RemotingConnectException,
    RemotingTimeoutException, RemotingSendRequestException,
    MQBrokerException {
    Map<String, List<String>> masterAndSlaveMap = new HashMap<String, List<String>>(4);

    ClusterInfo clusterInfoSerializeWrapper = adminExt.examineBrokerClusterInfo();
    Set<String> brokerNameSet = clusterInfoSerializeWrapper.getClusterAddrTable().get(clusterName);

    if (brokerNameSet == null) {
        System.out
            .printf("[error] Make sure the specified clusterName exists or the nameserver which connected is correct.");
        return masterAndSlaveMap;
    }

    for (String brokerName : brokerNameSet) {
        BrokerData brokerData = clusterInfoSerializeWrapper.getBrokerAddrTable().get(brokerName);

        if (brokerData == null || brokerData.getBrokerAddrs() == null) {
            continue;
        }

        String masterAddr = brokerData.getBrokerAddrs().get(MixAll.MASTER_ID);
        masterAndSlaveMap.put(masterAddr, new ArrayList<String>());

        for (Long id : brokerData.getBrokerAddrs().keySet()) {
            if (brokerData.getBrokerAddrs().get(id) == null
                || id.longValue() == MixAll.MASTER_ID) {
                continue;
            }

            masterAndSlaveMap.get(masterAddr).add(brokerData.getBrokerAddrs().get(id));
        }
    }

    return masterAndSlaveMap;
}
 
Example 14
Source File: RouteInfoManager.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
public byte[] getSystemTopicList() {
        TopicList topicList = new TopicList();
        try {
            try {
                this.lock.readLock().lockInterruptibly();
                for (Map.Entry<String, Set<String>> entry : clusterAddrTable.entrySet()) {
//                    这里有点疑问,broker集群的clusterName、brokerName添加到topic列表中是什么意思,系统topic就是这些名字吗
                    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();
//                            从broker数据中获取broker地址
                            topicList.setBrokerAddr(brokerAddrs.get(it2.next()));
                            break;
                        }
                    }
                }
            } finally {
                this.lock.readLock().unlock();
            }
        } catch (Exception e) {
            log.error("getAllTopicList Exception", e);
        }

        return topicList.encode();
    }
 
Example 15
Source File: CommandUtil.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
public static Map<String/*master addr*/, List<String>/*slave addr*/> fetchMasterAndSlaveDistinguish(
    final MQAdminExt adminExt, final String clusterName)
    throws InterruptedException, RemotingConnectException,
    RemotingTimeoutException, RemotingSendRequestException,
    MQBrokerException {
    Map<String, List<String>> masterAndSlaveMap = new HashMap<String, List<String>>(4);

    ClusterInfo clusterInfoSerializeWrapper = adminExt.examineBrokerClusterInfo();
    Set<String> brokerNameSet = clusterInfoSerializeWrapper.getClusterAddrTable().get(clusterName);

    if (brokerNameSet == null) {
        System.out
            .printf("[error] Make sure the specified clusterName exists or the nameserver which connected is correct.");
        return masterAndSlaveMap;
    }

    for (String brokerName : brokerNameSet) {
        BrokerData brokerData = clusterInfoSerializeWrapper.getBrokerAddrTable().get(brokerName);

        if (brokerData == null || brokerData.getBrokerAddrs() == null) {
            continue;
        }

        String masterAddr = brokerData.getBrokerAddrs().get(MixAll.MASTER_ID);
        masterAndSlaveMap.put(masterAddr, new ArrayList<String>());

        for (Long id : brokerData.getBrokerAddrs().keySet()) {
            if (brokerData.getBrokerAddrs().get(id) == null
                || id.longValue() == MixAll.MASTER_ID) {
                continue;
            }

            masterAndSlaveMap.get(masterAddr).add(brokerData.getBrokerAddrs().get(id));
        }
    }

    return masterAndSlaveMap;
}
 
Example 16
Source File: RouteInfoManager.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 17
Source File: RouteInfoManager.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
private boolean isBrokerIdExist(final String clusterName, final String brokerAddr, final String brokerName,
    final long brokerId) {
    boolean brokerIdExist = false;
    BrokerData brokerData = brokerAddrTable.get(brokerName);
    if (brokerData != null && brokerData.getCluster().equals(clusterName) && brokerData.getBrokerAddrs() != null) {
        if (StringUtils.isNotEmpty(brokerData.getBrokerAddrs().get(brokerId)) &&
            !brokerAddr.equals(brokerData.getBrokerAddrs().get(brokerId))) {
            brokerIdExist = true;
            log.error("brokers:{}, {} have the same id:{} , cluster name:{}, broker name:{}", brokerAddr, brokerData.getBrokerAddrs().get(brokerId),
                brokerId, clusterName, brokerName);
        }
    }
    return brokerIdExist;
}
 
Example 18
Source File: BrokerController.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
private boolean checkNoMaster() {
    int i = 0;
    boolean isNoMaster = true;
    for (; i < this.messageStoreConfig.getWaitCountInRoleChange(); i++) {
        Map<String, ClusterInfo> clusterInfos = this.brokerOuterAPI.getBrokerClusterInfoOfAllNS();
        isNoMaster = true;
        for (ClusterInfo clusterInfo : clusterInfos.values()) {
            if (clusterInfo == null) {
                log.warn("get cluster info failed, wait and retry");
                isNoMaster = false;
                break;
            }
            BrokerData brokerData = clusterInfo.getBrokerAddrTable().get(this.brokerConfig.getBrokerName());
            if (brokerData != null && brokerData.getBrokerAddrs() != null) {
                if (brokerData.getBrokerAddrs().containsKey(MixAll.MASTER_ID)) {
                    isNoMaster = false;
                    break;
                }
            }
        }

        if (!isNoMaster) {
            try {
                log.info("still another master, wait a while, try the {} times", i + 1);
                Thread.sleep(this.messageStoreConfig.getWaitIntervalInRoleChange());
            } catch (Exception ex) {
                log.error("sleep failed");
            }
        } else {
            break;
        }
    }

    if (i > this.messageStoreConfig.getWaitCountInRoleChange() || !isNoMaster) {
        log.error("there is still a master in at least one name server");
        return false;
    }

    return true;
}
 
Example 19
Source File: HAManager.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
public RoleChangeInfo selectNewMaster(String cluster, String brokerName) {
    BrokerData brokerData = namesrvController.getRouteInfoManager().getBrokerData(brokerName);
    if (brokerData == null || !cluster.equals(brokerData.getCluster())) {
        log.warn("no broker data for broker name:{}, broker data:{}", brokerName, brokerData);
        return null;
    }

    HashMap<Long, String> brokerAddrs = new HashMap<>(brokerData.getBrokerAddrs());
    for (Iterator<Map.Entry<Long, String>> it = brokerAddrs.entrySet().iterator(); it.hasNext(); ) {
        Map.Entry<Long, String> item = it.next();
        if (item.getKey() > namesrvController.getNamesrvConfig().getMaxIdForRoleSwitch()) {
            it.remove();
        }
    }

    //no broker
    if (brokerAddrs == null || brokerAddrs.isEmpty()) {
        log.warn("no broker addrs, for broker name:{}, broker data:{}", brokerName, brokerData);
        return null;
    }

    //only one, and master
    if (brokerAddrs.size() == 1 && brokerAddrs.get(MixAll.MASTER_ID) != null) {
        log.warn("only on broker, but it is current master");
        return null;
    }

    //slave exist
    RoleChangeInfo roleChangeInfo = new RoleChangeInfo();
    SortedSet<Long> ids = new TreeSet<>(brokerAddrs.keySet());
    if (ids.first() == MixAll.MASTER_ID) {
        roleChangeInfo.oldMaster = new RoleInChange(brokerAddrs.get(ids.first()), ids.first(), ids.last() + 1);
    }

    long newMasterId = pickMaster(brokerAddrs);
    if (newMasterId == -1) {
        //newMasterId = ids.last();
        log.error("do not get master, broker name:{}", brokerName);
        return null;
    }
    roleChangeInfo.newMaster = new RoleInChange(brokerAddrs.get(newMasterId), newMasterId, MixAll.MASTER_ID);

    return roleChangeInfo;
}
 
Example 20
Source File: HAManager.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
public RoleChangeInfo selectNewMaster(String cluster, String brokerName) {
    BrokerData brokerData = namesrvController.getRouteInfoManager().getBrokerData(brokerName);
    if (brokerData == null || !cluster.equals(brokerData.getCluster())) {
        log.warn("no broker data for broker name:{}, broker data:{}", brokerName, brokerData);
        return null;
    }

    HashMap<Long, String> brokerAddrs = new HashMap<>(brokerData.getBrokerAddrs());
    for (Iterator<Map.Entry<Long, String>> it = brokerAddrs.entrySet().iterator(); it.hasNext(); ) {
        Map.Entry<Long, String> item = it.next();
        if (item.getKey() > namesrvController.getNamesrvConfig().getMaxIdForRoleSwitch()) {
            it.remove();
        }
    }

    //no broker
    if (brokerAddrs == null || brokerAddrs.isEmpty()) {
        log.warn("no broker addrs, for broker name:{}, broker data:{}", brokerName, brokerData);
        return null;
    }

    //only one, and master
    if (brokerAddrs.size() == 1 && brokerAddrs.get(MixAll.MASTER_ID) != null) {
        log.warn("only on broker, but it is current master");
        return null;
    }

    //slave exist
    RoleChangeInfo roleChangeInfo = new RoleChangeInfo();
    SortedSet<Long> ids = new TreeSet<>(brokerAddrs.keySet());
    if (ids.first() == MixAll.MASTER_ID) {
        roleChangeInfo.oldMaster = new RoleInChange(brokerAddrs.get(ids.first()), ids.first(), ids.last() + 1);
    }

    long newMasterId = pickMaster(brokerAddrs);
    if (newMasterId == -1) {
        //newMasterId = ids.last();
        log.error("do not get master, broker name:{}", brokerName);
        return null;
    }
    roleChangeInfo.newMaster = new RoleInChange(brokerAddrs.get(newMasterId), newMasterId, MixAll.MASTER_ID);

    return roleChangeInfo;
}