sayalimohite55 commented on code in PR #6784:
URL: https://github.com/apache/hive/pull/6784#discussion_r4333858058


##########
common/src/java/org/apache/hadoop/hive/common/ServerUtils.java:
##########
@@ -68,15 +70,58 @@ public static InetAddress getHostAddress(String hostname) 
throws UnknownHostExce
     }
     return serverIPAddress;
   }
+
+  /**
+   * Get the IP address string (e.g. "192.168.1.50") for the given host name.
+   * @param hostname The name of the host
+   * @return The network address of the host and UNKNOWN if resolution fails
+   */
+  public static String getHostAddressString(String hostname) {
+    try {
+      return InetAddress.getByName(hostname).getHostAddress();
+    } catch (UnknownHostException e) {
+      LOG.warn("Error trying to get host address for {}: {}", hostname, 
e.getMessage());
+      return "UNKNOWN";
+    }
+  }
+
   /**
    * @return name of current host
    */
   public static String hostname() {
     try {
       return InetAddress.getLocalHost().getHostName();
     } catch (UnknownHostException e) {
-      LOG.error("Unable to resolve my host name " + e.getMessage());
-      throw new RuntimeException(e);
+      LOG.warn("Error fetching hostname! Falling back to localhost.{}", 
e.getMessage());
+      return LOCALHOST;

Review Comment:
   Hi, thank you for reviewing this PR.
   Regarding how we handle UnknownHostException, our existing codebase uses a 
few different approaches:
   • Throws an exception: MetastoreLock.java (throws LockException).
   • Returns the input: Utils.java#getCanonicalHostName (returns hostName).
   • Fallbacks to localhost: RegistryUtilities.java and MetricUtils.java 
(return localhost).
   Additionally, where InetAddress.getLocalHost() is invoked directly, we 
either throw UnknownHostException/RuntimeException or catch it silently.
   Because most of these utilities ultimately rely on 
InetAddress.getLocalHost(), returning localhost on failure felt like the best 
way to ensure graceful degradation. However, I am completely open to 
suggestions or a different perspective on this!



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to