Actor模型



有关并行(Parallelism),不得不提的肯定是Erlang,通用的面向并发的函数编程语言,这种编程语言的选择,也是可以规避我们在程序设计上面减少很多精力放在锁上面,这样的设计方式,导致Erlang的性能特别好。

Erlang主要的设计框架的精华是 Actor模型。

Actor模型


先科普一下Reactor pattern,高手勿喷。
我们在用的比较多的,C, C++, Java这些语言的时候,本质还是一个面向过程的设计,随着对性能的要求,和优化的思想,出现了multithreading multiprocessing,这些都是为了并发(concurrency)提升性能。但是终究迫于整个架构影响,性能现在集大成的思想是node.js使用的Reactor pattern

Reactor pattern
Reactor pattern

回到Actor模型

The actor model in computer science is a mathematical model of concurrent computation that treats "actors" as the universal primitives of concurrent computation: in response to a message that it receives, an actor can make local decisions, create more actors, send more messages, and determine how to respond to the next message received.
The Actor model adopts the philosophy thateverything is an actor. This is similar to the everything is an object philosophy used by some object-oriented programming languages.

简单的交互方式如下,

Actor Model
Actor Model

在我们对传统模型不断压榨之后,发现性能上去了一些,但是终究设计起来还是很吃力,并且很难控制程序的稳定性,需要各种HA的帮助,保证(Web大型服务)的可靠性,不down机。后来大家发现诞生于上世纪80年代的Erlang了,这种一切皆Actor,面向并发的特性就是梦寐以求的。

Actor 模型中核心的是supervisor

A supervisor is responsible for starting, stopping and monitoring its child processes. The basic idea of a supervisor is that it should keep its child processes alive by restarting them when necessary.
有下面两种方式保障高可靠性,All-For-One and One-For-One.

One-For-One
One-For-One
All-For-One
All-For-One

可以看到整个体系已经不是传统的基于流程的设计,完全的是一套去中心化的,需要supervisor负责下面actor(process)的状态即可,一个actor挂了,在创建一个即可。actor之间通过message的方式通信。
这些被现在的后起之秀akka吸收进来,
Akka is an open-source toolkit and runtime simplifying the construction of concurrent and distributed applications on the JVM. Akka supports multiple programming models for concurrency, but it emphasizes actor-based concurrency, with inspiration drawn from Erlang.


这种优美的方案,第一次看到还是在读Kevin Kelly,《失控》

“若干年内利用几百万只低成本小机器人入侵一颗地外行星是可能的。”他提议用一次性火箭发射一群鞋盒大小的太阳能推土机去入侵月球。派出一支由无足轻重、能力有限的机器人个体组成的军队,让它们协同完成任务,并允许它们自由行动。有些士兵会死掉,大多数会继续工作,并最终做出一些成绩。
绝大多数的移动机器人会在着陆后的数月内死去。日复一日的严寒酷热会使电脑芯片开裂失效。但就像蚂蚁群落,单个的移动机器人是无足轻重的。和“漫步者”相比,他们被发射到太空的费用要便宜上千倍;这样一来,即便发射数百个小机器人,其成本也只是一个大机器人的零头。

其实就是这个模型,这种模型,可以有效自适应,对于我们现在常见的大型系统特别高效。


http://www.jianshu.com/p/6f6efaed11b1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值