-
Notifications
You must be signed in to change notification settings - Fork 708
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add failing tests * add another failing test to test writes * wip try to thread CancellationHandler through * finish threading cancellation handler through * try to fix test * wip cfuture/cpromise * test passes, everything compiles * reenable tests * remove sys error * fix indenting * don't recover on flow stop * move failFastSequence to CFuture; add CFuture helper methods * uncancellable * move CFuture/CPromise * CFuture.fromFuture * add typeclass * remove cec * execution changes * future cache backwards compatible * AsyncFlowDef fixes * change jdk * add failing test * delete failing test * add flatmap test * add flow step listener * tryFailure * handle null throwable * respond to review * stop flow in promise
- Loading branch information
Showing
13 changed files
with
555 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
language: scala | ||
jdk: oraclejdk8 | ||
jdk: openjdk8 | ||
sudo: false | ||
|
||
before_install: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
scalding-core/src/main/scala/com/twitter/scalding/CFuture.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.twitter.scalding | ||
|
||
import scala.concurrent.{ Future, ExecutionContext => ConcurrentExecutionContext } | ||
|
||
/** | ||
* Represents a cancellable future. | ||
*/ | ||
case class CFuture[+T](future: Future[T], cancellationHandler: CancellationHandler) { | ||
def map[S](fn: T => S)(implicit cec: ConcurrentExecutionContext): CFuture[S] = { | ||
val mapped = future.map(fn) | ||
CFuture(mapped, cancellationHandler) | ||
} | ||
|
||
def mapFuture[S](fn: Future[T] => Future[S]): CFuture[S] = { | ||
val transformed = fn(future) | ||
CFuture(transformed, cancellationHandler) | ||
} | ||
|
||
def zip[U](other: CFuture[U])(implicit cec: ConcurrentExecutionContext): CFuture[(T, U)] = { | ||
val zippedFut: Future[(T, U)] = Execution.failFastZip(future, other.future) | ||
val cancelHandler = cancellationHandler.compose(other.cancellationHandler) | ||
|
||
CFuture(zippedFut, cancelHandler) | ||
} | ||
} | ||
|
||
object CFuture { | ||
def successful[T](result: T): CFuture[T] = { | ||
CFuture(Future.successful(result), CancellationHandler.empty) | ||
} | ||
|
||
def failed(t: Throwable): CFuture[Nothing] = { | ||
val f = Future.failed(t) | ||
CFuture(f, CancellationHandler.empty) | ||
} | ||
|
||
def uncancellable[T](fut: Future[T]): CFuture[T] = { | ||
CFuture(fut, CancellationHandler.empty) | ||
} | ||
|
||
def fromFuture[T](fut: Future[CFuture[T]])(implicit cec: ConcurrentExecutionContext): CFuture[T] = { | ||
CFuture(fut.flatMap(_.future), CancellationHandler.fromFuture(fut.map(_.cancellationHandler))) | ||
} | ||
|
||
/** | ||
* Use our internal faster failing zip function rather than the standard one due to waiting | ||
*/ | ||
def failFastSequence[T](t: Iterable[CFuture[T]])(implicit cec: ConcurrentExecutionContext): CFuture[List[T]] = { | ||
t.foldLeft(CFuture.successful(Nil: List[T])) { (f, i) => | ||
f.zip(i).map { case (tail, h) => h :: tail } | ||
}.map(_.reverse) | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
scalding-core/src/main/scala/com/twitter/scalding/CPromise.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.twitter.scalding | ||
|
||
import scala.concurrent.{ Future, Promise, ExecutionContext => ConcurrentExecutionContext } | ||
|
||
/** | ||
* Represents a cancellable promise. | ||
*/ | ||
case class CPromise[T](promise: Promise[T], cancellationHandler: Promise[CancellationHandler]) { | ||
/** | ||
* Creates a CFuture using the given promises. | ||
*/ | ||
def cfuture: CFuture[T] = { | ||
CFuture(promise.future, CancellationHandler.fromFuture(cancellationHandler.future)) | ||
} | ||
|
||
def completeWith(other: CFuture[T]): this.type = { | ||
// fullfill the main and cancellation handler promises | ||
promise.completeWith(other.future) | ||
cancellationHandler.completeWith(Future.successful(other.cancellationHandler)) | ||
this | ||
} | ||
} | ||
object CPromise { | ||
def apply[T](): CPromise[T] = CPromise(Promise[T](), Promise[CancellationHandler]()) | ||
} |
28 changes: 28 additions & 0 deletions
28
scalding-core/src/main/scala/com/twitter/scalding/CancellationHandler.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.twitter.scalding | ||
|
||
import scala.concurrent.{ Future, ExecutionContext => ConcurrentExecutionContext } | ||
|
||
sealed trait CancellationHandler { outer => | ||
def stop()(implicit ec: ConcurrentExecutionContext): Future[Unit] | ||
def compose(other: CancellationHandler): CancellationHandler = new CancellationHandler { | ||
override def stop()(implicit ec: ConcurrentExecutionContext): Future[Unit] = { | ||
other.stop().zip(outer.stop()).map(_ => ()) | ||
} | ||
} | ||
} | ||
|
||
object CancellationHandler { | ||
val empty: CancellationHandler = new CancellationHandler { | ||
def stop()(implicit ec: ConcurrentExecutionContext): Future[Unit] = Future.successful(()) | ||
} | ||
|
||
def fromFn(fn: ConcurrentExecutionContext => Future[Unit]): CancellationHandler = new CancellationHandler { | ||
override def stop()(implicit ec: ConcurrentExecutionContext): Future[Unit] = fn(ec) | ||
} | ||
|
||
def fromFuture(f: Future[CancellationHandler]): CancellationHandler = new CancellationHandler { | ||
override def stop()(implicit ec: ConcurrentExecutionContext): Future[Unit] = { | ||
f.flatMap(_.stop()) | ||
} | ||
} | ||
} |
Oops, something went wrong.