/
SparkThrowableSuite.scala
182 lines (161 loc) · 6.22 KB
/
SparkThrowableSuite.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
/*
* 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
import java.io.File
import java.util.IllegalFormatException
import com.fasterxml.jackson.annotation.JsonInclude.Include
import com.fasterxml.jackson.core.JsonParser.Feature.STRICT_DUPLICATE_DETECTION
import com.fasterxml.jackson.core.`type`.TypeReference
import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.databind.json.JsonMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import org.apache.commons.io.IOUtils
import org.apache.spark.SparkThrowableHelper._
import org.apache.spark.util.Utils
/**
* Test suite for Spark Throwables.
*/
class SparkThrowableSuite extends SparkFunSuite {
override def beforeAll(): Unit = {
super.beforeAll()
}
def checkIfUnique(ss: Seq[Any]): Unit = {
val dups = ss.groupBy(identity).mapValues(_.size).filter(_._2 > 1).keys.toSeq
assert(dups.isEmpty)
}
def checkCondition(ss: Seq[String], fx: String => Boolean): Unit = {
ss.foreach { s =>
assert(fx(s))
}
}
test("No duplicate error classes") {
// Enabling this feature incurs performance overhead (20-30%)
val mapper = JsonMapper.builder()
.addModule(DefaultScalaModule)
.enable(STRICT_DUPLICATE_DETECTION)
.build()
mapper.readValue(errorClassesUrl, new TypeReference[Map[String, ErrorInfo]]() {})
}
test("Error classes are correctly formatted") {
val errorClassFileContents = IOUtils.toString(errorClassesUrl.openStream())
val mapper = JsonMapper.builder()
.addModule(DefaultScalaModule)
.enable(SerializationFeature.INDENT_OUTPUT)
.build()
val rewrittenString = mapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true)
.setSerializationInclusion(Include.NON_ABSENT)
.writeValueAsString(errorClassToInfoMap)
assert(rewrittenString == errorClassFileContents)
}
test("SQLSTATE invariants") {
val sqlStates = errorClassToInfoMap.values.toSeq.flatMap(_.sqlState)
val errorClassReadMe = Utils.getSparkClassLoader.getResource("error/README.md")
val errorClassReadMeContents = IOUtils.toString(errorClassReadMe.openStream())
val sqlStateTableRegex =
"(?s)<!-- SQLSTATE table start -->(.+)<!-- SQLSTATE table stop -->".r
val sqlTable = sqlStateTableRegex.findFirstIn(errorClassReadMeContents).get
val sqlTableRows = sqlTable.split("\n").filter(_.startsWith("|")).drop(2)
val validSqlStates = sqlTableRows.map(_.slice(1, 6)).toSet
// Sanity check
assert(Set("07000", "42000", "HZ000").subsetOf(validSqlStates))
assert(validSqlStates.forall(_.length == 5), validSqlStates)
checkCondition(sqlStates, s => validSqlStates.contains(s))
}
test("Message format invariants") {
val messageFormats = errorClassToInfoMap.values.toSeq.map(_.messageFormat)
checkCondition(messageFormats, s => s != null)
checkIfUnique(messageFormats)
}
test("Round trip") {
val tmpFile = File.createTempFile("rewritten", ".json")
val mapper = JsonMapper.builder()
.addModule(DefaultScalaModule)
.enable(SerializationFeature.INDENT_OUTPUT)
.build()
mapper.writeValue(tmpFile, errorClassToInfoMap)
val rereadErrorClassToInfoMap = mapper.readValue(
tmpFile, new TypeReference[Map[String, ErrorInfo]]() {})
assert(rereadErrorClassToInfoMap == errorClassToInfoMap)
}
test("Check if error class is missing") {
val ex1 = intercept[IllegalArgumentException] {
getMessage("", Array.empty)
}
assert(ex1.getMessage == "Cannot find error class ''")
val ex2 = intercept[IllegalArgumentException] {
getMessage("LOREM_IPSUM", Array.empty)
}
assert(ex2.getMessage == "Cannot find error class 'LOREM_IPSUM'")
}
test("Check if message parameters match message format") {
// Requires 2 args
intercept[IllegalFormatException] {
getMessage("MISSING_COLUMN", Array.empty)
}
// Does not fail with too many args (expects 0 args)
assert(getMessage("DIVIDE_BY_ZERO", Array("foo", "bar")) ==
"divide by zero. You can use 'try_divide' or set foo to false " +
"(except for ANSI interval type) to bypass this error.")
}
test("Error message is formatted") {
assert(getMessage("MISSING_COLUMN", Array("foo", "bar, baz")) ==
"Column 'foo' does not exist. Did you mean one of the following? [bar, baz]")
}
test("Try catching legacy SparkError") {
try {
throw new SparkException("Arbitrary legacy message")
} catch {
case e: SparkThrowable =>
assert(e.getErrorClass == null)
assert(e.getSqlState == null)
case _: Throwable =>
// Should not end up here
assert(false)
}
}
test("Try catching SparkError with error class") {
try {
throw new SparkException(
errorClass = "WRITING_JOB_ABORTED",
messageParameters = Array.empty,
cause = null)
} catch {
case e: SparkThrowable =>
assert(e.getErrorClass == "WRITING_JOB_ABORTED")
assert(e.getSqlState == "40000")
case _: Throwable =>
// Should not end up here
assert(false)
}
}
test("Try catching internal SparkError") {
try {
throw new SparkException(
errorClass = "INTERNAL_ERROR",
messageParameters = Array("this is an internal error"),
cause = null
)
} catch {
case e: SparkThrowable =>
assert(e.isInternalError)
assert(e.getSqlState == null)
case _: Throwable =>
// Should not end up here
assert(false)
}
}
}