Skip to content

Commit

Permalink
Add some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshRosen committed Jul 14, 2015
1 parent 55c41d3 commit 1deed38
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions core/src/main/scala/org/apache/spark/FutureAction.scala
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ trait FutureAction[T] extends Future[T] {
class SimpleFutureAction[T] private[spark](jobWaiter: JobWaiter[_], resultFunc: => T)
extends FutureAction[T] {

// Note: `resultFunc` is a closure which may contain references to state that's updated by the
// JobWaiter's result handler function. It should only be evaluated once the job has succeeded.

@volatile private var _cancelled: Boolean = false
// Null until the job has completed, then holds a Try representing success or failure.
@volatile private var _value: Try[T] = null

override def cancel() {
Expand All @@ -117,18 +121,22 @@ class SimpleFutureAction[T] private[spark](jobWaiter: JobWaiter[_], resultFunc:
}

override def ready(atMost: Duration)(implicit permit: CanAwait): SimpleFutureAction.this.type = {
// This call to the JobWaiter's future will throw an exception if the job failed.
jobWaiter.toFuture.ready(atMost)(permit)
this
}

@throws(classOf[Exception])
override def result(atMost: Duration)(implicit permit: CanAwait): T = {
// This call to the JobWaiter's future will throw an exception if the job failed.
jobWaiter.toFuture.result(atMost)(permit)
// At this point, we know that the job succeeded so it's safe to evaluate this function:
resultFunc
}

override def onComplete[U](func: (Try[T]) => U)(implicit executor: ExecutionContext): Unit = {
jobWaiter.toFuture.onComplete { (jobWaiterResult: Try[Unit]) =>
// If the job succeeded, then evaluate the result function; otherwise, preserve the exception.
_value = jobWaiterResult.map(_ => resultFunc)
func(_value)
}
Expand Down

0 comments on commit 1deed38

Please sign in to comment.