From ecb1af576e5e0be9f67cf0375f998d1fa7ac3a07 Mon Sep 17 00:00:00 2001 From: Matei Zaharia Date: Fri, 15 Oct 2010 19:04:18 -0700 Subject: Moved ClassServer out of repl packaged and renamed it to HttpServer. --- src/scala/spark/HttpServer.scala | 75 ++++++++++++++++++++++++++++ src/scala/spark/repl/ClassServer.scala | 77 ----------------------------- src/scala/spark/repl/SparkInterpreter.scala | 6 ++- 3 files changed, 79 insertions(+), 79 deletions(-) create mode 100644 src/scala/spark/HttpServer.scala delete mode 100644 src/scala/spark/repl/ClassServer.scala diff --git a/src/scala/spark/HttpServer.scala b/src/scala/spark/HttpServer.scala new file mode 100644 index 0000000000..55fb0a2218 --- /dev/null +++ b/src/scala/spark/HttpServer.scala @@ -0,0 +1,75 @@ +package spark + +import java.io.File +import java.net.InetAddress + +import org.eclipse.jetty.server.Server +import org.eclipse.jetty.server.handler.DefaultHandler +import org.eclipse.jetty.server.handler.HandlerList +import org.eclipse.jetty.server.handler.ResourceHandler + + +/** + * Exception type thrown by HttpServer when it is in the wrong state + * for an operation. + */ +class ServerStateException(message: String) extends Exception(message) + + +/** + * An HTTP server for static content used to allow worker nodes to access JARs + * added to SparkContext as well as classes created by the interpreter when + * the user types in code. This is just a wrapper around a Jetty server. + */ +class HttpServer(resourceBase: File) extends Logging { + private var server: Server = null + private var port: Int = -1 + + def start() { + if (server != null) { + throw new ServerStateException("Server is already started") + } else { + server = new Server(0) + val resHandler = new ResourceHandler + resHandler.setResourceBase(resourceBase.getAbsolutePath) + val handlerList = new HandlerList + handlerList.setHandlers(Array(resHandler, new DefaultHandler)) + server.setHandler(handlerList) + server.start() + port = server.getConnectors()(0).getLocalPort() + logDebug("HttpServer started at " + uri) + } + } + + def stop() { + if (server == null) { + throw new ServerStateException("Server is already stopped") + } else { + server.stop() + port = -1 + server = null + } + } + + /** + * Get the URI of this HTTP server (http://host:port) + */ + def uri: String = { + if (server == null) { + throw new ServerStateException("Server is not started") + } else { + return "http://" + getLocalIpAddress + ":" + port + } + } + + /** + * Get the local host's IP address in dotted-quad format (e.g. 1.2.3.4) + */ + private def getLocalIpAddress: String = { + // Get local IP as an array of four bytes + val bytes = InetAddress.getLocalHost().getAddress() + // Convert the bytes to ints (keeping in mind that they may be negative) + // and join them into a string + return bytes.map(b => (b.toInt + 256) % 256).mkString(".") + } +} diff --git a/src/scala/spark/repl/ClassServer.scala b/src/scala/spark/repl/ClassServer.scala deleted file mode 100644 index 6a40d92765..0000000000 --- a/src/scala/spark/repl/ClassServer.scala +++ /dev/null @@ -1,77 +0,0 @@ -package spark.repl - -import java.io.File -import java.net.InetAddress - -import org.eclipse.jetty.server.Server -import org.eclipse.jetty.server.handler.DefaultHandler -import org.eclipse.jetty.server.handler.HandlerList -import org.eclipse.jetty.server.handler.ResourceHandler - -import spark.Logging - - -/** - * Exception type thrown by ClassServer when it is in the wrong state - * for an operation. - */ -class ServerStateException(message: String) extends Exception(message) - - -/** - * An HTTP server used by the interpreter to allow worker nodes to access - * class files created as the user types in lines of code. This is just a - * wrapper around a Jetty embedded HTTP server. - */ -class ClassServer(classDir: File) extends Logging { - private var server: Server = null - private var port: Int = -1 - - def start() { - if (server != null) { - throw new ServerStateException("Server is already started") - } else { - server = new Server(0) - val resHandler = new ResourceHandler - resHandler.setResourceBase(classDir.getAbsolutePath) - val handlerList = new HandlerList - handlerList.setHandlers(Array(resHandler, new DefaultHandler)) - server.setHandler(handlerList) - server.start() - port = server.getConnectors()(0).getLocalPort() - logDebug("ClassServer started at " + uri) - } - } - - def stop() { - if (server == null) { - throw new ServerStateException("Server is already stopped") - } else { - server.stop() - port = -1 - server = null - } - } - - /** - * Get the URI of this HTTP server (http://host:port) - */ - def uri: String = { - if (server == null) { - throw new ServerStateException("Server is not started") - } else { - return "http://" + getLocalIpAddress + ":" + port - } - } - - /** - * Get the local host's IP address in dotted-quad format (e.g. 1.2.3.4) - */ - private def getLocalIpAddress: String = { - // Get local IP as an array of four bytes - val bytes = InetAddress.getLocalHost().getAddress() - // Convert the bytes to ints (keeping in mind that they may be negative) - // and join them into a string - return bytes.map(b => (b.toInt + 256) % 256).mkString(".") - } -} diff --git a/src/scala/spark/repl/SparkInterpreter.scala b/src/scala/spark/repl/SparkInterpreter.scala index ae2e7e8a68..41324333a3 100644 --- a/src/scala/spark/repl/SparkInterpreter.scala +++ b/src/scala/spark/repl/SparkInterpreter.scala @@ -36,6 +36,8 @@ import scala.tools.nsc.{ InterpreterResults => IR } import interpreter._ import SparkInterpreter._ +import spark.HttpServer + /**

* An interpreter for Scala code. *

@@ -120,14 +122,14 @@ class SparkInterpreter(val settings: Settings, out: PrintWriter) { val virtualDirectory = new PlainFile(outputDir) /** Jetty server that will serve our classes to worker nodes */ - val classServer = new ClassServer(outputDir) + val classServer = new HttpServer(outputDir) // Start the classServer and store its URI in a spark system property // (which will be passed to executors so that they can connect to it) classServer.start() System.setProperty("spark.repl.class.uri", classServer.uri) if (SPARK_DEBUG_REPL) { - println("ClassServer started, URI = " + classServer.uri) + println("Class server started, URI = " + classServer.uri) } /** reporter */ -- cgit v1.2.3