1717
1818package org .apache .spark .deploy .rest
1919
20- import java .io .DataOutputStream
2120import java .net .URL
22- import java .net .HttpURLConnection
23-
24- import scala .io .Source
25-
26- import com .google .common .base .Charsets
2721
2822import org .apache .spark .{SPARK_VERSION => sparkVersion }
2923import org .apache .spark .deploy .SparkSubmitArguments
3024import org .apache .spark .util .Utils
3125
3226/**
33- * A client that submits Spark applications using a stable REST protocol in standalone
34- * cluster mode. This client is intended to communicate with the StandaloneRestServer.
27+ * A client that submits Spark applications to the standalone Master using a stable
28+ * REST protocol. This client is intended to communicate with the StandaloneRestServer,
29+ * and currently only used in cluster mode.
3530 */
36- private [spark] class StandaloneRestClient {
37-
38- def submitDriver (args : SparkSubmitArguments ): Unit = {
39- validateSubmitArguments(args)
40- val url = getHttpUrl(args.master)
41- val request = constructSubmitRequest(args)
42- val response = sendHttp(url, request)
43- println(response.toJson)
44- }
45-
46- def killDriver (master : String , driverId : String ): Unit = {
47- validateMaster(master)
48- val url = getHttpUrl(master)
49- val request = constructKillRequest(master, driverId)
50- val response = sendHttp(url, request)
51- println(response.toJson)
52- }
53-
54- def requestDriverStatus (master : String , driverId : String ): Unit = {
55- validateMaster(master)
56- val url = getHttpUrl(master)
57- val request = constructStatusRequest(master, driverId)
58- val response = sendHttp(url, request)
59- println(response.toJson)
60- }
31+ private [spark] class StandaloneRestClient extends SubmitRestClient {
6132
62- /**
63- * Construct a submit driver request message.
64- */
65- private def constructSubmitRequest (args : SparkSubmitArguments ): SubmitDriverRequestMessage = {
33+ /** Construct a submit driver request message. */
34+ override protected def constructSubmitRequest (
35+ args : SparkSubmitArguments ): SubmitDriverRequestMessage = {
6636 import SubmitDriverRequestField ._
6737 val driverMemory = Option (args.driverMemory)
6838 .map { m => Utils .memoryStringToMb(m).toString }
@@ -78,7 +48,6 @@ private[spark] class StandaloneRestClient {
7848 .setFieldIfNotNull(MAIN_CLASS , args.mainClass)
7949 .setFieldIfNotNull(JARS , args.jars)
8050 .setFieldIfNotNull(FILES , args.files)
81- .setFieldIfNotNull(PY_FILES , args.pyFiles)
8251 .setFieldIfNotNull(DRIVER_MEMORY , driverMemory)
8352 .setFieldIfNotNull(DRIVER_CORES , args.driverCores)
8453 .setFieldIfNotNull(DRIVER_EXTRA_JAVA_OPTIONS , args.driverExtraJavaOptions)
@@ -97,10 +66,8 @@ private[spark] class StandaloneRestClient {
9766 message.validate()
9867 }
9968
100- /**
101- * Construct a kill driver request message.
102- */
103- private def constructKillRequest (
69+ /** Construct a kill driver request message. */
70+ override protected def constructKillRequest (
10471 master : String ,
10572 driverId : String ): KillDriverRequestMessage = {
10673 import KillDriverRequestField ._
@@ -111,10 +78,8 @@ private[spark] class StandaloneRestClient {
11178 .validate()
11279 }
11380
114- /**
115- * Construct a driver status request message.
116- */
117- private def constructStatusRequest (
81+ /** Construct a driver status request message. */
82+ override protected def constructStatusRequest (
11883 master : String ,
11984 driverId : String ): DriverStatusRequestMessage = {
12085 import DriverStatusRequestField ._
@@ -125,67 +90,23 @@ private[spark] class StandaloneRestClient {
12590 .validate()
12691 }
12792
128- /**
129- * Send the provided request in an HTTP message to the given URL.
130- * Return the response received from the REST server.
131- */
132- private def sendHttp (
133- url : URL ,
134- request : StandaloneRestProtocolMessage ): StandaloneRestProtocolMessage = {
135- val conn = url.openConnection().asInstanceOf [HttpURLConnection ]
136- conn.setRequestMethod(" POST" )
137- conn.setRequestProperty(" Content-Type" , " application/json" )
138- conn.setRequestProperty(" charset" , " utf-8" )
139- conn.setDoOutput(true )
140- println(" Sending this JSON blob to server:\n " + request.toJson)
141- val content = request.toJson.getBytes(Charsets .UTF_8 )
142- val out = new DataOutputStream (conn.getOutputStream)
143- out.write(content)
144- out.close()
145- val response = Source .fromInputStream(conn.getInputStream).mkString
146- StandaloneRestProtocolMessage .fromJson(response)
147- }
148-
149- /**
150- * Throw an exception if this is not standalone cluster mode.
151- */
152- private def validateSubmitArguments (args : SparkSubmitArguments ): Unit = {
153- validateMaster(args.master)
154- validateDeployMode(args.deployMode)
155- }
156-
157- /**
158- * Throw an exception if this is not standalone mode.
159- */
160- private def validateMaster (master : String ): Unit = {
93+ /** Throw an exception if this is not standalone mode. */
94+ override protected def validateMaster (master : String ): Unit = {
16195 if (! master.startsWith(" spark://" )) {
16296 throw new IllegalArgumentException (" This REST client is only supported in standalone mode." )
16397 }
16498 }
16599
166- /**
167- * Throw an exception if this is not cluster deploy mode.
168- */
169- private def validateDeployMode (deployMode : String ): Unit = {
100+ /** Throw an exception if this is not cluster deploy mode. */
101+ override protected def validateDeployMode (deployMode : String ): Unit = {
170102 if (deployMode != " cluster" ) {
171103 throw new IllegalArgumentException (" This REST client is only supported in cluster mode." )
172104 }
173105 }
174106
175- /**
176- * Extract the URL portion of the master address.
177- */
178- private def getHttpUrl (master : String ): URL = {
107+ /** Extract the URL portion of the master address. */
108+ override protected def getHttpUrl (master : String ): URL = {
179109 validateMaster(master)
180110 new URL (" http://" + master.stripPrefix(" spark://" ))
181111 }
182112}
183-
184- object StandaloneRestClient {
185- def main (args : Array [String ]): Unit = {
186- assert(args.length > 0 )
187- // val client = new StandaloneRestClient
188- // client.submitDriver("spark://" + args(0))
189- println(" Done." )
190- }
191- }
0 commit comments