Deadlocks in Database Systems: Prevention, Detection, and Recovery, Slides of Fundamentals of E-Commerce

Deadlocks in database systems, their causes, and various techniques for preventing, detecting, and recovering from deadlocks. Topics include the two-phase locking algorithm, serialization graphs, recoverability, and avoiding cascading aborts.

Typology: Slides

2012/2013

Uploaded on 07/30/2013

asif.ali
asif.ali 🇮🇳

5

(3)

129 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
1/11/05 1
3. C oncurrency C ontrol
for Transactions
Part One
Transaction Processing
1
/
11
/
05 2
Outline
1. A Simple System M odel
2. Serializab ility Theory
3. Synchronization R equirem ents
for R eco verability
4. Tw o-Ph ase Locking
5. Preserving Transaction Handshakes
6. Implem enting Two-P hase Locking
7. D eadlocks
1
/
11
/
05 3
3.1 A Simple System M odel
•Goal - Ensure serializable (SR) executions
•Implem entation technique -D elay operations
that w ould lead to non-SR results (e.g. se t locks
on sh ared data)
•For good perform ance minimize overhead and
delay from syn ch ronization operations
•First, well study how to get correct (SR) results
•Then, well study perform ance implications
(mostly in Part Tw o)
1
/
11
/
05 4
A ssu m p tion -A tom ic Operations
•We will synchronize Reads and Writes.
•We must therefore assu m e they’re atom ic
–else wed have to synchronize the finer-grained
operations that implem en t Read and Write
R ead (x) -returns the current value of x in the DB
•Write(x, val) overwrites allof x (the w hole page)
•This assu m p tion of atom ic operations is what
allow s us to ab stract executions as sequences of
reads and writes (without lo ss of information).
–Otherwise, what would wk[x] r
i
[x] mean?
•Also, co m m it (c
i
) and abort (a
i
) are atom ic
1
/
11
/
05 5
System M odel
Tran saction 1 Tran saction N
Start, Commit, Abort
Read(x), Write(x)
Data
Manager
Database
Tran saction 2
1
/
11
/
05 6
3.2 Serializability Theory
•The theory is based on m odeling executions as
histories, su ch as
H1= r
1[x] r
2[x] w 1[x] c
1w2[y] c
2
•First, characterize a concurrency control
algorithm by the properties of histories it allow s.
•Then prove that any history having these
properties is SR
•Why bother? It helps you understand why
concurrency control algorithm s work.
Docsity.com
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Deadlocks in Database Systems: Prevention, Detection, and Recovery and more Slides Fundamentals of E-Commerce in PDF only on Docsity!

1/11/ 05 1

3.Concurrency Control

forTransactions

PartO ne

Transaction Processing

1/11/ 05 2

O utline

1.A Sim ple System M odel 2.Serializability Theory 3.Synchronization Requirem ents forRecoverability 4.Tw o-Phase Locking 5.Preserving Transaction H andshakes 6.Im plem enting Tw o-Phase Locking 7.D eadlocks

1/11/ 05 3

3.1 A Sim ple System M odel

  • G oal-Ensure serializable (SR)executions
  • Im plem entation technique -D elay operations thatw ould lead to non-SR results (e.g.setlocks on shared data)
  • Forgood perform ance m inim ize overhead and delayfrom synchronization operations
  • First,w e’llstudy how to getcorrect(SR)results
  • Then,w e’llstudy perform ance im plications (m ostly in PartTw o)

1/11/ 05 4

A ssum ption -A tom ic O perations

  • W e w illsynchronize Reads and W rites.
  • W e m usttherefore assum e they’re atom ic
    • else w e’d have to synchronize the finer-grained operations thatim plem entRead and W rite
  • Read(x)-returns the currentvalue ofx in the D B
  • W rite(x,val)overw rites allofx (the whole page)
  • This assum ption ofatom ic operations is w hat allow s us to abstractexecutions as sequences of reads and w rites (w ithoutloss ofinform ation). - O therw ise,w hatw ould w (^) k[x]ri[x]m ean?
  • A lso,com m it(ci)and abort(ai)are atom ic

1/11/ 05 5

System M odel

Transaction 1 Transaction N

Start, Commit, Abort Read(x), Write(x)

D ata M anager

D atabase

Transaction 2

1/11/ 05 6

3.2 Serializability Theory

  • The theory is based on m odeling executions as histories,such as H 1 = r 1 [x]r 2 [x]w 1 [x]c 1 w 2 [y]c 2
  • First,characterize a concurrency control algorithm by the properties ofhistories itallow s.
  • Then prove thatany history having these properties is SR
  • W hy bother? Ithelps you understand w hy concurrency controlalgorithm s w ork.

1/11/ 05 7

Equivalence ofH istories

  • Tw o operations conflictiftheirexecution order affects theirreturn values orthe D B state. - a read and w rite on the sam e data item conflict - tw o w rites on the sam e data item conflict - tw o reads (on the sam e data item )do notconflict
  • Tw o histories are equivalent ifthey have the sam e operations and conflicting operations are in the sam e orderin both histories - because only the relative orderofconflicting operations can affectthe resultofthe histories

1/11/ 05 8

Exam ples ofEquivalence

  • The follow ing histories are equivalent H 1 = r 1 [x]r 2 [x]w 1 [x]c 1 w 2 [y]c 2 H 2 = r 2 [x]r 1 [x]w 1 [x]c 1 w 2 [y]c 2 H 3 = r 2 [x]r 1 [x]w 2 [y]c 2 w 1 [x]c 1 H 4 = r 2 [x]w 2 [y]c 2 r 1 [x]w 1 [x]c 1
  • Butnone ofthem are equivalentto H 5 = r 1 [x]w 1 [x]r 2 [x]c 1 w 2 [y]c 2 because r 2 [x]and w 1 [x]conflictand r 2 [x]precedes w 1 [x]in H 1 -H 4 ,but r 2 [x]follow s w 1 [x]in H 5.

1/11/ 05 9

Serializable H istories

  • A history is serializable ifitis equivalentto a serial history
  • Forexam ple,

H 1 = r 1 [x]r 2 [x]w 1 [x]c 1 w 2 [y]c 2 is equivalentto H 4 = r 2 [x]w 2 [y]c 2 r 1 [x]w 1 [x]c 1 (r 2 [x]and w 1 [x]are in the sam e orderin H 1 and H 4 .)

  • Therefore,H 1 is serializable.

1/11/ 05 10

A notherExam ple

  • H 6 = r 1 [x]r 2 [x]w 1 [x]r 3 [x]w 2 [y]w 3 [x]c 3 w 1 [y]c 1 c 2 is equivalentto a serialexecution ofT 2 T 1 T 3 , H 7 = r 2 [x]w 2 [y]c 2 r 1 [x]w 1 [x]w 1 [y]c 1 r 3 [x]w 3 [x]c 3
  • Each conflictim plies a constrainton any equivalent serialhistory:

H 6 = r 1 [x]r 2 [x]w 1 [x]r 3 [x]w 2 [y]w 3 [x]c 3 w 1 [y]c 1 c 2 T2fi T (^1) T1fi T (^3) T 2 fi T (^1)

T 2 fi T (^3)

1/11/ 05 11

Serialization G raphs

  • A serialization graph,SG (H ),forhistory H tells the effective execution orderoftransactions in H.
  • G iven history H ,SG (H )is a directed graph w hose nodes are the com m itted transactions and w hose edges are allT (^) ifi Tk such thatatleastone ofTi’s operations precedes and conflicts w ith atleastone ofTk’soperations

H 6 = r 1 [x]r 2 [x]w 1 [x]r 3 [x]w 2 [y]w 3 [x]c 3 w 1 [y]c 1 c 2

SG(H 6 )= T 2 fi T 1 fi T (^3)

1/11/ 05 12

The Serializability Theorem A history is SR ifand only ifSG (H )is acyclic. Proof:(if)SG (H )is acyclic.So letH (^) s be a serial history consistentw ith SG (H ).Each pairof conflicting ops in H induces an edge in SG (H ). Since conflicting ops in H (^) s and H are in the sam e order,H (^) s”H ,so H is SR. (only if)H is SR.LetH (^) s be a serialhistory equivalent to H .W e claim thatifT (^) ifi Tk in SG (H ),then Ti precedes Tk in H (^) s (else H (^) s ≢ H ).IfSG (H )had a cycle,T 1 fi T 2 fi … fi T (^) nfi T 1 ,then T 1 precedes T 1 in H (^) s,a contradiction.So SG (H )is acyclic.

1/11/ 05 19

3.4 Tw o-Phase Locking

  • Basic locking -Each transaction sets a lockon each data item before accessing the data - the lock is a reservation - there are read locks and w rite locks - if one transaction has a w rite lock on x,then no other transaction can have any lock on x
  • Exam ple
    • rli[x],rui[x],w li[x],w ui[x]denote lock/unlock operations
    • w l 1 [x]w 1 [x]rl 2 [x]r 2 [x]isim possible
    • w l 1 [x]w 1 [x]w u 1 [x]rl 2 [x]r 2 [x]isO K

1/11/ 05 20

Basic Locking Isn’tEnough

  • Basic locking doesn’tguarantee serializability
  • rl 1 [x]r 1 [x]ru 1 [x] w l 1 [y]w 1 [y]w u 1 [y]c 1

rl 2 [y]r 2 [y]w l 2 [x]w 2 [x]ru 2 [y]w u 2 [x]c 2

  • Elim inating the lock operations,w e have r 1 [x]r 2 [y]w 2 [x]c 2 w 1 [y]c 1 w hich isn’tSR
  • The problem is thatlocks aren’tbeing released properly.

1/11/ 05 21

Tw o-Phase Locking (2PL)Protocol

  • A transaction is two-phase locked if:
    • before reading x,itsets a read lock on x
    • before w riting x,itsets a w rite lock on x
    • itholds each lock untilafteritexecutes the corresponding operation
    • afterits firstunlock operation,itrequests no new locks
  • Each transaction sets locks during a growing phase and releases them during a shrinking phase.
  • Exam ple -on the previous page T 2 is tw o-phase locked,butnotT 1 since ru 1 [x]< w l 1 [y] - use “<” for“precedes” 1/11/ 05 22

2PL Theorem :Ifalltransactions in an execution are tw o-phase locked,then the execution is SR. Proof:D efine T (^) i⇒ T (^) k ifeither

  • T (^) i read x and Tk laterw rote x,or
  • T (^) i w rote x and T (^) k laterread orw rote x
  • IfT (^) i⇒ T (^) k,then T (^) i released a lock before T (^) k obtained som e lock.
  • IfT (^) i⇒ T (^) k ⇒ T (^) m ,then Ti released a lock before T (^) m obtained som e lock (because Tk is tw o-phase).
  • IfT (^) i⇒...⇒ T (^) i,then Ti released a lock before T (^) i obtained som e lock,breaking the 2-phase rule.
  • So there cannotbe a cycle.By the Serializability Theorem ,the execution is SR.

1/11/ 05 23

2PL and Recoverability

  • 2PL does notguarantee recoverability
  • This non-recoverable execution is 2-phase locked w l 1 [x]w 1 [x]w u 1 [x]rl 2 [x]r 2 [x]c 2 … c 1 - hence,itis notstrictand allow s cascading aborts
  • H ow ever,holding w rite locks untilaftercom m itor abortguarantees strictness - and hence avoids cascading aborts and is recoverable - In the above exam ple,T 1 m ustcom m itbefore its first unlock-w rite (w u 1 ):w l 1 [x]w 1 [x]c 1 w u 1 [x]rl 2 [x]r 2 [x]c 2

1/11/ 05 24

A utom ating Locking

  • 2PL can be hidden from the application
  • W hen a data m anagergets a Read orW rite operation from a transaction,itsets a read orw rite lock.
  • H ow does the data m anagerknow it’s safe to release locks (and be tw o-phase)?
  • O rdinarily,the data m anagerholds a transaction’s locks untilitcom m its oraborts.A data m anager - can release read locks afteritreceivescom m it - releases w ritelocks only afterprocessing com m it, to ensure strictness

1/11/ 05 25

3.5 Preserving Transaction H andshakes

  • Read and W rite are the only operations the system w illcontrolto attain serializability.
  • So,iftransactions com m unicate via m essages, then im plem entSendM sg as W rite,and ReceiveM sg as Read.
  • Else,you could have the follow ing: w 1 [x]r 2 [x]send 2 [M ]receive 1 [M ] - data m anagerdidn’tknow aboutsend/receive and thoughtthe execution w as SR.
  • A lso w atch outforbrain transport 1/11/ 05 26

Transactions Can Com m unicate via Brain

Transport

T1: Start

... Display output Commit

T2: Start Get input from display

... Commit

U serreads output … U serenters input

Brain transport

1/11/ 05 27

Brain Transport(cont’d)

  • Forpracticalpurposes,ifuserw aits forT 1 to com m itbefore starting T 2 ,then the data m anager can ignore brain transport.
  • This is called a transaction handshake (T 1 com m its before T 2 starts)
  • Reason -Locking preserves the orderim posed by transaction handshakes - e.g.,itserializes T 1 before T 2.

1/11/ 05 28

2PL Preserves Transaction H andshakes

  • 2PL serializes transactions (abbr.txns)consistent w ith alltransaction handshakes.I.e.there’s an equivalentserialexecution thatpreserves the transaction orderoftransaction handshakes
  • This isn’ttrue forarbitrary SR executions.E.g.
    • r 1 [x]w 2 [x]c 2 r 3 [y]c 3 w 1 [y]c 1
    • T 2 com m its before T 3 starts,butthe only equivalent serialexecution is T 3 T 1 T 2
    • rl 1 [x]r 1 [x]w l 1 [y]ru 1 [x]w l 2 [x]w 2 [x]w u 2 [x]c 2 butnow w e’re stuck,since w e can’tsetrl 3 [y])r 3 [y]. So the history cannotoccurusing 2PL.

1/11/ 05 29

2PL Preserves Transaction H andshakes (cont’d)

  • Stating this m ore form ally …
  • Theorem : Forany 2PL execution H , there is an equivalentserialexecution H (^) s, such thatforallT (^) i,Tk, ifTi com m itted before Tk started in H , then T (^) i precedes T (^) k in H (^) s.

1/11/ 05 30

Brain Transport O ne LastTim e

  • Ifa userreads com m itted displayed outputofTi and uses thatdisplayed outputas inputto transaction Tk,then he/she should w aitfor T (^) i to com m itbefore starting T (^) k.
  • The usercan then rely on transaction handshake preservation to ensure T (^) i is serialized before T (^) k.

1/11/ 05 37

Lock M anager(cont’d)

  • In M S SQ L Server
    • Locks are approx 32 bytes each.
    • Each lock contains a D atabase-ID ,O bject-Id,and other resource-specific lock inform ation such asrecord id (RID )orkey.
    • Each lock is attached to lock resource block (64 bytes) and lock ow nerblock (32 bytes)

1/11/ 05 38

Locking G ranularity

  • G ranularity -size ofdata item s to lock
    • e.g.,files,pages,records,fields
  • Coarse granularity im plies
    • very few locks,so little locking overhead
    • m ustlock large chunksofdata,so high chance of conflict,so concurrency m ay be low
  • Fine granularity im plies
    • m any locks,so high locking overhead
    • locking conflictoccurs only w hen tw o transactions try to access the exactsam e data concurrently
  • H igh perform ance TP requires record locking

1/11/ 05 39

M ultigranularity Locking (M G L)

  • A llow differenttxnsto lock atdifferentgranularity
    • big queries should lock coarse-grained data (e.g.tables)
    • shorttransactionslock fine-grained data (e.g.row s)
  • Lock m anagercan’tdetectthese conflicts
    • each data item (e.g.,table orrow )hasa differentid
  • M ultigranularity locking “trick”
    • exploitthe naturalhierarchy ofdata containm ent
    • before locking fine-grained data,setintention lockson coarse grained data thatcontains it
    • e.g.,before setting a read-lock on a row ,getan intention-read-lock on the table thatcontains the row
    • Intention-read-locks conflicts w ith a w rite lock 1/11/ 05 40

3.7 D eadlocks

  • A setoftransactions is deadlocked ifevery transaction in the setis blocked and w illrem ain blocked unless the system intervenes. - Exam ple rl 1 [x] granted rl 2 [y] granted w l 2 [x] blocked w l 1 [y] blocked and deadlocked
  • D eadlock is 2PL’s w ay to avoid non-SR executions
    • rl 1 [x]r 1 [x]rl 2 [y]r 2 [y]… can’trun w 2 [x]w 1 [y]and be SR
  • To repaira deadlock,you m ustaborta transaction
    • ifyou released a transaction’s lock w ithoutaborting it, you’d break 2PL

1/11/ 05 41

D eadlock Prevention

  • N evergranta lock thatcan lead to deadlock
  • O ften advocated in operating system s
  • U seless forTP,because itw ould require running transactions serially. - Exam ple to preventthe previous deadlock, rl 1 [x]rl 2 [y]w l 2 [x]w l 1 [y],the system can’tgrantrl 2 [y]
  • A voiding deadlock by resource ordering is unusable in general,since itoverly constrains applications. - Butm ay help forcertain high frequency deadlocks
  • Setting alllocks w hen txn begins requires too m uch advance know ledge and reduces concurrency. 1/11/ 05 42

D eadlock D etection

  • D etection approach:D etectdeadlocks autom atically, and aborta deadlocked transactions (the victim ).
  • It’s the preferred approach,because it
    • allow s higherresource utilization and
    • uses cheaperalgorithm s
  • Tim eout-based deadlock detection -Ifa transaction is blocked fortoo long,then abortit. - Sim ple and easy to im plem ent - Butaborts unnecessarily and - som e deadlocks persistfortoo long

1/11/ 05 43

D etection U sing W aits-ForG raph

  • Explicitdeadlock detection -U se a W aits-ForG raph
    • N odes = {transactions}
    • Edges = {T (^) ifi Tk |T (^) i is w aiting forT (^) k to release a lock}
    • Exam ple (previous deadlock) T 1 T
  • Theorem :Ifthere’s a deadlock,then the w aits-for graph has a cycle.

1/11/ 05 44

D etection U sing W aits-ForG raph (cont’d)

  • So,to find deadlocks
    • w hen a transaction blocks,add an edge to the graph
    • periodically check forcycles in the w aits-forgraph
  • N eed nottestfordeadlocks too often.(A cycle w on’tdisappearuntilyou detectitand break it.)
  • W hen a deadlock is detected,selecta victim from the cycle and abortit.
  • Selecta victim thathasn’tdone m uch w ork (e.g.,has setthe few estlocks).

1/11/ 05 45

Cyclic Restart

  • Transactions can cause each otherto abortforever.
    • T 1 starts running.Then T 2 starts running.
    • They deadlock and T 1 (the oldest)is aborted.
    • T 1 restarts,bum ps into T 2 and again deadlocks
    • T 2 (the oldest)is aborted ...
  • Choosing the youngestin a cycle as victim avoids cyclic restart,since the oldestrunning transaction is neverthe victim.
  • Can com bine w ith otherheuristics,e.g.few est-locks

1/11/ 05 46

M S SQ L Server

  • A borts the transaction thatis “cheapest” to roll back. - “Cheapest” is determ ined by the am ountoflog generated. - A llow s transactions thatyou’ve invested a lotin to com plete.
  • SET D EA D LO CK _PRIO RITY LO W (vs.N O RM A L)causes a transaction to sacrifice itselfas a victim.

1/11/ 05 47

D istributed Locking

  • Suppose a transaction can access data atm any data m anagers
  • Each data m anagersets locks in the usualw ay
  • W hen a transaction com m its oraborts,itruns tw o-phase com m itto notify alldata m anagers it accessed
  • The only rem aining issue is distributed deadlock

1/11/ 05 48

D istributed D eadlock

  • The deadlock spans tw o nodes. N eithernode alone can see it.
  • Tim eout-based detection is popular.Its w eaknesses are less im portantin the distributed case: - aborts unnecessarily and som e deadlocks persisttoo long - possibly abortyoungerunblocked transaction to avoid cyclic restart

rl 1 [x] w l 2 x

N ode 1 rl 2 [y] w l 1 y

N ode 2