Chris9an Kstner Charlie Garrod School of Computer - - PowerPoint PPT Presentation

chris9an k stner charlie garrod
SMART_READER_LITE
LIVE PREVIEW

Chris9an Kstner Charlie Garrod School of Computer - - PowerPoint PPT Presentation

Principles of So3ware Construc9on: Objects, Design, and Concurrency Part 6: Concurrency and distributed systems Abstrac5ons of State Chris9an Kstner


slide-1
SLIDE 1

1

15-­‑214

School ¡of ¡ ¡ Computer ¡Science ¡

Principles ¡of ¡So3ware ¡Construc9on: ¡Objects, ¡Design, ¡and ¡ Concurrency ¡ Part ¡6: ¡Concurrency ¡and ¡distributed ¡systems ¡ ¡ Abstrac5ons ¡of ¡State ¡ ¡

Chris9an ¡Kästner ¡Charlie ¡Garrod ¡

slide-2
SLIDE 2

2

15-­‑214

Administrivia ¡

  • Homework ¡6… ¡
  • Final ¡exam ¡Tuesday, ¡May ¡5th, ¡1 ¡– ¡4 ¡p.m. ¡ ¡DH ¡2210 ¡

– Final ¡exam ¡review ¡session ¡Sunday, ¡May ¡3rd, ¡4 ¡– ¡6:30 ¡p.m., ¡Hamburg ¡1000 ¡

slide-3
SLIDE 3

3

15-­‑214

Key ¡concepts ¡from ¡Tuesday ¡

slide-4
SLIDE 4

4

15-­‑214

Data ¡consistency ¡

  • Suppose ¡D ¡is ¡the ¡database ¡for ¡some ¡applica9on ¡and ¡ϕ is ¡a ¡

func9on ¡from ¡database ¡states ¡to ¡{true, ¡false} ¡

– We ¡call ¡ϕ ¡an ¡integrity ¡constraint ¡for ¡the ¡applica9on ¡if ¡ϕ(D) ¡is ¡true ¡if ¡the ¡ state ¡D ¡is ¡"good" ¡ – We ¡say ¡a ¡database ¡state ¡D ¡is ¡consistent ¡if ¡ϕ(D) ¡is ¡true ¡for ¡all ¡integrity ¡ constraints ¡ϕ ¡ – We ¡say ¡D ¡is ¡inconsistent ¡if ¡ϕ(D) ¡is ¡false ¡for ¡any ¡integrity ¡constraint ¡ϕ ¡

  • Transac9on ¡ACID ¡proper9es: ¡

– Atomicity: ¡ ¡ ¡All ¡or ¡nothing ¡ – Consistency: ¡ ¡ ¡Applica9on-­‑dependent ¡as ¡before ¡ – Isola9on: ¡ ¡ ¡ ¡Each ¡transac9on ¡runs ¡as ¡if ¡alone ¡ – Durability: ¡ ¡ ¡Database ¡will ¡not ¡abort ¡or ¡undo ¡work ¡of ¡ ¡ ¡ ¡a ¡transac9on ¡a3er ¡it ¡confirms ¡the ¡commit ¡

slide-5
SLIDE 5

5

15-­‑214

Concurrent ¡transac9ons ¡and ¡serializability ¡

  • For ¡good ¡performance, ¡database ¡interleaves ¡opera9ons ¡of ¡

concurrent ¡transac9ons ¡

  • Problems ¡to ¡avoid: ¡

– Lost ¡updates ¡

  • Another ¡transac9on ¡overwrites ¡your ¡update, ¡based ¡on ¡old ¡data ¡

– Inconsistent ¡retrievals ¡

  • Reading ¡par9al ¡writes ¡by ¡another ¡transac9on ¡
  • Reading ¡writes ¡by ¡another ¡transac9on ¡that ¡subsequently ¡aborts ¡
  • A ¡schedule ¡of ¡transac9on ¡opera9ons ¡is ¡serializable ¡if ¡it ¡is ¡

equivalent ¡to ¡some ¡serial ¡ordering ¡of ¡the ¡transac9ons ¡

slide-6
SLIDE 6

6

15-­‑214

2PC ¡sequence ¡of ¡events ¡for ¡a ¡successful ¡commit ¡

canCommit? yes doCommit confirmed Coordinator: Participants: “committed” (persistently) “prepared” “done” “uncertain” (objects still locked) “prepared” (persistently) “committed”

slide-7
SLIDE 7

7

15-­‑214

Problems ¡with ¡two-­‑phase ¡commit? ¡

slide-8
SLIDE 8

8

15-­‑214

Problems ¡with ¡two-­‑phase ¡commit? ¡

  • Failure ¡assump9ons ¡are ¡too ¡strong ¡

– Real ¡servers ¡can ¡fail ¡permanently ¡ – Persistent ¡storage ¡can ¡fail ¡permanently ¡

  • Temporary ¡failures ¡can ¡arbitrarily ¡delay ¡a ¡commit ¡
  • Poor ¡performance ¡

– Many ¡round-­‑trip ¡messages ¡

slide-9
SLIDE 9

9

15-­‑214

Aside: ¡ ¡The ¡CAP ¡theorem ¡for ¡distributed ¡systems ¡

  • For ¡any ¡distributed ¡system ¡you ¡want… ¡

– Consistency ¡ – Availability ¡ – tolerance ¡of ¡network ¡Par99ons ¡

  • …but ¡you ¡can ¡support ¡at ¡most ¡two ¡of ¡the ¡three ¡
slide-10
SLIDE 10

10

15-­‑214

Today: ¡ ¡Abstrac9ons ¡of ¡state ¡

  • State-­‑based ¡models ¡of ¡computa9on ¡

– Finite ¡state ¡machines ¡(FSMs) ¡

  • The ¡State ¡design ¡pacern ¡
  • A ¡distributed ¡applica9on: ¡ ¡The ¡actor ¡model ¡
slide-11
SLIDE 11

11

15-­‑214

An ¡aside: ¡ ¡I ¡need ¡two ¡volunteers… ¡

slide-12
SLIDE 12

12

15-­‑214

Memorize ¡the ¡following ¡number: ¡

4 2

slide-13
SLIDE 13

13

15-­‑214

What ¡was ¡the ¡number? ¡

slide-14
SLIDE 14

14

15-­‑214

Memorize ¡the ¡following ¡number: ¡

4 2 9 7

slide-15
SLIDE 15

15

15-­‑214

What ¡was ¡the ¡number? ¡

slide-16
SLIDE 16

16

15-­‑214

Memorize ¡the ¡following ¡number: ¡

4 2 9 7 2 8

slide-17
SLIDE 17

17

15-­‑214

What ¡was ¡the ¡number? ¡

slide-18
SLIDE 18

18

15-­‑214

Memorize ¡the ¡following ¡number: ¡

4 2 9 7 2 8 6 1 9 3 9 1 0 2 8 4 0 0 2 8 8 2 1 0 8 2 7 3 2 3 3 3 2 8 6 6 7 1 0 0 8 0 9 1 0 8 2 8 6 4 2 8 5 6 0 9 1 7 2 8 2 7 8 1 6 8 7 2 0 9

slide-19
SLIDE 19

19

15-­‑214

slide-20
SLIDE 20

20

15-­‑214

Memorize ¡the ¡following ¡number: ¡

4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2

slide-21
SLIDE 21

21

15-­‑214

An ¡aside's ¡aside: ¡ ¡Run-­‑length ¡encodings ¡

4 2 (5*7 times)

slide-22
SLIDE 22

22

15-­‑214

What ¡causes ¡programming ¡errors? ¡

slide-23
SLIDE 23

23

15-­‑214

What ¡causes ¡programming ¡errors? ¡

  • Knowledge ¡problems: ¡ ¡Inadequate, ¡inert, ¡

heuris9c, ¡oversimplified, ¡or ¡interfering ¡ content ¡or ¡organiza9on ¡

  • Acen9onal ¡problems: ¡ ¡Fixa9on, ¡loss ¡of ¡

situa9onal ¡awareness, ¡or ¡working ¡memory ¡ strain ¡

  • Strategic ¡problems: ¡ ¡Unforeseen ¡interac9ons ¡

from ¡goal ¡conflict ¡resolu9on ¡or ¡bounded ¡ ra9onality ¡

Recommended: ¡ ¡A. ¡Ko ¡and ¡B. ¡Myers, ¡"Development ¡and ¡ Evalua7on ¡of ¡a ¡Model ¡of ¡Programming ¡Errors". ¡ ¡HCC ¡2003. ¡ ¡

slide-24
SLIDE 24

24

15-­‑214

What ¡causes ¡programming ¡errors? ¡

  • Knowledge ¡problems: ¡ ¡Inadequate, ¡inert, ¡

heuris9c, ¡oversimplified, ¡or ¡interfering ¡ content ¡or ¡organiza9on ¡

  • Acen9onal ¡problems: ¡ ¡Fixa9on, ¡loss ¡of ¡

situa9onal ¡awareness, ¡or ¡working ¡memory ¡ strain ¡

  • Strategic ¡problems: ¡ ¡Unforeseen ¡interac9ons ¡

from ¡goal ¡conflict ¡resolu9on ¡or ¡bounded ¡ ra9onality ¡

Recommended: ¡ ¡A. ¡Ko ¡and ¡B. ¡Myers, ¡"Development ¡and ¡ Evalua7on ¡of ¡a ¡Model ¡of ¡Programming ¡Errors". ¡ ¡HCC ¡2003. ¡ ¡

A goal: Eliminate complexity

slide-25
SLIDE 25

25

15-­‑214

Today: ¡ ¡Abstrac9ons ¡of ¡state ¡

  • State-­‑based ¡models ¡of ¡computa9on ¡

– Finite ¡state ¡machines ¡(FSMs) ¡

  • The ¡State ¡design ¡pacern ¡
  • A ¡distributed ¡applica9on: ¡ ¡The ¡actor ¡model ¡
slide-26
SLIDE 26

26

15-­‑214

Related: ¡ ¡Determinis9c ¡Finite ¡Automata ¡(DFAs) ¡

  • A ¡simple ¡model ¡of ¡computa9on ¡in ¡which ¡input ¡is ¡accepted ¡or ¡

rejected ¡by ¡a ¡finite ¡state ¡machine ¡

– e.g. ¡ ¡A ¡DFA ¡that ¡accepts ¡the ¡input ¡42: ¡

4 2 0-3,5-9 0-9 0,1,3-9 0-9

slide-27
SLIDE 27

27

15-­‑214

Related: ¡ ¡Determinis9c ¡Finite ¡Automata ¡(DFAs) ¡

  • A ¡simple ¡model ¡of ¡computa9on ¡in ¡which ¡input ¡is ¡accepted ¡or ¡

rejected ¡by ¡a ¡finite ¡state ¡machine ¡

– e.g. ¡ ¡A ¡DFA ¡that ¡accepts ¡the ¡input ¡42: ¡

4 2 0-3,5-9 0-9 0,1,3-9 0-9

42

slide-28
SLIDE 28

28

15-­‑214

Related: ¡ ¡Determinis9c ¡Finite ¡Automata ¡(DFAs) ¡

  • A ¡simple ¡model ¡of ¡computa9on ¡in ¡which ¡input ¡is ¡accepted ¡or ¡

rejected ¡by ¡a ¡finite ¡state ¡machine ¡

– e.g. ¡ ¡A ¡DFA ¡that ¡accepts ¡the ¡input ¡42: ¡

4 2 0-3,5-9 0-9 0,1,3-9 0-9

2

slide-29
SLIDE 29

29

15-­‑214

Related: ¡ ¡Determinis9c ¡Finite ¡Automata ¡(DFAs) ¡

  • A ¡simple ¡model ¡of ¡computa9on ¡in ¡which ¡input ¡is ¡accepted ¡or ¡

rejected ¡by ¡a ¡finite ¡state ¡machine ¡

– e.g. ¡ ¡A ¡DFA ¡that ¡accepts ¡the ¡input ¡42: ¡

4 2 0-3,5-9 0-9 0,1,3-9 0-9

ε

slide-30
SLIDE 30

30

15-­‑214

Related: ¡ ¡Turing ¡Machines ¡

  • A ¡simple ¡model ¡of ¡computa9on ¡in ¡which ¡input ¡is ¡accepted ¡or ¡

rejected ¡by ¡a ¡finite ¡state ¡machine ¡

– Essen9ally ¡a ¡DFA ¡with ¡an ¡infinite ¡memory ¡tape ¡

slightly ¡more ¡complex ¡

4 2 0-3,5-9 0-9 0,1,3-9 0-9 … ¡ 0 ¡ 1 ¡ 0 ¡ 1 ¡ 1 ¡ … ¡

slide-31
SLIDE 31

31

15-­‑214

Finite ¡state ¡machines ¡(FSMs) ¡

4 2 0-3,5-9 0-9 0,1,3-9 0-9

"states" "states" "transition function"

slide-32
SLIDE 32

32

15-­‑214

FSMs ¡simply ¡represent ¡system ¡behavior ¡

  • E.g., ¡a ¡4-­‑func9on ¡calculator ¡
slide-33
SLIDE 33

33

15-­‑214

FSMs ¡simply ¡represent ¡system ¡behavior ¡

  • E.g., ¡a ¡4-­‑func9on ¡calculator ¡
  • E.g., ¡the ¡traffic ¡light ¡at ¡Forbes ¡and ¡Morewood ¡
slide-34
SLIDE 34

34

15-­‑214

FSMs ¡enable ¡precise ¡communica9on ¡

  • E.g., ¡the ¡Transmission ¡

Control ¡Protocol ¡(TCP) ¡

slide-35
SLIDE 35

35

15-­‑214

UML ¡state ¡diagrams ¡enable ¡richer ¡communica9on ¡

  • Condi9onal ¡transi9ons ¡
  • Independent ¡events/ac9ons ¡

(example from Wikipedia...)

slide-36
SLIDE 36

36

15-­‑214

FSMs ¡can ¡help ¡organize ¡the ¡implementa9on ¡

  • See ¡StateMachineCalculator.java ¡
slide-37
SLIDE 37

37

15-­‑214

FSMs ¡can ¡help ¡organize ¡the ¡implementa9on ¡

  • See ¡StateMachineCalculator.java ¡

– Warning: ¡ ¡The ¡StateMachineCalculator ¡inten9onally ¡demonstrates ¡poor ¡

  • design. ¡
slide-38
SLIDE 38

38

15-­‑214

A ¡calculator ¡with ¡the ¡State ¡design ¡pacern ¡

  • See ¡StatePacernCalculator.java ¡
slide-39
SLIDE 39

39

15-­‑214

The ¡State ¡design ¡pacern ¡

  • Applicability: ¡

– An ¡object's ¡behavior ¡depends ¡

  • n ¡its ¡state, ¡and ¡it ¡must ¡change ¡

its ¡behavior ¡at ¡run-­‑9me ¡based ¡

  • n ¡that ¡state ¡

– Transi9on ¡func9on ¡between ¡ states ¡is ¡highly ¡state-­‑dependent ¡ and ¡complex ¡

  • Consequences: ¡

– State-­‑specific ¡behavior ¡is ¡ par99oned, ¡localized, ¡ ¡and ¡ cohesive ¡ – State ¡transi9ons ¡are ¡explicit ¡ – State ¡objects ¡can ¡be ¡shared ¡

slide-40
SLIDE 40

40

15-­‑214

Recall ¡a ¡problem ¡of ¡concurrency: ¡ ¡Shared ¡state ¡

R L C

slide-41
SLIDE 41

41

15-­‑214

  • E.g., ¡for ¡each ¡word ¡on ¡the ¡Web, ¡count ¡the ¡number ¡of ¡9mes ¡

that ¡word ¡occurs ¡

§ For ¡Map: ¡ ¡key1 ¡is ¡a ¡document ¡name, ¡value ¡is ¡the ¡contents ¡of ¡that ¡

document ¡

§ For ¡Reduce: ¡ ¡key2 ¡is ¡a ¡word, ¡values ¡is ¡a ¡list ¡of ¡the ¡number ¡of ¡counts ¡

  • f ¡that ¡word ¡

MapReduce's ¡approach ¡to ¡shared ¡state ¡

f1(String key1, String value): for each word w in value: EmitIntermediate(w, 1); f2(String key2, Iterator values): int result = 0; for each v in values: result += v; Emit(key2, result); Map: (key1, v1) à (key2, v2)* Reduce: (key2, v2*) à (key3, v3)* MapReduce: (key1, v1)* à (key3, v3)* MapReduce: (docName, docText)* à (word, wordCount)*

slide-42
SLIDE 42

42

15-­‑214

Transac9onal ¡approach ¡to ¡shared ¡state ¡

  • For ¡good ¡performance, ¡database ¡interleaves ¡opera9ons ¡of ¡

concurrent ¡transac9ons ¡

  • Problems ¡to ¡avoid: ¡

– Lost ¡updates ¡

  • Another ¡transac9on ¡overwrites ¡your ¡update, ¡based ¡on ¡old ¡data ¡

– Inconsistent ¡retrievals ¡

  • Reading ¡par9al ¡writes ¡by ¡another ¡transac9on ¡
  • Reading ¡writes ¡by ¡another ¡transac9on ¡that ¡subsequently ¡aborts ¡
  • A ¡schedule ¡of ¡transac9on ¡opera9ons ¡is ¡serializable ¡if ¡it ¡is ¡

equivalent ¡to ¡some ¡serial ¡ordering ¡of ¡the ¡transac9ons ¡

slide-43
SLIDE 43

43

15-­‑214

Models ¡of ¡concurrency ¡and ¡parallelism ¡

  • Explicit ¡concurrency: ¡ ¡threads ¡and ¡locking ¡
  • Func9onal ¡programming ¡
  • Transac9ons ¡and ¡serializability ¡
  • MapReduce ¡and ¡other ¡data-­‑centric ¡architectures ¡
  • SIMD ¡and ¡data ¡parallelism ¡
  • Communica9ng ¡sequen9al ¡processes ¡

– Message ¡passing ¡ – Channels ¡ – The ¡actor ¡model ¡

slide-44
SLIDE 44

44

15-­‑214

The ¡actor ¡model ¡

  • System ¡is ¡composed ¡of ¡independent ¡actors ¡that ¡communicate ¡

via ¡asynchronous ¡messages ¡

sequential, no shared state i.e. concurrent function calls without return values

slide-45
SLIDE 45

45

15-­‑214

The ¡actor ¡model ¡

  • System ¡is ¡composed ¡of ¡independent ¡actors ¡that ¡communicate ¡

via ¡asynchronous ¡messages ¡

  • Proper9es ¡of ¡actors: ¡

– Sequen9al ¡and ¡non-­‑blocking ¡ – Non-­‑shared, ¡mutable ¡state ¡ – Queue ¡for ¡incoming ¡ ¡ ¡ ¡ ¡ ¡messages ¡ – Inherently ¡concurrent ¡ – Extremely ¡lightweight ¡ – Distributed ¡by ¡default ¡

while ¡true: ¡ ¡process ¡next ¡message ¡

slide-46
SLIDE 46

46

15-­‑214

Implementa9ons ¡of ¡the ¡actor ¡model ¡

  • Frameworks: ¡

– Java: ¡ ¡Akka ¡ – Python: ¡ ¡Pykka ¡ – C++: ¡ ¡CAF ¡(C++ ¡Actor ¡Framework) ¡

  • Languages: ¡

– Scala ¡ – Scratch ¡ – Erlang ¡ – Elixer ¡

  • Typically ¡provide: ¡

– Communica9on ¡between ¡actors ¡ – Distribu9on ¡among ¡servers ¡ – Supervisory ¡rela9onships ¡between ¡actors ¡ – Lightweight ¡management ¡and ¡scheduling ¡

slide-47
SLIDE 47

47

15-­‑214

Processing ¡messages ¡

  • An ¡actor ¡may: ¡

– Change ¡its ¡internal ¡state ¡ – Send ¡one ¡or ¡more ¡messages ¡to ¡other ¡actors ¡ – Create ¡one ¡or ¡more ¡new ¡actors ¡

slide-48
SLIDE 48

48

15-­‑214

Processing ¡messages ¡

  • An ¡actor ¡may: ¡

– Change ¡its ¡internal ¡state ¡ – Send ¡one ¡or ¡more ¡messages ¡to ¡other ¡actors ¡ – Create ¡one ¡or ¡more ¡new ¡actors ¡

  • Defines ¡a ¡hierarchy ¡of ¡actors ¡

(source: Seven Concurrency Models in Seven Weeks by Paul Butcher.

slide-49
SLIDE 49

49

15-­‑214

Recall ¡an ¡advantage ¡of ¡Excep9ons ¡

  • Separates ¡normal ¡and ¡excep9onal ¡control ¡flow ¡

try { FileInputStream fileInput = new FileInputStream(filename); DataInput dataInput = new DataInputStream(fileInput); int i = dataInput.readInt(); fileInput.close(); return i; } catch (FileNotFoundException e) { System.out.println("Could not open file " + filename); return -1; } catch (IOException e) { System.out.println("Error reading binary data from file " + filename); return -1; }

slide-50
SLIDE 50

50

15-­‑214

Error ¡handling ¡in ¡the ¡actor ¡model ¡

  • "Let ¡it ¡crash" ¡

– Resume ¡or ¡restart ¡failed ¡actors ¡ – Escalate ¡errors ¡to ¡higher ¡level ¡

(source: Seven Concurrency Models in Seven Weeks by Paul Butcher.

slide-51
SLIDE 51

51

15-­‑214

Trade-­‑offs ¡of ¡the ¡actor ¡model ¡

  • Strengths: ¡

– Strong ¡encapsula9on ¡via ¡isola9on ¡and ¡messaging ¡ – Fault ¡tolerance ¡ – Inherently ¡distributed ¡and ¡concurrent ¡

  • Weaknesses: ¡

– Messages ¡expensive ¡compared ¡to ¡shared, ¡local ¡memory ¡ – Subtle ¡systemic ¡problems, ¡e.g. ¡overflowing ¡mailboxes ¡

slide-52
SLIDE 52

52

15-­‑214

Next ¡9me… ¡

  • Version ¡control ¡systems ¡