Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HBASE-27660 Ignore invalid hostname when getNetworkInterfaces #5052

Merged
merged 1 commit into from
Feb 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}