Scaling   software with              Jonas Bonér                CTO Typesafe               Twitter: @jbonerAkka: ソフトウェアをスケ...
Scaling   Scaling software withsoftware with
Scaling            ScalingCopyright Ingeborg van Leeuwen          software with         software with
Selection of Akka Production Users
Manage System Overloadシステムの過負荷を管理する
Automatic Replication & Distribution   for Fault-tolerance & Scalability耐障害性とスケーラビリティを自動的に実現
Program at a Higher Level高レベルプログラミング
Program at a Higher Level高レベルプログラミング
Program at a Higher Level   • Never think in terms of shared state, state      visibility, threads, locks, concurrent coll...
Program at a Higher Level   • Never think in terms of shared state, state      visibility, threads, locks, concurrent coll...
Program at a Higher Level   • Never think in terms of shared state, state      visibility, threads, locks, concurrent coll...
Program at a Higher Level   • Never think in terms of shared state, state      visibility, threads, locks, concurrent coll...
Distributable by Design生まれながらに分散化されている
Distributable by Design生まれながらに分散化されている
Distributable by Design• Actors are location transparent & distributable by design生まれながらに分散化されている
Distributable by Design• Actors are location transparent & distributable by design• Scale UP and OUT for free as part of t...
Distributable by Design• Actors are location transparent & distributable by design• Scale UP and OUT for free as part of t...
Distributable by Design• Actors are location transparent & distributable by design• Scale UP and OUT for free as part of t...
Distributable by Design• Actors are location transparent & distributable by design• Scale UP and OUT for free as part of t...
Distributable by Design• Actors are location transparent & distributable by design• Scale UP and OUT for free as part of t...
Distributable by Design• Actors are location transparent & distributable by design• Scale UP and OUT for free as part of t...
How         can we achieve this?これをどうやって実現するか?
How           can we achieve this?アクターを使おう
Let’s use Actors                How           can we achieve this?アクターを使おう
What is an Actor?コードを分割して、並行性、スケーラビリティ、耐障害性の高いアプリケーションを支援する
What is an Actor?   • Akkas unit of code organization is called an Actorコードを分割して、並行性、スケーラビリティ、耐障害性の高いアプリケーションを支援する
What is an Actor?   • Akkas unit of code organization is called an Actor   • Actors helps you create concurrent, scalable ...
What is an Actor?   • Akkas unit of code organization is called an Actor   • Actors helps you create concurrent, scalable ...
What is an Actor?   • Akkas unit of code organization is called an Actor   • Actors helps you create concurrent, scalable ...
What can I use Actors for?スレッド、コンポーネント、コールバックなどの代替
What can I use Actors for?    In different scenarios, an Actor may be an    alternative to:スレッド、コンポーネント、コールバックなどの代替
What can I use Actors for?    In different scenarios, an Actor may be an    alternative to:     -   a threadスレッド、コンポーネント、コ...
What can I use Actors for?    In different scenarios, an Actor may be an    alternative to:     -   a thread     -   an ob...
What can I use Actors for?    In different scenarios, an Actor may be an    alternative to:     -   a thread     -   an ob...
What can I use Actors for?    In different scenarios, an Actor may be an    alternative to:     -   a thread     -   an ob...
What can I use Actors for?    In different scenarios, an Actor may be an    alternative to:     -   a thread     -   an ob...
What can I use Actors for?    In different scenarios, an Actor may be an    alternative to:     -   a thread     -   an ob...
What can I use Actors for?    In different scenarios, an Actor may be an    alternative to:     -   a thread     -   an ob...
What can I use Actors for?    In different scenarios, an Actor may be an    alternative to:     -   a thread     -   an ob...
So, what is the      Actor Model?アクターモデルとは何か?
Carl Hewitt’s definition       http://bit.ly/hewitt-on-actors処理、記憶、通信を行う基礎単位。受信時の 3公理:新規作成、他のアクターとの通信、次のメッセージ処理の指定
Carl Hewitt’s definition  -   The fundamental unit of computation that embodies:               http://bit.ly/hewitt-on-acto...
Carl Hewitt’s definition  -   The fundamental unit of computation that embodies:      -   Processing                  http:...
Carl Hewitt’s definition  -   The fundamental unit of computation that embodies:      -   Processing      -   Storage      ...
Carl Hewitt’s definition  -   The fundamental unit of computation that embodies:      -   Processing      -   Storage      ...
Carl Hewitt’s definition  -   The fundamental unit of computation that embodies:      -   Processing      -   Storage      ...
Carl Hewitt’s definition  -   The fundamental unit of computation that embodies:      -   Processing      -   Storage      ...
Carl Hewitt’s definition  -   The fundamental unit of computation that embodies:      -   Processing      -   Storage      ...
Carl Hewitt’s definition  -   The fundamental unit of computation that embodies:      -   Processing      -   Storage      ...
4 core Actor operations           0. DEFINE           1. CREATE           2. SEND           3. BECOME           4. SUPERVI...
0. DEFINE     case class Greeting(who: String)     class GreetingActor extends Actor with ActorLogging {       def receive...
0. DEFINE                     Define the message(s) the Actor                      should be able to respond to     case cl...
0. DEFINE                     Define the message(s) the Actor                      should be able to respond to            ...
0. DEFINE                     Define the message(s) the Actor                      should be able to respond to            ...
1. CREATE  • CREATE - creates a new instance of an Actor  • Extremely lightweight (2.7 Million per Gb RAM)  • Very strong ...
CREATE Actor   case class Greeting(who: String)   class GreetingActor extends Actor with ActorLogging {     def receive = ...
CREATE Actor   case class Greeting(who: String)   class GreetingActor extends Actor with ActorLogging {     def receive = ...
CREATE Actor   case class Greeting(who: String)   class GreetingActor extends Actor with ActorLogging {     def receive = ...
CREATE Actor   case class Greeting(who: String)   class GreetingActor extends Actor with ActorLogging {     def receive = ...
CREATE Actor   case class Greeting(who: String)   class GreetingActor extends Actor with ActorLogging {     def receive = ...
CREATE Actor   case class Greeting(who: String)   class GreetingActor extends Actor with ActorLogging {     def receive = ...
Actors can form hierarchies             Guardian System Actor階層構造を作ることができる
Actors can form hierarchies             Guardian System Actor             system.actorOf(Props[Foo], “Foo”)階層構造を作ることができる
Actors can form hierarchies                Guardian System Actor          Foo   system.actorOf(Props[Foo], “Foo”)階層構造を作ること...
Actors can form hierarchies                  Guardian System Actor          Foo                context.actorOf(Props[A], “...
Actors can form hierarchies                  Guardian System Actor          Foo         A      context.actorOf(Props[A], “...
Actors can form hierarchies                  Guardian System Actor            Foo                           Bar           ...
Name resolution - like a file-system                   Guardian System Actor             Foo                           Bar ...
Name resolution - like a file-system                     Guardian System Actor        /Foo               Foo               ...
Name resolution - like a file-system                           Guardian System Actor            /Foo                     Fo...
Name resolution - like a file-system                               Guardian System Actor                /Foo               ...
Name resolution - like a file-system                                   Guardian System Actor                /Foo           ...
2. SEND非同期でノンブロッキングなメッセージの送信
2. SEND   • SEND - sends a message to an Actor非同期でノンブロッキングなメッセージの送信
2. SEND   • SEND - sends a message to an Actor   • Asynchronous and Non-blocking - Fire-forget非同期でノンブロッキングなメッセージの送信
2. SEND   • SEND - sends a message to an Actor   • Asynchronous and Non-blocking - Fire-forget   • EVERYTHING is asynchron...
2. SEND   • SEND - sends a message to an Actor   • Asynchronous and Non-blocking - Fire-forget   • EVERYTHING is asynchron...
2. SEND   • SEND - sends a message to an Actor   • Asynchronous and Non-blocking - Fire-forget   • EVERYTHING is asynchron...
2. SEND   • SEND - sends a message to an Actor   • Asynchronous and Non-blocking - Fire-forget   • EVERYTHING is asynchron...
2. SEND   • SEND - sends a message to an Actor   • Asynchronous and Non-blocking - Fire-forget   • EVERYTHING is asynchron...
SEND messagecase class Greeting(who: String)class GreetingActor extends Actor with ActorLogging {  def receive = {    case...
SEND messagecase class Greeting(who: String)class GreetingActor extends Actor with ActorLogging {  def receive = {    case...
Full examplecase class Greeting(who: String)class GreetingActor extends Actor with ActorLogging {  def receive = {    case...
Load Balancing
Routers
Routers     val router =      system.actorOf(        Props[SomeActor].withRouter(         RoundRobinRouter(nrOfInstances =...
Router + Resizer    val resizer =      DefaultResizer(lowerBound = 2, upperBound = 15)    val router =      system.actorOf...
…or from config        akka.actor.deployment {          /path/to/actor {            router = round-robin            nr-of-i...
…or from config       akka.actor.deployment {         /path/to/actor {           router = round-robin           resizer {  ...
3. BECOME動的にアクターの振る舞いを再定義振る舞いはスタックして push/pop することができる
3. BECOME • BECOME - dynamically redefines Actor’s behavior動的にアクターの振る舞いを再定義振る舞いはスタックして push/pop することができる
3. BECOME • BECOME - dynamically redefines Actor’s behavior • Triggered reactively by receive of message動的にアクターの振る舞いを再定義振る舞...
3. BECOME • BECOME - dynamically redefines Actor’s behavior • Triggered reactively by receive of message • In a type system...
3. BECOME • BECOME - dynamically redefines Actor’s behavior • Triggered reactively by receive of message • In a type system...
3. BECOME • BECOME - dynamically redefines Actor’s behavior • Triggered reactively by receive of message • In a type system...
Why would I want to do that?例) 輻輳時にアクターをルータへ変身、FSM の実装など
Why would I want to do that?  • Let a highly contended Actor adaptively transform itself into     an Actor Pool or a Route...
Why would I want to do that?  • Let a highly contended Actor adaptively transform itself into     an Actor Pool or a Route...
Why would I want to do that?  • Let a highly contended Actor adaptively transform itself into     an Actor Pool or a Route...
Why would I want to do that?  • Let a highly contended Actor adaptively transform itself into     an Actor Pool or a Route...
Why would I want to do that?  • Let a highly contended Actor adaptively transform itself into     an Actor Pool or a Route...
Why would I want to do that?  • Let a highly contended Actor adaptively transform itself into     an Actor Pool or a Route...
becomecontext become {  // new body  case NewMessage =>    ...}
Failure Recovery in Java/C/C# etc.従来のリカバリはスレッドを用いたもの
Failure Recovery in Java/C/C# etc.    •   You are given a SINGLE thread of control従来のリカバリはスレッドを用いたもの
Failure Recovery in Java/C/C# etc.    •   You are given a SINGLE thread of control    •   If this thread blows up you are ...
Failure Recovery in Java/C/C# etc.    •   You are given a SINGLE thread of control    •   If this thread blows up you are ...
Failure Recovery in Java/C/C# etc.    •   You are given a SINGLE thread of control    •   If this thread blows up you are ...
Failure Recovery in Java/C/C# etc.    •   You are given a SINGLE thread of control    •   If this thread blows up you are ...
Failure Recovery in Java/C/C# etc.    •   You are given a SINGLE thread of control    •   If this thread blows up you are ...
Failure Recovery in Java/C/C# etc.    •   You are given a SINGLE thread of control    •   If this thread blows up you are ...
Failure Recovery in Java/C/C# etc.    •   You are given a SINGLE thread of control    •   If this thread blows up you are ...
Just      LET IT CRASHクラッシュさせろ
クラッシュさせろ
4. SUPERVISEアクターの故障時は上司の Supervisor が通知され、対応する。通常処理とエラー処理が分離できる。
4. SUPERVISE    • SUPERVISE - manage another Actor’s failuresアクターの故障時は上司の Supervisor が通知され、対応する。通常処理とエラー処理が分離できる。
4. SUPERVISE    • SUPERVISE - manage another Actor’s failures    • Error handling in actors is handle by letting Actors   ...
4. SUPERVISE    • SUPERVISE - manage another Actor’s failures    • Error handling in actors is handle by letting Actors   ...
4. SUPERVISE    • SUPERVISE - manage another Actor’s failures    • Error handling in actors is handle by letting Actors   ...
Fault-tolerant        onion-layered        Error Kernel玉ねぎのように階層化された耐障害性
ErrorKernel
ErrorKernel
ErrorKernel
ErrorKernel
ErrorKernel
ErrorKernel
Node 1   Node 2ErrorKernel
SUPERVISE Actor          Every single actor has a        default supervisor strategy.        Which is usually sufficient.  ...
SUPERVISE Actor                   Every single actor has a                 default supervisor strategy.                 Wh...
SUPERVISE Actor class Supervisor extends Actor {   override val supervisorStrategy =                      (maxNrOfRetries ...
Manage failure    class Worker extends Actor {      ...        override def preRestart(          reason: Throwable, messag...
Remoting
Remote deployment   Just feed the ActorSystem with this configuration      akka {        actor {          provider =    akk...
Remote deployment   Just feed the ActorSystem with this configuration                                          Configure a R...
Remote deployment      Just feed the ActorSystem with this configuration                                                   ...
Remote deployment      Just feed the ActorSystem with this configuration                                                   ...
Remote deployment     Just feed the ActorSystem with this configuration                                              Configu...
Remote deployment     Just feed the ActorSystem with this configuration                                              Configu...
Remote deployment     Just feed the ActorSystem with this configuration                                              Configu...
Remote deployment      Just feed the ActorSystem with this configuration                                               Confi...
Remote deployment      Just feed the ActorSystem with this configuration                       Zero code changes           ...
Remote Lookup    val greeter = system.actorFor(        "akka://MySystem@machine1:2552/user/greeter")リモートアクターを探す
Can you see the problem?問題に気付いたかな?
Fixed Addresses    akka {      actor {        provider =    akka.remote.RemoteActorRefProvider        deployment    {     ...
Akka Cluster
Features       • Gossip-based Cluster Membership       • Leader determination       • Accrual Failure Detector       • Clu...
Enable clustering   akka {      actor {        provider = "akka.cluster.ClusterActorRefProvider"        ...      }        ...
Configure a clustered router      akka.actor.deployment	  {      	  	  /statsService/workerRouter	  {      	  	  	  	  rout...
...we have much much more
...we have much much more              TestKit      FSM                                        Pub/SubDurable Mailboxes   ...
get it and learn more       http://akka.io    http://letitcrash.com    http://typesafe.com
E0F
Scaling software with akka
Scaling software with akka
Upcoming SlideShare
Loading in …5
×

Scaling software with akka

2,812
-1

Published on

Scala Conference in Japan 2013

0 Comments
10 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,812
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
75
Comments
0
Likes
10
Embeds 0
No embeds

No notes for slide

Scaling software with akka

  1. 1. Scaling software with Jonas Bonér CTO Typesafe Twitter: @jbonerAkka: ソフトウェアをスケールさせる
  2. 2. Scaling Scaling software withsoftware with
  3. 3. Scaling ScalingCopyright Ingeborg van Leeuwen software with software with
  4. 4. Selection of Akka Production Users
  5. 5. Manage System Overloadシステムの過負荷を管理する
  6. 6. Automatic Replication & Distribution for Fault-tolerance & Scalability耐障害性とスケーラビリティを自動的に実現
  7. 7. Program at a Higher Level高レベルプログラミング
  8. 8. Program at a Higher Level高レベルプログラミング
  9. 9. Program at a Higher Level • Never think in terms of shared state, state visibility, threads, locks, concurrent collections, thread notifications etc.高レベルプログラミング
  10. 10. Program at a Higher Level • Never think in terms of shared state, state visibility, threads, locks, concurrent collections, thread notifications etc. • Low level concurrency plumbing BECOMES SIMPLE WORKFLOW - you only think about how messages flow in the system高レベルプログラミング
  11. 11. Program at a Higher Level • Never think in terms of shared state, state visibility, threads, locks, concurrent collections, thread notifications etc. • Low level concurrency plumbing BECOMES SIMPLE WORKFLOW - you only think about how messages flow in the system • You get high CPU utilization, low latency, high throughput and scalability - FOR FREE as part of the model高レベルプログラミング
  12. 12. Program at a Higher Level • Never think in terms of shared state, state visibility, threads, locks, concurrent collections, thread notifications etc. • Low level concurrency plumbing BECOMES SIMPLE WORKFLOW - you only think about how messages flow in the system • You get high CPU utilization, low latency, high throughput and scalability - FOR FREE as part of the model • Proven and superior model for detecting and recovering from errors高レベルプログラミング
  13. 13. Distributable by Design生まれながらに分散化されている
  14. 14. Distributable by Design生まれながらに分散化されている
  15. 15. Distributable by Design• Actors are location transparent & distributable by design生まれながらに分散化されている
  16. 16. Distributable by Design• Actors are location transparent & distributable by design• Scale UP and OUT for free as part of the model生まれながらに分散化されている
  17. 17. Distributable by Design• Actors are location transparent & distributable by design• Scale UP and OUT for free as part of the model• You get the PERFECT FABRIC for the CLOUD生まれながらに分散化されている
  18. 18. Distributable by Design• Actors are location transparent & distributable by design• Scale UP and OUT for free as part of the model• You get the PERFECT FABRIC for the CLOUD - elastic & dynamic生まれながらに分散化されている
  19. 19. Distributable by Design• Actors are location transparent & distributable by design• Scale UP and OUT for free as part of the model• You get the PERFECT FABRIC for the CLOUD - elastic & dynamic - fault-tolerant & self-healing生まれながらに分散化されている
  20. 20. Distributable by Design• Actors are location transparent & distributable by design• Scale UP and OUT for free as part of the model• You get the PERFECT FABRIC for the CLOUD - elastic & dynamic - fault-tolerant & self-healing - adaptive load-balancing, cluster rebalancing & actor migration生まれながらに分散化されている
  21. 21. Distributable by Design• Actors are location transparent & distributable by design• Scale UP and OUT for free as part of the model• You get the PERFECT FABRIC for the CLOUD - elastic & dynamic - fault-tolerant & self-healing - adaptive load-balancing, cluster rebalancing & actor migration - build extremely loosely coupled and dynamic systems that can change and adapt at runtime生まれながらに分散化されている
  22. 22. How can we achieve this?これをどうやって実現するか?
  23. 23. How can we achieve this?アクターを使おう
  24. 24. Let’s use Actors How can we achieve this?アクターを使おう
  25. 25. What is an Actor?コードを分割して、並行性、スケーラビリティ、耐障害性の高いアプリケーションを支援する
  26. 26. What is an Actor? • Akkas unit of code organization is called an Actorコードを分割して、並行性、スケーラビリティ、耐障害性の高いアプリケーションを支援する
  27. 27. What is an Actor? • Akkas unit of code organization is called an Actor • Actors helps you create concurrent, scalable and fault-tolerant applicationsコードを分割して、並行性、スケーラビリティ、耐障害性の高いアプリケーションを支援する
  28. 28. What is an Actor? • Akkas unit of code organization is called an Actor • Actors helps you create concurrent, scalable and fault-tolerant applications • Like Java EE servlets and session beans, Actors is a model for organizing your code that keeps many “policy decisions” separate from the business logicコードを分割して、並行性、スケーラビリティ、耐障害性の高いアプリケーションを支援する
  29. 29. What is an Actor? • Akkas unit of code organization is called an Actor • Actors helps you create concurrent, scalable and fault-tolerant applications • Like Java EE servlets and session beans, Actors is a model for organizing your code that keeps many “policy decisions” separate from the business logic • Actors may be new to many in the Java community, but they are a tried-and-true concept (Hewitt 1973) used for many years in telecom systems with 9 nines uptimeコードを分割して、並行性、スケーラビリティ、耐障害性の高いアプリケーションを支援する
  30. 30. What can I use Actors for?スレッド、コンポーネント、コールバックなどの代替
  31. 31. What can I use Actors for? In different scenarios, an Actor may be an alternative to:スレッド、コンポーネント、コールバックなどの代替
  32. 32. What can I use Actors for? In different scenarios, an Actor may be an alternative to: - a threadスレッド、コンポーネント、コールバックなどの代替
  33. 33. What can I use Actors for? In different scenarios, an Actor may be an alternative to: - a thread - an object instance or componentスレッド、コンポーネント、コールバックなどの代替
  34. 34. What can I use Actors for? In different scenarios, an Actor may be an alternative to: - a thread - an object instance or component - a callback or listenerスレッド、コンポーネント、コールバックなどの代替
  35. 35. What can I use Actors for? In different scenarios, an Actor may be an alternative to: - a thread - an object instance or component - a callback or listener - a singleton or serviceスレッド、コンポーネント、コールバックなどの代替
  36. 36. What can I use Actors for? In different scenarios, an Actor may be an alternative to: - a thread - an object instance or component - a callback or listener - a singleton or service - a router, load-balancer or poolスレッド、コンポーネント、コールバックなどの代替
  37. 37. What can I use Actors for? In different scenarios, an Actor may be an alternative to: - a thread - an object instance or component - a callback or listener - a singleton or service - a router, load-balancer or pool - a Java EE Session Bean or Message-Driven Beanスレッド、コンポーネント、コールバックなどの代替
  38. 38. What can I use Actors for? In different scenarios, an Actor may be an alternative to: - a thread - an object instance or component - a callback or listener - a singleton or service - a router, load-balancer or pool - a Java EE Session Bean or Message-Driven Bean - an out-of-process serviceスレッド、コンポーネント、コールバックなどの代替
  39. 39. What can I use Actors for? In different scenarios, an Actor may be an alternative to: - a thread - an object instance or component - a callback or listener - a singleton or service - a router, load-balancer or pool - a Java EE Session Bean or Message-Driven Bean - an out-of-process service - a Finite State Machine (FSM)スレッド、コンポーネント、コールバックなどの代替
  40. 40. So, what is the Actor Model?アクターモデルとは何か?
  41. 41. Carl Hewitt’s definition http://bit.ly/hewitt-on-actors処理、記憶、通信を行う基礎単位。受信時の 3公理:新規作成、他のアクターとの通信、次のメッセージ処理の指定
  42. 42. Carl Hewitt’s definition - The fundamental unit of computation that embodies: http://bit.ly/hewitt-on-actors処理、記憶、通信を行う基礎単位。受信時の 3公理:新規作成、他のアクターとの通信、次のメッセージ処理の指定
  43. 43. Carl Hewitt’s definition - The fundamental unit of computation that embodies: - Processing http://bit.ly/hewitt-on-actors処理、記憶、通信を行う基礎単位。受信時の 3公理:新規作成、他のアクターとの通信、次のメッセージ処理の指定
  44. 44. Carl Hewitt’s definition - The fundamental unit of computation that embodies: - Processing - Storage http://bit.ly/hewitt-on-actors処理、記憶、通信を行う基礎単位。受信時の 3公理:新規作成、他のアクターとの通信、次のメッセージ処理の指定
  45. 45. Carl Hewitt’s definition - The fundamental unit of computation that embodies: - Processing - Storage - Communication http://bit.ly/hewitt-on-actors処理、記憶、通信を行う基礎単位。受信時の 3公理:新規作成、他のアクターとの通信、次のメッセージ処理の指定
  46. 46. Carl Hewitt’s definition - The fundamental unit of computation that embodies: - Processing - Storage - Communication - 3 axioms - When an Actor receives a message it can: http://bit.ly/hewitt-on-actors処理、記憶、通信を行う基礎単位。受信時の 3公理:新規作成、他のアクターとの通信、次のメッセージ処理の指定
  47. 47. Carl Hewitt’s definition - The fundamental unit of computation that embodies: - Processing - Storage - Communication - 3 axioms - When an Actor receives a message it can: - Create new Actors http://bit.ly/hewitt-on-actors処理、記憶、通信を行う基礎単位。受信時の 3公理:新規作成、他のアクターとの通信、次のメッセージ処理の指定
  48. 48. Carl Hewitt’s definition - The fundamental unit of computation that embodies: - Processing - Storage - Communication - 3 axioms - When an Actor receives a message it can: - Create new Actors - Send messages to Actors it knows http://bit.ly/hewitt-on-actors処理、記憶、通信を行う基礎単位。受信時の 3公理:新規作成、他のアクターとの通信、次のメッセージ処理の指定
  49. 49. Carl Hewitt’s definition - The fundamental unit of computation that embodies: - Processing - Storage - Communication - 3 axioms - When an Actor receives a message it can: - Create new Actors - Send messages to Actors it knows - Designate how it should handle the next message it receives http://bit.ly/hewitt-on-actors処理、記憶、通信を行う基礎単位。受信時の 3公理:新規作成、他のアクターとの通信、次のメッセージ処理の指定
  50. 50. 4 core Actor operations 0. DEFINE 1. CREATE 2. SEND 3. BECOME 4. SUPERVISEアクターの4つの主な仕事
  51. 51. 0. DEFINE case class Greeting(who: String) class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) => log.info("Hello " + who) } }処理可能なメッセージ、振る舞いの定義
  52. 52. 0. DEFINE Define the message(s) the Actor should be able to respond to case class Greeting(who: String) class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) => log.info("Hello " + who) } }処理可能なメッセージ、振る舞いの定義
  53. 53. 0. DEFINE Define the message(s) the Actor should be able to respond to Define the Actor class case class Greeting(who: String) class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) => log.info("Hello " + who) } }処理可能なメッセージ、振る舞いの定義
  54. 54. 0. DEFINE Define the message(s) the Actor should be able to respond to Define the Actor class case class Greeting(who: String) class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) => log.info("Hello " + who) } } Define the Actor’s behavior処理可能なメッセージ、振る舞いの定義
  55. 55. 1. CREATE • CREATE - creates a new instance of an Actor • Extremely lightweight (2.7 Million per Gb RAM) • Very strong encapsulation - encapsulates: -  state -  behavior -  message queue • State & behavior is indistinguishable from each other • Only way to observe state is by sending an actor a message and see how it reacts新規アクターの作成。強力なカプセル化。メッセージを送ってその反応を見て状態を観測する。
  56. 56. CREATE Actor case class Greeting(who: String) class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) => log.info("Hello " + who) } } val system = ActorSystem("MySystem") val greeter = system.actorOf(Props[GreetingActor], name = "greeter")ActorRef が返ってくる
  57. 57. CREATE Actor case class Greeting(who: String) class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) Create an Actor system " + who) => log.info("Hello } } val system = ActorSystem("MySystem") val greeter = system.actorOf(Props[GreetingActor], name = "greeter")ActorRef が返ってくる
  58. 58. CREATE Actor case class Greeting(who: String) class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) Create an Actor system " + who) => log.info("Hello } Actor configuration } val system = ActorSystem("MySystem") val greeter = system.actorOf(Props[GreetingActor], name = "greeter")ActorRef が返ってくる
  59. 59. CREATE Actor case class Greeting(who: String) class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) Create an Actor system " + who) => log.info("Hello } Actor configuration } val system = ActorSystem("MySystem") val greeter = system.actorOf(Props[GreetingActor], name = "greeter") Give it a nameActorRef が返ってくる
  60. 60. CREATE Actor case class Greeting(who: String) class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) Create an Actor system " + who) => log.info("Hello } Actor configuration } val system = ActorSystem("MySystem") val greeter = system.actorOf(Props[GreetingActor], name = "greeter") Create the Actor Give it a nameActorRef が返ってくる
  61. 61. CREATE Actor case class Greeting(who: String) class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) Create an Actor system " + who) => log.info("Hello } Actor configuration } val system = ActorSystem("MySystem") val greeter = system.actorOf(Props[GreetingActor], name = "greeter") Create the Actor You get an ActorRef back Give it a nameActorRef が返ってくる
  62. 62. Actors can form hierarchies Guardian System Actor階層構造を作ることができる
  63. 63. Actors can form hierarchies Guardian System Actor system.actorOf(Props[Foo], “Foo”)階層構造を作ることができる
  64. 64. Actors can form hierarchies Guardian System Actor Foo system.actorOf(Props[Foo], “Foo”)階層構造を作ることができる
  65. 65. Actors can form hierarchies Guardian System Actor Foo context.actorOf(Props[A], “A”)階層構造を作ることができる
  66. 66. Actors can form hierarchies Guardian System Actor Foo A context.actorOf(Props[A], “A”)階層構造を作ることができる
  67. 67. Actors can form hierarchies Guardian System Actor Foo Bar A A C E C B B D階層構造を作ることができる
  68. 68. Name resolution - like a file-system Guardian System Actor Foo Bar A A C E C B B Dファイルシステムのような名前解決
  69. 69. Name resolution - like a file-system Guardian System Actor /Foo Foo Bar A A C E C B B Dファイルシステムのような名前解決
  70. 70. Name resolution - like a file-system Guardian System Actor /Foo Foo Bar /Foo/A A A C E C B B Dファイルシステムのような名前解決
  71. 71. Name resolution - like a file-system Guardian System Actor /Foo Foo Bar /Foo/A A A C /Foo/A/B E C B B Dファイルシステムのような名前解決
  72. 72. Name resolution - like a file-system Guardian System Actor /Foo Foo Bar /Foo/A A A C /Foo/A/B E C B B D /Foo/A/Dファイルシステムのような名前解決
  73. 73. 2. SEND非同期でノンブロッキングなメッセージの送信
  74. 74. 2. SEND • SEND - sends a message to an Actor非同期でノンブロッキングなメッセージの送信
  75. 75. 2. SEND • SEND - sends a message to an Actor • Asynchronous and Non-blocking - Fire-forget非同期でノンブロッキングなメッセージの送信
  76. 76. 2. SEND • SEND - sends a message to an Actor • Asynchronous and Non-blocking - Fire-forget • EVERYTHING is asynchronous and lockless非同期でノンブロッキングなメッセージの送信
  77. 77. 2. SEND • SEND - sends a message to an Actor • Asynchronous and Non-blocking - Fire-forget • EVERYTHING is asynchronous and lockless • Everything happens Reactively非同期でノンブロッキングなメッセージの送信
  78. 78. 2. SEND • SEND - sends a message to an Actor • Asynchronous and Non-blocking - Fire-forget • EVERYTHING is asynchronous and lockless • Everything happens Reactively - An Actor is passive until a message is sent to it, which triggers something within the Actor非同期でノンブロッキングなメッセージの送信
  79. 79. 2. SEND • SEND - sends a message to an Actor • Asynchronous and Non-blocking - Fire-forget • EVERYTHING is asynchronous and lockless • Everything happens Reactively - An Actor is passive until a message is sent to it, which triggers something within the Actor - Messages is the Kinetic Energy in an Actor system非同期でノンブロッキングなメッセージの送信
  80. 80. 2. SEND • SEND - sends a message to an Actor • Asynchronous and Non-blocking - Fire-forget • EVERYTHING is asynchronous and lockless • Everything happens Reactively - An Actor is passive until a message is sent to it, which triggers something within the Actor - Messages is the Kinetic Energy in an Actor system - Actors can have lots of buffered Potential Energy but cant do anything with it until it is triggered by a message非同期でノンブロッキングなメッセージの送信
  81. 81. SEND messagecase class Greeting(who: String)class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) => log.info("Hello " + who) }}val system = ActorSystem("MySystem")val greeter = system.actorOf(Props[GreetingActor], name = "greeter")greeter ! Greeting("Charlie Parker")
  82. 82. SEND messagecase class Greeting(who: String)class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) => log.info("Hello " + who) }}val system = ActorSystem("MySystem")val greeter = system.actorOf(Props[GreetingActor], name = "greeter")greeter ! Greeting("Charlie Parker") Send the message
  83. 83. Full examplecase class Greeting(who: String)class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) => log.info("Hello " + who) }}val system = ActorSystem("MySystem")val greeter = system.actorOf(Props[GreetingActor], name = "greeter")greeter ! Greeting("Charlie Parker")
  84. 84. Load Balancing
  85. 85. Routers
  86. 86. Routers val router = system.actorOf( Props[SomeActor].withRouter( RoundRobinRouter(nrOfInstances = 5)))ルータを使う
  87. 87. Router + Resizer val resizer = DefaultResizer(lowerBound = 2, upperBound = 15) val router = system.actorOf( Props[ExampleActor1].withRouter( RoundRobinRouter(resizer = Some(resizer))))リサイザを加える
  88. 88. …or from config akka.actor.deployment { /path/to/actor { router = round-robin nr-of-instances = 5 } }config ファイルからも設定できる
  89. 89. …or from config akka.actor.deployment { /path/to/actor { router = round-robin resizer { lower-bound = 12 upper-bound = 15 } } }もしくは設定から
  90. 90. 3. BECOME動的にアクターの振る舞いを再定義振る舞いはスタックして push/pop することができる
  91. 91. 3. BECOME • BECOME - dynamically redefines Actor’s behavior動的にアクターの振る舞いを再定義振る舞いはスタックして push/pop することができる
  92. 92. 3. BECOME • BECOME - dynamically redefines Actor’s behavior • Triggered reactively by receive of message動的にアクターの振る舞いを再定義振る舞いはスタックして push/pop することができる
  93. 93. 3. BECOME • BECOME - dynamically redefines Actor’s behavior • Triggered reactively by receive of message • In a type system analogy it is as if the object changed type - changed interface, protocol & implementation動的にアクターの振る舞いを再定義振る舞いはスタックして push/pop することができる
  94. 94. 3. BECOME • BECOME - dynamically redefines Actor’s behavior • Triggered reactively by receive of message • In a type system analogy it is as if the object changed type - changed interface, protocol & implementation • Will now react differently to the messages it receives動的にアクターの振る舞いを再定義振る舞いはスタックして push/pop することができる
  95. 95. 3. BECOME • BECOME - dynamically redefines Actor’s behavior • Triggered reactively by receive of message • In a type system analogy it is as if the object changed type - changed interface, protocol & implementation • Will now react differently to the messages it receives • Behaviors are stacked & can be pushed and popped動的にアクターの振る舞いを再定義振る舞いはスタックして push/pop することができる
  96. 96. Why would I want to do that?例) 輻輳時にアクターをルータへ変身、FSM の実装など
  97. 97. Why would I want to do that? • Let a highly contended Actor adaptively transform itself into an Actor Pool or a Router例) 輻輳時にアクターをルータへ変身、FSM の実装など
  98. 98. Why would I want to do that? • Let a highly contended Actor adaptively transform itself into an Actor Pool or a Router • Implement an FSM (Finite State Machine)例) 輻輳時にアクターをルータへ変身、FSM の実装など
  99. 99. Why would I want to do that? • Let a highly contended Actor adaptively transform itself into an Actor Pool or a Router • Implement an FSM (Finite State Machine) • Implement graceful degradation例) 輻輳時にアクターをルータへ変身、FSM の実装など
  100. 100. Why would I want to do that? • Let a highly contended Actor adaptively transform itself into an Actor Pool or a Router • Implement an FSM (Finite State Machine) • Implement graceful degradation • Spawn up (empty) generic Worker processes that can become whatever the Master currently needs例) 輻輳時にアクターをルータへ変身、FSM の実装など
  101. 101. Why would I want to do that? • Let a highly contended Actor adaptively transform itself into an Actor Pool or a Router • Implement an FSM (Finite State Machine) • Implement graceful degradation • Spawn up (empty) generic Worker processes that can become whatever the Master currently needs • Other: Use your imagination!例) 輻輳時にアクターをルータへ変身、FSM の実装など
  102. 102. Why would I want to do that? • Let a highly contended Actor adaptively transform itself into an Actor Pool or a Router • Implement an FSM (Finite State Machine) • Implement graceful degradation • Spawn up (empty) generic Worker processes that can become whatever the Master currently needs • Other: Use your imagination! • Very useful once you get the used to it例) 輻輳時にアクターをルータへ変身、FSM の実装など
  103. 103. becomecontext become { // new body case NewMessage => ...}
  104. 104. Failure Recovery in Java/C/C# etc.従来のリカバリはスレッドを用いたもの
  105. 105. Failure Recovery in Java/C/C# etc. • You are given a SINGLE thread of control従来のリカバリはスレッドを用いたもの
  106. 106. Failure Recovery in Java/C/C# etc. • You are given a SINGLE thread of control • If this thread blows up you are screwed従来のリカバリはスレッドを用いたもの
  107. 107. Failure Recovery in Java/C/C# etc. • You are given a SINGLE thread of control • If this thread blows up you are screwed • So you need to do all explicit error handling WITHIN this single thread従来のリカバリはスレッドを用いたもの
  108. 108. Failure Recovery in Java/C/C# etc. • You are given a SINGLE thread of control • If this thread blows up you are screwed • So you need to do all explicit error handling WITHIN this single thread • To make things worse - errors do not propagate between threads so there is NO WAY OF EVEN FINDING OUT that something have failed従来のリカバリはスレッドを用いたもの
  109. 109. Failure Recovery in Java/C/C# etc. • You are given a SINGLE thread of control • If this thread blows up you are screwed • So you need to do all explicit error handling WITHIN this single thread • To make things worse - errors do not propagate between threads so there is NO WAY OF EVEN FINDING OUT that something have failed • This leads to DEFENSIVE programming with:従来のリカバリはスレッドを用いたもの
  110. 110. Failure Recovery in Java/C/C# etc. • You are given a SINGLE thread of control • If this thread blows up you are screwed • So you need to do all explicit error handling WITHIN this single thread • To make things worse - errors do not propagate between threads so there is NO WAY OF EVEN FINDING OUT that something have failed • This leads to DEFENSIVE programming with: • Error handling TANGLED with business logic従来のリカバリはスレッドを用いたもの
  111. 111. Failure Recovery in Java/C/C# etc. • You are given a SINGLE thread of control • If this thread blows up you are screwed • So you need to do all explicit error handling WITHIN this single thread • To make things worse - errors do not propagate between threads so there is NO WAY OF EVEN FINDING OUT that something have failed • This leads to DEFENSIVE programming with: • Error handling TANGLED with business logic • SCATTERED all over the code base従来のリカバリはスレッドを用いたもの
  112. 112. Failure Recovery in Java/C/C# etc. • You are given a SINGLE thread of control • If this thread blows up you are screwed • So you need to do all explicit error handling WITHIN this single thread • To make things worse - errors do not propagate between threads so there is NO WAY OF EVEN FINDING OUT that something have failed • This leads to DEFENSIVE programming with: • Error handling TANGLED with business logic • SCATTERED all over the code base We can do better than this!!!従来のリカバリはスレッドを用いたもの
  113. 113. Just LET IT CRASHクラッシュさせろ
  114. 114. クラッシュさせろ
  115. 115. 4. SUPERVISEアクターの故障時は上司の Supervisor が通知され、対応する。通常処理とエラー処理が分離できる。
  116. 116. 4. SUPERVISE • SUPERVISE - manage another Actor’s failuresアクターの故障時は上司の Supervisor が通知され、対応する。通常処理とエラー処理が分離できる。
  117. 117. 4. SUPERVISE • SUPERVISE - manage another Actor’s failures • Error handling in actors is handle by letting Actors monitor (supervise) each other for failureアクターの故障時は上司の Supervisor が通知され、対応する。通常処理とエラー処理が分離できる。
  118. 118. 4. SUPERVISE • SUPERVISE - manage another Actor’s failures • Error handling in actors is handle by letting Actors monitor (supervise) each other for failure • This means that if an Actor crashes, a notification will be sent to his supervisor, who can react upon the failureアクターの故障時は上司の Supervisor が通知され、対応する。通常処理とエラー処理が分離できる。
  119. 119. 4. SUPERVISE • SUPERVISE - manage another Actor’s failures • Error handling in actors is handle by letting Actors monitor (supervise) each other for failure • This means that if an Actor crashes, a notification will be sent to his supervisor, who can react upon the failure • This provides clean separation of processing and error handlingアクターの故障時は上司の Supervisor が通知され、対応する。通常処理とエラー処理が分離できる。
  120. 120. Fault-tolerant onion-layered Error Kernel玉ねぎのように階層化された耐障害性
  121. 121. ErrorKernel
  122. 122. ErrorKernel
  123. 123. ErrorKernel
  124. 124. ErrorKernel
  125. 125. ErrorKernel
  126. 126. ErrorKernel
  127. 127. Node 1 Node 2ErrorKernel
  128. 128. SUPERVISE Actor Every single actor has a default supervisor strategy. Which is usually sufficient. But it can be overridden.デフォルトの Supervisor 戦略をオーバーライドできる
  129. 129. SUPERVISE Actor Every single actor has a default supervisor strategy. Which is usually sufficient. But it can be overridden. class Supervisor extends Actor { override val supervisorStrategy = (maxNrOfRetries = 10, withinTimeRange = 1 minute) { case _: ArithmeticException => Resume case _: NullPointerException => Restart case _: Exception => Escalate }デフォルトの=Supervisor 戦略をオーバーライドできる val worker context.actorOf(Props[Worker])
  130. 130. SUPERVISE Actor class Supervisor extends Actor { override val supervisorStrategy = (maxNrOfRetries = 10, withinTimeRange = 1 minute) { case _: ArithmeticException => Resume case _: NullPointerException => Restart case _: Exception => Escalate } val worker = context.actorOf(Props[Worker]) def receive = { case n: Int => worker forward n } }デフォルトの Supervisor 戦略をオーバーライドできる
  131. 131. Manage failure class Worker extends Actor { ... override def preRestart( reason: Throwable, message: Option[Any]) { ... // clean up before restart } override def postRestart(reason: Throwable) { ... // init after restart } }故障の管理
  132. 132. Remoting
  133. 133. Remote deployment Just feed the ActorSystem with this configuration akka { actor { provider = akka.remote.RemoteActorRefProvider deployment { /greeter { remote = } } } }リモート配備: ActorSystem にこの設定を渡すだけ
  134. 134. Remote deployment Just feed the ActorSystem with this configuration Configure a Remote Provider akka { actor { provider = akka.remote.RemoteActorRefProvider deployment { /greeter { remote = } } } }リモート配備: ActorSystem にこの設定を渡すだけ
  135. 135. Remote deployment Just feed the ActorSystem with this configuration Configure a Remote Provider akka { For the Greeter actor { actor provider = akka.remote.RemoteActorRefProvider deployment { /greeter { remote = } } } }リモート配備: ActorSystem にこの設定を渡すだけ
  136. 136. Remote deployment Just feed the ActorSystem with this configuration Configure a Remote Provider akka { For the Greeter actor { actor provider = akka.remote.RemoteActorRefProvider deployment { /greeter { remote = } } Define Remote Path } }リモート配備: ActorSystem にこの設定を渡すだけ
  137. 137. Remote deployment Just feed the ActorSystem with this configuration Configure a Remote Provider akka { For the Greeter actor { actor provider = akka.remote.RemoteActorRefProvider deployment { /greeter { remote = akka:// } } Define Remote Path } Protocol }リモート配備: ActorSystem にこの設定を渡すだけ
  138. 138. Remote deployment Just feed the ActorSystem with this configuration Configure a Remote Provider akka { For the Greeter actor { actor provider = akka.remote.RemoteActorRefProvider deployment { /greeter { remote = akka://MySystem } } Define Remote Path } Protocol Actor System }リモート配備: ActorSystem にこの設定を渡すだけ
  139. 139. Remote deployment Just feed the ActorSystem with this configuration Configure a Remote Provider akka { For the Greeter actor { actor provider = akka.remote.RemoteActorRefProvider deployment { /greeter { remote = akka://MySystem@machine1 } } Define Remote Path } Protocol Actor System Hostname }リモート配備: ActorSystem にこの設定を渡すだけ
  140. 140. Remote deployment Just feed the ActorSystem with this configuration Configure a Remote Provider akka { For the Greeter actor { actor provider = akka.remote.RemoteActorRefProvider deployment { /greeter { remote = akka://MySystem@machine1:2552 } } Define Remote Path } Protocol Actor System Hostname Port }リモート配備: ActorSystem にこの設定を渡すだけ
  141. 141. Remote deployment Just feed the ActorSystem with this configuration Zero code changes Configure a Remote Provider akka { For the Greeter actor { actor provider = akka.remote.RemoteActorRefProvider deployment { /greeter { remote = akka://MySystem@machine1:2552 } } Define Remote Path } Protocol Actor System Hostname Port }リモート配備: ActorSystem にこの設定を渡すだけ
  142. 142. Remote Lookup val greeter = system.actorFor( "akka://MySystem@machine1:2552/user/greeter")リモートアクターを探す
  143. 143. Can you see the problem?問題に気付いたかな?
  144. 144. Fixed Addresses akka { actor { provider = akka.remote.RemoteActorRefProvider deployment { /greeter { remote = akka://MySystem@machine1:2552 } } } } val greeter = system.actorFor( "akka://MySystem@machine1:2552/user/greeter")アドレスが決め打ちされている
  145. 145. Akka Cluster
  146. 146. Features • Gossip-based Cluster Membership • Leader determination • Accrual Failure Detector • Cluster DeathWatch • Cluster-Aware RoutersGossip-based なクラスタ・メンバーシップ
  147. 147. Enable clustering akka { actor { provider = "akka.cluster.ClusterActorRefProvider" ... }    cluster { seed-nodes = [ "akka://ClusterSystem@127.0.0.1:2551", "akka://ClusterSystem@127.0.0.1:2552" ]   auto-down = on } }クラスタを使う
  148. 148. Configure a clustered router akka.actor.deployment  {    /statsService/workerRouter  {        router  =  consistent-­‐hashing        nr-­‐of-­‐instances  =  100        cluster  {            enabled  =  on            max-nr-of-instances-per-node = 3            allow-­‐local-­‐routees  =  on        }    } }クラスタ化されたルータの設定
  149. 149. ...we have much much more
  150. 150. ...we have much much more TestKit FSM Pub/SubDurable Mailboxes IO Camel EventBus Pooling Typed ChannelsMicrokernel SLF4J TypedActor ZeroMQ Dataflow Transactors Agents Extensions
  151. 151. get it and learn more http://akka.io http://letitcrash.com http://typesafe.com
  152. 152. E0F
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×