Skip to content

Commit 066dc7a

Browse files
committed
Fix up HttpServer port increments
1 parent cad16da commit 066dc7a

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ private[spark] class HttpServer(resourceBase: File,
4747
private var server: Server = null
4848
private var port: Int = localPort
4949

50-
private def startOnPort(startPort: Int) {
50+
private def startOnPort(startPort: Int): Tuple2[Server,Int] = {
5151
val server = new Server()
5252
val connector = new SocketConnector
5353
connector.setMaxIdleTime(60*1000)
@@ -81,29 +81,33 @@ private[spark] class HttpServer(resourceBase: File,
8181
return (server, actualPort)
8282
}
8383

84-
private def startWithIncrements(startPort: Int, maxTries: Int) {
85-
for( tryPort <- startPort until (startPort+maxTries)) {
84+
private def startWithIncrements(startPort: Int, maxRetries: Int): Tuple2[Server,Int] = {
85+
for( offset <- 0 to maxRetries) {
8686
try {
87-
val (server, actualPort) = startOnPort(startPort)
87+
val (server, actualPort) = startOnPort(startPort+offset)
8888
return (server, actualPort)
8989
} catch {
9090
case e: java.net.BindException => {
91-
if (!e.getMessage.contains("Address already in use")) {
91+
if (!e.getMessage.contains("Address already in use") ||
92+
offset == (maxRetries-1)) {
9293
throw e
9394
}
94-
logInfo("Could not bind on port: " + (tryPort))
95+
logInfo("Could not bind on port: " + (startPort+offset))
9596
}
9697
case e: Exception => throw e
9798
}
9899
}
100+
return (null, -1)
99101
}
100102

101103
def start() {
102104
if (server != null) {
103105
throw new ServerStateException("Server is already started")
104106
} else {
105107
logInfo("Starting HTTP Server")
106-
(server, port) = startWithIncrements(localPort, 3)
108+
val (actualServer, actualPort) = startWithIncrements(localPort, 3)
109+
server = actualServer
110+
port = actualPort
107111
}
108112
}
109113

0 commit comments

Comments
 (0)