Github user dragos commented on a diff in the pull request:
https://github.com/apache/spark/pull/10808#discussion_r50108893
--- Diff: core/src/main/scala/org/apache/spark/util/Utils.scala ---
@@ -1967,39 +1967,139 @@ private[spark] object Utils extends Logging {
conf: SparkConf,
serviceName: String = ""): (T, Int) = {
+ val serviceString = if (serviceName.isEmpty) "" else s" '$serviceName'"
+
+ def portRangeToList(ranges: String): List[(Long, Long)] = {
+ ranges.split(":").map { r => val ret = r.substring(1, r.length -
1).split(",")
+ (ret(0).toLong, ret(1).toLong)
+ }.toList
+ }
+
+ def startOnce(tryPort: Int): (Option[T], Int) = {
+ val serviceString = if (serviceName.isEmpty) "" else s"
'$serviceName'"
+ try {
+ val (service, port) = startService(tryPort)
+ logInfo(s"Successfully started service$serviceString on port
$port.")
+ (Some(service), port)
+ } catch {
+ case e: Exception if isBindCollision(e) =>
+ logWarning(s"Service$serviceString could not bind on port
$tryPort. ")
+ (None, -1)
+ }
+ }
+
+ def retryPort(next: Int => Int, maxRetries: Int): (T, Int) = {
+
+ for (offset <- 0 to maxRetries) {
+ val tryPort = next(offset)
+ try {
+ val (service, port) = startService(tryPort)
+ logInfo(s"Successfully started service$serviceString on port
$port.")
+ return (service, port)
+ } catch {
+ case e: Exception if isBindCollision(e) =>
+ if (offset >= maxRetries) {
+ val exceptionMessage =
+ s"${e.getMessage}: Service$serviceString failed after
$maxRetries retries!"
+ val exception = new BindException(exceptionMessage)
+ // restore original stack trace
+ exception.setStackTrace(e.getStackTrace)
+ throw exception
+ }
+ logWarning(s"Service$serviceString could not bind on port
$tryPort.")
+ }
+ }
+ // Should never happen
+ throw new SparkException(s"Failed to start service$serviceString on
port $startPort")
+ }
+
+ def startFromAvailable(rand: Boolean = false): (T, Int) = {
+
+ val ports = portRangeToList(getPortRangeRestriction(conf).get) //
checked above for empty
+
+ val filteredPorts = ports.map(r => (r._1 to r._2).toList).reduce(_
++ _).
+ distinct.filterNot(_ == startPort)
+ val maxRetries = Math.min(portMaxRetries(conf), filteredPorts.size)
+
+ val availPorts = {
+ if (rand) {
+ scala.util.Random.shuffle(filteredPorts.take(maxRetries)).toArray
+ } else {
+ filteredPorts.sorted.toArray
+ }
+ }
+
+ val tryPort = (x: Int) => availPorts(x).toInt
+
+ retryPort(tryPort, maxRetries)
+ }
+
require(startPort == 0 || (1024 <= startPort && startPort < 65536),
"startPort should be between 1024 and 65535 (inclusive), or 0 for a
random free port.")
- val serviceString = if (serviceName.isEmpty) "" else s" '$serviceName'"
- val maxRetries = portMaxRetries(conf)
- for (offset <- 0 to maxRetries) {
+ if (hasPortRangeRestriction(conf)) {
+
+ if (startPort != 0) {
+
+ val (service, port) = startOnce(startPort)
+
+ if (port != -1) {
+ (service.get, port)
+ } else {
+ // try other
+ startFromAvailable()
+ }
+ }
+ else {
--- End diff --
`else` should go on the line above, as in Java.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]