Skip to content
This repository has been archived by the owner on Jan 20, 2022. It is now read-only.

Put all Collector actions in one execute #365

Merged
merged 15 commits into from
Nov 18, 2013
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@ object TimeExtractor {
}
}

trait TimeExtractor[T] extends (T => Long) with java.io.Serializable
/** This cannot be a subclass of function and use the pattern
* of implicit dependencies, since then you get an implicit function.
* Not good
*/
trait TimeExtractor[T] extends java.io.Serializable {
def apply(t: T): Long
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
Copyright 2013 Twitter, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.twitter.summingbird.online

import com.twitter.util.{ Await, Duration, Future, Try }

import java.util.Queue
import java.util.concurrent.{ArrayBlockingQueue, BlockingQueue, LinkedBlockingQueue, TimeUnit}
import java.util.concurrent.ConcurrentLinkedQueue
import java.util.concurrent.atomic.AtomicInteger
/**
*
* @author Oscar Boykin
*/

object Channel {
/**
* By default, don't block on put
*/
def apply[T]() = linkedNonBlocking[T]

def arrayBlocking[T](size: Int): Channel[T] =
new Channel[T](new ArrayBlockingQueue(size))

def linkedBlocking[T]: Channel[T] =
new Channel[T](new LinkedBlockingQueue())

def linkedNonBlocking[T]: Channel[T] =
new Channel[T](new ConcurrentLinkedQueue())
}

/**
* Use this class with a thread-safe queue to receive
* results from futures in one thread.
* Storm needs us to touch it's code in one event path (via
* the execute method in bolts)
*/
class Channel[T] private (queue: Queue[T]) {

private val count = new AtomicInteger(0)

def put(item: T): Int = {
queue.add(item)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was your intention for this to block if queue is an ArrayBlockingQueue with a fixed size? What actually will happen is that a java.lang.IllegalStateException: Queue full exception will be thrown instead of it actually blocking (you would need to call put instead for the blocking behavior).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch. Fixed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks nice!

count.incrementAndGet
}

/** Returns the size immediately after the put */
def putAll(items: TraversableOnce[T]): Int = {
val added = items.foldLeft(0) { (cnt, item) =>
queue.add(item)
cnt + 1
}
count.addAndGet(added)
}

/**
* check if something is ready now
*/
def poll: Option[T] = Option(queue.poll())

/**
* Obviously, this might not be the same by the time you
* call spill
*/
def size: Int = count.get

// Do something on all the elements ready:
@annotation.tailrec
final def foreach(fn: T => Unit): Unit =
queue.poll() match {
case null => ()
case itt => fn(itt); foreach(fn)
}

// fold on all the elements ready:
@annotation.tailrec
final def foldLeft[V](init: V)(fn: (V, T) => V): V = {
queue.poll() match {
case null => init
case itt => foldLeft(fn(init, itt))(fn)
}
}

/**
* Take enough elements to get the queue under the maxLength
*/
def trimTo(maxLength: Int): Seq[T] = {
require(maxLength >= 0, "maxLength must be >= 0.")

@annotation.tailrec
def loop(size: Int, acc: List[T] = Nil): List[T] = {
if(size > maxLength) {
queue.poll match {
case null => acc.reverse // someone else cleared us out
case item =>
loop(count.decrementAndGet, item::acc)
}
}
else acc.reverse
}
loop(count.get)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
Copyright 2013 Twitter, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.twitter.summingbird.online

import org.scalacheck._
import Gen._
import Arbitrary._
import org.scalacheck.Prop._

import com.twitter.util.{Return, Throw, Future, Try}

object QueueChannelLaws extends Properties("Channel") {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work with all the tests, the only one I don't see and am not sure matters is just ordering of put to poll

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will add.


property("Putting into a BoundedQueue gets size right") = forAll { (items: List[String]) =>
val q = Channel[String]()
q.putAll(items)
q.size == items.size
}
property("not spill if capacity is enough") = forAll { (items: List[Int]) =>
val q = Channel[Int]()
q.putAll(items)
q.trimTo(items.size).size == 0
}
property("Work with indepent additions") = forAll { (items: List[Int]) =>
val q = Channel[Int]()
items.map(q.put(_)) == (1 to items.size).toList
}
property("spill all with zero capacity") = forAll { (items: List[Int]) =>
val q = Channel[Int]()
q.putAll(items)
q.trimTo(0) == items
}
property("Channel works with finished futures") = forAll { (items: List[Int]) =>
val q = Channel.linkedBlocking[(Int,Try[Int])]
items.foreach { i => q.put((i, Try(i*i))) }
q.foldLeft((0, true)) { case ((cnt, good), (i, ti)) =>
ti match {
case Return(ii) => (cnt + 1, good)
case Throw(e) => (cnt + 1, false)
}
} == (items.size, true)
}
property("Channel.linkedNonBlocking works") = forAll { (items: List[Int]) =>
val q = Channel.linkedNonBlocking[(Int,Try[Int])]
items.foreach { i => q.put((i, Try(i*i))) }
q.foldLeft((0, true)) { case ((cnt, good), (i, ti)) =>
ti match {
case Return(ii) => (cnt + 1, good)
case Throw(e) => (cnt + 1, false)
}
} == (items.size, true)
}
property("Channel foreach works") = forAll { (items: List[Int]) =>
// Make sure we can fit everything
val q = Channel.arrayBlocking[(Int,Try[Int])](items.size + 1)
items.foreach { i => q.put((i,Try(i*i))) }
var works = true
q.foreach { case (i, Return(ii)) =>
works = works && (ii == i*i)
}
works
}
}
Loading