Spark-on-Yarn资源调度和作业调度

作业调度

Spark默认采取FIFO策略运行多个Jobs,它提供一个队列来保存已经提交的Jobs,如果队头的Job不需要占用所有的集群资源,那么后续的 Job可以立即运行,但是如果队头的Job需要占用所有的集群资源,且运行时间很长,那么即使后续的Job很小,也要等待前面的Job执行完后才可以执 行,这样就造成了大量的延迟。

Spark0.8+版本开始支持公平调度策略,在该策略下,Spark以round robin的方式为每个Job分配执行的Tasks,每个Job在同一时间都可以运行多个Tasks,Jobs之间是共享集群资源的。这就意味着当一个大 任务运行的同时,用户提交的小任务可以立即执行,且获得良好的响应时间,而不用去等待大任务的结束。

通过

1
2
3
4
``` scala
val conf = new SparkConf().setMaster(...).setAppName(...)
conf.set("spark.scheduler.mode", "FAIR")
val sc = new SparkContext(conf)

1. 公平调度池

Spark支持将不同的Jobs划分到不同的调度池中,可以为每个调度池设置不同的属性(比如权重),这样就可以将重要的Job分到高优先级的调度池中, 比如可以为每个用户创建一个调度池,并为每个用户划分相等的集群资源,而不用去管每个用户同时运行多少个Job。

如果用户没有设置公平调度池,默认情况下提交的任务被划分到default调度池中,可通过

1
2
``` scala
sc.setLocalProperty("spark.scheduler.pool", "pool1")

清除调度池

1
sc.setLocalProperty("spark.scheduler.pool", null)

2. 默认调度池

默认情况下不同的调度池共享集群的资源,但在每个调度池内部,Job是以FIFO的方式运行的,比如为每个用户创建一个调度池,集群的资源平均分配给各个调度池,在调度池内部,每个Job按照FIFO的顺序运行。

3. 调度池的属性

schedulingMode:调度池内部Job之间的调度顺序,FIFO或FAIR,默认是FIFO
weight:调度池的权重,默认是1,如果设置为2,那么它将获取相当于其他调度池2倍的资源。
minShare:最少满足资源数,默认是0,只有在满足每个调度池minShare资源数的基础之上,才会按照weight比例来获取额外的资源。

可创建一个类似于

1
2
3
``` scala
conf.set("spark.scheduler.allocation.file", "/path/to/file")
fairscheduler.xml.template文件内容

fairscheduler.xml.template文件内容

1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0"?>
<allocations>
<pool name="production">
<schedulingMode>FAIR</schedulingMode>
<weight>1</weight>
<minShare>2</minShare>
</pool>
<pool name="test">
<schedulingMode>FIFO</schedulingMode>
<weight>2</weight>
<minShare>3</minShare>
</pool>
</allocations>

4. 测试

myFairScheduler.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0"?>
<allocations>
<pool name="onlinetestpool">
<schedulingMode>FAIR</schedulingMode>
<weight>2</weight>
<minShare>7</minShare>
</pool>
<pool name="default">
<schedulingMode>FAIR</schedulingMode>
<weight>1</weight>
<minShare>7</minShare>
</pool>
</allocations>

启动Spark应用
spark-shell \
–conf spark.scheduler.mode=FAIR \
–conf spark.scheduler.pool=onlinetestpool \
–conf spark.scheduler.allocation.file=/home/spark/spark/conf/fairscheduler.xml