@@ -22,6 +22,7 @@ import java.net.{HttpURLConnection, SocketException, URL}
2222
2323import scala .io .Source
2424
25+ import com .fasterxml .jackson .databind .JsonMappingException
2526import com .google .common .base .Charsets
2627
2728import org .apache .spark .{Logging , SparkConf , SPARK_VERSION => sparkVersion }
@@ -54,15 +55,15 @@ private[spark] class StandaloneRestClient extends Logging {
5455 import StandaloneRestClient ._
5556
5657 /**
57- * Submit an application specified by the provided arguments .
58+ * Submit an application specified by the parameters in the provided request .
5859 *
5960 * If the submission was successful, poll the status of the submission and report
6061 * it to the user. Otherwise, report the error message provided by the server.
6162 */
6263 def createSubmission (
6364 master : String ,
6465 request : CreateSubmissionRequest ): SubmitRestProtocolResponse = {
65- logInfo(s " Submitting a request to launch a driver in $master. " )
66+ logInfo(s " Submitting a request to launch an application in $master. " )
6667 validateMaster(master)
6768 val url = getSubmitUrl(master)
6869 val response = postJson(url, request.toJson)
@@ -103,6 +104,24 @@ private[spark] class StandaloneRestClient extends Logging {
103104 response
104105 }
105106
107+ /** Construct a message that captures the specified parameters for submitting an application. */
108+ def constructSubmitRequest (
109+ appResource : String ,
110+ mainClass : String ,
111+ appArgs : Array [String ],
112+ sparkProperties : Map [String , String ],
113+ environmentVariables : Map [String , String ]): CreateSubmissionRequest = {
114+ val message = new CreateSubmissionRequest
115+ message.clientSparkVersion = sparkVersion
116+ message.appResource = appResource
117+ message.mainClass = mainClass
118+ message.appArgs = appArgs
119+ message.sparkProperties = sparkProperties
120+ message.environmentVariables = environmentVariables
121+ message.validate()
122+ message
123+ }
124+
106125 /** Send a GET request to the specified URL. */
107126 private def get (url : URL ): SubmitRestProtocolResponse = {
108127 logDebug(s " Sending GET request to server at $url. " )
@@ -134,40 +153,33 @@ private[spark] class StandaloneRestClient extends Logging {
134153 }
135154
136155 /**
137- * Read the response from the given connection.
138- *
139- * The response is expected to represent a [[SubmitRestProtocolResponse ]] in the form of JSON.
140- * Additionally, this validates the response to ensure that it is properly constructed.
141- * If the response represents an error, report the message from the server.
156+ * Read the response from the server and return it as a validated [[SubmitRestProtocolResponse ]].
157+ * If the response represents an error, report the embedded message to the user.
142158 */
143159 private def readResponse (connection : HttpURLConnection ): SubmitRestProtocolResponse = {
144160 try {
145161 val responseJson = Source .fromInputStream(connection.getInputStream).mkString
146162 logDebug(s " Response from the server: \n $responseJson" )
147163 val response = SubmitRestProtocolMessage .fromJson(responseJson)
148- // The response should have already been validated on the server.
149- // In case this is not true, validate it ourselves to avoid potential NPEs.
150- try {
151- response.validate()
152- } catch {
153- case e : SubmitRestProtocolException =>
154- throw new SubmitRestProtocolException (" Malformed response received from server" , e)
155- }
156- // If the response is an error, log the message
157- // Otherwise, simply return the response
164+ response.validate()
158165 response match {
166+ // If the response is an error, log the message
159167 case error : ErrorResponse =>
160168 logError(s " Server responded with error: \n ${error.message}" )
161169 error
170+ // Otherwise, simply return the response
162171 case response : SubmitRestProtocolResponse => response
163172 case unexpected =>
164173 throw new SubmitRestProtocolException (
165174 s " Message received from server was not a response: \n ${unexpected.toJson}" )
166175 }
167176 } catch {
168- case e @ (_ : FileNotFoundException | _ : SocketException ) =>
177+ case unreachable @ (_ : FileNotFoundException | _ : SocketException ) =>
169178 throw new SubmitRestConnectionException (
170- s " Unable to connect to server ${connection.getURL}" , e)
179+ s " Unable to connect to server ${connection.getURL}" , unreachable)
180+ case malformed @ (_ : SubmitRestProtocolException | _ : JsonMappingException ) =>
181+ throw new SubmitRestProtocolException (
182+ " Malformed response received from server" , malformed)
171183 }
172184 }
173185
@@ -202,24 +214,6 @@ private[spark] class StandaloneRestClient extends Logging {
202214 }
203215 }
204216
205- /** Construct a message that captures the specified parameters for submitting an application. */
206- def constructSubmitRequest (
207- appResource : String ,
208- mainClass : String ,
209- appArgs : Array [String ],
210- sparkProperties : Map [String , String ],
211- environmentVariables : Map [String , String ]): CreateSubmissionRequest = {
212- val message = new CreateSubmissionRequest
213- message.clientSparkVersion = sparkVersion
214- message.appResource = appResource
215- message.mainClass = mainClass
216- message.appArgs = appArgs
217- message.sparkProperties = sparkProperties
218- message.environmentVariables = environmentVariables
219- message.validate()
220- message
221- }
222-
223217 /** Report the status of a newly created submission. */
224218 private def reportSubmissionStatus (
225219 master : String ,
@@ -292,7 +286,7 @@ private[spark] object StandaloneRestClient {
292286 val REPORT_DRIVER_STATUS_MAX_TRIES = 10
293287 val PROTOCOL_VERSION = " v1"
294288
295- /** Submit an application, assuming parameters are specified through system properties. */
289+ /** Submit an application, assuming Spark parameters are specified through system properties. */
296290 def main (args : Array [String ]): Unit = {
297291 if (args.size < 2 ) {
298292 sys.error(" Usage: StandaloneRestClient [app resource] [main class] [app args*]" )
@@ -301,12 +295,13 @@ private[spark] object StandaloneRestClient {
301295 val appResource = args(0 )
302296 val mainClass = args(1 )
303297 val appArgs = args.slice(2 , args.size)
304- val client = new StandaloneRestClient
305- val master = sys.props.get (" spark.master" ).getOrElse {
298+ val conf = new SparkConf
299+ val master = conf.getOption (" spark.master" ).getOrElse {
306300 throw new IllegalArgumentException (" 'spark.master' must be set." )
307301 }
308- val sparkProperties = new SparkConf () .getAll.toMap
302+ val sparkProperties = conf .getAll.toMap
309303 val environmentVariables = sys.env.filter { case (k, _) => k.startsWith(" SPARK_" ) }
304+ val client = new StandaloneRestClient
310305 val submitRequest = client.constructSubmitRequest(
311306 appResource, mainClass, appArgs, sparkProperties, environmentVariables)
312307 client.createSubmission(master, submitRequest)
0 commit comments