Actor, an elegant model for concurrent and distributed computation
1. Actor, an elegant model for concurrent and
distributed computation
1
Alessio Coltellacci
Twitter: @lightplay8 Github: @NotBad4U
Mail: alessio.coltellacci@clever-cloud.com
3. Actor, an elegant model for concurrent computation
3
Concurrency refers to the ability of different parts or units
of a program to be executed out-of-order or in partial
order, without affecting the final outcome
TL;DR: dealing a lots of things at once
9. Communicating Sequential Processes (CSP)
CONSUMER ==
nb : integer;
*[
true -> producer ? nb; print (nb)
]
9
PRODUCER ==
nb : integer := 0;
*[
nb < 100 -> consumer ! nb; nb :=
nb + 1
]
10. Communicating Sequential Processes (CSP)
CONSUMER ==
nb : integer;
*[
true -> producer ? nb; print (nb)
]
10
PRODUCER ==
nb : integer := 0;
*[
nb < 100 -> consumer ! nb; nb :=
nb + 1
]
11. Communicating Sequential Processes (CSP)
CONSUMER ==
nb : integer;
*[
true -> producer ? nb; print (nb)
]
11
PRODUCER ==
nb : integer := 0;
*[
nb < 100 -> consume ! nb; nb := nb + 1
]
MAIN ==
[
producer :: PRODUCER
||
delegate :: DELEGATE
]
12. 12
Distributed system is a system whose components are located on different
networked computers, which coordinate their actions by passing messages to
one another
TL;DR: multiple software components that are on multiple
computers
Actor, an elegant model for distributed computation
25. Actor Axiomes
1. Create actors to replace failed actors or to increase the processing
2. Send messages to actors it knows i.e. Acquaintances
3. Behaviour: designate what to do with the next message
25
26. Actor Axiomes illustrated
26
1. class Energy(value: Int)(region: ActorRef) extends Actor {
2.
3. def receive: Receive = {
4. case TryConsume =>
5. sender ! Consumed(value)
6. region ! Destroy(self)
7. context become consumed
8. }
9.
10. def consumed: Receive = {
11. case _ =>
12. ()
13. }
14. }
27. Actor Axiomes illustrated
27
1. class Energy(value: Int)(region: ActorRef) extends Actor {
2.
3. def receive: Receive = {
4. case TryConsume =>
5. sender ! Consumed(value)
6. region ! Destroy(self)
7. context become consumed
8. }
9.
10. def consumed: Receive = {
11. case _ =>
12. ()
13. }
14. }
28. Actor Axiomes illustrated
28
1. class Energy(value: Int)(region: ActorRef) extends Actor {
2.
3. def receive: Receive = {
4. case TryConsume =>
5. sender ! Consumed(value)
6. region ! Destroy(self)
7. context become consumed
8. }
9.
10. def consumed: Receive = {
11. case _ =>
12. ()
13. }
14. }
29. Actor Axiomes illustrated
29
1. class Energy(value: Int)(region: ActorRef) extends Actor {
2.
3. def receive: Receive = {
4. case TryConsume =>
5. sender ! Consumed(value)
6. region ! Destroy(self)
7. context become consumed
8. }
9.
10. def consumed: Receive = {
11. case _ =>
12. ()
13. }
14. }
30. Actor Axiomes illustrated
30
1. class Energy(value: Int)(region: ActorRef) extends Actor {
2.
3. def receive: Receive = {
4. case TryConsume =>
5. sender ! Consumed(value)
6. region ! Destroy(self)
7. context become consumed
8. }
9.
10. def consumed: Receive = {
11. case _ =>
12. ()
13. }
14. }
31. Properties of Communication
The only way for an Actor to affect another
is through explicit messaging.
This allow to avoid concurrency problem.
31
32. Do not communicate by sharing memory;
instead, share memory by communicating.
32
39. The isolated Turn (epoch) principle
● Macro-step semantics
● An actor processing a message from its Inbox
● Single isolated step
● Model is free of low-level data race & deadlocks
39
43. Can I use ?
Synchronisation: superviser actor
Actor
consumer
Actor
consumer
Actor
consumer
Actor
superviser
Actor supply
clerk
Can I use ?
Actor don’t compete for resources they use a superviser
43
44. You usually have
to wait for that
which is worth
waiting for.
Synchronisation: superviser actor
Actor
consumer
Actor
consumer
Actor
consumer
Actor
superviser
Actor supply
clerk
yes
44
59. Handling Failure
59
“An actor never fails:
If a server S crashes, the next
message sent to an actor A that was
running on S causes Orleans to
automatically reinstantiate A on
another server.”
TL;DR: If the recipient doesn’t exist…
create it so thing will never fails
60. Akka Hierarchy and Supervision
● Supervision
● Strategy (Resume, Restart, Stop or Escalate)
60
63. Blue Green
Actors can be improved or re-factored
without affecting or stopping the rest of the application
63
64. Actor Use-Cases
64
● Multithreaded program for concurrent computation
● Distributed system
● System that can need mobility and discovery
● Based on events or commands (event s
65. Anti Use-Cases
65
“Shared-state concurrency is nevertheless a fundamental programming style,
needed for systems code, for maximal performance, and for implementing other
styles of concurrency.”
Aaron Turon
TL;DR: If you need true execution time performance, don’t use the
actor model
66. Concurrent, distributed and mobile computation
● Join-Calculus: Formalism for distributed computations design
● Brane-Calculus: …
● Ambient-Calculus: Formalism for distributed and mobile computations
design
66