diff --git a/core/src/main/scala/org/apache/spark/util/Utils.scala b/core/src/main/scala/org/apache/spark/util/Utils.scala index cf46a3d34c41..3e4a7e727a87 100644 --- a/core/src/main/scala/org/apache/spark/util/Utils.scala +++ b/core/src/main/scala/org/apache/spark/util/Utils.scala @@ -1071,21 +1071,30 @@ private[spark] object Utils extends Logging { * Get the local machine's FQDN. */ def localCanonicalHostName(): String = { - customHostname.getOrElse(localIpAddress.getCanonicalHostName) + addBracketsIfNeeded(customHostname.getOrElse(localIpAddress.getCanonicalHostName)) } /** * Get the local machine's hostname. + * In case of IPv6, getHostAddress may return '0:0:0:0:0:0:0:1'. */ def localHostName(): String = { - customHostname.getOrElse(localIpAddress.getHostAddress) + addBracketsIfNeeded(customHostname.getOrElse(localIpAddress.getHostAddress)) } /** * Get the local machine's URI. */ def localHostNameForURI(): String = { - customHostname.getOrElse(InetAddresses.toUriString(localIpAddress)) + addBracketsIfNeeded(customHostname.getOrElse(InetAddresses.toUriString(localIpAddress))) + } + + private def addBracketsIfNeeded(addr: String): String = { + if (addr.contains(":") && !addr.contains("[")) { + "[" + addr + "]" + } else { + addr + } } /**