40
C. Varela 1 Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute

C. Varela1 Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute

Embed Size (px)

Citation preview

Page 1: C. Varela1 Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute

C. Varela 1

Chapter 4: Actors

Programming Distributed Computing Systems: A Foundational Approach

Carlos Varela

Rensselaer Polytechnic Institute

Page 2: C. Varela1 Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute

Open Distributed Systems

• Addition of new components

• Replacement of existing components

• Changes in interconnections

C. Varela 2

Page 3: C. Varela1 Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute

Actor Configurations

• Model open system components:

– Set of indivitually named actors

– Messages “en-route”

– Interface to environment:

• Receptionists

• External actors

C. Varela 3

Page 4: C. Varela1 Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute

Synchronous vs. AsynchronousComminucation

• π-Calculus (and other process algebras such as CSS, CSP) take synchronous communication as a primitive

• Actors assume asynchronous communication is more primitive

C. Varela 4

Page 5: C. Varela1 Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute

Communication Medium

• In π-Calculus, channels are explicitly modelled.Multiple processes can share a channel, potentially causing intereference.

• In the actor model, the communication medium is not explicit. Actors (active objects) are first-class, history-sensitive entities with an explicit identity used for communication.

C. Varela 5

Page 6: C. Varela1 Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute

Fairness

• The actor model theory assumes fair computations:1. Message delivery is guaranteed.

2. Individual actor computations are guaranteed to progress.

Fairness is very useful for reasoning about equivalences of actor programs but can be hard/expensive to guarantee; in particular when distribution and failures are considered.

C. Varela 6

Page 7: C. Varela1 Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute

Programming Languages Influenced by П-Calculus and

Actors• Scheme ‘75

• Act1 ‘87

• Acore ‘87

• Rosette ‘89

• Obliq ‘94

• Erlang ‘93

• ABCL ‘90

• SALSA ‘99

C. Varela 7

• Amber ’86

• Facile ’89• CML ’91

• Pict ’94

• Nomadic Pict ’99• JOCAML ‘99

Page 8: C. Varela1 Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute

Actor (Agent) Model

C. Varela 8

Page 9: C. Varela1 Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute

Agha, Mason, Smith & Talcott

1. Extend a functional language (λ-calculus + ifs and pairs) with actor primitives.

2. Define an operational semantics for actor configurations.

3. Study various notions of equivalence of actor expressions and configurations.

4. Assume fairness:– Guaranteed message delivery.

– Individual actor progress.

C. Varela 9

Page 10: C. Varela1 Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute

λ-Calculus

Syntax

e ::= v value

| λv.e functional abstraction

| ( e e ) application

Example

( λx.x2 2 )

x2{2/x} (in π-calculus)

[2/x]x2 (in λ-calculus)

C. Varela 10

Page 11: C. Varela1 Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute

λ-Calculus

• pr(x,y) returns a pair containing x & y

• ispr(x) returns t if x is a pair; f otherwise

• 1st(pr(x,y)) = x returns the first value of a pair

• 2nd(pr(x,y)) = y returns the second value of a pair

C. Varela 11

Page 12: C. Varela1 Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute

Actor Primitives

• send(a,v)– Sends value v to actor a.

• new(b)– Creates a new actor with behavior b and returns the identity/name

of the newly created actor.

• ready(b)– Becomes ready to receive a new message with behavior b.

C. Varela 12

Page 13: C. Varela1 Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute

Actor Language Example

b5 = rec(λy. λx.seq(send(x,5), ready(y)))

Receives an actor name x and sends the number 5 to that actor, then it becomes ready to process new messages with the same behavior y.

Sample usage:

send(new(b5), a)

A Sink:

sink = rec(λb. λm.ready(b))

An actor that disregards all messages.C. Varela 13

Page 14: C. Varela1 Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute

Reference Cell

Cell = rec(λb.λc.λm.

if ( get?(m),

seq( send(cust(m), c),

ready(b(c)))

if ( set?(m),

ready(b(contents(m))),

ready(b(c)))))

Using the cell:Let a = new(cell(0)) in seq( send(a, mkset(7)),

send(a, mkset(2)),

send(a, mkget(c)))

C. Varela 14

Page 15: C. Varela1 Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute

Exercises

1. Write get?custset?contentsmksetmkget

to complete the reference cell example in the AMST actor language.

2. Modify Bcell to notify a customer when the cell value is updated (such as in the П-calculus cell example).

C. Varela 15

Page 16: C. Varela1 Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute

Dining Philosophers

phil = rec(λb.λl.λr.λself.λsticks.λm.

if (eq?(sticks, 0),

ready(b(l,r,self,1)),

seq( send(l,mkrelease(self)),

send(r,mkrelease(self)),

send(l,mkpickup(self)),

send(r,mkpickup(self)),

ready(b(l,r,self,0)))))

C. Varela 16

Page 17: C. Varela1 Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute

Dining Philosophers

chopstick = rec(λb.λh.λw.λm.

if(pickup?(m),

if(eq?(h,nil),

seq( send(getphil(m), nil),

ready(b(getphil(m),nil))),

ready(b(h,getphil(m))),

if(release?(m),

if(eq?(w,nil),

ready(b(nil, nil)),

seq( send(w,nil),

ready(b(w,nil)))),

ready(b(h,w)))))

C. Varela 17

Page 18: C. Varela1 Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute

Dining Philosophers

letrec c1 = new(chopstick(nil,nil)),

c2 = new(chopstick(nil,nil)),

p1 = new(phil(c1,c2,p1,0)),

p2 = new(phil(c2,c1,p2,0))

in seq( send(c1,mkpickup(p1)),

send(c2,mkpickup(p1)),

send(c1,mkpickup(p2)),

send(c2,mkpickup(p2)))

C. Varela 18

Page 19: C. Varela1 Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute

Dining Philosophers

Auxiliary definitions:

mkpickup = λp.p

mkrelease = nil

pickup? = λm.not(eq?(m,nil))

release? = λm.eq?(m,nil)

getphil = λm.m

C. Varela 19

Page 20: C. Varela1 Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute

Actor Garbage Collection

C. Varela 20

2

8

34

5

7

9

11

10

12

6

1

Blocked (idle)

Unblocked (non-idle)

Root

Page 21: C. Varela1 Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute

Join Continuations

Consider:

treeprod = rec(λf.λtree.

if(isnat(tree),

tree,

f(left(tree))*f(right(tree))

))

Which multiplies all leaves of a tree, which are numbers.

You can do the “left” and “right” computations concurrently.

C. Varela 21

Page 22: C. Varela1 Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute

Tree Product Behavior

Btreeprod =

rec(λb.λself.λm.

seq(become(b(self)),

if(isnat(tree(m)),

send(cust(m),tree(m)),

letactor{newcust := Bjoincont

(cust(m),0,nil)}

seq(send(self,

pr(left(tree(m)),newcust)),

send(self,

pr(right(tree(m)),newcust)))

)

)

)C. Varela 22

Page 23: C. Varela1 Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute

Tree Product (continued)

Bjoincont =

rec(λb.λcust.λnargs.λfirstnum.λnum.

if(eq(nargs,0),

become(b(cust,1,num)),

seq(become(sink),

send(cust,firstnum*num))))

C. Varela 23

Page 24: C. Varela1 Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute

Sample Execution

C. Varela 24

Cust

f(tree,cust)

JC JC

Cust Cust JC

(a) (b)

Page 25: C. Varela1 Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute

Sample Execution

C. Varela 25

Cust

JC’ JC’

JC JC

CustJC

firstnum

(c)

JC'

JCJC

firstnum

firstnum

JC'

Cust Cust

firstnum

JC

(d)

Page 26: C. Varela1 Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute

Sample Execution

C. Varela 26

num

Cust

firstnum

Cust

JC

(e)

firstnum * num

Cust

(f)

Page 27: C. Varela1 Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute

Operational Semantics for Actor Configurations

k = << α | µ >>

α is a function mapping actor names (represented as variables) to actor states.

µ is a multi-set of messages “en-route.”

ρ is a set of receptionists.

χ is a set of external actors

C. Varela 27

ρχ

Page 28: C. Varela1 Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute

Operational Semantics

Given A = Dom(α):

P is a subset of A, A ∩ χ is empty.

• If α(a) = (?a’), then a’ is in A.

• If a in A, then fv(α(a)) is a subset of A U χ.

• If <v0 in v1> in µ, then fv(v0,v1) is a subset of A U χ.

C. Varela 28

Page 29: C. Varela1 Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute

Operational Semantics

α in X As

As = (?x) U (V) U [E]

µ in Mw [M]

M = <V <= V>

ρ,χ in Pw[X] <a <= 5>

V = At U X U λX.E U pr(V,V)

E = V U app(E,E) U Fn(En)

C. Varela 29

Page 30: C. Varela1 Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute

Labeled Transition Relation

<fun: a>

e e'

<<α, [e]a | µ>> << α, [e'] | µ>>

<newactor: a, a’>

<<α, [R[newactor(e)]]a | µ>> <<α, [R[a']]a, [e]a' | µ>>

<send: a, v0, v1>

<<α, [R[send(v0, v1)]]a | µ>> << α, [R[nil]]a | µ<v0 <= v1> >>

C. Varela 30

Dom(α)U{a}ρχ

ρχ

ρχ

ρχ

(where a’ is fresh)

ρχ

ρχ

Page 31: C. Varela1 Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute

Labeled Transition Relation

<receive: v0, v1>

<<α, [ready(v)]v0| <v0 <= v1>, µ>> <<α, [app(v,v1)]v0 | µ>>

<out: v0, v1>

<<α | µ, <v0 <= v1> >> <<α | µ>>

<in: v0, v1>

<< α | µ>> << α | µ, <v0 <= v1> >>

C. Varela 31

ρχ

ρχ

ρχ

ρ'χ

If v0 is in χ and ρ'= ρ U (fv(v1) ∩ Dom(α))

ρχ

ρχ'

If v0 is in ρ and fv(v1) ∩ Dom(α) is a subset of ρ and χ' =χ U (fv(v1) – Dom(α)

Page 32: C. Varela1 Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute

Computation Sequences and Paths

• If k is a configuration, then the computation tree T(k) is the set of all finite sequences of labeled transitions

[ki ki+1 | i < n ]

for some n in the natural numbers with k = k0. Such sequences are called computation sequences.

• A computation path from k is a maximal linearly ordered set of computation sequences in the computation tree,T(k).

• T∞(k) denotes the set of all paths from k.

C. Varela 32

li

Page 33: C. Varela1 Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute

A path π = [ki ki+1 | i < ] in the computation tree T∞(k) is fair if each enabled transition eventually happens or becomes permanently disabled.

For a configuration k we define F(k) to be the subset of T∞(k) that are fair.

Fairness

C. Varela 33

li

Page 34: C. Varela1 Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute

Composition of Actor Configurations

k0 = <<α0 | µ0>>

k1 = <<α1 | µ1>>

k0 || k1 = <<α0 U α1 | µ0 U µ1>>

Configurations are “composable” if

Dom(α0) ∩ Dom(α1) is empty and

χ0 ∩ Dom(α1) is a subset of ρ1

χ1 ∩ Dom(α0) is a subset of ρ0

k0 = << 0 | 0>>

C. Varela 34

ρ0

χ0ρ1

χ1

ρ0 U ρ1

(χ0 U χ1) – (ρ0 U ρ1)

00

Page 35: C. Varela1 Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute

Composition of Actor Configurations

(AC)

k0 || k1 = k1 || k0

k0 || k = k0

(k0 || k1) || k2 = k0 || (k1 || k2)

Closed Configurations: A configuration in which both the receptionist and external actor sets are empty is said to be closed

Composition preserves computations paths:

T(k0 || k1) = T(k0) || T(k1)

F(k0|| k1) = F(k0) || F(k1)

C. Varela 35

0

Page 36: C. Varela1 Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute

Equivalence of Expressions

Operational equivalence

Testing equivalence

Two program expressions are said to be equivalent if they behave the same when placed in any observing context.

An observing context is a complete program with a hole, such that all free variables in expressions being evaluated become captured when placed in the hole.

C. Varela 36

Observational equivalence

Page 37: C. Varela1 Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute

Events and Observing Contexts

A new event primitive operator is introduced.

The reduction relation is extended:

<e: a>

<<α, [R[event()]]a | µ>> <<α, [R[nil]]a | µ>>

An observing configuration is on of the form:

<< α, [C]a | µ>>

where C is a hole-containing expression or context.

C. Varela 37

ρχ

ρχ

Page 38: C. Varela1 Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute

Observations

Let k be a configuration of the extended language and let π = [ki ki+1 | i < ] be a fair path, i.e., π is in F(k).

Define:

obs(π) =

obs(k) =

C. Varela 38

li

S if there exists a, i < such that li = <e: a>F otherwise

S if, for all π in F(k), obs(π) = s SF otherwiseF if, for all π in F(k), obs(π) = s

Page 39: C. Varela1 Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute

Three Equivalences

The natural equivalence is equal are made in all closing configuration contexts.

Other two equivalences (weaker) arise if SF observations are considered as good as S observations; or if SF observations are considered as bad as F observations.

1. Testing or Convex or Plotkin or Egli-Milner

e0 =1 e1 iff Obs(O[e0]) = Obs(O[e1]).

1. Must or Upper or Smythe0 =2 e1 iff Obs(O[e0]) = S Obs(O[e1]) = S.

2. May or Lower or Hoaree0 =3 e1 iff Obs(O[e0]) = F Obs(O[e1]) = F.

C. Varela 39

~

~

~

Page 40: C. Varela1 Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute

Congruence

e0 =j e1 c[e0] =j c[e1] for j = 1,2,3,…

By construction, all equivalences defined are congruencies.

Partial Collapse

C. Varela 40

~ ~

=1 S SF FS

SF

F

X X

XX

X X

=2 S SF FS

SF

F

=3 S SF FS

SF

F

(1 = 2): e0 =1 e1 iff e0 =2 e1 (due to fairness)(1 => 3): e0 =1 e1 implies e0 =3 e1

~~~ ~

X X

X

X X X

X

X

*

*

e0

e1