首页 > 代码库 > Akka的fault tolerant
Akka的fault tolerant
要想容错,该怎么办?
父actor首先要获知子actor的失败状态,然后确定该怎么办, “怎么办”这回事叫做“supervisorStrategy"。
// Restart the storage child when StorageException is thrown.
// After 3 restarts within 5 seconds it will be stopped.
override val supervisorStrategy = OneForOneStrategy(maxNrOfRetries = 3,
withinTimeRange = 5 seconds) {
case _: Storage.StorageException => Restart
}
// After 3 restarts within 5 seconds it will be stopped.
override val supervisorStrategy = OneForOneStrategy(maxNrOfRetries = 3,
withinTimeRange = 5 seconds) {
case _: Storage.StorageException => Restart
}
SupervisorStrategy是干啥的呢? An Akka Supervisor Strategy is the policy to apply for crashing children.
有哪些SupervisorStrategy可以用呢?
1. AllForOneStrategy : 当一个child失败以后,把所有的fault handling "Directive"应用于所有children。
2. OneForOneStrategy : 当一个child失败以后,把fault handling "Directive"应用于失败的那个child。
”Directive"是干啥的?
首先,这个东西需要决定当child failure之后,应该怎么办? “应该怎么办”是Akka提供了几个实现,表现在Directive的子类:
1. Resume 继续让失败的那个actor处理消息
2. Restart 抛弃旧的actor,然后创建一个新的,然后恢复消息处理
3. Stop 停止出问题的子actor
4. Escalate 把失败提升到父Actor的父Actor,通过重新跑出失败的cause,使用父Actor由于子actor同样的异常失败。
由此看,所谓的failure,就是异常,准确地说就是Throwable。
应该如何把Directive传给strategy?
OneForOneStrategy和 AllForOneStrategy是一个柯里化函数,第二个参数块是一个叫Decider的东西,它是一个PartialFunction,把异常和Directive对应起来。
type Decider = PartialFunction[Throwable, Directive]
在XXXStrategy的参数中,第一个参数块里的参数都可以省略,使得可以不限制重试次数,或者只限制总次数不限制频繁程度。
当没有指定supervisor strategy时,默认的行为是怎么样的呢?
• ActorInitializationException will stop the failing child actor
• ActorKilledException will stop the failing child actor
• Exception will restart the failing child actor
• Other types of Throwable will be escalated to parent actor
• ActorKilledException will stop the failing child actor
• Exception will restart the failing child actor
• Other types of Throwable will be escalated to parent actor
可以订制Strategy的行为:例子
import akka.actor.OneForOneStrategy
import akka.actor.SupervisorStrategy._
import scala.concurrent.duration._
override val supervisorStrategy =
OneForOneStrategy(maxNrOfRetries = 10, withinTimeRange = 1 minute) {
case _: ArithmeticException => Resume
case t =>
super.supervisorStrategy.decider.applyOrElse(t, (_: Any) => Escalate)
}
import akka.actor.SupervisorStrategy._
import scala.concurrent.duration._
override val supervisorStrategy =
OneForOneStrategy(maxNrOfRetries = 10, withinTimeRange = 1 minute) {
case _: ArithmeticException => Resume
case t =>
super.supervisorStrategy.decider.applyOrElse(t, (_: Any) => Escalate)
}
Supervision of Top-Level Actors
所有Top-Level Actors都是User Guardian的子actor。所以它们的失败由guardian根据它配置的strategy来处理
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。