HBASE-27660 Ignore invalid hostname when getNetworkInterfaces (#5052)

Signed-off-by: Duo Zhang <zhangduo@apache.org>
This commit is contained in:
tianhang 2023-02-27 20:11:42 +08:00 committed by GitHub
parent 10037df035
commit 4bee21e96b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 3 deletions

View File

@ -24,6 +24,7 @@ import static org.junit.Assert.assertTrue;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.NetworkInterface; import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -89,9 +90,7 @@ public class TestRegionServerHostname {
@Test @Test
public void testRegionServerHostname() throws Exception { public void testRegionServerHostname() throws Exception {
Enumeration<NetworkInterface> netInterfaceList = NetworkInterface.getNetworkInterfaces(); for (NetworkInterface ni : getValidNetworkInterfaces()) {
while (netInterfaceList.hasMoreElements()) {
NetworkInterface ni = netInterfaceList.nextElement();
Enumeration<InetAddress> addrList = ni.getInetAddresses(); Enumeration<InetAddress> addrList = ni.getInetAddresses();
// iterate through host addresses and use each as hostname // iterate through host addresses and use each as hostname
while (addrList.hasMoreElements()) { while (addrList.hasMoreElements()) {
@ -205,4 +204,22 @@ public class TestRegionServerHostname {
assertEquals(expectedRS, servers.size()); 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;
}
} }