Simulation, Orchestration and Logical Clocks · Real time and Virtual time Logical timers Coding...

Post on 16-Jul-2020

9 views 0 download

Transcript of Simulation, Orchestration and Logical Clocks · Real time and Virtual time Logical timers Coding...

Real time and Virtual time Logical timers Coding simulations

Simulation, Orchestration and Logical Clocks

David Kitchin, Evan Powell, Jayadev Misra

Department of Computer ScienceUniversity of Texas at Austin

WG2.3, Cambridge, 2008

Real time and Virtual time Logical timers Coding simulations

Outline

Real time and Virtual timeDesigning Time-based AlgorithmsOverview of OrcExamples

Logical timersA motivating Example: Shortest pathUsing Logical timers

Coding simulationsSimulation: BankSimulation: Fast Food RestaurantCollecting Statistics

Real time and Virtual time Logical timers Coding simulations

Designing Time-based Algorithms

... the strong assumption of global time is first assumed, andthenrelaxed to the weaker logical time –could help us to design newalgorithms

Global And Logical Time In Distributed AlgorithmsCarroll Morgan, 1985

Real time and Virtual time Logical timers Coding simulations

Designing Time-based Algorithms

• Morgan’s algorithms use time only for comparison.

• The clock value is never used.

• Simple conversion from real time to logical time.

We propose algorithms that use the clock value.

Real time and Virtual time Logical timers Coding simulations

Overview of Orc

• Orc program has• aGoalexpression,• a set of definitions.

• A Program execution evaluates the goal. It

• callssites, to invoke services,• publishesvalues.

• Orc is simple• Language has only 3 combinators to form expressions.• Can handle time-outs, priorities, failures, synchronizations, · · ·• Combinators are (monotonic and) continuous.

Real time and Virtual time Logical timers Coding simulations

Structure of Orc Expression

• Simple: just a site call,CNN(d)Publishes the value returned by the site.

• compositionof two Orc expressions:

do f andg in parallel f | g Symmetric compositionfor all x from f do g f >x> g Sequential compositionfor somex from g do f f <x< g Asymmetric composition

Real time and Virtual time Logical timers Coding simulations

Structure of Orc Expression

• Simple: just a site call,CNN(d)Publishes the value returned by the site.

• compositionof two Orc expressions:

do f andg in parallel f | g Symmetric compositionfor all x from f do g f >x> g Sequential compositionfor somex from g do f f <x< g Asymmetric composition

Real time and Virtual time Logical timers Coding simulations

Structure of Orc Expression

• Simple: just a site call,CNN(d)Publishes the value returned by the site.

• compositionof two Orc expressions:

do f andg in parallel f | g Symmetric compositionfor all x from f do g f >x> g Sequential compositionfor somex from g do f f <x< g Asymmetric composition

Real time and Virtual time Logical timers Coding simulations

Structure of Orc Expression

• Simple: just a site call,CNN(d)Publishes the value returned by the site.

• compositionof two Orc expressions:

do f andg in parallel f | g Symmetric compositionfor all x from f do g f >x> g Sequential compositionfor somex from g do f f <x< g Asymmetric composition

Real time and Virtual time Logical timers Coding simulations

Symmetric composition:f | g

• Evaluate f and g independently.

• Publish all values from both.

• No direct communication or interaction betweenf and g.They may communicate only through sites.

CNN(d) | BBC(d): callsboth CNN and BBC simultaneously.Publishes values returned by both sites. (0, 1 or 2 values)

Real time and Virtual time Logical timers Coding simulations

Sequential composition:f >x> g

For all values published byf do g.Publish only the values fromg.

• CNN(d) >x> Email(address, x)

• Call CNN(d).• Bind result (if any) tox.• Call Email(address, x).• Publish the value, if any, returned byEmail.

• (CNN(d) | BBC(d)) >x> Email(address, x)

• May call Email twice.• Publishes up to two values fromEmail.

Real time and Virtual time Logical timers Coding simulations

Schematic of Sequential composition

f

g1g0 g2

Figure:Schematic off >x> g

Real time and Virtual time Logical timers Coding simulations

Notation

f ≫ g for f >x> g,if x unused ing.

Precedence:f >x> g | h >y> u for(f >x> g) | (h >y> u)

Real time and Virtual time Logical timers Coding simulations

Asymmetric composition:(f <x< g)

For some value published byg do f .Publish only the values fromf .

• Evaluate f and g in parallel.

• Site calls that needx are suspended.• Other site calls proceed.• see (M() | N(x)) <x< g

• When g returns a value:

• Assign it to x.• Terminateg.• Resume suspended calls.

• Values published byf are the values of(f <x< g).

Real time and Virtual time Logical timers Coding simulations

Example of Asymmetric composition

Email(address, x) <x< (CNN(d) | BBC(d))

Binds x to the first value fromCNN(d) | BBC(d).Sends at most one email.

Real time and Virtual time Logical timers Coding simulations

Some Fundamental Sites

• if (b): booleanb,returns asignalif b is true; remainssilent if b is false.

• Null: never responds. Same asif (false).

• Signal() returns a signal immediately. Same asif (true).

• Rtimer(t): integer t, t ≥ 0, returns a signalt time units later.

Real time and Virtual time Logical timers Coding simulations

Centralized Execution Model

• An expression is evaluated on a single machine (client).

• Client communicates with sites by messages.

• All fundamental sites are local to the client.All except Rtimer respond immediately.

• Round-based Execution.

Real time and Virtual time Logical timers Coding simulations

Expression Definition

MailOnce(a) ∆Email(a, m) <m< (CNN(d) | BBC(d))

MailLoop(a, d) ∆MailOnce(a) ≫ Rtimer(d) ≫ MailLoop(a, d)

• Expression is called like a procedure.It may publish many values.MailLoop does not publish.

• Site calls are strict; expression calls non-strict.

Real time and Virtual time Logical timers Coding simulations

Metronome

Publish a signal at every time unit.

Metronome() ∆ Signal() | (Rtimer(1) ≫ Metronome())

S R

S R

Real time and Virtual time Logical timers Coding simulations

Example of Expression call

• Site Query() returns a value (different ones at different times).

• Site Accept(x) returns x if x is acceptable;it is silent otherwise.

• Publish all acceptable values by callingQuery at unit intervalsforever.

Metronome() ≫ Query() >x> Accept(x)

Real time and Virtual time Logical timers Coding simulations

Time-out

Publish M’s response if it arrives before timet,Otherwise, publish0.

z <z< (M() | (Rtimer(t) ≫ 0))

Real time and Virtual time Logical timers Coding simulations

Fork-join parallelism

Call M and N in parallel.Return their values as a tuple after both respond.

((u, v)<u< M())

<v< N()

Notational Convention: <u< is left-associative.

(u, v) <u< M() <v< N(), or(u, v)

<u< M()<v< N()

Real time and Virtual time Logical timers Coding simulations

Recursive definition with time-out

Call a list of sites.Count the number of responses received within 10 time units.

tally([ ]) ∆ 0tally(M : MS) ∆ u + v

<u< (M() ≫ 1) | (Rtimer(10) ≫ 0)<v< tally(MS)

Real time and Virtual time Logical timers Coding simulations

Barrier Synchronization inM ≫ f | N ≫ g

f and g start only afterboth M and N complete.Rendezvous of CSP or CCS;M and N are complementary actions.

((u, v)<u< M()<v< N())

≫ (f | g)

Real time and Virtual time Logical timers Coding simulations

Priority

• Publish N’s response asap, but no earlier than 1 unit from now.Apply fork-join betweenRtimer(1) and N.

Delay() ∆ (Rtimer(1) ≫ u) <u< N()

• Call M, N together.If M responds within one unit, publish its response.Else, publish the first response.

x <x< (M() | Delay())

Real time and Virtual time Logical timers Coding simulations

Interrupt f

Evaluation of f can not be directly interrupted.Introduce two sites:

• Interrupt.set: to interrupt f

• Interrupt.get: responds afterInterrupt.set has been called.

Instead of f , evaluate

z <z< (f | Interrupt.get())

Real time and Virtual time Logical timers Coding simulations

Parallel or

Sites M and N return booleans. Compute theirparallel or.

if (x) ≫ true | if (y) ≫ true | or(x, y)<x< M()<y< N()

To return just one value:

z<z< if (x) ≫ true | if (y) ≫ true | or(x, y)<x< M()<y< N()

Real time and Virtual time Logical timers Coding simulations

Shortest path problem

• Directed graph; non-negative weights on edges.

• Find shortest path from source to sink.

We calculate just the length of the shortest path.

Real time and Virtual time Logical timers Coding simulations

Algorithm with Lights and Mirrors

• Source node sends rays of light to each neighbor.

• Edge weight is the time for the ray to traverse the edge.

• When a node receives its first ray, sends rays to all neighbors.Ignores subsequent rays.

• Shortest path length= time for sink to receive its first ray.

Real time and Virtual time Logical timers Coding simulations

Expressions and Sites needed for Shortest path

succ(u): Publish all(v, d), where edge(u, v) has weightd.

write(u, t): Write value t for nodeu. If already written, block.

read(u): Return value for nodeu. If unwritten, block.

Real time and Virtual time Logical timers Coding simulations

First Algorithm

eval1(u, t) ∆ write(u, t) ≫

Succ(u) >(v, d)>Rtimer(d) ≫

eval1(v, t + d)

Goal : eval1(source, 0) | read(sink)

First call to eval1(u, t):

• The relative time in the evaluation ist.

• Length of the shortest path from source tou is t.

• eval1 does not publish.

Real time and Virtual time Logical timers Coding simulations

Second Algorithm

Use timer rt. Initial value 0.Retrieve its value by callingrt.C().

eval2(u, rt) ∆ rt.C() >t> write(u, t) ≫

Succ(u) >(v, d)>Rtimer(d) ≫

eval2(v, rt)

Goal : RealTimer >rt>(eval2(source, rt) | read(sink))

Real time and Virtual time Logical timers Coding simulations

Third Algorithm

• Simplymarkintermediate nodes. No values are written.

• Marking showsu has been reached. Subsequent markings block.

• scanresponds only ifu has been marked. Else blocks.

eval3(u) ∆ mark(u) ≫

Succ(u) >(v, d)>Rtimer(d) ≫

eval3(v)

Goal : RealTimer >rt>(eval3(source) | scan(sink) ≫ rt.C())

Real time and Virtual time Logical timers Coding simulations

Drawbacks of these algorithms

• Running time proportional to shortest path length.

• Executions ofSucc, read and write should take no time.

Real time and Virtual time Logical timers Coding simulations

Properties of Real-time Timers

1. (Monotonicity) Successive calls tort.C() return non-decreasingvalues.

2. (Relativity)

{rt.C()= n} rt.R(t) {rt.C()= n + t}

provided rt.R(t) responds. (rt.R(t)=Rtimer(t))

Real time and Virtual time Logical timers Coding simulations

Logical/Virtual Timer

Logical Timer lt: Methods lt.C() and lt.R(t).

• Many logical timers may coexist independently.

• Monotonicity holds for all: Successive calls tolt.C() returnnon-decreasing values.

• Relativity holds for all:

{lt.C()= n} lt.R(t) {lt.C()= n + t}

provided lt.R(t) responds.

• Difference from Real-time Timer:Sites other thanlt.R(.) take 0 logical time.

Real time and Virtual time Logical timers Coding simulations

Shortest Path Algorithm using logical timer

eval4(u, lt) ∆ mark(u) ≫

Succ(u) >(v, d)>lt.R(d) ≫

eval4(v, lt)

Goal : VirtTimer() >lt>(eval4(source, lt) | scan(sink) ≫ lt.C())

lt has to be passed as a parameter, to performlt.R(d).For real-time timerrt, rt.R(d) = Rtimer(d).

Real time and Virtual time Logical timers Coding simulations

Implementing logical timer

lt is a logical timer.n is the timer value. Initially0.

• lt.C() responds withn.

• lt.R(t) is assigned rankn + t and queued.

• At the time of removal, the removed item has the lowest rank,r.n := r.

Real time and Virtual time Logical timers Coding simulations

Implementing logical timer; contd.

• Monotonicity and Relativity hold.

• Progress: If there is no other outstanding site call, some item isremoved from a non-empty queue.

• Generalize to multiple logical timers: They are causallyunrelated.

Real time and Virtual time Logical timers Coding simulations

Simulation: Bank

• Bank with two tellers and one queue for customers.

• Customers generated by asource process.

• When free, a teller serves the first customer in the queue.

• Service times vary for customers.

• Determine

• Average wait time for a customer.• Queue length distribution.• Average idle time for a teller.

Real time and Virtual time Logical timers Coding simulations

Goal Expression for Bank Simulation

Run the simulation forsimtime.

VirtTimer() >lt>

(z <z< Bank(lt) | lt.R(simtime)) ≫

Stats()

Real time and Virtual time Logical timers Coding simulations

Description of Bank

Bank(lt) ∆ (Customers() | Teller(lt) | Teller(lt)) ≫ NullCustomers() ∆ Source() >c> enter(c)

Teller(lt) ∆ next() >c>lt.R(c.ServTime) ≫

Teller(lt)enter(c) ∆ q.put(c)

next() ∆ q.get()

Real time and Virtual time Logical timers Coding simulations

Fast Food Restaurant

• Restaurant with one cashier, two cooking stations and one queuefor customers.

• Customers generated by asource process.

• When free, cashier serves the first customer in the queue.

• Cashier service times vary for customers.

• Cashier places the order in another queue for the cookingstations.

• Each order has 3 parts: main entree, side dish, drink

• A cooking station processes parts of an order in parallel.

Real time and Virtual time Logical timers Coding simulations

Goal Expression for Restaurant Simulation

VirtTimer() >lt>

(z <z< Restaurant(lt) | lt.R(simtime)) ≫

Stats()

Real time and Virtual time Logical timers Coding simulations

Description of Restaurant

Restaurant(lt) ∆ (Customers() | Cashier(lt) | Cook(lt) | Cook(lt)) ≫ NullCustomers() ∆ Source() >c> enter(c)Cashier(lt) ∆ next() >c>

lt.R(c.ringupTime) ≫

orders.put(c.order) ≫

Cashier(lt)Cook(lt) ∆ orders.get() >order>

((e, s, d)

<e< prepTime(order.entree) >t> lt.R(t)<s< prepTime(order.side) >t> lt.R(t)<d< prepTime(order.drink) >t> lt.R(t)

) ≫

Cook(lt)enter(c) ∆ q.put(c)

next() ∆ q.get()

Real time and Virtual time Logical timers Coding simulations

Collecting Statistics: waiting time

Change

enter(c) ∆ q.put(c)next() ∆ q.get()

to

enter(c) ∆ lt.C >s> q.put(c, s)next() ∆ q.get() >(c, t)>

lt.C >s>reportWait(s − t) ≫

c

Real time and Virtual time Logical timers Coding simulations

Stopwatch

A stopwatchis aligned with some timer, real or virtual.Supports 4 methods:

• reset

• read

• start

• stop

Real time and Virtual time Logical timers Coding simulations

Histogram: Queue length

• CreateN + 1 stopwatches,sw[0..N], at the beginning ofsimulation.

• Final value of sw[i], 0 ≤ i < N, is the duration for which thequeue length has beeni.

• sw[N] is the duration for which the queue length is at leastN.

• On adding an item to queue of lengthi, 0 ≤ i < N, do

sw[i].stop | sw[i + 1].start

• After removing an item if the queue length isi, 0 ≤ i < N, do

sw[i].start | sw[i + 1].stop