-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
SparkSubmitProcessEngineConnLaunchBuilder.scala
465 lines (385 loc) · 16.4 KB
/
SparkSubmitProcessEngineConnLaunchBuilder.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/*
* Copyright 2019 WeBank
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.webank.wedatasphere.linkis.engineplugin.spark.launch
import java.lang.ProcessBuilder.Redirect
import java.util
import com.google.common.collect.Lists
import com.webank.wedatasphere.linkis.common.conf.CommonVars
import com.webank.wedatasphere.linkis.engineplugin.spark.config.SparkResourceConfiguration.LINKIS_SPARK_DRIVER_MEMORY
import com.webank.wedatasphere.linkis.engineplugin.spark.config.{SparkConfiguration, SparkResourceConfiguration}
import com.webank.wedatasphere.linkis.engineplugin.spark.launch.SparkSubmitProcessEngineConnLaunchBuilder.{AbsolutePath, Path, RelativePath, getValueAndRemove}
import com.webank.wedatasphere.linkis.manager.common.entity.resource.{DriverAndYarnResource, NodeResource}
import com.webank.wedatasphere.linkis.manager.engineplugin.common.conf.EnvConfiguration
import com.webank.wedatasphere.linkis.manager.engineplugin.common.launch.entity.EngineConnBuildRequest
import com.webank.wedatasphere.linkis.manager.engineplugin.common.launch.process.Environment._
import com.webank.wedatasphere.linkis.manager.engineplugin.common.launch.process.JavaProcessEngineConnLaunchBuilder
import com.webank.wedatasphere.linkis.manager.label.entity.Label
import com.webank.wedatasphere.linkis.manager.label.entity.engine.UserCreatorLabel
import com.webank.wedatasphere.linkis.protocol.UserWithCreator
import org.apache.commons.lang.StringUtils
import scala.collection.JavaConverters._
import scala.collection.mutable.ArrayBuffer
import com.webank.wedatasphere.linkis.hadoop.common.conf.HadoopConf
/**
*
* @Date 2020/10/23
*/
class SparkSubmitProcessEngineConnLaunchBuilder private extends JavaProcessEngineConnLaunchBuilder {
private[this] val fsRoot = "hdfs://"
protected var port: Int = _
protected var request: EngineConnBuildRequest = _
protected var userEngineResource: NodeResource = _
private[this] var _executable: Path = _
private[this] var _master: Option[String] = None
private[this] var _deployMode: Option[String] = None
private[this] var _className: Option[String] = None
private[this] var _name: Option[String] = None
private[this] var _jars: ArrayBuffer[Path] = ArrayBuffer()
private[this] var _pyFiles: ArrayBuffer[Path] = ArrayBuffer()
private[this] var _files: ArrayBuffer[Path] = ArrayBuffer()
private[this] var _conf: ArrayBuffer[(String, String)] = ArrayBuffer()
private[this] var _driverMemory: Option[String] = None
private[this] var _driverJavaOptions: Option[String] = None
private[this] var _driverClassPath: ArrayBuffer[String] = ArrayBuffer()
private[this] var _executorMemory: Option[String] = None
private[this] var _proxyUser: Option[String] = None
private[this] var _driverCores: Option[String] = None
private[this] var _executorCores: Option[String] = None
private[this] var _queue: Option[String] = None
private[this] var _numExecutors: Option[String] = None
private[this] var _archives: ArrayBuffer[Path] = ArrayBuffer()
private[this] var _env: ArrayBuffer[(String, String)] = ArrayBuffer()
private[this] var _redirectOutput: Option[ProcessBuilder.Redirect] = None
private[this] var _redirectError: Option[ProcessBuilder.Redirect] = None
private[this] var _redirectErrorStream: Option[Boolean] = None
private[this] var _userWithCreator: UserWithCreator = UserWithCreator("DefaultUser", "DefaultCreator")
private[this] var _labels: ArrayBuffer[Label[_]] = ArrayBuffer()
def executable(executable: Path): SparkSubmitProcessEngineConnLaunchBuilder = {
_executable = executable
this
}
def jars(jars: Traversable[Path]): SparkSubmitProcessEngineConnLaunchBuilder = {
this._jars ++= jars
this
}
def pyFile(pyFile: Path): SparkSubmitProcessEngineConnLaunchBuilder = {
this._pyFiles += pyFile
this
}
def pyFiles(pyFiles: Traversable[Path]): SparkSubmitProcessEngineConnLaunchBuilder = {
this._pyFiles ++= pyFiles
this
}
def files(files: Traversable[Path]): SparkSubmitProcessEngineConnLaunchBuilder = {
this._files ++= files
this
}
def conf(conf: Traversable[(String, String)]): SparkSubmitProcessEngineConnLaunchBuilder = {
this._conf ++= conf
this
}
def conf(conf: (String, String)): SparkSubmitProcessEngineConnLaunchBuilder = this.conf(conf._1, conf._2)
def driverJavaOptions(driverJavaOptions: String): SparkSubmitProcessEngineConnLaunchBuilder = {
_driverJavaOptions = Some(driverJavaOptions)
this
}
def driverClassPaths(classPaths: Traversable[String]): SparkSubmitProcessEngineConnLaunchBuilder = {
_driverClassPath ++= classPaths
this
}
def archives(archives: Traversable[Path]): SparkSubmitProcessEngineConnLaunchBuilder = {
archives.foreach(archive)
this
}
def archive(archive: Path): SparkSubmitProcessEngineConnLaunchBuilder = {
_archives += archive
this
}
def redirectError(redirect: ProcessBuilder.Redirect): SparkSubmitProcessEngineConnLaunchBuilder = {
_redirectError = Some(redirect)
this
}
def setPort(port: Int): Unit = this.port = port
override protected def getCommands(implicit engineConnBuildRequest: EngineConnBuildRequest): Array[String] = {
build(engineConnBuildRequest)
val commandLine: ArrayBuffer[String] = ArrayBuffer[String]()
commandLine += SparkConfiguration.SPARK_SUBMIT_PATH.getValue
def addOpt(option: String, value: Option[String]): Unit = {
value.foreach { v =>
commandLine += option
commandLine += v
}
}
def addList(option: String, values: Traversable[String]): Unit = {
if (values.nonEmpty) {
commandLine += option
commandLine += values.mkString(",")
}
}
def addClasspath(option: String, values: Traversable[String]): Unit = {
if (values.nonEmpty) {
commandLine += option
commandLine += values.mkString(":")
}
}
addOpt("--master", _master)
addOpt("--deploy-mode", _deployMode)
addOpt("--name", _name)
if (HadoopConf.KEYTAB_PROXYUSER_ENABLED.getValue && _proxyUser.nonEmpty) {
addOpt("--proxy-user", _proxyUser)
}
//addOpt("--jars",Some(ENGINEMANAGER_JAR.getValue))
// info("No need to add jars for " + _jars.map(fromPath).exists(x => x.equals("hdfs:///")).toString())
_jars = _jars.filter(_.isNotBlankPath())
if(_jars.nonEmpty) {
addList("--jars", _jars.map(fromPath))
}
_pyFiles = _pyFiles.filter(_.isNotBlankPath())
if(_pyFiles.nonEmpty) {
addList("--py-files", _pyFiles.map(fromPath))
}
_files = _files.filter(_.isNotBlankPath())
if(_files.nonEmpty) {
addList("--files", _files.map(fromPath))
}
_archives = _archives.filter(_.isNotBlankPath())
if(_archives.nonEmpty) {
addList("--archives", _archives.map(fromPath))
}
_conf.foreach {
case (key, value) =>
if (key.startsWith("spark.")) {
// subcommand cannot be quoted by double quote, use single quote instead
addOpt("--conf", Some(key + "=\"" + value + "\""))
}
}
addOpt("--driver-memory", _driverMemory)
addClasspath("--driver-class-path", _driverClassPath)
addOpt("--driver-cores", _driverCores)
addOpt("--executor-memory", _executorMemory)
addOpt("--executor-cores", _executorCores)
addOpt("--num-executors", _numExecutors)
addOpt("--queue", _queue)
addOpt("--class", _className)
addOpt("1>", Some(s"${variable(LOG_DIRS)}/stdout"))
addOpt("2>>", Some(s"${variable(LOG_DIRS)}/stderr"))
addOpt("", Some(s" ${variable(PWD)}/lib/${SparkConfiguration.ENGINE_JAR.getValue}"))
commandLine.toArray.filter(StringUtils.isNotEmpty)
}
override def enablePublicModule = true
def master(masterUrl: String): SparkSubmitProcessEngineConnLaunchBuilder = {
_master = Some(masterUrl)
this
}
def deployMode(deployMode: String): SparkSubmitProcessEngineConnLaunchBuilder = {
_deployMode = Some(deployMode)
this
}
def className(className: String): SparkSubmitProcessEngineConnLaunchBuilder = {
_className = Some(className)
this
}
def name(name: String): SparkSubmitProcessEngineConnLaunchBuilder = {
_name = Some(name)
this
}
def jar(jar: Path): SparkSubmitProcessEngineConnLaunchBuilder = {
this._jars += jar
this
}
def file(file: Path): SparkSubmitProcessEngineConnLaunchBuilder = {
this._files += file
this
}
def conf(key: String, value: String): SparkSubmitProcessEngineConnLaunchBuilder = {
this._conf += ((key, value))
this
}
def driverMemory(driverMemory: String): SparkSubmitProcessEngineConnLaunchBuilder = {
_driverMemory = Some(driverMemory)
this
}
def driverClassPath(classPath: String): SparkSubmitProcessEngineConnLaunchBuilder = {
_driverClassPath += classPath
this
}
def executorMemory(executorMemory: String): SparkSubmitProcessEngineConnLaunchBuilder = {
_executorMemory = Some(executorMemory)
this
}
def proxyUser(proxyUser: String): SparkSubmitProcessEngineConnLaunchBuilder = {
_proxyUser = Some(proxyUser)
this
}
def driverCores(driverCores: Int): SparkSubmitProcessEngineConnLaunchBuilder = {
this.driverCores(driverCores.toString)
}
def driverCores(driverCores: String): SparkSubmitProcessEngineConnLaunchBuilder = {
_driverCores = Some(driverCores)
this
}
def executorCores(executorCores: Int): SparkSubmitProcessEngineConnLaunchBuilder = {
this.executorCores(executorCores.toString)
}
def executorCores(executorCores: String): SparkSubmitProcessEngineConnLaunchBuilder = {
_executorCores = Some(executorCores)
this
}
def numExecutors(numExecutors: Int): SparkSubmitProcessEngineConnLaunchBuilder = {
this.numExecutors(numExecutors.toString)
}
def numExecutors(numExecutors: String): SparkSubmitProcessEngineConnLaunchBuilder = {
_numExecutors = Some(numExecutors)
this
}
def queue(queue: String): SparkSubmitProcessEngineConnLaunchBuilder = {
_queue = Some(queue)
this
}
def env(key: String, value: String): SparkSubmitProcessEngineConnLaunchBuilder = {
_env += ((key, value))
this
}
def redirectOutput(redirect: ProcessBuilder.Redirect): SparkSubmitProcessEngineConnLaunchBuilder = {
_redirectOutput = Some(redirect)
this
}
def redirectErrorStream(redirect: Boolean): SparkSubmitProcessEngineConnLaunchBuilder = {
_redirectErrorStream = Some(redirect)
this
}
def getEngineResource: NodeResource = userEngineResource
def build(engineRequest: EngineConnBuildRequest): Unit = {
this.request = engineRequest
this.userEngineResource = engineRequest.engineResource
val darResource: DriverAndYarnResource = userEngineResource.getLockedResource.asInstanceOf[DriverAndYarnResource]
val properties = engineRequest.engineConnCreationDesc.properties
this.master("yarn")
this.deployMode("client")
val driverJavaSet = new StringBuilder(" -server")
if (StringUtils.isNotEmpty(EnvConfiguration.ENGINE_CONN_DEFAULT_JAVA_OPTS.getValue))
EnvConfiguration.ENGINE_CONN_DEFAULT_JAVA_OPTS.getValue.format(getGcLogDir(engineRequest)).split("\\s+").foreach(l => {
driverJavaSet.append(" ").append(l)
})
getLogDir(engineRequest).trim.split(" ").foreach(l => {
driverJavaSet.append(" ").append(l)
})
driverJavaSet.append(" -Djava.io.tmpdir=" + variable(TEMP_DIRS))
if (EnvConfiguration.ENGINE_CONN_DEBUG_ENABLE.getValue) {
driverJavaSet.append(s" -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=${variable(RANDOM_PORT)}")
}
this.conf(SparkConfiguration.SPARK_DRIVER_EXTRA_JAVA_OPTIONS.key, driverJavaSet.toString())
//this.conf("spark.sql.extensions", "com.webank.wedatasphere.linkis.hook.spark.extension.SparkHistoryExtension")
this.className(getValueAndRemove(properties,"className", getMainClass))
getValueAndRemove(properties,"archives", "").toString.split(",").map(AbsolutePath).foreach(this.archive)
this.driverCores(getValueAndRemove(properties, SparkResourceConfiguration.LINKIS_SPARK_DRIVER_CORES))
val driverMemory = getValueAndRemove(properties, LINKIS_SPARK_DRIVER_MEMORY)
val driverMemoryWithUnit = if (StringUtils.isNumeric(driverMemory)) {
driverMemory + "g"
} else {
driverMemory
}
this.driverMemory(driverMemoryWithUnit)
this.executorCores(getValueAndRemove(properties, SparkResourceConfiguration.LINKIS_SPARK_EXECUTOR_CORES))
val executorMemory = getValueAndRemove(properties, SparkResourceConfiguration.LINKIS_SPARK_EXECUTOR_MEMORY)
val executorMemoryWithUnit = if (StringUtils.isNumeric(executorMemory)) {
executorMemory + "g"
} else {
executorMemory
}
this.executorMemory(executorMemoryWithUnit)
this.numExecutors(getValueAndRemove(properties, SparkResourceConfiguration.LINKIS_SPARK_EXECUTOR_INSTANCES))
getValueAndRemove(properties,"files", "").split(",").map(AbsolutePath).foreach(file)
getValueAndRemove(properties,"jars", "").split(",").map(AbsolutePath).foreach(jar)
val defaultExternalJars = getValueAndRemove(properties, SparkConfiguration.SPARK_DEFAULT_EXTERNAL_JARS_PATH)
defaultExternalJars.split(",").map(AbsolutePath).filter(x => {
val file = new java.io.File(x.path)
file.isFile
}).foreach(jar)
if (null != darResource) {
this.queue(darResource.yarnResource.queueName)
} else {
this.queue("default")
}
this.driverClassPath(getValueAndRemove(properties, SparkConfiguration.SPARK_DRIVER_CLASSPATH))
this.driverClassPath(variable(CLASSPATH))
this.redirectOutput(Redirect.PIPE)
this.redirectErrorStream(true)
val labels = engineRequest.labels.asScala
labels.foreach { l => {
this._labels += l
l match {
case label: UserCreatorLabel =>
this._userWithCreator = UserWithCreator(label.getUser, label.getCreator)
case _ =>
}
}
}
if (!HadoopConf.KEYTAB_PROXYUSER_ENABLED.getValue) {
this.proxyUser(getValueAndRemove(properties, "proxyUser", ""))
} else {
this.proxyUser(this._userWithCreator.user)
}
//deal spark conf and spark.hadoop.*
val iterator = properties.entrySet().iterator()
val sparkConfKeys = ArrayBuffer[String]()
while (iterator.hasNext) {
val keyValue = iterator.next()
if (!SparkConfiguration.SPARK_PYTHON_VERSION.key.equals(keyValue.getKey) && keyValue.getKey.startsWith("spark.") && StringUtils.isNotBlank(keyValue.getValue)) {
conf(keyValue.getKey, keyValue.getValue)
sparkConfKeys += keyValue.getKey
}
}
this.name(getValueAndRemove(properties, SparkConfiguration.SPARK_APP_NAME ) + "_" +this._userWithCreator.creator)
sparkConfKeys.foreach(properties.remove(_))
}
private def fromPath(path: Path): String = path match {
case AbsolutePath(p) => p
case RelativePath(p) => p
}
override protected def getEngineConnManagerHooks(implicit engineConnBuildRequest: EngineConnBuildRequest): util.List[String] = {
Lists.newArrayList("JarUDFLoadECMHook")
}
}
object SparkSubmitProcessEngineConnLaunchBuilder {
def apply(): SparkSubmitProcessEngineConnLaunchBuilder = {
new SparkSubmitProcessEngineConnLaunchBuilder
}
sealed trait Path {
def isNotBlankPath(): Boolean;
protected def isNotBlankPath(path: String): Boolean = {
StringUtils.isNotBlank(path) && !"/".equals(path.trim) && !"hdfs:///".equals(path.trim) && !"file:///".equals(path.trim)
}
}
case class AbsolutePath(path: String) extends Path {
override def isNotBlankPath(): Boolean = isNotBlankPath(path)
}
case class RelativePath(path: String) extends Path {
override def isNotBlankPath(): Boolean = isNotBlankPath(path)
}
def getValueAndRemove[T](properties: java.util.Map[String, String], commonVars: CommonVars[T]): T = {
val value = commonVars.getValue(properties)
properties.remove(commonVars.key)
value
}
def getValueAndRemove(properties: java.util.Map[String, String], key: String, defaultValue: String): String = {
if (properties.containsKey(key)) {
properties.remove(key)
} else {
defaultValue
}
}
}