Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-24660][SHS] Show correct error pages when downloading logs #21644

Closed
wants to merge 6 commits into from
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import org.glassfish.jersey.server.ServerProperties
import org.glassfish.jersey.servlet.ServletContainer

import org.apache.spark.SecurityManager
import org.apache.spark.ui.SparkUI
import org.apache.spark.ui.{SparkUI, UIUtils}

/**
* Main entry point for serving spark application metrics as json, using JAX-RS.
Expand Down Expand Up @@ -148,38 +148,18 @@ private[v1] trait BaseAppResource extends ApiRequestContext {
}

private[v1] class ForbiddenException(msg: String) extends WebApplicationException(
Response.status(Response.Status.FORBIDDEN).entity(msg).build())
UIUtils.buildErrorResponse(Response.Status.FORBIDDEN, msg))

private[v1] class NotFoundException(msg: String) extends WebApplicationException(
new NoSuchElementException(msg),
Response
.status(Response.Status.NOT_FOUND)
.entity(ErrorWrapper(msg))
.build()
)
UIUtils.buildErrorResponse(Response.Status.NOT_FOUND, msg))

private[v1] class ServiceUnavailable(msg: String) extends WebApplicationException(
new ServiceUnavailableException(msg),
Response
.status(Response.Status.SERVICE_UNAVAILABLE)
.entity(ErrorWrapper(msg))
.build()
)
UIUtils.buildErrorResponse(Response.Status.SERVICE_UNAVAILABLE, msg))

private[v1] class BadParameterException(msg: String) extends WebApplicationException(
new IllegalArgumentException(msg),
Response
.status(Response.Status.BAD_REQUEST)
.entity(ErrorWrapper(msg))
.build()
) {
UIUtils.buildErrorResponse(Response.Status.BAD_REQUEST, msg)) {
def this(param: String, exp: String, actual: String) = {
this(raw"""Bad value for parameter "$param". Expected a $exp, got "$actual"""")
}
}

/**
* Signal to JacksonMessageWriter to not convert the message into json (which would result in an
* extra set of quotes).
*/
private[v1] case class ErrorWrapper(s: String)
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ private[v1] class JacksonMessageWriter extends MessageBodyWriter[Object]{
mediaType: MediaType,
multivaluedMap: MultivaluedMap[String, AnyRef],
outputStream: OutputStream): Unit = {
t match {
case ErrorWrapper(err) => outputStream.write(err.getBytes(StandardCharsets.UTF_8))
case _ => mapper.writeValue(outputStream, t)
}
mapper.writeValue(outputStream, t)
}

override def getSize(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,8 @@ private[v1] class AbstractApplicationResource extends BaseAppResource {
.header("Content-Type", MediaType.APPLICATION_OCTET_STREAM)
.build()
} catch {
case NonFatal(e) =>
Response.serverError()
.entity(s"Event logs are not available for app: $appId.")
.status(Response.Status.SERVICE_UNAVAILABLE)
.build()
case NonFatal(_) =>
throw new ServiceUnavailable(s"Event logs are not available for app: $appId.")
}
}

Expand Down
5 changes: 5 additions & 0 deletions core/src/main/scala/org/apache/spark/ui/UIUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import java.net.URLDecoder
import java.text.SimpleDateFormat
import java.util.{Date, Locale, TimeZone}
import javax.servlet.http.HttpServletRequest
import javax.ws.rs.core.{MediaType, Response}

import scala.util.control.NonFatal
import scala.xml._
Expand Down Expand Up @@ -566,4 +567,8 @@ private[spark] object UIUtils extends Logging {
NEWLINE_AND_SINGLE_QUOTE_REGEX.replaceAllIn(requestParameter, ""))
}
}

def buildErrorResponse(status: Response.Status, msg: String): Response = {
Response.status(status).entity(msg).`type`(MediaType.TEXT_PLAIN).build()
}
}