Skip to content

Commit 89e5650

Browse files
authored
Find local InetAddress (apache#801)
* Find local InetAddress * Find local InetAddress * Find local InetAddress * Find local InetAddress
1 parent 57bda78 commit 89e5650

File tree

6 files changed

+45
-9
lines changed

6 files changed

+45
-9
lines changed

kyuubi-common/src/main/scala/org/apache/kyuubi/Utils.scala

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.kyuubi
1919

2020
import java.io.{File, InputStreamReader, IOException}
21+
import java.net.{Inet4Address, InetAddress, NetworkInterface}
2122
import java.nio.charset.StandardCharsets
2223
import java.nio.file.{Files, Path, Paths}
2324
import java.util.{Properties, UUID}
@@ -175,4 +176,32 @@ private[kyuubi] object Utils extends Logging {
175176
def addShutdownHook(hook: Runnable, priority: Int): Unit = {
176177
ShutdownHookManager.get().addShutdownHook(hook, priority)
177178
}
179+
180+
/**
181+
* This block of code is based on Spark's Utils.findLocalInetAddress()
182+
*/
183+
def findLocalInetAddress: InetAddress = {
184+
val address = InetAddress.getLocalHost
185+
if (address.isLoopbackAddress) {
186+
val activeNetworkIFs = NetworkInterface.getNetworkInterfaces.asScala.toSeq
187+
val reOrderedNetworkIFs = if (isWindows) activeNetworkIFs else activeNetworkIFs.reverse
188+
189+
for (ni <- reOrderedNetworkIFs) {
190+
val addresses = ni.getInetAddresses.asScala
191+
.filterNot(addr => addr.isLinkLocalAddress || addr.isLoopbackAddress).toSeq
192+
if (addresses.nonEmpty) {
193+
val addr = addresses.find(_.isInstanceOf[Inet4Address]).getOrElse(addresses.head)
194+
// because of Inet6Address.toHostName may add interface at the end if it knows about it
195+
val strippedAddress = InetAddress.getByAddress(addr.getAddress)
196+
// We've found an address that looks reasonable!
197+
warn(s"${address.getHostName} was resolved to a loopback address: " +
198+
s"${address.getHostAddress}, using ${strippedAddress.getHostAddress}")
199+
return strippedAddress
200+
}
201+
}
202+
warn(s"${address.getHostName} was resolved to a loopback address: ${address.getHostAddress}" +
203+
" but we couldn't find any external IP address!")
204+
}
205+
address
206+
}
178207
}

kyuubi-common/src/main/scala/org/apache/kyuubi/service/FrontendService.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import org.apache.thrift.server.{ServerContext, TServer, TServerEventHandler, TT
3030
import org.apache.thrift.transport.{TServerSocket, TTransport}
3131

3232
import org.apache.kyuubi.{KyuubiException, KyuubiSQLException, Logging}
33+
import org.apache.kyuubi.Utils
3334
import org.apache.kyuubi.config.KyuubiConf
3435
import org.apache.kyuubi.operation.{FetchOrientation, OperationHandle}
3536
import org.apache.kyuubi.service.authentication.KyuubiAuthenticationFactory
@@ -60,7 +61,7 @@ class FrontendService private (name: String, be: BackendService, oomHook: Runnab
6061
try {
6162
hadoopConf = KyuubiHadoopUtils.newHadoopConf(conf)
6263
val serverHost = conf.get(FRONTEND_BIND_HOST)
63-
serverAddr = serverHost.map(InetAddress.getByName).getOrElse(InetAddress.getLocalHost)
64+
serverAddr = serverHost.map(InetAddress.getByName).getOrElse(Utils.findLocalInetAddress)
6465
portNum = conf.get(FRONTEND_BIND_PORT)
6566
val minThreads = conf.get(FRONTEND_MIN_WORKER_THREADS)
6667
val maxThreads = conf.get(FRONTEND_MAX_WORKER_THREADS)

kyuubi-common/src/test/scala/org/apache/kyuubi/KyuubiFunSuite.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach, Outcome}
2626
import org.scalatest.concurrent.Eventually
2727
import org.scalatest.funsuite.AnyFunSuite
2828

29-
import org.apache.kyuubi.config.KyuubiConf
3029
import org.apache.kyuubi.config.internal.Tests.IS_TESTING
3130

3231
trait KyuubiFunSuite extends AnyFunSuite
@@ -36,7 +35,6 @@ trait KyuubiFunSuite extends AnyFunSuite
3635
with ThreadAudit
3736
with Logging {
3837
// scalastyle:on
39-
System.setProperty(KyuubiConf.FRONTEND_BIND_HOST.key, "127.0.0.1")
4038
override def beforeAll(): Unit = {
4139
System.setProperty(IS_TESTING.key, "true")
4240
doThreadPreAudit()

kyuubi-common/src/test/scala/org/apache/kyuubi/UtilsSuite.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.kyuubi
1919

2020
import java.io.{File, IOException}
21+
import java.net.InetAddress
2122
import java.nio.file.Files
2223
import java.security.PrivilegedExceptionAction
2324
import java.util.Properties
@@ -123,4 +124,13 @@ class UtilsSuite extends KyuubiFunSuite {
123124
intercept[IllegalArgumentException](Utils.shortVersion("-" + KYUUBI_VERSION))
124125
intercept[IllegalArgumentException](Utils.majorMinorVersion("-" + KYUUBI_VERSION))
125126
}
127+
128+
test("findLocalInetAddress") {
129+
val address = InetAddress.getLocalHost
130+
if (!address.isLoopbackAddress) {
131+
assert(Utils.findLocalInetAddress === InetAddress.getLocalHost)
132+
} else {
133+
assert(Utils.findLocalInetAddress !== InetAddress.getLocalHost)
134+
}
135+
}
126136
}

kyuubi-common/src/test/scala/org/apache/kyuubi/operation/JDBCTestUtils.scala

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import org.apache.thrift.protocol.TBinaryProtocol
2525
import org.apache.thrift.transport.TSocket
2626

2727
import org.apache.kyuubi.{KyuubiFunSuite, Utils}
28-
import org.apache.kyuubi.config.KyuubiConf
2928
import org.apache.kyuubi.service.authentication.PlainSASLHelper
3029

3130
trait JDBCTestUtils extends KyuubiFunSuite {
@@ -38,10 +37,7 @@ trait JDBCTestUtils extends KyuubiFunSuite {
3837
private var _sparkHiveConfs: Map[String, String] = Map.empty
3938
private var _sparkHiveVars: Map[String, String] = Map.empty
4039
protected def sessionConfigs: Map[String, String] = _sessionConfs
41-
protected def sparkHiveConfigs: Map[String, String] = {
42-
// TODO: KYUUBI-504: forbid setting FRONTEND_BIND_HOST by connection string in engine side
43-
Map(KyuubiConf.FRONTEND_BIND_HOST.key -> "localhost") ++: _sparkHiveConfs
44-
}
40+
protected def sparkHiveConfigs: Map[String, String] = _sparkHiveConfs
4541
protected def sparkHiveVars: Map[String, String] = _sparkHiveVars
4642

4743
def withSessionConf[T](

kyuubi-zookeeper/src/main/scala/org/apache/kyuubi/zookeeper/EmbeddedZookeeper.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.kyuubi.zookeeper
1919

2020
import java.io.File
21+
import java.net.InetAddress
2122

2223
import scala.collection.JavaConverters._
2324

@@ -53,7 +54,8 @@ class EmbeddedZookeeper extends AbstractService("EmbeddedZookeeper") {
5354
"maxSessionTimeout" -> Integer.valueOf(timeout)
5455
}).toMap[String, Object].asJava
5556

56-
val hostname = conf.get(ZK_CLIENT_PORT_ADDRESS).orNull
57+
val hostname = conf.get(ZK_CLIENT_PORT_ADDRESS).map(InetAddress.getByName)
58+
.getOrElse(Utils.findLocalInetAddress).getCanonicalHostName
5759
spec = new InstanceSpec(
5860
dataDirectory,
5961
clientPort,

0 commit comments

Comments
 (0)