Title: Concurrency Control and Crash Recovery
1Concurrency Control and Crash Recovery
2Deadlock Detection
- Waits-for graph
- Each node corresponds to an active transaction
- There is an arc (edge) from Tk to Tl currently
holding a lock if and only if Tk is waiting for
Tl to release the lock. - If a waits-for graph is cyclic, then the schedule
contains a deadlock
3Example 9
T1 T2 T3 T4
R(A)
R(C)
W(B)
W(A)
W(C)
R(B)
W(A)
Does the schedule contain a deadlock?
4Frequency of Deadlock Detection
- Assign initial time step ?t, ?tmin, and ?tmax
- If during ?t no deadlock is detected
- ?t 2 ?t
- If during ?t a deadlock is detected
- ?t ?t/2
5Recovery after Deadlock Detection
- Choice of a victim
- A transaction that has just started
- A transaction that has a few locks
- A transaction that have a few changes left.
6Database Performance
- Deadlock prevention
- Aborted transactions
- Suspended transactions
- Too strong conditions
- Deadlock detection
- Analysis of schedules
7Concurrency Control with Timestamping Methods
- Timestamp unique identifier indicates starting
time of the transaction - Each data item has read and write timestamps
timestamps of last transactions operating with
the data item - Methods
- Basic timestamp ordering
- Thomass write rule
8Basic Timestamp Ordering - Read Rule
- Let T1 and T2 transactions with timestamps
ts(T1) lt ts(T2) - T1 issues a R(A) and A is modified by T2
- T1 is aborted and restarted with a new timestamp
- T2 issues a R(B) and B is modified by T1
- T2 reads B
-
9Basic Timestamp Ordering Write Rule
- T1 issues a W(B) and B is read by T2
- T1 is aborted and restarted with a new timestamp
- T1 issues a W(C) and C is modified by T2
- T1 is aborted and restarted with a new timestamp
- T2 issues a W(D) and D is modified by T1
- T2 modifies D
10Thomass Write Rule
- Let T1 and T2 transactions with timestamps
ts(T1) lt ts(T2) - T1 issues a W(B) and B is read by T2
- T1 is aborted and restarted with a new timestamp
- T1 issues a W(C) and C is modified by T2
- W1(C) is ignored
- T2 issues a W(D) and D is modified by T1
- T2 modifies D
11Multiversion Schemes
- Each W(O) from the schedule creates a new version
of O - When a transaction issues R(O), the system
selects the version that ensures serializability - Versions that are no longer needed are deleted
12Multiversion Timestamping Method
- Each transaction receives a static timestamp
ts(Ti) - For each data item used the system maintains
versions O1, O2,,Om - Each version Ok consists of
- Content
- W-timestamp
- R-timestamp
13Multiversion Timestamp Ordering
- Possible version for any transaction T
- Om - version with maximal W-timestamp which is
less or equal to ts(T) - If T requests R(O), the content of Om is returned
- If T requests W(O)
- If ts(T)ltR-timestamp(Om), T is aborted
- If ts(T)R-tamestamp(Om), a new version Om1 is
created.
14Implementation Issues
- Control for schedule recoverability
- Version withdrawal
- Determine the timestamp of the oldest active
transaction ts(T) - Find all versions with W-timestamp less than
ts(T) - Delete all versions except the youngest one
15Concurrency Control with Optimistic Methods
- Most database operations do not conflict
- Transaction is executed without restrictions
until commits - Phases
- Read Phase
- Validation Phase
- Write Phase
16Transaction Phases
- Read-only transaction
- Read from the start until the commit point
- Validation possible conflicts are checked
- Update transaction
- Read
- Validation
- Write updates made to the local copy are stored
to the database
17Validation
- Each transaction is assigned three timestamps
start, validation, and finish - Test for transaction T
- For all S such that start(S)ltstart(T),
finish(S)ltstart(T) - For any S such that start(S)ltstart(T), but
finish(S)start(T) - T doesnt read data modified by S
- start(T)ltfinish(S)ltvalidation(T)
18Rollback with Optimistic Method
- If the schedule contains a conflict, T is aborted
and restarted - Rollback affects only local copy
- No cascading rollbacks
19Phantom Problem
- Two transactions are concurrently executed and
one of them implements INSERT or DELETE statement - Phantom the possibility that a transaction
retrieves a set of objects twice and receives
different sets
20Transaction Support in SQL
- The beginning of a transaction
- Ending of the transaction
- Explicit
- COMMIT
- ROLLBACK
- Implicit
- Successful end of the program
- Abnormal program termination (Transaction is
aborted)
21Transaction Characteristics
- Access mode
- READ ONLY
- READ WRITE
- Isolation level
- Diagnostics size
22Isolation Levels
- Serializable
- T reads only committed data
- No value read or written by T is modified by T
until T is complete - Phantom is not allowed
- Repeatable read
- T reads only committed data
- No value read or written by T is modified by T
until T is complete - Phantom is allowed
23Isolation Levels (cont)
- Read committed
- T reads only committed data
- No value written by T is modified by T until T
is complete - Phantom is allowed
- Read uncommitted
- T reads changes of active transactions, values
read can be further modified - Is allowed for READ ONLY access mode
24Transaction Configuration
- SET TRANSACTION
- READ ONLY l READ WRITE
- ISOLATION LEVEL
- READ UNCOMMITTED l READ COMMITTED l REPEATABLE
READ l - SERIALIZABLE
25Transaction Support in Oracle
- SET TRANSACTION READ ONLY
- SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
- SET TRANSACTION ISOLATION LEVEL READ COMMITTED
26Crash Recovery
- The process of restoring the database to a
correct state - A transaction is a unit of recovery
- Loss of updates of committed transactions
- Atomicity and durability support
27Buffers
- Main memory locations which are used to transfer
data to and from secondary storage - Dirty bit and dirty page
- Replacement strategies
- First-in-first-out (FIFO)
- Least recently used (LRU)
28Buffer management
- A steal policy buffer manager writes a buffer to
disk before a transaction commits - Alternative policy no-steal
- A force policy all pages updated by a
transaction are immediately written to disk when
the transaction commits. - Alternative policy no-force
29Recovery Facilities
- Backup mechanism
- Logging
- Checkpoints
- Recovery algorithms
30The Log
- History of actions executed by the DBMS
- Stable storage
- Log tail
- Actions are recorded in chronological order
31Log Sequence Number
- Monotonically increasing order
- Every page (object) contains pageLSN
- PageLSN - the most recent LSN from the log
32Actions recorded to the Log
- Start
- Updating an object
- Update record is appended to the log tail
- pageLSN is set to the current LSN
- Commit
- Commit record is appended to the log
- Log tail is written to stable storage
33Actions recorded to the Log (cont)
- Abort
- Abort log record is appended to the log
- Undo is initiated
- End
- Undoing updates
- After the action described in update log record
is undone, a compensation log record (CLR) is
appended to the log.
34Update Log Record
- Common fields for all log records
- LSN, prevLSN, transID, type
- Fields for update records only
- PageID
- Length of changes in bytes
- Offset (start byte)
- Before-image
- After-image
35Compensation Log Record
- Is written just before the action recorded in
Update log record is undone. - The same fields as for Update record
- Field undonextLSN
- LSN from prevLSN of correspondent Update log
record
36The Write-Ahead Log Protocol
- All Update log records must be written to stable
storage first - Log records
- Transaction records
- Systems records
37Checkpoints
- A point of synchronization between database and
transaction log - All buffers are force-written to secondary
storage - Checkpoints in AREIS
- Begin_checkpoint record
- End_checkpoint record
- Master record holds LSN of the begin_checkpoint
record
38Recovery Using Deferred Update
- Updates are not written to the database until the
transaction T commits - T starts
- Start log record
- T commits
- Commit log record
- Log tail is force-written to disk
- Updates are implemented
39Recovery Using Deferred Update (cont)
- T aborts
- Abort log record
- Ignore the log records related to T
- Recovery after the crash
- Most recent checkpoint
- Transactions with Start and Commit log records
are redone - Transactions with Start and Abort log records are
ignored - Transactions that have neither Commit nor Abort
log records receive Abort log records
40Recovery Using Immediate Update
- Updates are written to the database as they occur
- T starts
- Start log record
- T writes
- Update log record
- Update is written to the buffer
- Update is applied to the database when the buffer
content is transferred to secondary storage - T commits
- Commit log record
41Recovery Using Immediate Update (cont)
- T aborts
- Updates are undone in reverse order
- Recovery after the crash
- Most recent checkpoint
- Transactions with Start and Commit log records
are redone - Transactions with Start log record that dont
have Commit log record are undone
42Recovery (Example 10)
T1
T2
T3
T4
T5
T6
tc
tf
43ARIES
- Algorithm for Recovery and Isolation Exploiting
Semantics - Works with steal, no-force policies
- Three main phases
- Analysis
- Redo
- Undo
44Basic Principles
- Write-ahead logging
- Repeating history during Redo phase
- Logging of all changes during Undo phase
45Analysis Phase
- Scan log from the checkpoint to the end
- Main goal
- To determine the point in the log at which to
start the Redo step - To determine dirty pages at the moment of the
crash - To identify active transactions at the time of
the crash
46Analysis Step Result
- Transaction Table contains accurate list of
transactions active at the moment of the crash - (actions must be undone)
- Dirty Page Table contains list of modified pages
that must be recorded to secondary storage - (actions must be redone)
47Redo Phase
- Repeating history paradigm
- Redo(Redo(A)) Redo(A)
- Redo actions are recorded to the log
- The result
- The database is brought to the state as before
the crash
48Undo Phase
- Scans backward from the end of the log
- Undo(Undo(A)) Undo(A)
- For every Undo action the CLR is added to the log
- Removes all changes caused by incomplete
transactions