400 likes | 495 Views
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. Actor Configurations.
E N D
Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute C. Varela
Open Distributed Systems • Addition of new components • Replacement of existing components • Changes in interconnections C. Varela
Actor Configurations • Model open system components: • Set of indivitually named actors • Messages “en-route” • Interface to environment: • Receptionists • External actors C. Varela
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
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
Fairness • The actor model theory assumes fair computations: • Message delivery is guaranteed. • 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
Programming Languages Influenced by П-Calculus and Actors • Scheme ‘75 • Act1 ‘87 • Acore ‘87 • Rosette ‘89 • Obliq ‘94 • Erlang ‘93 • ABCL ‘90 • SALSA ‘99 • Amber ’86 • Facile ’89 • CML ’91 • Pict ’94 • Nomadic Pict ’99 • JOCAML ‘99 C. Varela
Actor (Agent) Model C. Varela
Agha, Mason, Smith & Talcott • Extend a functional language (λ-calculus + ifs and pairs) with actor primitives. • Define an operational semantics for actor configurations. • Study various notions of equivalence of actor expressions and configurations. • Assume fairness: • Guaranteed message delivery. • Individual actor progress. C. Varela
λ-Calculus 2 λx.x2 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) 22 C. Varela
λ-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
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
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
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
Exercises • Write get? cust set? contents mkset mkgetto complete the reference cell example in the AMST actor language. • Modify Bcell to notify a customer when the cell value is updated (such as in the П-calculus cell example). C. Varela
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
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
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
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
Actor Garbage Collection Blocked (idle) 9 10 Unblocked (non-idle) 11 12 1 Root 3 2 4 5 8 6 7 C. Varela
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
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
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
Sample Execution f(tree,cust) JC JC Cust Cust Cust JC (a) (b) C. Varela
Sample Execution JC’ JC' JC’ firstnum JC JC' JC JC JC firstnum JC JC Cust Cust Cust Cust firstnum firstnum (c) (d) C. Varela
Sample Execution num JC firstnum * num Cust firstnum Cust Cust (e) (f) C. Varela
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
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
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
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> >> Dom(α)U{a} ρ χ ρ χ ρ χ ρ χ (where a’ is fresh) ρ χ ρ χ C. Varela
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> >> ρ χ ρ χ ρ χ ρ' χ 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(α) C. Varela
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. li C. Varela
Fairness 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. li C. Varela
Composition of Actor Configurations ρ0 χ0 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>> ρ1 χ1 ρ0 U ρ1 (χ0 Uχ1) – (ρ0 U ρ1) 0 0 C. Varela
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) 0 C. Varela
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. Observational equivalence C. Varela
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
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) = li S if there exists a, i < such that li = <e: a> F otherwise S if, for all π in F(k),obs(π) = s SF otherwise F if, for all π in F(k),obs(π) = s C. Varela
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. • Testing or Convex or Plotkin or Egli-Milner e0 =1 e1 iff Obs(O[e0]) = Obs(O[e1]). • Must or Upper or Smythe0 =2 e1 iff Obs(O[e0]) = S Obs(O[e1]) = S. • May or Lower or Hoaree0 =3 e1 iff Obs(O[e0]) = F Obs(O[e1]) = F. ~ ~ ~ C. Varela
Congruence ~ ~ e0 =j e1 c[e0] =j c[e1] for j = 1,2,3,… By construction, all equivalences defined are congruencies. Partial Collapse e1 e0 =1 S SF F S SF F =2 S SF F S SF F =3 S SF F S SF F X X X X X X X X * X X X X * X X ~ ~ (1 = 2): e0 =1 e1 iff e0 =2 e1(due to fairness) (1 => 3): e0 =1 e1 implies e0 =3 e1 ~ ~ C. Varela