Skip to content

Commit

Permalink
Fix up HttpServer port increments
Browse files Browse the repository at this point in the history
  • Loading branch information
ash211 committed Jul 25, 2014
1 parent cad16da commit 066dc7a
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions core/src/main/scala/org/apache/spark/HttpServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private[spark] class HttpServer(resourceBase: File,
private var server: Server = null
private var port: Int = localPort

private def startOnPort(startPort: Int) {
private def startOnPort(startPort: Int): Tuple2[Server,Int] = {
val server = new Server()
val connector = new SocketConnector
connector.setMaxIdleTime(60*1000)
Expand Down Expand Up @@ -81,29 +81,33 @@ private[spark] class HttpServer(resourceBase: File,
return (server, actualPort)
}

private def startWithIncrements(startPort: Int, maxTries: Int) {
for( tryPort <- startPort until (startPort+maxTries)) {
private def startWithIncrements(startPort: Int, maxRetries: Int): Tuple2[Server,Int] = {
for( offset <- 0 to maxRetries) {
try {
val (server, actualPort) = startOnPort(startPort)
val (server, actualPort) = startOnPort(startPort+offset)
return (server, actualPort)
} catch {
case e: java.net.BindException => {
if (!e.getMessage.contains("Address already in use")) {
if (!e.getMessage.contains("Address already in use") ||
offset == (maxRetries-1)) {
throw e
}
logInfo("Could not bind on port: " + (tryPort))
logInfo("Could not bind on port: " + (startPort+offset))
}
case e: Exception => throw e
}
}
return (null, -1)
}

def start() {
if (server != null) {
throw new ServerStateException("Server is already started")
} else {
logInfo("Starting HTTP Server")
(server, port) = startWithIncrements(localPort, 3)
val (actualServer, actualPort) = startWithIncrements(localPort, 3)
server = actualServer
port = actualPort
}
}

Expand Down

0 comments on commit 066dc7a

Please sign in to comment.