Download presentation
Presentation is loading. Please wait.
1
CS 245Notes 101 CS 245: Database System Principles Notes 10: More TP Hector Garcia-Molina
2
CS 245Notes 102 Sections to Skim: Section 18.8 [18.8] Sections 19.2 19.4, 19.5, 19.6 [none, i.e., read all Ch 19] [In the Second Edition, skip all of Chapter 20, and Sections 21.5, 21.6, 21.7, 22.2 through 22.7]
3
CS 245Notes 103 Chapter 19 [19] More on transaction processing Topics: Cascading rollback, recoverable schedule Deadlocks –Prevention –Detection View serializability Distributed transactions Long transactions (nested, compensation)
4
CS 245Notes 104 Example: T j T i W j (A) r i (A) Commit T i Abort T j Concurrency control & recovery … … … … … …
5
CS 245Notes 105 Example: T j T i W j (A) r i (A) Commit T i Abort T j Concurrency control & recovery … … … … … … Non-Persistent Commit (Bad!)
6
CS 245Notes 106 Example: T j T i W j (A) r i (A) Commit T i Abort T j Concurrency control & recovery … … … … … … Non-Persistent Commit (Bad!) avoided by recoverable schedules
7
CS 245Notes 107 Example: T j T i W j (A) r i (A) w i (B) Abort T j [ Commit T i] Concurrency control & recovery … … … … … …
8
CS 245Notes 108 Example: T j T i W j (A) r i (A) w i (B) Abort T j [ Commit T i] Concurrency control & recovery … … … … … … Cascading rollback (Bad!)
9
CS 245Notes 109 Example: T j T i W j (A) r i (A) w i (B) Abort T j [ Commit T i] Concurrency control & recovery … … … … … … Cascading rollback (Bad!) avoided by avoids-cascading- rollback (ACR) schedules
10
CS 245Notes 1010 Schedule is conflict serializable T j T i But not recoverable
11
CS 245Notes 1011 Need to make “final’ decision for each transaction: –commit decision - system guarantees transaction will or has completed, no matter what –abort decision - system guarantees transaction will or has been rolled back (has no effect)
12
CS 245Notes 1012 To model this, two new actions: C i - transaction T i commits A i - transaction T i aborts
13
CS 245Notes 1013... Back to example: T j T i W j (A) r i (A) C i can we commit here?
14
CS 245Notes 1014 Definition T i reads from T j in S (T j S T i ) if (1) w j (A) < S r i (A) (2) a j < S r i (A) (< : does not precede ) (3) If w j (A) < S w k (A) < S r i (A) then a k < S r i (A)
15
CS 245Notes 1015 Definition Schedule S is recoverable if whenever T j S T i and j i and C i S then C j < S C i
16
CS 245Notes 1016 Note: in transactions, reads and writes precede commit or abort If C i T i, then r i (A) < C i w i (A) < C i If A i T i, then r i (A) < A i w i (A) < A i Also, one of Ci, Ai per transaction
17
CS 245Notes 1017 How to achieve recoverable schedules?
18
CS 245Notes 1018 With 2PL, hold write locks to commit (strict 2PL) T j T i W j (A) C j u j (A) r i (A)...
19
CS 245Notes 1019 With validation, no change!
20
CS 245Notes 1020 S is recoverable if each transaction commits only after all transactions from which it read have committed.
21
CS 245Notes 1021 S is recoverable if each transaction commits only after all transactions from which it read have committed. S avoids cascading rollback if each transaction may read only those values written by committed transactions.
22
CS 245Notes 1022 S is recoverable if each transaction commits only after all transactions from which it read have committed. S avoids cascading rollback if each transaction may read only those values written by committed transactions. S is strict if each transaction may read and write only items previously written by committed transactions.
23
CS 245Notes 1023 Relationship of RC, ACR, Strict Avoids cascading rollback RC ACR ST SERIAL
24
CS 245Notes 1024 Examples Recoverable: –w 1 (A) w 1 (B) w 2 (A) r 2 (B) c 1 c 2 Avoids Cascading Rollback: –w 1 (A) w 1 (B) w 2 (A) c 1 r 2 (B) c 2 Strict: –w 1 (A) w 1 (B) c 1 w 2 (A) r 2 (B) c 2 Assumes w 2 (A) is done without reading
25
CS 245Notes 1025 Where are serializable schedules? Avoids cascading rollback RC ACR ST SERIAL
26
CS 245Notes 1026 Deadlocks Detection –Wait-for graph Prevention –Resource ordering –Timeout –Wait-die –Wound-wait
27
CS 245Notes 1027 Deadlock Detection Build Wait-For graph Use lock table structures Build incrementally or periodically When cycle found, rollback victim T1T1 T3T3 T2T2 T6T6 T5T5 T4T4 T7T7
28
CS 245Notes 1028 Resource Ordering Order all elements A 1, A 2, …, A n A transaction T can lock A i after A j only if i > j
29
CS 245Notes 1029 Resource Ordering Order all elements A 1, A 2, …, A n A transaction T can lock A i after A j only if i > j Problem : Ordered lock requests not realistic in most cases
30
CS 245Notes 1030 Timeout If transaction waits more than L sec., roll it back! Simple scheme Hard to select L
31
CS 245Notes 1031 Wait-die Transactions given a timestamp when they arrive …. ts(T i ) T i can only wait for T j if ts(T i )< ts(T j )...else die
32
CS 245Notes 1032 T 1 (ts =10) T 2 (ts =20) T 3 (ts =25) wait Example:
33
CS 245Notes 1033 T 1 (ts =10) T 2 (ts =20) T 3 (ts =25) wait Example: wait?
34
CS 245Notes 1034 T 1 (ts =10) T 2 (ts =20) T 3 (ts =25) wait Example: wait?
35
CS 245Notes 1035 Starvation with Wait-Die When transaction dies, re-try later with what timestamp? –original timestamp –new timestamp (time of re-submit)
36
CS 245Notes 1036 Starvation with Wait-Die Resubmit with original timestamp Guarantees no starvation –Transaction with oldest ts never dies –A transaction that dies will eventually have oldest ts and will complete...
37
CS 245Notes 1037 T 1 (ts =22) T 2 (ts =20) T 3 (ts =25) wait(A) Second Example: requests A: wait for T 2 or T 3 ? Note: ts between 20 and 25.
38
CS 245Notes 1038 T 1 (ts =22) T 2 (ts =20) T 3 (ts =25) wait(A) Second Example (continued): wait(A) One option: T 1 waits just for T 3, transaction holding lock. But when T 2 gets lock, T 1 will have to die!
39
CS 245Notes 1039 T 1 (ts =22) T 2 (ts =20) T 3 (ts =25) wait(A) Second Example (continued): wait(A) Another option: T 1 only gets A lock after T 2, T 3 complete, so T 1 waits for both T 2, T 3 T 1 dies right away!
40
CS 245Notes 1040 T 1 (ts =22) T 2 (ts =20) T 3 (ts =25) wait(A) Second Example (continued): wait(A) Yet another option: T 1 preempts T 2, so T 1 only waits for T 3 ; T 2 then waits for T 3 and T 1... T 2 may starve? redundant arc
41
CS 245Notes 1041 Wound-wait Transactions given a timestamp when they arrive … ts(T i ) T i wounds T j if ts(T i )< ts(T j ) else T i waits “Wound”: T j rolls back and gives lock to T i
42
CS 245Notes 1042 T 1 (ts =25) T 2 (ts =20) T 3 (ts =10) wait Example:
43
CS 245Notes 1043 T 1 (ts =25) T 2 (ts =20) T 3 (ts =10) wait Example: wait
44
CS 245Notes 1044 Starvation with Wound-Wait When transaction dies, re-try later with what timestamp? –original timestamp –new timestamp (time of re-submit)
45
CS 245Notes 1045 T 1 (ts =15) T 2 (ts =20) T 3 (ts =10) wait(A) Second Example: requests A: wait for T 2 or T 3 ? Note: ts between 10 and 20.
46
CS 245Notes 1046 T 1 (ts =15) T 2 (ts =20) T 3 (ts =10) wait(A) Second Example (continued): wait(A) One option: T 1 waits just for T 3, transaction holding lock. But when T 2 gets lock, T 1 waits for T 2 and wounds T 2.
47
CS 245Notes 1047 T 1 (ts =15) T 2 (ts =20) T 3 (ts =10) wait(A) Second Example (continued): wait(A) Another option: T 1 only gets A lock after T 2, T 3 complete, so T 1 waits for both T 2, T 3 T 2 wounded right away!
48
CS 245Notes 1048 T 1 (ts =15) T 2 (ts =20) T 3 (ts =10) wait(A) Second Example (continued): wait(A) Yet another option: T 1 preempts T 2, so T 1 only waits for T 3 ; T 2 then waits for T 3 and T 1... T 2 is spared!
49
CS 245Notes 1049 User/Program commands Lots of variations, but in general Begin_work Commit_work Abort_work
50
CS 245Notes 1050 Nested transactions User program: Begin_work; If results_ok, then commit work else abort_work...
51
CS 245Notes 1051 Nested transactions User program: Begin_work; If results_ok, then commit work else {abort_work; try something else…} If results_ok, then commit work else abort_work...
52
CS 245Notes 1052 Parallel Nested Transactions T 1 : begin-work parallel: T 11 :begin_work commit_work T 12 :begin_work commit_work...
53
CS 245Notes 1053 Parallel Nested Transactions T 1 : begin-work parallel: T 11 :begin_work commit_work T 12 :begin_work commit_work... T1T1 T 11 T 12
54
CS 245Notes 1054 Parallel Nested Transactions T 1 : begin-work parallel: T 11 :begin_work commit_work T 12 :begin_work commit_work... T1T1 T 11 T 12 T1T1
55
CS 245Notes 1055 Locking What are we really locking?
56
CS 245Notes 1056 Example: T i Read record r 1 Read record r 1 do record locking Modify record r 3...
57
CS 245Notes 1057 But underneath: Disk pages R3R3 R1R1 R2R2 record id
58
CS 245Notes 1058 But underneath: Disk pages R3R3 R1R1 R2R2 record id If we lock all data involved in read of R1, we may prevent an update to R2 (which may require reorganization within block)
59
CS 245Notes 1059 Solution: view DB at two levels Top level: record actions record locks undo/redo actions — logical e.g., Insert record(X,Y,Z) Redo: insert(X,Y,Z) Undo: delete
60
CS 245Notes 1060 Low level: deal with physical details latch page during action (release at end of action)
61
CS 245Notes 1061 Note: undo does not return physical DB to original state; only same logical state e.g., Insert R3Undo (delete R3) R1 R2 R1 R2 R3
62
CS 245Notes 1062 Logging Logical Actions Logical action typically span one block (physiological actions) Undo/redo log entry specifies undo/redo logical action
63
Question How to deal with spanned record? CS 245Notes 1063 part (a)part (b)part (c)
64
CS 245Notes 1064 Logging Logical Actions Logical action typically span one block (physiological actions) Undo/redo log entry specifies undo/redo logical action Challenge: making actions idempotent Example (bad): redo insert key inserted multiple times!
65
CS 245Notes 1065 Solution: Add Log Sequence Number Log record: LSN=26 OP=insert(5,v2) into P... 3, v1 semlsn=25... 3, v1 semlsn=26... 5, v2
66
CS 245Notes 1066 Still Have a Problem! 3, v1 lsn=24... 4, v2 3, v1 lsn=25... 3, v1 lsn=26... 5, v3 T1 Del 4 T2 Ins 5
67
CS 245Notes 1067 Still Have a Problem! 3, v1 lsn=24... 4, v2 3, v1 lsn=25... 3, v1 lsn=26... 5, v3 T1 Del 4 T2 Ins 5 3, v1 lsn=??... 5, v3 4, v2 undo Del 4
68
CS 245Notes 1068 Still Have a Problem! 3, v1 lsn=24... 4, v2 3, v1 lsn=25... 3, v1 lsn=26... 5, v3 T1 Del 4 T2 Ins 5 3, v1 lsn=??... 5, v3 4, v2 undo Del 4 Make log entry for undo lsn=27
69
CS 245Notes 1069 Compensation Log Records Log record to indicate undo (not redo) action performed Note: Compensation may not return page to exactly the initial state
70
CS 245Notes 1070 At Recovery: Example lsn=21 T1 a1 p1 lsn=35 T1 a2 -1 p2 lsn=27 T1 a2 p2... Log:
71
CS 245Notes 1071 What to do with p2 (during T1 rollback)? If lsn(p2)<27 then... ? If 27 lsn(p2) < 35 then... ? If lsn(p2) 35 then... ? Note: lsn(p2) is lsn of p copy on disk
72
CS 245Notes 1072 Recovery Strategy [1] Reconstruct state at time of crash –Find latest valid checkpoint, Ck, and let ac be its set of active transactions –Scan log from Ck to end: For each log entry [lsn, page] do: if lsn(page) < lsn then redo action If log entry is start or commit, update ac
73
CS 245Notes 1073 Recovery Strategy [2] Abort uncommitted transactions –Set ac contains transactions to abort –Scan log from end to Ck : For each log entry (not undo) of an ac transaction, undo action (making log entry) –For ac transactions not fully aborted, read their log entries older than Ck and undo their actions
74
CS 245Notes 1074 Example: What To Do After Crash lsn=21 T1 a1 p1 lsn=35 T1 a2 -1 p2 lsn=27 T1 a2 p2... Log:... lsn=29 T1 a3 p3 lsn=31 T1 a3 -1 p3... chk pt
75
CS 245Notes 1075 During Undo: Skip Undo’s lsn=21 T1 a1 p1 lsn=35 T1 a2 -1 p2 lsn=27 T1 a2 p2... Log:... lsn=29 T1 a3 p3 lsn=31 T1 a3 -1 p3... chk pt pointer to forward action pointer to previous T1 action
76
CS 245Notes 1076 Related idea: Sagas Long running activity: T 1, T 2,... T n Each step/trasnaction Ti has a compensating transaction Ti-1 Semantic atomicity: execute one of –T 1, T 2,... T n –T 1, T 2,... T n-1 T -1 n-1, T -1 n-2,... T -1 1 –T 1, T 2,... T n-2 T -1 n-2, T -1 n-3,... T -1 1 –T 1, T -1 1 –nothing...
77
CS 245Notes 1077 Summary Cascading rollback Recoverable schedule Deadlock –Prevention –Detectoin Nested transactions Multi-level view
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.