Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions core/src/main/scala/org/apache/spark/util/JsonProtocol.scala
Original file line number Diff line number Diff line change
Expand Up @@ -874,8 +874,14 @@ private[spark] object JsonProtocol extends JsonUtils {
case `stageExecutorMetrics` => stageExecutorMetricsFromJson(json)
case `blockUpdate` => blockUpdateFromJson(json)
case `resourceProfileAdded` => resourceProfileAddedFromJson(json)
case other => mapper.readValue(json.toString, Utils.classForName(other))
.asInstanceOf[SparkListenerEvent]
case other =>
val otherClass = Utils.classForName(other)
if (classOf[SparkListenerEvent].isAssignableFrom(otherClass)) {
mapper.readValue(json.toString, otherClass)
.asInstanceOf[SparkListenerEvent]
} else {
throw new SparkException(s"Unknown event type: $other")
}
}
}

Expand Down
30 changes: 30 additions & 0 deletions core/src/test/scala/org/apache/spark/util/JsonProtocolSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,36 @@ class JsonProtocolSuite extends SparkFunSuite {
val jobFailedEvent = JsonProtocol.sparkEventFromJson(exJobFailureNoStackJson)
testEvent(jobFailedEvent, exJobFailureExpectedJson)
}

test("SPARK-52381: handle class not found") {
val unknownJson =
"""{
| "Event" : "com.example.UnknownEvent",
| "foo" : "foo"
|}""".stripMargin
try {
JsonProtocol.sparkEventFromJson(unknownJson)
fail("Expected ClassNotFoundException for unknown event type")
} catch {
case e: ClassNotFoundException =>
}
}

test("SPARK-52381: only read classes that extend SparkListenerEvent") {
val unknownJson =
"""{
| "Event" : "org.apache.spark.SparkException",
| "foo" : "foo"
|}""".stripMargin
try {
JsonProtocol.sparkEventFromJson(unknownJson)
fail("Expected SparkException for unknown event type")
} catch {
case e: SparkException =>
assert(e.getMessage.startsWith("Unknown event type"))
}
}

}


Expand Down