What is Akka?
Scalable real-time transaction processing
We believe that writing correct concurrent, fault-tolerant and scalable applications is too hard. Most of the time it's because we are using the wrong tools and the wrong level of abstraction. Akka is here to change that. Using the Actor Model we raise the abstraction level and provide a better platform to build scalable, resilient and responsive applications—see the Reactive Manifesto for more details. For fault-tolerance we adopt the "let it crash" model which the telecom industry has used with great success to build applications that self-heal and systems that never stop. Actors also provide the abstraction for transparent distribution and the basis for truly scalable and fault-tolerant applications.
Akka is Open Source and available under the Apache 2 License.
Download from http://akka.io/downloads.
Please note that all code samples compile, so if you want direct access to the sources, have a look over at the Akka Docs subproject on github: for Java and Scala.
什么是AKKA?
可扩展的实时事务处理
我们相信,编写正确的并发,容错和可扩展的应用程序是太难了。大多数时候是因为我们使用了错误的工具和错误的抽象层次。Akka将改变这些。使用的Actor模型,我们提高了抽象层次,并提供一个更好的平台,以建立可扩展的,弹性和响应的应用程序看到的反应性宣言更多的细节。对于容错能力,我们采用“让它崩溃”的模式,电信行业已经成功地建立应用程序,自愈和系统,永远不会停止。演员还提供了透明的分布和真正的可扩展性和容错应用的基础。
阿卡是Apache 2许可下的开放源代码和可。
从http://akka.io/downloads下载。
Actor Systems
Actors are objects which encapsulate state and behavior, they communicate exclusively by exchanging messages which are placed into the recipient’s mailbox. In a sense, actors are the most stringent form of object-oriented programming, but it serves better to view them as persons: while modeling a solution with actors, envision a group of people and assign sub-tasks to them, arrange their functions into an organizational structure and think about how to escalate failure (all with the benefit of not actually dealing with people, which means that we need not concern ourselves with their emotional state or moral issues). The result can then serve as a mental scaffolding for building the software implementation.
ActorSystem
Actor是封装状态和行为的对象,它们通过将被放置在收件人邮箱中的消息进行通信。在一种意义上,Actor是最严格的形式,面向对象的编程,但它可以更好地把它们看作人:在建模与Actor的解决方案,设想一组人,并分配给他们的任务,安排他们的功能到一个组织结构,并考虑如何升级失败(得益于并不是实际人之间的打交道,这意味着我们需要不关心自己的情绪状态或道德问题)。结果可以作为一个心理支架的软件实现。
Hierarchical Structure
Like in an economic organization, actors naturally form hierarchies. One actor, which is to oversee a certain function in the program might want to split up its task into smaller, more manageable pieces. For this purpose it starts child actors which it supervises. While the details of supervision are explained here, we shall concentrate on the underlying concepts in this section. The only prerequisite is to know that each actor has exactly one supervisor, which is the actor that created it.
The quintessential feature of actor systems is that tasks are split up and delegated until they become small enough to be handled in one piece. In doing so, not only is the task itself clearly structured, but the resulting actors can be reasoned about in terms of which messages they should process, how they should react normally and how failure should be handled. If one actor does not have the means for dealing with a certain situation, it sends a corresponding failure message to its supervisor, asking for help. The recursive structure then allows to handle failure at the right level.
Compare this to layered software design which easily devolves into defensive programming with the aim of not leaking any failure out: if the problem is communicated to the right person, a better solution can be found than if trying to keep everything “under the carpet”.
Now, the difficulty in designing such a system is how to decide who should supervise what. There is of course no single best solution, but there are a few guidelines which might be helpful:
- If one actor manages the work another actor is doing, e.g. by passing on sub-tasks, then the manager should supervise the child. The reason is that the manager knows which kind of failures are expected and how to handle them.
- If one actor carries very important data (i.e. its state shall not be lost if avoidable), this actor should source out any possibly dangerous sub-tasks to children it supervises and handle failures of these children as appropriate. Depending on the nature of the requests, it may be best to create a new child for each request, which simplifies state management for collecting the replies. This is known as the “Error Kernel Pattern” from Erlang.
- If one actor depends on another actor for carrying out its duty, it should watch that other actor’s liveness and act upon receiving a termination notice. This is different from supervision, as the watching party has no influence on the supervisor strategy, and it should be noted that a functional dependency alone is not a criterion for deciding where to place a certain child actor in the hierarchy.
There are of course always exceptions to these rules, but no matter whether you follow the rules or break them, you should always have a reason.
层次结构
就像在一个经济组织中,Actor自然形成层次结构。一个Actor,这是监督在程序中的某些功能可能要分裂成更小,更容易管理的任务。为了这个目的,它会建立子对象并监督子对象。唯一的前提是要知道每个Actor都有一个主管,这个主管创造了它。
ActorSystem最典型的特征是将任务分割和下放,直到它们足够小和轻量级的处理。在这样做时,不仅是任务本身的结构清晰,但由此产生的Actor可以推理的信息,他们应该如何处理,他们应该如何反应,以及如何处理失败。如果一个Actor没有处理某个特定情况的手段,它会向它的主管发出一个相应的故障信息,请求帮助。递归结构,然后允许在正确的水平处理故障。
从软件设计层面上,这样的设计更容易实现容错的程序:这会是一个比较好的方案,将问题传达给正确的人,而不是所有的人都趴在地上。
现在,在设计系统的难度是如何决定谁应该监督。当然没有一个最佳的解决方案,但有一些指导原则,这可能是有益的:
如果一个Actor管理另一个Actor的工作,例如,通过分任务,那么父类应该监督子类。原因是父类知道什么样的失败,以及如何处理它们。
如果一个Actor携带非常重要的数据(即它的状态不应该失去),这个Actor应该发起子任务,并适当的监督和处理这些孩子的失败。根据请求的性质,它可能是最好的为每个请求创建一个新的子类,从而简化了收集回复的状态管理。the “Error Kernel Pattern” from Erlang(不知道怎么翻译,又懂得帮我指正啊)
如果一个Actor取决于另一个Actor,应该观察这个Actor的活动和行为以及收到的终止通知。这不等同于监督,因为这对观看者并没有影响,必须指出的是,这并不是唯一的决定子类层次的标准。
当然,这些规则总是有例外的,但是不管你是否遵守规则,还是要打破规则,你都应该有一个理由。
Actor Best Practices
- Actors should be like nice co-workers: do their job efficiently without bothering everyone else needlessly and avoid hogging resources. Translated to programming this means to process events and generate responses (or more requests) in an event-driven manner. Actors should not block (i.e. passively wait while occupying a Thread) on some external entity—which might be a lock, a network socket, etc.—unless it is unavoidable; in the latter case see below.
- Do not pass mutable objects between actors. In order to ensure that, prefer immutable messages. If the encapsulation of actors is broken by exposing their mutable state to the outside, you are back in normal Java concurrency land with all the drawbacks.
- Actors are made to be containers for behavior and state, embracing this means to not routinely send behavior within messages (which may be tempting using Scala closures). One of the risks is to accidentally share mutable state between actors, and this violation of the actor model unfortunately breaks all the properties which make programming in actors such a nice experience.
- Top-level actors are the innermost part of your Error Kernel, so create them sparingly and prefer truly hierarchical systems. This has benefits with respect to fault-handling (both considering the granularity of configuration and the performance) and it also reduces the strain on the guardian actor, which is a single point of contention if over-used.
Actor最佳实践
Actor应该像好的同事:有效完成工作而没有麻烦任何人,避免占用资源。在事件驱动的方式中,翻译为编程这意味着处理事件并生成响应(或更多请求)。在某些外部实体中,Actor不应该阻止(即被动地等待),而在某些外部实体可能是一个锁、一个网络插座等,除非它是不可避免的;在后者的情况下见。
Actor之间不要传递易变的对象,更喜欢一成不变的消息。如果Actor的封装是并暴露自己的可变状态,这将失去Akka所有的有点,回到普通java程序的状况。
Actor是一个行为和状态的容器,包括不常见的行为通过消息发送行为(这很可能是使用了Scala闭包)。风险之一是,当偶然共享了Actor之间的可变状态,这违反了Actor模型良好的编程经验。(暂时不理解)
(TODO: Top-level.....)
What is an Actor?
The previous section about Actor Systems explained how actors form hierarchies and are the smallest unit when building an application. This section looks at one such actor in isolation, explaining the concepts you encounter while implementing it. For a more in depth reference with all the details please refer to Actors (Scala) and Untyped Actors (Java).
An actor is a container for State, Behavior, a Mailbox, Children and a Supervisor Strategy. All of this is encapsulated behind an Actor Reference. Finally, this happens When an Actor Terminates.
什么是Actor?
之前介绍了Actor如何形成层次结构,是构建应用程序最小的单位。本节从一个孤立的Actor角度,解释遇到的概念,同时实施。在深度参考了所有的细节,更请参考 Actors (Scala) and Untyped Actors (Java)。
一个Actor是包含状态,行为,一个邮箱,孩子和一个主管策略。所有这一切都是封装在一个的Actor中。最后,当一个演员结束时:通过停止本身或停止其主管的方式停止下来的,他将释放资源,所有剩余的mail将进入系统的 “dead letter mailbox”并将他们转发EventStream 作为 DeadLetters。
What Supervision Means
As described in Actor Systems supervision describes a dependency relationship between actors: the supervisor delegates tasks to subordinates and therefore must respond to their failures. When a subordinate detects a failure (i.e. throws an exception), it suspends itself and all its subordinates and sends a message to its supervisor, signaling failure. Depending on the nature of the work to be supervised and the nature of the failure, the supervisor has a choice of the following four options:
- Resume the subordinate, keeping its accumulated internal state
- Restart the subordinate, clearing out its accumulated internal state
- Stop the subordinate permanently
- Escalate the failure, thereby failing itself
监督手段
在Actor系统的监督描述了一个Actor之间的依赖关系:主管委托给下属的任务,因此必须回应他们的失败。当一个下属的检测到故障(即抛出一个异常),它将本身及其所有下属和发送一个消息给其主管信号故障。根据工作的性质,监督和性质的失败,主管有以下四个选项的选择:
1.恢复下属,保持内在状态
2.重新启动下属,清除其累积的内部状态
3.永久停止
4.升级失败,从而失败
It is important to always view an actor as part of a supervision hierarchy, which explains the existence of the fourth choice (as a supervisor also is subordinate to another supervisor higher up) and has implications on the first three: resuming an actor resumes all its subordinates, restarting an actor entails restarting all its subordinates (but see below for more details), similarly terminating an actor will also terminate all its subordinates. It should be noted that the default behavior of the preRestart hook of the Actor class is to terminate all its children before restarting, but this hook can be overridden; the recursive restart applies to all children left after this hook has been executed.
Each supervisor is configured with a function translating all possible failure causes (i.e. exceptions) into one of the four choices given above; notably, this function does not take the failed actor’s identity as an input. It is quite easy to come up with examples of structures where this might not seem flexible enough, e.g. wishing for different strategies to be applied to different subordinates. At this point it is vital to understand that supervision is about forming a recursive fault handling structure. If you try to do too much at one level, it will become hard to reason about, hence the recommended way in this case is to add a level of supervision.
Akka implements a specific form called “parental supervision”. Actors can only be created by other actors—where the top-level actor is provided by the library—and each created actor is supervised by its parent. This restriction makes the formation of actor supervision hierarchies implicit and encourages sound design decisions. It should be noted that this also guarantees that actors cannot be orphaned or attached to supervisors from the outside, which might otherwise catch them unawares. In addition, this yields a natural and clean shutdown procedure for (sub-trees of) actor applications.
重要的是要始终认为一个角色的监督层次,这解释了存在的第四个选择(作为一个主管也属于另一个主管),并有影响的第1和3:恢复一个Actor恢复所有下属,重新启动一个演员需要重新启动所有的下属(但见下面的更多细节),同样终止一个Actor也将终止其所有下属。应该指出的是,Actor的prerestart的默认行为是终止所有的孩子重新启动前,但可以重写;递归重启应用后钩已执行的所有留守子类。
每个主管都配置了一个功能,将所有可能的故障原因(即例外)转化为上述四个选择之一,尤其是,这个功能并没有把失败的演员的身份作为输入。这是很容易提出的例子,这可能是不够灵活的结构,如希望不同的策略,以适用于不同的下属。在这一点上,了解这一点是至关重要的,监管是形成一个递归的故障处理结构。如果你尝试在一个层面上做太多的话,它会变得很难,因此,在这种情况下,建议的方式是增加一个级别的监督。
Akka实现特定形式称为“家长监督”。Actor只能由其他Actor创造,其中最顶级的Actor是由图书馆提供的,每个Actor都是由其父母监督的。这种限制使得Actor的监督层次结构的形成,并鼓励健全的设计决策。应该指出的是,这也保证了Actor不能孤立或连接到外部监事,否则可能捉住他们。此外,这将产生一个自然的和干净的关机程序(分树的)演员的应用。
相关推荐
读书笔记:基于Akka编写的RPC通讯程序Demo以Scala为开发语言
读书笔记:Scala支持的著名的并发编程框架Akka的学习记录
读书笔记:以Scala带动对异步编程webflux、akka、pulsar、gRpc等框架学习
Akka笔记 Akka消息 文档 源代码 从 Akka记录 文档 源代码 从 Akka测试 文档 源代码 从 Akka消息传递请求和响应 文档 源代码 从arunma / AkkaMessagingRequestResponse分叉
读书笔记:《实战Java高并发程序设计》第2版 第7章使用Akka构建高并发程序 源码
阿卡持久性rocksdb Akka 的实验性基于的持久性存储。 这是一个 alpha 版本; 目前它更像是 RocksDB 和 Akka-Persistence 的实验,而不是 Akka-...笔记 需要 Akka 2.4-Snapshot,因为它使用简化的 akka-persi
使用 Akka IO 在 Scala 中实现非阻塞 Redis 客户端重新反应基于 Akka I/O 的非...Timeout(5 seconds)// Redis client setupval client = RedisClient("localhost", 6379)笔记下面的示例取自测试用例。每个 API 调用都
akka-cluster-example-inloop 简单的 akka 集群示例。... Counter1 在 node1、node2、node3 上运行 Counter2 在 node7、node8、node9 上运行 Query 想同时查询 Counter1 分片和 Counter2 分片。 你必
在"scala学习笔记整理"中,我们可以深入探讨以下关键知识点: 1. **基础语法**:Scala的基础语法与Java有相似之处,但也有很多独特的特点。例如,它支持变量的不可变性(immutability),使用`val`声明常量,`var`...
3. **集群构建**(Xitrum学习笔记20 - 和Akka、Hazelcast组成集群.pdf):Xitrum可以与Akka和Hazelcast等工具集成,实现应用的集群部署,以提高服务的可用性和伸缩性。这部分会讲解如何配置和管理集群,以及如何处理...
接下来,我们来看`akka笔记.txt`中的内容,可能会涉及如何创建Actor以及如何进行远程调用: 1. 创建Actor:使用`Props`和`ActorSystem`创建Actor。例如: ```scala val system = ActorSystem("MySystem") val ...
3. **启动Akka Actor系统**:使用Akka框架创建Actor系统,这是Spark集群通信的基础。通过`startSystemAndActor`方法启动Master节点相关的Actor。 4. **等待Actor系统终止**:最后一步是通过`actorSystem....
第四部分:"Scala入门及进阶-part04-Akka Actor.pdf" 专注于Scala与Akka框架的集成,Akka是用于构建高度并发、分布式和容错系统的工具。Actor模型在Akka中扮演核心角色,这部分将解释Actor如何工作,以及如何创建、...
1. **通信框架**: Spark采用了**Akka** 和 **Netty** 这两种成熟的通信技术,这些技术已经被广泛应用于生产环境,具有稳定性和高效性。 2. **Shuffle实现**: Spark中的Shuffle功能主要借鉴了**MapReduce**的设计...
笔记: 我们强烈建议在本教程中使用 Java 8。 有一个实验分支latest-dependency-versions可以使用 Java 9 进行编译,但在成功运行时仍然存在问题。 另请参阅我们较新的教程 ,它扩展了此处的概念,更侧重于服务 ML ...
akka-http-routes-guard 我发现 Spray.io / Akka-http 新手经常犯常见的错误 - 他们忘记用波浪号 ( ~ ) 运算符连接路由。 在期间,我想编写一个 Scala 宏,当它遇到路由之间缺少的连接运算符时,它会中止编译(或...
目录如下 Scala简介&快速入门 基础语法 变量 数据类型 流程控制 操作符重载 模式匹配 函数式编程基础 函数式编程说明 函数定义/声明 函数运行机制 递归 函数注意事项和细节 ...Akka 介绍
3. **客户服务和支持**:提供客户支持平台,记录客户问题和解决方案,提升服务质量和客户满意度。 4. **数据分析**:收集并分析客户数据,提供洞察,帮助企业做出更好的决策。 5. **客户细分和个性化**:根据客户...
5. Akka框架:Akka是用Scala编写的开源框架,用于构建高度可扩展、容错的应用程序,它充分利用了Scala的Actor模型。 6. Scala与Java互操作:由于Scala是运行在JVM上的,所以可以直接使用Java库,与Java代码无缝集成...