Skip to content
Snippets Groups Projects
Commit 8db40f67 authored by zsxwing's avatar zsxwing Committed by Sean Owen
Browse files

[SPARK-7863] [CORE] Create SimpleDateFormat for every SimpleDateParam instance...

[SPARK-7863] [CORE] Create SimpleDateFormat for every SimpleDateParam instance because it's not thread-safe

SimpleDateFormat is not thread-safe. This PR creates new `SimpleDateFormat` for each `SimpleDateParam` instance.

Author: zsxwing <zsxwing@gmail.com>

Closes #6406 from zsxwing/SPARK-7863 and squashes the following commits:

aeed4c1 [zsxwing] Rewrite SimpleDateParam
8cdd986 [zsxwing] Inline formats
9680a15 [zsxwing] Create SimpleDateFormat for each SimpleDateParam instance because it's not thread-safe
parent bf465807
No related branches found
No related tags found
No related merge requests found
......@@ -16,40 +16,33 @@
*/
package org.apache.spark.status.api.v1
import java.text.SimpleDateFormat
import java.text.{ParseException, SimpleDateFormat}
import java.util.TimeZone
import javax.ws.rs.WebApplicationException
import javax.ws.rs.core.Response
import javax.ws.rs.core.Response.Status
import scala.util.Try
private[v1] class SimpleDateParam(val originalValue: String) {
val timestamp: Long = {
SimpleDateParam.formats.collectFirst {
case fmt if Try(fmt.parse(originalValue)).isSuccess =>
fmt.parse(originalValue).getTime()
}.getOrElse(
throw new WebApplicationException(
Response
.status(Status.BAD_REQUEST)
.entity("Couldn't parse date: " + originalValue)
.build()
)
)
}
}
private[v1] object SimpleDateParam {
val formats: Seq[SimpleDateFormat] = {
val gmtDay = new SimpleDateFormat("yyyy-MM-dd")
gmtDay.setTimeZone(TimeZone.getTimeZone("GMT"))
Seq(
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSz"),
gmtDay
)
val timestamp: Long = {
val format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSz")
try {
format.parse(originalValue).getTime()
} catch {
case _: ParseException =>
val gmtDay = new SimpleDateFormat("yyyy-MM-dd")
gmtDay.setTimeZone(TimeZone.getTimeZone("GMT"))
try {
gmtDay.parse(originalValue).getTime()
} catch {
case _: ParseException =>
throw new WebApplicationException(
Response
.status(Status.BAD_REQUEST)
.entity("Couldn't parse date: " + originalValue)
.build()
)
}
}
}
}
......@@ -16,6 +16,8 @@
*/
package org.apache.spark.status.api.v1
import javax.ws.rs.WebApplicationException
import org.scalatest.{Matchers, FunSuite}
class SimpleDateParamSuite extends FunSuite with Matchers {
......@@ -24,6 +26,9 @@ class SimpleDateParamSuite extends FunSuite with Matchers {
new SimpleDateParam("2015-02-20T23:21:17.190GMT").timestamp should be (1424474477190L)
new SimpleDateParam("2015-02-20T17:21:17.190EST").timestamp should be (1424470877190L)
new SimpleDateParam("2015-02-20").timestamp should be (1424390400000L) // GMT
intercept[WebApplicationException] {
new SimpleDateParam("invalid date")
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment