Skip to content

Commit

Permalink
HBASE-27660 Ignore invalid hostname when getNetworkInterfaces (#5052)
Browse files Browse the repository at this point in the history
Signed-off-by: Duo Zhang <[email protected]>
  • Loading branch information
thangTang authored Feb 27, 2023
1 parent 10037df commit 4bee21e
Showing 1 changed file with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -89,9 +90,7 @@ public void testInvalidRegionServerHostnameAbortsServer() throws Exception {

@Test
public void testRegionServerHostname() throws Exception {
Enumeration<NetworkInterface> netInterfaceList = NetworkInterface.getNetworkInterfaces();
while (netInterfaceList.hasMoreElements()) {
NetworkInterface ni = netInterfaceList.nextElement();
for (NetworkInterface ni : getValidNetworkInterfaces()) {
Enumeration<InetAddress> addrList = ni.getInetAddresses();
// iterate through host addresses and use each as hostname
while (addrList.hasMoreElements()) {
Expand Down Expand Up @@ -205,4 +204,22 @@ public void testRegionServerHostnameReportedToMaster() throws Exception {
assertEquals(expectedRS, servers.size());
}
}

private boolean ignoreNetworkInterface(NetworkInterface networkInterface) throws Exception {
return networkInterface == null || networkInterface.isLoopback() || networkInterface.isVirtual()
|| !networkInterface.isUp();
}

private List<NetworkInterface> getValidNetworkInterfaces() throws Exception {
List<NetworkInterface> validNetworkInterfaces = new ArrayList<>();
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface networkInterface = interfaces.nextElement();
if (ignoreNetworkInterface(networkInterface)) {
continue;
}
validNetworkInterfaces.add(networkInterface);
}
return validNetworkInterfaces;
}
}

0 comments on commit 4bee21e

Please sign in to comment.