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

Akka的fault tolerant

时间: 2024-08-04 01:00:19

Akka的fault tolerant的相关文章

解决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.dll,FthSysprepSpecialize 参考http://msdn.microsoft.com/en-us/library/dd744764 解决Qt4.8.6+VS2010运行程序提示 FTH: (6512): *** Fault tolerant heap shim applied

Akka和VertX比较

Akka和VertX都是scala写大数据框架的极其赞的technology Akka是用来写高concurrent,distributed,和fault tolerant event-driven的一个工具箱/库 而vertx是一个具有类似功能的framework 他们背后的general idea是很相近的,不同点在于: In Akka you would create an Actor subclass that would receive messages which are seria

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

本文结合网上一些资料,对他们进行整理,摘选和翻译而成,对Akka进行简要的说明.引用资料在最后列出. 1.什么是Akka Akka 是一个用 Scala 编写的库,用于简化编写容错的.高可伸缩性的 Java 和 Scala 的 Actor 模型应用. 官方网站 (http://akka.io/)的介绍是: Akka is a toolkit and runtime for building highly concurrent, distributed, and fault tolerant ev

Apache Flink fault tolerance源码剖析(五)

上一篇文章我们谈论了保存点的相关内容,其中就谈到了保存点状态的存储.这篇文章我们来探讨用户程序状态的存储,也是在之前的文章中多次提及的state backend(中文暂译为状态终端). 基于数据流API而编写的程序经常以各种各样的形式保存着状态: 窗口收集/聚合元素(这里的元素可以看作是窗口的状态)直到它们被触发 转换函数可能会使用key/value状态接口来存储数据 转换函数可能实现Checkpointed接口来让它们的本地变量受益于fault tolerant机制 当检查点机制工作时,上面谈

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

Akka Akka是什么呢?直接引用Akka站点上面的描写叙述吧: Akka is a toolkit and runtime for building highly concurrent, distributed, and fault tolerant event-driven applications on the JVM. 反正我认为Akka是比較难上手的,并且文档对于新手来说,也不太友好.本文会用Akka写一个Hello World程序,从这个程序入手,介绍Akka的一些基本概念. he

Akka学习笔记(1)-- Hello, World!

Akka Akka是什么呢?直接引用Akka网站上面的描述吧: Akka is a toolkit and runtime for building highly concurrent, distributed, and fault tolerant event-driven applications on the JVM. 反正我觉得Akka是比较难上手的,而且文档对于新手来说,也不太友好.本文会用Akka写一个Hello World程序,从这个程序入手,介绍Akka的一些基本概念. hell

Java开源框架推荐(全)

Build Tool Tools which handle the buildcycle of an application. Apache Maven - Declarative build and dependency management which favors convention over configuration. It's preferable to Apache Ant which uses a rather procedural approach and can be di

awesome-java

Awesome Java A curated list of awesome Java frameworks, libraries and software. Awesome Java Ancients Bean Mapping Build Bytecode Manipulation Cluster Management Code Analysis Code Coverage Compiler-compiler Configuration Constraint Satisfaction Prob

Let it crash philosophy part II

Designing fault tolerant systems is extremely difficult.  You can try to anticipate and reason about all of the things that can go wrong with your software and code defensively for these situations, but in a complex system it is very likely that some