2013-04-15 09:26:51 +02:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
|
|
|
|
package akka.cluster
|
|
|
|
|
|
|
|
|
|
import scala.concurrent.duration.FiniteDuration
|
|
|
|
|
import akka.actor.Actor
|
|
|
|
|
import akka.actor.Address
|
|
|
|
|
import akka.actor.Props
|
|
|
|
|
import akka.cluster.ClusterEvent.CurrentClusterState
|
|
|
|
|
import akka.cluster.ClusterEvent.MemberEvent
|
|
|
|
|
import akka.cluster.ClusterEvent.MemberUp
|
|
|
|
|
import akka.cluster.ClusterEvent.MemberRemoved
|
2013-04-19 08:52:27 +02:00
|
|
|
import akka.cluster.ClusterEvent.UnreachableMember
|
2013-04-15 09:26:51 +02:00
|
|
|
import akka.remote.FailureDetectorRegistry
|
|
|
|
|
import akka.remote.RemoteWatcher
|
|
|
|
|
|
2013-04-17 21:03:16 +02:00
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
2013-04-15 09:26:51 +02:00
|
|
|
private[cluster] object ClusterRemoteWatcher {
|
|
|
|
|
/**
|
|
|
|
|
* Factory method for `ClusterRemoteWatcher` [[akka.actor.Props]].
|
|
|
|
|
*/
|
|
|
|
|
def props(
|
|
|
|
|
failureDetector: FailureDetectorRegistry[Address],
|
|
|
|
|
heartbeatInterval: FiniteDuration,
|
|
|
|
|
unreachableReaperInterval: FiniteDuration,
|
|
|
|
|
heartbeatExpectedResponseAfter: FiniteDuration,
|
|
|
|
|
numberOfEndHeartbeatRequests: Int): Props =
|
|
|
|
|
Props(classOf[ClusterRemoteWatcher], failureDetector, heartbeatInterval, unreachableReaperInterval,
|
|
|
|
|
heartbeatExpectedResponseAfter, numberOfEndHeartbeatRequests)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*
|
|
|
|
|
* Specialization of [[akka.remote.RemoteWatcher]] that keeps
|
|
|
|
|
* track of cluster member nodes and is responsible for watchees on cluster nodes.
|
|
|
|
|
* [[akka.actor.AddressTerminate]] is published when node is removed from cluster.
|
|
|
|
|
*
|
|
|
|
|
* `RemoteWatcher` handles non-cluster nodes. `ClusterRemoteWatcher` will take
|
|
|
|
|
* over responsibility from `RemoteWatcher` if a watch is added before a node is member
|
|
|
|
|
* of the cluster and then later becomes cluster member.
|
|
|
|
|
*/
|
|
|
|
|
private[cluster] class ClusterRemoteWatcher(
|
|
|
|
|
failureDetector: FailureDetectorRegistry[Address],
|
|
|
|
|
heartbeatInterval: FiniteDuration,
|
|
|
|
|
unreachableReaperInterval: FiniteDuration,
|
|
|
|
|
heartbeatExpectedResponseAfter: FiniteDuration,
|
|
|
|
|
numberOfEndHeartbeatRequests: Int)
|
|
|
|
|
extends RemoteWatcher(
|
|
|
|
|
failureDetector,
|
|
|
|
|
heartbeatInterval,
|
|
|
|
|
unreachableReaperInterval,
|
|
|
|
|
heartbeatExpectedResponseAfter,
|
|
|
|
|
numberOfEndHeartbeatRequests) {
|
|
|
|
|
|
|
|
|
|
import RemoteWatcher._
|
|
|
|
|
|
2013-04-19 08:52:27 +02:00
|
|
|
val cluster = Cluster(context.system)
|
|
|
|
|
import cluster.selfAddress
|
|
|
|
|
|
2013-04-15 09:26:51 +02:00
|
|
|
var clusterNodes: Set[Address] = Set.empty
|
|
|
|
|
|
|
|
|
|
override def preStart(): Unit = {
|
|
|
|
|
super.preStart()
|
2013-04-19 08:52:27 +02:00
|
|
|
cluster.subscribe(self, classOf[MemberEvent])
|
|
|
|
|
cluster.subscribe(self, classOf[UnreachableMember])
|
2013-04-15 09:26:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override def postStop(): Unit = {
|
|
|
|
|
super.postStop()
|
2013-04-19 08:52:27 +02:00
|
|
|
cluster.unsubscribe(self)
|
2013-04-15 09:26:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override def receive = receiveClusterEvent orElse super.receive
|
|
|
|
|
|
|
|
|
|
def receiveClusterEvent: Actor.Receive = {
|
|
|
|
|
case WatchRemote(watchee, watcher) if clusterNodes(watchee.path.address) ⇒
|
|
|
|
|
() // cluster managed node, don't propagate to super
|
|
|
|
|
case state: CurrentClusterState ⇒
|
2013-04-19 08:52:27 +02:00
|
|
|
clusterNodes = state.members.collect { case m if m.address != selfAddress ⇒ m.address }
|
2013-04-15 09:26:51 +02:00
|
|
|
clusterNodes foreach takeOverResponsibility
|
2013-04-25 21:25:46 +02:00
|
|
|
val clusterUnreachable = state.unreachable.collect { case m if m.address != selfAddress ⇒ m.address }
|
|
|
|
|
unreachable --= clusterNodes
|
|
|
|
|
unreachable ++= clusterUnreachable
|
2013-04-15 09:26:51 +02:00
|
|
|
case MemberUp(m) ⇒
|
2013-04-19 08:52:27 +02:00
|
|
|
if (m.address != selfAddress) {
|
|
|
|
|
clusterNodes += m.address
|
|
|
|
|
takeOverResponsibility(m.address)
|
|
|
|
|
unreachable -= m.address
|
|
|
|
|
}
|
|
|
|
|
case UnreachableMember(m) ⇒
|
|
|
|
|
if (m.address != selfAddress)
|
|
|
|
|
unreachable += m.address
|
2013-04-15 09:26:51 +02:00
|
|
|
case MemberRemoved(m) ⇒
|
2013-04-19 08:52:27 +02:00
|
|
|
if (m.address != selfAddress) {
|
|
|
|
|
clusterNodes -= m.address
|
|
|
|
|
if (unreachable contains m.address) {
|
|
|
|
|
quarantine(m.address, m.uniqueAddress.uid)
|
|
|
|
|
unreachable -= m.address
|
|
|
|
|
}
|
|
|
|
|
publishAddressTerminated(m.address)
|
|
|
|
|
}
|
2013-04-25 21:25:46 +02:00
|
|
|
case _: MemberEvent ⇒ // not interesting
|
2013-04-15 09:26:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* When a cluster node is added this class takes over the
|
|
|
|
|
* responsibility for watchees on that node already handled
|
|
|
|
|
* by super RemoteWatcher.
|
|
|
|
|
*/
|
|
|
|
|
def takeOverResponsibility(address: Address): Unit = {
|
|
|
|
|
watching foreach {
|
|
|
|
|
case (watchee, watcher) ⇒ if (watchee.path.address == address)
|
|
|
|
|
unwatchRemote(watchee, watcher)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|