akka还有一个不常使用、但我觉得比较方便的一个模块,那就是FSM(有限状态机)。我们知道了akka中Actor模型的具体实现之后,就会发现,Akka的actor可以非常方便的实现FSM。其实在akka中实现FSM还是非常简单的,或者说我们在akka中有意无意的都在使用FSM。

  在介绍分析akka中FSM源码之前,先简单介绍下FSM基本概念。一个有限状态机是一个设备,或者是一个设备模型,具有有限数量的状态,它可以在任何给定的时间根据输入进行操作,使得一个状态变换到另一个状态,或者是使一个输入或者一种行为的发生。一个有限状态机在任何瞬间只能处在一种状态。FSM有几个重要的概念:状态、行为、状态变换条件。状态就是当前FSM所处的状态,行为就是当前状态下FSM对输入的响应,状态变换就是指FSM在什么条件下需要更改当前的状态。

  那么如何将FSM映射到akka中呢?其实非常简单。FSM就是一个actor;状态就是actor中的某个变量的值;行为其实可以定义为某个状态下的receive函数;状态转移,定义成某个函数就行了,只不过什么时候转移需要开发者自定义。当然这只是我们在没有分析源码之前的想当然,那具体是怎么样的呢?

 /**
* This captures all of the managed state of the [[akka.actor.FSM]]: the state
* name, the state data, possibly custom timeout, stop reason and replies
* accumulated while processing the last message.
*/
case class State[S, D](stateName: S, stateData: D, timeout: Option[FiniteDuration] = None, stopReason: Option[Reason] = None, replies: List[Any] = Nil)

  上面是状态的定义,有两个类型参数:S、D。分别是状态名称和状态数据,其中状态名称比较容易理解,状态数据可以认为是与当前状态关联的值。timeout标志当前状态持续的最长时间,这一点比较特殊,普通的状态机,如果没有主动更改状态会一直持续在当前状态的。stopReason当前状态停止的原因。replies暂时不做分析,可以理解为一种通知机制,退出当前状态时对listener的通知。

  /**
* All messages sent to the [[akka.actor.FSM]] will be wrapped inside an
* `Event`, which allows pattern matching to extract both state and data.
*/
final case class Event[D](event: Any, stateData: D) extends NoSerializationVerificationNeeded

  FSM还定义了一个Event,用来对用户数据的封装,用来做模式匹配。

type StateFunction = scala.PartialFunction[Event, State]

  上面是FSM行为的定义,比较简单,就是一个PartialFunction,它输入一个Event,输出下一个状态。其实从这一点也可以看出,转义状态是需要开发手动指定的。

/*
* A fsm hakker is an awesome dude or dudette who either thinks about hacking or has to eat ;-)
*/
class FSMHakker(name: String, left: ActorRef, right: ActorRef) extends Actor with FSM[FSMHakkerState, TakenChopsticks] { //All hakkers start waiting
startWith(Waiting, TakenChopsticks(None, None)) when(Waiting) {
case Event(Think, _) =>
println("%s starts to think".format(name))
startThinking(5.seconds)
} //When a hakker is thinking it can become hungry
//and try to pick up its chopsticks and eat
when(Thinking) {
case Event(StateTimeout, _) =>
left ! Take
right ! Take
goto(Hungry)
} // When a hakker is hungry it tries to pick up its chopsticks and eat
// When it picks one up, it goes into wait for the other
// If the hakkers first attempt at grabbing a chopstick fails,
// it starts to wait for the response of the other grab
when(Hungry) {
case Event(Taken(`left`), _) =>
goto(WaitForOtherChopstick) using TakenChopsticks(Some(left), None)
case Event(Taken(`right`), _) =>
goto(WaitForOtherChopstick) using TakenChopsticks(None, Some(right))
case Event(Busy(_), _) =>
goto(FirstChopstickDenied)
} // When a hakker is waiting for the last chopstick it can either obtain it
// and start eating, or the other chopstick was busy, and the hakker goes
// back to think about how he should obtain his chopsticks :-)
when(WaitForOtherChopstick) {
case Event(Taken(`left`), TakenChopsticks(None, Some(right))) => startEating(left, right)
case Event(Taken(`right`), TakenChopsticks(Some(left), None)) => startEating(left, right)
case Event(Busy(chopstick), TakenChopsticks(leftOption, rightOption)) =>
leftOption.foreach(_ ! Put)
rightOption.foreach(_ ! Put)
startThinking(10.milliseconds)
} private def startEating(left: ActorRef, right: ActorRef): State = {
println("%s has picked up %s and %s and starts to eat".format(name, left.path.name, right.path.name))
goto(Eating) using TakenChopsticks(Some(left), Some(right)) forMax (5.seconds)
} // When the results of the other grab comes back,
// he needs to put it back if he got the other one.
// Then go back and think and try to grab the chopsticks again
when(FirstChopstickDenied) {
case Event(Taken(secondChopstick), _) =>
secondChopstick ! Put
startThinking(10.milliseconds)
case Event(Busy(chopstick), _) =>
startThinking(10.milliseconds)
} // When a hakker is eating, he can decide to start to think,
// then he puts down his chopsticks and starts to think
when(Eating) {
case Event(StateTimeout, _) =>
println("%s puts down his chopsticks and starts to think".format(name))
left ! Put
right ! Put
startThinking(5.seconds)
} // Initialize the hakker
initialize() private def startThinking(duration: FiniteDuration): State = {
goto(Thinking) using TakenChopsticks(None, None) forMax duration
}
}

  上面是官方的一个demo,有几个重要的点需要注意。startWith用来定义状态机起始状态;when用来定义某个状态下对Event的处理行为;goto用来转移状态;using用来定义转移状态时,当前状态关联的值;initialize用来初始化。下面我们逐步分析FSM这些概念的实现源码。

  /**
* Set initial state. Call this method from the constructor before the [[#initialize]] method.
* If different state is needed after a restart this method, followed by [[#initialize]], can
* be used in the actor life cycle hooks [[akka.actor.Actor#preStart]] and [[akka.actor.Actor#postRestart]].
*
* @param stateName initial state designator
* @param stateData initial state data
* @param timeout state timeout for the initial state, overriding the default timeout for that state
*/
final def startWith(stateName: S, stateData: D, timeout: Timeout = None): Unit =
currentState = FSM.State(stateName, stateData, timeout)
private var currentState: State = _

  startWith实现非常简单,就是给currentState赋值,官网注释也说了,必须在initialize之前调用。挺不喜欢这种设计的,居然需要跟调用顺序绑定。

/**
* Insert a new StateFunction at the end of the processing chain for the
* given state. If the stateTimeout parameter is set, entering this state
* without a differing explicit timeout setting will trigger a StateTimeout
* event; the same is true when using #stay.
*
* @param stateName designator for the state
* @param stateTimeout default state timeout for this state
* @param stateFunction partial function describing response to input
*/
final def when(stateName: S, stateTimeout: FiniteDuration = null)(stateFunction: StateFunction): Unit =
register(stateName, stateFunction, Option(stateTimeout))
private def register(name: S, function: StateFunction, timeout: Timeout): Unit = {
if (stateFunctions contains name) {
stateFunctions(name) = stateFunctions(name) orElse function
stateTimeouts(name) = timeout orElse stateTimeouts(name)
} else {
stateFunctions(name) = function
stateTimeouts(name) = timeout
}
}
/*
* State definitions
*/
private val stateFunctions = mutable.Map[S, StateFunction]()
private val stateTimeouts = mutable.Map[S, Timeout]()

  综合上面三段代码,其实when方法就是把状态名与对应的行为之间的对应关系保存到一个map中,但需要注意的是,map中保存的是状态行为的一个合并,如果对同一个状态多次调用when,则每次注册的函数会依次调用,如果已经命中某个Event则不会再往后传递执行。

/**
* Produce transition to other state.
* Return this from a state function in order to effect the transition.
*
* This method always triggers transition events, even for `A -> A` transitions.
* If you want to stay in the same state without triggering an state transition event use [[#stay]] instead.
*
* @param nextStateName state designator for the next state
* @return state transition descriptor
*/
final def goto(nextStateName: S): State = FSM.State(nextStateName, currentState.stateData)

  goto看起来非常简单,它就是构造了下一个状态,那么为啥状态就会转移了呢?这个后面再分析。官网注释里面说,调用这个方法时会触发状态转移,而且会触发转移事件,即使是同状态的转义;如果不想触发状态转移事件,则可以使用stay方法,那么stay方法是什么呢?

/**
* Produce "empty" transition descriptor.
* Return this from a state function when no state change is to be effected.
*
* No transition event will be triggered by [[#stay]].
* If you want to trigger an event like `S -> S` for `onTransition` to handle use `goto` instead.
*
* @return descriptor for staying in current state
*/
final def stay(): State = goto(currentState.stateName).withNotification(false)

  它居然还是调用了goto,不同的是调用了withNotification。

 private[akka] def withNotification(notifies: Boolean): State[S, D] = {
if (notifies)
State(stateName, stateData, timeout, stopReason, replies)
else
new SilentState(stateName, stateData, timeout, stopReason, replies)
}
}

  而当notifies是false时,withNotification返回了一个SilentState

private[akka] class SilentState[S, D](_stateName: S, _stateData: D, _timeout: Option[FiniteDuration], _stopReason: Option[Reason], _replies: List[Any])
extends State[S, D](_stateName, _stateData, _timeout, _stopReason, _replies) { /**
* INTERNAL API
*/
private[akka] override def notifies: Boolean = false override def copy(stateName: S = stateName, stateData: D = stateData, timeout: Option[FiniteDuration] = timeout, stopReason: Option[Reason] = stopReason, replies: List[Any] = replies): State[S, D] = {
new SilentState(stateName, stateData, timeout, stopReason, replies)
}
}

  SilentState与State的唯一区别就是notifies的值是false。其实简单来说,goto和stay都是创建了下一个状态的值。

  分析到这里,startWith/when/StateFunction/goto基本就可以定义一个状态机了。那么FSM是如何进行状态的变换,或者说是如何运行的呢?其实吧,如果你对actor比较了解,就一定知道,这是在receive里面实现的。

override def receive: Receive = {
case TimeoutMarker(gen) ⇒
if (generation == gen) {
processMsg(StateTimeout, "state timeout")
}
case t @ Timer(name, msg, repeat, gen, owner) ⇒
if ((owner eq this) && (timers contains name) && (timers(name).generation == gen)) {
if (timeoutFuture.isDefined) {
timeoutFuture.get.cancel()
timeoutFuture = None
}
generation += 1
if (!repeat) {
timers -= name
}
processMsg(msg, t)
}
case SubscribeTransitionCallBack(actorRef) ⇒
// TODO Use context.watch(actor) and receive Terminated(actor) to clean up list
listeners.add(actorRef)
// send current state back as reference point
actorRef ! CurrentState(self, currentState.stateName)
case Listen(actorRef) ⇒
// TODO Use context.watch(actor) and receive Terminated(actor) to clean up list
listeners.add(actorRef)
// send current state back as reference point
actorRef ! CurrentState(self, currentState.stateName)
case UnsubscribeTransitionCallBack(actorRef) ⇒
listeners.remove(actorRef)
case Deafen(actorRef) ⇒
listeners.remove(actorRef)
case value ⇒ {
if (timeoutFuture.isDefined) {
timeoutFuture.get.cancel()
timeoutFuture = None
}
generation += 1
processMsg(value, sender())
}
}

  我们只关注最后一段代码processMsg(value, sender())。

private def processMsg(value: Any, source: AnyRef): Unit = {
val event = Event(value, currentState.stateData)
processEvent(event, source)
} private[akka] def processEvent(event: Event, source: AnyRef): Unit = {
val stateFunc = stateFunctions(currentState.stateName)
val nextState = if (stateFunc isDefinedAt event) {
stateFunc(event)
} else {
// handleEventDefault ensures that this is always defined
handleEvent(event)
}
applyState(nextState)
} private[akka] def applyState(nextState: State): Unit = {
nextState.stopReason match {
case None ⇒ makeTransition(nextState)
case _ ⇒
nextState.replies.reverse foreach { r ⇒ sender() ! r }
terminate(nextState)
context.stop(self)
}
} private[akka] def makeTransition(nextState: State): Unit = {
if (!stateFunctions.contains(nextState.stateName)) {
terminate(stay withStopReason Failure("Next state %s does not exist".format(nextState.stateName)))
} else {
nextState.replies.reverse foreach { r ⇒ sender() ! r }
if (currentState.stateName != nextState.stateName || nextState.notifies) {
this.nextState = nextState
handleTransition(currentState.stateName, nextState.stateName)
gossip(Transition(self, currentState.stateName, nextState.stateName))
this.nextState = null
}
currentState = nextState def scheduleTimeout(d: FiniteDuration): Some[Cancellable] = {
import context.dispatcher
Some(context.system.scheduler.scheduleOnce(d, self, TimeoutMarker(generation)))
} currentState.timeout match {
case SomeMaxFiniteDuration ⇒ // effectively disable stateTimeout
case Some(d: FiniteDuration) if d.length >= 0 ⇒ timeoutFuture = scheduleTimeout(d)
case _ ⇒
val timeout = stateTimeouts(currentState.stateName)
if (timeout.isDefined) timeoutFuture = scheduleTimeout(timeout.get)
}
}
}

  processMsg方法对用户输入的消息,进行了简单的包装,然后调用processEvent;processEvent根据当前状态名找到对应的状态函数,处理当前事件,状态函数返回的下一个状态(是用goto或stay返回的)传给applyState,进行状态转移。

  下一个状态没有stopReason则进行正常的状态转移,否则就停止当前状态机。makeTransition是最终处理状态转移的方法,其实也比较简单,会首先判断是否需要通知,需要则通知相关的listener;然后更新当前状态为最新的值,当然了,还会处理超时时间。

  其实分析道这里,FSM就没必要再深入研究了,非常简单。就是定义了一些DSL,用trait Actor的receive函数进行行为函数的调用和状态转移,如果你的状态不多,转移条件简单,完全没必要用FSM,自己用beconme/unbecome实现就行了。而且官方的FSM是用一个函数调用列表(或者说是一个函数指针,C++里面经常这样实现)实现的,并没有用beconme/unbecome,这一点倒让我挺意外的。当然了,如果你的场景确实是FSM,那最好还是用官方的实现,毕竟它已经把FSM可能遇到的问题都帮你处理好了。

  但有一点需要说明那就是akka的FSM对超时的处理机制,完全需要自己处理,比如你通过forMax给某个状态设置了超时时间,那么在该状态对应的行为中你需要处理Event(FSM.StateTimeout)消息,否则akka是不知道在某个状态超时之后应该转移到哪个状态的。akka所做的就是在某个状态指定时间内没有收到消息时,发送一个超时事件给你,而指定的时间内收到消息,则会取消上一次的超时设置,设置新的超时时间。

Akka源码分析-FSM的更多相关文章

  1. Akka源码分析-Cluster-Singleton

    akka Cluster基本实现原理已经分析过,其实它就是在remote基础上添加了gossip协议,同步各个节点信息,使集群内各节点能够识别.在Cluster中可能会有一个特殊的节点,叫做单例节点. ...

  2. Akka源码分析-Persistence

    在学习akka过程中,我们了解了它的监督机制,会发现actor非常可靠,可以自动的恢复.但akka框架只会简单的创建新的actor,然后调用对应的生命周期函数,如果actor有状态需要回复,我们需要h ...

  3. Akka源码分析-Akka Typed

    对不起,akka typed 我是不准备进行源码分析的,首先这个库的API还没有release,所以会may change,也就意味着其概念和设计包括API都会修改,基本就没有再深入分析源码的意义了. ...

  4. Akka源码分析-Akka-Streams-概念入门

    今天我们来讲解akka-streams,这应该算akka框架下实现的一个很高级的工具.之前在学习akka streams的时候,我是觉得云里雾里的,感觉非常复杂,而且又难学,不过随着对akka源码的深 ...

  5. Akka源码分析-Cluster-Metrics

    一个应用软件维护的后期一定是要做监控,akka也不例外,它提供了集群模式下的度量扩展插件. 其实如果读者读过前面的系列文章的话,应该是能够自己写一个这样的监控工具的.简单来说就是创建一个actor,它 ...

  6. Akka源码分析-Cluster-Distributed Publish Subscribe in Cluster

    在ClusterClient源码分析中,我们知道,他是依托于“Distributed Publish Subscribe in Cluster”来实现消息的转发的,那本文就来分析一下Pub/Sub是如 ...

  7. Akka源码分析-local-DeathWatch

    生命周期监控,也就是死亡监控,是akka编程中常用的机制.比如我们有了某个actor的ActorRef之后,希望在该actor死亡之后收到响应的消息,此时我们就可以使用watch函数达到这一目的. c ...

  8. Akka源码分析-Cluster-ActorSystem

    前面几篇博客,我们依次介绍了local和remote的一些内容,其实再分析cluster就会简单很多,后面关于cluster的源码分析,能够省略的地方,就不再贴源码而是一句话带过了,如果有不理解的地方 ...

  9. Akka源码分析-CircuitBreaker(熔断器)

    熔断器,在很多技术栈中都会出现的一种技术.它是在分布式系统中提供一个稳定的阻止嵌套失败的机制. 该怎么理解呢?简单来说,在分布式环境中,如果某个计算节点出现问题,很容易出现失败的逆向传到或整个系统的雪 ...

随机推荐

  1. Python之两个值对换

  2. 使用 XMLHttpRequest实现Ajax

    [XMLHttpRequest的概述] 1.XMLHttpRequest最早是在IE5中以ActiveX组件的形式实现的.非W3C标准 2.创建XMLHttpRequest对象(由于非标准所以实现方法 ...

  3. Java Web学习总结(29)——Java Web中的Filter和Interceptor比较

    1. 背景 在设计web应用的时候,用户登录/注册是必不可少的功能,对用户登录信息进行验证的方法也是多种多样,大致可以认为如下模式:前端验证+后台验证.根据笔者的经验,一般会在前端进行一些例如是否输入 ...

  4. [luoguP1433] 吃奶酪(DP || Dfs)

    传送门 深搜加剪纸可A(O(玄学) 1274ms) ——代码 #include <cmath> #include <cstdio> #include <iostream& ...

  5. [poj2311]Cutting Game_博弈论

    Cutting Game poj-2311 题目大意:题目链接 注释:略. 想法: 我们发现一次操作就是将这个ICG对应游戏图上的一枚棋子变成两枚. 又因为SG定理的存在,记忆化搜索即可. 最后,附上 ...

  6. [CH#56]过河(贪心)

    题目:http://ch.ezoj.tk/contest/CH%20Round%20%2356%20-%20%E5%9B%BD%E5%BA%86%E8%8A%82%E6%AC%A2%E4%B9%90% ...

  7. Spring-data-jpa 笔记(一)

    Spring Data JPA简介: 可以理解为 JPA 规范的再次封装抽象,底层还是使用了 Hibernate 的 JPA 技术实现,引用 JPQL(Java Persistence Query L ...

  8. 4560 NOIP2015 D2T2 子串 code vs

    4560 NOIP2015 D2T2 子串  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 有两个仅包含小写 ...

  9. W3School Memcached教程(安装/基本操作/高级操作/命令)

    来自W3School的Memcached教程,基本上涵盖了从安装到状态监控的教程. 不过最全的应该是官方提供在GitHub上的Wiki教程,一切的标准都来自官方,参考:https://github.c ...

  10. Eclipse代码/目录虚线对齐设置

    前提: 我的Eclipse版本如下: 比这个版本新或者旧都可以实现如下效果. 实现步骤: 在代码上显示虚线设置有如下方法: 1.如果不使用插件,Eclipse是不支持虚线的,只能是横条的点状,效果如下 ...