From 2fb4f4450230fee09ff8932eb107f09ef72f2402 Mon Sep 17 00:00:00 2001 From: yantangzhai Date: Tue, 30 Dec 2014 21:41:59 +0800 Subject: [PATCH 1/2] [SPARK-5007] [CORE] Try random port when startServiceOnPort to reduce the chance of port collision --- core/src/main/scala/org/apache/spark/util/Utils.scala | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) 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 0d771baaa6abc..8c29f40ad7d8b 100644 --- a/core/src/main/scala/org/apache/spark/util/Utils.scala +++ b/core/src/main/scala/org/apache/spark/util/Utils.scala @@ -1705,11 +1705,10 @@ private[spark] object Utils extends Logging { /** * Attempt to start a service on the given port, or fail after a number of attempts. - * Each subsequent attempt uses 1 + the port used in the previous attempt (unless the port is 0). + * Each subsequent attempt uses random + the port used in the previous attempt (unless the port is 0). * * @param startPort The initial port to start the service on. * @param maxRetries Maximum number of retries to attempt. - * A value of 3 means attempting ports n, n+1, n+2, and n+3, for example. * @param startService Function to start service on a given port. * This is expected to throw java.net.BindException on port collision. */ @@ -1719,13 +1718,13 @@ private[spark] object Utils extends Logging { serviceName: String = "", maxRetries: Int = portMaxRetries): (T, Int) = { val serviceString = if (serviceName.isEmpty) "" else s" '$serviceName'" - for (offset <- 0 to maxRetries) { + for (retry <- 0 to maxRetries) { // Do not increment port if startPort is 0, which is treated as a special port val tryPort = if (startPort == 0) { startPort } else { // If the new port wraps around, do not try a privilege port - ((startPort + offset - 1024) % (65536 - 1024)) + 1024 + ((startPort + random.nextInt(65536) - 1024) % (65536 - 1024)) + 1024 } try { val (service, port) = startService(tryPort) @@ -1733,7 +1732,7 @@ private[spark] object Utils extends Logging { return (service, port) } catch { case e: Exception if isBindCollision(e) => - if (offset >= maxRetries) { + if (retry >= maxRetries) { val exceptionMessage = s"${e.getMessage}: Service$serviceString failed after $maxRetries retries!" val exception = new BindException(exceptionMessage) From 114ec737b5a81c5f338e2e531a7dc73958fb6339 Mon Sep 17 00:00:00 2001 From: yantangzhai Date: Wed, 31 Dec 2014 10:43:20 +0800 Subject: [PATCH 2/2] [SPARK-5007] [CORE] Try random port when startServiceOnPort to reduce the chance of port collision --- core/src/main/scala/org/apache/spark/util/Utils.scala | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 8c29f40ad7d8b..2506d673b3f69 100644 --- a/core/src/main/scala/org/apache/spark/util/Utils.scala +++ b/core/src/main/scala/org/apache/spark/util/Utils.scala @@ -1705,7 +1705,8 @@ private[spark] object Utils extends Logging { /** * Attempt to start a service on the given port, or fail after a number of attempts. - * Each subsequent attempt uses random + the port used in the previous attempt (unless the port is 0). + * Each subsequent attempt uses random + the port used in the previous attempt + * (unless the port is 0). * * @param startPort The initial port to start the service on. * @param maxRetries Maximum number of retries to attempt.