Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Concurrency
Control & Recovery
Sections 22.1, 23.1-23.2
Fall
2014
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ),
Database Systems: Complete Book (Garcia-Molina et al.)
Active state
Partially committed state
Committed state
Failed state
Terminated State
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6)
(Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book
(Garcia-Molina et al.)
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6)
(Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book
(Garcia-Molina et al.)
Roll Back
Needed for transactions that have a [start_transaction, T]
entry into the log but no commit entry [commit, T] into the
log
5
Transaction Schedule
A schedule (or history) S of n transactions T1, T2, ,
Tn is an ordering of the operations of the transactions
which can be interleaved in the schedule S
For each transaction Ti that participates in S, the operations of
T1 in S must appear in the same order in which they occur in
T1. However, that operations from other transactions Tj can
be interleaved with the operations of Ti in S.
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6)
(Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book
(Garcia-Molina et al.)
Transaction Schedule
For purpose of recovery and concurrency control,
we are mainly interested in the read_item and
write_item operations of the transactions
We also consider the commit and abort operations
begin_transaction
read_item
write_item
end_transaction
commit
abort
Example:
Sa: r1(X); r2(X); w1(X); r1(Y); w2(X); w1(Y)
Transaction Schedule
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6)
(Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book
(Garcia-Molina et al.)
Schedule Serializability
Serial Schedule
A schedule S is serial if, for every transaction T participating
in the schedule, all the operations of T are executed
consecutively in the schedule.
Otherwise, the schedule is called nonserial schedule.
Problem with
serial schedules
is that they limit
concurrency by
prohibiting
interleaving of
operations
9
Schedule Serializability
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6)
(Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book
(Garcia-Molina et al.)
Schedule Serializability
Equivalent Schedules
X=500
X=100
X=500
X=100+10
X=110
X=500+10
X=510
X=100 * 1.1
X=110
X=500 * 1.1
X=550
Two schedules that are result equivalent for the initial value X=100 but are
not result equivalent in general
11
Schedule Serializability
Conflict Equivalent
Example
S2
S1
T1
T2
T1
T2
---
---
write_item(X)
---
---
---
---
read_item(X)
write_item(X)
---
---
---
---
read_item(X)
---
---
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6)
(Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book
(Garcia-Molina et al.)
Schedule Serializability
13
14
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6)
(Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book
(Garcia-Molina et al.)
Serialization Graph
Serialization Graph
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6)
(Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book
(Garcia-Molina et al.)
Concurrency Control
17
T1
T2
X = 80
X = 75
X = 90
read_item(X);
X = 80
X := X 5;
X = 75
read_item(X);
X = 80
X := X + 10;
X = 90
write_item(X);
write_item(X);
18
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6)
(Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book
(Garcia-Molina et al.)
T1
T2
X = 80
X = 75
read_item(X);
X = 80
X := X 5;
X = 75
write_item(X);
X := X / 0;
read_item(X);
X = 75
X := X + 10;
X = 85
T1 aborts
X = 85
write_item(X);
19
T1
T2
read_item(X1);
X1 = 80
X1 := X1 + 5;
X1 = 85
read_item(X1);
X1 = 80
SUM := X1;
SUM = 80
read_item(X2);
X2 = 15
SUM := SUM+X2;
SUM = 95
read_item(X3);
X3= 30
SUM := SUM+X3;
SUM = 125
write_item(X1);
read_item(X3);
X3 = 25
X3 := X3 + 5;
X3 = 30
write_item(X3);
20
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6)
(Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book
(Garcia-Molina et al.)
10
Locking Protocols
One way to ensure isolation is to require that data
items be accessed in a mutually exclusive manner
While one transaction is accessing a data item, no other
transaction can modify that data item
Most common method used to implement this requirement
is to allow a transaction to access a data item only if it is
currently holding a lock on that item
22
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6)
(Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book
(Garcia-Molina et al.)
11
Locking Protocols
Lock requests are sent to the concurrency control
manager
A transaction can proceed with the operation only after the
concurrency-control manager grants the lock to the
transaction
The use of these two lock modes allows multiple
transactions to read a data item but limits write access to
just one transaction at a time
Lock-compatibility matrix
S
true
false
false
false
23
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6)
(Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book
(Garcia-Molina et al.)
12
Lock-based Protocols
Two-Phase Locking Protocols
This is a protocol which ensures conflict-serializable
schedules.
Phase 1: Growing Phase
transaction may obtain locks
transaction may not release locks
26
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6)
(Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book
(Garcia-Molina et al.)
13
Lock-based Protocols
Two-Phase Locking Protocols
Lock Conversions
Two-phase locking with lock conversions:
First Phase:
can acquire a lock-shared on item
can acquire a lock-exclusive on item
can convert a lock-shared to a lock-exclusive (upgrade)
Second Phase:
can release a lock-shared
can release a lock-exclusive
can convert a lock-exclusive to a lock-shared (downgrade)
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6)
(Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book
(Garcia-Molina et al.)
14
Implementation of Locking
A lock manager can be implemented as a separate process
to which transactions send lock and unlock requests
The lock manager replies to a lock request by sending a
lock grant messages (or a message asking the transaction
to roll back, in case of a deadlock)
The requesting transaction waits until its request is
answered
The lock manager maintains a data-structure called a lock
table to record granted locks and pending requests
Lock Table
Black rectangles indicate granted locks,
white ones indicate waiting requests
Lock table also records the type of lock
granted or requested
New request is added to the end of the
queue of requests for the data item, and
granted if it is compatible with all
earlier locks
Unlock requests result in the request
being deleted, and later requests are
checked to see if they can now be
granted
If transaction aborts, all waiting or
granted requests of the transaction are
deleted
lock manager may keep a list of locks
held by each transaction, to implement
this efficiently
30
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6)
(Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book
(Garcia-Molina et al.)
15
Recovery
31
Database Recovery
Purpose of Database Recovery
To bring the database into the last consistent state, which
existed prior to the failure.
To preserve transaction properties (Atomicity,
Consistency, Isolation and Durability).
Example:
If the system crashes before a fund transfer transaction
completes its execution, then either one or both accounts
may have incorrect value. Thus, the database must be
restored to the state before the transaction modified any
of the accounts.
32
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6)
(Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book
(Garcia-Molina et al.)
16
Database Recovery
Failure Classification
The database may become unavailable for
use due to
Transaction failure: Transactions may fail
because of incorrect input, deadlock, incorrect
synchronization.
System failure: System may fail because of
addressing error, application error, operating
system fault, RAM failure, etc.
Media failure: Disk head crash, power
disruption, etc.
33
Database Recovery
Transaction Log
For recovery from any type of failure data values prior to
modification (BFIM - BeFore IMage) and the new value
after modification (AFIM AFter IMage) are required.
These values and other information is stored in a sequential
file called Transaction log. A sample log is given below.
Back P and Next P point to the previous and next log
records of the same transaction.
T ID Back P Next P Operation Data item
Begin
T1
0
1
T1
1
4
Write
X
Begin
T2
0
8
T1
2
5
W
Y
T1
4
7
R
M
T3
0
9
R
N
T1
5
nil
End
BFIM
AFIM
X = 100
X = 200
Y = 50 Y = 100
M = 200 M = 200
N = 400 N = 400
34
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6)
(Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book
(Garcia-Molina et al.)
17
Database Recovery
Data Update
Immediate Update: As soon as a data item is modified in
cache, the disk copy is updated.
Deferred Update: All modified data items in the cache is
written either after a transaction ends its execution or after a
fixed number of transactions have completed their
execution.
Shadow Update: The modified version of a data item does
not overwrite its disk copy but is written at a separate disk
location.
In-place Update: The disk version of the data item is
overwritten by the cache version.
35
Database Recovery
Data Caching
Data items to be modified are first stored into
database cache by the Cache Manager (CM) and
after modification they are flushed (written) to the
disk.
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6)
(Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book
(Garcia-Molina et al.)
18
Database Recovery
Transaction Roll-back (Undo) and Roll-Forward (Redo)
To maintain atomicity, a transactions operations are
redone or undone.
Undo: Restore all BFIMs on to disk (Remove all AFIMs).
Redo: Restore all AFIMs on to disk.
Database Recovery
Write-Ahead Logging (WAL)
When in-place update (immediate or deferred) is
used then log is necessary for recovery and it must
be available to recovery manager. This is achieved
by Write-Ahead Logging (WAL) protocol. WAL
states that
For Undo: Before a data items AFIM is flushed to the
database disk (overwriting the BFIM) its BFIM must be
written to the log and the log must be saved on a stable store
(log disk).
For Redo: Before a transaction executes its commit operation,
all its AFIMs must be written to the log and the log must be
saved on a stable store.
38
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6)
(Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book
(Garcia-Molina et al.)
19
Database Recovery
Checkpointing
Time to time (randomly or under some criteria) the
database flushes its buffer to database disk to
minimize the task of recovery. The following steps
defines a checkpoint operation:
1. Suspend execution of transactions temporarily.
2. Force write modified buffer data to disk.
3. Write a [checkpoint] record to the log, save the log to disk.
Database Recovery
Steal/No-Steal and Force/No-Force
Possible ways for flushing database cache to database
disk:
1.
2.
3.
4.
Steal/No-Force (Undo/Redo)
Steal/Force (Undo/No-redo)
No-Steal/No-Force (Redo/No-undo)
No-Steal/Force (No-undo/No-redo)
40
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6)
(Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book
(Garcia-Molina et al.)
20
Recovery Scheme
Deferred Update (No Undo/Redo)
The data update goes as follows:
A set of transactions records their updates in the
log.
At commit point under WAL scheme these
updates are saved on database disk.
After reboot from a failure the log is used to redo
all the transactions affected by this failure. No
undo is required because no AFIM is flushed to
the disk before a transaction commits.
41
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6)
(Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book
(Garcia-Molina et al.)
21