aboutsummaryrefslogtreecommitdiff
path: root/core/src/test/scala/org/apache/spark/storage/StorageStatusListenerSuite.scala
blob: 14daa003bc5a6082799da5a4f68bc38f73777234 (plain) (blame)
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
/*
 * 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.storage

import org.apache.spark.{SparkConf, SparkFunSuite, Success}
import org.apache.spark.executor.TaskMetrics
import org.apache.spark.scheduler._

/**
 * Test the behavior of StorageStatusListener in response to all relevant events.
 */
class StorageStatusListenerSuite extends SparkFunSuite {
  private val bm1 = BlockManagerId("big", "dog", 1)
  private val bm2 = BlockManagerId("fat", "duck", 2)
  private val taskInfo1 = new TaskInfo(0, 0, 0, 0, "big", "dog", TaskLocality.ANY, false)
  private val taskInfo2 = new TaskInfo(0, 0, 0, 0, "fat", "duck", TaskLocality.ANY, false)
  private val conf = new SparkConf()

  test("block manager added/removed") {
    conf.set("spark.ui.retainedDeadExecutors", "1")
    val listener = new StorageStatusListener(conf)

    // Block manager add
    assert(listener.executorIdToStorageStatus.size === 0)
    listener.onBlockManagerAdded(SparkListenerBlockManagerAdded(1L, bm1, 1000L))
    assert(listener.executorIdToStorageStatus.size === 1)
    assert(listener.executorIdToStorageStatus.get("big").isDefined)
    assert(listener.executorIdToStorageStatus("big").blockManagerId === bm1)
    assert(listener.executorIdToStorageStatus("big").maxMem === 1000L)
    assert(listener.executorIdToStorageStatus("big").numBlocks === 0)
    listener.onBlockManagerAdded(SparkListenerBlockManagerAdded(1L, bm2, 2000L))
    assert(listener.executorIdToStorageStatus.size === 2)
    assert(listener.executorIdToStorageStatus.get("fat").isDefined)
    assert(listener.executorIdToStorageStatus("fat").blockManagerId === bm2)
    assert(listener.executorIdToStorageStatus("fat").maxMem === 2000L)
    assert(listener.executorIdToStorageStatus("fat").numBlocks === 0)

    // Block manager remove
    listener.onBlockManagerRemoved(SparkListenerBlockManagerRemoved(1L, bm1))
    assert(listener.executorIdToStorageStatus.size === 1)
    assert(!listener.executorIdToStorageStatus.get("big").isDefined)
    assert(listener.executorIdToStorageStatus.get("fat").isDefined)
    assert(listener.deadExecutorStorageStatus.size === 1)
    assert(listener.deadExecutorStorageStatus(0).blockManagerId.executorId.equals("big"))
    listener.onBlockManagerRemoved(SparkListenerBlockManagerRemoved(1L, bm2))
    assert(listener.executorIdToStorageStatus.size === 0)
    assert(!listener.executorIdToStorageStatus.get("big").isDefined)
    assert(!listener.executorIdToStorageStatus.get("fat").isDefined)
    assert(listener.deadExecutorStorageStatus.size === 1)
    assert(listener.deadExecutorStorageStatus(0).blockManagerId.executorId.equals("fat"))
  }

  test("task end without updated blocks") {
    val listener = new StorageStatusListener(conf)
    listener.onBlockManagerAdded(SparkListenerBlockManagerAdded(1L, bm1, 1000L))
    listener.onBlockManagerAdded(SparkListenerBlockManagerAdded(1L, bm2, 2000L))
    val taskMetrics = new TaskMetrics

    // Task end with no updated blocks
    assert(listener.executorIdToStorageStatus("big").numBlocks === 0)
    assert(listener.executorIdToStorageStatus("fat").numBlocks === 0)
    listener.onTaskEnd(SparkListenerTaskEnd(1, 0, "obliteration", Success, taskInfo1, taskMetrics))
    assert(listener.executorIdToStorageStatus("big").numBlocks === 0)
    assert(listener.executorIdToStorageStatus("fat").numBlocks === 0)
    listener.onTaskEnd(SparkListenerTaskEnd(1, 0, "obliteration", Success, taskInfo2, taskMetrics))
    assert(listener.executorIdToStorageStatus("big").numBlocks === 0)
    assert(listener.executorIdToStorageStatus("fat").numBlocks === 0)
  }

  test("task end with updated blocks") {
    val listener = new StorageStatusListener(conf)
    listener.onBlockManagerAdded(SparkListenerBlockManagerAdded(1L, bm1, 1000L))
    listener.onBlockManagerAdded(SparkListenerBlockManagerAdded(1L, bm2, 2000L))
    val taskMetrics1 = new TaskMetrics
    val taskMetrics2 = new TaskMetrics
    val block1 = (RDDBlockId(1, 1), BlockStatus(StorageLevel.DISK_ONLY, 0L, 100L))
    val block2 = (RDDBlockId(1, 2), BlockStatus(StorageLevel.DISK_ONLY, 0L, 200L))
    val block3 = (RDDBlockId(4, 0), BlockStatus(StorageLevel.DISK_ONLY, 0L, 300L))
    taskMetrics1.setUpdatedBlockStatuses(Seq(block1, block2))
    taskMetrics2.setUpdatedBlockStatuses(Seq(block3))

    // Task end with new blocks
    assert(listener.executorIdToStorageStatus("big").numBlocks === 0)
    assert(listener.executorIdToStorageStatus("fat").numBlocks === 0)
    listener.onTaskEnd(SparkListenerTaskEnd(1, 0, "obliteration", Success, taskInfo1, taskMetrics1))
    assert(listener.executorIdToStorageStatus("big").numBlocks === 2)
    assert(listener.executorIdToStorageStatus("fat").numBlocks === 0)
    assert(listener.executorIdToStorageStatus("big").containsBlock(RDDBlockId(1, 1)))
    assert(listener.executorIdToStorageStatus("big").containsBlock(RDDBlockId(1, 2)))
    assert(listener.executorIdToStorageStatus("fat").numBlocks === 0)
    listener.onTaskEnd(SparkListenerTaskEnd(1, 0, "obliteration", Success, taskInfo2, taskMetrics2))
    assert(listener.executorIdToStorageStatus("big").numBlocks === 2)
    assert(listener.executorIdToStorageStatus("fat").numBlocks === 1)
    assert(listener.executorIdToStorageStatus("big").containsBlock(RDDBlockId(1, 1)))
    assert(listener.executorIdToStorageStatus("big").containsBlock(RDDBlockId(1, 2)))
    assert(listener.executorIdToStorageStatus("fat").containsBlock(RDDBlockId(4, 0)))

    // Task end with dropped blocks
    val droppedBlock1 = (RDDBlockId(1, 1), BlockStatus(StorageLevel.NONE, 0L, 0L))
    val droppedBlock2 = (RDDBlockId(1, 2), BlockStatus(StorageLevel.NONE, 0L, 0L))
    val droppedBlock3 = (RDDBlockId(4, 0), BlockStatus(StorageLevel.NONE, 0L, 0L))
    taskMetrics1.setUpdatedBlockStatuses(Seq(droppedBlock1, droppedBlock3))
    taskMetrics2.setUpdatedBlockStatuses(Seq(droppedBlock2, droppedBlock3))

    listener.onTaskEnd(SparkListenerTaskEnd(1, 0, "obliteration", Success, taskInfo1, taskMetrics1))
    assert(listener.executorIdToStorageStatus("big").numBlocks === 1)
    assert(listener.executorIdToStorageStatus("fat").numBlocks === 1)
    assert(!listener.executorIdToStorageStatus("big").containsBlock(RDDBlockId(1, 1)))
    assert(listener.executorIdToStorageStatus("big").containsBlock(RDDBlockId(1, 2)))
    assert(listener.executorIdToStorageStatus("fat").containsBlock(RDDBlockId(4, 0)))
    listener.onTaskEnd(SparkListenerTaskEnd(1, 0, "obliteration", Success, taskInfo2, taskMetrics2))
    assert(listener.executorIdToStorageStatus("big").numBlocks === 1)
    assert(listener.executorIdToStorageStatus("fat").numBlocks === 0)
    assert(!listener.executorIdToStorageStatus("big").containsBlock(RDDBlockId(1, 1)))
    assert(listener.executorIdToStorageStatus("big").containsBlock(RDDBlockId(1, 2)))
    assert(listener.executorIdToStorageStatus("fat").numBlocks === 0)
  }

  test("unpersist RDD") {
    val listener = new StorageStatusListener(conf)
    listener.onBlockManagerAdded(SparkListenerBlockManagerAdded(1L, bm1, 1000L))
    val taskMetrics1 = new TaskMetrics
    val taskMetrics2 = new TaskMetrics
    val block1 = (RDDBlockId(1, 1), BlockStatus(StorageLevel.DISK_ONLY, 0L, 100L))
    val block2 = (RDDBlockId(1, 2), BlockStatus(StorageLevel.DISK_ONLY, 0L, 200L))
    val block3 = (RDDBlockId(4, 0), BlockStatus(StorageLevel.DISK_ONLY, 0L, 300L))
    taskMetrics1.setUpdatedBlockStatuses(Seq(block1, block2))
    taskMetrics2.setUpdatedBlockStatuses(Seq(block3))
    listener.onTaskEnd(SparkListenerTaskEnd(1, 0, "obliteration", Success, taskInfo1, taskMetrics1))
    listener.onTaskEnd(SparkListenerTaskEnd(1, 0, "obliteration", Success, taskInfo1, taskMetrics2))
    assert(listener.executorIdToStorageStatus("big").numBlocks === 3)

    // Unpersist RDD
    listener.onUnpersistRDD(SparkListenerUnpersistRDD(9090))
    assert(listener.executorIdToStorageStatus("big").numBlocks === 3)
    listener.onUnpersistRDD(SparkListenerUnpersistRDD(4))
    assert(listener.executorIdToStorageStatus("big").numBlocks === 2)
    assert(listener.executorIdToStorageStatus("big").containsBlock(RDDBlockId(1, 1)))
    assert(listener.executorIdToStorageStatus("big").containsBlock(RDDBlockId(1, 2)))
    listener.onUnpersistRDD(SparkListenerUnpersistRDD(1))
    assert(listener.executorIdToStorageStatus("big").numBlocks === 0)
  }
}