@@ -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