Java Code Examples for org.apache.rocketmq.common.MixAll#MASTER_ID
The following examples show how to use
org.apache.rocketmq.common.MixAll#MASTER_ID .
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: PullAPIWrapper.java From rocketmq with Apache License 2.0 | 6 votes |
/** * 重新计算消息队列拉取消息对应的Broker编号 * 若之前的请求返回了建议拉取的BrokerId,则使用此id * 比如Master开启了 SlaveReadEnable=true ,且Master内存积压了大量未消费消息, * 会返回id = 1,,也就是建议下次从BrokerId = 1 的Slave拉取消息, 但默认 SlaveReadEnable=false * * @param mq 消息队列 * @return Broker编号 */ public long recalculatePullFromWhichNode(final MessageQueue mq) { // 若开启默认Broker开关,则返回Master的编号 : 0l if (this.isConnectBrokerByUser()) { return this.defaultBrokerId; } // 若消息队列映射拉取Broker存在,则返回映射Broker编号 AtomicLong suggest = this.pullFromWhichNodeTable.get(mq); if (suggest != null) { return suggest.get(); } // 返回Broker主节点编号 return MixAll.MASTER_ID; }
Example 2
Source File: HAManager.java From DDMQ with Apache License 2.0 | 6 votes |
private long pickMaster(HashMap<Long, String> brokerAddrs) { long maxOffset = -1; long brokerId = -1; for (Map.Entry<Long, String> broker : brokerAddrs.entrySet()) { if (broker.getKey() == MixAll.MASTER_ID) { continue; } long offset = namesrvController.getRouteInfoManager().getBrokerMaxPhyOffset(broker.getValue()); if (offset > maxOffset) { brokerId = broker.getKey(); maxOffset = offset; } } log.info("get new master id:{}, maxOffset:{}", brokerId, maxOffset); return brokerId; }
Example 3
Source File: CommandUtil.java From rocketmq with Apache License 2.0 | 5 votes |
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 4
Source File: PullAPIWrapper.java From rocketmq-read with Apache License 2.0 | 5 votes |
/** * 计算从哪个节点拉取数据 * @param mq mq * @return ; */ public long recalculatePullFromWhichNode(final MessageQueue mq) { if (this.isConnectBrokerByUser()) { return this.defaultBrokerId; } AtomicLong suggest = this.pullFromWhichNodeTable.get(mq); if (suggest != null) { return suggest.get(); } return MixAll.MASTER_ID; }
Example 5
Source File: PullAPIWrapper.java From rocketmq-all-4.1.0-incubating with Apache License 2.0 | 5 votes |
public long recalculatePullFromWhichNode(final MessageQueue mq) { if (this.isConnectBrokerByUser()) { return this.defaultBrokerId; } AtomicLong suggest = this.pullFromWhichNodeTable.get(mq); if (suggest != null) { return suggest.get(); } return MixAll.MASTER_ID; }
Example 6
Source File: MQClientInstance.java From rocketmq-read with Apache License 2.0 | 5 votes |
/** * 查询broker的地址 * @param brokerName broker的地址 * @param brokerId brokerId * @param onlyThisBroker 是否就这个broker * @return ; */ public FindBrokerResult findBrokerAddressInSubscribe( final String brokerName, final long brokerId, final boolean onlyThisBroker) { String brokerAddr = null; boolean slave = false; boolean found = false; HashMap<Long/* brokerId */, String/* address */> map = this.brokerAddrTable.get(brokerName); if (map != null && !map.isEmpty()) { brokerAddr = map.get(brokerId); slave = brokerId != MixAll.MASTER_ID; found = brokerAddr != null; if (!found && !onlyThisBroker) { Entry<Long, String> entry = map.entrySet().iterator().next(); brokerAddr = entry.getValue(); slave = entry.getKey() != MixAll.MASTER_ID; found = true; } } if (found) { return new FindBrokerResult(brokerAddr, slave, findBrokerVersion(brokerName, brokerAddr)); } return null; }
Example 7
Source File: MQClientInstance.java From rocketmq-read with Apache License 2.0 | 5 votes |
/** * 从本地的brokerAddrTable find一个Broker * @param brokerName brokerName * @return ; */ public FindBrokerResult findBrokerAddressInAdmin(final String brokerName) { String brokerAddr = null; boolean slave = false; boolean found = false; HashMap<Long/* brokerId */, String/* address */> map = this.brokerAddrTable.get(brokerName); if (map != null && !map.isEmpty()) { for (Map.Entry<Long, String> entry : map.entrySet()) { Long id = entry.getKey(); brokerAddr = entry.getValue(); if (brokerAddr != null) { found = true; if (MixAll.MASTER_ID == id) { slave = false; } else { slave = true; } break; } } // end of for } if (found) { return new FindBrokerResult(brokerAddr, slave, findBrokerVersion(brokerName, brokerAddr)); } return null; }
Example 8
Source File: MQClientInstance.java From rocketmq-4.3.0 with Apache License 2.0 | 5 votes |
public FindBrokerResult findBrokerAddressInSubscribe( final String brokerName, final long brokerId, final boolean onlyThisBroker ) { String brokerAddr = null; boolean slave = false; boolean found = false; // 获取broker的缓存信息 HashMap<Long/* brokerId */, String/* address */> map = this.brokerAddrTable.get(brokerName); if (map != null && !map.isEmpty()) { brokerAddr = map.get(brokerId); slave = brokerId != MixAll.MASTER_ID; found = brokerAddr != null; if (!found && !onlyThisBroker) { Entry<Long, String> entry = map.entrySet().iterator().next(); brokerAddr = entry.getValue(); slave = entry.getKey() != MixAll.MASTER_ID; found = true; } } if (found) { return new FindBrokerResult(brokerAddr, slave, findBrokerVersion(brokerName, brokerAddr)); } return null; }
Example 9
Source File: MQClientInstance.java From DDMQ with Apache License 2.0 | 5 votes |
public FindBrokerResult findBrokerAddressInAdmin(final String brokerName) { String brokerAddr = null; boolean slave = false; boolean found = false; HashMap<Long/* brokerId */, String/* address */> map = this.brokerAddrTable.get(brokerName); if (map != null && !map.isEmpty()) { for (Map.Entry<Long, String> entry : map.entrySet()) { Long id = entry.getKey(); brokerAddr = entry.getValue(); if (brokerAddr != null) { found = true; if (MixAll.MASTER_ID == id) { slave = false; } else { slave = true; } break; } } // end of for } if (found) { return new FindBrokerResult(brokerAddr, slave, findBrokerVersion(brokerName, brokerAddr)); } return null; }
Example 10
Source File: PullAPIWrapper.java From rocketmq-4.3.0 with Apache License 2.0 | 5 votes |
public long recalculatePullFromWhichNode(final MessageQueue mq) { if (this.isConnectBrokerByUser()) { return this.defaultBrokerId; } AtomicLong suggest = this.pullFromWhichNodeTable.get(mq); if (suggest != null) { return suggest.get(); } return MixAll.MASTER_ID; }
Example 11
Source File: PullAPIWrapper.java From DDMQ with Apache License 2.0 | 5 votes |
public long recalculatePullFromWhichNode(final MessageQueue mq) { if (this.isConnectBrokerByUser()) { //only true for filter server. return this.defaultBrokerId; } AtomicLong suggest = this.pullFromWhichNodeTable.get(mq); if (suggest != null) { return suggest.get(); } return MixAll.MASTER_ID; }
Example 12
Source File: CommandUtil.java From DDMQ with Apache License 2.0 | 5 votes |
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 13
Source File: MQClientInstance.java From rocketmq_trans_message with Apache License 2.0 | 5 votes |
public FindBrokerResult findBrokerAddressInSubscribe(// final String brokerName, // final long brokerId, // final boolean onlyThisBroker// ) { String brokerAddr = null; boolean slave = false; boolean found = false; HashMap<Long/* brokerId */, String/* address */> map = this.brokerAddrTable.get(brokerName); if (map != null && !map.isEmpty()) { brokerAddr = map.get(brokerId); slave = brokerId != MixAll.MASTER_ID; found = brokerAddr != null; if (!found && !onlyThisBroker) { Entry<Long, String> entry = map.entrySet().iterator().next(); brokerAddr = entry.getValue(); slave = entry.getKey() != MixAll.MASTER_ID; found = true; } } if (found) { return new FindBrokerResult(brokerAddr, slave); } return null; }
Example 14
Source File: MQClientInstance.java From DDMQ with Apache License 2.0 | 5 votes |
public FindBrokerResult findBrokerAddressInSubscribeSlaveFirst(final String brokerName) { String brokerAddr = null; boolean slave = false; boolean found = false; HashMap<Long/* brokerId */, String/* address */> map = this.brokerAddrTable.get(brokerName); if (map != null && !map.isEmpty()) { if (map.size() == 1) { brokerAddr = map.values().iterator().next(); slave = map.keySet().iterator().next() == MixAll.MASTER_ID; found = true; } else { for (Map.Entry<Long, String> entry : map.entrySet()) { if (entry.getKey() != MixAll.MASTER_ID) { brokerAddr = entry.getValue(); slave = true; found = true; break; } } } } if (found) { return new FindBrokerResult(brokerAddr, slave, findBrokerVersion(brokerName, brokerAddr)); } return null; }
Example 15
Source File: MQClientInstance.java From rocketmq-all-4.1.0-incubating with Apache License 2.0 | 5 votes |
public FindBrokerResult findBrokerAddressInSubscribe(// final String brokerName, // final long brokerId, // final boolean onlyThisBroker// ) { String brokerAddr = null; boolean slave = false; boolean found = false; HashMap<Long/* brokerId */, String/* address */> map = this.brokerAddrTable.get(brokerName); if (map != null && !map.isEmpty()) { brokerAddr = map.get(brokerId); slave = brokerId != MixAll.MASTER_ID; found = brokerAddr != null; if (!found && !onlyThisBroker) { Entry<Long, String> entry = map.entrySet().iterator().next(); brokerAddr = entry.getValue(); slave = entry.getKey() != MixAll.MASTER_ID; found = true; } } if (found) { return new FindBrokerResult(brokerAddr, slave, findBrokerVersion(brokerName, brokerAddr)); } return null; }
Example 16
Source File: MQClientInstance.java From DDMQ with Apache License 2.0 | 5 votes |
public FindBrokerResult findBrokerAddressInSubscribeSlaveFirst(final String brokerName) { String brokerAddr = null; boolean slave = false; boolean found = false; HashMap<Long/* brokerId */, String/* address */> map = this.brokerAddrTable.get(brokerName); if (map != null && !map.isEmpty()) { if (map.size() == 1) { brokerAddr = map.values().iterator().next(); slave = map.keySet().iterator().next() == MixAll.MASTER_ID; found = true; } else { for (Map.Entry<Long, String> entry : map.entrySet()) { if (entry.getKey() != MixAll.MASTER_ID) { brokerAddr = entry.getValue(); slave = true; found = true; break; } } } } if (found) { return new FindBrokerResult(brokerAddr, slave, findBrokerVersion(brokerName, brokerAddr)); } return null; }
Example 17
Source File: MQClientInstance.java From rocketmq with Apache License 2.0 | 5 votes |
/** * 找到指定Broker的Master,也可能Master宕机了,则找到个Slave * 优先用Master * * @param brokerName * @return */ public FindBrokerResult findBrokerAddressInAdmin(final String brokerName) { String brokerAddr = null; boolean slave = false; boolean found = false; HashMap<Long/* brokerId */, String/* address */> map = this.brokerAddrTable.get(brokerName); if (map != null && !map.isEmpty()) { FOR_SEG: for (Map.Entry<Long, String> entry : map.entrySet()) { Long id = entry.getKey(); brokerAddr = entry.getValue(); if (brokerAddr != null) { //只找第一个Entry,Long类型的Key, 0的序号比1靠前 若Master存在,则必定排第一位 found = true; if (MixAll.MASTER_ID == id) { slave = false; break FOR_SEG; } else { slave = true; } break; } } // end of for } if (found) { return new FindBrokerResult(brokerAddr, slave); } return null; }
Example 18
Source File: MQClientInstance.java From rocketmq-all-4.1.0-incubating with Apache License 2.0 | 4 votes |
private void sendHeartbeatToAllBroker() { final HeartbeatData heartbeatData = this.prepareHeartbeatData(); final boolean producerEmpty = heartbeatData.getProducerDataSet().isEmpty(); final boolean consumerEmpty = heartbeatData.getConsumerDataSet().isEmpty(); if (producerEmpty && consumerEmpty) { log.warn("sending heartbeat, but no consumer and no producer"); return; } if (!this.brokerAddrTable.isEmpty()) { long times = this.sendHeartbeatTimesTotal.getAndIncrement(); Iterator<Entry<String, HashMap<Long, String>>> it = this.brokerAddrTable.entrySet().iterator(); while (it.hasNext()) { Entry<String, HashMap<Long, String>> entry = it.next(); String brokerName = entry.getKey(); HashMap<Long, String> oneTable = entry.getValue(); if (oneTable != null) { for (Map.Entry<Long, String> entry1 : oneTable.entrySet()) { Long id = entry1.getKey(); String addr = entry1.getValue(); if (addr != null) { if (consumerEmpty) { if (id != MixAll.MASTER_ID) continue; } try { int version = this.mQClientAPIImpl.sendHearbeat(addr, heartbeatData, 3000); if (!this.brokerVersionTable.containsKey(brokerName)) { this.brokerVersionTable.put(brokerName, new HashMap<String, Integer>(4)); } this.brokerVersionTable.get(brokerName).put(addr, version); if (times % 20 == 0) { log.info("send heart beat to broker[{} {} {}] success", brokerName, id, addr); log.info(heartbeatData.toString()); } } catch (Exception e) { if (this.isBrokerInNameServer(addr)) { log.info("send heart beat to broker[{} {} {}] failed", brokerName, id, addr); } else { log.info("send heart beat to broker[{} {} {}] exception, because the broker not up, forget it", brokerName, id, addr); } } } } } } } }
Example 19
Source File: SubscriptionGroupConfig.java From DDMQ with Apache License 2.0 | 4 votes |
public Long getB() { return brokerId == MixAll.MASTER_ID ? null : brokerId; }
Example 20
Source File: HAManager.java From DDMQ with Apache License 2.0 | 4 votes |
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; }