Akka Essentials - 1
参考Akka Essentials
1 Introduction to Akka
Actor Model
Actor模式的由来
In 1973, Carl Hewitt, Peter Bishop, and Richard Steiger wrote a paper—A Universal Modular ACTOR Formalism for Artificial Intelligence, which introduced the concept of Actors. Subsequently, the Actor Model was implemented in the Erlang language by Joe Armstrong and Ericsson implemented the AXD 301 telecom switch that went onto achieve reliability of 99.9999999 percent (nine 9's).
Actor模式改变传统oo的并发策略, 如下图
Actors change their state only when they receive a stimulus in the form of a message.
So unlike the object-oriented world where the objects are executed sequentially, the actors execute concurrently.
The Actor Model is based on the following principles:
• The immutable messages are used to communicate between actors.
Actors do not share state, and if any information is shared, it is done via message only.
Actors control the access to the state and nobody else can access the state. This means there is no shared, mutable state.
不可变消息, actor不会share自己的state, 只会通过不可变消息进行通信
• Each actor has a queue attached where the incoming messages are enqueued.
Messages are picked from the queue and processed by the actor, one at a time. An actor can respond to the received message by sending immutable messages to other actors, creating a new set of actors, updating their own state, or designating the computational logic to be used when the next message arrives (behavior change).
每个actor会有一个queue来接收message, actor会依次处理收到的message, 并发送消息给其他的actors
• Messages are passed between actors asynchronously.
It means that the sender does not wait for the message to be received and can go back to its execution immediately. Any actor can send a message to another actor with no guarantee on the sequence of the message arrival and execution.
消息是异步发送, actor不会保证消息到达或执行的顺序
• Communication between the sender and receiver is decoupled and asynchronous, allowing them to execute in different threads.
By having invocation and execution in separate threads coupled with no shared state, allows actors to provide a concurrent and scalable model.
这种低耦合和异步的方式, 很容易进行concurrent和扩展
Akka framework
The Akka framework has taken the Actor Model concept to build an event-driven, middleware framework that allows building concurrent, scalable, distributed systems. Akka uses the Actor Model to raise the abstraction level that decouples the business logic from low-level constructs of threads, locks, and non-blocking I/O.
Akka其实就是一种达到工业级别的Actor编程框架, 另外具有如下特性
The Akka framework provides the following features:
• Concurrency: Akka Actor Model abstracts the concurrency handling and allows the programmer to focus on the business logic.
• Scalability: Akka Actor Model's asynchronous message passing allows applications to scale up on multicore servers.
• Fault tolerance: Akka borrows the concepts and techniques from Erlang to build a "Let It Crash" fault-tolerance model using supervisor hierarchies to allow applications to fail fast and recover from the failure as soon as possible.
• Event-driven architecture: Asynchronous messaging makes Akka a perfect platform for building event-driven architectures.
• Transaction support: Akka implements transactors that combine actors and software transactional memory (STM) into transactional actors. This allows composition of atomic message flows with automatic retry and rollback.
• Location transparency: Akka treats remote and local process actors the same, providing a unified programming model for multicore and distributed computing needs.
• Scala/Java APIs: Akka supports both Java and Scala APIs for building applications.
What is an actor?
Actor is modeled as the object that encapsulates state and behavior.
All the messages intended for the actors are parked in a queue and actors process the messages from that queue
State and Behavior
这个很容易理解, 如下图
actor需要确保state不会丢失, crash, 并可以被concurrency访问
Akka implements actors as a reactive, event-driven, lightweight thread that shields and protects the actor's state. Actors provide the concurrent access to the state allowing us to write programs without worrying about concurrency and locking issues.
When the actors fail and are restarted, the actors' state is reinitialized to make sure that the actors behave in a consistent manner with a consistent state.
Mailbox
用于存放收到message的地方, Akka提供多种类型的mailbox, 有限, 无限, 优先级
Akka provides multiple mailbox implementations. The mailboxes can be bounded or unbounded.
Akka provides a priority mailbox where the messages are enqueued based on the assigned priority.
Actor lifecycle
Actor定义preStart和postStop接口用于初始化和善后
Every actor that is defined and created has an associated lifecycle.
Akka provides hooks such as preStart that allow the actor's state and behavior to be initialized.
When the actor is stopped, Akka disables the message queuing for the actor before PostStop is invoked. In the postStop hook, any persistence of the state or clean up of any hold-up resources can be done:
Fault tolerance
首先是actor hierarchy, 看看右图以项目管理为例
分而治之, 每个actor都有自己明确的职责, 不会去干涉和了解别人的工作, 大家通过message或输出物来沟通
同时PM需要监督和管理项目进行, 如果actor发现自己处理不了的message或error, 需要向上级反映以得到帮助
并且Akka actor都只会有一个supervisor
Akka的fault-tolerance就是基于actor hierarchy和supervisor model
The whole idea is to break down the task into smaller tasks to the point where the task is granular and structured enough to be performed by one actor.
Each actor knows which kind of message it will process and how he reacts in terms of failure.
So, if the actor does not know how to handle a particular message or an abnormal runtime behavior, the actor asks its supervisor for help. The recursive actor hierarchy allows the problem to be propagated upwards to the point where it can be handled.
Remember, every actor in Akka has one and only one supervisor.
This actor hierarchy forms the basis of the Akka's "Let It Crash" fault-tolerance model.
Akka's fault-tolerance model is built using the actor hierarchy and supervisor model.
Location transparency
Akka通过配置来透明化actor的位置
Akka uses configuration to indicate whether the actor is running locally or on a remote machine.
Akka uses the actor hierarchy and combines it with the actor system address to make each actor identifiable and reachable.
总结
As we move ahead and delve deep into the constructs provided by the Akka framework, we need to make sure that we keep in mind the following concepts:
• An actor is a computation unit with state, behavior, and its own mailbox
• There are two types of actors—untyped and typed
• Communication between actors can be asynchronous or synchronous
• Message passing to the actors happens using dispatchers
• Actors are organized in a hierarchy via the actor system
• Actors are proxied via ActorRef
• Supervisor actors are used to build the fault-tolerance mechanism
• Actor path follows the URL scheme, which enables location transparency
• STM is used to provide transactional support to multiple actor state updates
2 Starting with Akka
这章写的非常好, 非常详细的描述的Akka的环境搭建, 需要可以参考, 这儿就不列了
并且通过一个例子来告诉你Akka怎么用, 最给力的是给出了Java和Scala两个版本, 估计比一下, 谁都想把java扔到垃圾堆去
要解决的问题很简单, 对句子分词, 统计, 最后aggregate的过程
对应到Actor基本就是下面的过程,
看看scala怎么实现的, 很清晰, 一下就对Akka有个全局的概念
定义Message类, 为了后面用模式匹配, 加上case
sealed trait MapReduceMessage
case class WordCount(word: String, count: Int) extends MapReduceMessage
case class MapData(dataList: ArrayBuffer[WordCount]) extends MapReduceMessage
case class ReduceData(reduceDataMap: Map[String, Int]) extends MapReduceMessage
case class Result extends MapReduceMessage
MapActor.scala
package akka.first.app.mapreduce.actors
import akka.actor.Actor
import akka.actor.ActorRef
import akka.first.app.mapreduce.MapData
import akka.first.app.mapreduce.WordCount
import scala.collection.mutable.ArrayBuffer
class MapActor extends Actor {
val STOP_WORDS_LIST = List("a", "am", "......", "to") //stopword,列出部分
val defaultCount: Int = 1
def receive: Receive = {
case message: String => //匹配string,即原始的句子
sender ! evaluateExpression(message) //将结果发送给reduceActor
}
def evaluateExpression(line: String): MapData = MapData {
line.split("""\s+""").foldLeft(ArrayBuffer.empty[WordCount]) {//比较奇葩的操作, 类似reduce
(index, word) =>
if(!STOP_WORDS_LIST.contains(word.toLowerCase))
index += WordCount(word.toLowerCase, 1)
else
index
}
}
}
ReduceActor.scala
package akka.first.app.mapreduce.actors
import scala.collection.immutable.Map
import akka.actor.Actor
import akka.first.app.mapreduce.MapData
import akka.first.app.mapreduce.ReduceData
import akka.first.app.mapreduce.WordCount
class ReduceActor extends Actor {
def receive: Receive = {
case MapData(dataList) => //匹配MapData message类
sender ! reduce(dataList) //发送到aggregator
}
def reduce(words: IndexedSeq[WordCount]): ReduceData = ReduceData {
words.foldLeft(Map.empty[String, Int]) { (index, words) => //又使用foldLeft
if (index contains words.word)
index + (words.word -> (index.get(words.word).get + 1))
else
index + (words.word -> 1)
}
}
}
AggregateActor.scala
package akka.first.app.mapreduce.actors
import scala.collection.immutable.Map
import scala.collection.mutable.HashMap
import akka.actor.Actor
import akka.first.app.mapreduce.ReduceData
import akka.first.app.mapreduce.Result
class AggregateActor extends Actor {
val finalReducedMap = new HashMap[String, Int] //存放统计结果
def receive: Receive = { //需要处理两种message
case ReduceData(reduceDataMap) => //ReduceData Message, 需要aggregate
aggregateInMemoryReduce(reduceDataMap)
case Result => //结果请求
sender ! finalReducedMap.toString()
}
def aggregateInMemoryReduce(reducedList: Map[String, Int]): Unit = {
for ((key,value) <- reducedList) {
if (finalReducedMap contains key)
finalReducedMap(key) = (value + finalReducedMap.get(key).get)
else
finalReducedMap += (key -> value)
}
}
}
MasterActor.scala
Supervisor负责协调各个actors
package akka.first.app.mapreduce.actors
import akka.actor.Actor
import akka.actor.ActorRef
import akka.actor.Props
import akka.first.app.mapreduce._
import akka.routing.RoundRobinRouter
class MasterActor extends Actor {
//定义各个actor, 用context.actorOf
val mapActor = context.actorOf(Props[MapActor].withRouter(
RoundRobinRouter(nrOfInstances = 5)), name = "map")
val reduceActor = context.actorOf(Props[ReduceActor].withRouter(
RoundRobinRouter(nrOfInstances = 5)), name = "reduce")
val aggregateActor = context.actorOf(Props[AggregateActor], name = "aggregate")
def receive: Receive = {
case line: String =>
mapActor ! line
case mapData: MapData =>
reduceActor ! mapData
case reduceData: ReduceData =>
aggregateActor ! reduceData
case Result =>
aggregateActor forward Result
}
}
MapReduceApplication.scala
整个执行程序
package akka.first.app.mapreduce
import scala.collection.immutable.Map
import scala.collection.mutable.ArrayBuffer
import akka.actor.actorRef2Scala
import akka.actor.ActorSystem
import akka.actor.Props
import akka.dispatch.Await
import akka.first.app.mapreduce.actors.MasterActor
import akka.pattern.ask
import akka.util.duration.intToDurationInt
import akka.util.Timeout sealed trait MapReduceMessage
case class WordCount(word: String, count: Int) extends MapReduceMessage
case class MapData(dataList: ArrayBuffer[WordCount]) extends MapReduceMessage
case class ReduceData(reduceDataMap: Map[String, Int]) extends MapReduceMessage
case class Result extends MapReduceMessage object MapReduceApplication extends App {
val _system = ActorSystem("MapReduceApp")
val master = _system.actorOf(Props[MasterActor], name = "master") //创建master actor
implicit val timeout = Timeout(5 seconds)
master ! "The quick brown fox tried to jump over the lazy dog and fell on the dog"
master ! "Dog is man's best friend"
master ! "Dog and Fox belong to the same family"
Thread.sleep(500)
val future = (master ? Result).mapTo[String]
val result = Await.result(future, timeout.duration)
println(result)
_system.shutdown
}
Akka Essentials - 1的更多相关文章
- Akka Essentials - 2
Actors Defining an actor class MyActor extends Actor { def receive = { } } In Scala, the receive blo ...
- akka构建简单分布式应用
http://www.cnblogs.com/hequn/articles/3764630.html 当程序的要求达到一台计算机的极限时,我们便需要将程序分布式化,让程序运行在多台计算机上.akka提 ...
- 2014.8.12-AKKA和Actor model 分布式开发环境学习小结
学习使用AKKA 断断续续有一年了. 眼下还是习惯用java来写akka以下的程序.对于原生的scala还是没有时间和兴趣去学习它. 毕竟学习一门语言须要兴趣和时间的. AKKA学习资源还是不算丰富. ...
- Akka.net路径里的user
因为经常买双色球,嫌每次对彩票号麻烦,于是休息的时候做了个双色球兑奖的小程序,做完了发现业务还挺复杂的,于是改DDD重做设计,拆分服务,各种折腾...,不过这和本随笔没多大关系,等差不多了再总结一下, ...
- Aaron Stannard谈Akka.NET 1.1
Akka.NET 1.1近日发布,带来新特性和性能提升.InfoQ采访了Akka.net维护者Aaron Stannard,了解更多有关Akka.Streams和Akka.Cluster的信息.Aar ...
- Akka.NET v1.0 已发布,支持Mono
Akka.NET 是Java/Scala 流行框架Akka的一个 .NET 开源移植.可用于构建高并发,分布式和容错事件驱动的应用在 .NET 和 Mono 平台之上.Akka.NET 经过一年多的努 ...
- AKKA 笔记 - 有限状态机 -2
AKKA 笔记 - 有限状态机 -2 原文地址: http://rerun.me/2016/05/22/akka-notes-finite-state-machines-2/ 在上一节的Akka FS ...
- [翻译]AKKA笔记 - 有限状态机 -1
原文地址:http://rerun.me/2016/05/21/akka-notes-finite-state-machines-1/ 我最近有个机会在工作上使用了Akka FSM,是个非常有趣的例子 ...
- [翻译]AKKA笔记 -ACTOR SUPERVISION - 8
失败更像是分布式系统的一个特性.因此Akka用一个容忍失败的模型,在你的业务逻辑与失败处理逻辑(supervision逻辑)中间你能有一个清晰的边界.只需要一点点工作,这很赞.这就是我们要讨论的主题. ...
随机推荐
- Tokyo Tyrant(TTServer)系列(六)-数据丢失谁的错
,false,1,100);$mem->addServer ("127.9.9.1",1978,false,1,100);$start=microtime(true);for ...
- Form.ShowWithoutActivation 属性
获取一个值,该值指示显示窗口时是否激活它. 命名空间: System.Windows.Forms程序集: System.Windows.Forms(在 System.Windows.Forms.d ...
- SMARTY 变量
变量 模板变量以美元符号$开头,由字母.数组和下划线组成,和 PHP variable相似. 变量可以引用数字索引或非数字索引的数组,对象的属性和方法等. 配置变量 是例外的,它不是以美元符号$开头, ...
- sdut Mountain Subsequences 2013年山东省第四届ACM大学生程序设计竞赛
Mountain Subsequences 题目描述 Coco is a beautiful ACMer girl living in a very beautiful mountain. There ...
- 广告过滤神器(ADMuncher)4.93
Ad Muncher 介绍: 浏览网页时,冷不防地被网站播放的MIDI音乐声音吓一跳,或是因为弹出的广告窗口碍事,而影响你上网络的心情.Ad Muncher支持Netscape.Inte ...
- Jquery学习笔记(2)--五角星评分
网购五星评分模拟: <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- iOS swift HandyJSON组合Alamofire发起网络请求并转换成模型
在swift开发中,发起网络请求大部分开发者应该都是使用Alamofire发起的网络请求,至于请求完成后JSON解析这一块有很多解决方案,我们今天这里使用HandyJSON来解析请求返回的数据并转化成 ...
- centos 6.5 安装mysql 5.7.21 community
Step1: 检测系统是否自带安装mysql # yum list installed | grep mysql Step2: 删除系统自带的mysql及其依赖命令: # yum -y remove ...
- httpd-2.2.22安装
cgi部分共分三部分:html,cgi和服务器(httpd). 首先介绍httpd安装: 1. 软件包:httpd-2.2.22.tar.gz 系统:3.8.0-44-generic #66~pre ...
- org.apache.jasper.JasperException: The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in ……
编程中遇到:org.apache.jasper.JasperException: The absolute uri: http://java.sun.com/jsp/jstl/core cannot ...