2012-01-18 11:52:35 +01:00
|
|
|
|
/**
|
2012-01-23 18:25:43 +01:00
|
|
|
|
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
2012-01-18 11:52:35 +01:00
|
|
|
|
*/
|
|
|
|
|
|
package akka.pattern
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.concurrent.TimeoutException
|
|
|
|
|
|
import akka.dispatch.{ Promise, Terminate, SystemMessage, Future }
|
|
|
|
|
|
import akka.event.DeathWatch
|
|
|
|
|
|
import akka.util.Timeout
|
2012-03-23 15:52:36 +01:00
|
|
|
|
import annotation.tailrec
|
2012-03-23 21:35:52 +01:00
|
|
|
|
import akka.util.Unsafe
|
2012-03-24 23:02:37 +01:00
|
|
|
|
import akka.actor._
|
2012-01-18 11:52:35 +01:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* This is what is used to complete a Future that is returned from an ask/? call,
|
|
|
|
|
|
* when it times out.
|
|
|
|
|
|
*/
|
2012-03-06 14:56:34 +01:00
|
|
|
|
class AskTimeoutException(message: String, cause: Throwable) extends TimeoutException(message) {
|
2012-01-18 11:52:35 +01:00
|
|
|
|
def this(message: String) = this(message, null: Throwable)
|
2012-03-06 14:56:34 +01:00
|
|
|
|
override def getCause(): Throwable = cause
|
2012-01-18 11:52:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-01-23 18:23:34 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* This object contains implementation details of the “ask” pattern.
|
|
|
|
|
|
*/
|
2012-02-01 13:37:57 +01:00
|
|
|
|
trait AskSupport {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Import this implicit conversion to gain `?` and `ask` methods on
|
|
|
|
|
|
* [[akka.actor.ActorRef]], which will defer to the
|
|
|
|
|
|
* `ask(actorRef, message)(timeout)` method defined here.
|
|
|
|
|
|
*
|
|
|
|
|
|
* {{{
|
|
|
|
|
|
* import akka.pattern.ask
|
|
|
|
|
|
*
|
|
|
|
|
|
* val future = actor ? message // => ask(actor, message)
|
|
|
|
|
|
* val future = actor ask message // => ask(actor, message)
|
|
|
|
|
|
* val future = actor.ask(message)(timeout) // => ask(actor, message)(timeout)
|
|
|
|
|
|
* }}}
|
|
|
|
|
|
*
|
|
|
|
|
|
* All of the above use an implicit [[akka.actor.Timeout]].
|
|
|
|
|
|
*/
|
|
|
|
|
|
implicit def ask(actorRef: ActorRef): AskableActorRef = new AskableActorRef(actorRef)
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Sends a message asynchronously and returns a [[akka.dispatch.Future]]
|
|
|
|
|
|
* holding the eventual reply message; this means that the target actor
|
|
|
|
|
|
* needs to send the result to the `sender` reference provided. The Future
|
2012-05-18 15:04:08 +02:00
|
|
|
|
* will be completed with an [[akka.pattern.AskTimeoutException]] after the
|
2012-02-01 13:37:57 +01:00
|
|
|
|
* given timeout has expired; this is independent from any timeout applied
|
|
|
|
|
|
* while awaiting a result for this future (i.e. in
|
|
|
|
|
|
* `Await.result(..., timeout)`).
|
|
|
|
|
|
*
|
|
|
|
|
|
* <b>Warning:</b>
|
|
|
|
|
|
* When using future callbacks, inside actors you need to carefully avoid closing over
|
|
|
|
|
|
* the containing actor’s object, i.e. do not call methods or access mutable state
|
|
|
|
|
|
* on the enclosing actor from within the callback. This would break the actor
|
|
|
|
|
|
* encapsulation and may introduce synchronization bugs and race conditions because
|
|
|
|
|
|
* the callback will be scheduled concurrently to the enclosing actor. Unfortunately
|
|
|
|
|
|
* there is not yet a way to detect these illegal accesses at compile time.
|
|
|
|
|
|
*
|
|
|
|
|
|
* <b>Recommended usage:</b>
|
|
|
|
|
|
*
|
|
|
|
|
|
* {{{
|
|
|
|
|
|
* val f = ask(worker, request)(timeout)
|
|
|
|
|
|
* flow {
|
|
|
|
|
|
* EnrichedRequest(request, f())
|
|
|
|
|
|
* } pipeTo nextActor
|
|
|
|
|
|
* }}}
|
|
|
|
|
|
*
|
|
|
|
|
|
* [see [[akka.dispatch.Future]] for a description of `flow`]
|
|
|
|
|
|
*/
|
|
|
|
|
|
def ask(actorRef: ActorRef, message: Any)(implicit timeout: Timeout): Future[Any] = actorRef match {
|
2012-05-03 21:14:47 +02:00
|
|
|
|
case ref: InternalActorRef if ref.isTerminated ⇒
|
2012-02-01 13:37:57 +01:00
|
|
|
|
actorRef.tell(message)
|
|
|
|
|
|
Promise.failed(new AskTimeoutException("sending to terminated ref breaks promises"))(ref.provider.dispatcher)
|
|
|
|
|
|
case ref: InternalActorRef ⇒
|
|
|
|
|
|
val provider = ref.provider
|
|
|
|
|
|
if (timeout.duration.length <= 0) {
|
|
|
|
|
|
actorRef.tell(message)
|
|
|
|
|
|
Promise.failed(new AskTimeoutException("not asking with negative timeout"))(provider.dispatcher)
|
|
|
|
|
|
} else {
|
2012-03-23 21:35:52 +01:00
|
|
|
|
val a = PromiseActorRef(provider, timeout)
|
2012-02-01 13:37:57 +01:00
|
|
|
|
actorRef.tell(message, a)
|
|
|
|
|
|
a.result
|
|
|
|
|
|
}
|
|
|
|
|
|
case _ ⇒ throw new IllegalArgumentException("incompatible ActorRef " + actorRef)
|
|
|
|
|
|
}
|
2012-01-18 11:52:35 +01:00
|
|
|
|
|
2012-01-23 18:23:34 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Implementation detail of the “ask” pattern enrichment of ActorRef
|
|
|
|
|
|
*/
|
|
|
|
|
|
private[akka] final class AskableActorRef(val actorRef: ActorRef) {
|
2012-01-18 11:52:35 +01:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Sends a message asynchronously and returns a [[akka.dispatch.Future]]
|
|
|
|
|
|
* holding the eventual reply message; this means that the target actor
|
|
|
|
|
|
* needs to send the result to the `sender` reference provided. The Future
|
2012-05-18 15:04:08 +02:00
|
|
|
|
* will be completed with an [[akka.pattern.AskTimeoutException]] after the
|
2012-01-18 11:52:35 +01:00
|
|
|
|
* given timeout has expired; this is independent from any timeout applied
|
|
|
|
|
|
* while awaiting a result for this future (i.e. in
|
|
|
|
|
|
* `Await.result(..., timeout)`).
|
|
|
|
|
|
*
|
|
|
|
|
|
* <b>Warning:</b>
|
|
|
|
|
|
* When using future callbacks, inside actors you need to carefully avoid closing over
|
|
|
|
|
|
* the containing actor’s object, i.e. do not call methods or access mutable state
|
|
|
|
|
|
* on the enclosing actor from within the callback. This would break the actor
|
|
|
|
|
|
* encapsulation and may introduce synchronization bugs and race conditions because
|
|
|
|
|
|
* the callback will be scheduled concurrently to the enclosing actor. Unfortunately
|
|
|
|
|
|
* there is not yet a way to detect these illegal accesses at compile time.
|
|
|
|
|
|
*
|
|
|
|
|
|
* <b>Recommended usage:</b>
|
|
|
|
|
|
*
|
|
|
|
|
|
* {{{
|
|
|
|
|
|
* flow {
|
2012-01-23 18:23:34 +01:00
|
|
|
|
* val f = worker.ask(request)(timeout)
|
2012-01-18 11:52:35 +01:00
|
|
|
|
* EnrichedRequest(request, f())
|
|
|
|
|
|
* } pipeTo nextActor
|
|
|
|
|
|
* }}}
|
|
|
|
|
|
*
|
|
|
|
|
|
* [see the [[akka.dispatch.Future]] companion object for a description of `flow`]
|
|
|
|
|
|
*/
|
2012-01-23 15:59:18 +01:00
|
|
|
|
def ask(message: Any)(implicit timeout: Timeout): Future[Any] = akka.pattern.ask(actorRef, message)(timeout)
|
2012-01-18 11:52:35 +01:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Sends a message asynchronously and returns a [[akka.dispatch.Future]]
|
|
|
|
|
|
* holding the eventual reply message; this means that the target actor
|
|
|
|
|
|
* needs to send the result to the `sender` reference provided. The Future
|
2012-05-18 15:04:08 +02:00
|
|
|
|
* will be completed with an [[akka.pattern.AskTimeoutException]] after the
|
2012-01-18 11:52:35 +01:00
|
|
|
|
* given timeout has expired; this is independent from any timeout applied
|
|
|
|
|
|
* while awaiting a result for this future (i.e. in
|
|
|
|
|
|
* `Await.result(..., timeout)`).
|
|
|
|
|
|
*
|
|
|
|
|
|
* <b>Warning:</b>
|
|
|
|
|
|
* When using future callbacks, inside actors you need to carefully avoid closing over
|
|
|
|
|
|
* the containing actor’s object, i.e. do not call methods or access mutable state
|
|
|
|
|
|
* on the enclosing actor from within the callback. This would break the actor
|
|
|
|
|
|
* encapsulation and may introduce synchronization bugs and race conditions because
|
|
|
|
|
|
* the callback will be scheduled concurrently to the enclosing actor. Unfortunately
|
|
|
|
|
|
* there is not yet a way to detect these illegal accesses at compile time.
|
|
|
|
|
|
*
|
|
|
|
|
|
* <b>Recommended usage:</b>
|
|
|
|
|
|
*
|
|
|
|
|
|
* {{{
|
|
|
|
|
|
* flow {
|
2012-01-23 18:23:34 +01:00
|
|
|
|
* val f = worker ? request
|
2012-01-18 11:52:35 +01:00
|
|
|
|
* EnrichedRequest(request, f())
|
|
|
|
|
|
* } pipeTo nextActor
|
|
|
|
|
|
* }}}
|
|
|
|
|
|
*
|
|
|
|
|
|
* [see the [[akka.dispatch.Future]] companion object for a description of `flow`]
|
|
|
|
|
|
*/
|
2012-01-23 15:59:18 +01:00
|
|
|
|
def ?(message: Any)(implicit timeout: Timeout): Future[Any] = akka.pattern.ask(actorRef, message)(timeout)
|
2012-01-18 11:52:35 +01:00
|
|
|
|
}
|
2012-03-23 21:35:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Akka private optimized representation of the temporary actor spawned to
|
|
|
|
|
|
* receive the reply to an "ask" operation.
|
|
|
|
|
|
*/
|
2012-03-27 09:28:54 +02:00
|
|
|
|
private[akka] final class PromiseActorRef private (val provider: ActorRefProvider, val result: Promise[Any])
|
|
|
|
|
|
extends MinimalActorRef {
|
2012-03-23 21:35:52 +01:00
|
|
|
|
import PromiseActorRef._
|
2012-03-24 23:02:37 +01:00
|
|
|
|
import AbstractPromiseActorRef.stateOffset
|
2012-01-18 11:52:35 +01:00
|
|
|
|
|
|
|
|
|
|
/**
|
2012-03-23 21:35:52 +01:00
|
|
|
|
* As an optimization for the common (local) case we only register this PromiseActorRef
|
|
|
|
|
|
* with the provider when the `path` member is actually queried, which happens during
|
2012-03-24 23:02:37 +01:00
|
|
|
|
* serialization (but also during a simple call to `toString`, `equals` or `hashCode`!).
|
2012-03-23 21:35:52 +01:00
|
|
|
|
*
|
|
|
|
|
|
* Defined states:
|
2012-03-24 23:02:37 +01:00
|
|
|
|
* null => started, path not yet created
|
|
|
|
|
|
* Registering => currently creating temp path and registering it
|
|
|
|
|
|
* path: ActorPath => path is available and was registered
|
|
|
|
|
|
* StoppedWithPath(path) => stopped, path available
|
|
|
|
|
|
* Stopped => stopped, path not yet created
|
2012-01-18 11:52:35 +01:00
|
|
|
|
*/
|
2012-03-23 21:35:52 +01:00
|
|
|
|
@volatile
|
2012-03-27 09:28:54 +02:00
|
|
|
|
private[this] var _stateDoNotCallMeDirectly: AnyRef = _
|
2012-01-18 11:52:35 +01:00
|
|
|
|
|
2012-03-23 21:35:52 +01:00
|
|
|
|
@inline
|
2012-03-24 23:02:37 +01:00
|
|
|
|
private def state: AnyRef = Unsafe.instance.getObjectVolatile(this, stateOffset)
|
|
|
|
|
|
|
|
|
|
|
|
@inline
|
|
|
|
|
|
private def updateState(oldState: AnyRef, newState: AnyRef): Boolean =
|
|
|
|
|
|
Unsafe.instance.compareAndSwapObject(this, stateOffset, oldState, newState)
|
|
|
|
|
|
|
|
|
|
|
|
@inline
|
2012-03-27 09:28:54 +02:00
|
|
|
|
private def setState(newState: AnyRef): Unit =
|
2012-03-24 23:02:37 +01:00
|
|
|
|
Unsafe.instance.putObjectVolatile(this, stateOffset, newState)
|
2012-03-23 15:52:36 +01:00
|
|
|
|
|
2012-03-27 09:28:54 +02:00
|
|
|
|
override def getParent = provider.tempContainer
|
|
|
|
|
|
|
2012-03-23 21:35:52 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Contract of this method:
|
2012-03-24 23:02:37 +01:00
|
|
|
|
* Must always return the same ActorPath, which must have
|
2012-03-23 21:35:52 +01:00
|
|
|
|
* been registered if we haven't been stopped yet.
|
|
|
|
|
|
*/
|
|
|
|
|
|
@tailrec
|
|
|
|
|
|
def path: ActorPath = state match {
|
|
|
|
|
|
case null ⇒
|
2012-03-24 23:02:37 +01:00
|
|
|
|
if (updateState(null, Registering)) {
|
2012-03-23 21:35:52 +01:00
|
|
|
|
var p: ActorPath = null
|
|
|
|
|
|
try {
|
|
|
|
|
|
p = provider.tempPath()
|
2012-03-23 15:52:36 +01:00
|
|
|
|
provider.registerTempActor(this, p)
|
|
|
|
|
|
p
|
2012-03-24 23:02:37 +01:00
|
|
|
|
} finally { setState(p) }
|
2012-03-23 21:35:52 +01:00
|
|
|
|
} else path
|
2012-03-24 23:02:37 +01:00
|
|
|
|
case p: ActorPath ⇒ p
|
|
|
|
|
|
case StoppedWithPath(p) ⇒ p
|
2012-03-23 21:35:52 +01:00
|
|
|
|
case Stopped ⇒
|
|
|
|
|
|
// even if we are already stopped we still need to produce a proper path
|
2012-03-24 23:02:37 +01:00
|
|
|
|
updateState(Stopped, StoppedWithPath(provider.tempPath()))
|
2012-03-23 21:35:52 +01:00
|
|
|
|
path
|
|
|
|
|
|
case Registering ⇒ path // spin until registration is completed
|
|
|
|
|
|
}
|
2012-01-18 11:52:35 +01:00
|
|
|
|
|
2012-03-23 21:35:52 +01:00
|
|
|
|
override def !(message: Any)(implicit sender: ActorRef = null): Unit = state match {
|
2012-03-24 23:02:37 +01:00
|
|
|
|
case Stopped | _: StoppedWithPath ⇒ provider.deadLetters ! message
|
|
|
|
|
|
case _ ⇒
|
|
|
|
|
|
val completedJustNow = result.tryComplete {
|
|
|
|
|
|
message match {
|
|
|
|
|
|
case Status.Success(r) ⇒ Right(r)
|
|
|
|
|
|
case Status.Failure(f) ⇒ Left(f)
|
|
|
|
|
|
case other ⇒ Right(other)
|
|
|
|
|
|
}
|
2012-03-23 10:27:17 +01:00
|
|
|
|
}
|
2012-03-24 23:02:37 +01:00
|
|
|
|
if (!completedJustNow) provider.deadLetters ! message
|
2012-03-23 21:35:52 +01:00
|
|
|
|
}
|
2012-01-18 11:52:35 +01:00
|
|
|
|
|
2012-03-23 21:35:52 +01:00
|
|
|
|
override def sendSystemMessage(message: SystemMessage): Unit = message match {
|
|
|
|
|
|
case _: Terminate ⇒ stop()
|
|
|
|
|
|
case _ ⇒
|
|
|
|
|
|
}
|
2012-01-18 11:52:35 +01:00
|
|
|
|
|
2012-05-03 21:14:47 +02:00
|
|
|
|
override def isTerminated = state match {
|
2012-03-24 23:02:37 +01:00
|
|
|
|
case Stopped | _: StoppedWithPath ⇒ true
|
|
|
|
|
|
case _ ⇒ false
|
|
|
|
|
|
}
|
2012-01-18 11:52:35 +01:00
|
|
|
|
|
2012-03-23 21:35:52 +01:00
|
|
|
|
@tailrec
|
2012-03-24 23:02:37 +01:00
|
|
|
|
override def stop(): Unit = {
|
|
|
|
|
|
def ensurePromiseCompleted(): Unit =
|
2012-05-03 21:14:47 +02:00
|
|
|
|
if (!result.isCompleted) result.tryComplete(Left(new ActorKilledException("Stopped")))
|
2012-03-24 23:02:37 +01:00
|
|
|
|
state match {
|
|
|
|
|
|
case null ⇒
|
|
|
|
|
|
// if path was never queried nobody can possibly be watching us, so we don't have to publish termination either
|
|
|
|
|
|
if (updateState(null, Stopped)) ensurePromiseCompleted()
|
|
|
|
|
|
else stop()
|
|
|
|
|
|
case p: ActorPath ⇒
|
|
|
|
|
|
if (updateState(p, StoppedWithPath(p))) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
ensurePromiseCompleted()
|
2012-03-27 09:28:54 +02:00
|
|
|
|
provider.deathWatch.publish(Terminated(this))
|
2012-03-24 23:02:37 +01:00
|
|
|
|
} finally {
|
|
|
|
|
|
provider.unregisterTempActor(p)
|
|
|
|
|
|
}
|
|
|
|
|
|
} else stop()
|
|
|
|
|
|
case Stopped | _: StoppedWithPath ⇒
|
|
|
|
|
|
case Registering ⇒ stop() // spin until registration is completed before stopping
|
|
|
|
|
|
}
|
2012-01-18 11:52:35 +01:00
|
|
|
|
}
|
2012-03-23 21:35:52 +01:00
|
|
|
|
}
|
2012-01-18 11:52:35 +01:00
|
|
|
|
|
2012-03-23 21:35:52 +01:00
|
|
|
|
private[akka] object PromiseActorRef {
|
2012-03-24 23:02:37 +01:00
|
|
|
|
private case object Registering
|
|
|
|
|
|
private case object Stopped
|
|
|
|
|
|
private case class StoppedWithPath(path: ActorPath)
|
2012-03-23 21:35:52 +01:00
|
|
|
|
|
|
|
|
|
|
def apply(provider: ActorRefProvider, timeout: Timeout): PromiseActorRef = {
|
2012-01-18 11:52:35 +01:00
|
|
|
|
val result = Promise[Any]()(provider.dispatcher)
|
2012-03-27 09:28:54 +02:00
|
|
|
|
val a = new PromiseActorRef(provider, result)
|
2012-02-21 12:14:13 +01:00
|
|
|
|
val f = provider.scheduler.scheduleOnce(timeout.duration) { result.tryComplete(Left(new AskTimeoutException("Timed out"))) }
|
2012-05-18 14:55:38 +02:00
|
|
|
|
result onComplete { _ ⇒ try a.stop() finally f.cancel() }
|
2012-01-18 11:52:35 +01:00
|
|
|
|
a
|
|
|
|
|
|
}
|
2012-05-03 21:14:47 +02:00
|
|
|
|
}
|