Skip to content

Commit d4bd5d6

Browse files
authored
ensure AddressResolver supports localhost even if ipv6 is disabled in sysctl but not /etc/hosts (#3285)
1 parent 9ebe5f2 commit d4bd5d6

File tree

1 file changed

+26
-6
lines changed
  • persistence/nosql/persistence/cdi/quarkus-distcache/src/main/java/org/apache/polaris/persistence/nosql/quarkus/distcache

1 file changed

+26
-6
lines changed

persistence/nosql/persistence/cdi/quarkus-distcache/src/main/java/org/apache/polaris/persistence/nosql/quarkus/distcache/AddressResolver.java

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.util.Set;
3636
import java.util.stream.IntStream;
3737
import java.util.stream.Stream;
38+
import org.jspecify.annotations.NonNull;
3839
import org.slf4j.Logger;
3940
import org.slf4j.LoggerFactory;
4041

@@ -54,14 +55,27 @@ record AddressResolver(DnsClient dnsClient, List<String> searchList) {
5455

5556
static {
5657
try {
58+
IP_V4_ONLY = Boolean.parseBoolean(System.getProperty("java.net.preferIPv4Stack", "false"));
5759
LOCAL_ADDRESSES =
5860
networkInterfaces()
5961
.flatMap(
6062
ni ->
61-
ni.getInterfaceAddresses().stream()
62-
// Need to do this InetAddress->byte[]->InetAddress dance to get rid of
63-
// host-address suffixes as in `0:0:0:0:0:0:0:1%lo`
64-
.map(InterfaceAddress::getAddress)
63+
Stream.concat(
64+
// localhost can be ipv6 when sysctl disable ipv6
65+
// if ::1 is registered for localhost in /etc/hosts
66+
// in this case java stack can still capture it
67+
// and it will work (even if not great)
68+
// this is a workaround to ensure at least localhost is
69+
// in the list but this could be more general to all /etc/hosts
70+
// mappings
71+
IP_V4_ONLY || !"lo".equalsIgnoreCase(ni.getName())
72+
? Stream.empty()
73+
: findLocalhostAddresses(),
74+
ni.getInterfaceAddresses().stream()
75+
// Need to do this InetAddress->byte[]->InetAddress dance to get
76+
// rid of
77+
// host-address suffixes as in `0:0:0:0:0:0:0:1%lo`
78+
.map(InterfaceAddress::getAddress))
6579
.map(InetAddress::getAddress)
6680
.map(
6781
a -> {
@@ -75,13 +89,19 @@ record AddressResolver(DnsClient dnsClient, List<String> searchList) {
7589
})
7690
.map(InetAddress::getHostAddress))
7791
.collect(toUnmodifiableSet());
78-
79-
IP_V4_ONLY = Boolean.parseBoolean(System.getProperty("java.net.preferIPv4Stack", "false"));
8092
} catch (SocketException e) {
8193
throw new RuntimeException(e);
8294
}
8395
}
8496

97+
private static @NonNull Stream<InetAddress> findLocalhostAddresses() {
98+
try {
99+
return Stream.of(InetAddress.getAllByName("localhost"));
100+
} catch (final RuntimeException | UnknownHostException e) {
101+
return Stream.empty();
102+
}
103+
}
104+
85105
/**
86106
* Uses a "default" {@link DnsClient} using the first {@code nameserver} and the {@code search}
87107
* list configured in {@code /etc/resolv.conf}.

0 commit comments

Comments
 (0)