You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@geode.apache.org by GitBox <gi...@apache.org> on 2020/11/12 00:04:18 UTC

[GitHub] [geode] pivotal-jbarrett commented on a change in pull request #5680: GEODE-8623: Retry getting local host if it fails.

pivotal-jbarrett commented on a change in pull request #5680:
URL: https://github.com/apache/geode/pull/5680#discussion_r521717339



##########
File path: geode-common/src/main/java/org/apache/geode/internal/inet/LocalHostUtil.java
##########
@@ -54,70 +59,80 @@
   private static final boolean useLinkLocalAddresses =
       Boolean.getBoolean(USE_LINK_LOCAL_ADDRESSES_PROPERTY);
 
-  /**
-   * we cache localHost to avoid bug #40619, access-violation in native code
-   */
-  private static final InetAddress localHost;
-
   /**
    * all classes should use this variable to determine whether to use IPv4 or IPv6 addresses
    */
   @MakeNotStatic
   private static boolean useIPv6Addresses = !Boolean.getBoolean("java.net.preferIPv4Stack")
       && Boolean.getBoolean("java.net.preferIPv6Addresses");
 
-  static {
-    InetAddress inetAddress = null;
+  /**
+   * Resolves local host. Will retry if resolution fails.
+   *
+   * @return local host if resolved otherwise null.
+   */
+  private static InetAddress resolveLocalHost() {
     try {
-      inetAddress = InetAddress.getByAddress(InetAddress.getLocalHost().getAddress());
-      if (inetAddress.isLoopbackAddress()) {
-        InetAddress ipv4Fallback = null;
-        InetAddress ipv6Fallback = null;
-        // try to find a non-loopback address
-        Set<InetAddress> myInterfaces = getMyAddresses();
-        boolean preferIPv6 = useIPv6Addresses;
-        String lhName = null;
-        for (InetAddress addr : myInterfaces) {
-          if (addr.isLoopbackAddress() || addr.isAnyLocalAddress() || lhName != null) {
-            break;
-          }
-          boolean ipv6 = addr instanceof Inet6Address;
-          boolean ipv4 = addr instanceof Inet4Address;
-          if ((preferIPv6 && ipv6) || (!preferIPv6 && ipv4)) {
-            String addrName = reverseDNS(addr);
-            if (inetAddress.isLoopbackAddress()) {
-              inetAddress = addr;
-              lhName = addrName;
-            } else if (addrName != null) {
-              inetAddress = addr;
-              lhName = addrName;
+      return tryFor(() -> {
+        InetAddress inetAddress = null;
+        try {
+          inetAddress = InetAddress.getByAddress(InetAddress.getLocalHost().getAddress());
+          if (inetAddress.isLoopbackAddress()) {
+            InetAddress ipv4Fallback = null;
+            InetAddress ipv6Fallback = null;
+            // try to find a non-loopback address
+            Set<InetAddress> myInterfaces = getMyAddresses();
+            boolean preferIPv6 = useIPv6Addresses;
+            String lhName = null;
+            for (InetAddress addr : myInterfaces) {
+              if (addr.isLoopbackAddress() || addr.isAnyLocalAddress() || lhName != null) {
+                break;
+              }
+              boolean ipv6 = addr instanceof Inet6Address;
+              boolean ipv4 = addr instanceof Inet4Address;
+              if ((preferIPv6 && ipv6) || (!preferIPv6 && ipv4)) {
+                String addrName = reverseDNS(addr);
+                if (inetAddress.isLoopbackAddress()) {
+                  inetAddress = addr;
+                  lhName = addrName;
+                } else if (addrName != null) {
+                  inetAddress = addr;
+                  lhName = addrName;
+                }
+              } else {
+                if (preferIPv6 && ipv4 && ipv4Fallback == null) {
+                  ipv4Fallback = addr;
+                } else if (!preferIPv6 && ipv6 && ipv6Fallback == null) {
+                  ipv6Fallback = addr;
+                }
+              }
             }
-          } else {
-            if (preferIPv6 && ipv4 && ipv4Fallback == null) {
-              ipv4Fallback = addr;
-            } else if (!preferIPv6 && ipv6 && ipv6Fallback == null) {
-              ipv6Fallback = addr;
+            // vanilla Ubuntu installations will have a usable IPv6 address when
+            // running as a guest OS on an IPv6-enabled machine. We also look for
+            // the alternative IPv4 configuration.
+            if (inetAddress.isLoopbackAddress()) {
+              if (ipv4Fallback != null) {
+                inetAddress = ipv4Fallback;
+                useIPv6Addresses = false;
+              } else if (ipv6Fallback != null) {
+                inetAddress = ipv6Fallback;
+                useIPv6Addresses = true;
+              }
             }
           }
+        } catch (UnknownHostException ignored) {
         }
-        // vanilla Ubuntu installations will have a usable IPv6 address when
-        // running as a guest OS on an IPv6-enabled machine. We also look for
-        // the alternative IPv4 configuration.
-        if (inetAddress.isLoopbackAddress()) {
-          if (ipv4Fallback != null) {
-            inetAddress = ipv4Fallback;
-            useIPv6Addresses = false;
-          } else if (ipv6Fallback != null) {
-            inetAddress = ipv6Fallback;
-            useIPv6Addresses = true;
-          }
-        }
-      }
-    } catch (UnknownHostException ignored) {
+        return inetAddress;
+      }, Objects::nonNull, 1, MINUTES);

Review comment:
       It certainly could. I am not to concerned about the one in thousand times this might be hit in a managed environment though. A hot loop here probably isn't the end of the world. The request will go to the local resolver first then to the DNS so its not going to be all that hot.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org