Skip to content
This repository has been archived by the owner on May 22, 2019. It is now read-only.

Commit

Permalink
Merge branch 'multi_cluster' of github.com:twitter/gizzard into multi…
Browse files Browse the repository at this point in the history
…_cluster
  • Loading branch information
eaceaser committed Dec 29, 2010
2 parents 6ec6a43 + c6eadab commit ce950dd
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 18 deletions.
Expand Up @@ -11,11 +11,11 @@ class JournaledJob(val job: JsonJob, journaller: String => Unit) extends JsonJob
def apply() {
job()
try {
journaller(new String(job.toJson, "UTF-8"))
journaller(job.toJson)
} catch {
case e: Exception =>
val log = Logger.get(getClass.getName)
log.warning(e, "Failed to journal job: %s", job.toString)
log.warning(e, "Failed to journal job: %s", job.toJson)
}
}
}
Expand Up @@ -36,7 +36,7 @@ class JsonCodec(unparsableJobHandler: Array[Byte] => Unit) extends Codec[JsonJob
def +=(item: (Regex, JsonJobParser)) = processors += item
def +=(r: Regex, p: JsonJobParser) = processors += ((r, p))

def flatten(job: JsonJob): Array[Byte] = job.toJson
def flatten(job: JsonJob): Array[Byte] = job.toJsonBytes

def inflate(data: Array[Byte]): JsonJob = {
try {
Expand Down
6 changes: 4 additions & 2 deletions src/main/scala/com/twitter/gizzard/scheduler/JsonJob.scala
Expand Up @@ -23,12 +23,14 @@ trait JsonJob extends Job {
def shouldReplicate = true
def className = getClass.getName

def toJson = {
def toJsonBytes = {
def json = toMap ++ Map("error_count" -> errorCount, "error_message" -> errorMessage)
val javaMap = deepConvert(Map(className -> json))
JsonJob.mapper.writeValueAsBytes(javaMap)
}

def toJson = new String(toJsonBytes, "UTF-8")

private def deepConvert(scalaMap: Map[String, Any]): JMap[String, Any] = {
val map = new java.util.LinkedHashMap[String, Any]()
scalaMap.map { case (k, v) =>
Expand All @@ -53,7 +55,7 @@ trait JsonJob extends Job {
list
}

override def toString = new String(toJson, "UTF-8")
override def toString = toJson
}

/**
Expand Down
Expand Up @@ -59,7 +59,7 @@ class ReplicatingJob(
extends JsonNestedJob(jobs) {

def this(relay: JobRelay, jobs: Iterable[JsonJob], clusters: Iterable[String]) =
this(relay, jobs, clusters, jobs.map(_.toJson))
this(relay, jobs, clusters, jobs.map(_.toJsonBytes))

def this(relay: JobRelay, jobs: Iterable[JsonJob]) = this(relay, jobs, relay.clusters)

Expand Down
Expand Up @@ -27,9 +27,9 @@ extends JobInjector.Iface {
def apply = deserialized.apply()
def toMap = deserialized.toMap

override def toJson = {
override def toJsonBytes = {
if (isDeserialized) {
deserialized.toJson
deserialized.toJsonBytes
} else {
serialized
}
Expand Down
Expand Up @@ -46,7 +46,7 @@ object CopyJobSpec extends ConfiguredSpecification with JMocker with ClassMocker

"toJson" in {
val copy = makeCopy(Some(nextCopy))
val json = new String(copy.toJson, "UTF-8")
val json = copy.toJson
json mustMatch "Copy"
json mustMatch "\"source_shard_hostname\":\"%s\"".format(sourceShardId.hostname)
json mustMatch "\"source_shard_table_prefix\":\"%s\"".format(sourceShardId.tablePrefix)
Expand Down
Expand Up @@ -14,7 +14,7 @@ class JournaledJobSpec extends ConfiguredSpecification with JMocker with ClassMo
"journal on success" in {
expect {
one(job).apply()
one(job).toJson willReturn "hello".getBytes("UTF-8")
one(job).toJson willReturn "hello"
one(queue).apply("hello")
}

Expand Down
Expand Up @@ -55,7 +55,7 @@ class JsonJobParserSpec extends ConfiguredSpecification with JMocker with ClassM
}

"JsonJob" in {
val json = new String(job.toJson, "UTF-8")
val json = job.toJson
json mustMatch "\"FakeJob\""
json mustMatch "\"a\":1"
json mustMatch "\"error_count\":0"
Expand All @@ -64,7 +64,7 @@ class JsonJobParserSpec extends ConfiguredSpecification with JMocker with ClassM

"JsonNestedJob" in {
val nestedJob = new JsonNestedJob(List(job))
val json = new String(nestedJob.toJson, "UTF-8")
val json = nestedJob.toJson

json mustMatch "\"com.twitter.gizzard.scheduler.JsonNestedJob\":\\{"
json mustMatch "\"error_count\":0"
Expand All @@ -76,7 +76,7 @@ class JsonJobParserSpec extends ConfiguredSpecification with JMocker with ClassM
job.errorCount = 23
job.errorMessage = "Good heavens!"

val json = new String(job.toJson, "UTF-8")
val json = job.toJson
json mustMatch "\\{\"FakeJob\":\\{"
json mustMatch "\"a\":1"
json mustMatch "\"error_count\":23"
Expand Down
Expand Up @@ -13,9 +13,9 @@ class ReplicatingJobSpec extends ConfiguredSpecification with JMocker with Class

"toMap" in {
expect {
one(job1).toJson willReturn """{"foo":"bar"}""".getBytes("UTF-8")
one(job1).className willReturn testJsonJobClass
one(job1).toMap willReturn Map[String, Any]()
one(job1).toJsonBytes willReturn """{"foo":"bar"}""".getBytes("UTF-8")
one(job1).className willReturn testJsonJobClass
one(job1).toMap willReturn Map[String, Any]()
}

val job = new ReplicatingJob(relay, Array(job1), List("c1"))
Expand All @@ -31,7 +31,7 @@ class ReplicatingJobSpec extends ConfiguredSpecification with JMocker with Class
val json = """{"foo":"bar"}""".getBytes("UTF-8")

expect {
one(job1).toJson willReturn json
one(job1).toJsonBytes willReturn json
}

val job = new ReplicatingJob(relay, List(job1), List("c1"))
Expand All @@ -48,7 +48,7 @@ class ReplicatingJobSpec extends ConfiguredSpecification with JMocker with Class

"not replicate when list of clusters is empty" in {
expect {
one(job1).toJson willReturn """{"foo":"bar"}""".getBytes("UTF-8")
one(job1).toJsonBytes willReturn """{"foo":"bar"}""".getBytes("UTF-8")
one(job1).apply()
}

Expand Down

0 comments on commit ce950dd

Please sign in to comment.