public static String getHostIp()

in common/src/main/java/org/apache/uniffle/common/util/RssUtils.java [119:184]


  public static String getHostIp() throws Exception {
    // For K8S, there are too many IPs, it's hard to decide which we should use.
    // So we use the environment variable to tell RSS to use which one.
    String ip = System.getenv("RSS_IP");
    if (ip != null) {
      if (!InetAddresses.isInetAddress(ip)) {
        throw new RssException("Environment RSS_IP: " + ip + " is wrong format");
      }
      return ip;
    }
    // Unit tests are executed on a single machine and do not interact with other machines.
    // Therefore, unit tests should not require a valid broadcast address.
    // When running UTs, we will still return the IP address whose broadcast address is invalid.
    boolean isTestMode = Boolean.parseBoolean(System.getProperty("test.mode", "false"));
    Enumeration<NetworkInterface> nif = NetworkInterface.getNetworkInterfaces();
    String siteLocalAddress = null;
    while (nif.hasMoreElements()) {
      NetworkInterface ni = nif.nextElement();
      if (!ni.isUp() || ni.isLoopback() || ni.isPointToPoint() || ni.isVirtual()) {
        continue;
      }
      for (InterfaceAddress ifa : ni.getInterfaceAddresses()) {
        InetAddress ia = ifa.getAddress();
        InetAddress brd = ifa.getBroadcast();
        if ((brd == null || brd.isAnyLocalAddress()) && !isTestMode) {
          LOGGER.info(
              "ip {} was filtered, because it don't have effective broadcast address",
              ia.getHostAddress());
          continue;
        }
        if (!ia.isLinkLocalAddress()
            && !ia.isAnyLocalAddress()
            && !ia.isLoopbackAddress()
            && ia instanceof Inet4Address
            && ia.isReachable(5000)) {
          if (!ia.isSiteLocalAddress()) {
            return ia.getHostAddress();
          } else if (siteLocalAddress == null) {
            LOGGER.info(
                "ip {} was candidate, if there is no better choice, we will choose it",
                ia.getHostAddress());
            siteLocalAddress = ia.getHostAddress();
          } else {
            LOGGER.info(
                "ip {} was filtered, because it's not first effect site local address",
                ia.getHostAddress());
          }
        } else if (!(ia instanceof Inet4Address)) {
          LOGGER.info("ip {} was filtered, because it's just a ipv6 address", ia.getHostAddress());
        } else if (ia.isLinkLocalAddress()) {
          LOGGER.info(
              "ip {} was filtered, because it's just a link local address", ia.getHostAddress());
        } else if (ia.isAnyLocalAddress()) {
          LOGGER.info(
              "ip {} was filtered, because it's just a any local address", ia.getHostAddress());
        } else if (ia.isLoopbackAddress()) {
          LOGGER.info(
              "ip {} was filtered, because it's just a loop back address", ia.getHostAddress());
        } else {
          LOGGER.info(
              "ip {} was filtered, because it's just not reachable address", ia.getHostAddress());
        }
      }
    }
    return siteLocalAddress;
  }