2011-05-19 21:34:21 +02:00
|
|
|
/**
|
2012-01-19 18:21:06 +01:00
|
|
|
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
2011-05-19 21:34:21 +02:00
|
|
|
*/
|
2011-03-06 22:45:44 +01:00
|
|
|
package akka.testkit
|
|
|
|
|
|
2012-06-21 16:09:14 +02:00
|
|
|
import language.postfixOps
|
|
|
|
|
|
2012-01-24 11:33:40 +01:00
|
|
|
import java.lang.ref.WeakReference
|
2011-03-06 22:45:44 +01:00
|
|
|
import java.util.concurrent.locks.ReentrantLock
|
|
|
|
|
import java.util.LinkedList
|
|
|
|
|
import scala.annotation.tailrec
|
2011-12-20 21:08:27 +01:00
|
|
|
import com.typesafe.config.Config
|
2012-02-13 15:33:31 +01:00
|
|
|
import akka.actor.{ ActorInitializationException, ExtensionIdProvider, ExtensionId, Extension, ExtendedActorSystem, ActorRef, ActorCell }
|
2012-02-19 10:28:56 +01:00
|
|
|
import akka.dispatch.{ MailboxType, TaskInvocation, SystemMessage, Suspend, Resume, MessageDispatcherConfigurator, MessageDispatcher, Mailbox, Envelope, DispatcherPrerequisites, DefaultSystemMessageQueue }
|
2012-06-29 14:07:38 +02:00
|
|
|
import scala.concurrent.util.duration.intToDurationInt
|
2012-06-29 13:33:20 +02:00
|
|
|
import akka.util.{ Switch, NonFatal }
|
|
|
|
|
import scala.concurrent.util.Duration
|
2012-06-29 16:06:26 +02:00
|
|
|
import scala.concurrent.Awaitable
|
2012-02-19 10:28:56 +01:00
|
|
|
import akka.actor.ActorContext
|
|
|
|
|
import akka.dispatch.MessageQueue
|
2012-01-24 11:33:40 +01:00
|
|
|
|
2011-03-06 22:45:44 +01:00
|
|
|
/*
|
|
|
|
|
* Locking rules:
|
|
|
|
|
*
|
2011-09-21 15:01:47 +02:00
|
|
|
* While not suspendSwitch, messages are processed (!isActive) or queued
|
|
|
|
|
* thread-locally (isActive). While suspendSwitch, messages are queued
|
2011-03-06 22:45:44 +01:00
|
|
|
* thread-locally. When resuming, all messages are atomically scooped from all
|
|
|
|
|
* non-active threads and queued on the resuming thread's queue, to be
|
|
|
|
|
* processed immediately. Processing a queue checks suspend before each
|
2011-09-21 15:01:47 +02:00
|
|
|
* invocation, leaving the active state if suspendSwitch. For this to work
|
2011-03-06 22:45:44 +01:00
|
|
|
* reliably, the active flag needs to be set atomically with the initial check
|
|
|
|
|
* for suspend. Scooping up messages means replacing the ThreadLocal's contents
|
|
|
|
|
* with an empty new NestingQueue.
|
|
|
|
|
*
|
2011-09-21 15:01:47 +02:00
|
|
|
* All accesses to the queue must be done under the suspendSwitch-switch's lock, so
|
2011-03-06 22:45:44 +01:00
|
|
|
* within one of its methods taking a closure argument.
|
|
|
|
|
*/
|
|
|
|
|
|
2011-11-30 15:16:20 +01:00
|
|
|
private[testkit] object CallingThreadDispatcherQueues extends ExtensionId[CallingThreadDispatcherQueues] with ExtensionIdProvider {
|
|
|
|
|
override def lookup = CallingThreadDispatcherQueues
|
2012-01-24 11:33:40 +01:00
|
|
|
override def createExtension(system: ExtendedActorSystem): CallingThreadDispatcherQueues = new CallingThreadDispatcherQueues
|
2011-11-30 15:16:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private[testkit] class CallingThreadDispatcherQueues extends Extension {
|
2011-04-16 22:20:04 +02:00
|
|
|
|
|
|
|
|
// PRIVATE DATA
|
|
|
|
|
|
2011-03-06 22:45:44 +01:00
|
|
|
private var queues = Map[CallingThreadMailbox, Set[WeakReference[NestingQueue]]]()
|
2011-10-21 18:47:44 +02:00
|
|
|
private var lastGC = 0l
|
2011-03-06 22:45:44 +01:00
|
|
|
|
|
|
|
|
// we have to forget about long-gone threads sometime
|
|
|
|
|
private def gc {
|
|
|
|
|
queues = queues mapValues (_ filter (_.get ne null)) filter (!_._2.isEmpty)
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-30 15:50:52 +02:00
|
|
|
protected[akka] def registerQueue(mbox: CallingThreadMailbox, q: NestingQueue): Unit = synchronized {
|
2011-03-06 22:45:44 +01:00
|
|
|
if (queues contains mbox) {
|
|
|
|
|
val newSet = queues(mbox) + new WeakReference(q)
|
|
|
|
|
queues += mbox -> newSet
|
|
|
|
|
} else {
|
|
|
|
|
queues += mbox -> Set(new WeakReference(q))
|
|
|
|
|
}
|
2011-10-21 18:47:44 +02:00
|
|
|
val now = System.nanoTime
|
|
|
|
|
if (now - lastGC > 1000000000l) {
|
|
|
|
|
lastGC = now
|
|
|
|
|
gc
|
|
|
|
|
}
|
2011-03-06 22:45:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* This method must be called with "own" being this thread's queue for the
|
|
|
|
|
* given mailbox. When this method returns, the queue will be entered
|
|
|
|
|
* (active).
|
|
|
|
|
*/
|
2011-10-21 18:47:44 +02:00
|
|
|
protected[akka] def gatherFromAllOtherQueues(mbox: CallingThreadMailbox, own: NestingQueue): Unit = synchronized {
|
2011-03-06 22:45:44 +01:00
|
|
|
if (!own.isActive) own.enter
|
|
|
|
|
if (queues contains mbox) {
|
|
|
|
|
for {
|
2011-05-18 17:25:30 +02:00
|
|
|
ref ← queues(mbox)
|
2012-06-15 13:04:10 +02:00
|
|
|
q = ref.get
|
2011-10-21 18:47:44 +02:00
|
|
|
if (q ne null) && (q ne own)
|
2011-03-06 22:45:44 +01:00
|
|
|
} {
|
2012-02-19 10:28:56 +01:00
|
|
|
val owner = mbox.actor.self
|
|
|
|
|
var msg = q.q.dequeue()
|
|
|
|
|
while (msg ne null) {
|
2011-10-21 18:47:44 +02:00
|
|
|
// this is safe because this method is only ever called while holding the suspendSwitch monitor
|
2012-02-19 10:28:56 +01:00
|
|
|
own.q.enqueue(owner, msg)
|
|
|
|
|
msg = q.q.dequeue()
|
2011-03-06 22:45:44 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-20 21:08:27 +01:00
|
|
|
object CallingThreadDispatcher {
|
2011-12-21 19:02:06 +01:00
|
|
|
val Id = "akka.test.calling-thread-dispatcher"
|
2011-12-20 21:08:27 +01:00
|
|
|
}
|
|
|
|
|
|
2011-03-06 22:45:44 +01:00
|
|
|
/**
|
|
|
|
|
* Dispatcher which runs invocations on the current thread only. This
|
|
|
|
|
* dispatcher does not create any new threads, but it can be used from
|
|
|
|
|
* different threads concurrently for the same actor. The dispatch strategy is
|
2011-09-21 15:01:47 +02:00
|
|
|
* to run on the current thread unless the target actor is either suspendSwitch or
|
2011-03-06 22:45:44 +01:00
|
|
|
* already running on the current thread (if it is running on a different
|
|
|
|
|
* thread, then this thread will block until that other invocation is
|
|
|
|
|
* finished); if the invocation is not run, it is queued in a thread-local
|
|
|
|
|
* queue to be executed once the active invocation further up the call stack
|
|
|
|
|
* finishes. This leads to completely deterministic execution order if only one
|
|
|
|
|
* thread is used.
|
|
|
|
|
*
|
|
|
|
|
* Suspending and resuming are global actions for one actor, meaning they can
|
|
|
|
|
* affect different threads, which leads to complications. If messages are
|
2011-09-21 15:01:47 +02:00
|
|
|
* queued (thread-locally) during the suspendSwitch period, the only thread to run
|
2011-03-06 22:45:44 +01:00
|
|
|
* them upon resume is the thread actually calling the resume method. Hence,
|
|
|
|
|
* all thread-local queues which are not currently being drained (possible,
|
|
|
|
|
* since suspend-queue-resume might happen entirely during an invocation on a
|
|
|
|
|
* different thread) are scooped up into the current thread-local queue which
|
|
|
|
|
* is then executed. It is possible to suspend an actor from within its call
|
|
|
|
|
* stack.
|
|
|
|
|
*
|
|
|
|
|
* @author Roland Kuhn
|
|
|
|
|
* @since 1.1
|
|
|
|
|
*/
|
2011-11-14 16:03:26 +01:00
|
|
|
class CallingThreadDispatcher(
|
2011-11-17 16:09:18 +01:00
|
|
|
_prerequisites: DispatcherPrerequisites,
|
2012-02-19 10:28:56 +01:00
|
|
|
val mailboxType: MailboxType,
|
2011-11-17 16:09:18 +01:00
|
|
|
val name: String = "calling-thread") extends MessageDispatcher(_prerequisites) {
|
2011-03-06 22:45:44 +01:00
|
|
|
import CallingThreadDispatcher._
|
|
|
|
|
|
2011-11-18 11:59:43 +01:00
|
|
|
val log = akka.event.Logging(prerequisites.eventStream, "CallingThreadDispatcher")
|
|
|
|
|
|
2011-12-21 19:02:06 +01:00
|
|
|
override def id: String = Id
|
2011-12-20 21:08:27 +01:00
|
|
|
|
2012-06-13 17:57:56 +02:00
|
|
|
protected[akka] override def createMailbox(actor: akka.actor.Cell) = new CallingThreadMailbox(actor, mailboxType)
|
2011-03-06 22:45:44 +01:00
|
|
|
|
2011-08-30 15:50:52 +02:00
|
|
|
protected[akka] override def shutdown() {}
|
2011-03-06 22:45:44 +01:00
|
|
|
|
2011-09-21 15:01:47 +02:00
|
|
|
protected[akka] override def throughput = 0
|
2011-11-21 10:48:21 +01:00
|
|
|
protected[akka] override def throughputDeadlineTime = Duration.Zero
|
2011-09-23 13:14:17 +02:00
|
|
|
protected[akka] override def registerForExecution(mbox: Mailbox, hasMessageHint: Boolean, hasSystemMessageHint: Boolean): Boolean = false
|
2011-09-21 15:01:47 +02:00
|
|
|
|
2011-12-02 10:32:17 +01:00
|
|
|
protected[akka] override def shutdownTimeout = 1 second
|
2011-03-06 22:45:44 +01:00
|
|
|
|
2012-02-13 15:33:31 +01:00
|
|
|
protected[akka] override def register(actor: ActorCell): Unit = {
|
|
|
|
|
super.register(actor)
|
|
|
|
|
actor.mailbox match {
|
|
|
|
|
case mbox: CallingThreadMailbox ⇒
|
|
|
|
|
val queue = mbox.queue
|
|
|
|
|
queue.enter
|
|
|
|
|
runQueue(mbox, queue)
|
|
|
|
|
case x ⇒ throw new ActorInitializationException("expected CallingThreadMailbox, got " + x.getClass)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-20 15:43:57 +02:00
|
|
|
override def suspend(actor: ActorCell) {
|
2012-01-10 13:33:57 +01:00
|
|
|
actor.mailbox match {
|
|
|
|
|
case m: CallingThreadMailbox ⇒ m.suspendSwitch.switchOn
|
|
|
|
|
case m ⇒ m.systemEnqueue(actor.self, Suspend())
|
|
|
|
|
}
|
2011-03-06 22:45:44 +01:00
|
|
|
}
|
|
|
|
|
|
2011-09-20 15:43:57 +02:00
|
|
|
override def resume(actor: ActorCell) {
|
2011-11-12 10:57:28 +01:00
|
|
|
actor.mailbox match {
|
|
|
|
|
case mbox: CallingThreadMailbox ⇒
|
|
|
|
|
val queue = mbox.queue
|
|
|
|
|
val wasActive = queue.isActive
|
|
|
|
|
val switched = mbox.suspendSwitch.switchOff {
|
2011-11-30 15:16:20 +01:00
|
|
|
CallingThreadDispatcherQueues(actor.system).gatherFromAllOtherQueues(mbox, queue)
|
2011-11-12 10:57:28 +01:00
|
|
|
}
|
|
|
|
|
if (switched && !wasActive) {
|
|
|
|
|
runQueue(mbox, queue)
|
|
|
|
|
}
|
|
|
|
|
case m ⇒ m.systemEnqueue(actor.self, Resume())
|
2011-03-06 22:45:44 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-18 16:44:35 +02:00
|
|
|
protected[akka] override def systemDispatch(receiver: ActorCell, message: SystemMessage) {
|
2011-11-12 10:57:28 +01:00
|
|
|
receiver.mailbox match {
|
|
|
|
|
case mbox: CallingThreadMailbox ⇒
|
|
|
|
|
mbox.systemEnqueue(receiver.self, message)
|
|
|
|
|
val queue = mbox.queue
|
|
|
|
|
if (!queue.isActive) {
|
|
|
|
|
queue.enter
|
|
|
|
|
runQueue(mbox, queue)
|
|
|
|
|
}
|
|
|
|
|
case m ⇒ m.systemEnqueue(receiver.self, message)
|
2011-09-21 08:25:08 +02:00
|
|
|
}
|
2011-09-20 18:34:21 +02:00
|
|
|
}
|
|
|
|
|
|
2011-10-19 13:19:44 +02:00
|
|
|
protected[akka] override def dispatch(receiver: ActorCell, handle: Envelope) {
|
2011-11-12 10:57:28 +01:00
|
|
|
receiver.mailbox match {
|
|
|
|
|
case mbox: CallingThreadMailbox ⇒
|
|
|
|
|
val queue = mbox.queue
|
|
|
|
|
val execute = mbox.suspendSwitch.fold {
|
2012-02-19 10:28:56 +01:00
|
|
|
queue.q.enqueue(receiver.self, handle)
|
2011-11-12 10:57:28 +01:00
|
|
|
false
|
|
|
|
|
} {
|
2012-02-19 10:28:56 +01:00
|
|
|
queue.q.enqueue(receiver.self, handle)
|
2012-01-10 13:33:57 +01:00
|
|
|
if (!queue.isActive) {
|
2011-11-12 10:57:28 +01:00
|
|
|
queue.enter
|
|
|
|
|
true
|
2012-01-10 13:33:57 +01:00
|
|
|
} else false
|
2011-11-12 10:57:28 +01:00
|
|
|
}
|
|
|
|
|
if (execute) runQueue(mbox, queue)
|
2012-02-21 13:22:25 +01:00
|
|
|
case m ⇒ m.enqueue(receiver.self, handle)
|
2011-05-18 17:25:30 +02:00
|
|
|
}
|
2011-03-06 22:45:44 +01:00
|
|
|
}
|
|
|
|
|
|
2011-08-30 15:50:52 +02:00
|
|
|
protected[akka] override def executeTask(invocation: TaskInvocation) { invocation.run }
|
2011-03-17 22:18:39 +01:00
|
|
|
|
2011-03-06 22:45:44 +01:00
|
|
|
/*
|
|
|
|
|
* This method must be called with this thread's queue, which must already
|
|
|
|
|
* have been entered (active). When this method returns, the queue will be
|
|
|
|
|
* inactive.
|
|
|
|
|
*
|
|
|
|
|
* If the catch block is executed, then a non-empty mailbox may be stalled as
|
|
|
|
|
* there is no-one who cares to execute it before the next message is sent or
|
2011-09-21 15:01:47 +02:00
|
|
|
* it is suspendSwitch and resumed.
|
2011-03-06 22:45:44 +01:00
|
|
|
*/
|
2011-05-18 17:25:30 +02:00
|
|
|
@tailrec
|
2011-10-25 15:07:20 +02:00
|
|
|
private def runQueue(mbox: CallingThreadMailbox, queue: NestingQueue, interruptedex: InterruptedException = null) {
|
|
|
|
|
var intex = interruptedex;
|
2011-03-06 22:45:44 +01:00
|
|
|
assert(queue.isActive)
|
2012-01-10 13:33:57 +01:00
|
|
|
mbox.ctdLock.lock
|
2011-03-06 22:45:44 +01:00
|
|
|
val recurse = try {
|
2011-10-21 18:47:44 +02:00
|
|
|
mbox.processAllSystemMessages()
|
2011-09-21 15:01:47 +02:00
|
|
|
val handle = mbox.suspendSwitch.fold[Envelope] {
|
2011-05-18 17:25:30 +02:00
|
|
|
queue.leave
|
|
|
|
|
null
|
|
|
|
|
} {
|
2012-02-19 10:28:56 +01:00
|
|
|
val ret = if (mbox.isClosed) null else queue.q.dequeue()
|
2011-05-18 17:25:30 +02:00
|
|
|
if (ret eq null) queue.leave
|
|
|
|
|
ret
|
|
|
|
|
}
|
2011-03-06 22:45:44 +01:00
|
|
|
if (handle ne null) {
|
|
|
|
|
try {
|
2011-12-03 18:16:41 +01:00
|
|
|
if (Mailbox.debug) println(mbox.actor.self + " processing message " + handle)
|
2011-10-19 13:19:44 +02:00
|
|
|
mbox.actor.invoke(handle)
|
2011-06-13 22:36:46 +02:00
|
|
|
true
|
2011-03-06 22:45:44 +01:00
|
|
|
} catch {
|
2011-10-21 18:47:44 +02:00
|
|
|
case ie: InterruptedException ⇒
|
2011-11-18 11:59:43 +01:00
|
|
|
log.error(ie, "Interrupted during message processing")
|
2011-10-21 18:47:44 +02:00
|
|
|
Thread.currentThread().interrupt()
|
2011-10-25 15:07:20 +02:00
|
|
|
intex = ie
|
2011-10-21 18:47:44 +02:00
|
|
|
true
|
2012-02-01 17:38:12 +01:00
|
|
|
case NonFatal(e) ⇒
|
2011-11-18 11:59:43 +01:00
|
|
|
log.error(e, "Error during message processing")
|
2011-06-13 22:36:46 +02:00
|
|
|
queue.leave
|
|
|
|
|
false
|
2011-03-06 22:45:44 +01:00
|
|
|
}
|
|
|
|
|
} else if (queue.isActive) {
|
|
|
|
|
queue.leave
|
|
|
|
|
false
|
|
|
|
|
} else false
|
2011-10-21 18:47:44 +02:00
|
|
|
} catch {
|
2012-02-01 17:38:12 +01:00
|
|
|
case NonFatal(e) ⇒ queue.leave; throw e
|
2011-03-06 22:45:44 +01:00
|
|
|
} finally {
|
2012-01-10 13:33:57 +01:00
|
|
|
mbox.ctdLock.unlock
|
2011-03-06 22:45:44 +01:00
|
|
|
}
|
|
|
|
|
if (recurse) {
|
2011-10-25 15:07:20 +02:00
|
|
|
runQueue(mbox, queue, intex)
|
|
|
|
|
} else {
|
|
|
|
|
if (intex ne null) {
|
|
|
|
|
Thread.interrupted // clear flag
|
|
|
|
|
throw intex
|
|
|
|
|
}
|
2011-03-06 22:45:44 +01:00
|
|
|
}
|
|
|
|
|
}
|
2011-12-20 21:08:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class CallingThreadDispatcherConfigurator(config: Config, prerequisites: DispatcherPrerequisites)
|
|
|
|
|
extends MessageDispatcherConfigurator(config, prerequisites) {
|
2012-02-19 10:28:56 +01:00
|
|
|
|
|
|
|
|
private val instance = new CallingThreadDispatcher(prerequisites, mailboxType())
|
2011-12-20 21:08:27 +01:00
|
|
|
|
|
|
|
|
override def dispatcher(): MessageDispatcher = instance
|
2011-03-06 22:45:44 +01:00
|
|
|
}
|
|
|
|
|
|
2012-02-19 10:28:56 +01:00
|
|
|
class NestingQueue(val q: MessageQueue) {
|
2011-05-18 17:25:30 +02:00
|
|
|
@volatile
|
|
|
|
|
private var active = false
|
2011-04-08 14:43:15 +02:00
|
|
|
def enter { if (active) sys.error("already active") else active = true }
|
|
|
|
|
def leave { if (!active) sys.error("not active") else active = false }
|
2011-03-06 22:45:44 +01:00
|
|
|
def isActive = active
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-13 17:57:56 +02:00
|
|
|
class CallingThreadMailbox(_receiver: akka.actor.Cell, val mailboxType: MailboxType)
|
|
|
|
|
extends Mailbox(null) with DefaultSystemMessageQueue {
|
|
|
|
|
|
|
|
|
|
val system = _receiver.system
|
|
|
|
|
val self = _receiver.self
|
2011-03-06 22:45:44 +01:00
|
|
|
|
|
|
|
|
private val q = new ThreadLocal[NestingQueue]() {
|
2011-10-21 18:47:44 +02:00
|
|
|
override def initialValue = {
|
2012-06-13 17:57:56 +02:00
|
|
|
val queue = new NestingQueue(mailboxType.create(Some(self), Some(system)))
|
|
|
|
|
CallingThreadDispatcherQueues(system).registerQueue(CallingThreadMailbox.this, queue)
|
2011-10-21 18:47:44 +02:00
|
|
|
queue
|
|
|
|
|
}
|
2011-03-06 22:45:44 +01:00
|
|
|
}
|
|
|
|
|
|
2012-06-13 17:57:56 +02:00
|
|
|
override def enqueue(receiver: ActorRef, msg: Envelope): Unit = q.get.q.enqueue(receiver, msg)
|
2012-04-03 15:38:54 +02:00
|
|
|
override def dequeue(): Envelope = throw new UnsupportedOperationException("CallingThreadMailbox cannot dequeue normally")
|
|
|
|
|
override def hasMessages: Boolean = q.get.q.hasMessages
|
2012-04-03 16:24:50 +02:00
|
|
|
override def numberOfMessages: Int = 0
|
2012-04-03 15:38:54 +02:00
|
|
|
|
2011-03-06 22:45:44 +01:00
|
|
|
def queue = q.get
|
|
|
|
|
|
2012-01-10 13:33:57 +01:00
|
|
|
val ctdLock = new ReentrantLock
|
2011-09-21 15:01:47 +02:00
|
|
|
val suspendSwitch = new Switch
|
2011-03-06 22:45:44 +01:00
|
|
|
|
2012-01-10 13:33:57 +01:00
|
|
|
override def cleanUp(): Unit = {
|
|
|
|
|
/*
|
2012-02-01 14:40:12 +01:00
|
|
|
* This is called from dispatcher.unregister, i.e. under this.lock. If
|
|
|
|
|
* another thread obtained a reference to this mailbox and enqueues after
|
2012-01-10 13:33:57 +01:00
|
|
|
* the gather operation, tough luck: no guaranteed delivery to deadLetters.
|
|
|
|
|
*/
|
|
|
|
|
suspendSwitch.locked {
|
2012-02-19 10:28:56 +01:00
|
|
|
val q = queue
|
|
|
|
|
CallingThreadDispatcherQueues(actor.system).gatherFromAllOtherQueues(this, q)
|
2012-01-10 13:33:57 +01:00
|
|
|
super.cleanUp()
|
2012-06-13 17:57:56 +02:00
|
|
|
q.q.cleanUp(actor.self, actor.systemImpl.deadLetterQueue)
|
2012-01-10 13:33:57 +01:00
|
|
|
}
|
|
|
|
|
}
|
2011-03-06 22:45:44 +01:00
|
|
|
}
|