-
Notifications
You must be signed in to change notification settings - Fork 29.1k
[SPARK-5760][SPARK-5761] Fix standalone rest protocol corner cases + revamp tests #4557
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
00999a8
Revamp tests + fix a few corner cases
ea48f65
Merge branch 'master' of github.com:apache/spark into rest-tests
d82d971
v1 -> serverVersion
578cf45
Clean up test code a little
cc96993
private[spark] -> private[rest]
b55e40f
Add test for unknown fields
b4dc980
Merge branch 'master' of github.com:apache/spark into rest-tests
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,15 +17,14 @@ | |
|
|
||
| package org.apache.spark.deploy.rest | ||
|
|
||
| import java.io.{DataOutputStream, File} | ||
| import java.io.File | ||
| import java.net.InetSocketAddress | ||
| import javax.servlet.http.{HttpServlet, HttpServletRequest, HttpServletResponse} | ||
|
|
||
| import scala.io.Source | ||
|
|
||
| import akka.actor.ActorRef | ||
| import com.fasterxml.jackson.databind.JsonMappingException | ||
| import com.google.common.base.Charsets | ||
| import com.fasterxml.jackson.core.JsonProcessingException | ||
| import org.eclipse.jetty.server.Server | ||
| import org.eclipse.jetty.servlet.{ServletHolder, ServletContextHandler} | ||
| import org.eclipse.jetty.util.thread.QueuedThreadPool | ||
|
|
@@ -70,14 +69,14 @@ private[spark] class StandaloneRestServer( | |
| import StandaloneRestServer._ | ||
|
|
||
| private var _server: Option[Server] = None | ||
| private val baseContext = s"/$PROTOCOL_VERSION/submissions" | ||
|
|
||
| // A mapping from servlets to the URL prefixes they are responsible for | ||
| private val servletToContext = Map[StandaloneRestServlet, String]( | ||
| new SubmitRequestServlet(masterActor, masterUrl, masterConf) -> s"$baseContext/create/*", | ||
| new KillRequestServlet(masterActor, masterConf) -> s"$baseContext/kill/*", | ||
| new StatusRequestServlet(masterActor, masterConf) -> s"$baseContext/status/*", | ||
| new ErrorServlet -> "/*" // default handler | ||
|
|
||
| // A mapping from URL prefixes to servlets that serve them. Exposed for testing. | ||
| protected val baseContext = s"/$PROTOCOL_VERSION/submissions" | ||
| protected val contextToServlet = Map[String, StandaloneRestServlet]( | ||
| s"$baseContext/create/*" -> new SubmitRequestServlet(masterActor, masterUrl, masterConf), | ||
| s"$baseContext/kill/*" -> new KillRequestServlet(masterActor, masterConf), | ||
| s"$baseContext/status/*" -> new StatusRequestServlet(masterActor, masterConf), | ||
| "/*" -> new ErrorServlet // default handler | ||
| ) | ||
|
|
||
| /** Start the server and return the bound port. */ | ||
|
|
@@ -99,7 +98,7 @@ private[spark] class StandaloneRestServer( | |
| server.setThreadPool(threadPool) | ||
| val mainHandler = new ServletContextHandler | ||
| mainHandler.setContextPath("/") | ||
| servletToContext.foreach { case (servlet, prefix) => | ||
| contextToServlet.foreach { case (prefix, servlet) => | ||
| mainHandler.addServlet(new ServletHolder(servlet), prefix) | ||
| } | ||
| server.setHandler(mainHandler) | ||
|
|
@@ -113,28 +112,15 @@ private[spark] class StandaloneRestServer( | |
| } | ||
| } | ||
|
|
||
| private object StandaloneRestServer { | ||
| private[rest] object StandaloneRestServer { | ||
| val PROTOCOL_VERSION = StandaloneRestClient.PROTOCOL_VERSION | ||
| val SC_UNKNOWN_PROTOCOL_VERSION = 468 | ||
| } | ||
|
|
||
| /** | ||
| * An abstract servlet for handling requests passed to the [[StandaloneRestServer]]. | ||
| */ | ||
| private abstract class StandaloneRestServlet extends HttpServlet with Logging { | ||
|
|
||
| /** Service a request. If an exception is thrown in the process, indicate server error. */ | ||
| protected override def service( | ||
| request: HttpServletRequest, | ||
| response: HttpServletResponse): Unit = { | ||
| try { | ||
| super.service(request, response) | ||
| } catch { | ||
| case e: Exception => | ||
| logError("Exception while handling request", e) | ||
| response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR) | ||
| } | ||
| } | ||
| private[rest] abstract class StandaloneRestServlet extends HttpServlet with Logging { | ||
|
|
||
| /** | ||
| * Serialize the given response message to JSON and send it through the response servlet. | ||
|
|
@@ -146,11 +132,7 @@ private abstract class StandaloneRestServlet extends HttpServlet with Logging { | |
| val message = validateResponse(responseMessage, responseServlet) | ||
| responseServlet.setContentType("application/json") | ||
| responseServlet.setCharacterEncoding("utf-8") | ||
| responseServlet.setStatus(HttpServletResponse.SC_OK) | ||
| val content = message.toJson.getBytes(Charsets.UTF_8) | ||
| val out = new DataOutputStream(responseServlet.getOutputStream) | ||
| out.write(content) | ||
| out.close() | ||
| responseServlet.getWriter.write(message.toJson) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This fixes (1) on SPARK-5760 |
||
| } | ||
|
|
||
| /** | ||
|
|
@@ -186,6 +168,19 @@ private abstract class StandaloneRestServlet extends HttpServlet with Logging { | |
| e | ||
| } | ||
|
|
||
| /** | ||
| * Parse a submission ID from the relative path, assuming it is the first part of the path. | ||
| * For instance, we expect the path to take the form /[submission ID]/maybe/something/else. | ||
| * The returned submission ID cannot be empty. If the path is unexpected, return None. | ||
| */ | ||
| protected def parseSubmissionId(path: String): Option[String] = { | ||
| if (path == null || path.isEmpty) { | ||
| None | ||
| } else { | ||
| path.stripPrefix("/").split("/").headOption.filter(_.nonEmpty) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Validate the response to ensure that it is correctly constructed. | ||
| * | ||
|
|
@@ -209,7 +204,7 @@ private abstract class StandaloneRestServlet extends HttpServlet with Logging { | |
| /** | ||
| * A servlet for handling kill requests passed to the [[StandaloneRestServer]]. | ||
| */ | ||
| private class KillRequestServlet(masterActor: ActorRef, conf: SparkConf) | ||
| private[rest] class KillRequestServlet(masterActor: ActorRef, conf: SparkConf) | ||
| extends StandaloneRestServlet { | ||
|
|
||
| /** | ||
|
|
@@ -219,18 +214,15 @@ private class KillRequestServlet(masterActor: ActorRef, conf: SparkConf) | |
| protected override def doPost( | ||
| request: HttpServletRequest, | ||
| response: HttpServletResponse): Unit = { | ||
| val submissionId = request.getPathInfo.stripPrefix("/") | ||
| val responseMessage = | ||
| if (submissionId.nonEmpty) { | ||
| handleKill(submissionId) | ||
| } else { | ||
| response.setStatus(HttpServletResponse.SC_BAD_REQUEST) | ||
| handleError("Submission ID is missing in kill request.") | ||
| } | ||
| val submissionId = parseSubmissionId(request.getPathInfo) | ||
| val responseMessage = submissionId.map(handleKill).getOrElse { | ||
| response.setStatus(HttpServletResponse.SC_BAD_REQUEST) | ||
| handleError("Submission ID is missing in kill request.") | ||
| } | ||
| sendResponse(responseMessage, response) | ||
| } | ||
|
|
||
| private def handleKill(submissionId: String): KillSubmissionResponse = { | ||
| protected def handleKill(submissionId: String): KillSubmissionResponse = { | ||
| val askTimeout = AkkaUtils.askTimeout(conf) | ||
| val response = AkkaUtils.askWithReply[DeployMessages.KillDriverResponse]( | ||
| DeployMessages.RequestKillDriver(submissionId), masterActor, askTimeout) | ||
|
|
@@ -246,7 +238,7 @@ private class KillRequestServlet(masterActor: ActorRef, conf: SparkConf) | |
| /** | ||
| * A servlet for handling status requests passed to the [[StandaloneRestServer]]. | ||
| */ | ||
| private class StatusRequestServlet(masterActor: ActorRef, conf: SparkConf) | ||
| private[rest] class StatusRequestServlet(masterActor: ActorRef, conf: SparkConf) | ||
| extends StandaloneRestServlet { | ||
|
|
||
| /** | ||
|
|
@@ -256,18 +248,15 @@ private class StatusRequestServlet(masterActor: ActorRef, conf: SparkConf) | |
| protected override def doGet( | ||
| request: HttpServletRequest, | ||
| response: HttpServletResponse): Unit = { | ||
| val submissionId = request.getPathInfo.stripPrefix("/") | ||
| val responseMessage = | ||
| if (submissionId.nonEmpty) { | ||
| handleStatus(submissionId) | ||
| } else { | ||
| response.setStatus(HttpServletResponse.SC_BAD_REQUEST) | ||
| handleError("Submission ID is missing in status request.") | ||
| } | ||
| val submissionId = parseSubmissionId(request.getPathInfo) | ||
| val responseMessage = submissionId.map(handleStatus).getOrElse { | ||
| response.setStatus(HttpServletResponse.SC_BAD_REQUEST) | ||
| handleError("Submission ID is missing in status request.") | ||
| } | ||
| sendResponse(responseMessage, response) | ||
| } | ||
|
|
||
| private def handleStatus(submissionId: String): SubmissionStatusResponse = { | ||
| protected def handleStatus(submissionId: String): SubmissionStatusResponse = { | ||
| val askTimeout = AkkaUtils.askTimeout(conf) | ||
| val response = AkkaUtils.askWithReply[DeployMessages.DriverStatusResponse]( | ||
| DeployMessages.RequestDriverStatus(submissionId), masterActor, askTimeout) | ||
|
|
@@ -287,7 +276,7 @@ private class StatusRequestServlet(masterActor: ActorRef, conf: SparkConf) | |
| /** | ||
| * A servlet for handling submit requests passed to the [[StandaloneRestServer]]. | ||
| */ | ||
| private class SubmitRequestServlet( | ||
| private[rest] class SubmitRequestServlet( | ||
| masterActor: ActorRef, | ||
| masterUrl: String, | ||
| conf: SparkConf) | ||
|
|
@@ -313,7 +302,7 @@ private class SubmitRequestServlet( | |
| handleSubmit(requestMessageJson, requestMessage, responseServlet) | ||
| } catch { | ||
| // The client failed to provide a valid JSON, so this is not our fault | ||
| case e @ (_: JsonMappingException | _: SubmitRestProtocolException) => | ||
| case e @ (_: JsonProcessingException | _: SubmitRestProtocolException) => | ||
| responseServlet.setStatus(HttpServletResponse.SC_BAD_REQUEST) | ||
| handleError("Malformed request: " + formatException(e)) | ||
| } | ||
|
|
@@ -413,7 +402,7 @@ private class ErrorServlet extends StandaloneRestServlet { | |
| request: HttpServletRequest, | ||
| response: HttpServletResponse): Unit = { | ||
| val path = request.getPathInfo | ||
| val parts = path.stripPrefix("/").split("/").toSeq | ||
| val parts = path.stripPrefix("/").split("/").filter(_.nonEmpty).toList | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This fixes (3) on SPARK-5760 |
||
| var versionMismatch = false | ||
| var msg = | ||
| parts match { | ||
|
|
@@ -423,10 +412,10 @@ private class ErrorServlet extends StandaloneRestServlet { | |
| case `serverVersion` :: Nil => | ||
| // http://host:port/correct-version | ||
| "Missing the /submissions prefix." | ||
| case `serverVersion` :: "submissions" :: Nil => | ||
| // http://host:port/correct-version/submissions | ||
| case `serverVersion` :: "submissions" :: tail => | ||
| // http://host:port/correct-version/submissions/* | ||
| "Missing an action: please specify one of /create, /kill, or /status." | ||
| case unknownVersion :: _ => | ||
| case unknownVersion :: tail => | ||
| // http://host:port/unknown-version/* | ||
| versionMismatch = true | ||
| s"Unknown protocol version '$unknownVersion'." | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fixes (2) on SPARK-5760