怒涛のAkka: General
akka generalのまとめ,重要だと思った箇所のみ抜粋している.
Actor Sytem
- ActorSystem(Actorを作るobject)は,threadをたくさん使い,heavyweight stuructureなので,1つのロジカルアプリケーションに1つつくること.
- Hierachical Structure
- 親acotrが子actorを監視する.子actorのエラーハンドリングは親actorが行う
- リクエストにも依るが,リクエスト毎に子アクターを作ることがいい場合もある,そうすることで,状態のマネジメントが楽になる場合がある.これは"Error Kernel Pattern"と呼ばれている
Best Practice
- event-driven manner, actorはblockするべきでない(threadを占めるべきでない),e.g., lock, network socket
- actors間でmutableなobjectを受け渡すべきでない
- actorは状態と行動の入れもの.行動を受け渡さいこと
- actorは,階層システムだということをを頭に入れておいて,エラーハンドリングをすること.gurdian actorへ負荷をあまりかけないようにすること.
Blocking Needs Careful Management
- blockin callは,適切なサイズなthread poolを持つactor or routerで行うこと.
- blockin callはFutureを使う.ただし,その際には,上限を持つthread poolを与えること.
- blocking resourceの集合を管理するために1 threadを当てること(manage用の親アクターを作るということか?)
- DB関連の典型例は,routerを作って,そこで作成されるactor毎にDB connectionをもたせる.
What Supervision Means
supervisorはfailure時に4つの選択がある
- resume: 子actorの状態を保って再開
- restart: 子actorの状態をクリアして再開
- stop: 子actorを止める
- escalate: 自分もfailureとて,親へエスカレーションする
The Top-Level Supervisors
ActorSystemを作ると3つのアクターができる
- root guardian: root actor. top-level actor.
- system guardian: system actor. user guardianを監視している
- user guardian: user actor. system.actorOf()で作成されたacotrはこのactorの下にできる. e.g., /user/${actorname}.
What Restarting Means
Restartのシークエンス
1. actorを一時停止,子actorも一時停止
2. old instance (actor)のpreRestart hookを呼ぶ .defaultでは子供にterminationメッセージを読んでpostStopを呼ぶ
3. すべての子供が止まるまで待つ.これはnon-blocking.
4. 新しいactorを作る
5. 新しいactorでpostStartを呼ぶ(defaultだとpreStartも呼ぶ)
6. 3でkilledでない子供には,restart requestを送る
7. resume actro
One-For-One Strategy vs. All-For-One Strategy
Supervision Strategyには2通りある
- OneForOneStrategy: 子供のfailureで兄弟をもfailure扱いされない.Default.
- AllForOneStrategy: 子供のfailureで兄弟をもfailure扱い.子供に強い結びつきがある時に使用する.
Actor References, Paths and Addresses
- ここ図をみる
- actor.context.self.pathという階層構造になっていて,ざっくり
- actor.context: conext
- actor.context.self: actorRef
- actor.context.parent: actorRef
- actor.context.children: actorRef
- actor.context.self.path: path
What is an Actor Reference?
- Purely local: networkingをサポートしないactorSystemで使われる.
- Local actor references: networkingをサポートするactorSystemで使われるが,同じJVM内の場合.
- There is a subtype of local actor references which is used for routers: 論理構造はlocal actore referenceと同じだが,routerに使用される
- Remote actor references: remote JVMへメッセージを送るのに使用される
- その他
- PromiseActorRef: askの時にメッセージを受け取る際のpromiseの特別な表現
- DeadLetterActorRef: Dead Letters Serviceのdefault実装
- EmptyLocalActorRef: DeadLetterActorRefと同じだが,pathを持っている
What is an Actor Path?
- Logical Actor Paths: networkのtraverを含むパス
- Physical Actor Paths: networkのtraverを含まないパス
How are Actor References obtained?
- ActorSystem.actorSelectionで,Actor Referencesのlookupができる
- seletion時に,wildcardが使えるが,single ActorSystem.actorSelectionが返ってくるわけではなく,Actor Refから呼べる関数がすべて呼べるわけではないので注意.
The Interplay with Remote Deployment
- この図をみる
- context.parent (phycical path)とcontext.path.parent (logical path)は異なる.
Top-Level Scopes for Actor Paths
- /user: ActorSystem.actorOf で作成されてたactor
- /system: systemに作成されたtop-levelのactor, e.g., logging actorとかコンフィグでディプロイされたActor
- /deadLetters: ストップ or 存在しないactorへのメッセージが送られてくる.on a best-effort basis: messages may be lost even within the local JVMらしい
- /temp: askとかのリプライを待つactorがここパスの下に置かれる
- /remote: supervisor (i.e., 親)がremoteにいる場合のactorが置かれるパス
アクターは階層だということに注意.
Peer-to-Peer vs. Client-Server
Actor SystemはPeer-to-Peerだということ
The General Rules
messageを送る(ask, tell)にはルールがある
- at-most-one-delivery (i.e., no guarantee delivery): actorのよくある実装
- message ordering per sender-receiver pair: akka独自 (networkの都合でremote actorへのmessages orderが変わることはないのか?)