2013-04-15 09:26:51 +02:00
|
|
|
/**
|
2015-03-07 22:58:48 -08:00
|
|
|
* Copyright (C) 2009-2015 Typesafe Inc. <http://www.typesafe.com>
|
2013-04-15 09:26:51 +02:00
|
|
|
*/
|
|
|
|
|
package akka.cluster
|
|
|
|
|
|
|
|
|
|
import scala.concurrent.duration.FiniteDuration
|
2015-02-19 15:49:02 +01:00
|
|
|
import akka.actor._
|
2013-04-15 09:26:51 +02:00
|
|
|
import akka.cluster.ClusterEvent.CurrentClusterState
|
|
|
|
|
import akka.cluster.ClusterEvent.MemberEvent
|
|
|
|
|
import akka.cluster.ClusterEvent.MemberUp
|
|
|
|
|
import akka.cluster.ClusterEvent.MemberRemoved
|
|
|
|
|
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,
|
2013-05-04 16:22:40 +02:00
|
|
|
heartbeatExpectedResponseAfter: FiniteDuration): Props =
|
2013-04-15 09:26:51 +02:00
|
|
|
Props(classOf[ClusterRemoteWatcher], failureDetector, heartbeatInterval, unreachableReaperInterval,
|
2013-05-30 14:03:35 +02:00
|
|
|
heartbeatExpectedResponseAfter).withDeploy(Deploy.local)
|
2013-04-15 09:26:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*
|
|
|
|
|
* Specialization of [[akka.remote.RemoteWatcher]] that keeps
|
|
|
|
|
* track of cluster member nodes and is responsible for watchees on cluster nodes.
|
2015-02-19 15:49:02 +01:00
|
|
|
* [[akka.actor.AddressTerminated]] is published when node is removed from cluster.
|
2013-04-15 09:26:51 +02:00
|
|
|
*
|
|
|
|
|
* `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,
|
2013-05-04 16:22:40 +02:00
|
|
|
heartbeatExpectedResponseAfter: FiniteDuration)
|
2013-04-15 09:26:51 +02:00
|
|
|
extends RemoteWatcher(
|
|
|
|
|
failureDetector,
|
|
|
|
|
heartbeatInterval,
|
|
|
|
|
unreachableReaperInterval,
|
2013-05-04 16:22:40 +02:00
|
|
|
heartbeatExpectedResponseAfter) {
|
2013-04-15 09:26:51 +02:00
|
|
|
|
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])
|
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 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
|
|
|
unreachable --= clusterNodes
|
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
|
|
|
|
|
}
|
2013-05-23 11:09:32 +02:00
|
|
|
case MemberRemoved(m, previousStatus) ⇒
|
2013-04-19 08:52:27 +02:00
|
|
|
if (m.address != selfAddress) {
|
|
|
|
|
clusterNodes -= m.address
|
2013-05-23 11:09:32 +02:00
|
|
|
if (previousStatus == MemberStatus.Down) {
|
2014-02-13 11:27:40 +01:00
|
|
|
quarantine(m.address, Some(m.uniqueAddress.uid))
|
2013-04-19 08:52:27 +02:00
|
|
|
}
|
|
|
|
|
publishAddressTerminated(m.address)
|
|
|
|
|
}
|
2013-04-25 21:25:46 +02:00
|
|
|
case _: MemberEvent ⇒ // not interesting
|
2013-04-15 09:26:51 +02:00
|
|
|
}
|
|
|
|
|
|
2015-02-19 15:49:02 +01:00
|
|
|
override def watchNode(watchee: InternalActorRef) =
|
|
|
|
|
if (!clusterNodes(watchee.path.address)) super.watchNode(watchee)
|
|
|
|
|
|
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.
|
|
|
|
|
*/
|
2015-02-19 15:49:02 +01:00
|
|
|
def takeOverResponsibility(address: Address): Unit =
|
|
|
|
|
if (watchingNodes(address)) {
|
|
|
|
|
log.debug("Cluster is taking over responsibility of node: [{}]", address)
|
|
|
|
|
unwatchNode(address)
|
2013-04-15 09:26:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|