Permalink
Browse files

Don't discard jobs when the queue is full.

  • Loading branch information...
1 parent cbc4192 commit 5ad4444048384ea466bcefb4de272dfa7afb42e9 @rcohen rcohen committed May 3, 2011
View
7 src/main/scala/com/twitter/gizzard/scheduler/MemoryJobQueue.scala
@@ -1,7 +1,7 @@
package com.twitter.gizzard.scheduler
import java.util.{ArrayList => JArrayList}
-import java.util.concurrent.{LinkedBlockingQueue, TimeUnit}
+import java.util.concurrent.{LinkedBlockingQueue, TimeUnit, TimeoutException}
import scala.collection.jcl
import com.twitter.ostrich.Stats
import com.twitter.util.{Duration, Time}
@@ -34,9 +34,8 @@ class MemoryJobQueue[J <: Job](queueName: String, maxSize: Int) extends JobQueue
var expireDuration = 0.seconds
def put(job: J) {
- while (!queue.offer((job, Time.now + expireDuration))) {
- queue.poll(TIMEOUT, TimeUnit.MILLISECONDS)
- Stats.incr(queueName + "_discarded")
+ if (!queue.offer((job, Time.now + expireDuration))) {
+ throw new TimeoutException("queue full")
}
}
View
9 src/test/scala/com/twitter/gizzard/scheduler_new/MemoryJobQueueSpec.scala
@@ -1,5 +1,6 @@
package com.twitter.gizzard.scheduler
+import java.util.concurrent.TimeoutException
import scala.collection.mutable
import com.twitter.util.Time
import com.twitter.util.TimeConversions._
@@ -60,9 +61,13 @@ object MemoryJobQueueSpec extends ConfiguredSpecification with JMocker with Clas
"full" in {
queue.put(job1)
queue.put(job2)
- (0 until 19).foreach { n => queue.put(job3) }
+ try {
+ (0 until 19).foreach { n => queue.put(job3) }
+ } catch {
+ case e: TimeoutException => {}
+ }
queue.size mustEqual 20
- queue.get() must beSome[Ticket[Job]].which { _.job eq job2 }
+ queue.get() must beSome[Ticket[Job]].which { _.job eq job1 }
}
}

0 comments on commit 5ad4444

Please sign in to comment.