进来首先,如果channel不为空,则获取读锁,这里注意RouteInfoManager内部是声明了读写锁的:
private final ReadWriteLock lock = new ReentrantReadWriteLock();
获取锁成功后,接下来从brokerLiveTable(在线的broker表)中遍历出value(BrokerLiveInfo)的channel为目标channel的key作为brokerAddrFound。也就是说第一个if,就是从brokerLiveTable中取出一个key:brokerAddrFound(broker的地址)
整个方法的主要部分都集中在对这个取出的key:brokerAddrFound的处理上。虽然代码很长,但整体逻辑并不复杂,都是删除key为brokerAddrFound的各种表中的元素:(合法化处理还有异常捕获这里就不分析了)
this.lock.writeLock().lockInterruptibly(); this.brokerLiveTable.remove(brokerAddrFound); this.filterServerTable.remove(brokerAddrFound);
获取读锁(增删改都是取读锁),先从brokerLiveTable,filterServerTable中移除key为broker地址的元素。这也符合该方法的逻辑:channelDestory。
while (itBrokerAddrTable.hasNext() && (null == brokerNameFound)) {
BrokerData brokerData = itBrokerAddrTable.next().getValue();
Iterator> it = brokerData.getBrokerAddrs().entrySet().iterator();
while (it.hasNext()) {
Entry 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());
}
}
从brokerData中移除
if (brokerNameFound != null && removeBrokerName) {
Iterator>> it = this.clusterAddrTable.entrySet().iterator();
while (it.hasNext()) {
Entry> entry = it.next();
String clusterName = entry.getKey();
Set 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;
}
}
}



