天天看點

Akka Essentials - 1

參考akka essentials

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.

Akka Essentials - 1

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和擴充

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

這個很容易了解, 如下圖

Akka Essentials - 1

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.

Akka Essentials - 1

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:

Akka Essentials - 1

fault tolerance

首先是actor hierarchy, 看看右圖以項目管理為例 

分而治之, 每個actor都有自己明确的職責, 不會去幹涉和了解别人的工作, 大家通過message或輸出物來溝通 

同時pm需要監督和管理項目進行, 如果actor發現自己處理不了的message或error, 需要向上級反映以得到幫助 

并且akka actor都隻會有一個supervisor

akka的fault-tolerance就是基于actor hierarchy和supervisor model

Akka Essentials - 1

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.

Akka Essentials - 1

總結

Akka Essentials - 1

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

這章寫的非常好, 非常詳細的描述的akka的環境搭建, 需要可以參考, 這兒就不列了 

并且通過一個例子來告訴你akka怎麼用, 最給力的是給出了java和scala兩個版本, 估計比一下, 誰都想把java扔到垃圾堆去

要解決的問題很簡單, 對句子分詞, 統計, 最後aggregate的過程

Akka Essentials - 1

對應到actor基本就是下面的過程,

Akka Essentials - 1

看看scala怎麼實作的, 很清晰, 一下就對akka有個全局的概念

定義message類, 為了後面用模式比對, 加上case

mapactor.scala

reduceactor.scala

aggregateactor.scala

masteractor.scala

supervisor負責協調各個actors

mapreduceapplication.scala

整個執行程式