Skip to content

Commit 73fbe89

Browse files
committed
Move start service logic to Utils
1 parent ec676f4 commit 73fbe89

File tree

5 files changed

+38
-67
lines changed

5 files changed

+38
-67
lines changed

core/src/main/scala/org/apache/spark/HttpServer.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ package org.apache.spark
1919

2020
import java.io.File
2121

22-
import org.apache.spark.network.PortManager
2322
import org.eclipse.jetty.util.security.{Constraint, Password}
2423
import org.eclipse.jetty.security.authentication.DigestAuthenticator
25-
import org.eclipse.jetty.security.{ConstraintMapping, ConstraintSecurityHandler, HashLoginService, SecurityHandler}
24+
import org.eclipse.jetty.security.{ConstraintMapping, ConstraintSecurityHandler, HashLoginService}
2625

2726
import org.eclipse.jetty.server.Server
2827
import org.eclipse.jetty.server.bio.SocketConnector
@@ -87,7 +86,7 @@ private[spark] class HttpServer(resourceBase: File,
8786
throw new ServerStateException("Server is already started")
8887
} else {
8988
logInfo("Starting HTTP Server")
90-
val (actualServer, actualPort) = PortManager.startWithIncrements(localPort, 3, startOnPort)
89+
val (actualServer, actualPort) = Utils.startServiceOnPort(localPort, 3, startOnPort)
9190
server = actualServer
9291
port = actualPort
9392
}

core/src/main/scala/org/apache/spark/network/ConnectionManager.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ private[spark] class ConnectionManager(port: Int, conf: SparkConf,
109109
serverChannel.socket.bind(new InetSocketAddress(port))
110110
(serverChannel, port)
111111
}
112-
PortManager.startWithIncrements(port, 3, startService)
112+
Utils.startServiceOnPort(port, 3, startService)
113113
serverChannel.register(selector, SelectionKey.OP_ACCEPT)
114114

115115
val id = new ConnectionManagerId(Utils.localHostName, serverChannel.socket.getLocalPort)

core/src/main/scala/org/apache/spark/network/PortManager.scala

Lines changed: 0 additions & 60 deletions
This file was deleted.

core/src/main/scala/org/apache/spark/ui/JettyUtils.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
package org.apache.spark.ui
1919

20-
import java.net.{InetSocketAddress, URL}
20+
import java.net.{BindException, InetSocketAddress, URL}
2121
import javax.servlet.DispatcherType
2222
import javax.servlet.http.{HttpServlet, HttpServletRequest, HttpServletResponse}
2323

@@ -33,7 +33,7 @@ import org.eclipse.jetty.util.thread.QueuedThreadPool
3333
import org.json4s.JValue
3434
import org.json4s.jackson.JsonMethods.{pretty, render}
3535

36-
import org.apache.spark.{Logging, SecurityManager, SparkConf}
36+
import org.apache.spark.{Logging, SecurityManager, SparkConf, SparkException}
3737
import org.apache.spark.util.Utils
3838

3939
/**

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
package org.apache.spark.util
1919

2020
import java.io._
21-
import java.net.{InetAddress, Inet4Address, NetworkInterface, URI, URL, URLConnection}
21+
import java.net._
2222
import java.nio.ByteBuffer
2323
import java.util.{Locale, Random, UUID}
2424
import java.util.concurrent.{ThreadFactory, ConcurrentHashMap, Executors, ThreadPoolExecutor}
@@ -1331,4 +1331,36 @@ private[spark] object Utils extends Logging {
13311331
.map { case (k, v) => s"-D$k=$v" }
13321332
}
13331333

1334+
/**
1335+
* Attempt to start a service on the given port, or fail after a number of attempts.
1336+
* Each subsequent attempt uses 1 + the port used in the previous attempt.
1337+
*
1338+
* @param startPort The initial port to start the service on.
1339+
* @param maxRetries Maximum number of retries to attempt.
1340+
* A value of 3 means attempting ports n, n+1, n+2, and n+3, for example.
1341+
* @param startService Function to start service on a given port.
1342+
* This is expected to throw java.net.BindException on port collision.
1343+
* @throws SparkException When unable to start service in the given number of attempts
1344+
* @return
1345+
*/
1346+
def startServiceOnPort[T](
1347+
startPort: Int,
1348+
maxRetries: Int,
1349+
startService: Int => (T, Int)): (T, Int) = {
1350+
for (offset <- 0 to maxRetries) {
1351+
val tryPort = (startPort + offset) % 65536
1352+
try {
1353+
return startService(tryPort)
1354+
} catch {
1355+
case e: BindException =>
1356+
if (!e.getMessage.contains("Address already in use") || offset >= maxRetries) {
1357+
throw e
1358+
}
1359+
logInfo("Could not bind on port: " + tryPort + ". Attempting port " + (tryPort + 1))
1360+
}
1361+
}
1362+
// Should never happen
1363+
throw new SparkException(s"Couldn't start service on port $startPort")
1364+
}
1365+
13341366
}

0 commit comments

Comments
 (0)