2013-04-15 09:26:51 +02:00
|
|
|
/**
|
2014-02-02 19:05:45 -06:00
|
|
|
* Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
|
2013-04-15 09:26:51 +02:00
|
|
|
*/
|
|
|
|
|
package akka.remote
|
|
|
|
|
|
|
|
|
|
import scala.concurrent.duration._
|
|
|
|
|
import akka.actor.Actor
|
|
|
|
|
import akka.actor.ActorLogging
|
|
|
|
|
import akka.actor.ActorRef
|
|
|
|
|
import akka.actor.Address
|
|
|
|
|
import akka.actor.AddressTerminated
|
|
|
|
|
import akka.actor.Props
|
|
|
|
|
import akka.actor.RootActorPath
|
|
|
|
|
import akka.actor.Terminated
|
|
|
|
|
import akka.actor.ExtendedActorSystem
|
|
|
|
|
import akka.ConfigurationException
|
2013-04-26 12:18:01 +02:00
|
|
|
import akka.dispatch.{ UnboundedMessageQueueSemantics, RequiresMessageQueue }
|
2013-05-21 17:42:22 +02:00
|
|
|
import akka.actor.InternalActorRef
|
|
|
|
|
import akka.dispatch.sysmsg.DeathWatchNotification
|
2013-08-19 17:50:15 +02:00
|
|
|
import akka.dispatch.sysmsg.Watch
|
2013-05-30 14:03:35 +02:00
|
|
|
import akka.actor.Deploy
|
2013-04-15 09:26:51 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
|
|
|
|
private[akka] object RemoteWatcher {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Factory method for `RemoteWatcher` [[akka.actor.Props]].
|
|
|
|
|
*/
|
|
|
|
|
def props(
|
|
|
|
|
failureDetector: FailureDetectorRegistry[Address],
|
|
|
|
|
heartbeatInterval: FiniteDuration,
|
|
|
|
|
unreachableReaperInterval: FiniteDuration,
|
2013-05-04 16:22:40 +02:00
|
|
|
heartbeatExpectedResponseAfter: FiniteDuration): Props =
|
2013-04-15 09:26:51 +02:00
|
|
|
Props(classOf[RemoteWatcher], failureDetector, heartbeatInterval, unreachableReaperInterval,
|
2013-05-30 14:03:35 +02:00
|
|
|
heartbeatExpectedResponseAfter).withDeploy(Deploy.local)
|
2013-04-15 09:26:51 +02:00
|
|
|
|
|
|
|
|
case class WatchRemote(watchee: ActorRef, watcher: ActorRef)
|
|
|
|
|
case class UnwatchRemote(watchee: ActorRef, watcher: ActorRef)
|
2013-12-06 12:54:10 +01:00
|
|
|
case class RewatchRemote(watchee: ActorRef, watcher: ActorRef)
|
|
|
|
|
@SerialVersionUID(1L)
|
|
|
|
|
class Rewatch(watchee: InternalActorRef, watcher: InternalActorRef) extends Watch(watchee, watcher)
|
2013-04-15 09:26:51 +02:00
|
|
|
|
2013-05-04 16:22:40 +02:00
|
|
|
@SerialVersionUID(1L) case object Heartbeat
|
|
|
|
|
@SerialVersionUID(1L) case class HeartbeatRsp(addressUid: Int)
|
2013-04-15 09:26:51 +02:00
|
|
|
|
|
|
|
|
// sent to self only
|
|
|
|
|
case object HeartbeatTick
|
|
|
|
|
case object ReapUnreachableTick
|
|
|
|
|
case class ExpectedFirstHeartbeat(from: Address)
|
|
|
|
|
|
|
|
|
|
// test purpose
|
|
|
|
|
object Stats {
|
2013-05-04 16:22:40 +02:00
|
|
|
lazy val empty: Stats = counts(0, 0)
|
|
|
|
|
def counts(watching: Int, watchingNodes: Int): Stats =
|
|
|
|
|
new Stats(watching, watchingNodes)(Set.empty)
|
2013-04-15 09:26:51 +02:00
|
|
|
}
|
2013-05-04 16:22:40 +02:00
|
|
|
case class Stats(watching: Int, watchingNodes: Int)(val watchingRefs: Set[(ActorRef, ActorRef)]) {
|
2013-04-15 09:26:51 +02:00
|
|
|
override def toString: String = {
|
|
|
|
|
def formatWatchingRefs: String =
|
|
|
|
|
if (watchingRefs.isEmpty) ""
|
|
|
|
|
else ", watchingRefs=" + watchingRefs.map(x ⇒ x._2.path.name + " -> " + x._1.path.name).mkString("[", ", ", "]")
|
|
|
|
|
|
2013-05-04 16:22:40 +02:00
|
|
|
s"Stats(watching=${watching}, watchingNodes=${watchingNodes}${formatWatchingRefs})"
|
2013-04-15 09:26:51 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*
|
|
|
|
|
* Remote nodes with actors that are watched are monitored by this actor to be able
|
|
|
|
|
* to detect network failures and JVM crashes. [[akka.remote.RemoteActorRefProvider]]
|
|
|
|
|
* intercepts Watch and Unwatch system messages and sends corresponding
|
|
|
|
|
* [[RemoteWatcher.WatchRemote]] and [[RemoteWatcher.UnwatchRemote]] to this actor.
|
|
|
|
|
*
|
2013-05-04 16:22:40 +02:00
|
|
|
* For a new node to be watched this actor periodically sends [[RemoteWatcher.Heartbeat]]
|
|
|
|
|
* to the peer actor on the other node, which replies with [[RemoteWatcher.HeartbeatRsp]]
|
|
|
|
|
* message back. The failure detector on the watching side monitors these heartbeat messages.
|
2013-04-15 09:26:51 +02:00
|
|
|
* If arrival of hearbeat messages stops it will be detected and this actor will publish
|
|
|
|
|
* [[akka.actor.AddressTerminated]] to the `eventStream`.
|
|
|
|
|
*
|
2013-05-04 16:22:40 +02:00
|
|
|
* When all actors on a node have been unwatched it will stop sending heartbeat messages.
|
2013-04-15 09:26:51 +02:00
|
|
|
*
|
|
|
|
|
* For bi-directional watch between two nodes the same thing will be established in
|
|
|
|
|
* both directions, but independent of each other.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
private[akka] class RemoteWatcher(
|
|
|
|
|
failureDetector: FailureDetectorRegistry[Address],
|
|
|
|
|
heartbeatInterval: FiniteDuration,
|
|
|
|
|
unreachableReaperInterval: FiniteDuration,
|
2013-05-04 16:22:40 +02:00
|
|
|
heartbeatExpectedResponseAfter: FiniteDuration)
|
2013-04-26 12:18:01 +02:00
|
|
|
extends Actor with ActorLogging with RequiresMessageQueue[UnboundedMessageQueueSemantics] {
|
2013-04-15 09:26:51 +02:00
|
|
|
|
|
|
|
|
import RemoteWatcher._
|
|
|
|
|
import context.dispatcher
|
|
|
|
|
def scheduler = context.system.scheduler
|
|
|
|
|
|
|
|
|
|
val remoteProvider: RemoteActorRefProvider = context.system.asInstanceOf[ExtendedActorSystem].provider match {
|
|
|
|
|
case rarp: RemoteActorRefProvider ⇒ rarp
|
|
|
|
|
case other ⇒ throw new ConfigurationException(
|
|
|
|
|
s"ActorSystem [${context.system}] needs to have a 'RemoteActorRefProvider' enabled in the configuration, currently uses [${other.getClass.getName}]")
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-04 16:22:40 +02:00
|
|
|
val selfHeartbeatRspMsg = HeartbeatRsp(AddressUidExtension(context.system).addressUid)
|
2013-04-15 09:26:51 +02:00
|
|
|
|
|
|
|
|
// actors that this node is watching, tuple with (watcher, watchee)
|
|
|
|
|
var watching: Set[(ActorRef, ActorRef)] = Set.empty
|
|
|
|
|
// nodes that this node is watching, i.e. expecting hearteats from these nodes
|
|
|
|
|
var watchingNodes: Set[Address] = Set.empty
|
|
|
|
|
var unreachable: Set[Address] = Set.empty
|
|
|
|
|
var addressUids: Map[Address, Int] = Map.empty
|
|
|
|
|
|
|
|
|
|
val heartbeatTask = scheduler.schedule(heartbeatInterval, heartbeatInterval, self, HeartbeatTick)
|
|
|
|
|
val failureDetectorReaperTask = scheduler.schedule(unreachableReaperInterval, unreachableReaperInterval,
|
|
|
|
|
self, ReapUnreachableTick)
|
|
|
|
|
|
|
|
|
|
override def postStop(): Unit = {
|
|
|
|
|
super.postStop()
|
|
|
|
|
heartbeatTask.cancel()
|
|
|
|
|
failureDetectorReaperTask.cancel()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def receive = {
|
2013-05-04 16:22:40 +02:00
|
|
|
case HeartbeatTick ⇒ sendHeartbeat()
|
|
|
|
|
case Heartbeat ⇒ receiveHeartbeat()
|
|
|
|
|
case HeartbeatRsp(uid) ⇒ receiveHeartbeatRsp(uid)
|
2013-04-15 09:26:51 +02:00
|
|
|
case ReapUnreachableTick ⇒ reapUnreachable()
|
|
|
|
|
case ExpectedFirstHeartbeat(from) ⇒ triggerFirstHeartbeat(from)
|
|
|
|
|
case WatchRemote(watchee, watcher) ⇒ watchRemote(watchee, watcher)
|
|
|
|
|
case UnwatchRemote(watchee, watcher) ⇒ unwatchRemote(watchee, watcher)
|
2013-05-21 17:42:22 +02:00
|
|
|
case t @ Terminated(watchee) ⇒ terminated(watchee, t.existenceConfirmed, t.addressTerminated)
|
2013-12-06 12:54:10 +01:00
|
|
|
case RewatchRemote(watchee, watcher) ⇒ rewatchRemote(watchee, watcher)
|
2013-04-15 09:26:51 +02:00
|
|
|
|
|
|
|
|
// test purpose
|
|
|
|
|
case Stats ⇒
|
2014-01-16 15:16:35 +01:00
|
|
|
sender() ! Stats(
|
2013-04-15 09:26:51 +02:00
|
|
|
watching = watching.size,
|
2013-05-04 16:22:40 +02:00
|
|
|
watchingNodes = watchingNodes.size)(watching)
|
2013-04-15 09:26:51 +02:00
|
|
|
}
|
|
|
|
|
|
2013-05-04 16:22:40 +02:00
|
|
|
def receiveHeartbeat(): Unit =
|
2014-01-16 15:16:35 +01:00
|
|
|
sender() ! selfHeartbeatRspMsg
|
2013-05-04 16:22:40 +02:00
|
|
|
|
|
|
|
|
def receiveHeartbeatRsp(uid: Int): Unit = {
|
2014-01-16 15:16:35 +01:00
|
|
|
val from = sender().path.address
|
2013-04-15 09:26:51 +02:00
|
|
|
|
|
|
|
|
if (failureDetector.isMonitoring(from))
|
2013-05-04 16:22:40 +02:00
|
|
|
log.debug("Received heartbeat rsp from [{}]", from)
|
2013-04-15 09:26:51 +02:00
|
|
|
else
|
2013-05-04 16:22:40 +02:00
|
|
|
log.debug("Received first heartbeat rsp from [{}]", from)
|
2013-04-15 09:26:51 +02:00
|
|
|
|
|
|
|
|
if (watchingNodes(from) && !unreachable(from)) {
|
2013-08-19 17:50:15 +02:00
|
|
|
if (!addressUids.contains(from) || addressUids(from) != uid)
|
|
|
|
|
reWatch(from)
|
2013-04-15 09:26:51 +02:00
|
|
|
addressUids += (from -> uid)
|
|
|
|
|
failureDetector.heartbeat(from)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def reapUnreachable(): Unit =
|
|
|
|
|
watchingNodes foreach { a ⇒
|
|
|
|
|
if (!unreachable(a) && !failureDetector.isAvailable(a)) {
|
|
|
|
|
log.warning("Detected unreachable: [{}]", a)
|
2013-04-18 16:54:57 +02:00
|
|
|
addressUids.get(a) foreach { uid ⇒ quarantine(a, uid) }
|
2013-04-15 09:26:51 +02:00
|
|
|
publishAddressTerminated(a)
|
|
|
|
|
unreachable += a
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-18 16:54:57 +02:00
|
|
|
def publishAddressTerminated(address: Address): Unit =
|
2013-04-15 09:26:51 +02:00
|
|
|
context.system.eventStream.publish(AddressTerminated(address))
|
2013-04-18 16:54:57 +02:00
|
|
|
|
|
|
|
|
def quarantine(address: Address, uid: Int): Unit =
|
|
|
|
|
remoteProvider.quarantine(address, uid)
|
2013-04-15 09:26:51 +02:00
|
|
|
|
2013-12-06 12:54:10 +01:00
|
|
|
def rewatchRemote(watchee: ActorRef, watcher: ActorRef): Unit =
|
|
|
|
|
if (watching.contains((watchee, watcher)))
|
|
|
|
|
watchRemote(watchee, watcher)
|
|
|
|
|
else
|
|
|
|
|
//has been unwatched inbetween, skip re-watch
|
|
|
|
|
log.debug("Ignoring re-watch after being unwatched in the meantime: [{} -> {}]", watcher.path, watchee.path)
|
|
|
|
|
|
2013-04-15 09:26:51 +02:00
|
|
|
def watchRemote(watchee: ActorRef, watcher: ActorRef): Unit =
|
|
|
|
|
if (watchee.path.uid == akka.actor.ActorCell.undefinedUid)
|
|
|
|
|
logActorForDeprecationWarning(watchee)
|
|
|
|
|
else if (watcher != self) {
|
|
|
|
|
log.debug("Watching: [{} -> {}]", watcher.path, watchee.path)
|
2013-04-25 21:25:46 +02:00
|
|
|
addWatching(watchee, watcher)
|
2013-04-15 09:26:51 +02:00
|
|
|
|
|
|
|
|
// also watch from self, to be able to cleanup on termination of the watchee
|
|
|
|
|
context watch watchee
|
|
|
|
|
watching += ((watchee, self))
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-25 21:25:46 +02:00
|
|
|
def addWatching(watchee: ActorRef, watcher: ActorRef): Unit = {
|
|
|
|
|
watching += ((watchee, watcher))
|
|
|
|
|
val watcheeAddress = watchee.path.address
|
|
|
|
|
if (!watchingNodes(watcheeAddress) && unreachable(watcheeAddress)) {
|
|
|
|
|
// first watch to that node after a previous unreachable
|
|
|
|
|
unreachable -= watcheeAddress
|
|
|
|
|
failureDetector.remove(watcheeAddress)
|
|
|
|
|
}
|
|
|
|
|
watchingNodes += watcheeAddress
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-15 09:26:51 +02:00
|
|
|
def unwatchRemote(watchee: ActorRef, watcher: ActorRef): Unit =
|
|
|
|
|
if (watchee.path.uid == akka.actor.ActorCell.undefinedUid)
|
|
|
|
|
logActorForDeprecationWarning(watchee)
|
|
|
|
|
else if (watcher != self) {
|
|
|
|
|
log.debug("Unwatching: [{} -> {}]", watcher.path, watchee.path)
|
|
|
|
|
watching -= ((watchee, watcher))
|
|
|
|
|
|
|
|
|
|
// clean up self watch when no more watchers of this watchee
|
|
|
|
|
if (watching.forall { case (wee, wer) ⇒ wee != watchee || wer == self }) {
|
|
|
|
|
log.debug("Cleanup self watch of [{}]", watchee.path)
|
|
|
|
|
context unwatch watchee
|
|
|
|
|
watching -= ((watchee, self))
|
|
|
|
|
}
|
|
|
|
|
checkLastUnwatchOfNode(watchee.path.address)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def logActorForDeprecationWarning(watchee: ActorRef): Unit = {
|
|
|
|
|
log.debug("actorFor is deprecated, and watching a remote ActorRef acquired with actorFor is not reliable: [{}]", watchee.path)
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-21 17:42:22 +02:00
|
|
|
def terminated(watchee: ActorRef, existenceConfirmed: Boolean, addressTerminated: Boolean): Unit = {
|
2013-05-04 16:22:40 +02:00
|
|
|
log.debug("Watchee terminated: [{}]", watchee.path)
|
2013-05-21 17:42:22 +02:00
|
|
|
|
|
|
|
|
// When watchee is stopped it sends DeathWatchNotification to the watcher and to this RemoteWatcher,
|
|
|
|
|
// which is also watching. Send extra DeathWatchNotification to the watcher in case the
|
|
|
|
|
// DeathWatchNotification message is only delivered to RemoteWatcher. Otherwise there is a risk that
|
|
|
|
|
// the monitoring is removed, subsequent node failure is not detected and the original watcher is
|
|
|
|
|
// never notified. This may occur for normal system shutdown of the watchee system when not all remote
|
|
|
|
|
// messages are flushed at shutdown.
|
|
|
|
|
watching --= watching collect {
|
|
|
|
|
case tuple @ (wee, wer: InternalActorRef) if wee == watchee ⇒
|
|
|
|
|
if (!addressTerminated && wer != self)
|
|
|
|
|
wer.sendSystemMessage(DeathWatchNotification(watchee, existenceConfirmed, addressTerminated))
|
|
|
|
|
tuple
|
2013-04-15 09:26:51 +02:00
|
|
|
}
|
2013-05-21 17:42:22 +02:00
|
|
|
|
2013-04-25 21:25:46 +02:00
|
|
|
checkLastUnwatchOfNode(watchee.path.address)
|
2013-04-15 09:26:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def checkLastUnwatchOfNode(watcheeAddress: Address): Unit = {
|
|
|
|
|
if (watchingNodes(watcheeAddress) && watching.forall {
|
2013-05-04 16:22:40 +02:00
|
|
|
case (wee, wer) ⇒ wee.path.address != watcheeAddress
|
2013-04-15 09:26:51 +02:00
|
|
|
}) {
|
2013-05-04 16:22:40 +02:00
|
|
|
// unwatched last watchee on that node
|
2013-04-15 09:26:51 +02:00
|
|
|
log.debug("Unwatched last watchee of node: [{}]", watcheeAddress)
|
|
|
|
|
watchingNodes -= watcheeAddress
|
|
|
|
|
addressUids -= watcheeAddress
|
|
|
|
|
failureDetector.remove(watcheeAddress)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def sendHeartbeat(): Unit =
|
2013-05-04 16:22:40 +02:00
|
|
|
watchingNodes foreach { a ⇒
|
2013-04-15 09:26:51 +02:00
|
|
|
if (!unreachable(a)) {
|
2013-05-04 16:22:40 +02:00
|
|
|
if (failureDetector.isMonitoring(a)) {
|
|
|
|
|
log.debug("Sending Heartbeat to [{}]", a)
|
2013-04-15 09:26:51 +02:00
|
|
|
} else {
|
2013-05-04 16:22:40 +02:00
|
|
|
log.debug("Sending first Heartbeat to [{}]", a)
|
|
|
|
|
// schedule the expected first heartbeat for later, which will give the
|
|
|
|
|
// other side a chance to reply, and also trigger some resends if needed
|
|
|
|
|
scheduler.scheduleOnce(heartbeatExpectedResponseAfter, self, ExpectedFirstHeartbeat(a))
|
2013-04-15 09:26:51 +02:00
|
|
|
}
|
2013-05-04 16:22:40 +02:00
|
|
|
context.actorSelection(RootActorPath(a) / self.path.elements) ! Heartbeat
|
|
|
|
|
}
|
2013-04-15 09:26:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def triggerFirstHeartbeat(address: Address): Unit =
|
|
|
|
|
if (watchingNodes(address) && !failureDetector.isMonitoring(address)) {
|
|
|
|
|
log.debug("Trigger extra expected heartbeat from [{}]", address)
|
|
|
|
|
failureDetector.heartbeat(address)
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-19 17:50:15 +02:00
|
|
|
/**
|
|
|
|
|
* To ensure that we receive heartbeat messages from the right actor system
|
|
|
|
|
* incarnation we send Watch again for the first HeartbeatRsp (containing
|
|
|
|
|
* the system UID) and if HeartbeatRsp contains a new system UID.
|
|
|
|
|
* Terminated will be triggered if the watchee (including correct Actor UID)
|
|
|
|
|
* does not exist.
|
|
|
|
|
*/
|
|
|
|
|
def reWatch(address: Address): Unit =
|
|
|
|
|
watching.foreach {
|
2013-08-26 11:59:19 +02:00
|
|
|
case (wee: InternalActorRef, wer: InternalActorRef) ⇒
|
|
|
|
|
if (wee.path.address == address) {
|
2013-12-06 12:54:10 +01:00
|
|
|
// this re-watch will result in a RewatchRemote message to this actor
|
|
|
|
|
// must be a special message to be able to detect if an UnwatchRemote comes in
|
|
|
|
|
// before the extra RewatchRemote, then the re-watch should be ignored
|
2013-08-26 11:59:19 +02:00
|
|
|
log.debug("Re-watch [{} -> {}]", wer, wee)
|
2013-12-06 12:54:10 +01:00
|
|
|
wee.sendSystemMessage(new Rewatch(wee, wer)) // ➡➡➡ NEVER SEND THE SAME SYSTEM MESSAGE OBJECT TO TWO ACTORS ⬅⬅⬅
|
2013-08-26 11:59:19 +02:00
|
|
|
}
|
2013-08-19 17:50:15 +02:00
|
|
|
}
|
|
|
|
|
|
2013-04-15 09:26:51 +02:00
|
|
|
}
|