1
15-‑214
School ¡of ¡ ¡ Computer ¡Science ¡
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
1
15-‑214
School ¡of ¡ ¡ Computer ¡Science ¡
2
15-‑214
– Final ¡exam ¡review ¡session ¡Sunday, ¡May ¡3rd, ¡4 ¡– ¡6:30 ¡p.m., ¡Hamburg ¡1000 ¡
3
15-‑214
4
15-‑214
– 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 ¡ϕ ¡
– 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 ¡
5
15-‑214
– Lost ¡updates ¡
– Inconsistent ¡retrievals ¡
6
15-‑214
canCommit? yes doCommit confirmed Coordinator: Participants: “committed” (persistently) “prepared” “done” “uncertain” (objects still locked) “prepared” (persistently) “committed”
7
15-‑214
8
15-‑214
– Real ¡servers ¡can ¡fail ¡permanently ¡ – Persistent ¡storage ¡can ¡fail ¡permanently ¡
– Many ¡round-‑trip ¡messages ¡
9
15-‑214
– Consistency ¡ – Availability ¡ – tolerance ¡of ¡network ¡Par99ons ¡
10
15-‑214
– Finite ¡state ¡machines ¡(FSMs) ¡
11
15-‑214
12
15-‑214
13
15-‑214
14
15-‑214
15
15-‑214
16
15-‑214
17
15-‑214
18
15-‑214
19
15-‑214
20
15-‑214
21
15-‑214
22
15-‑214
23
15-‑214
Recommended: ¡ ¡A. ¡Ko ¡and ¡B. ¡Myers, ¡"Development ¡and ¡ Evalua7on ¡of ¡a ¡Model ¡of ¡Programming ¡Errors". ¡ ¡HCC ¡2003. ¡ ¡
24
15-‑214
Recommended: ¡ ¡A. ¡Ko ¡and ¡B. ¡Myers, ¡"Development ¡and ¡ Evalua7on ¡of ¡a ¡Model ¡of ¡Programming ¡Errors". ¡ ¡HCC ¡2003. ¡ ¡
25
15-‑214
– Finite ¡state ¡machines ¡(FSMs) ¡
26
15-‑214
– e.g. ¡ ¡A ¡DFA ¡that ¡accepts ¡the ¡input ¡42: ¡
4 2 0-3,5-9 0-9 0,1,3-9 0-9
27
15-‑214
– e.g. ¡ ¡A ¡DFA ¡that ¡accepts ¡the ¡input ¡42: ¡
4 2 0-3,5-9 0-9 0,1,3-9 0-9
28
15-‑214
– e.g. ¡ ¡A ¡DFA ¡that ¡accepts ¡the ¡input ¡42: ¡
4 2 0-3,5-9 0-9 0,1,3-9 0-9
29
15-‑214
– e.g. ¡ ¡A ¡DFA ¡that ¡accepts ¡the ¡input ¡42: ¡
4 2 0-3,5-9 0-9 0,1,3-9 0-9
30
15-‑214
– Essen9ally ¡a ¡DFA ¡with ¡an ¡infinite ¡memory ¡tape ¡
4 2 0-3,5-9 0-9 0,1,3-9 0-9 … ¡ 0 ¡ 1 ¡ 0 ¡ 1 ¡ 1 ¡ … ¡
31
15-‑214
4 2 0-3,5-9 0-9 0,1,3-9 0-9
32
15-‑214
33
15-‑214
34
15-‑214
35
15-‑214
(example from Wikipedia...)
36
15-‑214
37
15-‑214
– Warning: ¡ ¡The ¡StateMachineCalculator ¡inten9onally ¡demonstrates ¡poor ¡
38
15-‑214
39
15-‑214
– An ¡object's ¡behavior ¡depends ¡
its ¡behavior ¡at ¡run-‑9me ¡based ¡
– Transi9on ¡func9on ¡between ¡ states ¡is ¡highly ¡state-‑dependent ¡ and ¡complex ¡
– State-‑specific ¡behavior ¡is ¡ par99oned, ¡localized, ¡ ¡and ¡ cohesive ¡ – State ¡transi9ons ¡are ¡explicit ¡ – State ¡objects ¡can ¡be ¡shared ¡
40
15-‑214
R L C
41
15-‑214
§ 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 ¡
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)*
42
15-‑214
– Lost ¡updates ¡
– Inconsistent ¡retrievals ¡
43
15-‑214
– Message ¡passing ¡ – Channels ¡ – The ¡actor ¡model ¡
44
15-‑214
sequential, no shared state i.e. concurrent function calls without return values
45
15-‑214
– Sequen9al ¡and ¡non-‑blocking ¡ – Non-‑shared, ¡mutable ¡state ¡ – Queue ¡for ¡incoming ¡ ¡ ¡ ¡ ¡ ¡messages ¡ – Inherently ¡concurrent ¡ – Extremely ¡lightweight ¡ – Distributed ¡by ¡default ¡
while ¡true: ¡ ¡process ¡next ¡message ¡
46
15-‑214
– Java: ¡ ¡Akka ¡ – Python: ¡ ¡Pykka ¡ – C++: ¡ ¡CAF ¡(C++ ¡Actor ¡Framework) ¡
– Scala ¡ – Scratch ¡ – Erlang ¡ – Elixer ¡
– Communica9on ¡between ¡actors ¡ – Distribu9on ¡among ¡servers ¡ – Supervisory ¡rela9onships ¡between ¡actors ¡ – Lightweight ¡management ¡and ¡scheduling ¡
47
15-‑214
– Change ¡its ¡internal ¡state ¡ – Send ¡one ¡or ¡more ¡messages ¡to ¡other ¡actors ¡ – Create ¡one ¡or ¡more ¡new ¡actors ¡
48
15-‑214
– Change ¡its ¡internal ¡state ¡ – Send ¡one ¡or ¡more ¡messages ¡to ¡other ¡actors ¡ – Create ¡one ¡or ¡more ¡new ¡actors ¡
(source: Seven Concurrency Models in Seven Weeks by Paul Butcher.
49
15-‑214
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; }
50
15-‑214
– Resume ¡or ¡restart ¡failed ¡actors ¡ – Escalate ¡errors ¡to ¡higher ¡level ¡
(source: Seven Concurrency Models in Seven Weeks by Paul Butcher.
51
15-‑214
– Strong ¡encapsula9on ¡via ¡isola9on ¡and ¡messaging ¡ – Fault ¡tolerance ¡ – Inherently ¡distributed ¡and ¡concurrent ¡
– Messages ¡expensive ¡compared ¡to ¡shared, ¡local ¡memory ¡ – Subtle ¡systemic ¡problems, ¡e.g. ¡overflowing ¡mailboxes ¡
52
15-‑214