zoukankan      html  css  js  c++  java
  • 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
      }
     
    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
     
    可以订制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)
    }
     
    Supervision of Top-Level Actors
     
    所有Top-Level Actors都是User Guardian的子actor。所以它们的失败由guardian根据它配置的strategy来处理
     
     
  • 相关阅读:
    iOS开发objectc优势与补足
    UITableView详解
    ipad 、iphone开发-通过定时器显示进度条
    [Yii Framework][SHARE] The directory structure of the Yii project site
    [Yii Framework] Yii中事件和行为的区别和应用
    [Ubuntu] 创建桌面启动图标
    [Ubuntu] Access denied for user ‘debiansysmaint’@'localhost’ (using password: YES)
    [Yii Framework] Chive: which is developed base on Yii, and its aims to be an alternative to phpMyAdmin!
    [javascript] 数组扩展操作
    [php] Generate PhpDoc with NetBeans
  • 原文地址:https://www.cnblogs.com/devos/p/3818548.html
Copyright © 2011-2022 走看看