2013-01-14 14:09:53 +01:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package akka.contrib.pattern
|
|
|
|
|
|
|
|
|
|
import scala.concurrent.duration._
|
2013-04-28 22:05:40 +02:00
|
|
|
import scala.collection.immutable
|
2013-01-14 14:09:53 +01:00
|
|
|
import akka.actor.Actor
|
|
|
|
|
import akka.actor.Actor.Receive
|
|
|
|
|
import akka.actor.ActorLogging
|
|
|
|
|
import akka.actor.ActorRef
|
2013-03-26 18:17:50 +01:00
|
|
|
import akka.actor.ActorSelection
|
2013-01-14 14:09:53 +01:00
|
|
|
import akka.actor.Address
|
|
|
|
|
import akka.actor.FSM
|
|
|
|
|
import akka.actor.Props
|
|
|
|
|
import akka.actor.Terminated
|
|
|
|
|
import akka.cluster.Cluster
|
|
|
|
|
import akka.cluster.ClusterEvent._
|
2013-04-28 22:05:40 +02:00
|
|
|
import akka.cluster.Member
|
|
|
|
|
import akka.cluster.MemberStatus
|
2013-01-14 14:09:53 +01:00
|
|
|
import akka.AkkaException
|
|
|
|
|
|
|
|
|
|
object ClusterSingletonManager {
|
|
|
|
|
|
2013-04-17 22:14:19 +02:00
|
|
|
/**
|
|
|
|
|
* Scala API: Factory method for `ClusterSingletonManager` [[akka.actor.Props]].
|
2013-05-24 14:43:01 +02:00
|
|
|
* Note that the `singletonProps` function is applied when creating
|
|
|
|
|
* the singleton actor and it must not use members that are not thread safe, e.g.
|
|
|
|
|
* mutable state in enclosing actor.
|
2013-04-17 22:14:19 +02:00
|
|
|
*/
|
|
|
|
|
def props(
|
|
|
|
|
singletonProps: Option[Any] ⇒ Props,
|
|
|
|
|
singletonName: String,
|
|
|
|
|
terminationMessage: Any,
|
|
|
|
|
role: Option[String],
|
|
|
|
|
maxHandOverRetries: Int = 20,
|
|
|
|
|
maxTakeOverRetries: Int = 15,
|
2013-05-23 13:36:35 +02:00
|
|
|
retryInterval: FiniteDuration = 1.second): Props =
|
2013-04-17 22:14:19 +02:00
|
|
|
Props(classOf[ClusterSingletonManager], singletonProps, singletonName, terminationMessage, role,
|
2013-05-23 13:36:35 +02:00
|
|
|
maxHandOverRetries, maxTakeOverRetries, retryInterval)
|
2013-04-17 22:14:19 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Java API: Factory method for `ClusterSingletonManager` [[akka.actor.Props]].
|
2013-05-24 14:43:01 +02:00
|
|
|
* Note that the `singletonPropsFactory` is invoked when creating
|
|
|
|
|
* the singleton actor and it must not use members that are not thread safe, e.g.
|
|
|
|
|
* mutable state in enclosing actor.
|
2013-04-17 22:14:19 +02:00
|
|
|
*/
|
|
|
|
|
def props(
|
|
|
|
|
singletonName: String,
|
|
|
|
|
terminationMessage: Any,
|
|
|
|
|
role: String,
|
|
|
|
|
maxHandOverRetries: Int,
|
|
|
|
|
maxTakeOverRetries: Int,
|
|
|
|
|
retryInterval: FiniteDuration,
|
|
|
|
|
singletonPropsFactory: ClusterSingletonPropsFactory): Props =
|
|
|
|
|
props(handOverData ⇒ singletonPropsFactory.create(handOverData.orNull), singletonName, terminationMessage,
|
|
|
|
|
ClusterSingletonManager.Internal.roleOption(role), maxHandOverRetries, maxTakeOverRetries, retryInterval)
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Java API: Factory method for `ClusterSingletonManager` [[akka.actor.Props]]
|
|
|
|
|
* with default values.
|
2013-05-24 14:43:01 +02:00
|
|
|
* Note that the `singletonPropsFactory` is invoked when creating
|
|
|
|
|
* the singleton actor and it must not use members that are not thread safe, e.g.
|
|
|
|
|
* mutable state in enclosing actor.
|
2013-04-17 22:14:19 +02:00
|
|
|
*/
|
|
|
|
|
def defaultProps(
|
|
|
|
|
singletonName: String,
|
|
|
|
|
terminationMessage: Any,
|
|
|
|
|
role: String,
|
|
|
|
|
singletonPropsFactory: ClusterSingletonPropsFactory): Props =
|
|
|
|
|
props(handOverData ⇒ singletonPropsFactory.create(handOverData.orNull), singletonName, terminationMessage,
|
|
|
|
|
ClusterSingletonManager.Internal.roleOption(role))
|
|
|
|
|
|
2013-01-14 14:09:53 +01:00
|
|
|
/**
|
2013-02-08 13:13:52 +01:00
|
|
|
* INTERNAL API
|
2013-01-14 14:09:53 +01:00
|
|
|
* public due to the `with FSM` type parameters
|
|
|
|
|
*/
|
|
|
|
|
sealed trait State
|
|
|
|
|
/**
|
2013-02-08 13:13:52 +01:00
|
|
|
* INTERNAL API
|
2013-01-14 14:09:53 +01:00
|
|
|
* public due to the `with FSM` type parameters
|
|
|
|
|
*/
|
|
|
|
|
sealed trait Data
|
|
|
|
|
|
|
|
|
|
/**
|
2013-02-08 13:13:52 +01:00
|
|
|
* INTERNAL API
|
2013-01-14 14:09:53 +01:00
|
|
|
*/
|
|
|
|
|
private object Internal {
|
|
|
|
|
/**
|
2013-04-28 22:05:40 +02:00
|
|
|
* Sent from new oldest to previous oldest to initate the
|
2013-01-28 08:47:52 +01:00
|
|
|
* hand-over process. `HandOverInProgress` and `HandOverDone`
|
2013-01-14 14:09:53 +01:00
|
|
|
* are expected replies.
|
|
|
|
|
*/
|
|
|
|
|
case object HandOverToMe
|
|
|
|
|
/**
|
2013-04-28 22:05:40 +02:00
|
|
|
* Confirmation by the previous oldest that the hand
|
2013-01-28 08:47:52 +01:00
|
|
|
* over process, shut down of the singleton actor, has
|
2013-01-14 14:09:53 +01:00
|
|
|
* started.
|
|
|
|
|
*/
|
|
|
|
|
case object HandOverInProgress
|
|
|
|
|
/**
|
2013-04-28 22:05:40 +02:00
|
|
|
* Confirmation by the previous oldest that the singleton
|
2013-01-28 08:47:52 +01:00
|
|
|
* actor has been terminated and the hand-over process is
|
2013-01-14 14:09:53 +01:00
|
|
|
* completed. The `handOverData` holds the message, if any,
|
|
|
|
|
* sent from the singleton actor to its parent ClusterSingletonManager
|
|
|
|
|
* when shutting down. It is passed to the `singletonProps`
|
2013-04-28 22:05:40 +02:00
|
|
|
* factory on the new oldest node.
|
2013-01-14 14:09:53 +01:00
|
|
|
*/
|
|
|
|
|
case class HandOverDone(handOverData: Option[Any])
|
|
|
|
|
/**
|
2013-04-28 22:05:40 +02:00
|
|
|
* Sent from from previous oldest to new oldest to
|
2013-01-28 08:47:52 +01:00
|
|
|
* initiate the normal hand-over process.
|
2013-01-14 14:09:53 +01:00
|
|
|
* Especially useful when new node joins and becomes
|
2013-04-28 22:05:40 +02:00
|
|
|
* oldest immediately, without knowing who was previous
|
|
|
|
|
* oldest.
|
2013-01-14 14:09:53 +01:00
|
|
|
*/
|
|
|
|
|
case object TakeOverFromMe
|
|
|
|
|
|
|
|
|
|
case class HandOverRetry(count: Int)
|
2013-02-07 14:25:29 +01:00
|
|
|
case class TakeOverRetry(count: Int)
|
2013-01-14 14:09:53 +01:00
|
|
|
case object Cleanup
|
2013-04-28 22:05:40 +02:00
|
|
|
case object StartOldestChangedBuffer
|
2013-01-14 14:09:53 +01:00
|
|
|
|
|
|
|
|
case object Start extends State
|
2013-04-28 22:05:40 +02:00
|
|
|
case object Oldest extends State
|
|
|
|
|
case object Younger extends State
|
|
|
|
|
case object BecomingOldest extends State
|
|
|
|
|
case object WasOldest extends State
|
2013-01-14 14:09:53 +01:00
|
|
|
case object HandingOver extends State
|
|
|
|
|
case object TakeOver extends State
|
2013-05-09 09:49:59 +02:00
|
|
|
case object End extends State
|
2013-01-14 14:09:53 +01:00
|
|
|
|
|
|
|
|
case object Uninitialized extends Data
|
2013-04-28 22:05:40 +02:00
|
|
|
case class YoungerData(oldestOption: Option[Address]) extends Data
|
|
|
|
|
case class BecomingOldestData(previousOldestOption: Option[Address]) extends Data
|
|
|
|
|
case class OldestData(singleton: ActorRef, singletonTerminated: Boolean = false,
|
2013-01-14 14:09:53 +01:00
|
|
|
handOverData: Option[Any] = None) extends Data
|
2013-04-28 22:05:40 +02:00
|
|
|
case class WasOldestData(singleton: ActorRef, singletonTerminated: Boolean, handOverData: Option[Any],
|
|
|
|
|
newOldestOption: Option[Address]) extends Data
|
2013-01-14 14:09:53 +01:00
|
|
|
case class HandingOverData(singleton: ActorRef, handOverTo: Option[ActorRef], handOverData: Option[Any]) extends Data
|
2013-05-09 09:49:59 +02:00
|
|
|
case object EndData extends Data
|
2013-01-14 14:09:53 +01:00
|
|
|
|
|
|
|
|
val HandOverRetryTimer = "hand-over-retry"
|
|
|
|
|
val TakeOverRetryTimer = "take-over-retry"
|
|
|
|
|
val CleanupTimer = "cleanup"
|
|
|
|
|
|
2013-03-14 20:32:43 +01:00
|
|
|
def roleOption(role: String): Option[String] = role match {
|
|
|
|
|
case null | "" ⇒ None
|
|
|
|
|
case _ ⇒ Some(role)
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-28 22:05:40 +02:00
|
|
|
object OldestChangedBuffer {
|
2013-01-14 14:09:53 +01:00
|
|
|
/**
|
|
|
|
|
* Request to deliver one more event.
|
|
|
|
|
*/
|
|
|
|
|
case object GetNext
|
|
|
|
|
/**
|
|
|
|
|
* The first event, corresponding to CurrentClusterState.
|
|
|
|
|
*/
|
2013-04-28 22:05:40 +02:00
|
|
|
case class InitialOldestState(oldest: Option[Address], memberCount: Int)
|
|
|
|
|
|
|
|
|
|
case class OldestChanged(oldest: Option[Address])
|
2013-01-14 14:09:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2013-04-28 22:05:40 +02:00
|
|
|
* Notifications of member events that track oldest member is tunneled
|
2013-01-14 14:09:53 +01:00
|
|
|
* via this actor (child of ClusterSingletonManager) to be able to deliver
|
2013-04-28 22:05:40 +02:00
|
|
|
* one change at a time. Avoiding simultaneous changes simplifies
|
2013-01-14 14:09:53 +01:00
|
|
|
* the process in ClusterSingletonManager. ClusterSingletonManager requests
|
|
|
|
|
* next event with `GetNext` when it is ready for it. Only one outstanding
|
|
|
|
|
* `GetNext` request is allowed. Incoming events are buffered and delivered
|
|
|
|
|
* upon `GetNext` request.
|
|
|
|
|
*/
|
2013-04-28 22:05:40 +02:00
|
|
|
class OldestChangedBuffer(role: Option[String]) extends Actor {
|
|
|
|
|
import OldestChangedBuffer._
|
2013-01-14 14:09:53 +01:00
|
|
|
import context.dispatcher
|
|
|
|
|
|
|
|
|
|
val cluster = Cluster(context.system)
|
2013-04-28 22:05:40 +02:00
|
|
|
// sort by age, oldest first
|
|
|
|
|
val ageOrdering = Ordering.fromLessThan[Member] { (a, b) ⇒ a.isOlderThan(b) }
|
|
|
|
|
var membersByAge: immutable.SortedSet[Member] = immutable.SortedSet.empty(ageOrdering)
|
|
|
|
|
|
2013-01-14 14:09:53 +01:00
|
|
|
var changes = Vector.empty[AnyRef]
|
|
|
|
|
|
2013-04-28 22:05:40 +02:00
|
|
|
// subscribe to MemberEvent, re-subscribe when restart
|
|
|
|
|
override def preStart(): Unit = {
|
|
|
|
|
cluster.subscribe(self, classOf[MemberEvent])
|
2013-03-14 20:32:43 +01:00
|
|
|
}
|
2013-01-14 14:09:53 +01:00
|
|
|
override def postStop(): Unit = cluster.unsubscribe(self)
|
|
|
|
|
|
2013-04-28 22:05:40 +02:00
|
|
|
def matchingRole(member: Member): Boolean = role match {
|
|
|
|
|
case None ⇒ true
|
|
|
|
|
case Some(r) ⇒ member.hasRole(r)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def trackChange(block: () ⇒ Unit): Unit = {
|
|
|
|
|
val before = membersByAge.headOption
|
|
|
|
|
block()
|
|
|
|
|
val after = membersByAge.headOption
|
|
|
|
|
if (before != after)
|
|
|
|
|
changes :+= OldestChanged(after.map(_.address))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def handleInitial(state: CurrentClusterState): Unit = {
|
|
|
|
|
membersByAge = immutable.SortedSet.empty(ageOrdering) ++ state.members.collect {
|
|
|
|
|
case m if m.status == MemberStatus.Up && matchingRole(m) ⇒ m
|
|
|
|
|
}
|
|
|
|
|
val initial = InitialOldestState(membersByAge.headOption.map(_.address), membersByAge.size)
|
|
|
|
|
changes :+= initial
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def add(m: Member): Unit = {
|
|
|
|
|
if (matchingRole(m))
|
|
|
|
|
trackChange { () ⇒ membersByAge += m }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def remove(m: Member): Unit = {
|
|
|
|
|
if (matchingRole(m))
|
|
|
|
|
trackChange { () ⇒ membersByAge -= m }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def sendFirstChange(): Unit = {
|
|
|
|
|
val event = changes.head
|
|
|
|
|
changes = changes.tail
|
|
|
|
|
context.parent ! event
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-14 14:09:53 +01:00
|
|
|
def receive = {
|
2013-04-28 22:05:40 +02:00
|
|
|
case state: CurrentClusterState ⇒ handleInitial(state)
|
|
|
|
|
case MemberUp(m) ⇒ add(m)
|
|
|
|
|
case mEvent: MemberEvent if (mEvent.isInstanceOf[MemberExited] || mEvent.isInstanceOf[MemberRemoved]) ⇒
|
|
|
|
|
remove(mEvent.member)
|
2013-01-14 14:09:53 +01:00
|
|
|
case GetNext if changes.isEmpty ⇒
|
|
|
|
|
context.become(deliverNext, discardOld = false)
|
|
|
|
|
case GetNext ⇒
|
2013-04-28 22:05:40 +02:00
|
|
|
sendFirstChange()
|
2013-01-14 14:09:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// the buffer was empty when GetNext was received, deliver next event immediately
|
|
|
|
|
def deliverNext: Actor.Receive = {
|
|
|
|
|
case state: CurrentClusterState ⇒
|
2013-04-28 22:05:40 +02:00
|
|
|
handleInitial(state)
|
|
|
|
|
sendFirstChange()
|
2013-01-14 14:09:53 +01:00
|
|
|
context.unbecome()
|
2013-04-28 22:05:40 +02:00
|
|
|
case MemberUp(m) ⇒
|
|
|
|
|
add(m)
|
|
|
|
|
if (changes.nonEmpty) {
|
|
|
|
|
sendFirstChange()
|
|
|
|
|
context.unbecome()
|
|
|
|
|
}
|
|
|
|
|
case mEvent: MemberEvent if (mEvent.isInstanceOf[MemberExited] || mEvent.isInstanceOf[MemberRemoved]) ⇒
|
|
|
|
|
remove(mEvent.member)
|
|
|
|
|
if (changes.nonEmpty) {
|
|
|
|
|
sendFirstChange()
|
2013-03-14 20:32:43 +01:00
|
|
|
context.unbecome()
|
|
|
|
|
}
|
2013-01-14 14:09:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Java API. Factory for the [[akka.actor.Props]] of the singleton
|
|
|
|
|
* actor instance. Used in constructor of
|
|
|
|
|
* [[akka.contrib.pattern.ClusterSingletonManager]]
|
|
|
|
|
*/
|
|
|
|
|
@SerialVersionUID(1L)
|
|
|
|
|
trait ClusterSingletonPropsFactory extends Serializable {
|
|
|
|
|
/**
|
|
|
|
|
* Create the `Props` from the `handOverData` sent from
|
|
|
|
|
* previous singleton. `handOverData` might be null
|
2013-01-28 08:47:52 +01:00
|
|
|
* when no hand-over took place, or when the there is no need
|
2013-01-14 14:09:53 +01:00
|
|
|
* for sending data to the new singleton.
|
|
|
|
|
*/
|
|
|
|
|
def create(handOverData: Any): Props
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Thrown when a consistent state can't be determined within the
|
|
|
|
|
* defined retry limits. Eventually it will reach a stable state and
|
|
|
|
|
* can continue, and that is simplified by starting over with a clean
|
|
|
|
|
* state. Parent supervisor should typically restart the actor, i.e.
|
|
|
|
|
* default decision.
|
|
|
|
|
*/
|
|
|
|
|
class ClusterSingletonManagerIsStuck(message: String) extends AkkaException(message, null)
|
|
|
|
|
|
|
|
|
|
/**
|
2013-03-14 20:32:43 +01:00
|
|
|
* Manages singleton actor instance among all cluster nodes or a group
|
|
|
|
|
* of nodes tagged with a specific role. At most one singleton instance
|
|
|
|
|
* is running at any point in time.
|
|
|
|
|
*
|
|
|
|
|
* The ClusterSingletonManager is supposed to be started on all nodes,
|
|
|
|
|
* or all nodes with specified role, in the cluster with `actorOf`.
|
2013-04-28 22:05:40 +02:00
|
|
|
* The actual singleton is started on the oldest node by creating a child
|
2013-01-14 14:09:53 +01:00
|
|
|
* actor from the supplied `singletonProps`.
|
|
|
|
|
*
|
2013-04-28 22:05:40 +02:00
|
|
|
* The singleton actor is always running on the oldest member, which can
|
|
|
|
|
* be determined by [[akka.cluster.Member#isOlderThan]].
|
|
|
|
|
* This can change when removing members. A graceful hand over can normally
|
|
|
|
|
* be performed when current oldest node is leaving the cluster. Be aware that
|
|
|
|
|
* there is a short time period when there is no active singleton during the
|
2013-01-28 08:47:52 +01:00
|
|
|
* hand-over process.
|
2013-01-14 14:09:53 +01:00
|
|
|
*
|
|
|
|
|
* The singleton actor can at any time send a message to its parent
|
|
|
|
|
* ClusterSingletonManager and this message will be passed to the
|
2013-04-28 22:05:40 +02:00
|
|
|
* `singletonProps` factory on the new oldest node when a graceful
|
2013-01-28 08:47:52 +01:00
|
|
|
* hand-over is performed.
|
2013-01-14 14:09:53 +01:00
|
|
|
*
|
2013-04-28 22:05:40 +02:00
|
|
|
* The cluster failure detector will notice when oldest node
|
2013-01-28 08:47:52 +01:00
|
|
|
* becomes unreachable due to things like JVM crash, hard shut down,
|
2013-04-28 22:05:40 +02:00
|
|
|
* or network failure. Then a new oldest node will take over and a
|
2013-01-14 14:09:53 +01:00
|
|
|
* new singleton actor is created. For these failure scenarios there
|
2013-01-28 08:47:52 +01:00
|
|
|
* will not be a graceful hand-over, but more than one active singletons
|
2013-01-14 14:09:53 +01:00
|
|
|
* is prevented by all reasonable means. Some corner cases are eventually
|
|
|
|
|
* resolved by configurable timeouts.
|
|
|
|
|
*
|
2013-03-26 18:17:50 +01:00
|
|
|
* You access the singleton actor with `actorSelection` using the names you have
|
2013-01-14 14:09:53 +01:00
|
|
|
* specified when creating the ClusterSingletonManager. You can subscribe to
|
2013-04-28 22:05:40 +02:00
|
|
|
* [[akka.cluster.ClusterEvent.MemberEvent]] and sort the members by age
|
|
|
|
|
* ([[akka.cluster.ClusterEvent.Member#isOlderThan]]) to keep track of oldest member.
|
|
|
|
|
* Alternatively the singleton actor may broadcast its existence when it is started.
|
2013-01-14 14:09:53 +01:00
|
|
|
*
|
2013-04-17 22:14:19 +02:00
|
|
|
* Use factory method [[ClusterSingletonManager#props] to create the
|
|
|
|
|
* [[akka.actor.Props]] for the actor.
|
|
|
|
|
*
|
2013-01-14 14:09:53 +01:00
|
|
|
* ==Arguments==
|
|
|
|
|
*
|
|
|
|
|
* '''''singletonProps''''' Factory for [[akka.actor.Props]] of the
|
|
|
|
|
* singleton actor instance. The `Option` parameter is the the
|
|
|
|
|
* `handOverData` sent from previous singleton. `handOverData`
|
2013-01-28 08:47:52 +01:00
|
|
|
* might be None when no hand-over took place, or when the there
|
2013-01-14 14:09:53 +01:00
|
|
|
* is no need for sending data to the new singleton. The `handOverData`
|
|
|
|
|
* is typically passed as parameter to the constructor of the
|
2013-05-24 14:43:01 +02:00
|
|
|
* singleton actor. Note that the `singletonProps` function is applied when creating
|
|
|
|
|
* the singleton actor and it must not use members that are not thread safe, e.g.
|
|
|
|
|
* mutable state in enclosing actor.
|
2013-01-14 14:09:53 +01:00
|
|
|
*
|
|
|
|
|
* '''''singletonName''''' The actor name of the child singleton actor.
|
|
|
|
|
*
|
2013-04-28 22:05:40 +02:00
|
|
|
* '''''terminationMessage''''' When handing over to a new oldest node
|
2013-01-14 14:09:53 +01:00
|
|
|
* this `terminationMessage` is sent to the singleton actor to tell
|
|
|
|
|
* it to finish its work, close resources, and stop. It can sending
|
|
|
|
|
* a message back to the parent ClusterSingletonManager, which will
|
2013-04-28 22:05:40 +02:00
|
|
|
* passed to the `singletonProps` factory on the new oldest node.
|
|
|
|
|
* The hand-over to the new oldest node is completed when the
|
2013-01-14 14:09:53 +01:00
|
|
|
* singleton actor is terminated.
|
|
|
|
|
* Note that [[akka.actor.PoisonPill]] is a perfectly fine
|
|
|
|
|
* `terminationMessage` if you only need to stop the actor.
|
|
|
|
|
*
|
2013-03-14 20:32:43 +01:00
|
|
|
* '''''role''''' Singleton among the nodes tagged with specified role.
|
|
|
|
|
* If the role is not specified it's a singleton among all nodes in
|
|
|
|
|
* the cluster.
|
|
|
|
|
*
|
2013-04-28 22:05:40 +02:00
|
|
|
* '''''maxHandOverRetries''''' When a node is becoming oldest it sends
|
|
|
|
|
* hand-over request to previous oldest. This is retried with the
|
|
|
|
|
* `retryInterval` until the previous oldest confirms that the hand
|
2013-01-14 14:09:53 +01:00
|
|
|
* over has started, or this `maxHandOverRetries` limit has been
|
|
|
|
|
* reached. If the retry limit is reached it takes the decision to be
|
2013-04-28 22:05:40 +02:00
|
|
|
* the new oldest if previous oldest is unknown (typically removed),
|
2013-03-05 21:05:11 +01:00
|
|
|
* otherwise it initiates a new round by throwing
|
2013-01-14 14:09:53 +01:00
|
|
|
* [[akka.contrib.pattern.ClusterSingletonManagerIsStuck]] and expecting
|
|
|
|
|
* restart with fresh state. For a cluster with many members you might
|
|
|
|
|
* need to increase this retry limit because it takes longer time to
|
|
|
|
|
* propagate changes across all nodes.
|
|
|
|
|
*
|
2013-04-28 22:05:40 +02:00
|
|
|
* '''''maxTakeOverRetries''''' When a oldest node is not oldest any more
|
|
|
|
|
* it sends take over request to the new oldest to initiate the normal
|
2013-01-28 08:47:52 +01:00
|
|
|
* hand-over process. This is especially useful when new node joins and becomes
|
2013-04-28 22:05:40 +02:00
|
|
|
* oldest immediately, without knowing who was previous oldest. This is retried
|
2013-01-14 14:09:53 +01:00
|
|
|
* with the `retryInterval` until this retry limit has been reached. If the retry
|
|
|
|
|
* limit is reached it initiates a new round by throwing
|
|
|
|
|
* [[akka.contrib.pattern.ClusterSingletonManagerIsStuck]] and expecting
|
|
|
|
|
* restart with fresh state. This will also cause the singleton actor to be
|
|
|
|
|
* stopped. `maxTakeOverRetries` must be less than `maxHandOverRetries` to
|
2013-04-28 22:05:40 +02:00
|
|
|
* ensure that new oldest doesn't start singleton actor before previous is
|
2013-01-14 14:09:53 +01:00
|
|
|
* stopped for certain corner cases.
|
|
|
|
|
*/
|
|
|
|
|
class ClusterSingletonManager(
|
|
|
|
|
singletonProps: Option[Any] ⇒ Props,
|
|
|
|
|
singletonName: String,
|
|
|
|
|
terminationMessage: Any,
|
2013-03-14 20:32:43 +01:00
|
|
|
role: Option[String],
|
2013-04-17 22:14:19 +02:00
|
|
|
maxHandOverRetries: Int,
|
|
|
|
|
maxTakeOverRetries: Int,
|
2013-05-23 13:36:35 +02:00
|
|
|
retryInterval: FiniteDuration)
|
2013-01-14 14:09:53 +01:00
|
|
|
extends Actor with FSM[ClusterSingletonManager.State, ClusterSingletonManager.Data] {
|
|
|
|
|
|
2013-04-28 22:05:40 +02:00
|
|
|
// to ensure that new oldest doesn't start singleton actor before previous is stopped for certain corner cases
|
2013-01-14 14:09:53 +01:00
|
|
|
require(maxTakeOverRetries < maxHandOverRetries,
|
|
|
|
|
s"maxTakeOverRetries [${maxTakeOverRetries}]must be < maxHandOverRetries [${maxHandOverRetries}]")
|
|
|
|
|
|
|
|
|
|
import ClusterSingletonManager._
|
|
|
|
|
import ClusterSingletonManager.Internal._
|
2013-04-28 22:05:40 +02:00
|
|
|
import ClusterSingletonManager.Internal.OldestChangedBuffer._
|
2013-01-14 14:09:53 +01:00
|
|
|
|
|
|
|
|
val cluster = Cluster(context.system)
|
|
|
|
|
val selfAddressOption = Some(cluster.selfAddress)
|
2013-05-23 13:36:35 +02:00
|
|
|
import cluster.settings.LogInfo
|
2013-03-14 20:32:43 +01:00
|
|
|
|
2013-04-06 16:22:30 +02:00
|
|
|
require(role.forall(cluster.selfRoles.contains),
|
|
|
|
|
s"This cluster member [${cluster.selfAddress}] doesn't have the role [$role]")
|
2013-03-14 20:32:43 +01:00
|
|
|
|
2013-01-14 14:09:53 +01:00
|
|
|
// started when when self member is Up
|
2013-04-28 22:05:40 +02:00
|
|
|
var oldestChangedBuffer: ActorRef = _
|
2013-01-14 14:09:53 +01:00
|
|
|
// Previous GetNext request delivered event and new GetNext is to be sent
|
2013-04-28 22:05:40 +02:00
|
|
|
var oldestChangedReceived = true
|
2013-01-14 14:09:53 +01:00
|
|
|
|
2013-05-09 09:49:59 +02:00
|
|
|
var selfExited = false
|
|
|
|
|
|
2013-01-14 14:09:53 +01:00
|
|
|
// keep track of previously removed members
|
|
|
|
|
var removed = Map.empty[Address, Deadline]
|
|
|
|
|
|
|
|
|
|
def addRemoved(address: Address): Unit =
|
|
|
|
|
removed += address -> (Deadline.now + 15.minutes)
|
|
|
|
|
|
|
|
|
|
def cleanupOverdueNotMemberAnyMore(): Unit = {
|
|
|
|
|
removed = removed filter { case (address, deadline) ⇒ deadline.hasTimeLeft }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def logInfo(message: String): Unit =
|
2013-05-23 13:36:35 +02:00
|
|
|
if (LogInfo) log.info(message)
|
2013-01-14 14:09:53 +01:00
|
|
|
|
|
|
|
|
def logInfo(template: String, arg1: Any): Unit =
|
2013-05-23 13:36:35 +02:00
|
|
|
if (LogInfo) log.info(template, arg1)
|
2013-01-14 14:09:53 +01:00
|
|
|
|
|
|
|
|
def logInfo(template: String, arg1: Any, arg2: Any): Unit =
|
2013-05-23 13:36:35 +02:00
|
|
|
if (LogInfo) log.info(template, arg1, arg2)
|
2013-01-14 14:09:53 +01:00
|
|
|
|
|
|
|
|
override def preStart(): Unit = {
|
|
|
|
|
super.preStart()
|
|
|
|
|
require(!cluster.isTerminated, "Cluster node must not be terminated")
|
|
|
|
|
|
|
|
|
|
// subscribe to cluster changes, re-subscribe when restart
|
2013-05-09 09:49:59 +02:00
|
|
|
cluster.subscribe(self, classOf[MemberExited])
|
2013-01-14 14:09:53 +01:00
|
|
|
cluster.subscribe(self, classOf[MemberRemoved])
|
|
|
|
|
|
|
|
|
|
setTimer(CleanupTimer, Cleanup, 1.minute, repeat = true)
|
|
|
|
|
|
2013-04-28 22:05:40 +02:00
|
|
|
// defer subscription to avoid some jitter when
|
2013-01-14 14:09:53 +01:00
|
|
|
// starting/joining several nodes at the same time
|
2013-04-28 22:05:40 +02:00
|
|
|
cluster.registerOnMemberUp(self ! StartOldestChangedBuffer)
|
2013-01-14 14:09:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override def postStop(): Unit = {
|
|
|
|
|
cancelTimer(CleanupTimer)
|
|
|
|
|
cluster.unsubscribe(self)
|
|
|
|
|
super.postStop()
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-26 18:17:50 +01:00
|
|
|
def peer(at: Address): ActorSelection = context.actorSelection(self.path.toStringWithAddress(at))
|
2013-01-14 14:09:53 +01:00
|
|
|
|
2013-04-28 22:05:40 +02:00
|
|
|
def getNextOldestChanged(): Unit =
|
|
|
|
|
if (oldestChangedReceived) {
|
|
|
|
|
oldestChangedReceived = false
|
|
|
|
|
oldestChangedBuffer ! GetNext
|
2013-01-14 14:09:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
startWith(Start, Uninitialized)
|
|
|
|
|
|
|
|
|
|
when(Start) {
|
2013-04-28 22:05:40 +02:00
|
|
|
case Event(StartOldestChangedBuffer, _) ⇒
|
|
|
|
|
oldestChangedBuffer = context.actorOf(Props(classOf[OldestChangedBuffer], role).
|
2013-04-17 22:14:19 +02:00
|
|
|
withDispatcher(context.props.dispatcher))
|
2013-04-28 22:05:40 +02:00
|
|
|
getNextOldestChanged()
|
2013-01-14 14:09:53 +01:00
|
|
|
stay
|
|
|
|
|
|
2013-04-28 22:05:40 +02:00
|
|
|
case Event(InitialOldestState(oldestOption, memberCount), _) ⇒
|
|
|
|
|
oldestChangedReceived = true
|
|
|
|
|
if (oldestOption == selfAddressOption && memberCount == 1)
|
|
|
|
|
// alone, oldest immediately
|
|
|
|
|
gotoOldest(None)
|
|
|
|
|
else if (oldestOption == selfAddressOption)
|
|
|
|
|
goto(BecomingOldest) using BecomingOldestData(None)
|
2013-01-14 14:09:53 +01:00
|
|
|
else
|
2013-04-28 22:05:40 +02:00
|
|
|
goto(Younger) using YoungerData(oldestOption)
|
2013-01-14 14:09:53 +01:00
|
|
|
}
|
|
|
|
|
|
2013-04-28 22:05:40 +02:00
|
|
|
when(Younger) {
|
|
|
|
|
case Event(OldestChanged(oldestOption), YoungerData(previousOldestOption)) ⇒
|
|
|
|
|
oldestChangedReceived = true
|
|
|
|
|
if (oldestOption == selfAddressOption) {
|
|
|
|
|
logInfo("Younger observed OldestChanged: [{} -> myself]", previousOldestOption)
|
|
|
|
|
previousOldestOption match {
|
|
|
|
|
case None ⇒ gotoOldest(None)
|
|
|
|
|
case Some(prev) if removed.contains(prev) ⇒ gotoOldest(None)
|
2013-01-14 14:09:53 +01:00
|
|
|
case Some(prev) ⇒
|
|
|
|
|
peer(prev) ! HandOverToMe
|
2013-04-28 22:05:40 +02:00
|
|
|
goto(BecomingOldest) using BecomingOldestData(previousOldestOption)
|
2013-01-14 14:09:53 +01:00
|
|
|
}
|
|
|
|
|
} else {
|
2013-04-28 22:05:40 +02:00
|
|
|
logInfo("Younger observed OldestChanged: [{} -> {}]", previousOldestOption, oldestOption)
|
|
|
|
|
getNextOldestChanged()
|
|
|
|
|
stay using YoungerData(oldestOption)
|
2013-01-14 14:09:53 +01:00
|
|
|
}
|
|
|
|
|
|
2013-05-23 11:09:32 +02:00
|
|
|
case Event(MemberRemoved(m, _), YoungerData(Some(previousOldest))) if m.address == previousOldest ⇒
|
2013-04-28 22:05:40 +02:00
|
|
|
logInfo("Previous oldest removed [{}]", m.address)
|
2013-03-05 21:05:11 +01:00
|
|
|
addRemoved(m.address)
|
2013-04-28 22:05:40 +02:00
|
|
|
// transition when OldestChanged
|
|
|
|
|
stay using YoungerData(None)
|
2013-01-14 14:09:53 +01:00
|
|
|
|
2013-05-23 11:09:32 +02:00
|
|
|
case Event(MemberRemoved(m, _), _) if m.address == cluster.selfAddress ⇒
|
2013-01-14 14:09:53 +01:00
|
|
|
logInfo("Self removed, stopping ClusterSingletonManager")
|
|
|
|
|
stop()
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-28 22:05:40 +02:00
|
|
|
when(BecomingOldest) {
|
2013-01-14 14:09:53 +01:00
|
|
|
|
|
|
|
|
case Event(HandOverInProgress, _) ⇒
|
2013-01-28 08:47:52 +01:00
|
|
|
// confirmation that the hand-over process has started
|
|
|
|
|
logInfo("Hand-over in progress at [{}]", sender.path.address)
|
2013-01-14 14:09:53 +01:00
|
|
|
cancelTimer(HandOverRetryTimer)
|
|
|
|
|
stay
|
|
|
|
|
|
2013-04-28 22:05:40 +02:00
|
|
|
case Event(HandOverDone(handOverData), BecomingOldestData(Some(previousOldest))) ⇒
|
|
|
|
|
if (sender.path.address == previousOldest)
|
|
|
|
|
gotoOldest(handOverData)
|
2013-01-14 14:09:53 +01:00
|
|
|
else {
|
2013-04-28 22:05:40 +02:00
|
|
|
logInfo("Ignoring HandOverDone in BecomingOldest from [{}]. Expected previous oldest [{}]",
|
|
|
|
|
sender.path.address, previousOldest)
|
2013-01-14 14:09:53 +01:00
|
|
|
stay
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-23 11:09:32 +02:00
|
|
|
case Event(MemberRemoved(m, _), BecomingOldestData(Some(previousOldest))) if m.address == previousOldest ⇒
|
2013-04-28 22:05:40 +02:00
|
|
|
logInfo("Previous oldest [{}] removed", previousOldest)
|
2013-03-05 21:05:11 +01:00
|
|
|
addRemoved(m.address)
|
2013-03-08 09:39:48 +01:00
|
|
|
stay
|
2013-01-14 14:09:53 +01:00
|
|
|
|
2013-04-28 22:05:40 +02:00
|
|
|
case Event(TakeOverFromMe, BecomingOldestData(None)) ⇒
|
2013-01-14 14:09:53 +01:00
|
|
|
sender ! HandOverToMe
|
2013-04-28 22:05:40 +02:00
|
|
|
stay using BecomingOldestData(Some(sender.path.address))
|
2013-01-14 14:09:53 +01:00
|
|
|
|
2013-04-28 22:05:40 +02:00
|
|
|
case Event(TakeOverFromMe, BecomingOldestData(Some(previousOldest))) ⇒
|
|
|
|
|
if (previousOldest == sender.path.address) sender ! HandOverToMe
|
|
|
|
|
else logInfo("Ignoring TakeOver request in BecomingOldest from [{}]. Expected previous oldest [{}]",
|
|
|
|
|
sender.path.address, previousOldest)
|
2013-01-14 14:09:53 +01:00
|
|
|
stay
|
|
|
|
|
|
2013-04-28 22:05:40 +02:00
|
|
|
case Event(HandOverRetry(count), BecomingOldestData(previousOldestOption)) ⇒
|
2013-01-14 14:09:53 +01:00
|
|
|
if (count <= maxHandOverRetries) {
|
2013-04-28 22:05:40 +02:00
|
|
|
logInfo("Retry [{}], sending HandOverToMe to [{}]", count, previousOldestOption)
|
|
|
|
|
previousOldestOption foreach { peer(_) ! HandOverToMe }
|
2013-01-14 14:09:53 +01:00
|
|
|
setTimer(HandOverRetryTimer, HandOverRetry(count + 1), retryInterval, repeat = false)
|
2013-03-27 17:47:56 +01:00
|
|
|
stay()
|
2013-04-28 22:05:40 +02:00
|
|
|
} else if (previousOldestOption forall removed.contains) {
|
|
|
|
|
// can't send HandOverToMe, previousOldest unknown for new node (or restart)
|
|
|
|
|
// previous oldest might be down or removed, so no TakeOverFromMe message is received
|
|
|
|
|
logInfo("Timeout in BecomingOldest. Previous oldest unknown, removed and no TakeOver request.")
|
|
|
|
|
gotoOldest(None)
|
2013-01-14 14:09:53 +01:00
|
|
|
} else
|
|
|
|
|
throw new ClusterSingletonManagerIsStuck(
|
2013-04-28 22:05:40 +02:00
|
|
|
s"Becoming singleton oldest was stuck because previous oldest [${previousOldestOption}] is unresponsive")
|
2013-01-14 14:09:53 +01:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-28 22:05:40 +02:00
|
|
|
def gotoOldest(handOverData: Option[Any]): State = {
|
2013-01-14 14:09:53 +01:00
|
|
|
logInfo("Singleton manager [{}] starting singleton actor", cluster.selfAddress)
|
|
|
|
|
val singleton = context watch context.actorOf(singletonProps(handOverData), singletonName)
|
2013-04-28 22:05:40 +02:00
|
|
|
goto(Oldest) using OldestData(singleton)
|
2013-01-14 14:09:53 +01:00
|
|
|
}
|
|
|
|
|
|
2013-04-28 22:05:40 +02:00
|
|
|
when(Oldest) {
|
|
|
|
|
case Event(OldestChanged(oldestOption), OldestData(singleton, singletonTerminated, handOverData)) ⇒
|
|
|
|
|
oldestChangedReceived = true
|
|
|
|
|
logInfo("Oldest observed OldestChanged: [{} -> {}]", cluster.selfAddress, oldestOption)
|
|
|
|
|
oldestOption match {
|
2013-01-14 14:09:53 +01:00
|
|
|
case Some(a) if a == cluster.selfAddress ⇒
|
2013-04-28 22:05:40 +02:00
|
|
|
// already oldest
|
2013-01-14 14:09:53 +01:00
|
|
|
stay
|
2013-05-29 09:11:43 +02:00
|
|
|
case Some(a) if !selfExited && removed.contains(a) ⇒
|
2013-01-14 14:09:53 +01:00
|
|
|
gotoHandingOver(singleton, singletonTerminated, handOverData, None)
|
|
|
|
|
case Some(a) ⇒
|
2013-04-28 22:05:40 +02:00
|
|
|
// send TakeOver request in case the new oldest doesn't know previous oldest
|
2013-02-07 14:25:29 +01:00
|
|
|
peer(a) ! TakeOverFromMe
|
|
|
|
|
setTimer(TakeOverRetryTimer, TakeOverRetry(1), retryInterval, repeat = false)
|
2013-04-28 22:05:40 +02:00
|
|
|
goto(WasOldest) using WasOldestData(singleton, singletonTerminated, handOverData, newOldestOption = Some(a))
|
2013-02-07 14:25:29 +01:00
|
|
|
case None ⇒
|
2013-04-28 22:05:40 +02:00
|
|
|
// new oldest will initiate the hand-over
|
2013-02-07 14:25:29 +01:00
|
|
|
setTimer(TakeOverRetryTimer, TakeOverRetry(1), retryInterval, repeat = false)
|
2013-04-28 22:05:40 +02:00
|
|
|
goto(WasOldest) using WasOldestData(singleton, singletonTerminated, handOverData, newOldestOption = None)
|
2013-01-14 14:09:53 +01:00
|
|
|
}
|
|
|
|
|
|
2013-04-28 22:05:40 +02:00
|
|
|
case Event(HandOverToMe, OldestData(singleton, singletonTerminated, handOverData)) ⇒
|
2013-01-14 14:09:53 +01:00
|
|
|
gotoHandingOver(singleton, singletonTerminated, handOverData, Some(sender))
|
|
|
|
|
|
2013-04-28 22:05:40 +02:00
|
|
|
case Event(singletonHandOverMessage, d @ OldestData(singleton, _, _)) if sender == singleton ⇒
|
2013-01-14 14:09:53 +01:00
|
|
|
stay using d.copy(handOverData = Some(singletonHandOverMessage))
|
|
|
|
|
|
2013-04-28 22:05:40 +02:00
|
|
|
case Event(Terminated(ref), d @ OldestData(singleton, _, _)) if ref == singleton ⇒
|
2013-01-14 14:09:53 +01:00
|
|
|
stay using d.copy(singletonTerminated = true)
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-28 22:05:40 +02:00
|
|
|
when(WasOldest) {
|
|
|
|
|
case Event(TakeOverRetry(count), WasOldestData(_, _, _, newOldestOption)) ⇒
|
2013-01-14 14:09:53 +01:00
|
|
|
if (count <= maxTakeOverRetries) {
|
2013-04-28 22:05:40 +02:00
|
|
|
logInfo("Retry [{}], sending TakeOverFromMe to [{}]", count, newOldestOption)
|
|
|
|
|
newOldestOption foreach { peer(_) ! TakeOverFromMe }
|
2013-02-07 14:25:29 +01:00
|
|
|
setTimer(TakeOverRetryTimer, TakeOverRetry(count + 1), retryInterval, repeat = false)
|
2013-01-14 14:09:53 +01:00
|
|
|
stay
|
|
|
|
|
} else
|
2013-04-28 22:05:40 +02:00
|
|
|
throw new ClusterSingletonManagerIsStuck(s"Expected hand-over to [${newOldestOption}] never occured")
|
2013-01-14 14:09:53 +01:00
|
|
|
|
2013-04-28 22:05:40 +02:00
|
|
|
case Event(HandOverToMe, WasOldestData(singleton, singletonTerminated, handOverData, _)) ⇒
|
2013-01-14 14:09:53 +01:00
|
|
|
gotoHandingOver(singleton, singletonTerminated, handOverData, Some(sender))
|
|
|
|
|
|
2013-05-23 11:09:32 +02:00
|
|
|
case Event(MemberRemoved(m, _), WasOldestData(singleton, singletonTerminated, handOverData, Some(newOldest))) if !selfExited && m.address == newOldest ⇒
|
2013-03-05 21:05:11 +01:00
|
|
|
addRemoved(m.address)
|
2013-01-14 14:09:53 +01:00
|
|
|
gotoHandingOver(singleton, singletonTerminated, handOverData, None)
|
|
|
|
|
|
2013-04-28 22:05:40 +02:00
|
|
|
case Event(singletonHandOverMessage, d @ WasOldestData(singleton, _, _, _)) if sender == singleton ⇒
|
2013-01-14 14:09:53 +01:00
|
|
|
stay using d.copy(handOverData = Some(singletonHandOverMessage))
|
|
|
|
|
|
2013-04-28 22:05:40 +02:00
|
|
|
case Event(Terminated(ref), d @ WasOldestData(singleton, _, _, _)) if ref == singleton ⇒
|
2013-01-14 14:09:53 +01:00
|
|
|
stay using d.copy(singletonTerminated = true)
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def gotoHandingOver(singleton: ActorRef, singletonTerminated: Boolean, handOverData: Option[Any], handOverTo: Option[ActorRef]): State = {
|
|
|
|
|
if (singletonTerminated) {
|
|
|
|
|
handOverDone(handOverTo, handOverData)
|
|
|
|
|
} else {
|
|
|
|
|
handOverTo foreach { _ ! HandOverInProgress }
|
|
|
|
|
singleton ! terminationMessage
|
|
|
|
|
goto(HandingOver) using HandingOverData(singleton, handOverTo, handOverData)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
when(HandingOver) {
|
|
|
|
|
case (Event(Terminated(ref), HandingOverData(singleton, handOverTo, handOverData))) if ref == singleton ⇒
|
|
|
|
|
handOverDone(handOverTo, handOverData)
|
|
|
|
|
|
|
|
|
|
case Event(HandOverToMe, d @ HandingOverData(singleton, handOverTo, _)) if handOverTo == Some(sender) ⇒
|
|
|
|
|
// retry
|
|
|
|
|
sender ! HandOverInProgress
|
|
|
|
|
stay
|
|
|
|
|
|
|
|
|
|
case Event(singletonHandOverMessage, d @ HandingOverData(singleton, _, _)) if sender == singleton ⇒
|
|
|
|
|
stay using d.copy(handOverData = Some(singletonHandOverMessage))
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def handOverDone(handOverTo: Option[ActorRef], handOverData: Option[Any]): State = {
|
2013-04-28 22:05:40 +02:00
|
|
|
val newOldest = handOverTo.map(_.path.address)
|
|
|
|
|
logInfo("Singleton terminated, hand-over done [{} -> {}]", cluster.selfAddress, newOldest)
|
2013-01-14 14:09:53 +01:00
|
|
|
handOverTo foreach { _ ! HandOverDone(handOverData) }
|
2013-05-09 09:49:59 +02:00
|
|
|
if (selfExited || removed.contains(cluster.selfAddress))
|
|
|
|
|
goto(End) using EndData
|
|
|
|
|
else
|
|
|
|
|
goto(Younger) using YoungerData(newOldest)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
when(End) {
|
2013-05-23 11:09:32 +02:00
|
|
|
case Event(MemberRemoved(m, _), _) if m.address == cluster.selfAddress ⇒
|
2013-05-09 09:49:59 +02:00
|
|
|
logInfo("Self removed, stopping ClusterSingletonManager")
|
|
|
|
|
stop()
|
2013-01-14 14:09:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
whenUnhandled {
|
|
|
|
|
case Event(_: CurrentClusterState, _) ⇒ stay
|
2013-05-09 09:49:59 +02:00
|
|
|
case Event(MemberExited(m), _) ⇒
|
|
|
|
|
if (m.address == cluster.selfAddress) {
|
|
|
|
|
selfExited = true
|
|
|
|
|
logInfo("Exited [{}]", m.address)
|
|
|
|
|
}
|
|
|
|
|
stay
|
2013-05-23 11:09:32 +02:00
|
|
|
case Event(MemberRemoved(m, _), _) ⇒
|
2013-05-09 09:49:59 +02:00
|
|
|
if (!selfExited) logInfo("Member removed [{}]", m.address)
|
2013-01-14 14:09:53 +01:00
|
|
|
addRemoved(m.address)
|
|
|
|
|
stay
|
|
|
|
|
case Event(TakeOverFromMe, _) ⇒
|
|
|
|
|
logInfo("Ignoring TakeOver request in [{}] from [{}].", stateName, sender.path.address)
|
|
|
|
|
stay
|
|
|
|
|
case Event(Cleanup, _) ⇒
|
|
|
|
|
cleanupOverdueNotMemberAnyMore()
|
|
|
|
|
stay
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onTransition {
|
|
|
|
|
case from -> to ⇒ logInfo("ClusterSingletonManager state change [{} -> {}]", from, to)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onTransition {
|
2013-04-28 22:05:40 +02:00
|
|
|
case _ -> BecomingOldest ⇒ setTimer(HandOverRetryTimer, HandOverRetry(1), retryInterval, repeat = false)
|
2013-01-14 14:09:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onTransition {
|
2013-04-28 22:05:40 +02:00
|
|
|
case BecomingOldest -> _ ⇒ cancelTimer(HandOverRetryTimer)
|
|
|
|
|
case WasOldest -> _ ⇒ cancelTimer(TakeOverRetryTimer)
|
2013-01-14 14:09:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onTransition {
|
2013-04-28 22:05:40 +02:00
|
|
|
case _ -> (Younger | Oldest) ⇒ getNextOldestChanged()
|
2013-01-14 14:09:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onTransition {
|
2013-05-09 09:49:59 +02:00
|
|
|
case _ -> (Younger | End) if removed.contains(cluster.selfAddress) ⇒
|
2013-01-14 14:09:53 +01:00
|
|
|
logInfo("Self removed, stopping ClusterSingletonManager")
|
2013-05-09 09:49:59 +02:00
|
|
|
// note that FSM.stop() can't be used in onTransition
|
|
|
|
|
context.stop(self)
|
2013-01-14 14:09:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|