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
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.spark.scheduler.cluster.mesos
import java.util.Collections
import scala.collection.JavaConverters._
import scala.collection.mutable.ArrayBuffer
import scala.reflect.ClassTag
import org.apache.mesos.{Protos, Scheduler, SchedulerDriver}
import org.apache.mesos.Protos._
import org.mockito.Matchers
import org.mockito.Matchers._
import org.mockito.Mockito._
import org.scalatest.mock.MockitoSugar
import org.scalatest.BeforeAndAfter
import org.apache.spark.{LocalSparkContext, SecurityManager, SparkConf, SparkContext, SparkFunSuite}
import org.apache.spark.network.shuffle.mesos.MesosExternalShuffleClient
import org.apache.spark.rpc.RpcEndpointRef
import org.apache.spark.scheduler.cluster.CoarseGrainedClusterMessages.RemoveExecutor
import org.apache.spark.scheduler.TaskSchedulerImpl
import org.apache.spark.scheduler.cluster.mesos.Utils._
class MesosCoarseGrainedSchedulerBackendSuite extends SparkFunSuite
with LocalSparkContext
with MockitoSugar
with BeforeAndAfter {
private var sparkConf: SparkConf = _
private var driver: SchedulerDriver = _
private var taskScheduler: TaskSchedulerImpl = _
private var backend: MesosCoarseGrainedSchedulerBackend = _
private var externalShuffleClient: MesosExternalShuffleClient = _
private var driverEndpoint: RpcEndpointRef = _
@volatile private var stopCalled = false
test("mesos supports killing and limiting executors") {
setBackend()
sparkConf.set("spark.driver.host", "driverHost")
sparkConf.set("spark.driver.port", "1234")
val minMem = backend.executorMemory(sc)
val minCpu = 4
val offers = List((minMem, minCpu))
// launches a task on a valid offer
offerResources(offers)
verifyTaskLaunched(driver, "o1")
// kills executors
backend.doRequestTotalExecutors(0)
assert(backend.doKillExecutors(Seq("0")))
val taskID0 = createTaskId("0")
verify(driver, times(1)).killTask(taskID0)
// doesn't launch a new task when requested executors == 0
offerResources(offers, 2)
verifyDeclinedOffer(driver, createOfferId("o2"))
// Launches a new task when requested executors is positive
backend.doRequestTotalExecutors(2)
offerResources(offers, 2)
verifyTaskLaunched(driver, "o2")
}
test("mesos supports killing and relaunching tasks with executors") {
setBackend()
// launches a task on a valid offer
val minMem = backend.executorMemory(sc) + 1024
val minCpu = 4
val offer1 = (minMem, minCpu)
val offer2 = (minMem, 1)
offerResources(List(offer1, offer2))
verifyTaskLaunched(driver, "o1")
// accounts for a killed task
val status = createTaskStatus("0", "s1", TaskState.TASK_KILLED)
backend.statusUpdate(driver, status)
verify(driver, times(1)).reviveOffers()
// Launches a new task on a valid offer from the same slave
offerResources(List(offer2))
verifyTaskLaunched(driver, "o2")
}
test("mesos supports spark.executor.cores") {
val executorCores = 4
setBackend(Map("spark.executor.cores" -> executorCores.toString))
val executorMemory = backend.executorMemory(sc)
val offers = List((executorMemory * 2, executorCores + 1))
offerResources(offers)
val taskInfos = verifyTaskLaunched(driver, "o1")
assert(taskInfos.length == 1)
val cpus = backend.getResource(taskInfos.head.getResourcesList, "cpus")
assert(cpus == executorCores)
}
test("mesos supports unset spark.executor.cores") {
setBackend()
val executorMemory = backend.executorMemory(sc)
val offerCores = 10
offerResources(List((executorMemory * 2, offerCores)))
val taskInfos = verifyTaskLaunched(driver, "o1")
assert(taskInfos.length == 1)
val cpus = backend.getResource(taskInfos.head.getResourcesList, "cpus")
assert(cpus == offerCores)
}
test("mesos does not acquire more than spark.cores.max") {
val maxCores = 10
setBackend(Map("spark.cores.max" -> maxCores.toString))
val executorMemory = backend.executorMemory(sc)
offerResources(List((executorMemory, maxCores + 1)))
val taskInfos = verifyTaskLaunched(driver, "o1")
assert(taskInfos.length == 1)
val cpus = backend.getResource(taskInfos.head.getResourcesList, "cpus")
assert(cpus == maxCores)
}
test("mesos declines offers that violate attribute constraints") {
setBackend(Map("spark.mesos.constraints" -> "x:true"))
offerResources(List((backend.executorMemory(sc), 4)))
verifyDeclinedOffer(driver, createOfferId("o1"), true)
}
test("mesos declines offers with a filter when reached spark.cores.max") {
val maxCores = 3
setBackend(Map("spark.cores.max" -> maxCores.toString))
val executorMemory = backend.executorMemory(sc)
offerResources(List(
(executorMemory, maxCores + 1),
(executorMemory, maxCores + 1)))
verifyTaskLaunched(driver, "o1")
verifyDeclinedOffer(driver, createOfferId("o2"), true)
}
test("mesos assigns tasks round-robin on offers") {
val executorCores = 4
val maxCores = executorCores * 2
setBackend(Map("spark.executor.cores" -> executorCores.toString,
"spark.cores.max" -> maxCores.toString))
val executorMemory = backend.executorMemory(sc)
offerResources(List(
(executorMemory * 2, executorCores * 2),
(executorMemory * 2, executorCores * 2)))
verifyTaskLaunched(driver, "o1")
verifyTaskLaunched(driver, "o2")
}
test("mesos creates multiple executors on a single slave") {
val executorCores = 4
setBackend(Map("spark.executor.cores" -> executorCores.toString))
// offer with room for two executors
val executorMemory = backend.executorMemory(sc)
offerResources(List((executorMemory * 2, executorCores * 2)))
// verify two executors were started on a single offer
val taskInfos = verifyTaskLaunched(driver, "o1")
assert(taskInfos.length == 2)
}
test("mesos doesn't register twice with the same shuffle service") {
setBackend(Map("spark.shuffle.service.enabled" -> "true"))
val (mem, cpu) = (backend.executorMemory(sc), 4)
val offer1 = createOffer("o1", "s1", mem, cpu)
backend.resourceOffers(driver, List(offer1).asJava)
verifyTaskLaunched(driver, "o1")
val offer2 = createOffer("o2", "s1", mem, cpu)
backend.resourceOffers(driver, List(offer2).asJava)
verifyTaskLaunched(driver, "o2")
val status1 = createTaskStatus("0", "s1", TaskState.TASK_RUNNING)
backend.statusUpdate(driver, status1)
val status2 = createTaskStatus("1", "s1", TaskState.TASK_RUNNING)
backend.statusUpdate(driver, status2)
verify(externalShuffleClient, times(1))
.registerDriverWithShuffleService(anyString, anyInt, anyLong, anyLong)
}
test("mesos kills an executor when told") {
setBackend()
val (mem, cpu) = (backend.executorMemory(sc), 4)
val offer1 = createOffer("o1", "s1", mem, cpu)
backend.resourceOffers(driver, List(offer1).asJava)
verifyTaskLaunched(driver, "o1")
backend.doKillExecutors(List("0"))
verify(driver, times(1)).killTask(createTaskId("0"))
}
test("weburi is set in created scheduler driver") {
setBackend()
val taskScheduler = mock[TaskSchedulerImpl]
when(taskScheduler.sc).thenReturn(sc)
val driver = mock[SchedulerDriver]
when(driver.start()).thenReturn(Protos.Status.DRIVER_RUNNING)
val securityManager = mock[SecurityManager]
val backend = new MesosCoarseGrainedSchedulerBackend(
taskScheduler, sc, "master", securityManager) {
override protected def createSchedulerDriver(
masterUrl: String,
scheduler: Scheduler,
sparkUser: String,
appName: String,
conf: SparkConf,
webuiUrl: Option[String] = None,
checkpoint: Option[Boolean] = None,
failoverTimeout: Option[Double] = None,
frameworkId: Option[String] = None): SchedulerDriver = {
markRegistered()
assert(webuiUrl.isDefined)
assert(webuiUrl.get.equals("http://webui"))
driver
}
}
backend.start()
}
test("honors unset spark.mesos.containerizer") {
setBackend(Map("spark.mesos.executor.docker.image" -> "test"))
val (mem, cpu) = (backend.executorMemory(sc), 4)
val offer1 = createOffer("o1", "s1", mem, cpu)
backend.resourceOffers(driver, List(offer1).asJava)
val taskInfos = verifyTaskLaunched(driver, "o1")
assert(taskInfos.head.getContainer.getType == ContainerInfo.Type.DOCKER)
}
test("honors spark.mesos.containerizer=\"mesos\"") {
setBackend(Map(
"spark.mesos.executor.docker.image" -> "test",
"spark.mesos.containerizer" -> "mesos"))
val (mem, cpu) = (backend.executorMemory(sc), 4)
val offer1 = createOffer("o1", "s1", mem, cpu)
backend.resourceOffers(driver, List(offer1).asJava)
val taskInfos = verifyTaskLaunched(driver, "o1")
assert(taskInfos.head.getContainer.getType == ContainerInfo.Type.MESOS)
}
test("docker settings are reflected in created tasks") {
setBackend(Map(
"spark.mesos.executor.docker.image" -> "some_image",
"spark.mesos.executor.docker.forcePullImage" -> "true",
"spark.mesos.executor.docker.volumes" -> "/host_vol:/container_vol:ro",
"spark.mesos.executor.docker.portmaps" -> "8080:80:tcp"
))
val (mem, cpu) = (backend.executorMemory(sc), 4)
val offer1 = createOffer("o1", "s1", mem, cpu)
backend.resourceOffers(driver, List(offer1).asJava)
val launchedTasks = verifyTaskLaunched(driver, "o1")
assert(launchedTasks.size == 1)
val containerInfo = launchedTasks.head.getContainer
assert(containerInfo.getType == ContainerInfo.Type.DOCKER)
val volumes = containerInfo.getVolumesList.asScala
assert(volumes.size == 1)
val volume = volumes.head
assert(volume.getHostPath == "/host_vol")
assert(volume.getContainerPath == "/container_vol")
assert(volume.getMode == Volume.Mode.RO)
val dockerInfo = containerInfo.getDocker
assert(dockerInfo.getImage == "some_image")
assert(dockerInfo.getForcePullImage)
val portMappings = dockerInfo.getPortMappingsList.asScala
assert(portMappings.size == 1)
val portMapping = portMappings.head
assert(portMapping.getHostPort == 8080)
assert(portMapping.getContainerPort == 80)
assert(portMapping.getProtocol == "tcp")
}
test("force-pull-image option is disabled by default") {
setBackend(Map(
"spark.mesos.executor.docker.image" -> "some_image"
))
val (mem, cpu) = (backend.executorMemory(sc), 4)
val offer1 = createOffer("o1", "s1", mem, cpu)
backend.resourceOffers(driver, List(offer1).asJava)
val launchedTasks = verifyTaskLaunched(driver, "o1")
assert(launchedTasks.size == 1)
val containerInfo = launchedTasks.head.getContainer
assert(containerInfo.getType == ContainerInfo.Type.DOCKER)
val dockerInfo = containerInfo.getDocker
assert(dockerInfo.getImage == "some_image")
assert(!dockerInfo.getForcePullImage)
}
test("Do not call removeExecutor() after backend is stopped") {
setBackend()
// launches a task on a valid offer
val offers = List((backend.executorMemory(sc), 1))
offerResources(offers)
verifyTaskLaunched(driver, "o1")
// launches a thread simulating status update
val statusUpdateThread = new Thread {
override def run(): Unit = {
while (!stopCalled) {
Thread.sleep(100)
}
val status = createTaskStatus("0", "s1", TaskState.TASK_FINISHED)
backend.statusUpdate(driver, status)
}
}.start
backend.stop()
// Any method of the backend involving sending messages to the driver endpoint should not
// be called after the backend is stopped.
verify(driverEndpoint, never()).askWithRetry(isA(classOf[RemoveExecutor]))(any[ClassTag[_]])
}
private def verifyDeclinedOffer(driver: SchedulerDriver,
offerId: OfferID,
filter: Boolean = false): Unit = {
if (filter) {
verify(driver, times(1)).declineOffer(Matchers.eq(offerId), anyObject[Filters])
} else {
verify(driver, times(1)).declineOffer(Matchers.eq(offerId))
}
}
private def offerResources(offers: List[(Int, Int)], startId: Int = 1): Unit = {
val mesosOffers = offers.zipWithIndex.map {case (offer, i) =>
createOffer(s"o${i + startId}", s"s${i + startId}", offer._1, offer._2)}
backend.resourceOffers(driver, mesosOffers.asJava)
}
private def createTaskStatus(taskId: String, slaveId: String, state: TaskState): TaskStatus = {
TaskStatus.newBuilder()
.setTaskId(TaskID.newBuilder().setValue(taskId).build())
.setSlaveId(SlaveID.newBuilder().setValue(slaveId).build())
.setState(state)
.build
}
private def createSchedulerBackend(
taskScheduler: TaskSchedulerImpl,
driver: SchedulerDriver,
shuffleClient: MesosExternalShuffleClient,
endpoint: RpcEndpointRef): MesosCoarseGrainedSchedulerBackend = {
val securityManager = mock[SecurityManager]
val backend = new MesosCoarseGrainedSchedulerBackend(
taskScheduler, sc, "master", securityManager) {
override protected def createSchedulerDriver(
masterUrl: String,
scheduler: Scheduler,
sparkUser: String,
appName: String,
conf: SparkConf,
webuiUrl: Option[String] = None,
checkpoint: Option[Boolean] = None,
failoverTimeout: Option[Double] = None,
frameworkId: Option[String] = None): SchedulerDriver = driver
override protected def getShuffleClient(): MesosExternalShuffleClient = shuffleClient
override protected def createDriverEndpointRef(
properties: ArrayBuffer[(String, String)]): RpcEndpointRef = endpoint
// override to avoid race condition with the driver thread on `mesosDriver`
override def startScheduler(newDriver: SchedulerDriver): Unit = {
mesosDriver = newDriver
}
override def stopExecutors(): Unit = {
stopCalled = true
}
markRegistered()
}
backend.start()
backend
}
private def setBackend(sparkConfVars: Map[String, String] = null) {
sparkConf = (new SparkConf)
.setMaster("local[*]")
.setAppName("test-mesos-dynamic-alloc")
.setSparkHome("/path")
.set("spark.mesos.driver.webui.url", "http://webui")
if (sparkConfVars != null) {
sparkConf.setAll(sparkConfVars)
}
sc = new SparkContext(sparkConf)
driver = mock[SchedulerDriver]
when(driver.start()).thenReturn(Protos.Status.DRIVER_RUNNING)
taskScheduler = mock[TaskSchedulerImpl]
when(taskScheduler.sc).thenReturn(sc)
externalShuffleClient = mock[MesosExternalShuffleClient]
driverEndpoint = mock[RpcEndpointRef]
backend = createSchedulerBackend(taskScheduler, driver, externalShuffleClient, driverEndpoint)
}
}
|