SlideShare a Scribd company logo
CS501: DATABASES AND DATA
MINING
Concurrency and Recovery Control1
OTHER NOTIONS OF SERIALIZABILITY
 The schedule shown
produces same
outcome as the serial
schedule < T T >schedule < T1, T5 >
 yet is not conflict
equivalent or view
i l t t itequivalent to it.
2
RECOVERABILITYRECOVERABILITY
 So far we have seen whether a schedule is
acceptable from the viewpoint of consistency ofacceptable from the viewpoint of consistency of
the database
 Implicit assumption- no transaction failures
 However if a transaction fails then we need to
undo the effect of this transaction to ensure the
atomicity propertyy p p y
 If concurrent execution is allowed then a
transaction may depend on some other
t titransaction
 So in the case of a failure, if a transaction is aborted
then the dependent transaction may also be aborted
3
RECOVERABLE SCHEDULES
 Let’s consider the following schedule (Schedule 11)
 It is not recoverable if T9 commits immediately after the
read
 If T8 aborts, T9 would have read (and possibly shown to
the user) an inconsistent database state. Hence,
database must ensure that schedules are recoverable.
 Recoverable schedule — if a transaction Tj reads a
data item previously written by a transaction Ti , then
the commit operation of Ti appears before the commiti
operation of Tj
4
CASCADING ROLLBACKS
 Cascading rollback – a single transaction failure leads
to a series of transaction rollbacks.
 Consider the following schedule where none of the
transactions have yet committed (so the schedule is
recoverable)
 If T10 fails, T11 and T12 must also be rolled back.
 Can lead to the undoing of a significant amount of work Can lead to the undoing of a significant amount of work
 How to avoid cascading rollbacks?
5
CASCADELESS SCHEDULES
 Cascadeless schedules — cascading rollbacksg
cannot occur; for each pair of transactions Ti and
Tj such that Tj reads a data item previously
written by T the commit operation of T appearswritten by Ti, the commit operation of Ti appears
before the read operation of Tj.
 Every cascadeless schedule is also recoverable
 It is desirable to restrict the schedules to those
that are cascadeless
6
CONCURRENCY CONTROLCONCURRENCY CONTROL
 A database must provide a mechanism that will
ensure that all possible schedules are
 either conflict or view serializable, and
 are recoverable and preferably cascadeless
 A policy in which only one transaction can execute at A policy in which only one transaction can execute at
a time generates serial schedules, but provides a
poor degree of concurrency
 Testing a schedule for serializability after it has
executed is a little too late!
 Goal to develop concurrency control protocols that Goal – to develop concurrency control protocols that
will assure serializability.
7
LOCK-BASED PROTOCOLS
 A lock is a mechanism to control concurrent access to
a data item
 Data items can be locked in two modes :
1. exclusive (X) mode. Data item can be both read as
well as written X lock is requested using lock Xwell as written. X-lock is requested using lock-X
instruction.
2. shared (S) mode. Data item can only be read. S-
lock is requested using lock-S instruction.
 Lock requests are made to concurrency-control
manager
 Transaction can proceed only after request is granted.
8
LOCK-BASED PROTOCOLS (CONT.)
 Lock-compatibility matrix
 A transaction may be granted a lock on an item if the
requested lock is compatible with locks already held on
the item by other transactions
 Any number of transactions can hold shared locks on an
item,
 but if any transaction holds an exclusive lock on the item no other
transaction may hold any lock on the item.
 If a lock cannot be granted, the requesting transaction is
made to wait till all incompatible locks held by other
transactions have been released
h l k i h d the lock is then granted. 9
LOCK-BASED PROTOCOLS (CONT.)
 Example of a transaction performing locking:
T1: lock-X(B);
read (B);
B:=B-50;
write (B)
unlock(B);
l k X(A)lock-X(A);
read (A);
A:=A+50;A:=A+50;
write (A);
unlock(A);
10
( );
LOCK-BASED PROTOCOLS (CONT.)
 Example of another transaction performing locking:
T2: lock-S(A);
d (A)read (A);
unlock(A);
lock-S(B);
read (B);
unlock(B);
display(A+B)display(A+B)
 Locking as above is not sufficient to guarantee serializability
— if A and B get updated in-between the read of A and B,
the displayed sum would be wrongthe displayed sum would be wrong.
 A locking protocol is a set of rules followed by all
transactions while requesting and releasing locks. Locking
protocols restrict the set of possible schedulesprotocols restrict the set of possible schedules. 11
PITFALLS OF LOCK-BASED PROTOCOLS
 Consider the partial schedule
 Neither T3 nor T4 can make progress — executing lock-
S(B) causes T4 to wait for T3 to release its lock on B,S( ) ca ses 4 o wa o 3 o e ease s oc o ,
while executing lock-X(A) causes T3 to wait for T4 to
release its lock on A.
 Such a situation is called a deadlock.
 To handle a deadlock one of T3 or T4 must be rolled back
and its locks released.
12
PITFALLS OF LOCK-BASED PROTOCOLS (CONT.)
 The potential for deadlock exists in most locking
protocols.
 Deadlocks are a necessary evil
 Preferable to inconsistent states
13
STARVATION
 Starvation is also possible if concurrency controlp y
manager is badly designed.
 For example:
 A transaction may be waiting for an X-lock on an
item, while a sequence of other transactions request
and are granted an S-lock on the same item.
 The same transaction is repeatedly rolled back due to
deadlocks.
14
GRANTING OF LOCKS
 Concurrency control manager can be designed toy g g
prevent starvation
 When a transaction Ti requests a lock on data
it Q i ti l d M thitem Q in a particular mode M, the concurrency
control manager grants the lock provided that –
 There is no other transaction holding a lock on Q in ag Q
mode that conflicts with M
 There is no other transaction that is waiting for a
lock on Q and that made its lock request before Tilock on Q and that made its lock request before Ti
 Thus a lock request will never get blocked by a
lock request that is made later
15
THE TWO-PHASE LOCKING PROTOCOL
 This is a protocol which ensures conflict-serializable
schedule.
Thi t l i l k d l k t i t This protocol issues lock and unlock requests in two
phases:
 Phase 1: Growing Phase Phase 1: Growing Phase
 A transaction may obtain locks, but may not release locks
 Phase 2: Shrinking Phase
 A transaction may release locks but may not obtain any
new locks
 The transactions can be serialized in the order of their The transactions can be serialized in the order of their
lock points (i.e. the point where a transaction
acquired its final lock).q )
16
THE TWO-PHASE LOCKING PROTOCOL (CONT.)
 Two-phase locking does not ensure freedom from
deadlocks
17
TWO PHASE LOCKING PROTOCOL (CONTD.)
 Cascading roll-back is also possible under two-
phase locking.p g
T5 T6 T7
lock-X(A)
read (A)
lock-S(B)
d (B)read (B)
write (A)
unlock (A)
lock-X(A)lock-X(A)
read (A)
write (A)
unlock (A)
18lock-S(A)
read (A)
 Cascading rollbacks can be avoided byg y
 Following a modified protocol called strict two-
phase locking. Here a transaction must hold all its
l i l k till it it / b texclusive locks till it commits/aborts.
 Rigorous two-phase locking is even stricter: here
all locks are held till commit/abort. In this protocol
transactions can be serialized in the order in which
they commit.
19
LOCK CONVERSIONS
 Two-phase locking with lock conversions:
– First Phase:
 can acquire a lock-S on item
 can acquire a lock-X on item
 can convert a lock S to a lock X (upgrade) can convert a lock-S to a lock-X (upgrade)
– Second Phase:
 can release a lock-S
 can release a lock-X
 can convert a lock-X to a lock-S (downgrade)
Thi t l i li bilit B t till li This protocol assures serializability. But still relies
on the programmer to insert the various locking
instructions.
20
 Consider the following two transactions-g
T8 T9
Read(A1)
Read(A2)
…
Read(A )
Read(A1)
Read(A2)
display(A1+ A2)
Read(An)
Write(A1)
If two phase locking protocol is employed, then T8 must lock A1
in exclusive mode
However, T8 needs the exclusive mode lock only at the end
So initially it can lock A in shared mode and later upgrade to 21
So initially it can lock A1 in shared mode and later upgrade to
exclusive mode
This will allow more concurrency
IMPLEMENTATION OF LOCKING
 A lock manager can be implemented as a separate
process to which transactions send lock and unlock
requestsrequests
 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
requestsrequests
 The lock table is usually implemented as an in-memory
hash table indexed on the name of the data item being
l k dlocked 22
LOCK TABLE
Bl k t l i di t t d l k 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 locksearlier locks
 Unlock requests result in the request
being deleted, and later requests are
checked to see if they can now be
g t dgranted
 If transaction aborts, all waiting or
granted requests of the transaction are
deletedGranted
 lock manager may keep a list of
locks held by each transaction, to
implement this efficiently
Waiting
23
GRAPH-BASED PROTOCOLS
 Graph-based protocols are an alternative to two-p p
phase locking
 Impose a partial ordering  on the set D = {d1, d2
d } f ll d t it,..., dh} of all data items.
 If di  dj then any transaction accessing both di and
dj must access di before accessing dj.j j
 Implies that the set D may now be viewed as a
directed acyclic graph, called a database graph.
 The tree-protocol is a simple kind of graph The tree-protocol is a simple kind of graph
protocol.
24
TREE PROTOCOL
1. Only exclusive locks are
allowed.
2. The first lock by Ti may be
on any data item.
Subsequently, a data Qq y, Q
can be locked by Ti only if
the parent of Q is
currently locked by Ti.
3. Data items may be
unlocked at any time.
4. A data item that has been
locked and unlocked by Ti
cannot subsequently be
relocked by Ti
25
SCHEDULE USING THE TREE PROTOCOL
T10 T11 T12 T13T10 T11 T12 T13
Lock-X(B)
Lock-X(D)
Lock-X(H)
Lock-X(E)
Lock-X(D)
Unlock(B)
Unlock (D)
Unlock(B)
Unlock (E)
Lock-X(B)
Lock-X(E)
Lock-X(G)
Unlock (D)
Unlock (H)
Lock-X(D)
U l k (E)
Lock X(D)
Lock-X(H)
Unlock (D)
Unlock (H)
Unlock (G)
Unlock (E)
Unlock (B)
26
GRAPH-BASED PROTOCOLS (CONT.)
 Advantages:
 The tree protocol ensures conflict serializability
 No rollback is required as it is free from deadlock No rollback is required as it is free from deadlock
 Unlocking may occur earlier in the tree-locking protocol
than in the two-phase locking protocol.
 shorter waiting times, and increase in concurrency
 Disadvantages:
 Protocol does not guarantee recoverability or cascade
freedom
N d i d i d d i Need to introduce commit dependencies to ensure
recoverability
 Transactions may have to lock data items that they do
not access.
 increased locking overhead, and additional waiting time
 potential decrease in concurrency
27
DEADLOCK HANDLING
 Consider the following two transactions:
T1: write (A) T2: write(B)
write(B) write(A)
 Schedule with deadlock
T1 T2
lock-X on A
write (A)
lock-X on B
write (B)write (B)
wait for lock-X on A
wait for lock-X on B
28
DEADLOCK HANDLING
 System is deadlocked if there is a set of
transactions such that every transaction in the set
i i i f h i i his waiting for another transaction in the set
 Deadlock prevention protocols ensure that the
system will never enter into a deadlock statesystem will never enter into a deadlock state.
 Some prevention strategies :
 Require that each transaction locks all its data items
before it begins execution (predeclaration).
 Impose partial ordering of all data items and require
that a transaction can lock data items only in the order
specified by the partial order (graph-based protocol).
29
MORE DEADLOCK PREVENTION STRATEGIES
 Following schemes use transaction timestamps for
the sake of deadlock prevention alone.
 wait-die scheme — non-preemptive
 older transaction may wait for younger one to release
data item. Younger transactions never wait for olderdata item. Younger transactions never wait for older
ones; they are rolled back instead.
 a transaction may die several times before acquiring
needed data itemneeded data item
 wound-wait scheme — preemptive
 older transaction wounds (forces rollback) of younger
transaction instead of waiting for it. Younger
transactions may wait for older ones
 may be fewer rollbacks than wait-die scheme
30
DEADLOCK PREVENTION (CONT.)
 Both in wait-die and in wound-wait schemes
 A rolled back transaction is restarted with its
original timestamp.
 Older transactions thus have precedence over newer Older transactions thus have precedence over newer
ones, and starvation is hence avoided.
 Timeout-Based Schemes :
 A transaction waits for a lock only for a specified
amount of time. After that, the wait times out and
the transaction is rolled back.
 Thus deadlocks are not possible
 Simple to implement; but starvation is possible.
 Also difficult to determine good value of the timeout Also difficult to determine good value of the timeout
interval.
31
DEADLOCK DETECTION
 Deadlocks can be described as a wait-for graph, which
consists of a pair G = (V,E),
 V is a set of vertices (all the transactions in the system)
 E is a set of edges; each element is an ordered pair Ti Tj.
 If Ti  Tj is in E, then there is a directed edge from Ti
to Tj, implying that Ti is waiting for Tj to release a
data item.
 When Ti requests a data item currently being held by
Tj, then the edge Ti  Tj is inserted in the wait-for
graph. This edge is removed only when Tj is no longer
h ldi d i d d b Tholding a data item needed by Ti.
 The system is in a deadlock state if and only if the
wait-for graph has a cycle. Must invoke a deadlock-
d i l i h i di ll l k f ldetection algorithm periodically to look for cycles. 32
DEADLOCK DETECTION (CONT.)
Wait for graph without a cycle Wait-for graph with a cycleWait-for graph without a cycle Wait for graph with a cycle
33
DEADLOCK RECOVERY
 When deadlock is detected
 The system must recover from the deadlock state
 The most common solution is to roll back one or more
transactionstransactions
 Actions to be taken
 Selection of a victim
 Rollback
 Starvation
34
SELECTION OF A VICTIM
 Some transactions will have to be rolled back
(made a victim) to break deadlock
 Select that transaction as victim that will incur
minimum cost while rolling backminimum cost while rolling back
 Depends on
 How long the transaction has computed, and how
much longer the transaction will compute before itmuch longer the transaction will compute before it
completes it designated task
 How many data items the transaction has used
Ho man more data items the transactions needs How many more data items the transactions needs
for it to complete
 How many transactions will be involved in the
rollbackrollback 35
ROLLBACK
 How far a particular transaction should be rolledp
back
 Total rollback: abort the transaction and restarts it
P ti l llb k llb k th t ti l f Partial rollback: rollback the transaction only as far
as necessary to break deadlock
 The system must maintain some additional information –
sequence of lock requests/grants updates performed by thesequence of lock requests/grants, updates performed by the
transaction, etc.
36
STARVATION
 The selection of a victim is based on cost factor
 Starvation happens if same transaction is always chosen
as victim
 As a result this transaction never completes its As a result this transaction never completes its
designated task
 A transaction can be picked as a victim only a finite no.
f tiof times
 A possible solution:
 Include the number of rollbacks in the cost factor to avoid
starvation
37
TIMESTAMP-BASED PROTOCOLS
 Each transaction is issued a timestamp when it enters the
system.
 If an old transaction Ti has timestamp TS(Ti), a new transaction Tj
is assigned timestamp TS(Tj) such that TS(Ti) <TS(Tj).
 Two methods for implementing the timestamp Two methods for implementing the timestamp
 Use the value of the system clock as timestamp
 Use a logical counter that is incremented whenever a new
itransaction enters
 The protocol manages the concurrent execution in such a
manner so that the timestamps determine the serializabilityp y
order
38
TIMESTAMP-BASED PROTOCOLS (CONT.)
 The protocol maintains for each data item Q two
ti t ltimestamp values:
 W-timestamp(Q) is the largest timestamp of any
transaction that executed write(Q) successfully.(Q) y
 R-timestamp(Q) is the largest timestamp of any
transaction that executed read(Q) successfully.
Th i d d h These timestamps are updated whenever a new
read(Q) or write(Q) instruction is executed
39
TIMESTAMP-BASED PROTOCOLS (CONT.)
 The timestamp ordering protocol ensures that any
conflicting read and write operations are executed
i i din timestamp order.
 Suppose a transaction Ti issues a read(Q)
1 If TS(Ti) < W-timestamp(Q) then Ti needs to read a1. If TS(Ti) < W-timestamp(Q), then Ti needs to read a
value of Q that was already overwritten.
 Hence, the read operation is rejected, and Ti is rolled back.
2 If TS(T ) W timestamp(Q) then the read operation is2. If TS(Ti) W-timestamp(Q), then the read operation is
executed, and R-timestamp(Q) is set to max(R-
timestamp(Q), TS(Ti)).
40
TIMESTAMP-BASED PROTOCOLS (CONT.)
 Suppose that transaction Ti issues write(Q).
1. If TS(Ti) < R-timestamp(Q), then the value of Q that Ti
i d i d d i l d his producing was needed previously, and the system
assumed that that value would never be produced.
 Hence, the write operation is rejected, and Ti is rolled back.
2. If TS(Ti) < W-timestamp(Q), then Ti is attempting to
write an obsolete value of Q.
 Hence, this write operation is rejected, and Ti is rolled back.
3. Otherwise, the write operation is executed, and W-
timestamp(Q) is set to TS(Ti).
41
 T14  T15
 Read (B)
 Read (A)
Di l (A B)
 Read (B)
 B:=B-50;
W it (B) Display (A+B)  Write (B)
 Read (A)
 A:=A+50;;
 Write (A)
TS(T14) < TS(T15)
42
SCHEDULE USING TIMESTAMP PROTOCOL
T14 T15
Read (B)
Read (B)
B:=B-50;
W it (B)
Read (A)
Display (A+B)
Write (B)
Read (A)
p y ( )
A:=A+50;
Write (A)
43
CORRECTNESS OF TIMESTAMP-ORDERING PROTOCOL
 Th ti t d i t l t The timestamp-ordering protocol guarantees
serializability since all the arcs in the precedence
graph are of the form:
transaction
ith ll
transaction
with smaller
timestamp
with larger
timestamp
Thus, there will be no cycles in the precedence graph
 Timestamp protocol ensures freedom from deadlock Timestamp protocol ensures freedom from deadlock
as no transaction ever waits.
 But the schedule may not be cascade-free, and may
not even be recoverablenot even be recoverable. 44
RECOVERABILITY AND CASCADE FREEDOM
 Solution 1:
 A transaction is structured such that its writes are all
performed at the end of its processingperformed at the end of its processing
 All writes of a transaction form an atomic action; no
transaction may execute while a transaction is being
writtenwritten
 A transaction that aborts is restarted with a new
timestamp
 Solution 2: Limited form of locking: wait for data to Solution 2: Limited form of locking: wait for data to
be committed before reading it
 Solution 3: Use commit dependencies to ensure
bilitrecoverability
45
THOMAS’ WRITE RULE
T1 T2  TS(T1)<TS(T2)
Read (Q)
Write (Q)
Write (Q)
 As per timestamp protocol
the rollback of T1 is
required
 T2 has already written Q T2 has already written Q
and T1 is attempting to
write one that will never to
be read
 Any Ti with TS(Ti)<TS(T2) Any Ti with TS(Ti)<TS(T2)
that attempts read(Q) will
be rolled back, since
TS(Ti)<W-timestamp(Q)
A Tj i h TS(Tj) TS(T2) Any Tj with TS(Tj)>TS(T2)
will be allowed to read the
value Q written by T2
rather than T1
46
Leads to a modified version of timestamp protocol
THOMAS’ WRITE RULE
 Modified version of the timestamp-ordering
protocol
 When Ti attempts to write data item Q, if TS(Ti)
< W-timestamp(Q), then Ti is attempting to write
an obsolete value of {Q}an obsolete value of {Q}.
 Rather than rolling back Ti as the timestamp
ordering protocol would have done, this {write}
ti b i doperation can be ignored.
 Results in a view equivalent schedule
 Otherwise this protocol is the same as thep
timestamp ordering protocol
 Thomas' Write Rule allows greater potential
concurrency 47
RECOVERY SYSTEM
 A computer system is subject to failure from ap y j
variety of causes
 In any failure data may be lost
 So a good database system must take actions in
advance to ensure that the atomicity and
durability properties are preserveddurability properties are preserved
 Recovery system is an integral part of a database
system
 The recovery system does the followings-
 restore the database to a consistent state
 provide high availability provide high availability 48
FAILURE CLASSIFICATIONFAILURE CLASSIFICATION
 Transaction failure :
 Logical errors: transaction cannot complete due to some
i t l ditiinternal error condition
 System errors: the database system must terminate an
active transaction due to an error condition (e.g., deadlock)
 System crash: a power failure or other hardware or System crash: a power failure or other hardware or
software failure causes the system to crash.
 Fail-stop assumption: non-volatile storage contents are
assumed not to be corrupted by system crashassumed not to be corrupted by system crash
 Database systems have numerous integrity checks to prevent
corruption of disk data
 Disk failure: a head crash or similar disk failure
destroys all or part of disk storage
 Destruction is assumed to be detectable: disk drives use
checksums to detect failures
49
RECOVERY ALGORITHMS
 Recovery algorithms are techniques to ensurey g q
database consistency and transaction atomicity
and durability despite failures
R l ith h t t Recovery algorithms have two parts
1. Actions taken during normal transaction processing
 to ensure enough information exists to recover from
failures
2. Actions taken after a failure
 to recover the database contents to a state that ensures
atomicity, consistency and durability
50
STORAGE STRUCTURE
 Volatile storage:g
 does not survive system crashes
 examples: main memory, cache memory
N l il Nonvolatile storage:
 survives system crashes
 examples: disk, tape, flash memory,examples: disk, tape, flash memory,
non-volatile (battery backed up) RAM
 Stable storage:
hi l f f h i ll f il a mythical form of storage that survives all failures
 approximated by maintaining multiple copies on
distinct nonvolatile media
51
RECOVERY AND ATOMICITY
 Modifying the database without ensuring that the
transaction will commit may leave the database in an
inconsistent state.
 Consider transaction Ti that transfers $50 from
account A to account B; goal is either to perform allaccount A to account B; goal is either to perform all
database modifications made by Ti or none at all.
 Several output operations may be required for Ti (to
output A and B). A failure may occur after one of these
modifications have been made but before all of them
are made.are made.
52
RECOVERY AND ATOMICITY (CONT.)
 To ensure atomicity despite failuresy p
 we first output information describing the
modifications to stable storage without modifying the
database itselfdatabase itself.
 We study the following approach:
 log-based recovery
 For simplicity, we assume that transactions run
serially
53
LOG-BASED RECOVERY
 A log is kept on stable storage
 The log is a sequence of records which record all the update
activities in the databaseactivities in the database
 The fields of a log record
 Transaction identifier
 Data item identifier
 Old value
 New value
54
LOG BASED RECOVERY (CONTD )LOG-BASED RECOVERY (CONTD.)
 Update log record:
<T X V V > <Ti, Xj,V1,V2>
 Transaction Ti has performed a write on data item Xj. Xj had old
value V1 before the write, and will have valueV2 after the write.
 Special log records to record significant events in Special log records to record significant events in
transaction processing:
 <Ti start>
T ti T h t t d Transaction Ti has started
 <Ti commit>
 Transaction Ti has committed
<T b t> <Ti abort>
 Transaction Ti has aborted
55
 We assume for now that log records are writteng
directly to stable storage (that is, they are not
buffered)
 Two approaches using logs Two approaches using logs
 Deferred database modification
 Immediate database modification
56
DEFERRED DATABASE MODIFICATION
 The deferred database modification scheme
records all modifications to the log, but defers all
th it t ft ti l itthe writes to after partial commit.
 Transaction starts by writing <Ti start> record to
log.
 A write(X) operation results in a log record <Ti,
X, V> being written, where V is the new value for X
 Note: old value is not needed for this schemeNote: old value is not needed for this scheme
 The write is not performed on X at this time, but is
deferred.
Wh T ti ll it <T it> i When Ti partially commits, <Ti commit> is
written to the log
 Finally, the log records are read and used to
actually execute the previously deferred writes. 57
DEFERRED DATABASE MODIFICATION (CONT.)
 During recovery after a crash, a transaction needs to
be redone if and only if both <Ti start> and<Ti
it th i th lcommit> are there in the log.
 Redoing a transaction Ti ( redo Ti) sets the value of
all data items updated by the transaction to the newall data items updated by the transaction to the new
values.
 redo operation must be idempotent
 Executing it several times must be equivalent to
executing it once
 Crashes can occur while Crashes can occur while
 the transaction is executing the original updates, or
 while recovery action is being taken
58
 Example transactions T0 and T1 (T0 executesp 0 1 ( 0
before T1):
T0: read (A) T1 : read (C)
A:= A - 50 C:=C- 100
write (A) write (C)
d (B)read (B)
B:= B + 50
write (B)write (B)
59
DEFERRED DATABASE MODIFICATION (CONT.)
 Below we show the log as it appears at three instants
of time.
 If log on stable storage at time of crash is as in case:g g
(a) No redo actions need to be taken
(b) redo(T0) must be performed since <T0 commit> is present
(c) redo(T0) must be performed followed by redo(T1) since
<T0 commit> and <T1 commit> are presentT0 commit and T1 commit are present
60
IMMEDIATE DATABASE MODIFICATION
 The immediate database modification scheme
allows database updates of an uncommitted
transaction to be made as the writes are issuedtransaction to be made as the writes are issued
 since undoing may be needed, update logs must have both
old value and new value
 Update log record must be written before database Update log record must be written before database
item is written
 We assume that the log record is output directly to stable
storageg
 Can be extended to postpone log record output, so long as
prior to execution of an output(B) operation for a data
block B, all log records corresponding to items B must be
flushed to stable storageflushed to stable storage
 Output of updated blocks can take place at any time
before or after transaction commit
O d i hi h bl k t t b diff t Order in which blocks are output can be different
from the order in which they are written.
61
IMMEDIATE DATABASE MODIFICATION EXAMPLE
Log Write Output
<T0 start>
<T0, A, 1000, 950>
<To, B, 2000, 2050>o, , ,
A = 950
B = 2050
<T0 commit>
<T start><T1 start>
<T1, C, 700, 600>
C = 600
BB, BC
<T it><T1 commit>
BA
 Note: BX denotes block containing X.
62
IMMEDIATE DATABASE MODIFICATION (CONT.)
 Recovery procedure has two operations instead of one:
 undo(Ti) restores the value of all data items updated by Ti
to their old values going backwards from the last logto their old values, going backwards from the last log
record for Ti
 redo(Ti) sets the value of all data items updated by Ti to
the new values going forward from the first log record forthe new values, going forward from the first log record for
Ti
 Both operations must be idempotent
That is even if the operation is executed multiple times That is, even if the operation is executed multiple times
the effect is the same as if it is executed once
 Needed since operations may get re-executed during recovery
63
IMMEDIATE DATABASE MODIFICATION (CONT.)( )
 When recovering after failure:
 Transaction Ti needs to be undone if the log containsa sac o i ee s o e o e e og co a s
the record
<Ti start>, but does not contain the record <Ti
commit>.
 Transaction Ti needs to be redone if the log contains
both the record <Ti start> and the record <Ti commit>.
64
IMMEDIATE DB MODIFICATION RECOVERY
EXAMPLEEXAMPLE
Below we show the log as it appears at three instants of time.
Recovery actions in each case above are:
(a) undo (T0): B is restored to 2000 and A to 1000.
(b) d (T ) d d (T ) C i t d t 700 d th A d B(b) undo (T1) and redo (T0): C is restored to 700, and then A and B are
set to 950 and 2050 respectively.
(c) redo (T0) and redo (T1): A and B are set to 950 and 2050
respectively. Then C is set to 600respectively. Then C is set to 600
65
COMMON PROBLEMS IN RECOVERY
PROCEDURE
 Searching the entire log is time-consuming
 Unnecessarily redo transactions which have
l d t t th i d t t th d t balready output their updates to the database
66
CHECKPOINTING
 Streamline recovery procedure by periodicallyy p y p y
performing checkpointing
 It performs the following sequence of operations-
1. Output all log records currently residing in main
memory onto stable storage.
2. Output all modified buffer blocks to the disk.
3. Write a log record < checkpoint> onto stable
storage
 Transactions are not allowed to perform any Transactions are not allowed to perform any
update actions such as writing to buffer block or
writing a log record, while a checkpoint is in
progress 67
CHECKPOINTS (CONT.)
 During recovery we need to consider only the most
recent transaction Ti that started before the
checkpoint, and transactions that started after Ti.checkpoint, and transactions that started after Ti.
1. Scan backwards from end of log to find the most recent
<checkpoint> record
2 Continue scanning backwards till a record <Ti start>2. Continue scanning backwards till a record <Ti start>
is found.
3. Need only to consider the part of log following above
start record. Earlier part of log can be ignored duringp g g g
recovery, and can be erased whenever desired.
4. For all transactions (starting from Ti or later) with no
<Ti commit>, execute undo(Ti). (Done only in case of
immediate modification.)
5. Scanning forward in the log, for all transactions
starting from Ti or later with a <Ti commit>,
t d (T )execute redo(Ti). 68
EXAMPLE OF CHECKPOINTS
Tc
Tf
T1T1
T2
T3
T4
f il
 T1 can be ignored (updates already output to disk
checkpoint system failure
due to checkpoint)
 T2 and T3 redone.
 T undone T4 undone 69

More Related Content

PPTX
Lock based protocols
PPTX
Concurrency control PPT
PPT
Concurrency control ms neeti
PPT
16. Concurrency Control in DBMS
PPTX
Concurrency control!
PPT
Concurrency control
PPT
Locking unit 1 topic 3
Lock based protocols
Concurrency control PPT
Concurrency control ms neeti
16. Concurrency Control in DBMS
Concurrency control!
Concurrency control
Locking unit 1 topic 3

What's hot (20)

PDF
Concurrency Conrol
PPT
Deadlock Detection
PPT
Database concurrency control &amp; recovery (1)
PPTX
concurrency control
PPT
Concurrency control
PPT
Dbms ii mca-ch10-concurrency-control-2013
PPTX
2 phase locking protocol DBMS
PPT
Database management system chapter16
DOCX
Concurrency Control Techniques
DOCX
Locking base concurrency control
PPTX
Concurrency control
PPT
concurrency-control
PPTX
Deadlock dbms
PPTX
Concurrency Control in Database Management System
PPTX
Unit 6
PPTX
Transaction and concurrency control
PPT
Databases: Locking Methods
PPTX
Characteristics Schedule based on Recover-ability & Serial-ability
Concurrency Conrol
Deadlock Detection
Database concurrency control &amp; recovery (1)
concurrency control
Concurrency control
Dbms ii mca-ch10-concurrency-control-2013
2 phase locking protocol DBMS
Database management system chapter16
Concurrency Control Techniques
Locking base concurrency control
Concurrency control
concurrency-control
Deadlock dbms
Concurrency Control in Database Management System
Unit 6
Transaction and concurrency control
Databases: Locking Methods
Characteristics Schedule based on Recover-ability & Serial-ability
Ad

Similar to Cs501 concurrency (20)

PDF
PPT-concurrency Control database management system DBMS concurrent control (U...
PPTX
recoverability and serializability dbms
PPTX
CHapter four database managementm04.pptx
PPTX
db unit 4 dbms protocols in transaction
PPTX
BCT 2312 - Chapter 3 - Concurrency Control Techniques in DBMSs.pptx
PPT
deadlock and locking - dbms
PPTX
Concurrency control
PPT
Data concurrency means that many users can access data at the same time
PPTX
Unit 4 Concurrency control.pptx dbms lovely
PPT
Concurrency Control database management system
PPTX
Concurrency Control.pptx
PPT
5-Chapter Five - Concurrenc.ppt
PPTX
Concurrency Control in Databases.Database management systems
PDF
Concurrency Control in Database Management System
PDF
Module 5 part-2 concurrency control in dbms
PPTX
DBMS Unit-5.4. Concurrency Control.pptx it is a PPT on the topic of concurren...
PPTX
Concurrency control
PPTX
Chapter 4-Concrruncy controling techniques.pptx
PPTX
2 con control
PPTX
Transaction management DBMS
PPT-concurrency Control database management system DBMS concurrent control (U...
recoverability and serializability dbms
CHapter four database managementm04.pptx
db unit 4 dbms protocols in transaction
BCT 2312 - Chapter 3 - Concurrency Control Techniques in DBMSs.pptx
deadlock and locking - dbms
Concurrency control
Data concurrency means that many users can access data at the same time
Unit 4 Concurrency control.pptx dbms lovely
Concurrency Control database management system
Concurrency Control.pptx
5-Chapter Five - Concurrenc.ppt
Concurrency Control in Databases.Database management systems
Concurrency Control in Database Management System
Module 5 part-2 concurrency control in dbms
DBMS Unit-5.4. Concurrency Control.pptx it is a PPT on the topic of concurren...
Concurrency control
Chapter 4-Concrruncy controling techniques.pptx
2 con control
Transaction management DBMS
Ad

More from Kamal Singh Lodhi (16)

PDF
Introduction to Data Structure
PDF
Stack Algorithm
PPT
Data Structure (MC501)
PDF
Cs501 trc drc
PDF
Cs501 transaction
PDF
Cs501 rel algebra
PPT
Cs501 mining frequentpatterns
PDF
Cs501 intro
PDF
Cs501 fd nf
PDF
Cs501 dm intro
PDF
Cs501 data preprocessingdw
PPT
Cs501 cluster analysis
PPT
Cs501 classification prediction
PPTX
Attribute Classification
PDF
Real Time ImageVideo Processing with Applications in Face Recognition
PPTX
Flow diagram
Introduction to Data Structure
Stack Algorithm
Data Structure (MC501)
Cs501 trc drc
Cs501 transaction
Cs501 rel algebra
Cs501 mining frequentpatterns
Cs501 intro
Cs501 fd nf
Cs501 dm intro
Cs501 data preprocessingdw
Cs501 cluster analysis
Cs501 classification prediction
Attribute Classification
Real Time ImageVideo Processing with Applications in Face Recognition
Flow diagram

Recently uploaded (20)

PDF
Computing-Curriculum for Schools in Ghana
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Pre independence Education in Inndia.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
Cell Types and Its function , kingdom of life
PPTX
master seminar digital applications in india
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Lesson notes of climatology university.
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Computing-Curriculum for Schools in Ghana
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Pre independence Education in Inndia.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Cell Types and Its function , kingdom of life
master seminar digital applications in india
GDM (1) (1).pptx small presentation for students
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Microbial disease of the cardiovascular and lymphatic systems
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Lesson notes of climatology university.
VCE English Exam - Section C Student Revision Booklet
O7-L3 Supply Chain Operations - ICLT Program
3rd Neelam Sanjeevareddy Memorial Lecture.pdf

Cs501 concurrency

  • 1. CS501: DATABASES AND DATA MINING Concurrency and Recovery Control1
  • 2. OTHER NOTIONS OF SERIALIZABILITY  The schedule shown produces same outcome as the serial schedule < T T >schedule < T1, T5 >  yet is not conflict equivalent or view i l t t itequivalent to it. 2
  • 3. RECOVERABILITYRECOVERABILITY  So far we have seen whether a schedule is acceptable from the viewpoint of consistency ofacceptable from the viewpoint of consistency of the database  Implicit assumption- no transaction failures  However if a transaction fails then we need to undo the effect of this transaction to ensure the atomicity propertyy p p y  If concurrent execution is allowed then a transaction may depend on some other t titransaction  So in the case of a failure, if a transaction is aborted then the dependent transaction may also be aborted 3
  • 4. RECOVERABLE SCHEDULES  Let’s consider the following schedule (Schedule 11)  It is not recoverable if T9 commits immediately after the read  If T8 aborts, T9 would have read (and possibly shown to the user) an inconsistent database state. Hence, database must ensure that schedules are recoverable.  Recoverable schedule — if a transaction Tj reads a data item previously written by a transaction Ti , then the commit operation of Ti appears before the commiti operation of Tj 4
  • 5. CASCADING ROLLBACKS  Cascading rollback – a single transaction failure leads to a series of transaction rollbacks.  Consider the following schedule where none of the transactions have yet committed (so the schedule is recoverable)  If T10 fails, T11 and T12 must also be rolled back.  Can lead to the undoing of a significant amount of work Can lead to the undoing of a significant amount of work  How to avoid cascading rollbacks? 5
  • 6. CASCADELESS SCHEDULES  Cascadeless schedules — cascading rollbacksg cannot occur; for each pair of transactions Ti and Tj such that Tj reads a data item previously written by T the commit operation of T appearswritten by Ti, the commit operation of Ti appears before the read operation of Tj.  Every cascadeless schedule is also recoverable  It is desirable to restrict the schedules to those that are cascadeless 6
  • 7. CONCURRENCY CONTROLCONCURRENCY CONTROL  A database must provide a mechanism that will ensure that all possible schedules are  either conflict or view serializable, and  are recoverable and preferably cascadeless  A policy in which only one transaction can execute at A policy in which only one transaction can execute at a time generates serial schedules, but provides a poor degree of concurrency  Testing a schedule for serializability after it has executed is a little too late!  Goal to develop concurrency control protocols that Goal – to develop concurrency control protocols that will assure serializability. 7
  • 8. LOCK-BASED PROTOCOLS  A lock is a mechanism to control concurrent access to a data item  Data items can be locked in two modes : 1. exclusive (X) mode. Data item can be both read as well as written X lock is requested using lock Xwell as written. X-lock is requested using lock-X instruction. 2. shared (S) mode. Data item can only be read. S- lock is requested using lock-S instruction.  Lock requests are made to concurrency-control manager  Transaction can proceed only after request is granted. 8
  • 9. LOCK-BASED PROTOCOLS (CONT.)  Lock-compatibility matrix  A transaction may be granted a lock on an item if the requested lock is compatible with locks already held on the item by other transactions  Any number of transactions can hold shared locks on an item,  but if any transaction holds an exclusive lock on the item no other transaction may hold any lock on the item.  If a lock cannot be granted, the requesting transaction is made to wait till all incompatible locks held by other transactions have been released h l k i h d the lock is then granted. 9
  • 10. LOCK-BASED PROTOCOLS (CONT.)  Example of a transaction performing locking: T1: lock-X(B); read (B); B:=B-50; write (B) unlock(B); l k X(A)lock-X(A); read (A); A:=A+50;A:=A+50; write (A); unlock(A); 10 ( );
  • 11. LOCK-BASED PROTOCOLS (CONT.)  Example of another transaction performing locking: T2: lock-S(A); d (A)read (A); unlock(A); lock-S(B); read (B); unlock(B); display(A+B)display(A+B)  Locking as above is not sufficient to guarantee serializability — if A and B get updated in-between the read of A and B, the displayed sum would be wrongthe displayed sum would be wrong.  A locking protocol is a set of rules followed by all transactions while requesting and releasing locks. Locking protocols restrict the set of possible schedulesprotocols restrict the set of possible schedules. 11
  • 12. PITFALLS OF LOCK-BASED PROTOCOLS  Consider the partial schedule  Neither T3 nor T4 can make progress — executing lock- S(B) causes T4 to wait for T3 to release its lock on B,S( ) ca ses 4 o wa o 3 o e ease s oc o , while executing lock-X(A) causes T3 to wait for T4 to release its lock on A.  Such a situation is called a deadlock.  To handle a deadlock one of T3 or T4 must be rolled back and its locks released. 12
  • 13. PITFALLS OF LOCK-BASED PROTOCOLS (CONT.)  The potential for deadlock exists in most locking protocols.  Deadlocks are a necessary evil  Preferable to inconsistent states 13
  • 14. STARVATION  Starvation is also possible if concurrency controlp y manager is badly designed.  For example:  A transaction may be waiting for an X-lock on an item, while a sequence of other transactions request and are granted an S-lock on the same item.  The same transaction is repeatedly rolled back due to deadlocks. 14
  • 15. GRANTING OF LOCKS  Concurrency control manager can be designed toy g g prevent starvation  When a transaction Ti requests a lock on data it Q i ti l d M thitem Q in a particular mode M, the concurrency control manager grants the lock provided that –  There is no other transaction holding a lock on Q in ag Q mode that conflicts with M  There is no other transaction that is waiting for a lock on Q and that made its lock request before Tilock on Q and that made its lock request before Ti  Thus a lock request will never get blocked by a lock request that is made later 15
  • 16. THE TWO-PHASE LOCKING PROTOCOL  This is a protocol which ensures conflict-serializable schedule. Thi t l i l k d l k t i t This protocol issues lock and unlock requests in two phases:  Phase 1: Growing Phase Phase 1: Growing Phase  A transaction may obtain locks, but may not release locks  Phase 2: Shrinking Phase  A transaction may release locks but may not obtain any new locks  The transactions can be serialized in the order of their The transactions can be serialized in the order of their lock points (i.e. the point where a transaction acquired its final lock).q ) 16
  • 17. THE TWO-PHASE LOCKING PROTOCOL (CONT.)  Two-phase locking does not ensure freedom from deadlocks 17
  • 18. TWO PHASE LOCKING PROTOCOL (CONTD.)  Cascading roll-back is also possible under two- phase locking.p g T5 T6 T7 lock-X(A) read (A) lock-S(B) d (B)read (B) write (A) unlock (A) lock-X(A)lock-X(A) read (A) write (A) unlock (A) 18lock-S(A) read (A)
  • 19.  Cascading rollbacks can be avoided byg y  Following a modified protocol called strict two- phase locking. Here a transaction must hold all its l i l k till it it / b texclusive locks till it commits/aborts.  Rigorous two-phase locking is even stricter: here all locks are held till commit/abort. In this protocol transactions can be serialized in the order in which they commit. 19
  • 20. LOCK CONVERSIONS  Two-phase locking with lock conversions: – First Phase:  can acquire a lock-S on item  can acquire a lock-X on item  can convert a lock S to a lock X (upgrade) can convert a lock-S to a lock-X (upgrade) – Second Phase:  can release a lock-S  can release a lock-X  can convert a lock-X to a lock-S (downgrade) Thi t l i li bilit B t till li This protocol assures serializability. But still relies on the programmer to insert the various locking instructions. 20
  • 21.  Consider the following two transactions-g T8 T9 Read(A1) Read(A2) … Read(A ) Read(A1) Read(A2) display(A1+ A2) Read(An) Write(A1) If two phase locking protocol is employed, then T8 must lock A1 in exclusive mode However, T8 needs the exclusive mode lock only at the end So initially it can lock A in shared mode and later upgrade to 21 So initially it can lock A1 in shared mode and later upgrade to exclusive mode This will allow more concurrency
  • 22. IMPLEMENTATION OF LOCKING  A lock manager can be implemented as a separate process to which transactions send lock and unlock requestsrequests  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 requestsrequests  The lock table is usually implemented as an in-memory hash table indexed on the name of the data item being l k dlocked 22
  • 23. LOCK TABLE Bl k t l i di t t d l k 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 locksearlier locks  Unlock requests result in the request being deleted, and later requests are checked to see if they can now be g t dgranted  If transaction aborts, all waiting or granted requests of the transaction are deletedGranted  lock manager may keep a list of locks held by each transaction, to implement this efficiently Waiting 23
  • 24. GRAPH-BASED PROTOCOLS  Graph-based protocols are an alternative to two-p p phase locking  Impose a partial ordering  on the set D = {d1, d2 d } f ll d t it,..., dh} of all data items.  If di  dj then any transaction accessing both di and dj must access di before accessing dj.j j  Implies that the set D may now be viewed as a directed acyclic graph, called a database graph.  The tree-protocol is a simple kind of graph The tree-protocol is a simple kind of graph protocol. 24
  • 25. TREE PROTOCOL 1. Only exclusive locks are allowed. 2. The first lock by Ti may be on any data item. Subsequently, a data Qq y, Q can be locked by Ti only if the parent of Q is currently locked by Ti. 3. Data items may be unlocked at any time. 4. A data item that has been locked and unlocked by Ti cannot subsequently be relocked by Ti 25
  • 26. SCHEDULE USING THE TREE PROTOCOL T10 T11 T12 T13T10 T11 T12 T13 Lock-X(B) Lock-X(D) Lock-X(H) Lock-X(E) Lock-X(D) Unlock(B) Unlock (D) Unlock(B) Unlock (E) Lock-X(B) Lock-X(E) Lock-X(G) Unlock (D) Unlock (H) Lock-X(D) U l k (E) Lock X(D) Lock-X(H) Unlock (D) Unlock (H) Unlock (G) Unlock (E) Unlock (B) 26
  • 27. GRAPH-BASED PROTOCOLS (CONT.)  Advantages:  The tree protocol ensures conflict serializability  No rollback is required as it is free from deadlock No rollback is required as it is free from deadlock  Unlocking may occur earlier in the tree-locking protocol than in the two-phase locking protocol.  shorter waiting times, and increase in concurrency  Disadvantages:  Protocol does not guarantee recoverability or cascade freedom N d i d i d d i Need to introduce commit dependencies to ensure recoverability  Transactions may have to lock data items that they do not access.  increased locking overhead, and additional waiting time  potential decrease in concurrency 27
  • 28. DEADLOCK HANDLING  Consider the following two transactions: T1: write (A) T2: write(B) write(B) write(A)  Schedule with deadlock T1 T2 lock-X on A write (A) lock-X on B write (B)write (B) wait for lock-X on A wait for lock-X on B 28
  • 29. DEADLOCK HANDLING  System is deadlocked if there is a set of transactions such that every transaction in the set i i i f h i i his waiting for another transaction in the set  Deadlock prevention protocols ensure that the system will never enter into a deadlock statesystem will never enter into a deadlock state.  Some prevention strategies :  Require that each transaction locks all its data items before it begins execution (predeclaration).  Impose partial ordering of all data items and require that a transaction can lock data items only in the order specified by the partial order (graph-based protocol). 29
  • 30. MORE DEADLOCK PREVENTION STRATEGIES  Following schemes use transaction timestamps for the sake of deadlock prevention alone.  wait-die scheme — non-preemptive  older transaction may wait for younger one to release data item. Younger transactions never wait for olderdata item. Younger transactions never wait for older ones; they are rolled back instead.  a transaction may die several times before acquiring needed data itemneeded data item  wound-wait scheme — preemptive  older transaction wounds (forces rollback) of younger transaction instead of waiting for it. Younger transactions may wait for older ones  may be fewer rollbacks than wait-die scheme 30
  • 31. DEADLOCK PREVENTION (CONT.)  Both in wait-die and in wound-wait schemes  A rolled back transaction is restarted with its original timestamp.  Older transactions thus have precedence over newer Older transactions thus have precedence over newer ones, and starvation is hence avoided.  Timeout-Based Schemes :  A transaction waits for a lock only for a specified amount of time. After that, the wait times out and the transaction is rolled back.  Thus deadlocks are not possible  Simple to implement; but starvation is possible.  Also difficult to determine good value of the timeout Also difficult to determine good value of the timeout interval. 31
  • 32. DEADLOCK DETECTION  Deadlocks can be described as a wait-for graph, which consists of a pair G = (V,E),  V is a set of vertices (all the transactions in the system)  E is a set of edges; each element is an ordered pair Ti Tj.  If Ti  Tj is in E, then there is a directed edge from Ti to Tj, implying that Ti is waiting for Tj to release a data item.  When Ti requests a data item currently being held by Tj, then the edge Ti  Tj is inserted in the wait-for graph. This edge is removed only when Tj is no longer h ldi d i d d b Tholding a data item needed by Ti.  The system is in a deadlock state if and only if the wait-for graph has a cycle. Must invoke a deadlock- d i l i h i di ll l k f ldetection algorithm periodically to look for cycles. 32
  • 33. DEADLOCK DETECTION (CONT.) Wait for graph without a cycle Wait-for graph with a cycleWait-for graph without a cycle Wait for graph with a cycle 33
  • 34. DEADLOCK RECOVERY  When deadlock is detected  The system must recover from the deadlock state  The most common solution is to roll back one or more transactionstransactions  Actions to be taken  Selection of a victim  Rollback  Starvation 34
  • 35. SELECTION OF A VICTIM  Some transactions will have to be rolled back (made a victim) to break deadlock  Select that transaction as victim that will incur minimum cost while rolling backminimum cost while rolling back  Depends on  How long the transaction has computed, and how much longer the transaction will compute before itmuch longer the transaction will compute before it completes it designated task  How many data items the transaction has used Ho man more data items the transactions needs How many more data items the transactions needs for it to complete  How many transactions will be involved in the rollbackrollback 35
  • 36. ROLLBACK  How far a particular transaction should be rolledp back  Total rollback: abort the transaction and restarts it P ti l llb k llb k th t ti l f Partial rollback: rollback the transaction only as far as necessary to break deadlock  The system must maintain some additional information – sequence of lock requests/grants updates performed by thesequence of lock requests/grants, updates performed by the transaction, etc. 36
  • 37. STARVATION  The selection of a victim is based on cost factor  Starvation happens if same transaction is always chosen as victim  As a result this transaction never completes its As a result this transaction never completes its designated task  A transaction can be picked as a victim only a finite no. f tiof times  A possible solution:  Include the number of rollbacks in the cost factor to avoid starvation 37
  • 38. TIMESTAMP-BASED PROTOCOLS  Each transaction is issued a timestamp when it enters the system.  If an old transaction Ti has timestamp TS(Ti), a new transaction Tj is assigned timestamp TS(Tj) such that TS(Ti) <TS(Tj).  Two methods for implementing the timestamp Two methods for implementing the timestamp  Use the value of the system clock as timestamp  Use a logical counter that is incremented whenever a new itransaction enters  The protocol manages the concurrent execution in such a manner so that the timestamps determine the serializabilityp y order 38
  • 39. TIMESTAMP-BASED PROTOCOLS (CONT.)  The protocol maintains for each data item Q two ti t ltimestamp values:  W-timestamp(Q) is the largest timestamp of any transaction that executed write(Q) successfully.(Q) y  R-timestamp(Q) is the largest timestamp of any transaction that executed read(Q) successfully. Th i d d h These timestamps are updated whenever a new read(Q) or write(Q) instruction is executed 39
  • 40. TIMESTAMP-BASED PROTOCOLS (CONT.)  The timestamp ordering protocol ensures that any conflicting read and write operations are executed i i din timestamp order.  Suppose a transaction Ti issues a read(Q) 1 If TS(Ti) < W-timestamp(Q) then Ti needs to read a1. If TS(Ti) < W-timestamp(Q), then Ti needs to read a value of Q that was already overwritten.  Hence, the read operation is rejected, and Ti is rolled back. 2 If TS(T ) W timestamp(Q) then the read operation is2. If TS(Ti) W-timestamp(Q), then the read operation is executed, and R-timestamp(Q) is set to max(R- timestamp(Q), TS(Ti)). 40
  • 41. TIMESTAMP-BASED PROTOCOLS (CONT.)  Suppose that transaction Ti issues write(Q). 1. If TS(Ti) < R-timestamp(Q), then the value of Q that Ti i d i d d i l d his producing was needed previously, and the system assumed that that value would never be produced.  Hence, the write operation is rejected, and Ti is rolled back. 2. If TS(Ti) < W-timestamp(Q), then Ti is attempting to write an obsolete value of Q.  Hence, this write operation is rejected, and Ti is rolled back. 3. Otherwise, the write operation is executed, and W- timestamp(Q) is set to TS(Ti). 41
  • 42.  T14  T15  Read (B)  Read (A) Di l (A B)  Read (B)  B:=B-50; W it (B) Display (A+B)  Write (B)  Read (A)  A:=A+50;;  Write (A) TS(T14) < TS(T15) 42
  • 43. SCHEDULE USING TIMESTAMP PROTOCOL T14 T15 Read (B) Read (B) B:=B-50; W it (B) Read (A) Display (A+B) Write (B) Read (A) p y ( ) A:=A+50; Write (A) 43
  • 44. CORRECTNESS OF TIMESTAMP-ORDERING PROTOCOL  Th ti t d i t l t The timestamp-ordering protocol guarantees serializability since all the arcs in the precedence graph are of the form: transaction ith ll transaction with smaller timestamp with larger timestamp Thus, there will be no cycles in the precedence graph  Timestamp protocol ensures freedom from deadlock Timestamp protocol ensures freedom from deadlock as no transaction ever waits.  But the schedule may not be cascade-free, and may not even be recoverablenot even be recoverable. 44
  • 45. RECOVERABILITY AND CASCADE FREEDOM  Solution 1:  A transaction is structured such that its writes are all performed at the end of its processingperformed at the end of its processing  All writes of a transaction form an atomic action; no transaction may execute while a transaction is being writtenwritten  A transaction that aborts is restarted with a new timestamp  Solution 2: Limited form of locking: wait for data to Solution 2: Limited form of locking: wait for data to be committed before reading it  Solution 3: Use commit dependencies to ensure bilitrecoverability 45
  • 46. THOMAS’ WRITE RULE T1 T2  TS(T1)<TS(T2) Read (Q) Write (Q) Write (Q)  As per timestamp protocol the rollback of T1 is required  T2 has already written Q T2 has already written Q and T1 is attempting to write one that will never to be read  Any Ti with TS(Ti)<TS(T2) Any Ti with TS(Ti)<TS(T2) that attempts read(Q) will be rolled back, since TS(Ti)<W-timestamp(Q) A Tj i h TS(Tj) TS(T2) Any Tj with TS(Tj)>TS(T2) will be allowed to read the value Q written by T2 rather than T1 46 Leads to a modified version of timestamp protocol
  • 47. THOMAS’ WRITE RULE  Modified version of the timestamp-ordering protocol  When Ti attempts to write data item Q, if TS(Ti) < W-timestamp(Q), then Ti is attempting to write an obsolete value of {Q}an obsolete value of {Q}.  Rather than rolling back Ti as the timestamp ordering protocol would have done, this {write} ti b i doperation can be ignored.  Results in a view equivalent schedule  Otherwise this protocol is the same as thep timestamp ordering protocol  Thomas' Write Rule allows greater potential concurrency 47
  • 48. RECOVERY SYSTEM  A computer system is subject to failure from ap y j variety of causes  In any failure data may be lost  So a good database system must take actions in advance to ensure that the atomicity and durability properties are preserveddurability properties are preserved  Recovery system is an integral part of a database system  The recovery system does the followings-  restore the database to a consistent state  provide high availability provide high availability 48
  • 49. FAILURE CLASSIFICATIONFAILURE CLASSIFICATION  Transaction failure :  Logical errors: transaction cannot complete due to some i t l ditiinternal error condition  System errors: the database system must terminate an active transaction due to an error condition (e.g., deadlock)  System crash: a power failure or other hardware or System crash: a power failure or other hardware or software failure causes the system to crash.  Fail-stop assumption: non-volatile storage contents are assumed not to be corrupted by system crashassumed not to be corrupted by system crash  Database systems have numerous integrity checks to prevent corruption of disk data  Disk failure: a head crash or similar disk failure destroys all or part of disk storage  Destruction is assumed to be detectable: disk drives use checksums to detect failures 49
  • 50. RECOVERY ALGORITHMS  Recovery algorithms are techniques to ensurey g q database consistency and transaction atomicity and durability despite failures R l ith h t t Recovery algorithms have two parts 1. Actions taken during normal transaction processing  to ensure enough information exists to recover from failures 2. Actions taken after a failure  to recover the database contents to a state that ensures atomicity, consistency and durability 50
  • 51. STORAGE STRUCTURE  Volatile storage:g  does not survive system crashes  examples: main memory, cache memory N l il Nonvolatile storage:  survives system crashes  examples: disk, tape, flash memory,examples: disk, tape, flash memory, non-volatile (battery backed up) RAM  Stable storage: hi l f f h i ll f il a mythical form of storage that survives all failures  approximated by maintaining multiple copies on distinct nonvolatile media 51
  • 52. RECOVERY AND ATOMICITY  Modifying the database without ensuring that the transaction will commit may leave the database in an inconsistent state.  Consider transaction Ti that transfers $50 from account A to account B; goal is either to perform allaccount A to account B; goal is either to perform all database modifications made by Ti or none at all.  Several output operations may be required for Ti (to output A and B). A failure may occur after one of these modifications have been made but before all of them are made.are made. 52
  • 53. RECOVERY AND ATOMICITY (CONT.)  To ensure atomicity despite failuresy p  we first output information describing the modifications to stable storage without modifying the database itselfdatabase itself.  We study the following approach:  log-based recovery  For simplicity, we assume that transactions run serially 53
  • 54. LOG-BASED RECOVERY  A log is kept on stable storage  The log is a sequence of records which record all the update activities in the databaseactivities in the database  The fields of a log record  Transaction identifier  Data item identifier  Old value  New value 54
  • 55. LOG BASED RECOVERY (CONTD )LOG-BASED RECOVERY (CONTD.)  Update log record: <T X V V > <Ti, Xj,V1,V2>  Transaction Ti has performed a write on data item Xj. Xj had old value V1 before the write, and will have valueV2 after the write.  Special log records to record significant events in Special log records to record significant events in transaction processing:  <Ti start> T ti T h t t d Transaction Ti has started  <Ti commit>  Transaction Ti has committed <T b t> <Ti abort>  Transaction Ti has aborted 55
  • 56.  We assume for now that log records are writteng directly to stable storage (that is, they are not buffered)  Two approaches using logs Two approaches using logs  Deferred database modification  Immediate database modification 56
  • 57. DEFERRED DATABASE MODIFICATION  The deferred database modification scheme records all modifications to the log, but defers all th it t ft ti l itthe writes to after partial commit.  Transaction starts by writing <Ti start> record to log.  A write(X) operation results in a log record <Ti, X, V> being written, where V is the new value for X  Note: old value is not needed for this schemeNote: old value is not needed for this scheme  The write is not performed on X at this time, but is deferred. Wh T ti ll it <T it> i When Ti partially commits, <Ti commit> is written to the log  Finally, the log records are read and used to actually execute the previously deferred writes. 57
  • 58. DEFERRED DATABASE MODIFICATION (CONT.)  During recovery after a crash, a transaction needs to be redone if and only if both <Ti start> and<Ti it th i th lcommit> are there in the log.  Redoing a transaction Ti ( redo Ti) sets the value of all data items updated by the transaction to the newall data items updated by the transaction to the new values.  redo operation must be idempotent  Executing it several times must be equivalent to executing it once  Crashes can occur while Crashes can occur while  the transaction is executing the original updates, or  while recovery action is being taken 58
  • 59.  Example transactions T0 and T1 (T0 executesp 0 1 ( 0 before T1): T0: read (A) T1 : read (C) A:= A - 50 C:=C- 100 write (A) write (C) d (B)read (B) B:= B + 50 write (B)write (B) 59
  • 60. DEFERRED DATABASE MODIFICATION (CONT.)  Below we show the log as it appears at three instants of time.  If log on stable storage at time of crash is as in case:g g (a) No redo actions need to be taken (b) redo(T0) must be performed since <T0 commit> is present (c) redo(T0) must be performed followed by redo(T1) since <T0 commit> and <T1 commit> are presentT0 commit and T1 commit are present 60
  • 61. IMMEDIATE DATABASE MODIFICATION  The immediate database modification scheme allows database updates of an uncommitted transaction to be made as the writes are issuedtransaction to be made as the writes are issued  since undoing may be needed, update logs must have both old value and new value  Update log record must be written before database Update log record must be written before database item is written  We assume that the log record is output directly to stable storageg  Can be extended to postpone log record output, so long as prior to execution of an output(B) operation for a data block B, all log records corresponding to items B must be flushed to stable storageflushed to stable storage  Output of updated blocks can take place at any time before or after transaction commit O d i hi h bl k t t b diff t Order in which blocks are output can be different from the order in which they are written. 61
  • 62. IMMEDIATE DATABASE MODIFICATION EXAMPLE Log Write Output <T0 start> <T0, A, 1000, 950> <To, B, 2000, 2050>o, , , A = 950 B = 2050 <T0 commit> <T start><T1 start> <T1, C, 700, 600> C = 600 BB, BC <T it><T1 commit> BA  Note: BX denotes block containing X. 62
  • 63. IMMEDIATE DATABASE MODIFICATION (CONT.)  Recovery procedure has two operations instead of one:  undo(Ti) restores the value of all data items updated by Ti to their old values going backwards from the last logto their old values, going backwards from the last log record for Ti  redo(Ti) sets the value of all data items updated by Ti to the new values going forward from the first log record forthe new values, going forward from the first log record for Ti  Both operations must be idempotent That is even if the operation is executed multiple times That is, even if the operation is executed multiple times the effect is the same as if it is executed once  Needed since operations may get re-executed during recovery 63
  • 64. IMMEDIATE DATABASE MODIFICATION (CONT.)( )  When recovering after failure:  Transaction Ti needs to be undone if the log containsa sac o i ee s o e o e e og co a s the record <Ti start>, but does not contain the record <Ti commit>.  Transaction Ti needs to be redone if the log contains both the record <Ti start> and the record <Ti commit>. 64
  • 65. IMMEDIATE DB MODIFICATION RECOVERY EXAMPLEEXAMPLE Below we show the log as it appears at three instants of time. Recovery actions in each case above are: (a) undo (T0): B is restored to 2000 and A to 1000. (b) d (T ) d d (T ) C i t d t 700 d th A d B(b) undo (T1) and redo (T0): C is restored to 700, and then A and B are set to 950 and 2050 respectively. (c) redo (T0) and redo (T1): A and B are set to 950 and 2050 respectively. Then C is set to 600respectively. Then C is set to 600 65
  • 66. COMMON PROBLEMS IN RECOVERY PROCEDURE  Searching the entire log is time-consuming  Unnecessarily redo transactions which have l d t t th i d t t th d t balready output their updates to the database 66
  • 67. CHECKPOINTING  Streamline recovery procedure by periodicallyy p y p y performing checkpointing  It performs the following sequence of operations- 1. Output all log records currently residing in main memory onto stable storage. 2. Output all modified buffer blocks to the disk. 3. Write a log record < checkpoint> onto stable storage  Transactions are not allowed to perform any Transactions are not allowed to perform any update actions such as writing to buffer block or writing a log record, while a checkpoint is in progress 67
  • 68. CHECKPOINTS (CONT.)  During recovery we need to consider only the most recent transaction Ti that started before the checkpoint, and transactions that started after Ti.checkpoint, and transactions that started after Ti. 1. Scan backwards from end of log to find the most recent <checkpoint> record 2 Continue scanning backwards till a record <Ti start>2. Continue scanning backwards till a record <Ti start> is found. 3. Need only to consider the part of log following above start record. Earlier part of log can be ignored duringp g g g recovery, and can be erased whenever desired. 4. For all transactions (starting from Ti or later) with no <Ti commit>, execute undo(Ti). (Done only in case of immediate modification.) 5. Scanning forward in the log, for all transactions starting from Ti or later with a <Ti commit>, t d (T )execute redo(Ti). 68
  • 69. EXAMPLE OF CHECKPOINTS Tc Tf T1T1 T2 T3 T4 f il  T1 can be ignored (updates already output to disk checkpoint system failure due to checkpoint)  T2 and T3 redone.  T undone T4 undone 69