@@ -19,6 +19,7 @@ package org.apache.spark
1919
2020import java .io .File
2121
22+ import org .apache .spark .network .PortManager
2223import org .eclipse .jetty .util .security .{Constraint , Password }
2324import org .eclipse .jetty .security .authentication .DigestAuthenticator
2425import org .eclipse .jetty .security .{ConstraintMapping , ConstraintSecurityHandler , HashLoginService , SecurityHandler }
@@ -41,45 +42,54 @@ private[spark] class ServerStateException(message: String) extends Exception(mes
4142 * as well as classes created by the interpreter when the user types in code. This is just a wrapper
4243 * around a Jetty server.
4344 */
44- private [spark] class HttpServer (resourceBase : File , securityManager : SecurityManager )
45- extends Logging {
45+ private [spark] class HttpServer (resourceBase : File ,
46+ securityManager : SecurityManager ,
47+ localPort : Int = 0 ) extends Logging {
4648 private var server : Server = null
47- private var port : Int = - 1
49+ private var port : Int = localPort
50+
51+ private def startOnPort (startPort : Int ): (Server , Int ) = {
52+ val server = new Server ()
53+ val connector = new SocketConnector
54+ connector.setMaxIdleTime(60 * 1000 )
55+ connector.setSoLingerTime(- 1 )
56+ connector.setPort(startPort)
57+ server.addConnector(connector)
58+
59+ val threadPool = new QueuedThreadPool
60+ threadPool.setDaemon(true )
61+ server.setThreadPool(threadPool)
62+ val resHandler = new ResourceHandler
63+ resHandler.setResourceBase(resourceBase.getAbsolutePath)
64+
65+ val handlerList = new HandlerList
66+ handlerList.setHandlers(Array (resHandler, new DefaultHandler ))
67+
68+ if (securityManager.isAuthenticationEnabled()) {
69+ logDebug(" HttpServer is using security" )
70+ val sh = setupSecurityHandler(securityManager)
71+ // make sure we go through security handler to get resources
72+ sh.setHandler(handlerList)
73+ server.setHandler(sh)
74+ } else {
75+ logDebug(" HttpServer is not using security" )
76+ server.setHandler(handlerList)
77+ }
78+
79+ server.start()
80+ val actualPort = server.getConnectors()(0 ).getLocalPort()
81+
82+ (server, actualPort)
83+ }
4884
4985 def start () {
5086 if (server != null ) {
5187 throw new ServerStateException (" Server is already started" )
5288 } else {
5389 logInfo(" Starting HTTP Server" )
54- server = new Server ()
55- val connector = new SocketConnector
56- connector.setMaxIdleTime(60 * 1000 )
57- connector.setSoLingerTime(- 1 )
58- connector.setPort(0 )
59- server.addConnector(connector)
60-
61- val threadPool = new QueuedThreadPool
62- threadPool.setDaemon(true )
63- server.setThreadPool(threadPool)
64- val resHandler = new ResourceHandler
65- resHandler.setResourceBase(resourceBase.getAbsolutePath)
66-
67- val handlerList = new HandlerList
68- handlerList.setHandlers(Array (resHandler, new DefaultHandler ))
69-
70- if (securityManager.isAuthenticationEnabled()) {
71- logDebug(" HttpServer is using security" )
72- val sh = setupSecurityHandler(securityManager)
73- // make sure we go through security handler to get resources
74- sh.setHandler(handlerList)
75- server.setHandler(sh)
76- } else {
77- logDebug(" HttpServer is not using security" )
78- server.setHandler(handlerList)
79- }
80-
81- server.start()
82- port = server.getConnectors()(0 ).getLocalPort()
90+ val (actualServer, actualPort) = PortManager .startWithIncrements(localPort, 3 , startOnPort)
91+ server = actualServer
92+ port = actualPort
8393 }
8494 }
8595
0 commit comments