diff --git a/remoting/src/main/java/org/apache/rocketmq/remoting/netty/NettyRemotingClient.java b/remoting/src/main/java/org/apache/rocketmq/remoting/netty/NettyRemotingClient.java index 6ac54aed6d2..609f59ac95d 100644 --- a/remoting/src/main/java/org/apache/rocketmq/remoting/netty/NettyRemotingClient.java +++ b/remoting/src/main/java/org/apache/rocketmq/remoting/netty/NettyRemotingClient.java @@ -500,41 +500,64 @@ public void closeChannel(final Channel channel) { } @Override - public void updateNameServerAddressList(List addrs) { - List old = this.namesrvAddrList.get(); - boolean update = false; - - if (!addrs.isEmpty()) { - if (null == old) { - update = true; - } else if (addrs.size() != old.size()) { - update = true; - } else { - for (String addr : addrs) { - if (!old.contains(addr)) { - update = true; - break; - } - } + public void updateNameServerAddressList(List newAddresses) { + List oldAddresses = this.namesrvAddrList.get(); + + // Check if the address list needs to be updated + if (shouldUpdateAddressList(newAddresses, oldAddresses)) { + Collections.shuffle(newAddresses); + LOGGER.info("name server address updated. NEW : {} , OLD: {}", newAddresses, oldAddresses); + this.namesrvAddrList.set(newAddresses); + + // Handle channel closure if the chosen address is not in the new list + handleChannelClosureIfNeeded(newAddresses); + } + } + + /** + * Check if the address list should be updated + */ + private static boolean shouldUpdateAddressList(List newAddresses, List oldAddresses) { + if (newAddresses.isEmpty()) { + return false; + } + + if (oldAddresses == null || newAddresses.size() != oldAddresses.size()) { + return true; + } + + for (String addr : newAddresses) { + if (!oldAddresses.contains(addr)) { + return true; } + } - if (update) { - Collections.shuffle(addrs); - LOGGER.info("name server address updated. NEW : {} , OLD: {}", addrs, old); - this.namesrvAddrList.set(addrs); - - // should close the channel if choosed addr is not exist. - String chosenNameServerAddr = this.namesrvAddrChoosed.get(); - if (chosenNameServerAddr != null && !addrs.contains(chosenNameServerAddr)) { - namesrvAddrChoosed.compareAndSet(chosenNameServerAddr, null); - for (String addr : this.channelTables.keySet()) { - if (addr.contains(chosenNameServerAddr)) { - ChannelWrapper channelWrapper = this.channelTables.get(addr); - if (channelWrapper != null) { - channelWrapper.close(); - } - } - } + return false; + } + + /** + * Handle channel closure if the chosen address is no longer in the new list + */ + private void handleChannelClosureIfNeeded(List newAddresses) { + String chosenNameServerAddr = this.namesrvAddrChoosed.get(); + if (chosenNameServerAddr != null && !newAddresses.contains(chosenNameServerAddr)) { + // Set the chosen address to null + namesrvAddrChoosed.compareAndSet(chosenNameServerAddr, null); + + // Close the channels associated with the chosen address + closeChannelsForAddress(chosenNameServerAddr); + } + } + + /** + * Close channels associated with the given address + */ + private void closeChannelsForAddress(String chosenNameServerAddr) { + for (String addr : this.channelTables.keySet()) { + if (addr.contains(chosenNameServerAddr)) { + ChannelWrapper channelWrapper = this.channelTables.get(addr); + if (channelWrapper != null) { + channelWrapper.close(); } } }