一、Spark On YARN 动态资源
利用Spark的动态资源机制可以增加队列的利用率,减少空置,占用的无效使用。
原理:
考虑如下场景:
在Spark On YARN的运行机制下,有一个Application申请了50个executor 每个20G memory,10 个core,Applicaiton有10个job,每个job1个stage。
假设其中一个Job只有一个task并且需要运行50分钟,这时Spark将只使用一个exceutor进行处理, 剩余的49个executor 将空闲50分钟,相当于什么都没做。
打开这个功能后将自动释放这49个executor , 后续stage如果需要资源Spark将再次向YARN申请。
使用时增加如下参数即可:
参数名称 | 默认值 | 说明 |
---|---|---|
spark.dynamicAllocation.enabled | false | 是否使用动态资源分配,根据工作负载对应用程序executor进行扩展 |
spark.dynamicAllocation.executorIdleTimeout | 60s | 如果启用了动态分配并且executor已经空闲超过这个时间,executor将被释放 |
spark.dynamicAllocation.cachedExecutorIdleTimeout | infinity | 如果启用了动态分配,并且缓存数据块的executor已经空闲了超过这个时间,executor将被释放 |
spark.dynamicAllocation.initialExecutors | spark.dynamicAllocation.enabled | 如果启用动态分配,则要运行executor的初始数量。如果设置了“–num-executors”(或“spark.executor.instances”)并且大于这个值,则会使用这个值进行初始化。 如: max(initialExecuor = 3, –num-executors = 10) 取最大 |
spark.dynamicAllocation.maxExecutors | infinity | 如果启用动态分配,executor的上限个数。必须设置一个数,否则会占用集群所有资源 |
spark.dynamicAllocation.minExecutors | 0 | 如果启用动态分配,executor的下限个数。 |
spark.dynamicAllocation.schedulerBacklogTimeout | 1s | 如果启用动态分配,超过这个时间后还有未完成的积压任务,将会触发再次请求新的executor |
例子:
spark-submit \
--class mainclass \
--master yarn \
--name appname \
--deploy-mode cluster \
--executor-memory 16g \
--executor-cores 6 \
--num-executors 100 \
--driver-memory 16g \
--total-executor-cores 800 \
--conf 'spark.driver.extraJavaOptions=-XX:PermSize=512M' \
--conf 'spark.executor.extraJavaOptions=-verbose:gc -XX:+UseCompressedOops -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:CMSInitiatingOccupancyFraction=60 -XX:PermSize=512M' \
--conf spark.speculation=false \
--conf spark.sql.broadcastTimeout=600 \
--conf spark.sql.shuffle.partitions=800 \
--conf spark.default.parallelism=600 \
--conf spark.memory.fraction=0.8 \
--files $HIVE_CONF_DIR/hive-site.xml \
--conf spark.scheduler.listenerbus.eventqueue.size=100000 \
--conf spark.dynamicAllocation.enabled=true \ #打开动态资源
--conf spark.dynamicAllocation.minExecutors=1 \ #弹性收缩后最小execturo个数
--conf spark.dynamicAllocation.maxExecutors=100 \ #弹性放大后最大executor个数
--conf spark.dynamicAllocation.executorIdleTimeout=3s \ #当executor空闲多久时释放,这个值根据自己的场景调整
--conf spark.shuffle.service.enabled=true \ #打开动态资源后必须打开shuffle.service. 让shuffle数据与executor解耦。
apps.jar 20171231
更多Spark参数配置,请参考官网:
https://spark.apache.org/docs/latest/configuration.html