Skip to content

Commit febafef

Browse files
advancedxypwendell
authored andcommitted
[SPARK-3040] pick up a more proper local ip address for Utils.findLocalIpAddress method
Short version: NetworkInterface.getNetworkInterfaces returns ifs in reverse order compared to ifconfig output. It may pick up ip address associated with tun0 or virtual network interface. See [SPARK_3040](https://issues.apache.org/jira/browse/SPARK-3040) for more detail Author: Ye Xianjin <[email protected]> Closes apache#1946 from advancedxy/SPARK-3040 and squashes the following commits: f33f6b2 [Ye Xianjin] add windows support 087a785 [Ye Xianjin] reverse the Networkinterface.getNetworkInterfaces output order to get a more proper local ip address.
1 parent ecf0c02 commit febafef

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

core/src/main/scala/org/apache/spark/util/Utils.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,12 @@ private[spark] object Utils extends Logging {
530530
if (address.isLoopbackAddress) {
531531
// Address resolves to something like 127.0.1.1, which happens on Debian; try to find
532532
// a better address using the local network interfaces
533-
for (ni <- NetworkInterface.getNetworkInterfaces) {
533+
// getNetworkInterfaces returns ifs in reverse order compared to ifconfig output order
534+
// on unix-like system. On windows, it returns in index order.
535+
// It's more proper to pick ip address following system output order.
536+
val activeNetworkIFs = NetworkInterface.getNetworkInterfaces.toList
537+
val reOrderedNetworkIFs = if (isWindows) activeNetworkIFs else activeNetworkIFs.reverse
538+
for (ni <- reOrderedNetworkIFs) {
534539
for (addr <- ni.getInetAddresses if !addr.isLinkLocalAddress &&
535540
!addr.isLoopbackAddress && addr.isInstanceOf[Inet4Address]) {
536541
// We've found an address that looks reasonable!

0 commit comments

Comments
 (0)