Professional Documents
Culture Documents
• Read (x)
• Write (x, 5)
• Start (T)
• Commit (T)
• Abort (T)
• Active Transaction
– A transaction which has neither committed nor aborted
High level model
Transaction 1 Transaction 2
Transaction n
Transaction Manager
Scheduler
Recovery Manager
Disk
Cache Manager
Recoverability (1/2)
• Transaction T Aborts T T’
T1 T2 T1 T2
Write (x,2) Write (x,2)
Read (x) Read (x)
Write (y,2) Write (y,2)
Commit Commit
Commit
Cascading Aborts (1/2)
• Because T was aborted, T1,…, Tk also have to be aborted
T T’ T’’
Read (x)
Write (x, k)
Read (y)
Read (x)
Write (y, k’)
Abort
Read (y)
Cascading Aborts (2/2)
• Recoverable executions do not prevent cascading aborts
• How can we prevent them then ?
T1 T2 T1 T2
Write (x,2) Write (x,2)
Read (x)
Write (y,2)
Abort Abort
Abort Read (x)
Write (y,2)
Commit
What we learnt so far…
Reading a value, committing a transaction
T1 T2 T1 T2 T1 T2
Write (x,2) Write (x,2) Write (x,2)
Read (x) Read (x)
Write (y,2) Write (y,2)
Commit Abort Commit
Abort Read (x)
Write (y,2)
Commit
Before image/after image
• “Undo”-ing the effects of a transaction
– Restore the before image of the data item
T1 T2
Write (x,1)
T1 T2
Write (y,3) Equivalent to Write (x,1)
Write (y,1) Final value
Write (y,3)
Commit of y: 3
Commit
Read (x)
Abort
Strict Schedule
Initial value of x: 1
T1 T2 T1 T2 T1 T2
Write (x,2) Write (x,2) Write (x,2)
Write (x,3) Write (x,3)
Abort Abort Abort
Abort Write (x,3)
T1 T2
Read (x)
Read (x)
Write (x, 200,000)
Commit
Write (x, 200)
Commit
r1[x]
w1[z] c1
r1[y]
/ w[xl ,
7,[xl - rIbI,
H, = AYI ’ c’
T
/ 72[zl - WL[YIy
w2bl ’ c2
FlGURE 2-l
Example Histories
H2 and H3 are equivalent, but H4 is not equivalent to either.
Reading assignment
How do recoverability, strict schedules, cascading aborts fit into
the big picture?
LOCKING
High level model
Transaction 1 Transaction 2
Transaction n
Transaction Manager
Scheduler
Recovery Manager
Disk
Cache Manager
Transaction Management
Transaction Manager
• Receives Transactions
Transaction 1
• Sends operations to
Transaction 2
Transaction 3 scheduler
. Read1(x)
. Write2(y,k)
. Read2(x)
Transaction n Commit1
Scheduler
• Execute op
Disk
• Reject op
• Delay op
Schedulers
• Conservative schedulers
– Delay operations until more is known
– Declaration of readset and writeset
• Aggressive schedulers
– Schedule immediately
• Efficiency
– Conflicting vs. non-conflicting set of transactions
Locking
• Each data item x has a lock associated with it
• If T wants to access x
– Scheduler first acquires a lock on x
– Only one transaction can hold a lock on x
• T releases the lock after processing
Scheduler
rl1[x] wl2[y] r1[x] w2[y] <cannot proceed>
Deadlocks (2/2)
Strategies to deal with deadlocks
• Timeouts
– Leads to inefficiency
• Detecting deadlocks
– Maintain a wait-for graph, cycle indicates deadlock
– Once a deadlock is detected, break the cycle by aborting a
transaction
• New problem: Starvation
Conservative 2PL
• Avoids deadlocks altogether
– T declares its readset and writeset
– Scheduler tries to acquire all required locks
– If not all locks can be acquired, T waits in a queue
• T never “starts” until all locks are acquired
– Therefore, it can never be involved in a deadlock
Reading HW
Strict 2PL (2PL which ensures only strict schedules)
NON-LOCK-BASED SCHEDULERS
Timestamp Ordering (1/3)
• Each transaction is associated with a timestamp
– Ti indicates Transaction T with timestamp i.
• Each operation in the transaction has the same timestamp
Timestamp Ordering (2/3)
TO Rule
If pi[x] and qk[x] are conflicting operations, then pi[x] is processed
before qk[x] iff i < k
Receive pi[x]
is i < max-q, NO
pi[x] scheduled
q conflicts with p
pi[x] rejected
Reading HW: Validation
• Aggressively schedule all operations
• Do not commit until the transaction is “validated”
SOURCE: Database System: The complete book. Garcia-Molina, Ullman and Widom
RECOVERY
Logging
• Log the operations in the transaction(s)
• Believe the log
– Does the log say transaction T has committed?
– Or does it say aborted?
– Or has only a partial trace (implicit abort)?
• In case of failures, reconstruct the DB from its log
Terminology
Data item: an element which can be read or written
– tuple, relation, B+-tree index, etc
Read P, x
x -= x* 0.1
Write x,P
System fails here
Read S, y
y = “CHEAP”
Write y, S
Output P
System fails here
Output S
System fails here
Logs
• Sequence of log records
• Need to keep track of
– Start of transaction
– Update operations (Write operations)
– End of transaction (COMMIT or ABORT)
• “Believe” the log, use the log to reconstruct a consistent DB state
Types of logs
• Undo logs
– Ensure that uncommitted transactions are rolled back (or undone)
• Redo logs
– Ensure that committed transactions are redone
• Undo/Redo logs
– Both of the above
All 3 logging styles ensure atomicity and durability
Undo Logging (1/3)
• <START T>: Start of transaction T
• <COMMIT T>
• <ABORT T>
• <T, A, x>: Transaction T modified A whose before-image is x.
Undo Logging (2/3)
<START T>
Read P, x U1: <T, X, v> should be
x -= x* 0.1 flushed before Output X
Write x,P U2: <COMMIT T> should be
<T, P, x’>
Read S, y flushed after all OUTPUTs
y = “CHEAP”
Write y, S
<T, S, y’>
FLUSH LOG
Output P
Output S
<COMMIT T>
FLUSH LOG
Undo Logging (3/3)
• Recovery with Undo log
1. If T has a <COMMIT T> entry, do nothing
2. If T has a <START T> entry, but no <COMMIT T>
• T is incomplete and needs to be undone
• Restore old values from <T,X,v> records
• There may be multiple transactions
– Start scanning from the end of the log
Redo Logging (1/3)
• All incomplete transactions can be ignored
• Redo all completed transactions
• <T, A, x>: Transaction T modified A whose after-image is x.
Redo Logging (2/3)
<START T>
Read P, x R1: <T, X, v> and
x -= x* 0.1 <COMMIT T> should be
flushed before Output X
Write x,P
<T, P, x>
Read S, y
y = “CHEAP”
Write y, S
<T, S, y>
<COMMIT T>
FLUSH LOG Write-ahead
Logging
Output P
Output S
Redo Logging (3/3)
• Recovery with Redo Logging
– If T has a <COMMIT T> entry, redo T
– If T is incomplete, do nothing (add <ABORT T>)
• For multiple transactions
– Scan from the beginning of the log
Drawbacks
• Reading HW: What are the drawbacks of UNDO and REDO
logging?
Undo/Redo Logging (1/2)
<START T>
UR1: <T, X, b, a> should be
Read P, x flushed before Output X
x -= x* 0.1
Write x,P U1: <T, X, b> should be
<T, P, b, a>
Read S, y flushed before Output X
y = “CHEAP”
U2: <COMMIT T> should be
Write y, S
<T, S, b’, a’> flushed after all OUTPUTs
FLUSH LOG
Output P
<COMMIT T> R1: <T, X, a> and
Output S <COMMIT T> should be
flushed before Output X
Undo/Redo Logging (2/2)
• Recovery with Undo/Redo Logging
– Redo all committed transactions (earliest-first)
– Undo all uncommitted transactions (latest-first)
What happens if there is a crash when you are writing a log? What
happens if there is a crash during recovery?