要想容错,该怎么办?

父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来处理
 
 

Akka的fault tolerant的更多相关文章

  1. FTH: (7156): *** Fault tolerant heap shim applied to current process. This is usually due to previous crashes. ***

    这两天在Qtcreator上编译程序的时候莫名其妙的出现了FTH: (7156): *** Fault tolerant heap shim applied to current process. T ...

  2. 解决Qt4.8.6+VS2010运行程序提示 FTH: (6512): *** Fault tolerant heap shim applied to current process. This is usually due to previous crashes

    这个问题偶尔碰到两次,现在又遇上了,解决办法如下: 打开注册表,设置HKLM\Software\Microsoft\FTH\Enabled 为0 打开CMD,运行Rundll32.exe fthsvc ...

  3. 分布式应用框架Akka快速入门

    转自:http://blog.csdn.net/jmppok/article/details/17264495 本文结合网上一些资料,对他们进行整理,摘选和翻译而成,对Akka进行简要的说明.引用资料 ...

  4. Akka Essentials - 2

    Actors Defining an actor class MyActor extends Actor { def receive = { } } In Scala, the receive blo ...

  5. Akka边学边写(1)-- Hello, World!

    Akka Akka是什么呢?直接引用Akka站点上面的描写叙述吧: Akka is a toolkit and runtime for building highly concurrent, dist ...

  6. Java资源大全中文版(Awesome最新版)

    Awesome系列的Java资源整理.awesome-java 就是akullpp发起维护的Java资源列表,内容包括:构建工具.数据库.框架.模板.安全.代码分析.日志.第三方库.书籍.Java 站 ...

  7. 资源list:Github上关于大数据的开源项目、论文等合集

    Awesome Big Data A curated list of awesome big data frameworks, resources and other awesomeness. Ins ...

  8. Java开源框架推荐(全)

    Build Tool Tools which handle the buildcycle of an application. Apache Maven - Declarative build and ...

  9. awesome-java

    Awesome Java A curated list of awesome Java frameworks, libraries and software. Awesome Java Ancient ...

随机推荐

  1. ADO.NET笔记(一)XML导入导出和数据库

    数据导出成XML文件 #region 数据导出成XML文件 string sql = "select Id, CallerNumber, TelNum, StartDateTime, End ...

  2. Android的SharedPreferences实用技巧

    转自:http://blog.csdn.net/jingfeizhu/article/details/10017779 SharedPreferences也是一种轻型的数据存储方式,它的本质是基于XM ...

  3. 找不到System.Runtime.Serialization.Json的解决方案

    System.ServiceModel System.ServiceModel.Web System.Runtime.Serialization 三者均要添加引用

  4. 20141013--判断语句(if ,else)

    目前为止,之前所学的程序只能一步步往下执行. 语句:判断,循环,形成程序分支和循环. 选择控制: if , else, switch, case 循环控制:white, do, for, foreac ...

  5. Normalize [ 浏览器渲染格式化 ]

    /*! normalize.css v3.0.2 | MIT License | git.io/normalize */ /** * 1. Set default font family to san ...

  6. TIMAC 学习笔记(一)

    TIMAC是TI公司推出的基于IEEE 802.15.4的通讯协议栈,编译环境为IAR,使用IAR自带的CLIB库,CLIB库提供了轻量级的C库,它不支持嵌入式C++.适用于RF4CE协议和ZigBe ...

  7. initial,常用于消除css格式

    刚在群里有人问在不改变原有css的情况下怎么清除一个css属性.有人提出了 initial,再此记录下. initial 关键字用于设置 CSS 属性为它的默认值. initial 关键字可用于任何 ...

  8. JS焦点图,JS 多个页面放多个焦点图

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. DTcms会员中心添加新页面-会员投稿,获得所有文章并分页

    DAL.article.cs /// <summary> /// 自定义:获得查询分页数据 /// </summary> public DataSet GetList(int ...

  10. spring-cloud-bus

    安装rabbitmq 依赖erlang: http://erlang.org/download/otp_win64_18.2.exe